Commit 9d1e5045 authored by jkrcal's avatar jkrcal Committed by Commit bot

Clear browsing data from ContentSuggestionService only if exists.

Currently, we instantiate the service in BrowsingDataRemover even when
it does not exist, yet. This means that we instantiate it on non-android
platforms, in unit-tests, etc.

This CL changes the logic so that the service is accessed only if it has
been created previously. This way, the BrowsingDataRemover respects the
start-up logic that decides whether to create the service or not.

BUG=647222

Review-Url: https://codereview.chromium.org/2342673004
Cr-Commit-Position: refs/heads/master@{#419141}
parent c00adf44
...@@ -522,7 +522,7 @@ void BrowsingDataRemover::RemoveImpl( ...@@ -522,7 +522,7 @@ void BrowsingDataRemover::RemoveImpl(
} }
ntp_snippets::ContentSuggestionsService* content_suggestions_service = ntp_snippets::ContentSuggestionsService* content_suggestions_service =
ContentSuggestionsServiceFactory::GetForProfile(profile_); ContentSuggestionsServiceFactory::GetForProfileIfExists(profile_);
if (content_suggestions_service) { if (content_suggestions_service) {
content_suggestions_service->ClearHistory(delete_begin_, delete_end_, content_suggestions_service->ClearHistory(delete_begin_, delete_end_,
filter); filter);
...@@ -1016,7 +1016,7 @@ void BrowsingDataRemover::RemoveImpl( ...@@ -1016,7 +1016,7 @@ void BrowsingDataRemover::RemoveImpl(
weak_ptr_factory_.GetWeakPtr())); weak_ptr_factory_.GetWeakPtr()));
ntp_snippets::ContentSuggestionsService* content_suggestions_service = ntp_snippets::ContentSuggestionsService* content_suggestions_service =
ContentSuggestionsServiceFactory::GetForProfile(profile_); ContentSuggestionsServiceFactory::GetForProfileIfExists(profile_);
if (content_suggestions_service) if (content_suggestions_service)
content_suggestions_service->ClearAllCachedSuggestions(); content_suggestions_service->ClearAllCachedSuggestions();
} }
......
...@@ -184,6 +184,13 @@ ContentSuggestionsService* ContentSuggestionsServiceFactory::GetForProfile( ...@@ -184,6 +184,13 @@ ContentSuggestionsService* ContentSuggestionsServiceFactory::GetForProfile(
GetInstance()->GetServiceForBrowserContext(profile, true)); GetInstance()->GetServiceForBrowserContext(profile, true));
} }
// static
ContentSuggestionsService*
ContentSuggestionsServiceFactory::GetForProfileIfExists(Profile* profile) {
return static_cast<ContentSuggestionsService*>(
GetInstance()->GetServiceForBrowserContext(profile, false));
}
ContentSuggestionsServiceFactory::ContentSuggestionsServiceFactory() ContentSuggestionsServiceFactory::ContentSuggestionsServiceFactory()
: BrowserContextKeyedServiceFactory( : BrowserContextKeyedServiceFactory(
"ContentSuggestionsService", "ContentSuggestionsService",
......
...@@ -27,6 +27,8 @@ class ContentSuggestionsServiceFactory ...@@ -27,6 +27,8 @@ class ContentSuggestionsServiceFactory
static ContentSuggestionsServiceFactory* GetInstance(); static ContentSuggestionsServiceFactory* GetInstance();
static ntp_snippets::ContentSuggestionsService* GetForProfile( static ntp_snippets::ContentSuggestionsService* GetForProfile(
Profile* profile); Profile* profile);
static ntp_snippets::ContentSuggestionsService* GetForProfileIfExists(
Profile* profile);
private: private:
friend struct base::DefaultSingletonTraits<ContentSuggestionsServiceFactory>; friend struct base::DefaultSingletonTraits<ContentSuggestionsServiceFactory>;
......
...@@ -978,6 +978,12 @@ void NTPSnippetsService::EnterState(State state) { ...@@ -978,6 +978,12 @@ void NTPSnippetsService::EnterState(State state) {
case State::READY: { case State::READY: {
DCHECK(state_ == State::NOT_INITED || state_ == State::DISABLED); DCHECK(state_ == State::NOT_INITED || state_ == State::DISABLED);
// TODO(jkrcal): fetching snippets automatically upon creation of this
// lazily created service can cause troubles, e.g. in unittests where
// network I/O is not allowed.
// Either add a DCHECK here that we actually are allowed to do network I/O
// or change the logic so that some explicit call is always needed for the
// network request.
bool fetch_snippets = bool fetch_snippets =
categories_[articles_category_].snippets.empty() || fetch_after_load_; categories_[articles_category_].snippets.empty() || fetch_after_load_;
DVLOG(1) << "Entering state: READY"; DVLOG(1) << "Entering state: READY";
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment