Chromium Code Reviews| 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/offline_pages/background/request_coordinator.h" | 5 #include "components/offline_pages/background/request_coordinator.h" |
| 6 | 6 |
| 7 #include <limits> | 7 #include <limits> |
| 8 #include <utility> | 8 #include <utility> |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| (...skipping 163 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 174 queue_(std::move(queue)), | 174 queue_(std::move(queue)), |
| 175 scheduler_(std::move(scheduler)), | 175 scheduler_(std::move(scheduler)), |
| 176 policy_controller_(new ClientPolicyController()), | 176 policy_controller_(new ClientPolicyController()), |
| 177 network_quality_estimator_(network_quality_estimator), | 177 network_quality_estimator_(network_quality_estimator), |
| 178 active_request_(nullptr), | 178 active_request_(nullptr), |
| 179 last_offlining_status_(Offliner::RequestStatus::UNKNOWN), | 179 last_offlining_status_(Offliner::RequestStatus::UNKNOWN), |
| 180 scheduler_callback_(base::Bind(&EmptySchedulerCallback)), | 180 scheduler_callback_(base::Bind(&EmptySchedulerCallback)), |
| 181 immediate_schedule_callback_(base::Bind(&EmptySchedulerCallback)), | 181 immediate_schedule_callback_(base::Bind(&EmptySchedulerCallback)), |
| 182 weak_ptr_factory_(this) { | 182 weak_ptr_factory_(this) { |
| 183 DCHECK(policy_ != nullptr); | 183 DCHECK(policy_ != nullptr); |
| 184 std::unique_ptr<PickRequestTaskFactory> picker_factory( | 184 std::unique_ptr<CleanupTaskFactory> cleanup_factory( |
| 185 new PickRequestTaskFactory(policy_.get(), this, &event_logger_)); | 185 new CleanupTaskFactory(policy_.get(), this, &event_logger_)); |
| 186 queue_->SetPickerFactory(std::move(picker_factory)); | 186 queue_->SetCleanupFactory(std::move(cleanup_factory)); |
| 187 // Do a cleanup at startup time. | |
| 188 queue_->CleanupRequestQueue(); | |
| 187 } | 189 } |
| 188 | 190 |
| 189 RequestCoordinator::~RequestCoordinator() {} | 191 RequestCoordinator::~RequestCoordinator() {} |
| 190 | 192 |
| 191 int64_t RequestCoordinator::SavePageLater(const GURL& url, | 193 int64_t RequestCoordinator::SavePageLater(const GURL& url, |
| 192 const ClientId& client_id, | 194 const ClientId& client_id, |
| 193 bool user_requested, | 195 bool user_requested, |
| 194 RequestAvailability availability) { | 196 RequestAvailability availability) { |
| 195 DVLOG(2) << "URL is " << url << " " << __func__; | 197 DVLOG(2) << "URL is " << url << " " << __func__; |
| 196 | 198 |
| (...skipping 411 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 608 // TODO: Make sure the scheduler callback is valid before running it. | 610 // TODO: Make sure the scheduler callback is valid before running it. |
| 609 scheduler_callback_.Run(true); | 611 scheduler_callback_.Run(true); |
| 610 DVLOG(2) << " out of time, giving up. " << __func__; | 612 DVLOG(2) << " out of time, giving up. " << __func__; |
| 611 | 613 |
| 612 return; | 614 return; |
| 613 } | 615 } |
| 614 | 616 |
| 615 // Ask request queue to make a new PickRequestTask object, then put it on the | 617 // Ask request queue to make a new PickRequestTask object, then put it on the |
| 616 // task queue. | 618 // task queue. |
| 617 queue_->PickNextRequest( | 619 queue_->PickNextRequest( |
| 618 base::Bind(&RequestCoordinator::RequestPicked, | 620 policy_.get(), base::Bind(&RequestCoordinator::RequestPicked, |
| 619 weak_ptr_factory_.GetWeakPtr()), | 621 weak_ptr_factory_.GetWeakPtr()), |
| 620 base::Bind(&RequestCoordinator::RequestNotPicked, | 622 base::Bind(&RequestCoordinator::RequestNotPicked, |
| 621 weak_ptr_factory_.GetWeakPtr()), | 623 weak_ptr_factory_.GetWeakPtr()), |
| 622 base::Bind(&RequestCoordinator::RequestCounts, | 624 base::Bind(&RequestCoordinator::RequestCounts, |
| 623 weak_ptr_factory_.GetWeakPtr(), is_start_of_processing), | 625 weak_ptr_factory_.GetWeakPtr(), is_start_of_processing), |
| 624 *current_conditions_.get(), disabled_requests_); | 626 *current_conditions_.get(), disabled_requests_); |
| 625 // TODO(petewil): Verify current_conditions has a good value on all calling | 627 // TODO(petewil): Verify current_conditions has a good value on all calling |
| 626 // paths. It is really more of a "last known conditions" than "current | 628 // paths. It is really more of a "last known conditions" than "current |
| 627 // conditions". Consider having a call to Java to check the current | 629 // conditions". Consider having a call to Java to check the current |
| 628 // conditions. | 630 // conditions. |
| 629 } | 631 } |
| 630 | 632 |
| 631 // Called by the request picker when a request has been picked. | 633 // Called by the request picker when a request has been picked. |
| 632 void RequestCoordinator::RequestPicked(const SavePageRequest& request) { | 634 void RequestCoordinator::RequestPicked(const SavePageRequest& request, |
| 635 const bool cleanup_needed) { | |
|
fgorski
2016/12/02 21:44:04
const bool -> bool here as well.
Pete Williamson
2016/12/05 20:39:45
Done.
| |
| 633 DVLOG(2) << request.url() << " " << __func__; | 636 DVLOG(2) << request.url() << " " << __func__; |
| 634 is_starting_ = false; | 637 is_starting_ = false; |
| 635 | 638 |
| 636 // Make sure we were not stopped while picking. | 639 // Make sure we were not stopped while picking. |
| 637 if (processing_state_ != ProcessingWindowState::STOPPED) { | 640 if (processing_state_ != ProcessingWindowState::STOPPED) { |
| 638 // Send the request on to the offliner. | 641 // Send the request on to the offliner. |
| 639 SendRequestToOffliner(request); | 642 SendRequestToOffliner(request); |
| 640 } | 643 } |
| 644 | |
| 645 // Schedule a queue cleanup if needed. | |
| 646 // TODO(petewil): What if the request we chose expires before calling cleanup? | |
|
dougarnett
2016/12/02 17:20:27
Might consider adding some delta over expiration t
Pete Williamson
2016/12/05 20:39:45
Instead, to be sure, I pass in the request_id we s
| |
| 647 if (cleanup_needed) | |
| 648 queue_->CleanupRequestQueue(); | |
| 641 } | 649 } |
| 642 | 650 |
| 643 void RequestCoordinator::RequestNotPicked( | 651 void RequestCoordinator::RequestNotPicked( |
| 644 bool non_user_requested_tasks_remaining) { | 652 bool non_user_requested_tasks_remaining, |
| 653 const bool cleanup_needed) { | |
|
fgorski
2016/12/02 21:44:04
const bool -> bool
Pete Williamson
2016/12/05 20:39:45
Done.
| |
| 645 DVLOG(2) << __func__; | 654 DVLOG(2) << __func__; |
| 646 is_starting_ = false; | 655 is_starting_ = false; |
| 647 | 656 |
| 648 // Clear the outstanding "safety" task in the scheduler. | 657 // Clear the outstanding "safety" task in the scheduler. |
| 649 scheduler_->Unschedule(); | 658 scheduler_->Unschedule(); |
| 650 | 659 |
| 651 // If disabled tasks remain, post a new safety task for 5 sec from now. | 660 // If disabled tasks remain, post a new safety task for 5 sec from now. |
| 652 if (disabled_requests_.size() > 0) { | 661 if (disabled_requests_.size() > 0) { |
| 653 scheduler_->BackupSchedule(GetTriggerConditions(kUserRequest), | 662 scheduler_->BackupSchedule(GetTriggerConditions(kUserRequest), |
| 654 kDisabledTaskRecheckSeconds); | 663 kDisabledTaskRecheckSeconds); |
| 655 } else if (non_user_requested_tasks_remaining) { | 664 } else if (non_user_requested_tasks_remaining) { |
| 656 // If we don't have any of those, check for non-user-requested tasks. | 665 // If we don't have any of those, check for non-user-requested tasks. |
| 657 scheduler_->Schedule(GetTriggerConditions(!kUserRequest)); | 666 scheduler_->Schedule(GetTriggerConditions(!kUserRequest)); |
| 658 } | 667 } |
| 659 | 668 |
| 669 // Schedule a queue cleanup if needed. | |
| 670 if (cleanup_needed) | |
| 671 queue_->CleanupRequestQueue(); | |
| 672 | |
| 660 // Let the scheduler know we are done processing. | 673 // Let the scheduler know we are done processing. |
| 661 scheduler_callback_.Run(true); | 674 scheduler_callback_.Run(true); |
| 662 } | 675 } |
| 663 | 676 |
| 664 void RequestCoordinator::RequestCounts(bool is_start_of_processing, | 677 void RequestCoordinator::RequestCounts(bool is_start_of_processing, |
| 665 size_t total_requests, | 678 size_t total_requests, |
| 666 size_t available_requests) { | 679 size_t available_requests) { |
| 667 // Only capture request counts for the start of processing (not for | 680 // Only capture request counts for the start of processing (not for |
| 668 // continued processing in the same window). | 681 // continued processing in the same window). |
| 669 if (!is_start_of_processing) | 682 if (!is_start_of_processing) |
| (...skipping 286 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 956 | 969 |
| 957 ClientPolicyController* RequestCoordinator::GetPolicyController() { | 970 ClientPolicyController* RequestCoordinator::GetPolicyController() { |
| 958 return policy_controller_.get(); | 971 return policy_controller_.get(); |
| 959 } | 972 } |
| 960 | 973 |
| 961 void RequestCoordinator::Shutdown() { | 974 void RequestCoordinator::Shutdown() { |
| 962 network_quality_estimator_ = nullptr; | 975 network_quality_estimator_ = nullptr; |
| 963 } | 976 } |
| 964 | 977 |
| 965 } // namespace offline_pages | 978 } // namespace offline_pages |
| OLD | NEW |