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 27 matching lines...) Expand all Loading... | |
| 38 // macro adapted to allow for a dynamically suffixed histogram name. | 38 // macro adapted to allow for a dynamically suffixed histogram name. |
| 39 // Note: The factory creates and owns the histogram. | 39 // Note: The factory creates and owns the histogram. |
| 40 base::HistogramBase* histogram = base::LinearHistogram::FactoryGet( | 40 base::HistogramBase* histogram = base::LinearHistogram::FactoryGet( |
| 41 histogram_name, 1, | 41 histogram_name, 1, |
| 42 static_cast<int>(Offliner::RequestStatus::STATUS_COUNT), | 42 static_cast<int>(Offliner::RequestStatus::STATUS_COUNT), |
| 43 static_cast<int>(Offliner::RequestStatus::STATUS_COUNT) + 1, | 43 static_cast<int>(Offliner::RequestStatus::STATUS_COUNT) + 1, |
| 44 base::HistogramBase::kUmaTargetedHistogramFlag); | 44 base::HistogramBase::kUmaTargetedHistogramFlag); |
| 45 histogram->Add(static_cast<int>(request_status)); | 45 histogram->Add(static_cast<int>(request_status)); |
| 46 } | 46 } |
| 47 | 47 |
| 48 // Records the number of started attempts for completed requests (whether | |
| 49 // successful or not). | |
| 50 void RecordAttemptCount(const SavePageRequest& request, | |
|
Pete Williamson
2016/09/01 00:27:20
Maybe this should be RecordStartedAttemptCount(),
dougarnett
2016/09/01 19:27:37
Actually, my thought was to just add the completed
| |
| 51 RequestNotifier::SavePageStatus status) { | |
| 52 if (status == RequestNotifier::SavePageStatus::SUCCESS) { | |
| 53 UMA_HISTOGRAM_CUSTOM_COUNTS( | |
| 54 "OfflinePages.Background.RequestSuccess.StartedAttemptCount", | |
| 55 request.started_attempt_count(), 1, 10, 10); | |
| 56 } else { | |
| 57 UMA_HISTOGRAM_CUSTOM_COUNTS( | |
| 58 "OfflinePages.Background.RequestFailure.StartedAttemptCount", | |
| 59 request.started_attempt_count(), 1, 10, 10); | |
| 60 } | |
| 61 } | |
| 62 | |
| 48 // This should use the same algorithm as we use for OfflinePageItem, so the IDs | 63 // This should use the same algorithm as we use for OfflinePageItem, so the IDs |
| 49 // are similar. | 64 // are similar. |
| 50 int64_t GenerateOfflineId() { | 65 int64_t GenerateOfflineId() { |
| 51 return base::RandGenerator(std::numeric_limits<int64_t>::max()) + 1; | 66 return base::RandGenerator(std::numeric_limits<int64_t>::max()) + 1; |
| 52 } | 67 } |
| 53 | 68 |
| 54 // In case we start processing from SavePageLater, we need a callback, but there | 69 // In case we start processing from SavePageLater, we need a callback, but there |
| 55 // is nothing for it to do. | 70 // is nothing for it to do. |
| 56 void EmptySchedulerCallback(bool started) {} | 71 void EmptySchedulerCallback(bool started) {} |
| 57 | 72 |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 112 | 127 |
| 113 void RequestCoordinator::GetQueuedRequestsCallback( | 128 void RequestCoordinator::GetQueuedRequestsCallback( |
| 114 const GetRequestsCallback& callback, | 129 const GetRequestsCallback& callback, |
| 115 RequestQueue::GetRequestsResult result, | 130 RequestQueue::GetRequestsResult result, |
| 116 const std::vector<SavePageRequest>& requests) { | 131 const std::vector<SavePageRequest>& requests) { |
| 117 callback.Run(requests); | 132 callback.Run(requests); |
| 118 } | 133 } |
| 119 | 134 |
| 120 void RequestCoordinator::StopPrerendering() { | 135 void RequestCoordinator::StopPrerendering() { |
| 121 if (offliner_ && is_busy_) { | 136 if (offliner_ && is_busy_) { |
| 137 DCHECK(active_request_.get()); | |
| 122 offliner_->Cancel(); | 138 offliner_->Cancel(); |
| 123 // Find current request and mark attempt aborted. | 139 AbortRequestAttempt(active_request_.get()); |
| 124 active_request_->MarkAttemptAborted(); | |
| 125 queue_->UpdateRequest(*(active_request_.get()), | |
| 126 base::Bind(&RequestCoordinator::UpdateRequestCallback, | |
| 127 weak_ptr_factory_.GetWeakPtr(), | |
| 128 active_request_->client_id())); | |
| 129 } | 140 } |
| 130 | 141 |
| 131 // Stopping offliner means it will not call callback. | 142 // Stopping offliner means it will not call callback. |
| 132 last_offlining_status_ = | 143 last_offlining_status_ = |
| 133 Offliner::RequestStatus::REQUEST_COORDINATOR_CANCELED; | 144 Offliner::RequestStatus::REQUEST_COORDINATOR_CANCELED; |
| 134 | 145 |
| 135 if (active_request_) { | 146 if (active_request_) { |
| 136 RecordOfflinerResultUMA(active_request_->client_id(), | 147 RecordOfflinerResultUMA(active_request_->client_id(), |
| 137 last_offlining_status_); | 148 last_offlining_status_); |
| 138 is_busy_ = false; | 149 is_busy_ = false; |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 150 if (request_ids.end() != std::find(request_ids.begin(), request_ids.end(), | 161 if (request_ids.end() != std::find(request_ids.begin(), request_ids.end(), |
| 151 active_request_->request_id())) { | 162 active_request_->request_id())) { |
| 152 StopPrerendering(); | 163 StopPrerendering(); |
| 153 return true; | 164 return true; |
| 154 } | 165 } |
| 155 } | 166 } |
| 156 | 167 |
| 157 return false; | 168 return false; |
| 158 } | 169 } |
| 159 | 170 |
| 171 void RequestCoordinator::AbortRequestAttempt(SavePageRequest* request) { | |
|
Pete Williamson
2016/09/01 00:27:20
This splits the responsibility for garbage collect
dougarnett
2016/09/01 19:27:37
We are deleting the DoneCallback requests in other
| |
| 172 request->MarkAttemptAborted(); | |
| 173 if (request->started_attempt_count() >= policy_->GetMaxStartedTries()) { | |
| 174 RemoveAttemptedRequest(*request, SavePageStatus::START_COUNT_EXCEEDED); | |
| 175 } else { | |
| 176 queue_->UpdateRequest( | |
| 177 *request, | |
| 178 base::Bind(&RequestCoordinator::UpdateRequestCallback, | |
| 179 weak_ptr_factory_.GetWeakPtr(), request->client_id())); | |
| 180 } | |
| 181 } | |
| 182 | |
| 183 void RequestCoordinator::RemoveAttemptedRequest(const SavePageRequest& request, | |
| 184 SavePageStatus status) { | |
| 185 std::vector<int64_t> remove_requests; | |
| 186 remove_requests.push_back(request.request_id()); | |
| 187 queue_->RemoveRequests(remove_requests, | |
| 188 base::Bind(&RequestCoordinator::HandleRemovedRequests, | |
| 189 weak_ptr_factory_.GetWeakPtr(), status)); | |
| 190 RecordAttemptCount(request, status); | |
| 191 } | |
| 192 | |
| 160 void RequestCoordinator::RemoveRequests( | 193 void RequestCoordinator::RemoveRequests( |
| 161 const std::vector<int64_t>& request_ids, | 194 const std::vector<int64_t>& request_ids, |
| 162 const RemoveRequestsCallback& callback) { | 195 const RemoveRequestsCallback& callback) { |
| 163 bool canceled = CancelActiveRequestIfItMatches(request_ids); | 196 bool canceled = CancelActiveRequestIfItMatches(request_ids); |
| 164 queue_->RemoveRequests( | 197 queue_->RemoveRequests( |
| 165 request_ids, | 198 request_ids, |
| 166 base::Bind(&RequestCoordinator::HandleRemovedRequestsAndCallback, | 199 base::Bind(&RequestCoordinator::HandleRemovedRequestsAndCallback, |
| 167 weak_ptr_factory_.GetWeakPtr(), callback, | 200 weak_ptr_factory_.GetWeakPtr(), callback, |
| 168 SavePageStatus::REMOVED)); | 201 SavePageStatus::REMOVED)); |
| 169 if (canceled) | 202 if (canceled) |
| (...skipping 235 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 405 | 438 |
| 406 is_busy_ = false; | 439 is_busy_ = false; |
| 407 active_request_.reset(nullptr); | 440 active_request_.reset(nullptr); |
| 408 | 441 |
| 409 if (status == Offliner::RequestStatus::FOREGROUND_CANCELED || | 442 if (status == Offliner::RequestStatus::FOREGROUND_CANCELED || |
| 410 status == Offliner::RequestStatus::PRERENDERING_CANCELED) { | 443 status == Offliner::RequestStatus::PRERENDERING_CANCELED) { |
| 411 // Update the request for the canceled attempt. | 444 // Update the request for the canceled attempt. |
| 412 // TODO(dougarnett): See if we can conclusively identify other attempt | 445 // TODO(dougarnett): See if we can conclusively identify other attempt |
| 413 // aborted cases to treat this way (eg, for Render Process Killed). | 446 // aborted cases to treat this way (eg, for Render Process Killed). |
| 414 SavePageRequest updated_request(request); | 447 SavePageRequest updated_request(request); |
| 415 updated_request.MarkAttemptAborted(); | 448 AbortRequestAttempt(&updated_request); |
| 416 queue_->UpdateRequest(updated_request, | |
| 417 base::Bind(&RequestCoordinator::UpdateRequestCallback, | |
| 418 weak_ptr_factory_.GetWeakPtr(), | |
| 419 updated_request.client_id())); | |
| 420 SavePageStatus notify_status = | 449 SavePageStatus notify_status = |
| 421 (status == Offliner::RequestStatus::FOREGROUND_CANCELED) | 450 (status == Offliner::RequestStatus::FOREGROUND_CANCELED) |
| 422 ? SavePageStatus::FOREGROUND_CANCELED | 451 ? SavePageStatus::FOREGROUND_CANCELED |
| 423 : SavePageStatus::PRERENDER_CANCELED; | 452 : SavePageStatus::PRERENDER_CANCELED; |
| 424 NotifyCompleted(updated_request, notify_status); | 453 NotifyCompleted(updated_request, notify_status); |
| 425 | |
| 426 } else if (status == Offliner::RequestStatus::SAVED) { | 454 } else if (status == Offliner::RequestStatus::SAVED) { |
| 427 // Remove the request from the queue if it succeeded. | 455 // Remove the request from the queue if it succeeded. |
| 428 std::vector<int64_t> remove_requests; | 456 RemoveAttemptedRequest(request, SavePageStatus::SUCCESS); |
| 429 remove_requests.push_back(request.request_id()); | |
| 430 queue_->RemoveRequests( | |
| 431 remove_requests, | |
| 432 base::Bind(&RequestCoordinator::HandleRemovedRequests, | |
| 433 weak_ptr_factory_.GetWeakPtr(), SavePageStatus::SUCCESS)); | |
| 434 } else if (request.completed_attempt_count() + 1 >= | 457 } else if (request.completed_attempt_count() + 1 >= |
| 435 policy_->GetMaxCompletedTries()) { | 458 policy_->GetMaxCompletedTries()) { |
| 436 // Remove from the request queue if we exceeeded max retries. The +1 | 459 // Remove from the request queue if we exceeded max retries. The +1 |
| 437 // represents the request that just completed. Since we call | 460 // represents the request that just completed. Since we call |
| 438 // MarkAttemptCompleted within the if branches, the completed_attempt_count | 461 // MarkAttemptCompleted within the if branches, the completed_attempt_count |
| 439 // has not yet been updated when we are checking the if condition. | 462 // has not yet been updated when we are checking the if condition. |
| 440 std::vector<int64_t> remove_requests; | 463 RemoveAttemptedRequest(request, SavePageStatus::RETRY_COUNT_EXCEEDED); |
| 441 remove_requests.push_back(request.request_id()); | |
| 442 queue_->RemoveRequests( | |
| 443 remove_requests, base::Bind(&RequestCoordinator::HandleRemovedRequests, | |
| 444 weak_ptr_factory_.GetWeakPtr(), | |
| 445 SavePageStatus::RETRY_COUNT_EXCEEDED)); | |
| 446 } else { | 464 } else { |
| 447 // If we failed, but are not over the limit, update the request in the | 465 // If we failed, but are not over the limit, update the request in the |
| 448 // queue. | 466 // queue. |
| 449 SavePageRequest updated_request(request); | 467 SavePageRequest updated_request(request); |
| 450 updated_request.MarkAttemptCompleted(); | 468 updated_request.MarkAttemptCompleted(); |
| 451 queue_->UpdateRequest(updated_request, | 469 queue_->UpdateRequest(updated_request, |
| 452 base::Bind(&RequestCoordinator::UpdateRequestCallback, | 470 base::Bind(&RequestCoordinator::UpdateRequestCallback, |
| 453 weak_ptr_factory_.GetWeakPtr(), | 471 weak_ptr_factory_.GetWeakPtr(), |
| 454 updated_request.client_id())); | 472 updated_request.client_id())); |
| 455 NotifyChanged(updated_request); | 473 NotifyChanged(updated_request); |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 510 FOR_EACH_OBSERVER(Observer, observers_, OnChanged(request)); | 528 FOR_EACH_OBSERVER(Observer, observers_, OnChanged(request)); |
| 511 } | 529 } |
| 512 | 530 |
| 513 void RequestCoordinator::GetOffliner() { | 531 void RequestCoordinator::GetOffliner() { |
| 514 if (!offliner_) { | 532 if (!offliner_) { |
| 515 offliner_ = factory_->GetOffliner(policy_.get()); | 533 offliner_ = factory_->GetOffliner(policy_.get()); |
| 516 } | 534 } |
| 517 } | 535 } |
| 518 | 536 |
| 519 } // namespace offline_pages | 537 } // namespace offline_pages |
| OLD | NEW |