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

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

Issue 2178143002: Check time budget before starting new requests, and unittest. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: CR feedback per JianLi Created 4 years, 4 months 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 <utility> 7 #include <utility>
8 8
9 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "base/callback.h" 10 #include "base/callback.h"
(...skipping 17 matching lines...) Expand all
28 request_status, 28 request_status,
29 Offliner::RequestStatus::STATUS_COUNT); 29 Offliner::RequestStatus::STATUS_COUNT);
30 } 30 }
31 31
32 // TODO(dougarnett/petewil): Move to Policy object. Also consider lower minimum 32 // TODO(dougarnett/petewil): Move to Policy object. Also consider lower minimum
33 // battery percentage once there is some processing time limits in place. 33 // battery percentage once there is some processing time limits in place.
34 const Scheduler::TriggerConditions kUserRequestTriggerConditions( 34 const Scheduler::TriggerConditions kUserRequestTriggerConditions(
35 false /* require_power_connected */, 35 false /* require_power_connected */,
36 50 /* minimum_battery_percentage */, 36 50 /* minimum_battery_percentage */,
37 false /* require_unmetered_network */); 37 false /* require_unmetered_network */);
38
39 // Timeout is 2.5 minutes based on the size of Marshmallow doze mode
40 // maintenance window (3 minutes)
41 // TODO(petewil): Find the optimal timeout based on data for 2G connections and
42 // common EM websites. crbug.com/625204
43 // TODO(petewil): Move this value into the policy object.
44 long OFFLINER_TIMEOUT_SECONDS = 150;
45
46 } // namespace 38 } // namespace
47 39
48 RequestCoordinator::RequestCoordinator(std::unique_ptr<OfflinerPolicy> policy, 40 RequestCoordinator::RequestCoordinator(std::unique_ptr<OfflinerPolicy> policy,
49 std::unique_ptr<OfflinerFactory> factory, 41 std::unique_ptr<OfflinerFactory> factory,
50 std::unique_ptr<RequestQueue> queue, 42 std::unique_ptr<RequestQueue> queue,
51 std::unique_ptr<Scheduler> scheduler) 43 std::unique_ptr<Scheduler> scheduler)
52 : is_busy_(false), 44 : is_busy_(false),
53 is_canceled_(false), 45 is_canceled_(false),
54 offliner_timeout_(base::TimeDelta::FromSeconds(OFFLINER_TIMEOUT_SECONDS)),
55 offliner_(nullptr), 46 offliner_(nullptr),
56 policy_(std::move(policy)), 47 policy_(std::move(policy)),
57 factory_(std::move(factory)), 48 factory_(std::move(factory)),
58 queue_(std::move(queue)), 49 queue_(std::move(queue)),
59 scheduler_(std::move(scheduler)), 50 scheduler_(std::move(scheduler)),
60 last_offlining_status_(Offliner::RequestStatus::UNKNOWN), 51 last_offlining_status_(Offliner::RequestStatus::UNKNOWN),
52 offliner_timeout_(base::TimeDelta::FromSeconds(
53 policy_->GetSinglePageTimeBudgetInSeconds())),
61 weak_ptr_factory_(this) { 54 weak_ptr_factory_(this) {
62 DCHECK(policy_ != nullptr); 55 DCHECK(policy_ != nullptr);
63 picker_.reset(new RequestPicker(queue_.get(), policy_.get())); 56 picker_.reset(new RequestPicker(queue_.get(), policy_.get()));
64 } 57 }
65 58
66 RequestCoordinator::~RequestCoordinator() {} 59 RequestCoordinator::~RequestCoordinator() {}
67 60
68 bool RequestCoordinator::SavePageLater( 61 bool RequestCoordinator::SavePageLater(
69 const GURL& url, const ClientId& client_id, bool was_user_requested) { 62 const GURL& url, const ClientId& client_id, bool was_user_requested) {
70 DVLOG(2) << "URL is " << url << " " << __FUNCTION__; 63 DVLOG(2) << "URL is " << url << " " << __FUNCTION__;
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
121 } 114 }
122 115
123 // Returns true if the caller should expect a callback, false otherwise. For 116 // Returns true if the caller should expect a callback, false otherwise. For
124 // instance, this would return false if a request is already in progress. 117 // instance, this would return false if a request is already in progress.
125 bool RequestCoordinator::StartProcessing( 118 bool RequestCoordinator::StartProcessing(
126 const DeviceConditions& device_conditions, 119 const DeviceConditions& device_conditions,
127 const base::Callback<void(bool)>& callback) { 120 const base::Callback<void(bool)>& callback) {
128 current_conditions_.reset(new DeviceConditions(device_conditions)); 121 current_conditions_.reset(new DeviceConditions(device_conditions));
129 if (is_busy_) return false; 122 if (is_busy_) return false;
130 123
124 // Mark the time at which we started processing so we can check our time
125 // budget.
126 operation_start_time_ = base::Time::Now();
127
131 is_canceled_ = false; 128 is_canceled_ = false;
132 scheduler_callback_ = callback; 129 scheduler_callback_ = callback;
133 // TODO(petewil): Check existing conditions (should be passed down from 130 // TODO(petewil): Check existing conditions (should be passed down from
134 // BackgroundTask) 131 // BackgroundTask)
135 132
136 TryNextRequest(); 133 TryNextRequest();
137 134
138 return true; 135 return true;
139 } 136 }
140 137
141 void RequestCoordinator::TryNextRequest() { 138 void RequestCoordinator::TryNextRequest() {
139 // If there is no time left in the budget, return to the scheduler.
dougarnett 2016/07/26 16:11:34 So are we willing to overrun the time budget by ju
Pete Williamson 2016/07/26 18:42:19 We are totally willing to overrun the time budget
dougarnett 2016/07/26 20:26:44 I guess my meta point was that we have two things
Pete Williamson 2016/07/26 20:59:39 Done, see how you like new names.
140 // We do not remove the pending task that was set up earlier in case
141 // we run out of time, so the background scheduler will return to us
142 // at the next opportunity to run background tasks.
143 if (base::Time::Now() - operation_start_time_ >
144 base::TimeDelta::FromSeconds(
145 policy_->GetBackgroundProcessingTimeBudgetSeconds())) {
146 // Let the scheduler know we are done processing.
147 scheduler_callback_.Run(true);
148
149 return;
150 }
151
142 // Choose a request to process that meets the available conditions. 152 // Choose a request to process that meets the available conditions.
143 // This is an async call, and returns right away. 153 // This is an async call, and returns right away.
144 picker_->ChooseNextRequest( 154 picker_->ChooseNextRequest(
145 base::Bind(&RequestCoordinator::RequestPicked, 155 base::Bind(&RequestCoordinator::RequestPicked,
146 weak_ptr_factory_.GetWeakPtr()), 156 weak_ptr_factory_.GetWeakPtr()),
147 base::Bind(&RequestCoordinator::RequestQueueEmpty, 157 base::Bind(&RequestCoordinator::RequestQueueEmpty,
148 weak_ptr_factory_.GetWeakPtr()), 158 weak_ptr_factory_.GetWeakPtr()),
149 current_conditions_.get()); 159 current_conditions_.get());
150 } 160 }
151 161
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after
222 updated_request.set_last_attempt_time(base::Time::Now()); 232 updated_request.set_last_attempt_time(base::Time::Now());
223 RequestQueue::UpdateRequestCallback update_callback = 233 RequestQueue::UpdateRequestCallback update_callback =
224 base::Bind(&RequestCoordinator::UpdateRequestCallback, 234 base::Bind(&RequestCoordinator::UpdateRequestCallback,
225 weak_ptr_factory_.GetWeakPtr()); 235 weak_ptr_factory_.GetWeakPtr());
226 queue_->UpdateRequest( 236 queue_->UpdateRequest(
227 updated_request, 237 updated_request,
228 base::Bind(&RequestCoordinator::UpdateRequestCallback, 238 base::Bind(&RequestCoordinator::UpdateRequestCallback,
229 weak_ptr_factory_.GetWeakPtr())); 239 weak_ptr_factory_.GetWeakPtr()));
230 } 240 }
231 241
232 // TODO(petewil): Check time budget. Return to the scheduler if we are out.
233 // Start another request if we have time.
234 TryNextRequest(); 242 TryNextRequest();
235 } 243 }
236 244
237 const Scheduler::TriggerConditions& 245 const Scheduler::TriggerConditions&
238 RequestCoordinator::GetTriggerConditionsForUserRequest() { 246 RequestCoordinator::GetTriggerConditionsForUserRequest() {
239 return kUserRequestTriggerConditions; 247 return kUserRequestTriggerConditions;
240 } 248 }
241 249
242 void RequestCoordinator::GetOffliner() { 250 void RequestCoordinator::GetOffliner() {
243 if (!offliner_) { 251 if (!offliner_) {
244 offliner_ = factory_->GetOffliner(policy_.get()); 252 offliner_ = factory_->GetOffliner(policy_.get());
245 } 253 }
246 } 254 }
247 255
248 } // namespace offline_pages 256 } // namespace offline_pages
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698