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 189 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 200 return net::NetworkChangeNotifier::GetConnectionType(); | 200 return net::NetworkChangeNotifier::GetConnectionType(); |
| 201 } | 201 } |
| 202 | 202 |
| 203 void RequestCoordinator::AddRequestResultCallback( | 203 void RequestCoordinator::AddRequestResultCallback( |
| 204 RequestQueue::AddRequestResult result, | 204 RequestQueue::AddRequestResult result, |
| 205 const SavePageRequest& request) { | 205 const SavePageRequest& request) { |
| 206 NotifyAdded(request); | 206 NotifyAdded(request); |
| 207 // Inform the scheduler that we have an outstanding task.. | 207 // Inform the scheduler that we have an outstanding task.. |
| 208 scheduler_->Schedule(GetTriggerConditionsForUserRequest()); | 208 scheduler_->Schedule(GetTriggerConditionsForUserRequest()); |
| 209 | 209 |
| 210 // If it makes sense, start processing now. | 210 if (request.user_requested()) |
| 211 if (is_busy_) return; | 211 StartProcessingIfConnected(); |
| 212 | |
| 213 // Check for network | |
| 214 net::NetworkChangeNotifier::ConnectionType connection = GetConnectionType(); | |
| 215 | |
| 216 if ((connection != | |
| 217 net::NetworkChangeNotifier::ConnectionType::CONNECTION_NONE) && | |
| 218 request.user_requested()) { | |
| 219 // Create device conditions. | |
| 220 DeviceConditions device_conditions(false, 0, connection); | |
| 221 | |
| 222 // Start processing if it makes sense. (net, user requested) | |
| 223 StartProcessing(device_conditions, base::Bind(&EmptySchedulerCallback)); | |
| 224 } | |
| 225 } | 212 } |
| 226 | 213 |
| 227 // Called in response to updating a request in the request queue. | 214 // Called in response to updating a request in the request queue. |
| 228 void RequestCoordinator::UpdateRequestCallback( | 215 void RequestCoordinator::UpdateRequestCallback( |
| 229 const ClientId& client_id, | 216 const ClientId& client_id, |
| 230 RequestQueue::UpdateRequestResult result) { | 217 RequestQueue::UpdateRequestResult result) { |
| 231 // If the request succeeded, nothing to do. If it failed, we can't really do | 218 // If the request succeeded, nothing to do. If it failed, we can't really do |
| 232 // much, so just log it. | 219 // much, so just log it. |
| 233 if (result != RequestQueue::UpdateRequestResult::SUCCESS) { | 220 if (result != RequestQueue::UpdateRequestResult::SUCCESS) { |
| 234 DVLOG(1) << "Failed to update request attempt details. " | 221 DVLOG(1) << "Failed to update request attempt details. " |
| 235 << static_cast<int>(result); | 222 << static_cast<int>(result); |
| 236 event_logger_.RecordUpdateRequestFailed(client_id.name_space, result); | 223 event_logger_.RecordUpdateRequestFailed(client_id.name_space, result); |
| 237 } | 224 } |
| 238 } | 225 } |
| 239 | 226 |
| 240 // Called in response to updating multiple requests in the request queue. | 227 // Called in response to updating multiple requests in the request queue. |
| 241 void RequestCoordinator::UpdateMultipleRequestsCallback( | 228 void RequestCoordinator::UpdateMultipleRequestsCallback( |
| 242 const RequestQueue::UpdateMultipleRequestResults& results, | 229 const RequestQueue::UpdateMultipleRequestResults& results, |
| 243 const std::vector<SavePageRequest>& requests) { | 230 const std::vector<SavePageRequest>& requests) { |
| 244 for (SavePageRequest request : requests) | 231 |
| 232 bool available_user_request = false; | |
| 233 for (SavePageRequest request : requests) { | |
| 245 NotifyChanged(request); | 234 NotifyChanged(request); |
| 235 if (request.user_requested() && | |
| 236 request.request_state() == SavePageRequest::RequestState::AVAILABLE) | |
|
Pete Williamson
2016/08/26 20:39:12
We aren't checking the results, which makes me won
dougarnett
2016/08/26 20:57:47
Good point - we could start processing whatever ne
dougarnett
2016/08/26 21:20:19
uploaded change to just guard the new flag for cal
| |
| 237 available_user_request = true; | |
| 238 } | |
| 239 | |
| 240 if (available_user_request) | |
| 241 StartProcessingIfConnected(); | |
| 246 } | 242 } |
| 247 | 243 |
| 248 void RequestCoordinator::HandleRemovedRequestsAndCallback( | 244 void RequestCoordinator::HandleRemovedRequestsAndCallback( |
| 249 const RemoveRequestsCallback& callback, | 245 const RemoveRequestsCallback& callback, |
| 250 SavePageStatus status, | 246 SavePageStatus status, |
| 251 const RequestQueue::UpdateMultipleRequestResults& results, | 247 const RequestQueue::UpdateMultipleRequestResults& results, |
| 252 const std::vector<SavePageRequest>& requests) { | 248 const std::vector<SavePageRequest>& requests) { |
| 253 callback.Run(results); | 249 callback.Run(results); |
| 254 HandleRemovedRequests(status, results, requests); | 250 HandleRemovedRequests(status, results, requests); |
| 255 } | 251 } |
| (...skipping 27 matching lines...) Expand all Loading... | |
| 283 operation_start_time_ = base::Time::Now(); | 279 operation_start_time_ = base::Time::Now(); |
| 284 | 280 |
| 285 is_stopped_ = false; | 281 is_stopped_ = false; |
| 286 scheduler_callback_ = callback; | 282 scheduler_callback_ = callback; |
| 287 | 283 |
| 288 TryNextRequest(); | 284 TryNextRequest(); |
| 289 | 285 |
| 290 return true; | 286 return true; |
| 291 } | 287 } |
| 292 | 288 |
| 289 void RequestCoordinator::StartProcessingIfConnected() { | |
| 290 // Makes sure not already busy processing. | |
| 291 if (is_busy_) return; | |
| 292 | |
| 293 // Check for network connectivity. | |
| 294 net::NetworkChangeNotifier::ConnectionType connection = GetConnectionType(); | |
| 295 | |
| 296 if ((connection != | |
| 297 net::NetworkChangeNotifier::ConnectionType::CONNECTION_NONE)) { | |
| 298 // Create conservative device conditions for the connectivity | |
| 299 // (assume no battery). | |
| 300 DeviceConditions device_conditions(false, 0, connection); | |
| 301 StartProcessing(device_conditions, base::Bind(&EmptySchedulerCallback)); | |
| 302 } | |
| 303 } | |
| 304 | |
| 293 void RequestCoordinator::TryNextRequest() { | 305 void RequestCoordinator::TryNextRequest() { |
| 294 // If there is no time left in the budget, return to the scheduler. | 306 // If there is no time left in the budget, return to the scheduler. |
| 295 // We do not remove the pending task that was set up earlier in case | 307 // We do not remove the pending task that was set up earlier in case |
| 296 // we run out of time, so the background scheduler will return to us | 308 // we run out of time, so the background scheduler will return to us |
| 297 // at the next opportunity to run background tasks. | 309 // at the next opportunity to run background tasks. |
| 298 if (base::Time::Now() - operation_start_time_ > | 310 if (base::Time::Now() - operation_start_time_ > |
| 299 base::TimeDelta::FromSeconds( | 311 base::TimeDelta::FromSeconds( |
| 300 policy_->GetBackgroundProcessingTimeBudgetSeconds())) { | 312 policy_->GetBackgroundProcessingTimeBudgetSeconds())) { |
| 301 // Let the scheduler know we are done processing. | 313 // Let the scheduler know we are done processing. |
| 302 scheduler_callback_.Run(true); | 314 scheduler_callback_.Run(true); |
| (...skipping 180 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 483 FOR_EACH_OBSERVER(Observer, observers_, OnChanged(request)); | 495 FOR_EACH_OBSERVER(Observer, observers_, OnChanged(request)); |
| 484 } | 496 } |
| 485 | 497 |
| 486 void RequestCoordinator::GetOffliner() { | 498 void RequestCoordinator::GetOffliner() { |
| 487 if (!offliner_) { | 499 if (!offliner_) { |
| 488 offliner_ = factory_->GetOffliner(policy_.get()); | 500 offliner_ = factory_->GetOffliner(policy_.get()); |
| 489 } | 501 } |
| 490 } | 502 } |
| 491 | 503 |
| 492 } // namespace offline_pages | 504 } // namespace offline_pages |
| OLD | NEW |