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" |
| (...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 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::CancelActiveRequestIfItMatches( | |
|
dougarnett
2016/08/23 16:32:13
CancelRequestIfActive()? a bit shorter
Pete Williamson
2016/08/23 17:48:43
That's shorter, but not really accurate, since we
dougarnett
2016/08/23 20:59:44
ugh, seems no more accurate than mine. I guess sti
| |
| 116 const std::vector<int64_t>& request_ids) { | |
| 117 // If we have a request in progress and need to cancel it, call the | |
| 118 // pre-renderer to cancel. TODO Make sure we remove any page created by the | |
| 119 // prerenderer if it doesn't get the cancel in time. | |
| 120 if (active_request_ != nullptr) { | |
| 121 if (request_ids.end() != std::find(request_ids.begin(), request_ids.end(), | |
| 122 active_request_->request_id())) { | |
| 123 offliner_->Cancel(); | |
|
dougarnett
2016/08/23 16:32:13
consider calling StopProcessing() instead or check
Pete Williamson
2016/08/23 17:48:43
Added StopPrerendering() to handle common tasks.
| |
| 124 // When we get a callback from the offliner, we will call | |
| 125 // TryNextRequest(); even though return value is PRERENDER_CANCELLED. | |
|
dougarnett
2016/08/23 16:32:13
hmm, the Offliner interface says it will not call
Pete Williamson
2016/08/23 17:48:43
Ah, I had forgotten that. I don't need to modify
| |
| 126 } | |
| 127 } | |
| 128 } | |
| 129 | |
| 115 void RequestCoordinator::RemoveRequests( | 130 void RequestCoordinator::RemoveRequests( |
| 116 const std::vector<int64_t>& request_ids) { | 131 const std::vector<int64_t>& request_ids) { |
| 132 CancelActiveRequestIfItMatches(request_ids); | |
| 117 queue_->RemoveRequests(request_ids, | 133 queue_->RemoveRequests(request_ids, |
| 118 base::Bind(&RequestCoordinator::RemoveRequestsCallback, | 134 base::Bind(&RequestCoordinator::RemoveRequestsCallback, |
| 119 weak_ptr_factory_.GetWeakPtr())); | 135 weak_ptr_factory_.GetWeakPtr())); |
| 120 } | 136 } |
| 121 | 137 |
| 122 void RequestCoordinator::PauseRequests( | 138 void RequestCoordinator::PauseRequests( |
| 123 const std::vector<int64_t>& request_ids) { | 139 const std::vector<int64_t>& request_ids) { |
| 140 CancelActiveRequestIfItMatches(request_ids); | |
| 124 queue_->ChangeRequestsState( | 141 queue_->ChangeRequestsState( |
| 125 request_ids, SavePageRequest::RequestState::PAUSED, | 142 request_ids, SavePageRequest::RequestState::PAUSED, |
| 126 base::Bind(&RequestCoordinator::UpdateMultipleRequestsCallback, | 143 base::Bind(&RequestCoordinator::UpdateMultipleRequestsCallback, |
| 127 weak_ptr_factory_.GetWeakPtr())); | 144 weak_ptr_factory_.GetWeakPtr())); |
| 128 } | 145 } |
| 129 | 146 |
| 130 void RequestCoordinator::ResumeRequests( | 147 void RequestCoordinator::ResumeRequests( |
| 131 const std::vector<int64_t>& request_ids) { | 148 const std::vector<int64_t>& request_ids) { |
| 132 queue_->ChangeRequestsState( | 149 queue_->ChangeRequestsState( |
| 133 request_ids, SavePageRequest::RequestState::AVAILABLE, | 150 request_ids, SavePageRequest::RequestState::AVAILABLE, |
| (...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 257 | 274 |
| 258 GetOffliner(); | 275 GetOffliner(); |
| 259 if (!offliner_) { | 276 if (!offliner_) { |
| 260 DVLOG(0) << "Unable to create Offliner. " | 277 DVLOG(0) << "Unable to create Offliner. " |
| 261 << "Cannot background offline page."; | 278 << "Cannot background offline page."; |
| 262 return; | 279 return; |
| 263 } | 280 } |
| 264 | 281 |
| 265 DCHECK(!is_busy_); | 282 DCHECK(!is_busy_); |
| 266 is_busy_ = true; | 283 is_busy_ = true; |
| 267 active_request_.reset(new SavePageRequest(request)); | 284 active_request_.reset(new SavePageRequest(request)); |
|
dougarnett
2016/08/23 17:00:39
We may want to move this down and set with updated
Pete Williamson
2016/08/23 17:48:43
Done.
dougarnett
2016/08/23 17:51:30
not the current problem though, as the MarkAbortAt
| |
| 268 | 285 |
| 269 // Prepare an updated request to attempt. | 286 // Prepare an updated request to attempt. |
| 270 SavePageRequest updated_request(request); | 287 SavePageRequest updated_request(request); |
| 271 updated_request.MarkAttemptStarted(base::Time::Now()); | 288 updated_request.MarkAttemptStarted(base::Time::Now()); |
| 272 | 289 |
| 273 // Start the load and save process in the offliner (Async). | 290 // Start the load and save process in the offliner (Async). |
| 274 if (offliner_->LoadAndSave( | 291 if (offliner_->LoadAndSave( |
| 275 updated_request, base::Bind(&RequestCoordinator::OfflinerDoneCallback, | 292 updated_request, base::Bind(&RequestCoordinator::OfflinerDoneCallback, |
| 276 weak_ptr_factory_.GetWeakPtr()))) { | 293 weak_ptr_factory_.GetWeakPtr()))) { |
| 277 // Offliner accepted request so update it in the queue. | 294 // Offliner accepted request so update it in the queue. |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 311 // TODO(dougarnett): See if we can conclusively identify other attempt | 328 // TODO(dougarnett): See if we can conclusively identify other attempt |
| 312 // aborted cases to treat this way (eg, for Render Process Killed). | 329 // aborted cases to treat this way (eg, for Render Process Killed). |
| 313 SavePageRequest updated_request(request); | 330 SavePageRequest updated_request(request); |
| 314 updated_request.MarkAttemptAborted(); | 331 updated_request.MarkAttemptAborted(); |
| 315 queue_->UpdateRequest(updated_request, | 332 queue_->UpdateRequest(updated_request, |
| 316 base::Bind(&RequestCoordinator::UpdateRequestCallback, | 333 base::Bind(&RequestCoordinator::UpdateRequestCallback, |
| 317 weak_ptr_factory_.GetWeakPtr(), | 334 weak_ptr_factory_.GetWeakPtr(), |
| 318 updated_request.client_id())); | 335 updated_request.client_id())); |
| 319 NotifyCompleted(updated_request, SavePageStatus::FOREGROUND_CANCELED); | 336 NotifyCompleted(updated_request, SavePageStatus::FOREGROUND_CANCELED); |
| 320 | 337 |
| 338 // TODO: Will this be PRERENDERING_CANCELED or REQUEST_COORDINATOR_CANCELED | |
|
dougarnett
2016/08/23 16:32:13
as noted above, I wasn't expecting there to be a c
Pete Williamson
2016/08/23 17:48:43
We don't need one, this change reverted.
| |
| 339 // for a call to | |
| 340 // offliner_->Cancel()? | |
| 341 } else if (status == Offliner::RequestStatus::PRERENDERING_CANCELED) { | |
| 342 // If the user called cancel, the request never had a chance to complete, so | |
| 343 // mark the attempt as aborted, and it may be eligible for retrying later | |
| 344 // (for instance, if it is later unpaused). | |
| 345 SavePageRequest updated_request(request); | |
| 346 updated_request.MarkAttemptAborted(); | |
| 347 queue_->UpdateRequest(updated_request, | |
| 348 base::Bind(&RequestCoordinator::UpdateRequestCallback, | |
| 349 weak_ptr_factory_.GetWeakPtr(), | |
| 350 updated_request.client_id())); | |
| 321 } else if (status == Offliner::RequestStatus::SAVED) { | 351 } else if (status == Offliner::RequestStatus::SAVED) { |
| 322 // Remove the request from the queue if it succeeded. | 352 // Remove the request from the queue if it succeeded. |
| 323 std::vector<int64_t> remove_requests; | 353 std::vector<int64_t> remove_requests; |
| 324 remove_requests.push_back(request.request_id()); | 354 remove_requests.push_back(request.request_id()); |
| 325 queue_->RemoveRequests( | 355 queue_->RemoveRequests( |
| 326 remove_requests, base::Bind(&RequestCoordinator::RemoveRequestsCallback, | 356 remove_requests, base::Bind(&RequestCoordinator::RemoveRequestsCallback, |
| 327 weak_ptr_factory_.GetWeakPtr())); | 357 weak_ptr_factory_.GetWeakPtr())); |
| 328 NotifyCompleted(request, SavePageStatus::SUCCESS); | 358 NotifyCompleted(request, SavePageStatus::SUCCESS); |
| 329 } else if (request.completed_attempt_count() + 1 >= | 359 } else if (request.completed_attempt_count() + 1 >= |
| 330 policy_->GetMaxCompletedTries()) { | 360 policy_->GetMaxCompletedTries()) { |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 405 FOR_EACH_OBSERVER(Observer, observers_, OnChanged(request)); | 435 FOR_EACH_OBSERVER(Observer, observers_, OnChanged(request)); |
| 406 } | 436 } |
| 407 | 437 |
| 408 void RequestCoordinator::GetOffliner() { | 438 void RequestCoordinator::GetOffliner() { |
| 409 if (!offliner_) { | 439 if (!offliner_) { |
| 410 offliner_ = factory_->GetOffliner(policy_.get()); | 440 offliner_ = factory_->GetOffliner(policy_.get()); |
| 411 } | 441 } |
| 412 } | 442 } |
| 413 | 443 |
| 414 } // namespace offline_pages | 444 } // namespace offline_pages |
| OLD | NEW |