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

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

Issue 2266323002: Cancel prerendering when a request is paused or removed. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: CR feedback per DougArnett Created 4 years, 3 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 94 matching lines...) Expand 10 before | Expand all | Expand 10 after
105 weak_ptr_factory_.GetWeakPtr(), callback)); 105 weak_ptr_factory_.GetWeakPtr(), callback));
106 } 106 }
107 107
108 void RequestCoordinator::GetQueuedRequestsCallback( 108 void RequestCoordinator::GetQueuedRequestsCallback(
109 const GetRequestsCallback& callback, 109 const GetRequestsCallback& callback,
110 RequestQueue::GetRequestsResult result, 110 RequestQueue::GetRequestsResult result,
111 const std::vector<SavePageRequest>& requests) { 111 const std::vector<SavePageRequest>& requests) {
112 callback.Run(requests); 112 callback.Run(requests);
113 } 113 }
114 114
115 void RequestCoordinator::StopPrerendering() {
116 if (offliner_ && is_busy_) {
117 // TODO(dougarnett): Find current request and mark attempt aborted.
dougarnett 2016/08/23 20:59:45 maybe actually DO MarkAttemptAborted now on active
118 offliner_->Cancel();
119 }
120
121 // Stopping offliner means it will not call callback.
122 last_offlining_status_ =
123 Offliner::RequestStatus::REQUEST_COORDINATOR_CANCELED;
124
125 if (active_request_) {
126 RecordOfflinerResultUMA(active_request_->client_id(),
127 last_offlining_status_);
128 active_request_.reset();
129 }
130
131 }
132
133 bool RequestCoordinator::CancelActiveRequestIfItMatches(
134 const std::vector<int64_t>& request_ids) {
135 // If we have a request in progress and need to cancel it, call the
136 // pre-renderer to cancel. TODO Make sure we remove any page created by the
137 // prerenderer if it doesn't get the cancel in time.
138 if (active_request_ != nullptr) {
139 if (request_ids.end() != std::find(request_ids.begin(), request_ids.end(),
140 active_request_->request_id())) {
141 StopPrerendering();
142 return true;
143 }
144 }
145
146 return false;
147 }
148
115 void RequestCoordinator::RemoveRequests( 149 void RequestCoordinator::RemoveRequests(
116 const std::vector<int64_t>& request_ids) { 150 const std::vector<int64_t>& request_ids) {
151 bool canceled = CancelActiveRequestIfItMatches(request_ids);
117 queue_->RemoveRequests(request_ids, 152 queue_->RemoveRequests(request_ids,
118 base::Bind(&RequestCoordinator::RemoveRequestsCallback, 153 base::Bind(&RequestCoordinator::RemoveRequestsCallback,
119 weak_ptr_factory_.GetWeakPtr())); 154 weak_ptr_factory_.GetWeakPtr()));
155 if (canceled)
156 TryNextRequest();
120 } 157 }
121 158
122 void RequestCoordinator::PauseRequests( 159 void RequestCoordinator::PauseRequests(
123 const std::vector<int64_t>& request_ids) { 160 const std::vector<int64_t>& request_ids) {
161 bool canceled = CancelActiveRequestIfItMatches(request_ids);
124 queue_->ChangeRequestsState( 162 queue_->ChangeRequestsState(
125 request_ids, SavePageRequest::RequestState::PAUSED, 163 request_ids, SavePageRequest::RequestState::PAUSED,
126 base::Bind(&RequestCoordinator::UpdateMultipleRequestsCallback, 164 base::Bind(&RequestCoordinator::UpdateMultipleRequestsCallback,
127 weak_ptr_factory_.GetWeakPtr())); 165 weak_ptr_factory_.GetWeakPtr()));
166
167 if (canceled)
168 TryNextRequest();
128 } 169 }
129 170
130 void RequestCoordinator::ResumeRequests( 171 void RequestCoordinator::ResumeRequests(
131 const std::vector<int64_t>& request_ids) { 172 const std::vector<int64_t>& request_ids) {
132 queue_->ChangeRequestsState( 173 queue_->ChangeRequestsState(
133 request_ids, SavePageRequest::RequestState::AVAILABLE, 174 request_ids, SavePageRequest::RequestState::AVAILABLE,
134 base::Bind(&RequestCoordinator::UpdateMultipleRequestsCallback, 175 base::Bind(&RequestCoordinator::UpdateMultipleRequestsCallback,
135 weak_ptr_factory_.GetWeakPtr())); 176 weak_ptr_factory_.GetWeakPtr()));
136 // TODO: Should we also schedule a task, in case there is not one scheduled? 177 // TODO: Should we also schedule a task, in case there is not one scheduled?
137 } 178 }
(...skipping 27 matching lines...) Expand all
165 } 206 }
166 207
167 void RequestCoordinator::RemoveRequestsCallback( 208 void RequestCoordinator::RemoveRequestsCallback(
168 const RequestQueue::UpdateMultipleRequestResults& results, 209 const RequestQueue::UpdateMultipleRequestResults& results,
169 const std::vector<SavePageRequest>& requests) { 210 const std::vector<SavePageRequest>& requests) {
170 for (SavePageRequest request : requests) 211 for (SavePageRequest request : requests)
171 NotifyCompleted(request, SavePageStatus::REMOVED); 212 NotifyCompleted(request, SavePageStatus::REMOVED);
172 } 213 }
173 214
174 void RequestCoordinator::StopProcessing() { 215 void RequestCoordinator::StopProcessing() {
175 is_canceled_ = true; 216 is_canceled_ = true;
dougarnett 2016/08/23 20:59:45 if this flag only applies to StopProcessing (and n
Pete Williamson 2016/08/23 21:57:00 Done.
176 if (offliner_ && is_busy_) { 217 StopPrerendering();
177 // TODO(dougarnett): Find current request and mark attempt aborted.
178 offliner_->Cancel();
179 }
180
181 // Stopping offliner means it will not call callback.
182 last_offlining_status_ =
183 Offliner::RequestStatus::REQUEST_COORDINATOR_CANCELED;
184
185 if (active_request_) {
186 RecordOfflinerResultUMA(active_request_->client_id(),
187 last_offlining_status_);
188 active_request_.reset();
189 }
190 218
191 // Let the scheduler know we are done processing. 219 // Let the scheduler know we are done processing.
192 scheduler_callback_.Run(true); 220 scheduler_callback_.Run(true);
193 } 221 }
194 222
195 // Returns true if the caller should expect a callback, false otherwise. For 223 // Returns true if the caller should expect a callback, false otherwise. For
196 // instance, this would return false if a request is already in progress. 224 // instance, this would return false if a request is already in progress.
197 bool RequestCoordinator::StartProcessing( 225 bool RequestCoordinator::StartProcessing(
198 const DeviceConditions& device_conditions, 226 const DeviceConditions& device_conditions,
199 const base::Callback<void(bool)>& callback) { 227 const base::Callback<void(bool)>& callback) {
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
257 285
258 GetOffliner(); 286 GetOffliner();
259 if (!offliner_) { 287 if (!offliner_) {
260 DVLOG(0) << "Unable to create Offliner. " 288 DVLOG(0) << "Unable to create Offliner. "
261 << "Cannot background offline page."; 289 << "Cannot background offline page.";
262 return; 290 return;
263 } 291 }
264 292
265 DCHECK(!is_busy_); 293 DCHECK(!is_busy_);
266 is_busy_ = true; 294 is_busy_ = true;
267 active_request_.reset(new SavePageRequest(request));
268 295
269 // Prepare an updated request to attempt. 296 // Prepare an updated request to attempt.
270 SavePageRequest updated_request(request); 297 SavePageRequest updated_request(request);
271 updated_request.MarkAttemptStarted(base::Time::Now()); 298 updated_request.MarkAttemptStarted(base::Time::Now());
299 active_request_.reset(new SavePageRequest(updated_request));
272 300
273 // Start the load and save process in the offliner (Async). 301 // Start the load and save process in the offliner (Async).
274 if (offliner_->LoadAndSave( 302 if (offliner_->LoadAndSave(
275 updated_request, base::Bind(&RequestCoordinator::OfflinerDoneCallback, 303 updated_request, base::Bind(&RequestCoordinator::OfflinerDoneCallback,
276 weak_ptr_factory_.GetWeakPtr()))) { 304 weak_ptr_factory_.GetWeakPtr()))) {
277 // Offliner accepted request so update it in the queue. 305 // Offliner accepted request so update it in the queue.
278 queue_->UpdateRequest(updated_request, 306 queue_->UpdateRequest(updated_request,
279 base::Bind(&RequestCoordinator::UpdateRequestCallback, 307 base::Bind(&RequestCoordinator::UpdateRequestCallback,
280 weak_ptr_factory_.GetWeakPtr(), 308 weak_ptr_factory_.GetWeakPtr(),
281 updated_request.client_id())); 309 updated_request.client_id()));
(...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after
405 FOR_EACH_OBSERVER(Observer, observers_, OnChanged(request)); 433 FOR_EACH_OBSERVER(Observer, observers_, OnChanged(request));
406 } 434 }
407 435
408 void RequestCoordinator::GetOffliner() { 436 void RequestCoordinator::GetOffliner() {
409 if (!offliner_) { 437 if (!offliner_) {
410 offliner_ = factory_->GetOffliner(policy_.get()); 438 offliner_ = factory_->GetOffliner(policy_.get());
411 } 439 }
412 } 440 }
413 441
414 } // namespace offline_pages 442 } // namespace offline_pages
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698