| 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 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 84 offline_pages::SavePageRequest request( | 84 offline_pages::SavePageRequest request( |
| 85 id++, url, client_id, base::Time::Now(), user_requested); | 85 id++, url, client_id, base::Time::Now(), user_requested); |
| 86 | 86 |
| 87 // Put the request on the request queue. | 87 // Put the request on the request queue. |
| 88 queue_->AddRequest(request, | 88 queue_->AddRequest(request, |
| 89 base::Bind(&RequestCoordinator::AddRequestResultCallback, | 89 base::Bind(&RequestCoordinator::AddRequestResultCallback, |
| 90 weak_ptr_factory_.GetWeakPtr())); | 90 weak_ptr_factory_.GetWeakPtr())); |
| 91 return true; | 91 return true; |
| 92 } | 92 } |
| 93 void RequestCoordinator::GetQueuedRequests( | 93 void RequestCoordinator::GetQueuedRequests( |
| 94 const std::string& client_namespace, | |
| 95 const QueuedRequestCallback& callback) { | 94 const QueuedRequestCallback& callback) { |
| 96 // Get all matching requests from the request queue, send them to our | 95 // Get all matching requests from the request queue, send them to our |
| 97 // callback. We bind the namespace and callback to the front of the callback | 96 // callback. We bind the namespace and callback to the front of the callback |
| 98 // param set. | 97 // param set. |
| 99 queue_->GetRequests(base::Bind(&RequestCoordinator::GetQueuedRequestsCallback, | 98 queue_->GetRequests(base::Bind(&RequestCoordinator::GetQueuedRequestsCallback, |
| 100 weak_ptr_factory_.GetWeakPtr(), | 99 weak_ptr_factory_.GetWeakPtr(), callback)); |
| 101 client_namespace, callback)); | |
| 102 } | 100 } |
| 103 | 101 |
| 104 // For each request matching the client_namespace, return the ClientId. | |
| 105 void RequestCoordinator::GetQueuedRequestsCallback( | 102 void RequestCoordinator::GetQueuedRequestsCallback( |
| 106 const std::string& client_namespace, | |
| 107 const QueuedRequestCallback& callback, | 103 const QueuedRequestCallback& callback, |
| 108 RequestQueue::GetRequestsResult result, | 104 RequestQueue::GetRequestsResult result, |
| 109 const std::vector<SavePageRequest>& requests) { | 105 const std::vector<SavePageRequest>& requests) { |
| 110 std::vector<ClientId> client_ids; | |
| 111 | 106 |
| 112 for (const auto& request : requests) { | 107 callback.Run(requests); |
| 113 if (client_namespace == request.client_id().name_space) | |
| 114 client_ids.push_back(request.client_id()); | |
| 115 } | |
| 116 | |
| 117 callback.Run(client_ids); | |
| 118 } | 108 } |
| 119 | 109 |
| 120 void RequestCoordinator::RemoveRequests( | 110 void RequestCoordinator::RemoveRequests( |
| 121 const std::vector<ClientId>& client_ids) { | 111 const std::vector<int64_t>& request_ids) { |
| 122 queue_->RemoveRequestsByClientId( | 112 queue_->RemoveRequests( |
| 123 client_ids, base::Bind(&RequestCoordinator::UpdateMultipleRequestCallback, | 113 request_ids, |
| 124 weak_ptr_factory_.GetWeakPtr())); | 114 base::Bind(&RequestCoordinator::UpdateMultipleRequestCallback, |
| 115 weak_ptr_factory_.GetWeakPtr())); |
| 125 } | 116 } |
| 126 | 117 |
| 127 void RequestCoordinator::AddRequestResultCallback( | 118 void RequestCoordinator::AddRequestResultCallback( |
| 128 RequestQueue::AddRequestResult result, | 119 RequestQueue::AddRequestResult result, |
| 129 const SavePageRequest& request) { | 120 const SavePageRequest& request) { |
| 130 | 121 |
| 131 // Inform the scheduler that we have an outstanding task.. | 122 // Inform the scheduler that we have an outstanding task.. |
| 132 scheduler_->Schedule(GetTriggerConditionsForUserRequest()); | 123 scheduler_->Schedule(GetTriggerConditionsForUserRequest()); |
| 133 } | 124 } |
| 134 | 125 |
| (...skipping 168 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 303 updated_request.client_id())); | 294 updated_request.client_id())); |
| 304 | 295 |
| 305 } else if (status == Offliner::RequestStatus::SAVED || | 296 } else if (status == Offliner::RequestStatus::SAVED || |
| 306 request.completed_attempt_count() + 1 >= | 297 request.completed_attempt_count() + 1 >= |
| 307 policy_->GetMaxCompletedTries()) { | 298 policy_->GetMaxCompletedTries()) { |
| 308 // Remove the request from the queue if it either succeeded or exceeded the | 299 // Remove the request from the queue if it either succeeded or exceeded the |
| 309 // max number of retries. The +1 represents the request that just | 300 // max number of retries. The +1 represents the request that just |
| 310 // completed. Since we call MarkAttemptCompleted within the if branches, | 301 // completed. Since we call MarkAttemptCompleted within the if branches, |
| 311 // the completed_attempt_count has not yet been updated when we are checking | 302 // the completed_attempt_count has not yet been updated when we are checking |
| 312 // the if condition. | 303 // the if condition. |
| 313 queue_->RemoveRequest( | 304 std::vector<int64_t> remove_requests; |
| 314 request.request_id(), | 305 remove_requests.push_back(request.request_id()); |
| 306 queue_->RemoveRequests( |
| 307 remove_requests, |
| 315 base::Bind(&RequestCoordinator::UpdateRequestCallback, | 308 base::Bind(&RequestCoordinator::UpdateRequestCallback, |
| 316 weak_ptr_factory_.GetWeakPtr(), request.client_id())); | 309 weak_ptr_factory_.GetWeakPtr(), request.client_id())); |
| 317 } else { | 310 } else { |
| 318 // If we failed, but are not over the limit, update the request in the | 311 // If we failed, but are not over the limit, update the request in the |
| 319 // queue. | 312 // queue. |
| 320 SavePageRequest updated_request(request); | 313 SavePageRequest updated_request(request); |
| 321 updated_request.MarkAttemptCompleted(); | 314 updated_request.MarkAttemptCompleted(); |
| 322 queue_->UpdateRequest(updated_request, | 315 queue_->UpdateRequest(updated_request, |
| 323 base::Bind(&RequestCoordinator::UpdateRequestCallback, | 316 base::Bind(&RequestCoordinator::UpdateRequestCallback, |
| 324 weak_ptr_factory_.GetWeakPtr(), | 317 weak_ptr_factory_.GetWeakPtr(), |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 358 return trigger_conditions; | 351 return trigger_conditions; |
| 359 } | 352 } |
| 360 | 353 |
| 361 void RequestCoordinator::GetOffliner() { | 354 void RequestCoordinator::GetOffliner() { |
| 362 if (!offliner_) { | 355 if (!offliner_) { |
| 363 offliner_ = factory_->GetOffliner(policy_.get()); | 356 offliner_ = factory_->GetOffliner(policy_.get()); |
| 364 } | 357 } |
| 365 } | 358 } |
| 366 | 359 |
| 367 } // namespace offline_pages | 360 } // namespace offline_pages |
| OLD | NEW |