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 <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" |
| 11 #include "base/logging.h" | 11 #include "base/logging.h" |
| 12 #include "base/time/time.h" | |
| 12 #include "components/offline_pages/background/offliner_factory.h" | 13 #include "components/offline_pages/background/offliner_factory.h" |
| 13 #include "components/offline_pages/background/offliner_policy.h" | 14 #include "components/offline_pages/background/offliner_policy.h" |
| 14 #include "components/offline_pages/background/request_picker.h" | 15 #include "components/offline_pages/background/request_picker.h" |
| 15 #include "components/offline_pages/background/save_page_request.h" | 16 #include "components/offline_pages/background/save_page_request.h" |
| 16 #include "components/offline_pages/offline_page_item.h" | 17 #include "components/offline_pages/offline_page_item.h" |
| 17 | 18 |
| 18 namespace offline_pages { | 19 namespace offline_pages { |
| 19 | 20 |
| 20 namespace { | 21 namespace { |
| 21 // TODO(dougarnett/petewil): Move to Policy object. Also consider lower minimum | 22 // TODO(dougarnett/petewil): Move to Policy object. Also consider lower minimum |
| 22 // battery percentage once there is some processing time limits in place. | 23 // battery percentage once there is some processing time limits in place. |
| 23 const Scheduler::TriggerConditions kUserRequestTriggerConditions( | 24 const Scheduler::TriggerConditions kUserRequestTriggerConditions( |
| 24 false /* require_power_connected */, | 25 false /* require_power_connected */, |
| 25 50 /* minimum_battery_percentage */, | 26 50 /* minimum_battery_percentage */, |
| 26 false /* require_unmetered_network */); | 27 false /* require_unmetered_network */); |
| 28 | |
| 29 // Timeout is 3 minutes based on the size of Marshmallow doze mode | |
| 30 // maintenance window. TODO(petewil): Find the optimal timeout based | |
|
dougarnett
2016/06/30 19:16:48
maybe line break before TODO
dewittj
2016/06/30 20:38:33
Please make a bug specifically to find this number
Pete Williamson
2016/07/01 17:16:54
Done.
Pete Williamson
2016/07/01 17:16:55
Done.
| |
| 31 // on data for 2G connections and common EM websites. | |
|
dougarnett
2016/06/30 19:16:48
another TODO might be to move this value into Poli
Pete Williamson
2016/07/01 17:16:55
Done.
| |
| 32 long OFFLINER_TIMEOUT_SECONDS = 180; | |
|
dewittj
2016/06/30 20:38:33
If this is exactly the marshmallow timeout, then t
Pete Williamson
2016/07/01 17:16:54
Done.
| |
| 33 | |
| 27 } // namespace | 34 } // namespace |
| 28 | 35 |
| 29 RequestCoordinator::RequestCoordinator(std::unique_ptr<OfflinerPolicy> policy, | 36 RequestCoordinator::RequestCoordinator(std::unique_ptr<OfflinerPolicy> policy, |
| 30 std::unique_ptr<OfflinerFactory> factory, | 37 std::unique_ptr<OfflinerFactory> factory, |
| 31 std::unique_ptr<RequestQueue> queue, | 38 std::unique_ptr<RequestQueue> queue, |
| 32 std::unique_ptr<Scheduler> scheduler) | 39 std::unique_ptr<Scheduler> scheduler) |
| 33 : is_busy_(false), | 40 : is_busy_(false), |
| 34 is_canceled_(false), | 41 is_canceled_(false), |
| 42 offliner_timeout_(OFFLINER_TIMEOUT_SECONDS), | |
| 35 offliner_(nullptr), | 43 offliner_(nullptr), |
| 36 policy_(std::move(policy)), | 44 policy_(std::move(policy)), |
| 37 factory_(std::move(factory)), | 45 factory_(std::move(factory)), |
| 38 queue_(std::move(queue)), | 46 queue_(std::move(queue)), |
| 39 scheduler_(std::move(scheduler)), | 47 scheduler_(std::move(scheduler)), |
| 40 last_offlining_status_(Offliner::RequestStatus::UNKNOWN), | 48 last_offlining_status_(Offliner::RequestStatus::UNKNOWN), |
| 41 weak_ptr_factory_(this) { | 49 weak_ptr_factory_(this) { |
| 42 DCHECK(policy_ != nullptr); | 50 DCHECK(policy_ != nullptr); |
| 43 picker_.reset(new RequestPicker(queue_.get())); | 51 picker_.reset(new RequestPicker(queue_.get())); |
| 44 } | 52 } |
| (...skipping 25 matching lines...) Expand all Loading... | |
| 70 | 78 |
| 71 // Inform the scheduler that we have an outstanding task. | 79 // Inform the scheduler that we have an outstanding task. |
| 72 // TODO(petewil): Determine trigger conditions from policy. | 80 // TODO(petewil): Determine trigger conditions from policy. |
| 73 scheduler_->Schedule(GetTriggerConditionsForUserRequest()); | 81 scheduler_->Schedule(GetTriggerConditionsForUserRequest()); |
| 74 } | 82 } |
| 75 | 83 |
| 76 // Called in response to updating a request in the request queue. | 84 // Called in response to updating a request in the request queue. |
| 77 void RequestCoordinator::UpdateRequestCallback( | 85 void RequestCoordinator::UpdateRequestCallback( |
| 78 RequestQueue::UpdateRequestResult result) {} | 86 RequestQueue::UpdateRequestResult result) {} |
| 79 | 87 |
| 80 void RequestCoordinator::StopProcessing() { | 88 void RequestCoordinator::StopProcessing() { |
|
dewittj
2016/06/30 20:38:33
as far as I can tell, this is only called in tests
Pete Williamson
2016/07/01 17:16:54
The primary purpose of this function is to cancel
dewittj
2016/07/01 17:27:48
Ah right, turns out code search ignores new additi
Pete Williamson
2016/07/01 17:56:44
I presume that StopProcessing is being called beca
| |
| 81 is_canceled_ = true; | 89 is_canceled_ = true; |
| 82 if (offliner_ && is_busy_) | 90 if (offliner_ && is_busy_) |
| 83 offliner_->Cancel(); | 91 offliner_->Cancel(); |
| 92 | |
| 93 // Return control to the scheduler when there is no more to do. | |
|
dougarnett
2016/06/30 19:16:48
Think this comment needs some rewording. Perhaps
/
Pete Williamson
2016/07/01 17:16:55
Done (changed here and in RequestQueueEmpty)
| |
| 94 scheduler_callback_.Run(true); | |
| 84 } | 95 } |
| 85 | 96 |
| 86 // Returns true if the caller should expect a callback, false otherwise. For | 97 // Returns true if the caller should expect a callback, false otherwise. For |
| 87 // instance, this would return false if a request is already in progress. | 98 // instance, this would return false if a request is already in progress. |
| 88 bool RequestCoordinator::StartProcessing( | 99 bool RequestCoordinator::StartProcessing( |
| 89 const DeviceConditions& device_conditions, | 100 const DeviceConditions& device_conditions, |
| 90 const base::Callback<void(bool)>& callback) { | 101 const base::Callback<void(bool)>& callback) { |
| 91 if (is_busy_) return false; | 102 if (is_busy_) return false; |
| 92 | 103 |
| 93 is_canceled_ = false; | 104 is_canceled_ = false; |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 136 return; | 147 return; |
| 137 } | 148 } |
| 138 | 149 |
| 139 DCHECK(!is_busy_); | 150 DCHECK(!is_busy_); |
| 140 is_busy_ = true; | 151 is_busy_ = true; |
| 141 | 152 |
| 142 // Start the load and save process in the offliner (Async). | 153 // Start the load and save process in the offliner (Async). |
| 143 offliner_->LoadAndSave(request, | 154 offliner_->LoadAndSave(request, |
| 144 base::Bind(&RequestCoordinator::OfflinerDoneCallback, | 155 base::Bind(&RequestCoordinator::OfflinerDoneCallback, |
| 145 weak_ptr_factory_.GetWeakPtr())); | 156 weak_ptr_factory_.GetWeakPtr())); |
| 157 | |
| 158 // Start a watchdog timer to catch pre-renders running too long | |
| 159 watchdog_timer_.Start( | |
| 160 FROM_HERE, base::TimeDelta::FromSeconds(offliner_timeout_), this, | |
| 161 &RequestCoordinator::StopProcessing); | |
| 146 } | 162 } |
| 147 | 163 |
| 148 void RequestCoordinator::OfflinerDoneCallback(const SavePageRequest& request, | 164 void RequestCoordinator::OfflinerDoneCallback(const SavePageRequest& request, |
| 149 Offliner::RequestStatus status) { | 165 Offliner::RequestStatus status) { |
| 150 DVLOG(2) << "offliner finished, saved: " | 166 DVLOG(2) << "offliner finished, saved: " |
| 151 << (status == Offliner::RequestStatus::SAVED) << ", status: " | 167 << (status == Offliner::RequestStatus::SAVED) << ", status: " |
| 152 << (int) status << ", " << __FUNCTION__; | 168 << (int) status << ", " << __FUNCTION__; |
| 153 event_logger_.RecordSavePageRequestUpdated( | 169 event_logger_.RecordSavePageRequestUpdated( |
| 154 request.client_id().name_space, | 170 request.client_id().name_space, |
| 155 "Saved", | 171 "Saved", |
| 156 request.request_id()); | 172 request.request_id()); |
| 157 last_offlining_status_ = status; | 173 last_offlining_status_ = status; |
| 174 watchdog_timer_.Stop(); | |
| 158 | 175 |
| 159 is_busy_ = false; | 176 is_busy_ = false; |
| 160 | 177 |
| 161 // If the request succeeded, remove it from the Queue and maybe schedule | 178 // If the request succeeded, remove it from the Queue and maybe schedule |
| 162 // another one. | 179 // another one. |
| 163 if (status == Offliner::RequestStatus::SAVED) { | 180 if (status == Offliner::RequestStatus::SAVED) { |
| 164 queue_->RemoveRequest(request.request_id(), | 181 queue_->RemoveRequest(request.request_id(), |
| 165 base::Bind(&RequestCoordinator::UpdateRequestCallback, | 182 base::Bind(&RequestCoordinator::UpdateRequestCallback, |
| 166 weak_ptr_factory_.GetWeakPtr())); | 183 weak_ptr_factory_.GetWeakPtr())); |
| 167 | 184 |
| 168 // TODO(petewil): Check time budget. Return to the scheduler if we are out. | 185 // TODO(petewil): Check time budget. Return to the scheduler if we are out. |
| 169 | 186 |
| 170 // Start another request if we have time. | 187 // Start another request if we have time. |
| 171 TryNextRequest(); | 188 TryNextRequest(); |
| 172 } | 189 } |
| 173 } | 190 } |
| 174 | 191 |
| 175 const Scheduler::TriggerConditions& | 192 const Scheduler::TriggerConditions& |
| 176 RequestCoordinator::GetTriggerConditionsForUserRequest() { | 193 RequestCoordinator::GetTriggerConditionsForUserRequest() { |
| 177 return kUserRequestTriggerConditions; | 194 return kUserRequestTriggerConditions; |
| 178 } | 195 } |
| 179 | 196 |
| 180 void RequestCoordinator::GetOffliner() { | 197 void RequestCoordinator::GetOffliner() { |
| 181 if (!offliner_) { | 198 if (!offliner_) { |
| 182 offliner_ = factory_->GetOffliner(policy_.get()); | 199 offliner_ = factory_->GetOffliner(policy_.get()); |
| 183 } | 200 } |
| 184 } | 201 } |
| 185 | 202 |
| 186 } // namespace offline_pages | 203 } // namespace offline_pages |
| OLD | NEW |