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

Side by Side Diff: components/offline_pages/background/request_coordinator.cc

Issue 2543093002: Split the RequestPicker task into two separate tasks. (Closed)
Patch Set: CR feedback per FGorski and DougArnett Created 4 years 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 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 13 matching lines...) Expand all
24 24
25 namespace offline_pages { 25 namespace offline_pages {
26 26
27 namespace { 27 namespace {
28 const bool kUserRequest = true; 28 const bool kUserRequest = true;
29 const bool kStartOfProcessing = true; 29 const bool kStartOfProcessing = true;
30 const int kMinDurationSeconds = 1; 30 const int kMinDurationSeconds = 1;
31 const int kMaxDurationSeconds = 7 * 24 * 60 * 60; // 7 days 31 const int kMaxDurationSeconds = 7 * 24 * 60 * 60; // 7 days
32 const int kDurationBuckets = 50; 32 const int kDurationBuckets = 50;
33 const int kDisabledTaskRecheckSeconds = 5; 33 const int kDisabledTaskRecheckSeconds = 5;
34 const int kInvalidRequestId = 0;
34 35
35 // TODO(dougarnett): Move to util location and share with model impl. 36 // TODO(dougarnett): Move to util location and share with model impl.
36 std::string AddHistogramSuffix(const ClientId& client_id, 37 std::string AddHistogramSuffix(const ClientId& client_id,
37 const char* histogram_name) { 38 const char* histogram_name) {
38 if (client_id.name_space.empty()) { 39 if (client_id.name_space.empty()) {
39 NOTREACHED(); 40 NOTREACHED();
40 return histogram_name; 41 return histogram_name;
41 } 42 }
42 std::string adjusted_histogram_name(histogram_name); 43 std::string adjusted_histogram_name(histogram_name);
43 adjusted_histogram_name += "." + client_id.name_space; 44 adjusted_histogram_name += "." + client_id.name_space;
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after
174 queue_(std::move(queue)), 175 queue_(std::move(queue)),
175 scheduler_(std::move(scheduler)), 176 scheduler_(std::move(scheduler)),
176 policy_controller_(new ClientPolicyController()), 177 policy_controller_(new ClientPolicyController()),
177 network_quality_estimator_(network_quality_estimator), 178 network_quality_estimator_(network_quality_estimator),
178 active_request_(nullptr), 179 active_request_(nullptr),
179 last_offlining_status_(Offliner::RequestStatus::UNKNOWN), 180 last_offlining_status_(Offliner::RequestStatus::UNKNOWN),
180 scheduler_callback_(base::Bind(&EmptySchedulerCallback)), 181 scheduler_callback_(base::Bind(&EmptySchedulerCallback)),
181 immediate_schedule_callback_(base::Bind(&EmptySchedulerCallback)), 182 immediate_schedule_callback_(base::Bind(&EmptySchedulerCallback)),
182 weak_ptr_factory_(this) { 183 weak_ptr_factory_(this) {
183 DCHECK(policy_ != nullptr); 184 DCHECK(policy_ != nullptr);
184 std::unique_ptr<PickRequestTaskFactory> picker_factory( 185 std::unique_ptr<CleanupTaskFactory> cleanup_factory(
185 new PickRequestTaskFactory(policy_.get(), this, &event_logger_)); 186 new CleanupTaskFactory(policy_.get(), this, &event_logger_));
186 queue_->SetPickerFactory(std::move(picker_factory)); 187 queue_->SetCleanupFactory(std::move(cleanup_factory));
188 // Do a cleanup at startup time.
189 queue_->CleanupRequestQueue(kInvalidRequestId);
187 } 190 }
188 191
189 RequestCoordinator::~RequestCoordinator() {} 192 RequestCoordinator::~RequestCoordinator() {}
190 193
191 int64_t RequestCoordinator::SavePageLater(const GURL& url, 194 int64_t RequestCoordinator::SavePageLater(const GURL& url,
192 const ClientId& client_id, 195 const ClientId& client_id,
193 bool user_requested, 196 bool user_requested,
194 RequestAvailability availability) { 197 RequestAvailability availability) {
195 DVLOG(2) << "URL is " << url << " " << __func__; 198 DVLOG(2) << "URL is " << url << " " << __func__;
196 199
(...skipping 437 matching lines...) Expand 10 before | Expand all | Expand 10 after
634 // TODO: Make sure the scheduler callback is valid before running it. 637 // TODO: Make sure the scheduler callback is valid before running it.
635 scheduler_callback_.Run(true); 638 scheduler_callback_.Run(true);
636 DVLOG(2) << " out of time, giving up. " << __func__; 639 DVLOG(2) << " out of time, giving up. " << __func__;
637 640
638 return; 641 return;
639 } 642 }
640 643
641 // Ask request queue to make a new PickRequestTask object, then put it on the 644 // Ask request queue to make a new PickRequestTask object, then put it on the
642 // task queue. 645 // task queue.
643 queue_->PickNextRequest( 646 queue_->PickNextRequest(
644 base::Bind(&RequestCoordinator::RequestPicked, 647 policy_.get(), base::Bind(&RequestCoordinator::RequestPicked,
645 weak_ptr_factory_.GetWeakPtr()), 648 weak_ptr_factory_.GetWeakPtr()),
646 base::Bind(&RequestCoordinator::RequestNotPicked, 649 base::Bind(&RequestCoordinator::RequestNotPicked,
647 weak_ptr_factory_.GetWeakPtr()), 650 weak_ptr_factory_.GetWeakPtr()),
648 base::Bind(&RequestCoordinator::RequestCounts, 651 base::Bind(&RequestCoordinator::RequestCounts,
649 weak_ptr_factory_.GetWeakPtr(), is_start_of_processing), 652 weak_ptr_factory_.GetWeakPtr(), is_start_of_processing),
650 *current_conditions_.get(), disabled_requests_); 653 *current_conditions_.get(), disabled_requests_);
651 // TODO(petewil): Verify current_conditions has a good value on all calling 654 // TODO(petewil): Verify current_conditions has a good value on all calling
652 // paths. It is really more of a "last known conditions" than "current 655 // paths. It is really more of a "last known conditions" than "current
653 // conditions". Consider having a call to Java to check the current 656 // conditions". Consider having a call to Java to check the current
654 // conditions. 657 // conditions.
655 } 658 }
656 659
657 // Called by the request picker when a request has been picked. 660 // Called by the request picker when a request has been picked.
658 void RequestCoordinator::RequestPicked(const SavePageRequest& request) { 661 void RequestCoordinator::RequestPicked(const SavePageRequest& request,
662 bool cleanup_needed) {
659 DVLOG(2) << request.url() << " " << __func__; 663 DVLOG(2) << request.url() << " " << __func__;
660 is_starting_ = false; 664 is_starting_ = false;
661 665
662 // Make sure we were not stopped while picking. 666 // Make sure we were not stopped while picking.
663 if (processing_state_ != ProcessingWindowState::STOPPED) { 667 if (processing_state_ != ProcessingWindowState::STOPPED) {
664 // Send the request on to the offliner. 668 // Send the request on to the offliner.
665 SendRequestToOffliner(request); 669 SendRequestToOffliner(request);
666 } 670 }
671
672 // Schedule a queue cleanup if needed.
673 if (cleanup_needed)
674 queue_->CleanupRequestQueue(request.request_id());
667 } 675 }
668 676
669 void RequestCoordinator::RequestNotPicked( 677 void RequestCoordinator::RequestNotPicked(
670 bool non_user_requested_tasks_remaining) { 678 bool non_user_requested_tasks_remaining,
679 bool cleanup_needed) {
671 DVLOG(2) << __func__; 680 DVLOG(2) << __func__;
672 is_starting_ = false; 681 is_starting_ = false;
673 682
674 // Clear the outstanding "safety" task in the scheduler. 683 // Clear the outstanding "safety" task in the scheduler.
675 scheduler_->Unschedule(); 684 scheduler_->Unschedule();
676 685
677 // If disabled tasks remain, post a new safety task for 5 sec from now. 686 // If disabled tasks remain, post a new safety task for 5 sec from now.
678 if (disabled_requests_.size() > 0) { 687 if (disabled_requests_.size() > 0) {
679 scheduler_->BackupSchedule(GetTriggerConditions(kUserRequest), 688 scheduler_->BackupSchedule(GetTriggerConditions(kUserRequest),
680 kDisabledTaskRecheckSeconds); 689 kDisabledTaskRecheckSeconds);
681 } else if (non_user_requested_tasks_remaining) { 690 } else if (non_user_requested_tasks_remaining) {
682 // If we don't have any of those, check for non-user-requested tasks. 691 // If we don't have any of those, check for non-user-requested tasks.
683 scheduler_->Schedule(GetTriggerConditions(!kUserRequest)); 692 scheduler_->Schedule(GetTriggerConditions(!kUserRequest));
684 } 693 }
685 694
695 // Schedule a queue cleanup if needed.
696 if (cleanup_needed)
697 queue_->CleanupRequestQueue(kInvalidRequestId);
698
686 // Let the scheduler know we are done processing. 699 // Let the scheduler know we are done processing.
687 scheduler_callback_.Run(true); 700 scheduler_callback_.Run(true);
688 } 701 }
689 702
690 void RequestCoordinator::RequestCounts(bool is_start_of_processing, 703 void RequestCoordinator::RequestCounts(bool is_start_of_processing,
691 size_t total_requests, 704 size_t total_requests,
692 size_t available_requests) { 705 size_t available_requests) {
693 // Only capture request counts for the start of processing (not for 706 // Only capture request counts for the start of processing (not for
694 // continued processing in the same window). 707 // continued processing in the same window).
695 if (!is_start_of_processing) 708 if (!is_start_of_processing)
(...skipping 286 matching lines...) Expand 10 before | Expand all | Expand 10 after
982 995
983 ClientPolicyController* RequestCoordinator::GetPolicyController() { 996 ClientPolicyController* RequestCoordinator::GetPolicyController() {
984 return policy_controller_.get(); 997 return policy_controller_.get();
985 } 998 }
986 999
987 void RequestCoordinator::Shutdown() { 1000 void RequestCoordinator::Shutdown() {
988 network_quality_estimator_ = nullptr; 1001 network_quality_estimator_ = nullptr;
989 } 1002 }
990 1003
991 } // namespace offline_pages 1004 } // namespace offline_pages
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698