| 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 208 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 219 | 219 |
| 220 database_->SetErrorCallback(base::Bind(&NTPSnippetsService::OnDatabaseError, | 220 database_->SetErrorCallback(base::Bind(&NTPSnippetsService::OnDatabaseError, |
| 221 base::Unretained(this))); | 221 base::Unretained(this))); |
| 222 | 222 |
| 223 // We transition to other states while finalizing the initialization, when the | 223 // We transition to other states while finalizing the initialization, when the |
| 224 // database is done loading. | 224 // database is done loading. |
| 225 database_->LoadSnippets(base::Bind(&NTPSnippetsService::OnDatabaseLoaded, | 225 database_->LoadSnippets(base::Bind(&NTPSnippetsService::OnDatabaseLoaded, |
| 226 base::Unretained(this))); | 226 base::Unretained(this))); |
| 227 } | 227 } |
| 228 | 228 |
| 229 NTPSnippetsService::~NTPSnippetsService() { | 229 NTPSnippetsService::~NTPSnippetsService() = default; |
| 230 } | |
| 231 | 230 |
| 232 // static | 231 // static |
| 233 void NTPSnippetsService::RegisterProfilePrefs(PrefRegistrySimple* registry) { | 232 void NTPSnippetsService::RegisterProfilePrefs(PrefRegistrySimple* registry) { |
| 234 registry->RegisterListPref(prefs::kSnippetHosts); | 233 registry->RegisterListPref(prefs::kSnippetHosts); |
| 235 | 234 |
| 236 NTPSnippetsStatusService::RegisterProfilePrefs(registry); | 235 NTPSnippetsStatusService::RegisterProfilePrefs(registry); |
| 237 } | 236 } |
| 238 | 237 |
| 239 void NTPSnippetsService::FetchSnippets(bool interactive_request) { | 238 void NTPSnippetsService::FetchSnippets(bool interactive_request) { |
| 240 if (ready()) | 239 if (ready()) |
| (...skipping 295 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 536 Compact(&content->snippets); | 535 Compact(&content->snippets); |
| 537 ArchiveSnippets(articles_category_, &to_delete); | 536 ArchiveSnippets(articles_category_, &to_delete); |
| 538 | 537 |
| 539 StoreSnippetHostsToPrefs(hosts); | 538 StoreSnippetHostsToPrefs(hosts); |
| 540 | 539 |
| 541 // We removed some suggestions, so we want to let the client know about that. | 540 // We removed some suggestions, so we want to let the client know about that. |
| 542 // The fetch might take a long time or not complete so we don't want to wait | 541 // The fetch might take a long time or not complete so we don't want to wait |
| 543 // for its callback. | 542 // for its callback. |
| 544 NotifyNewSuggestions(); | 543 NotifyNewSuggestions(); |
| 545 | 544 |
| 546 FetchSnippetsFromHosts(hosts, /*force_request=*/false); | 545 FetchSnippetsFromHosts(hosts, /*interactive_request=*/false); |
| 547 } | 546 } |
| 548 | 547 |
| 549 void NTPSnippetsService::OnFetchFinished( | 548 void NTPSnippetsService::OnFetchFinished( |
| 550 NTPSnippetsFetcher::OptionalSnippets snippets) { | 549 NTPSnippetsFetcher::OptionalSnippets snippets) { |
| 551 if (!ready()) | 550 if (!ready()) |
| 552 return; | 551 return; |
| 553 | 552 |
| 554 for (auto& item : categories_) { | 553 for (auto& item : categories_) { |
| 555 CategoryContent* content = &item.second; | 554 CategoryContent* content = &item.second; |
| 556 content->provided_by_server = false; | 555 content->provided_by_server = false; |
| (...skipping 240 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 797 } | 796 } |
| 798 } | 797 } |
| 799 | 798 |
| 800 void NTPSnippetsService::OnSnippetImageFetchedFromDatabase( | 799 void NTPSnippetsService::OnSnippetImageFetchedFromDatabase( |
| 801 const ImageFetchedCallback& callback, | 800 const ImageFetchedCallback& callback, |
| 802 const std::string& suggestion_id, | 801 const std::string& suggestion_id, |
| 803 std::string data) { | 802 std::string data) { |
| 804 // |image_decoder_| is null in tests. | 803 // |image_decoder_| is null in tests. |
| 805 if (image_decoder_ && !data.empty()) { | 804 if (image_decoder_ && !data.empty()) { |
| 806 image_decoder_->DecodeImage( | 805 image_decoder_->DecodeImage( |
| 807 std::move(data), | 806 data, base::Bind(&NTPSnippetsService::OnSnippetImageDecodedFromDatabase, |
| 808 base::Bind(&NTPSnippetsService::OnSnippetImageDecodedFromDatabase, | 807 base::Unretained(this), callback, suggestion_id)); |
| 809 base::Unretained(this), callback, suggestion_id)); | |
| 810 return; | 808 return; |
| 811 } | 809 } |
| 812 | 810 |
| 813 // Fetching from the DB failed; start a network fetch. | 811 // Fetching from the DB failed; start a network fetch. |
| 814 FetchSnippetImageFromNetwork(suggestion_id, callback); | 812 FetchSnippetImageFromNetwork(suggestion_id, callback); |
| 815 } | 813 } |
| 816 | 814 |
| 817 void NTPSnippetsService::OnSnippetImageDecodedFromDatabase( | 815 void NTPSnippetsService::OnSnippetImageDecodedFromDatabase( |
| 818 const ImageFetchedCallback& callback, | 816 const ImageFetchedCallback& callback, |
| 819 const std::string& suggestion_id, | 817 const std::string& suggestion_id, |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 872 nuke_when_initialized_ = false; | 870 nuke_when_initialized_ = false; |
| 873 } | 871 } |
| 874 | 872 |
| 875 if (categories_[articles_category_].snippets.empty() || fetch_when_ready_) { | 873 if (categories_[articles_category_].snippets.empty() || fetch_when_ready_) { |
| 876 // TODO(jkrcal): Fetching snippets automatically upon creation of this | 874 // TODO(jkrcal): Fetching snippets automatically upon creation of this |
| 877 // lazily created service can cause troubles, e.g. in unit tests where | 875 // lazily created service can cause troubles, e.g. in unit tests where |
| 878 // network I/O is not allowed. | 876 // network I/O is not allowed. |
| 879 // Either add a DCHECK here that we actually are allowed to do network I/O | 877 // Either add a DCHECK here that we actually are allowed to do network I/O |
| 880 // or change the logic so that some explicit call is always needed for the | 878 // or change the logic so that some explicit call is always needed for the |
| 881 // network request. | 879 // network request. |
| 882 FetchSnippets(/*force_request=*/false); | 880 FetchSnippets(/*interactive_request=*/false); |
| 883 fetch_when_ready_ = false; | 881 fetch_when_ready_ = false; |
| 884 } | 882 } |
| 885 | 883 |
| 886 // FetchSnippets should set the status to |AVAILABLE_LOADING| if relevant, | 884 // FetchSnippets should set the status to |AVAILABLE_LOADING| if relevant, |
| 887 // otherwise we transition to |AVAILABLE| here. | 885 // otherwise we transition to |AVAILABLE| here. |
| 888 if (categories_[articles_category_].status != | 886 if (categories_[articles_category_].status != |
| 889 CategoryStatus::AVAILABLE_LOADING) { | 887 CategoryStatus::AVAILABLE_LOADING) { |
| 890 UpdateCategoryStatus(articles_category_, CategoryStatus::AVAILABLE); | 888 UpdateCategoryStatus(articles_category_, CategoryStatus::AVAILABLE); |
| 891 } | 889 } |
| 892 | 890 |
| (...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1049 } | 1047 } |
| 1050 | 1048 |
| 1051 NTPSnippetsService::CategoryContent::CategoryContent() = default; | 1049 NTPSnippetsService::CategoryContent::CategoryContent() = default; |
| 1052 NTPSnippetsService::CategoryContent::CategoryContent(CategoryContent&&) = | 1050 NTPSnippetsService::CategoryContent::CategoryContent(CategoryContent&&) = |
| 1053 default; | 1051 default; |
| 1054 NTPSnippetsService::CategoryContent::~CategoryContent() = default; | 1052 NTPSnippetsService::CategoryContent::~CategoryContent() = default; |
| 1055 NTPSnippetsService::CategoryContent& NTPSnippetsService::CategoryContent:: | 1053 NTPSnippetsService::CategoryContent& NTPSnippetsService::CategoryContent:: |
| 1056 operator=(CategoryContent&&) = default; | 1054 operator=(CategoryContent&&) = default; |
| 1057 | 1055 |
| 1058 } // namespace ntp_snippets | 1056 } // namespace ntp_snippets |
| OLD | NEW |