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