Chromium Code Reviews| Index: components/ntp_snippets/ntp_snippets_service.cc |
| diff --git a/components/ntp_snippets/ntp_snippets_service.cc b/components/ntp_snippets/ntp_snippets_service.cc |
| index 38c98f199319b6398624718deb225e539524eac1..0bd1f5f00b4e692531286e69a4783169d5f71463 100644 |
| --- a/components/ntp_snippets/ntp_snippets_service.cc |
| +++ b/components/ntp_snippets/ntp_snippets_service.cc |
| @@ -68,7 +68,7 @@ base::TimeDelta GetFetchingIntervalFallback() { |
| } |
| // Extracts the hosts from |suggestions| and returns them in a set. |
| -std::set<std::string> GetSuggestionsHosts( |
| +std::set<std::string> GetSuggestionsHostsImpl( |
| const SuggestionsProfile& suggestions) { |
| std::set<std::string> hosts; |
| for (int i = 0; i < suggestions.suggestions_size(); ++i) { |
| @@ -182,12 +182,39 @@ void NTPSnippetsService::Shutdown() { |
| } |
| void NTPSnippetsService::FetchSnippets() { |
| - // |suggestions_service_| can be null in tests. |
| - if (!suggestions_service_) |
| + FetchSnippetsFromHosts(GetSuggestionsHosts()); |
| +} |
| + |
| +void NTPSnippetsService::FetchSnippetsFromHosts( |
| + const std::set<std::string>& hosts) { |
| + if (base::CommandLine::ForCurrentProcess()->HasSwitch( |
| + switches::kDontRestrict)) { |
| + snippets_fetcher_->FetchSnippets(std::set<std::string>()); |
| return; |
| + } |
| + if (!hosts.empty()) |
| + snippets_fetcher_->FetchSnippets(hosts); |
| +} |
| + |
| +void NTPSnippetsService::ClearSnippets() { |
| + snippets_.clear(); |
| + |
| + StoreSnippetsToPrefs(); |
| + |
| + FOR_EACH_OBSERVER(NTPSnippetsServiceObserver, observers_, |
| + NTPSnippetsServiceLoaded()); |
| +} |
| + |
| +std::set<std::string> NTPSnippetsService::GetSuggestionsHosts() const { |
| + // |suggestions_service_| can be null in tests. |
| + if (!suggestions_service_) { |
| + std::set<std::string> empty; |
| + return empty; |
|
Marc Treib
2016/04/14 15:43:44
Just
return std::set<std::string>();
?
jkrcal
2016/04/14 16:41:01
Done.
|
| + } |
| - FetchSnippetsImpl(GetSuggestionsHosts( |
| - suggestions_service_->GetSuggestionsDataFromCache())); |
| + // TODO(treib) this should just call GetSnippetHostsFromPrefs |
| + return GetSuggestionsHostsImpl( |
| + suggestions_service_->GetSuggestionsDataFromCache()); |
| } |
| bool NTPSnippetsService::DiscardSnippet(const GURL& url) { |
| @@ -201,11 +228,21 @@ bool NTPSnippetsService::DiscardSnippet(const GURL& url) { |
| snippets_.erase(it); |
| StoreDiscardedSnippetsToPrefs(); |
| StoreSnippetsToPrefs(); |
| + FOR_EACH_OBSERVER(NTPSnippetsServiceObserver, observers_, |
| + NTPSnippetsServiceLoaded()); |
| return true; |
| } |
| +void NTPSnippetsService::ClearDiscardedSnippets() { |
| + discarded_snippets_.clear(); |
| + StoreDiscardedSnippetsToPrefs(); |
| + FetchSnippets(); |
| +} |
| + |
| void NTPSnippetsService::AddObserver(NTPSnippetsServiceObserver* observer) { |
| - observers_.AddObserver(observer); |
| + if (!observers_.HasObserver(observer)) { |
|
Marc Treib
2016/04/14 15:43:44
This shouldn't be here. It's the responsibility of
jkrcal
2016/04/14 16:41:01
Done.
|
| + observers_.AddObserver(observer); |
| + } |
| observer->NTPSnippetsServiceLoaded(); |
| } |
| @@ -215,7 +252,7 @@ void NTPSnippetsService::RemoveObserver(NTPSnippetsServiceObserver* observer) { |
| void NTPSnippetsService::OnSuggestionsChanged( |
| const SuggestionsProfile& suggestions) { |
| - std::set<std::string> hosts = GetSuggestionsHosts(suggestions); |
| + std::set<std::string> hosts = GetSuggestionsHostsImpl(suggestions); |
| if (hosts == GetSnippetHostsFromPrefs()) |
| return; |
| @@ -233,7 +270,7 @@ void NTPSnippetsService::OnSuggestionsChanged( |
| FOR_EACH_OBSERVER(NTPSnippetsServiceObserver, observers_, |
| NTPSnippetsServiceLoaded()); |
| - FetchSnippetsImpl(hosts); |
| + FetchSnippetsFromHosts(hosts); |
| } |
| void NTPSnippetsService::OnSnippetsDownloaded( |
| @@ -256,17 +293,6 @@ void NTPSnippetsService::OnJsonError(const std::string& snippets_json, |
| LOG(WARNING) << "Received invalid JSON (" << error << "): " << snippets_json; |
| } |
| -void NTPSnippetsService::FetchSnippetsImpl( |
| - const std::set<std::string>& hosts) { |
| - if (base::CommandLine::ForCurrentProcess()->HasSwitch( |
| - switches::kDontRestrict)) { |
| - snippets_fetcher_->FetchSnippets(std::set<std::string>()); |
| - return; |
| - } |
| - if (!hosts.empty()) |
| - snippets_fetcher_->FetchSnippets(hosts); |
| -} |
| - |
| bool NTPSnippetsService::LoadFromValue(const base::Value& value) { |
| const base::DictionaryValue* top_dict = nullptr; |
| if (!value.GetAsDictionary(&top_dict)) |