Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(698)

Side by Side Diff: components/ntp_snippets/ntp_snippets_service.cc

Issue 2227973002: Add request throttler to thumbnail fetching for articles on mobile NTP (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 192 matching lines...) Expand 10 before | Expand all | Expand 10 after
203 suggestions_service_(suggestions_service), 203 suggestions_service_(suggestions_service),
204 application_language_code_(application_language_code), 204 application_language_code_(application_language_code),
205 scheduler_(scheduler), 205 scheduler_(scheduler),
206 snippets_fetcher_(std::move(snippets_fetcher)), 206 snippets_fetcher_(std::move(snippets_fetcher)),
207 image_fetcher_(std::move(image_fetcher)), 207 image_fetcher_(std::move(image_fetcher)),
208 image_decoder_(std::move(image_decoder)), 208 image_decoder_(std::move(image_decoder)),
209 database_(std::move(database)), 209 database_(std::move(database)),
210 snippets_status_service_(std::move(status_service)), 210 snippets_status_service_(std::move(status_service)),
211 fetch_after_load_(false), 211 fetch_after_load_(false),
212 provided_category_( 212 provided_category_(
213 category_factory->FromKnownCategory(KnownCategories::ARTICLES)) { 213 category_factory->FromKnownCategory(KnownCategories::ARTICLES)),
214 thumbnail_requests_throttler_(
215 pref_service,
216 RequestThrottler::RequestType::CONTENT_SUGGESTION_THUMBNAIL) {
214 if (database_->IsErrorState()) { 217 if (database_->IsErrorState()) {
215 EnterState(State::ERROR_OCCURRED, CategoryStatus::LOADING_ERROR); 218 EnterState(State::ERROR_OCCURRED, CategoryStatus::LOADING_ERROR);
216 return; 219 return;
217 } 220 }
218 221
219 database_->SetErrorCallback(base::Bind(&NTPSnippetsService::OnDatabaseError, 222 database_->SetErrorCallback(base::Bind(&NTPSnippetsService::OnDatabaseError,
220 base::Unretained(this))); 223 base::Unretained(this)));
221 224
222 // We transition to other states while finalizing the initialization, when the 225 // We transition to other states while finalizing the initialization, when the
223 // database is done loading. 226 // database is done loading.
224 database_->LoadSnippets(base::Bind(&NTPSnippetsService::OnDatabaseLoaded, 227 database_->LoadSnippets(base::Bind(&NTPSnippetsService::OnDatabaseLoaded,
225 base::Unretained(this))); 228 base::Unretained(this)));
226 } 229 }
227 230
228 NTPSnippetsService::~NTPSnippetsService() { 231 NTPSnippetsService::~NTPSnippetsService() {
229 } 232 }
230 233
231 // static 234 // static
232 void NTPSnippetsService::RegisterProfilePrefs(PrefRegistrySimple* registry) { 235 void NTPSnippetsService::RegisterProfilePrefs(PrefRegistrySimple* registry) {
233 registry->RegisterListPref(prefs::kSnippetHosts); 236 registry->RegisterListPref(prefs::kSnippetHosts);
234 237
235 NTPSnippetsStatusService::RegisterProfilePrefs(registry); 238 NTPSnippetsStatusService::RegisterProfilePrefs(registry);
236 } 239 }
237 240
238 void NTPSnippetsService::FetchSnippets(bool force_request) { 241 void NTPSnippetsService::FetchSnippets(bool interactive_request) {
239 if (ready()) 242 if (ready())
240 FetchSnippetsFromHosts(GetSuggestionsHosts(), force_request); 243 FetchSnippetsFromHosts(GetSuggestionsHosts(), interactive_request);
241 else 244 else
242 fetch_after_load_ = true; 245 fetch_after_load_ = true;
243 } 246 }
244 247
245 void NTPSnippetsService::FetchSnippetsFromHosts( 248 void NTPSnippetsService::FetchSnippetsFromHosts(
246 const std::set<std::string>& hosts, 249 const std::set<std::string>& hosts,
247 bool force_request) { 250 bool interactive_request) {
248 if (!ready()) 251 if (!ready())
249 return; 252 return;
250 253
251 if (snippets_.empty()) 254 if (snippets_.empty())
252 UpdateCategoryStatus(CategoryStatus::AVAILABLE_LOADING); 255 UpdateCategoryStatus(CategoryStatus::AVAILABLE_LOADING);
253 256
254 snippets_fetcher_->FetchSnippetsFromHosts(hosts, application_language_code_, 257 snippets_fetcher_->FetchSnippetsFromHosts(
255 kMaxSnippetCount, force_request); 258 hosts, application_language_code_, kMaxSnippetCount, interactive_request);
256 } 259 }
257 260
258 void NTPSnippetsService::RescheduleFetching() { 261 void NTPSnippetsService::RescheduleFetching() {
259 // The scheduler only exists on Android so far, it's null on other platforms. 262 // The scheduler only exists on Android so far, it's null on other platforms.
260 if (!scheduler_) 263 if (!scheduler_)
261 return; 264 return;
262 265
263 if (ready()) { 266 if (ready()) {
264 base::Time now = base::Time::Now(); 267 base::Time now = base::Time::Now();
265 scheduler_->Schedule( 268 scheduler_->Schedule(
(...skipping 360 matching lines...) Expand 10 before | Expand all | Expand 10 after
626 } 629 }
627 630
628 void NTPSnippetsService::FetchSnippetImageFromNetwork( 631 void NTPSnippetsService::FetchSnippetImageFromNetwork(
629 const std::string& snippet_id, 632 const std::string& snippet_id,
630 const ImageFetchedCallback& callback) { 633 const ImageFetchedCallback& callback) {
631 auto it = 634 auto it =
632 std::find_if(snippets_.begin(), snippets_.end(), 635 std::find_if(snippets_.begin(), snippets_.end(),
633 [&snippet_id](const std::unique_ptr<NTPSnippet>& snippet) { 636 [&snippet_id](const std::unique_ptr<NTPSnippet>& snippet) {
634 return snippet->id() == snippet_id; 637 return snippet->id() == snippet_id;
635 }); 638 });
636 if (it == snippets_.end()) { 639
637 callback.Run(MakeUniqueID(provided_category_, snippet_id), gfx::Image()); 640 if (it == snippets_.end() ||
641 !thumbnail_requests_throttler_.DemandQuotaForRequest(
642 /*interactive_request=*/true)) {
643 base::ThreadTaskRunnerHandle::Get()->PostTask(
644 FROM_HERE,
645 base::Bind(callback, it != snippets_.end()
Marc Treib 2016/08/09 14:04:53 I don't understand the snippet_id-building here -
jkrcal 2016/08/10 10:27:26 Huh, I just kept the code equivalent :) This was a
Marc Treib 2016/08/10 11:38:14 Interesting! How did the fetched images ever arriv
jkrcal 2016/08/10 11:57:03 I've updated the issue. Interestingly, the UI does
Marc Treib 2016/08/10 12:04:59 Ah, it binds a callback directly to the UI element
jkrcal 2016/08/10 16:22:27 Acknowledged.
tschumann 2016/08/11 11:09:40 Given that was quite a bug, is there a chance to p
646 ? snippet_id
647 : MakeUniqueID(provided_category_, snippet_id),
648 gfx::Image()));
638 return; 649 return;
639 } 650 }
640 651
641 const NTPSnippet& snippet = *it->get(); 652 const NTPSnippet& snippet = *it->get();
653
642 image_fetcher_->StartOrQueueNetworkRequest( 654 image_fetcher_->StartOrQueueNetworkRequest(
643 snippet.id(), snippet.salient_image_url(), callback); 655 snippet.id(), snippet.salient_image_url(), callback);
644 } 656 }
645 657
646 void NTPSnippetsService::EnterStateEnabled(bool fetch_snippets) { 658 void NTPSnippetsService::EnterStateEnabled(bool fetch_snippets) {
647 if (fetch_snippets) 659 if (fetch_snippets)
648 FetchSnippets(/*force_request=*/false); 660 FetchSnippets(/*force_request=*/false);
649 661
650 // FetchSnippets should set the status to |AVAILABLE_LOADING| if relevant, 662 // FetchSnippets should set the status to |AVAILABLE_LOADING| if relevant,
651 // otherwise we transition to |AVAILABLE| here. 663 // otherwise we transition to |AVAILABLE| here.
(...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after
780 void NTPSnippetsService::UpdateCategoryStatus(CategoryStatus status) { 792 void NTPSnippetsService::UpdateCategoryStatus(CategoryStatus status) {
781 if (status == category_status_) 793 if (status == category_status_)
782 return; 794 return;
783 795
784 category_status_ = status; 796 category_status_ = status;
785 observer()->OnCategoryStatusChanged(this, provided_category_, 797 observer()->OnCategoryStatusChanged(this, provided_category_,
786 category_status_); 798 category_status_);
787 } 799 }
788 800
789 } // namespace ntp_snippets 801 } // namespace ntp_snippets
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698