OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "components/ntp_snippets/ntp_snippets_service.h" | 5 #include "components/ntp_snippets/ntp_snippets_service.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <iterator> | 8 #include <iterator> |
9 #include <utility> | 9 #include <utility> |
10 | 10 |
(...skipping 229 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
240 registry->RegisterListPref(prefs::kSnippetHosts); | 240 registry->RegisterListPref(prefs::kSnippetHosts); |
241 | 241 |
242 NTPSnippetsStatusService::RegisterProfilePrefs(registry); | 242 NTPSnippetsStatusService::RegisterProfilePrefs(registry); |
243 } | 243 } |
244 | 244 |
245 // Inherited from KeyedService. | 245 // Inherited from KeyedService. |
246 void NTPSnippetsService::Shutdown() { | 246 void NTPSnippetsService::Shutdown() { |
247 EnterState(State::SHUT_DOWN, ContentSuggestionsCategoryStatus::NOT_PROVIDED); | 247 EnterState(State::SHUT_DOWN, ContentSuggestionsCategoryStatus::NOT_PROVIDED); |
248 } | 248 } |
249 | 249 |
250 void NTPSnippetsService::FetchSnippets() { | 250 void NTPSnippetsService::FetchSnippets(bool force_request) { |
251 if (ready()) | 251 if (ready()) |
252 FetchSnippetsFromHosts(GetSuggestionsHosts()); | 252 FetchSnippetsFromHosts(GetSuggestionsHosts(), force_request); |
253 else | 253 else |
254 fetch_after_load_ = true; | 254 fetch_after_load_ = true; |
255 } | 255 } |
256 | 256 |
257 void NTPSnippetsService::FetchSnippetsFromHosts( | 257 void NTPSnippetsService::FetchSnippetsFromHosts( |
258 const std::set<std::string>& hosts) { | 258 const std::set<std::string>& hosts, |
| 259 bool force_request) { |
259 if (!ready()) | 260 if (!ready()) |
260 return; | 261 return; |
261 snippets_fetcher_->FetchSnippetsFromHosts(hosts, application_language_code_, | 262 snippets_fetcher_->FetchSnippetsFromHosts(hosts, application_language_code_, |
262 kMaxSnippetCount); | 263 kMaxSnippetCount, force_request); |
263 } | 264 } |
264 | 265 |
265 void NTPSnippetsService::RescheduleFetching() { | 266 void NTPSnippetsService::RescheduleFetching() { |
266 // The scheduler only exists on Android so far, it's null on other platforms. | 267 // The scheduler only exists on Android so far, it's null on other platforms. |
267 if (!scheduler_) | 268 if (!scheduler_) |
268 return; | 269 return; |
269 | 270 |
270 if (ready()) { | 271 if (ready()) { |
271 base::Time now = base::Time::Now(); | 272 base::Time now = base::Time::Now(); |
272 scheduler_->Schedule( | 273 scheduler_->Schedule( |
(...skipping 159 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
432 to_delete.emplace_back(std::move(snippet)); | 433 to_delete.emplace_back(std::move(snippet)); |
433 } | 434 } |
434 Compact(&snippets_); | 435 Compact(&snippets_); |
435 // Then delete the removed snippets from the database. | 436 // Then delete the removed snippets from the database. |
436 database_->DeleteSnippets(to_delete); | 437 database_->DeleteSnippets(to_delete); |
437 | 438 |
438 StoreSnippetHostsToPrefs(hosts); | 439 StoreSnippetHostsToPrefs(hosts); |
439 | 440 |
440 NotifyNewSuggestions(); | 441 NotifyNewSuggestions(); |
441 | 442 |
442 FetchSnippetsFromHosts(hosts); | 443 FetchSnippetsFromHosts(hosts, /*force_request=*/false); |
443 } | 444 } |
444 | 445 |
445 void NTPSnippetsService::OnFetchFinished( | 446 void NTPSnippetsService::OnFetchFinished( |
446 NTPSnippetsFetcher::OptionalSnippets snippets) { | 447 NTPSnippetsFetcher::OptionalSnippets snippets) { |
447 if (!ready()) | 448 if (!ready()) |
448 return; | 449 return; |
449 | 450 |
450 if (snippets) { | 451 if (snippets) { |
451 // Sparse histogram used because the number of snippets is small (bound by | 452 // Sparse histogram used because the number of snippets is small (bound by |
452 // kMaxSnippetCount). | 453 // kMaxSnippetCount). |
(...skipping 192 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
645 return; | 646 return; |
646 } | 647 } |
647 | 648 |
648 const NTPSnippet& snippet = *it->get(); | 649 const NTPSnippet& snippet = *it->get(); |
649 image_fetcher_->StartOrQueueNetworkRequest( | 650 image_fetcher_->StartOrQueueNetworkRequest( |
650 snippet.id(), snippet.salient_image_url(), callback); | 651 snippet.id(), snippet.salient_image_url(), callback); |
651 } | 652 } |
652 | 653 |
653 void NTPSnippetsService::EnterStateEnabled(bool fetch_snippets) { | 654 void NTPSnippetsService::EnterStateEnabled(bool fetch_snippets) { |
654 if (fetch_snippets) | 655 if (fetch_snippets) |
655 FetchSnippets(); | 656 FetchSnippets(/*force_request=*/false); |
656 | 657 |
657 // If host restrictions are enabled, register for host list updates. | 658 // If host restrictions are enabled, register for host list updates. |
658 // |suggestions_service_| can be null in tests. | 659 // |suggestions_service_| can be null in tests. |
659 if (snippets_fetcher_->UsesHostRestrictions() && suggestions_service_) { | 660 if (snippets_fetcher_->UsesHostRestrictions() && suggestions_service_) { |
660 suggestions_service_subscription_ = | 661 suggestions_service_subscription_ = |
661 suggestions_service_->AddCallback(base::Bind( | 662 suggestions_service_->AddCallback(base::Bind( |
662 &NTPSnippetsService::OnSuggestionsChanged, base::Unretained(this))); | 663 &NTPSnippetsService::OnSuggestionsChanged, base::Unretained(this))); |
663 } | 664 } |
664 | 665 |
665 RescheduleFetching(); | 666 RescheduleFetching(); |
(...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
822 } | 823 } |
823 | 824 |
824 void NTPSnippetsService::NotifyCategoryStatusChanged() { | 825 void NTPSnippetsService::NotifyCategoryStatusChanged() { |
825 if (observer_) { | 826 if (observer_) { |
826 observer_->OnCategoryStatusChanged(ContentSuggestionsCategory::ARTICLES, | 827 observer_->OnCategoryStatusChanged(ContentSuggestionsCategory::ARTICLES, |
827 category_status_); | 828 category_status_); |
828 } | 829 } |
829 } | 830 } |
830 | 831 |
831 } // namespace ntp_snippets | 832 } // namespace ntp_snippets |
OLD | NEW |