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 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
89 RequestQueue::AddRequestResult result, | 89 RequestQueue::AddRequestResult result, |
90 const SavePageRequest& request) { | 90 const SavePageRequest& request) { |
91 | 91 |
92 // Inform the scheduler that we have an outstanding task. | 92 // Inform the scheduler that we have an outstanding task. |
93 // TODO(petewil): Determine trigger conditions from policy. | 93 // TODO(petewil): Determine trigger conditions from policy. |
94 scheduler_->Schedule(GetTriggerConditionsForUserRequest()); | 94 scheduler_->Schedule(GetTriggerConditionsForUserRequest()); |
95 } | 95 } |
96 | 96 |
97 // Called in response to updating a request in the request queue. | 97 // Called in response to updating a request in the request queue. |
98 void RequestCoordinator::UpdateRequestCallback( | 98 void RequestCoordinator::UpdateRequestCallback( |
99 RequestQueue::UpdateRequestResult result) {} | 99 RequestQueue::UpdateRequestResult result) { |
| 100 // If the request succeeded, nothing to do. If it failed, we can't really do |
| 101 // much, so just log it. |
| 102 if (result != RequestQueue::UpdateRequestResult::SUCCESS) { |
| 103 // TODO(petewil): Consider adding UMA or showing on offline-internals page. |
| 104 DLOG(WARNING) << "Failed to update a request retry count. " |
| 105 << static_cast<int>(result); |
| 106 } |
| 107 } |
100 | 108 |
101 void RequestCoordinator::StopProcessing() { | 109 void RequestCoordinator::StopProcessing() { |
102 is_canceled_ = true; | 110 is_canceled_ = true; |
103 if (offliner_ && is_busy_) | 111 if (offliner_ && is_busy_) |
104 offliner_->Cancel(); | 112 offliner_->Cancel(); |
105 | 113 |
106 // Stopping offliner means it will not call callback. | 114 // Stopping offliner means it will not call callback. |
107 last_offlining_status_ = | 115 last_offlining_status_ = |
108 Offliner::RequestStatus::REQUEST_COORDINATOR_CANCELED; | 116 Offliner::RequestStatus::REQUEST_COORDINATOR_CANCELED; |
109 RecordOfflinerResultUMA(last_offlining_status_); | 117 RecordOfflinerResultUMA(last_offlining_status_); |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
190 event_logger_.RecordSavePageRequestUpdated( | 198 event_logger_.RecordSavePageRequestUpdated( |
191 request.client_id().name_space, | 199 request.client_id().name_space, |
192 "Saved", | 200 "Saved", |
193 request.request_id()); | 201 request.request_id()); |
194 last_offlining_status_ = status; | 202 last_offlining_status_ = status; |
195 RecordOfflinerResultUMA(last_offlining_status_); | 203 RecordOfflinerResultUMA(last_offlining_status_); |
196 watchdog_timer_.Stop(); | 204 watchdog_timer_.Stop(); |
197 | 205 |
198 is_busy_ = false; | 206 is_busy_ = false; |
199 | 207 |
200 // If the request succeeded, remove it from the Queue and maybe schedule | 208 int64_t new_attempt_count = request.attempt_count() + 1; |
201 // another one. | 209 |
202 if (status == Offliner::RequestStatus::SAVED) { | 210 // Remove the request from the queue if it either succeeded or exceeded the |
| 211 // max number of retries. |
| 212 if (status == Offliner::RequestStatus::SAVED |
| 213 || new_attempt_count > policy_->GetMaxRetries()) { |
203 queue_->RemoveRequest(request.request_id(), | 214 queue_->RemoveRequest(request.request_id(), |
204 base::Bind(&RequestCoordinator::UpdateRequestCallback, | 215 base::Bind(&RequestCoordinator::UpdateRequestCallback, |
205 weak_ptr_factory_.GetWeakPtr())); | 216 weak_ptr_factory_.GetWeakPtr())); |
| 217 } else { |
| 218 // If we failed, but are not over the limit, update the request in the |
| 219 // queue. |
| 220 SavePageRequest updated_request(request); |
| 221 updated_request.set_attempt_count(new_attempt_count); |
| 222 updated_request.set_last_attempt_time(base::Time::Now()); |
| 223 RequestQueue::UpdateRequestCallback update_callback = |
| 224 base::Bind(&RequestCoordinator::UpdateRequestCallback, |
| 225 weak_ptr_factory_.GetWeakPtr()); |
| 226 queue_->UpdateRequest( |
| 227 updated_request, |
| 228 base::Bind(&RequestCoordinator::UpdateRequestCallback, |
| 229 weak_ptr_factory_.GetWeakPtr())); |
| 230 } |
206 | 231 |
207 // TODO(petewil): Check time budget. Return to the scheduler if we are out. | 232 // TODO(petewil): Check time budget. Return to the scheduler if we are out. |
208 | 233 // Start another request if we have time. |
209 // Start another request if we have time. | 234 TryNextRequest(); |
210 TryNextRequest(); | |
211 } | |
212 } | 235 } |
213 | 236 |
214 const Scheduler::TriggerConditions& | 237 const Scheduler::TriggerConditions& |
215 RequestCoordinator::GetTriggerConditionsForUserRequest() { | 238 RequestCoordinator::GetTriggerConditionsForUserRequest() { |
216 return kUserRequestTriggerConditions; | 239 return kUserRequestTriggerConditions; |
217 } | 240 } |
218 | 241 |
219 void RequestCoordinator::GetOffliner() { | 242 void RequestCoordinator::GetOffliner() { |
220 if (!offliner_) { | 243 if (!offliner_) { |
221 offliner_ = factory_->GetOffliner(policy_.get()); | 244 offliner_ = factory_->GetOffliner(policy_.get()); |
222 } | 245 } |
223 } | 246 } |
224 | 247 |
225 } // namespace offline_pages | 248 } // namespace offline_pages |
OLD | NEW |