| 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 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 42 histogram_name, 1, | 42 histogram_name, 1, |
| 43 static_cast<int>(Offliner::RequestStatus::STATUS_COUNT), | 43 static_cast<int>(Offliner::RequestStatus::STATUS_COUNT), |
| 44 static_cast<int>(Offliner::RequestStatus::STATUS_COUNT) + 1, | 44 static_cast<int>(Offliner::RequestStatus::STATUS_COUNT) + 1, |
| 45 base::HistogramBase::kUmaTargetedHistogramFlag); | 45 base::HistogramBase::kUmaTargetedHistogramFlag); |
| 46 histogram->Add(static_cast<int>(request_status)); | 46 histogram->Add(static_cast<int>(request_status)); |
| 47 } | 47 } |
| 48 | 48 |
| 49 // Records the number of started attempts for completed requests (whether | 49 // Records the number of started attempts for completed requests (whether |
| 50 // successful or not). | 50 // successful or not). |
| 51 void RecordAttemptCount(const SavePageRequest& request, | 51 void RecordAttemptCount(const SavePageRequest& request, |
| 52 RequestNotifier::SavePageStatus status) { | 52 RequestNotifier::BackgroundSavePageResult status) { |
| 53 if (status == RequestNotifier::SavePageStatus::SUCCESS) { | 53 if (status == RequestNotifier::BackgroundSavePageResult::SUCCESS) { |
| 54 // TODO(dougarnett): Also record UMA for completed attempts here. | 54 // TODO(dougarnett): Also record UMA for completed attempts here. |
| 55 UMA_HISTOGRAM_CUSTOM_COUNTS( | 55 UMA_HISTOGRAM_CUSTOM_COUNTS( |
| 56 "OfflinePages.Background.RequestSuccess.StartedAttemptCount", | 56 "OfflinePages.Background.RequestSuccess.StartedAttemptCount", |
| 57 request.started_attempt_count(), 1, 10, 11); | 57 request.started_attempt_count(), 1, 10, 11); |
| 58 } else { | 58 } else { |
| 59 UMA_HISTOGRAM_CUSTOM_COUNTS( | 59 UMA_HISTOGRAM_CUSTOM_COUNTS( |
| 60 "OfflinePages.Background.RequestFailure.StartedAttemptCount", | 60 "OfflinePages.Background.RequestFailure.StartedAttemptCount", |
| 61 request.started_attempt_count(), 1, 10, 11); | 61 request.started_attempt_count(), 1, 10, 11); |
| 62 } | 62 } |
| 63 } | 63 } |
| (...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 186 return true; | 186 return true; |
| 187 } | 187 } |
| 188 } | 188 } |
| 189 | 189 |
| 190 return false; | 190 return false; |
| 191 } | 191 } |
| 192 | 192 |
| 193 void RequestCoordinator::AbortRequestAttempt(SavePageRequest* request) { | 193 void RequestCoordinator::AbortRequestAttempt(SavePageRequest* request) { |
| 194 request->MarkAttemptAborted(); | 194 request->MarkAttemptAborted(); |
| 195 if (request->started_attempt_count() >= policy_->GetMaxStartedTries()) { | 195 if (request->started_attempt_count() >= policy_->GetMaxStartedTries()) { |
| 196 RemoveAttemptedRequest(*request, SavePageStatus::START_COUNT_EXCEEDED); | 196 RemoveAttemptedRequest(*request, |
| 197 BackgroundSavePageResult::START_COUNT_EXCEEDED); |
| 197 } else { | 198 } else { |
| 198 queue_->UpdateRequest( | 199 queue_->UpdateRequest( |
| 199 *request, | 200 *request, |
| 200 base::Bind(&RequestCoordinator::UpdateRequestCallback, | 201 base::Bind(&RequestCoordinator::UpdateRequestCallback, |
| 201 weak_ptr_factory_.GetWeakPtr(), request->client_id())); | 202 weak_ptr_factory_.GetWeakPtr(), request->client_id())); |
| 202 } | 203 } |
| 203 } | 204 } |
| 204 | 205 |
| 205 void RequestCoordinator::RemoveAttemptedRequest(const SavePageRequest& request, | 206 void RequestCoordinator::RemoveAttemptedRequest( |
| 206 SavePageStatus status) { | 207 const SavePageRequest& request, BackgroundSavePageResult status) { |
| 207 std::vector<int64_t> remove_requests; | 208 std::vector<int64_t> remove_requests; |
| 208 remove_requests.push_back(request.request_id()); | 209 remove_requests.push_back(request.request_id()); |
| 209 queue_->RemoveRequests(remove_requests, | 210 queue_->RemoveRequests(remove_requests, |
| 210 base::Bind(&RequestCoordinator::HandleRemovedRequests, | 211 base::Bind(&RequestCoordinator::HandleRemovedRequests, |
| 211 weak_ptr_factory_.GetWeakPtr(), status)); | 212 weak_ptr_factory_.GetWeakPtr(), status)); |
| 212 RecordAttemptCount(request, status); | 213 RecordAttemptCount(request, status); |
| 213 } | 214 } |
| 214 | 215 |
| 215 void RequestCoordinator::RemoveRequests( | 216 void RequestCoordinator::RemoveRequests( |
| 216 const std::vector<int64_t>& request_ids, | 217 const std::vector<int64_t>& request_ids, |
| 217 const RemoveRequestsCallback& callback) { | 218 const RemoveRequestsCallback& callback) { |
| 218 bool canceled = CancelActiveRequestIfItMatches(request_ids); | 219 bool canceled = CancelActiveRequestIfItMatches(request_ids); |
| 219 queue_->RemoveRequests( | 220 queue_->RemoveRequests( |
| 220 request_ids, | 221 request_ids, |
| 221 base::Bind(&RequestCoordinator::HandleRemovedRequestsAndCallback, | 222 base::Bind(&RequestCoordinator::HandleRemovedRequestsAndCallback, |
| 222 weak_ptr_factory_.GetWeakPtr(), callback, | 223 weak_ptr_factory_.GetWeakPtr(), callback, |
| 223 SavePageStatus::REMOVED)); | 224 BackgroundSavePageResult::REMOVED)); |
| 224 if (canceled) | 225 if (canceled) |
| 225 TryNextRequest(); | 226 TryNextRequest(); |
| 226 } | 227 } |
| 227 | 228 |
| 228 void RequestCoordinator::PauseRequests( | 229 void RequestCoordinator::PauseRequests( |
| 229 const std::vector<int64_t>& request_ids) { | 230 const std::vector<int64_t>& request_ids) { |
| 230 bool canceled = CancelActiveRequestIfItMatches(request_ids); | 231 bool canceled = CancelActiveRequestIfItMatches(request_ids); |
| 231 queue_->ChangeRequestsState( | 232 queue_->ChangeRequestsState( |
| 232 request_ids, SavePageRequest::RequestState::PAUSED, | 233 request_ids, SavePageRequest::RequestState::PAUSED, |
| 233 base::Bind(&RequestCoordinator::UpdateMultipleRequestsCallback, | 234 base::Bind(&RequestCoordinator::UpdateMultipleRequestsCallback, |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 302 } | 303 } |
| 303 } | 304 } |
| 304 } | 305 } |
| 305 | 306 |
| 306 if (available_user_request) | 307 if (available_user_request) |
| 307 StartProcessingIfConnected(); | 308 StartProcessingIfConnected(); |
| 308 } | 309 } |
| 309 | 310 |
| 310 void RequestCoordinator::HandleRemovedRequestsAndCallback( | 311 void RequestCoordinator::HandleRemovedRequestsAndCallback( |
| 311 const RemoveRequestsCallback& callback, | 312 const RemoveRequestsCallback& callback, |
| 312 SavePageStatus status, | 313 BackgroundSavePageResult status, |
| 313 const RequestQueue::UpdateMultipleRequestResults& results, | 314 const RequestQueue::UpdateMultipleRequestResults& results, |
| 314 const std::vector<SavePageRequest>& requests) { | 315 const std::vector<SavePageRequest>& requests) { |
| 315 callback.Run(results); | 316 callback.Run(results); |
| 316 HandleRemovedRequests(status, results, requests); | 317 HandleRemovedRequests(status, results, requests); |
| 317 } | 318 } |
| 318 | 319 |
| 319 void RequestCoordinator::HandleRemovedRequests( | 320 void RequestCoordinator::HandleRemovedRequests( |
| 320 SavePageStatus status, | 321 BackgroundSavePageResult status, |
| 321 const RequestQueue::UpdateMultipleRequestResults& results, | 322 const RequestQueue::UpdateMultipleRequestResults& results, |
| 322 const std::vector<SavePageRequest>& requests) { | 323 const std::vector<SavePageRequest>& requests) { |
| 323 for (SavePageRequest request : requests) | 324 for (SavePageRequest request : requests) |
| 324 NotifyCompleted(request, status); | 325 NotifyCompleted(request, status); |
| 325 } | 326 } |
| 326 | 327 |
| 327 void RequestCoordinator::ScheduleAsNeeded() { | 328 void RequestCoordinator::ScheduleAsNeeded() { |
| 328 // Get all requests from queue (there is no filtering mechanism). | 329 // Get all requests from queue (there is no filtering mechanism). |
| 329 queue_->GetRequests( | 330 queue_->GetRequests( |
| 330 base::Bind(&RequestCoordinator::GetRequestsForSchedulingCallback, | 331 base::Bind(&RequestCoordinator::GetRequestsForSchedulingCallback, |
| (...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 472 is_busy_ = false; | 473 is_busy_ = false; |
| 473 active_request_.reset(nullptr); | 474 active_request_.reset(nullptr); |
| 474 | 475 |
| 475 if (status == Offliner::RequestStatus::FOREGROUND_CANCELED || | 476 if (status == Offliner::RequestStatus::FOREGROUND_CANCELED || |
| 476 status == Offliner::RequestStatus::PRERENDERING_CANCELED) { | 477 status == Offliner::RequestStatus::PRERENDERING_CANCELED) { |
| 477 // Update the request for the canceled attempt. | 478 // Update the request for the canceled attempt. |
| 478 // TODO(dougarnett): See if we can conclusively identify other attempt | 479 // TODO(dougarnett): See if we can conclusively identify other attempt |
| 479 // aborted cases to treat this way (eg, for Render Process Killed). | 480 // aborted cases to treat this way (eg, for Render Process Killed). |
| 480 SavePageRequest updated_request(request); | 481 SavePageRequest updated_request(request); |
| 481 AbortRequestAttempt(&updated_request); | 482 AbortRequestAttempt(&updated_request); |
| 482 SavePageStatus notify_status = | 483 BackgroundSavePageResult notify_status = |
| 483 (status == Offliner::RequestStatus::FOREGROUND_CANCELED) | 484 (status == Offliner::RequestStatus::FOREGROUND_CANCELED) |
| 484 ? SavePageStatus::FOREGROUND_CANCELED | 485 ? BackgroundSavePageResult::FOREGROUND_CANCELED |
| 485 : SavePageStatus::PRERENDER_CANCELED; | 486 : BackgroundSavePageResult::PRERENDER_CANCELED; |
| 486 NotifyCompleted(updated_request, notify_status); | 487 NotifyCompleted(updated_request, notify_status); |
| 487 } else if (status == Offliner::RequestStatus::SAVED) { | 488 } else if (status == Offliner::RequestStatus::SAVED) { |
| 488 // Remove the request from the queue if it succeeded. | 489 // Remove the request from the queue if it succeeded. |
| 489 RemoveAttemptedRequest(request, SavePageStatus::SUCCESS); | 490 RemoveAttemptedRequest(request, BackgroundSavePageResult::SUCCESS); |
| 490 } else if (request.completed_attempt_count() + 1 >= | 491 } else if (request.completed_attempt_count() + 1 >= |
| 491 policy_->GetMaxCompletedTries()) { | 492 policy_->GetMaxCompletedTries()) { |
| 492 // Remove from the request queue if we exceeded max retries. The +1 | 493 // Remove from the request queue if we exceeded max retries. The +1 |
| 493 // represents the request that just completed. Since we call | 494 // represents the request that just completed. Since we call |
| 494 // MarkAttemptCompleted within the if branches, the completed_attempt_count | 495 // MarkAttemptCompleted within the if branches, the completed_attempt_count |
| 495 // has not yet been updated when we are checking the if condition. | 496 // has not yet been updated when we are checking the if condition. |
| 496 RemoveAttemptedRequest(request, SavePageStatus::RETRY_COUNT_EXCEEDED); | 497 RemoveAttemptedRequest(request, |
| 498 BackgroundSavePageResult::RETRY_COUNT_EXCEEDED); |
| 497 } else { | 499 } else { |
| 498 // If we failed, but are not over the limit, update the request in the | 500 // If we failed, but are not over the limit, update the request in the |
| 499 // queue. | 501 // queue. |
| 500 SavePageRequest updated_request(request); | 502 SavePageRequest updated_request(request); |
| 501 updated_request.MarkAttemptCompleted(); | 503 updated_request.MarkAttemptCompleted(); |
| 502 queue_->UpdateRequest(updated_request, | 504 queue_->UpdateRequest(updated_request, |
| 503 base::Bind(&RequestCoordinator::UpdateRequestCallback, | 505 base::Bind(&RequestCoordinator::UpdateRequestCallback, |
| 504 weak_ptr_factory_.GetWeakPtr(), | 506 weak_ptr_factory_.GetWeakPtr(), |
| 505 updated_request.client_id())); | 507 updated_request.client_id())); |
| 506 NotifyChanged(updated_request); | 508 NotifyChanged(updated_request); |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 545 | 547 |
| 546 void RequestCoordinator::RemoveObserver(Observer* observer) { | 548 void RequestCoordinator::RemoveObserver(Observer* observer) { |
| 547 observers_.RemoveObserver(observer); | 549 observers_.RemoveObserver(observer); |
| 548 } | 550 } |
| 549 | 551 |
| 550 void RequestCoordinator::NotifyAdded(const SavePageRequest& request) { | 552 void RequestCoordinator::NotifyAdded(const SavePageRequest& request) { |
| 551 FOR_EACH_OBSERVER(Observer, observers_, OnAdded(request)); | 553 FOR_EACH_OBSERVER(Observer, observers_, OnAdded(request)); |
| 552 } | 554 } |
| 553 | 555 |
| 554 void RequestCoordinator::NotifyCompleted(const SavePageRequest& request, | 556 void RequestCoordinator::NotifyCompleted(const SavePageRequest& request, |
| 555 SavePageStatus status) { | 557 BackgroundSavePageResult status) { |
| 556 FOR_EACH_OBSERVER(Observer, observers_, OnCompleted(request, status)); | 558 FOR_EACH_OBSERVER(Observer, observers_, OnCompleted(request, status)); |
| 557 } | 559 } |
| 558 | 560 |
| 559 void RequestCoordinator::NotifyChanged(const SavePageRequest& request) { | 561 void RequestCoordinator::NotifyChanged(const SavePageRequest& request) { |
| 560 FOR_EACH_OBSERVER(Observer, observers_, OnChanged(request)); | 562 FOR_EACH_OBSERVER(Observer, observers_, OnChanged(request)); |
| 561 } | 563 } |
| 562 | 564 |
| 563 void RequestCoordinator::GetOffliner() { | 565 void RequestCoordinator::GetOffliner() { |
| 564 if (!offliner_) { | 566 if (!offliner_) { |
| 565 offliner_ = factory_->GetOffliner(policy_.get()); | 567 offliner_ = factory_->GetOffliner(policy_.get()); |
| 566 } | 568 } |
| 567 } | 569 } |
| 568 | 570 |
| 569 } // namespace offline_pages | 571 } // namespace offline_pages |
| OLD | NEW |