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 170 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
181 | 181 |
182 database_->SetErrorCallback(base::Bind(&NTPSnippetsService::OnDatabaseError, | 182 database_->SetErrorCallback(base::Bind(&NTPSnippetsService::OnDatabaseError, |
183 base::Unretained(this))); | 183 base::Unretained(this))); |
184 | 184 |
185 // We transition to other states while finalizing the initialization, when the | 185 // We transition to other states while finalizing the initialization, when the |
186 // database is done loading. | 186 // database is done loading. |
187 database_->LoadSnippets(base::Bind(&NTPSnippetsService::OnDatabaseLoaded, | 187 database_->LoadSnippets(base::Bind(&NTPSnippetsService::OnDatabaseLoaded, |
188 base::Unretained(this))); | 188 base::Unretained(this))); |
189 } | 189 } |
190 | 190 |
191 NTPSnippetsService::~NTPSnippetsService() { | 191 NTPSnippetsService::~NTPSnippetsService() = default; |
192 } | |
193 | 192 |
194 // static | 193 // static |
195 void NTPSnippetsService::RegisterProfilePrefs(PrefRegistrySimple* registry) { | 194 void NTPSnippetsService::RegisterProfilePrefs(PrefRegistrySimple* registry) { |
196 registry->RegisterListPref(prefs::kSnippetHosts); | 195 registry->RegisterListPref(prefs::kSnippetHosts); |
197 | 196 |
198 NTPSnippetsStatusService::RegisterProfilePrefs(registry); | 197 NTPSnippetsStatusService::RegisterProfilePrefs(registry); |
199 } | 198 } |
200 | 199 |
201 void NTPSnippetsService::FetchSnippets(bool interactive_request) { | 200 void NTPSnippetsService::FetchSnippets(bool interactive_request) { |
202 if (ready()) | 201 if (ready()) |
(...skipping 272 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
475 // Then delete the removed snippets from the database. | 474 // Then delete the removed snippets from the database. |
476 database_->DeleteSnippets(to_delete); | 475 database_->DeleteSnippets(to_delete); |
477 | 476 |
478 StoreSnippetHostsToPrefs(hosts); | 477 StoreSnippetHostsToPrefs(hosts); |
479 | 478 |
480 // We removed some suggestions, so we want to let the client know about that. | 479 // We removed some suggestions, so we want to let the client know about that. |
481 // The fetch might take a long time or not complete so we don't want to wait | 480 // The fetch might take a long time or not complete so we don't want to wait |
482 // for its callback. | 481 // for its callback. |
483 NotifyNewSuggestions(); | 482 NotifyNewSuggestions(); |
484 | 483 |
485 FetchSnippetsFromHosts(hosts, /*force_request=*/false); | 484 FetchSnippetsFromHosts(hosts, /*interactive_request=*/false); |
486 } | 485 } |
487 | 486 |
488 void NTPSnippetsService::OnFetchFinished( | 487 void NTPSnippetsService::OnFetchFinished( |
489 NTPSnippetsFetcher::OptionalSnippets snippets) { | 488 NTPSnippetsFetcher::OptionalSnippets snippets) { |
490 if (!ready()) | 489 if (!ready()) |
491 return; | 490 return; |
492 | 491 |
493 for (auto& item : categories_) { | 492 for (auto& item : categories_) { |
494 CategoryContent* content = &item.second; | 493 CategoryContent* content = &item.second; |
495 content->provided_by_server = false; | 494 content->provided_by_server = false; |
(...skipping 248 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
744 } | 743 } |
745 } | 744 } |
746 | 745 |
747 void NTPSnippetsService::OnSnippetImageFetchedFromDatabase( | 746 void NTPSnippetsService::OnSnippetImageFetchedFromDatabase( |
748 const ImageFetchedCallback& callback, | 747 const ImageFetchedCallback& callback, |
749 const std::string& suggestion_id, | 748 const std::string& suggestion_id, |
750 std::string data) { | 749 std::string data) { |
751 // |image_decoder_| is null in tests. | 750 // |image_decoder_| is null in tests. |
752 if (image_decoder_ && !data.empty()) { | 751 if (image_decoder_ && !data.empty()) { |
753 image_decoder_->DecodeImage( | 752 image_decoder_->DecodeImage( |
754 std::move(data), | 753 data, base::Bind(&NTPSnippetsService::OnSnippetImageDecodedFromDatabase, |
sfiera
2016/09/23 16:51:04
Because DecodeImage takes a const std::string&, so
Marc Treib
2016/09/23 16:53:06
Good point, I could have mentioned that one :)
| |
755 base::Bind(&NTPSnippetsService::OnSnippetImageDecodedFromDatabase, | 754 base::Unretained(this), callback, suggestion_id)); |
756 base::Unretained(this), callback, suggestion_id)); | |
757 return; | 755 return; |
758 } | 756 } |
759 | 757 |
760 // Fetching from the DB failed; start a network fetch. | 758 // Fetching from the DB failed; start a network fetch. |
761 FetchSnippetImageFromNetwork(suggestion_id, callback); | 759 FetchSnippetImageFromNetwork(suggestion_id, callback); |
762 } | 760 } |
763 | 761 |
764 void NTPSnippetsService::OnSnippetImageDecodedFromDatabase( | 762 void NTPSnippetsService::OnSnippetImageDecodedFromDatabase( |
765 const ImageFetchedCallback& callback, | 763 const ImageFetchedCallback& callback, |
766 const std::string& suggestion_id, | 764 const std::string& suggestion_id, |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
799 if (it == content.snippets.end() || | 797 if (it == content.snippets.end() || |
800 !thumbnail_requests_throttler_.DemandQuotaForRequest( | 798 !thumbnail_requests_throttler_.DemandQuotaForRequest( |
801 /*interactive_request=*/true)) { | 799 /*interactive_request=*/true)) { |
802 // Return an empty image. Directly, this is never synchronous with the | 800 // Return an empty image. Directly, this is never synchronous with the |
803 // original FetchSuggestionImage() call - an asynchronous database query has | 801 // original FetchSuggestionImage() call - an asynchronous database query has |
804 // happened in the meantime. | 802 // happened in the meantime. |
805 OnSnippetImageDecodedFromNetwork(callback, suggestion_id, gfx::Image()); | 803 OnSnippetImageDecodedFromNetwork(callback, suggestion_id, gfx::Image()); |
806 return; | 804 return; |
807 } | 805 } |
808 | 806 |
809 const NTPSnippet& snippet = *it->get(); | 807 const NTPSnippet& snippet = **it; |
810 | 808 |
811 // TODO(jkrcal): We probably should rename OnImageDataFetched() to | 809 // TODO(jkrcal): We probably should rename OnImageDataFetched() to |
812 // CacheImageData(). This would document that this is actually independent | 810 // CacheImageData(). This would document that this is actually independent |
813 // from the individual fetch-flow. | 811 // from the individual fetch-flow. |
814 // The image fetcher calls OnImageDataFetched() with the raw data (this object | 812 // The image fetcher calls OnImageDataFetched() with the raw data (this object |
815 // is an ImageFetcherDelegate) and then also | 813 // is an ImageFetcherDelegate) and then also |
816 // OnSnippetImageDecodedFromNetwork() after the raw data gets decoded. | 814 // OnSnippetImageDecodedFromNetwork() after the raw data gets decoded. |
817 image_fetcher_->StartOrQueueNetworkRequest( | 815 image_fetcher_->StartOrQueueNetworkRequest( |
818 suggestion_id, snippet.salient_image_url(), | 816 suggestion_id, snippet.salient_image_url(), |
819 base::Bind(&NTPSnippetsService::OnSnippetImageDecodedFromNetwork, | 817 base::Bind(&NTPSnippetsService::OnSnippetImageDecodedFromNetwork, |
(...skipping 13 matching lines...) Expand all Loading... | |
833 nuke_when_initialized_ = false; | 831 nuke_when_initialized_ = false; |
834 } | 832 } |
835 | 833 |
836 if (categories_[articles_category_].snippets.empty() || fetch_when_ready_) { | 834 if (categories_[articles_category_].snippets.empty() || fetch_when_ready_) { |
837 // TODO(jkrcal): Fetching snippets automatically upon creation of this | 835 // TODO(jkrcal): Fetching snippets automatically upon creation of this |
838 // lazily created service can cause troubles, e.g. in unit tests where | 836 // lazily created service can cause troubles, e.g. in unit tests where |
839 // network I/O is not allowed. | 837 // network I/O is not allowed. |
840 // Either add a DCHECK here that we actually are allowed to do network I/O | 838 // Either add a DCHECK here that we actually are allowed to do network I/O |
841 // or change the logic so that some explicit call is always needed for the | 839 // or change the logic so that some explicit call is always needed for the |
842 // network request. | 840 // network request. |
843 FetchSnippets(/*force_request=*/false); | 841 FetchSnippets(/*interactive_request=*/false); |
844 fetch_when_ready_ = false; | 842 fetch_when_ready_ = false; |
845 } | 843 } |
846 | 844 |
847 // FetchSnippets should set the status to |AVAILABLE_LOADING| if relevant, | 845 // FetchSnippets should set the status to |AVAILABLE_LOADING| if relevant, |
848 // otherwise we transition to |AVAILABLE| here. | 846 // otherwise we transition to |AVAILABLE| here. |
849 if (categories_[articles_category_].status != | 847 if (categories_[articles_category_].status != |
850 CategoryStatus::AVAILABLE_LOADING) { | 848 CategoryStatus::AVAILABLE_LOADING) { |
851 UpdateCategoryStatus(articles_category_, CategoryStatus::AVAILABLE); | 849 UpdateCategoryStatus(articles_category_, CategoryStatus::AVAILABLE); |
852 } | 850 } |
853 | 851 |
(...skipping 158 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1012 } | 1010 } |
1013 | 1011 |
1014 NTPSnippetsService::CategoryContent::CategoryContent() = default; | 1012 NTPSnippetsService::CategoryContent::CategoryContent() = default; |
1015 NTPSnippetsService::CategoryContent::CategoryContent(CategoryContent&&) = | 1013 NTPSnippetsService::CategoryContent::CategoryContent(CategoryContent&&) = |
1016 default; | 1014 default; |
1017 NTPSnippetsService::CategoryContent::~CategoryContent() = default; | 1015 NTPSnippetsService::CategoryContent::~CategoryContent() = default; |
1018 NTPSnippetsService::CategoryContent& NTPSnippetsService::CategoryContent:: | 1016 NTPSnippetsService::CategoryContent& NTPSnippetsService::CategoryContent:: |
1019 operator=(CategoryContent&&) = default; | 1017 operator=(CategoryContent&&) = default; |
1020 | 1018 |
1021 } // namespace ntp_snippets | 1019 } // namespace ntp_snippets |
OLD | NEW |