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

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: Created 4 years, 5 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),
47 operation_start_time_(base::Time::Now()),
jianli 2016/07/25 21:50:38 It seems to me that this initialization was not ne
Pete Williamson 2016/07/25 22:01:33 Done.
56 policy_(std::move(policy)), 48 policy_(std::move(policy)),
57 factory_(std::move(factory)), 49 factory_(std::move(factory)),
58 queue_(std::move(queue)), 50 queue_(std::move(queue)),
59 scheduler_(std::move(scheduler)), 51 scheduler_(std::move(scheduler)),
60 last_offlining_status_(Offliner::RequestStatus::UNKNOWN), 52 last_offlining_status_(Offliner::RequestStatus::UNKNOWN),
53 offliner_timeout_(base::TimeDelta::FromSeconds(
54 policy_->GetSinglePageTimeBudgetInSeconds())),
61 weak_ptr_factory_(this) { 55 weak_ptr_factory_(this) {
62 DCHECK(policy_ != nullptr); 56 DCHECK(policy_ != nullptr);
63 picker_.reset(new RequestPicker(queue_.get(), policy_.get())); 57 picker_.reset(new RequestPicker(queue_.get(), policy_.get()));
64 } 58 }
65 59
66 RequestCoordinator::~RequestCoordinator() {} 60 RequestCoordinator::~RequestCoordinator() {}
67 61
68 bool RequestCoordinator::SavePageLater( 62 bool RequestCoordinator::SavePageLater(
69 const GURL& url, const ClientId& client_id, bool was_user_requested) { 63 const GURL& url, const ClientId& client_id, bool was_user_requested) {
70 DVLOG(2) << "URL is " << url << " " << __FUNCTION__; 64 DVLOG(2) << "URL is " << url << " " << __FUNCTION__;
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
121 } 115 }
122 116
123 // Returns true if the caller should expect a callback, false otherwise. For 117 // 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. 118 // instance, this would return false if a request is already in progress.
125 bool RequestCoordinator::StartProcessing( 119 bool RequestCoordinator::StartProcessing(
126 const DeviceConditions& device_conditions, 120 const DeviceConditions& device_conditions,
127 const base::Callback<void(bool)>& callback) { 121 const base::Callback<void(bool)>& callback) {
128 current_conditions_.reset(new DeviceConditions(device_conditions)); 122 current_conditions_.reset(new DeviceConditions(device_conditions));
129 if (is_busy_) return false; 123 if (is_busy_) return false;
130 124
125 // Mark the time at which we started processing so we can check our time
126 // budget.
127 operation_start_time_ = base::Time::Now();
128
131 is_canceled_ = false; 129 is_canceled_ = false;
132 scheduler_callback_ = callback; 130 scheduler_callback_ = callback;
133 // TODO(petewil): Check existing conditions (should be passed down from 131 // TODO(petewil): Check existing conditions (should be passed down from
134 // BackgroundTask) 132 // BackgroundTask)
135 133
136 TryNextRequest(); 134 TryNextRequest();
137 135
138 return true; 136 return true;
139 } 137 }
140 138
141 void RequestCoordinator::TryNextRequest() { 139 void RequestCoordinator::TryNextRequest() {
140 // If there is no time left in the budget, return to the scheduler, but leave
141 // the "safety" task, so we get called again next time.
jianli 2016/07/25 21:50:38 I found it hard to understand what the "safety" ta
Pete Williamson 2016/07/25 22:01:33 Done. I was referring to something in the design
142 if (base::Time::Now() - operation_start_time_ >
143 base::TimeDelta::FromSeconds(
144 policy_->GetBackgroundProcessingTimeBudgetSeconds())) {
145 // Let the scheduler know we are done processing.
146 scheduler_callback_.Run(true);
jianli 2016/07/25 21:50:38 Do we need to return here?
Pete Williamson 2016/07/25 22:01:33 Oh my goodness, you're right! Added.
147 }
148
142 // Choose a request to process that meets the available conditions. 149 // Choose a request to process that meets the available conditions.
143 // This is an async call, and returns right away. 150 // This is an async call, and returns right away.
144 picker_->ChooseNextRequest( 151 picker_->ChooseNextRequest(
145 base::Bind(&RequestCoordinator::RequestPicked, 152 base::Bind(&RequestCoordinator::RequestPicked,
146 weak_ptr_factory_.GetWeakPtr()), 153 weak_ptr_factory_.GetWeakPtr()),
147 base::Bind(&RequestCoordinator::RequestQueueEmpty, 154 base::Bind(&RequestCoordinator::RequestQueueEmpty,
148 weak_ptr_factory_.GetWeakPtr()), 155 weak_ptr_factory_.GetWeakPtr()),
149 current_conditions_.get()); 156 current_conditions_.get());
150 } 157 }
151 158
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after
222 updated_request.set_last_attempt_time(base::Time::Now()); 229 updated_request.set_last_attempt_time(base::Time::Now());
223 RequestQueue::UpdateRequestCallback update_callback = 230 RequestQueue::UpdateRequestCallback update_callback =
224 base::Bind(&RequestCoordinator::UpdateRequestCallback, 231 base::Bind(&RequestCoordinator::UpdateRequestCallback,
225 weak_ptr_factory_.GetWeakPtr()); 232 weak_ptr_factory_.GetWeakPtr());
226 queue_->UpdateRequest( 233 queue_->UpdateRequest(
227 updated_request, 234 updated_request,
228 base::Bind(&RequestCoordinator::UpdateRequestCallback, 235 base::Bind(&RequestCoordinator::UpdateRequestCallback,
229 weak_ptr_factory_.GetWeakPtr())); 236 weak_ptr_factory_.GetWeakPtr()));
230 } 237 }
231 238
232 // TODO(petewil): Check time budget. Return to the scheduler if we are out.
233 // Start another request if we have time.
234 TryNextRequest(); 239 TryNextRequest();
235 } 240 }
236 241
237 const Scheduler::TriggerConditions& 242 const Scheduler::TriggerConditions&
238 RequestCoordinator::GetTriggerConditionsForUserRequest() { 243 RequestCoordinator::GetTriggerConditionsForUserRequest() {
239 return kUserRequestTriggerConditions; 244 return kUserRequestTriggerConditions;
240 } 245 }
241 246
242 void RequestCoordinator::GetOffliner() { 247 void RequestCoordinator::GetOffliner() {
243 if (!offliner_) { 248 if (!offliner_) {
244 offliner_ = factory_->GetOffliner(policy_.get()); 249 offliner_ = factory_->GetOffliner(policy_.get());
245 } 250 }
246 } 251 }
247 252
248 } // namespace offline_pages 253 } // namespace offline_pages
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698