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" |
(...skipping 13 matching lines...) Expand all Loading... | |
24 false /* require_power_connected */, | 24 false /* require_power_connected */, |
25 50 /* minimum_battery_percentage */, | 25 50 /* minimum_battery_percentage */, |
26 false /* require_unmetered_network */); | 26 false /* require_unmetered_network */); |
27 } // namespace | 27 } // namespace |
28 | 28 |
29 RequestCoordinator::RequestCoordinator(std::unique_ptr<OfflinerPolicy> policy, | 29 RequestCoordinator::RequestCoordinator(std::unique_ptr<OfflinerPolicy> policy, |
30 std::unique_ptr<OfflinerFactory> factory, | 30 std::unique_ptr<OfflinerFactory> factory, |
31 std::unique_ptr<RequestQueue> queue, | 31 std::unique_ptr<RequestQueue> queue, |
32 std::unique_ptr<Scheduler> scheduler) | 32 std::unique_ptr<Scheduler> scheduler) |
33 : is_busy_(false), | 33 : is_busy_(false), |
34 is_canceled_(false), | |
35 offliner_(nullptr), | |
34 policy_(std::move(policy)), | 36 policy_(std::move(policy)), |
35 factory_(std::move(factory)), | 37 factory_(std::move(factory)), |
36 queue_(std::move(queue)), | 38 queue_(std::move(queue)), |
37 scheduler_(std::move(scheduler)), | 39 scheduler_(std::move(scheduler)), |
38 last_offlining_status_(Offliner::RequestStatus::UNKNOWN), | 40 last_offlining_status_(Offliner::RequestStatus::UNKNOWN), |
39 weak_ptr_factory_(this) { | 41 weak_ptr_factory_(this) { |
40 DCHECK(policy_ != nullptr); | 42 DCHECK(policy_ != nullptr); |
41 picker_.reset(new RequestPicker(queue_.get())); | 43 picker_.reset(new RequestPicker(queue_.get())); |
42 } | 44 } |
43 | 45 |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
88 scheduler_callback_.Run(true); | 90 scheduler_callback_.Run(true); |
89 } | 91 } |
90 | 92 |
91 // Returns true if the caller should expect a callback, false otherwise. For | 93 // Returns true if the caller should expect a callback, false otherwise. For |
92 // instance, this would return false if a request is already in progress. | 94 // instance, this would return false if a request is already in progress. |
93 bool RequestCoordinator::StartProcessing( | 95 bool RequestCoordinator::StartProcessing( |
94 const DeviceConditions& device_conditions, | 96 const DeviceConditions& device_conditions, |
95 const base::Callback<void(bool)>& callback) { | 97 const base::Callback<void(bool)>& callback) { |
96 if (is_busy_) return false; | 98 if (is_busy_) return false; |
97 | 99 |
100 is_canceled_ = false; | |
98 scheduler_callback_ = callback; | 101 scheduler_callback_ = callback; |
99 // TODO(petewil): Check existing conditions (should be passed down from | 102 // TODO(petewil): Check existing conditions (should be passed down from |
100 // BackgroundTask) | 103 // BackgroundTask) |
101 | 104 |
105 GetOffliner(); | |
dougarnett
2016/06/23 17:19:17
I wonder about this check here. Maybe you can say
Pete Williamson
2016/06/23 17:54:53
My thought here was that if we can't get an offlin
| |
106 if (!offliner_) { | |
107 DVLOG(0) << "Unable to create Offliner. " | |
108 << "Cannot background offline page."; | |
109 return false; | |
110 } | |
111 | |
102 TryNextRequest(); | 112 TryNextRequest(); |
103 | 113 |
104 return true; | 114 return true; |
105 } | 115 } |
106 | 116 |
107 void RequestCoordinator::TryNextRequest() { | 117 void RequestCoordinator::TryNextRequest() { |
108 // Choose a request to process that meets the available conditions. | 118 // Choose a request to process that meets the available conditions. |
109 // This is an async call, and returns right away. | 119 // This is an async call, and returns right away. |
110 picker_->ChooseNextRequest( | 120 picker_->ChooseNextRequest( |
111 base::Bind(&RequestCoordinator::RequestPicked, | 121 base::Bind(&RequestCoordinator::RequestPicked, |
112 weak_ptr_factory_.GetWeakPtr()), | 122 weak_ptr_factory_.GetWeakPtr()), |
113 base::Bind(&RequestCoordinator::RequestQueueEmpty, | 123 base::Bind(&RequestCoordinator::RequestQueueEmpty, |
114 weak_ptr_factory_.GetWeakPtr())); | 124 weak_ptr_factory_.GetWeakPtr())); |
115 } | 125 } |
116 | 126 |
117 void RequestCoordinator::StopProcessing() { | 127 void RequestCoordinator::StopProcessing() { |
128 is_canceled_ = true; | |
129 if (offliner_ && is_busy_) | |
130 offliner_->Cancel(); | |
118 } | 131 } |
119 | 132 |
120 Scheduler::TriggerConditions const& | 133 Scheduler::TriggerConditions const& |
121 RequestCoordinator::GetTriggerConditionsForUserRequest() { | 134 RequestCoordinator::GetTriggerConditionsForUserRequest() { |
122 return kUserRequestTriggerConditions; | 135 return kUserRequestTriggerConditions; |
123 } | 136 } |
124 | 137 |
125 void RequestCoordinator::SendRequestToOffliner(const SavePageRequest& request) { | 138 void RequestCoordinator::SendRequestToOffliner(const SavePageRequest& request) { |
126 // TODO(petewil): When we have multiple offliners, we need to pick one. | 139 if (is_canceled_) |
dougarnett
2016/06/23 17:19:17
// Be sure processing hasn't been canceled.
Pete Williamson
2016/06/23 17:54:53
Comment added (I worded it slightly differently).
| |
127 Offliner* offliner = factory_->GetOffliner(policy_.get()); | 140 return; |
128 if (!offliner) { | 141 |
142 GetOffliner(); | |
143 if (!offliner_) { | |
129 DVLOG(0) << "Unable to create Offliner. " | 144 DVLOG(0) << "Unable to create Offliner. " |
130 << "Cannot background offline page."; | 145 << "Cannot background offline page."; |
131 return; | 146 return; |
132 } | 147 } |
133 | 148 |
134 DCHECK(!is_busy_); | 149 DCHECK(!is_busy_); |
135 is_busy_ = true; | 150 is_busy_ = true; |
136 | 151 |
137 // Start the load and save process in the offliner (Async). | 152 // Start the load and save process in the offliner (Async). |
138 offliner->LoadAndSave(request, | 153 offliner_->LoadAndSave(request, |
139 base::Bind(&RequestCoordinator::OfflinerDoneCallback, | 154 base::Bind(&RequestCoordinator::OfflinerDoneCallback, |
140 weak_ptr_factory_.GetWeakPtr())); | 155 weak_ptr_factory_.GetWeakPtr())); |
141 } | 156 } |
142 | 157 |
143 void RequestCoordinator::OfflinerDoneCallback(const SavePageRequest& request, | 158 void RequestCoordinator::OfflinerDoneCallback(const SavePageRequest& request, |
144 Offliner::RequestStatus status) { | 159 Offliner::RequestStatus status) { |
145 DVLOG(2) << "offliner finished, saved: " | 160 DVLOG(2) << "offliner finished, saved: " |
146 << (status == Offliner::RequestStatus::SAVED) << ", " | 161 << (status == Offliner::RequestStatus::SAVED) << ", status: " |
147 << __FUNCTION__; | 162 << (int) status << ", " << __FUNCTION__; |
148 last_offlining_status_ = status; | 163 last_offlining_status_ = status; |
149 | 164 |
150 is_busy_ = false; | 165 is_busy_ = false; |
151 | 166 |
152 // If the request succeeded, remove it from the Queue and maybe schedule | 167 // If the request succeeded, remove it from the Queue and maybe schedule |
153 // another one. | 168 // another one. |
154 if (status == Offliner::RequestStatus::SAVED) { | 169 if (status == Offliner::RequestStatus::SAVED) { |
155 queue_->RemoveRequest(request.request_id(), | 170 queue_->RemoveRequest(request.request_id(), |
156 base::Bind(&RequestCoordinator::UpdateRequestCallback, | 171 base::Bind(&RequestCoordinator::UpdateRequestCallback, |
157 weak_ptr_factory_.GetWeakPtr())); | 172 weak_ptr_factory_.GetWeakPtr())); |
158 | 173 |
159 // TODO(petewil): Check time budget. Return to the scheduler if we are out. | 174 // TODO(petewil): Check time budget. Return to the scheduler if we are out. |
160 | 175 |
161 // Start another request if we have time. | 176 // Start another request if we have time. |
162 TryNextRequest(); | 177 TryNextRequest(); |
163 } | 178 } |
164 } | 179 } |
165 | 180 |
181 void RequestCoordinator::GetOffliner() { | |
182 if (!offliner_) { | |
183 offliner_ = factory_->GetOffliner(policy_.get()); | |
184 } | |
185 } | |
186 | |
166 } // namespace offline_pages | 187 } // namespace offline_pages |
OLD | NEW |