OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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/remote/scheduling_remote_suggestions_provider.
h" | 5 #include "components/ntp_snippets/remote/scheduling_remote_suggestions_provider.
h" |
6 | 6 |
7 #include <random> | 7 #include <random> |
8 #include <string> | 8 #include <string> |
9 #include <utility> | 9 #include <utility> |
10 | 10 |
(...skipping 168 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
179 PersistentScheduler* persistent_scheduler, | 179 PersistentScheduler* persistent_scheduler, |
180 const UserClassifier* user_classifier, | 180 const UserClassifier* user_classifier, |
181 PrefService* pref_service, | 181 PrefService* pref_service, |
182 std::unique_ptr<base::Clock> clock) | 182 std::unique_ptr<base::Clock> clock) |
183 : RemoteSuggestionsProvider(observer), | 183 : RemoteSuggestionsProvider(observer), |
184 RemoteSuggestionsScheduler(), | 184 RemoteSuggestionsScheduler(), |
185 provider_(std::move(provider)), | 185 provider_(std::move(provider)), |
186 persistent_scheduler_(persistent_scheduler), | 186 persistent_scheduler_(persistent_scheduler), |
187 background_fetch_in_progress_(false), | 187 background_fetch_in_progress_(false), |
188 user_classifier_(user_classifier), | 188 user_classifier_(user_classifier), |
| 189 request_throttler_rare_ntp_user_( |
| 190 pref_service, |
| 191 RequestThrottler::RequestType:: |
| 192 CONTENT_SUGGESTION_FETCHER_RARE_NTP_USER), |
| 193 request_throttler_active_ntp_user_( |
| 194 pref_service, |
| 195 RequestThrottler::RequestType:: |
| 196 CONTENT_SUGGESTION_FETCHER_ACTIVE_NTP_USER), |
| 197 request_throttler_active_suggestions_consumer_( |
| 198 pref_service, |
| 199 RequestThrottler::RequestType:: |
| 200 CONTENT_SUGGESTION_FETCHER_ACTIVE_SUGGESTIONS_CONSUMER), |
189 pref_service_(pref_service), | 201 pref_service_(pref_service), |
190 clock_(std::move(clock)), | 202 clock_(std::move(clock)), |
191 enabled_triggers_(GetEnabledTriggerTypes()) { | 203 enabled_triggers_(GetEnabledTriggerTypes()) { |
192 DCHECK(user_classifier); | 204 DCHECK(user_classifier); |
193 DCHECK(pref_service); | 205 DCHECK(pref_service); |
194 | 206 |
195 LoadLastFetchingSchedule(); | 207 LoadLastFetchingSchedule(); |
196 | 208 |
197 provider_->SetRemoteSuggestionsScheduler(this); | 209 provider_->SetRemoteSuggestionsScheduler(this); |
198 } | 210 } |
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
285 void SchedulingRemoteSuggestionsProvider::RefetchInTheBackground( | 297 void SchedulingRemoteSuggestionsProvider::RefetchInTheBackground( |
286 std::unique_ptr<FetchStatusCallback> callback) { | 298 std::unique_ptr<FetchStatusCallback> callback) { |
287 if (background_fetch_in_progress_) { | 299 if (background_fetch_in_progress_) { |
288 if (callback) { | 300 if (callback) { |
289 callback->Run( | 301 callback->Run( |
290 Status(StatusCode::TEMPORARY_ERROR, "Background fetch in progress")); | 302 Status(StatusCode::TEMPORARY_ERROR, "Background fetch in progress")); |
291 } | 303 } |
292 return; | 304 return; |
293 } | 305 } |
294 | 306 |
| 307 if (!DemandQuotaForRequest(/*interactive_request=*/false)) { |
| 308 if (callback) { |
| 309 callback->Run(Status(StatusCode::TEMPORARY_ERROR, |
| 310 "Non-interactive quota exceeded")); |
| 311 } |
| 312 return; |
| 313 } |
| 314 |
295 background_fetch_in_progress_ = true; | 315 background_fetch_in_progress_ = true; |
296 RemoteSuggestionsProvider::FetchStatusCallback wrapper_callback = base::Bind( | 316 RemoteSuggestionsProvider::FetchStatusCallback wrapper_callback = base::Bind( |
297 &SchedulingRemoteSuggestionsProvider::RefetchInTheBackgroundFinished, | 317 &SchedulingRemoteSuggestionsProvider::RefetchInTheBackgroundFinished, |
298 base::Unretained(this), base::Passed(&callback)); | 318 base::Unretained(this), base::Passed(&callback)); |
299 provider_->RefetchInTheBackground( | 319 provider_->RefetchInTheBackground( |
300 base::MakeUnique<RemoteSuggestionsProvider::FetchStatusCallback>( | 320 base::MakeUnique<RemoteSuggestionsProvider::FetchStatusCallback>( |
301 std::move(wrapper_callback))); | 321 std::move(wrapper_callback))); |
302 } | 322 } |
303 | 323 |
304 const RemoteSuggestionsFetcher* | 324 const RemoteSuggestionsFetcher* |
(...skipping 19 matching lines...) Expand all Loading... |
324 void SchedulingRemoteSuggestionsProvider::FetchSuggestionImage( | 344 void SchedulingRemoteSuggestionsProvider::FetchSuggestionImage( |
325 const ContentSuggestion::ID& suggestion_id, | 345 const ContentSuggestion::ID& suggestion_id, |
326 const ImageFetchedCallback& callback) { | 346 const ImageFetchedCallback& callback) { |
327 provider_->FetchSuggestionImage(suggestion_id, callback); | 347 provider_->FetchSuggestionImage(suggestion_id, callback); |
328 } | 348 } |
329 | 349 |
330 void SchedulingRemoteSuggestionsProvider::Fetch( | 350 void SchedulingRemoteSuggestionsProvider::Fetch( |
331 const Category& category, | 351 const Category& category, |
332 const std::set<std::string>& known_suggestion_ids, | 352 const std::set<std::string>& known_suggestion_ids, |
333 const FetchDoneCallback& callback) { | 353 const FetchDoneCallback& callback) { |
| 354 if (!DemandQuotaForRequest(/*interactive_request=*/true)) { |
| 355 if (callback) { |
| 356 callback.Run( |
| 357 Status(StatusCode::TEMPORARY_ERROR, "Interactive quota exceeded"), |
| 358 std::vector<ContentSuggestion>()); |
| 359 } |
| 360 return; |
| 361 } |
| 362 |
334 provider_->Fetch( | 363 provider_->Fetch( |
335 category, known_suggestion_ids, | 364 category, known_suggestion_ids, |
336 base::Bind(&SchedulingRemoteSuggestionsProvider::FetchFinished, | 365 base::Bind(&SchedulingRemoteSuggestionsProvider::FetchFinished, |
337 base::Unretained(this), callback)); | 366 base::Unretained(this), callback)); |
338 } | 367 } |
339 | 368 |
340 void SchedulingRemoteSuggestionsProvider::ReloadSuggestions() { | 369 void SchedulingRemoteSuggestionsProvider::ReloadSuggestions() { |
| 370 if (!DemandQuotaForRequest(/*interactive_request=*/true)) { |
| 371 return; |
| 372 } |
| 373 |
341 provider_->ReloadSuggestions(); | 374 provider_->ReloadSuggestions(); |
342 } | 375 } |
343 | 376 |
344 void SchedulingRemoteSuggestionsProvider::ClearHistory( | 377 void SchedulingRemoteSuggestionsProvider::ClearHistory( |
345 base::Time begin, | 378 base::Time begin, |
346 base::Time end, | 379 base::Time end, |
347 const base::Callback<bool(const GURL& url)>& filter) { | 380 const base::Callback<bool(const GURL& url)>& filter) { |
348 provider_->ClearHistory(begin, end, filter); | 381 provider_->ClearHistory(begin, end, filter); |
349 } | 382 } |
350 | 383 |
(...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
491 if (schedule_.is_empty()) { | 524 if (schedule_.is_empty()) { |
492 return true; // Background fetches are disabled in general. | 525 return true; // Background fetches are disabled in general. |
493 } | 526 } |
494 | 527 |
495 if (enabled_triggers_.count(trigger) == 0) { | 528 if (enabled_triggers_.count(trigger) == 0) { |
496 return true; // Background fetches for |trigger| are not enabled. | 529 return true; // Background fetches for |trigger| are not enabled. |
497 } | 530 } |
498 return false; | 531 return false; |
499 } | 532 } |
500 | 533 |
| 534 bool SchedulingRemoteSuggestionsProvider::DemandQuotaForRequest( |
| 535 bool interactive_request) { |
| 536 switch (user_classifier_->GetUserClass()) { |
| 537 case UserClassifier::UserClass::RARE_NTP_USER: |
| 538 return request_throttler_rare_ntp_user_.DemandQuotaForRequest( |
| 539 interactive_request); |
| 540 case UserClassifier::UserClass::ACTIVE_NTP_USER: |
| 541 return request_throttler_active_ntp_user_.DemandQuotaForRequest( |
| 542 interactive_request); |
| 543 case UserClassifier::UserClass::ACTIVE_SUGGESTIONS_CONSUMER: |
| 544 return request_throttler_active_suggestions_consumer_ |
| 545 .DemandQuotaForRequest(interactive_request); |
| 546 } |
| 547 NOTREACHED(); |
| 548 return false; |
| 549 } |
| 550 |
501 void SchedulingRemoteSuggestionsProvider::FetchFinished( | 551 void SchedulingRemoteSuggestionsProvider::FetchFinished( |
502 const FetchDoneCallback& callback, | 552 const FetchDoneCallback& callback, |
503 Status fetch_status, | 553 Status fetch_status, |
504 std::vector<ContentSuggestion> suggestions) { | 554 std::vector<ContentSuggestion> suggestions) { |
505 OnFetchCompleted(fetch_status); | 555 OnFetchCompleted(fetch_status); |
506 if (callback) { | 556 if (callback) { |
507 callback.Run(fetch_status, std::move(suggestions)); | 557 callback.Run(fetch_status, std::move(suggestions)); |
508 } | 558 } |
509 } | 559 } |
510 | 560 |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
576 return enabled_types; | 626 return enabled_types; |
577 } | 627 } |
578 | 628 |
579 std::set<SchedulingRemoteSuggestionsProvider::TriggerType> | 629 std::set<SchedulingRemoteSuggestionsProvider::TriggerType> |
580 SchedulingRemoteSuggestionsProvider::GetDefaultEnabledTriggerTypes() { | 630 SchedulingRemoteSuggestionsProvider::GetDefaultEnabledTriggerTypes() { |
581 return {TriggerType::PERSISTENT_SCHEDULER_WAKE_UP, TriggerType::NTP_OPENED, | 631 return {TriggerType::PERSISTENT_SCHEDULER_WAKE_UP, TriggerType::NTP_OPENED, |
582 TriggerType::BROWSER_FOREGROUNDED}; | 632 TriggerType::BROWSER_FOREGROUNDED}; |
583 } | 633 } |
584 | 634 |
585 } // namespace ntp_snippets | 635 } // namespace ntp_snippets |
OLD | NEW |