| 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 | 209 |
| 198 SchedulingRemoteSuggestionsProvider::~SchedulingRemoteSuggestionsProvider() = | 210 SchedulingRemoteSuggestionsProvider::~SchedulingRemoteSuggestionsProvider() = |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 276 void SchedulingRemoteSuggestionsProvider::RefetchInTheBackground( | 288 void SchedulingRemoteSuggestionsProvider::RefetchInTheBackground( |
| 277 std::unique_ptr<FetchStatusCallback> callback) { | 289 std::unique_ptr<FetchStatusCallback> callback) { |
| 278 if (background_fetch_in_progress_) { | 290 if (background_fetch_in_progress_) { |
| 279 if (callback) { | 291 if (callback) { |
| 280 callback->Run( | 292 callback->Run( |
| 281 Status(StatusCode::TEMPORARY_ERROR, "Background fetch in progress")); | 293 Status(StatusCode::TEMPORARY_ERROR, "Background fetch in progress")); |
| 282 } | 294 } |
| 283 return; | 295 return; |
| 284 } | 296 } |
| 285 | 297 |
| 298 if (!AcquireQuota(/*interactive_request=*/false)) { |
| 299 if (callback) { |
| 300 callback->Run(Status(StatusCode::TEMPORARY_ERROR, |
| 301 "Non-interactive quota exceeded")); |
| 302 } |
| 303 return; |
| 304 } |
| 305 |
| 286 background_fetch_in_progress_ = true; | 306 background_fetch_in_progress_ = true; |
| 287 RemoteSuggestionsProvider::FetchStatusCallback wrapper_callback = base::Bind( | 307 RemoteSuggestionsProvider::FetchStatusCallback wrapper_callback = base::Bind( |
| 288 &SchedulingRemoteSuggestionsProvider::RefetchInTheBackgroundFinished, | 308 &SchedulingRemoteSuggestionsProvider::RefetchInTheBackgroundFinished, |
| 289 base::Unretained(this), base::Passed(&callback)); | 309 base::Unretained(this), base::Passed(&callback)); |
| 290 provider_->RefetchInTheBackground( | 310 provider_->RefetchInTheBackground( |
| 291 base::MakeUnique<RemoteSuggestionsProvider::FetchStatusCallback>( | 311 base::MakeUnique<RemoteSuggestionsProvider::FetchStatusCallback>( |
| 292 std::move(wrapper_callback))); | 312 std::move(wrapper_callback))); |
| 293 } | 313 } |
| 294 | 314 |
| 295 const RemoteSuggestionsFetcher* | 315 const RemoteSuggestionsFetcher* |
| (...skipping 19 matching lines...) Expand all Loading... |
| 315 void SchedulingRemoteSuggestionsProvider::FetchSuggestionImage( | 335 void SchedulingRemoteSuggestionsProvider::FetchSuggestionImage( |
| 316 const ContentSuggestion::ID& suggestion_id, | 336 const ContentSuggestion::ID& suggestion_id, |
| 317 const ImageFetchedCallback& callback) { | 337 const ImageFetchedCallback& callback) { |
| 318 provider_->FetchSuggestionImage(suggestion_id, callback); | 338 provider_->FetchSuggestionImage(suggestion_id, callback); |
| 319 } | 339 } |
| 320 | 340 |
| 321 void SchedulingRemoteSuggestionsProvider::Fetch( | 341 void SchedulingRemoteSuggestionsProvider::Fetch( |
| 322 const Category& category, | 342 const Category& category, |
| 323 const std::set<std::string>& known_suggestion_ids, | 343 const std::set<std::string>& known_suggestion_ids, |
| 324 const FetchDoneCallback& callback) { | 344 const FetchDoneCallback& callback) { |
| 345 if (!AcquireQuota(/*interactive_request=*/true)) { |
| 346 if (callback) { |
| 347 callback.Run( |
| 348 Status(StatusCode::TEMPORARY_ERROR, "Interactive quota exceeded"), |
| 349 std::vector<ContentSuggestion>()); |
| 350 } |
| 351 return; |
| 352 } |
| 353 |
| 325 provider_->Fetch( | 354 provider_->Fetch( |
| 326 category, known_suggestion_ids, | 355 category, known_suggestion_ids, |
| 327 base::Bind(&SchedulingRemoteSuggestionsProvider::FetchFinished, | 356 base::Bind(&SchedulingRemoteSuggestionsProvider::FetchFinished, |
| 328 base::Unretained(this), callback)); | 357 base::Unretained(this), callback)); |
| 329 } | 358 } |
| 330 | 359 |
| 331 void SchedulingRemoteSuggestionsProvider::ReloadSuggestions() { | 360 void SchedulingRemoteSuggestionsProvider::ReloadSuggestions() { |
| 361 if (!AcquireQuota(/*interactive_request=*/true)) { |
| 362 return; |
| 363 } |
| 364 |
| 332 provider_->ReloadSuggestions(); | 365 provider_->ReloadSuggestions(); |
| 333 } | 366 } |
| 334 | 367 |
| 335 void SchedulingRemoteSuggestionsProvider::ClearHistory( | 368 void SchedulingRemoteSuggestionsProvider::ClearHistory( |
| 336 base::Time begin, | 369 base::Time begin, |
| 337 base::Time end, | 370 base::Time end, |
| 338 const base::Callback<bool(const GURL& url)>& filter) { | 371 const base::Callback<bool(const GURL& url)>& filter) { |
| 339 provider_->ClearHistory(begin, end, filter); | 372 provider_->ClearHistory(begin, end, filter); |
| 340 } | 373 } |
| 341 | 374 |
| (...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 482 if (schedule_.is_empty()) { | 515 if (schedule_.is_empty()) { |
| 483 return true; // Background fetches are disabled in general. | 516 return true; // Background fetches are disabled in general. |
| 484 } | 517 } |
| 485 | 518 |
| 486 if (enabled_triggers_.count(trigger) == 0) { | 519 if (enabled_triggers_.count(trigger) == 0) { |
| 487 return true; // Background fetches for |trigger| are not enabled. | 520 return true; // Background fetches for |trigger| are not enabled. |
| 488 } | 521 } |
| 489 return false; | 522 return false; |
| 490 } | 523 } |
| 491 | 524 |
| 525 bool SchedulingRemoteSuggestionsProvider::AcquireQuota( |
| 526 bool interactive_request) { |
| 527 switch (user_classifier_->GetUserClass()) { |
| 528 case UserClassifier::UserClass::RARE_NTP_USER: |
| 529 return request_throttler_rare_ntp_user_.DemandQuotaForRequest( |
| 530 interactive_request); |
| 531 case UserClassifier::UserClass::ACTIVE_NTP_USER: |
| 532 return request_throttler_active_ntp_user_.DemandQuotaForRequest( |
| 533 interactive_request); |
| 534 case UserClassifier::UserClass::ACTIVE_SUGGESTIONS_CONSUMER: |
| 535 return request_throttler_active_suggestions_consumer_ |
| 536 .DemandQuotaForRequest(interactive_request); |
| 537 } |
| 538 NOTREACHED(); |
| 539 return false; |
| 540 } |
| 541 |
| 492 void SchedulingRemoteSuggestionsProvider::FetchFinished( | 542 void SchedulingRemoteSuggestionsProvider::FetchFinished( |
| 493 const FetchDoneCallback& callback, | 543 const FetchDoneCallback& callback, |
| 494 Status fetch_status, | 544 Status fetch_status, |
| 495 std::vector<ContentSuggestion> suggestions) { | 545 std::vector<ContentSuggestion> suggestions) { |
| 496 OnFetchCompleted(fetch_status); | 546 OnFetchCompleted(fetch_status); |
| 497 if (callback) { | 547 if (callback) { |
| 498 callback.Run(fetch_status, std::move(suggestions)); | 548 callback.Run(fetch_status, std::move(suggestions)); |
| 499 } | 549 } |
| 500 } | 550 } |
| 501 | 551 |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 567 return enabled_types; | 617 return enabled_types; |
| 568 } | 618 } |
| 569 | 619 |
| 570 std::set<SchedulingRemoteSuggestionsProvider::TriggerType> | 620 std::set<SchedulingRemoteSuggestionsProvider::TriggerType> |
| 571 SchedulingRemoteSuggestionsProvider::GetDefaultEnabledTriggerTypes() { | 621 SchedulingRemoteSuggestionsProvider::GetDefaultEnabledTriggerTypes() { |
| 572 return {TriggerType::PERSISTENT_SCHEDULER_WAKE_UP, TriggerType::NTP_OPENED, | 622 return {TriggerType::PERSISTENT_SCHEDULER_WAKE_UP, TriggerType::NTP_OPENED, |
| 573 TriggerType::BROWSER_FOREGROUNDED}; | 623 TriggerType::BROWSER_FOREGROUNDED}; |
| 574 } | 624 } |
| 575 | 625 |
| 576 } // namespace ntp_snippets | 626 } // namespace ntp_snippets |
| OLD | NEW |