| 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 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 83 // Build a SavePageRequest. | 83 // Build a SavePageRequest. |
| 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::GetAllRequests(const GetRequestsCallback& callback) { |
| 94 const std::string& client_namespace, | |
| 95 const QueuedRequestCallback& callback) { | |
| 96 // Get all matching requests from the request queue, send them to our | 94 // 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 | 95 // callback. We bind the namespace and callback to the front of the callback |
| 98 // param set. | 96 // param set. |
| 99 queue_->GetRequests(base::Bind(&RequestCoordinator::GetQueuedRequestsCallback, | 97 queue_->GetRequests(base::Bind(&RequestCoordinator::GetQueuedRequestsCallback, |
| 100 weak_ptr_factory_.GetWeakPtr(), | 98 weak_ptr_factory_.GetWeakPtr(), callback)); |
| 101 client_namespace, callback)); | |
| 102 } | 99 } |
| 103 | 100 |
| 104 // For each request matching the client_namespace, return the ClientId. | |
| 105 void RequestCoordinator::GetQueuedRequestsCallback( | 101 void RequestCoordinator::GetQueuedRequestsCallback( |
| 106 const std::string& client_namespace, | 102 const GetRequestsCallback& callback, |
| 107 const QueuedRequestCallback& callback, | |
| 108 RequestQueue::GetRequestsResult result, | 103 RequestQueue::GetRequestsResult result, |
| 109 const std::vector<SavePageRequest>& requests) { | 104 const std::vector<SavePageRequest>& requests) { |
| 110 std::vector<ClientId> client_ids; | 105 callback.Run(requests); |
| 111 | |
| 112 for (const auto& request : 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 } | 106 } |
| 119 | 107 |
| 120 void RequestCoordinator::RemoveRequests( | 108 void RequestCoordinator::RemoveRequests( |
| 121 const std::vector<ClientId>& client_ids) { | 109 const std::vector<int64_t>& request_ids) { |
| 122 queue_->RemoveRequestsByClientId( | 110 queue_->RemoveRequests(request_ids, |
| 123 client_ids, base::Bind(&RequestCoordinator::UpdateMultipleRequestCallback, | 111 base::Bind(&RequestCoordinator::RemoveRequestsCallback, |
| 124 weak_ptr_factory_.GetWeakPtr())); | 112 weak_ptr_factory_.GetWeakPtr())); |
| 125 } | 113 } |
| 126 | 114 |
| 127 void RequestCoordinator::AddRequestResultCallback( | 115 void RequestCoordinator::AddRequestResultCallback( |
| 128 RequestQueue::AddRequestResult result, | 116 RequestQueue::AddRequestResult result, |
| 129 const SavePageRequest& request) { | 117 const SavePageRequest& request) { |
| 130 | 118 |
| 131 // Inform the scheduler that we have an outstanding task.. | 119 // Inform the scheduler that we have an outstanding task.. |
| 132 scheduler_->Schedule(GetTriggerConditionsForUserRequest()); | 120 scheduler_->Schedule(GetTriggerConditionsForUserRequest()); |
| 133 } | 121 } |
| 134 | 122 |
| (...skipping 14 matching lines...) Expand all Loading... |
| 149 void RequestCoordinator::UpdateMultipleRequestCallback( | 137 void RequestCoordinator::UpdateMultipleRequestCallback( |
| 150 RequestQueue::UpdateRequestResult result) { | 138 RequestQueue::UpdateRequestResult result) { |
| 151 // If the request succeeded, nothing to do. If it failed, we can't really do | 139 // If the request succeeded, nothing to do. If it failed, we can't really do |
| 152 // much, so just log it. | 140 // much, so just log it. |
| 153 if (result != RequestQueue::UpdateRequestResult::SUCCESS) { | 141 if (result != RequestQueue::UpdateRequestResult::SUCCESS) { |
| 154 DVLOG(1) << "Failed to update request attempt details. " | 142 DVLOG(1) << "Failed to update request attempt details. " |
| 155 << static_cast<int>(result); | 143 << static_cast<int>(result); |
| 156 } | 144 } |
| 157 } | 145 } |
| 158 | 146 |
| 147 void RequestCoordinator::RemoveRequestsCallback( |
| 148 const RequestQueue::UpdateMultipleRequestResults& results) { |
| 149 // TODO(petewil): Today the RemoveRequests API does not come with a callback. |
| 150 // Should we add one? Perhaps the notifications from the observer will be |
| 151 // sufficient. |
| 152 } |
| 153 |
| 159 void RequestCoordinator::StopProcessing() { | 154 void RequestCoordinator::StopProcessing() { |
| 160 is_canceled_ = true; | 155 is_canceled_ = true; |
| 161 if (offliner_ && is_busy_) { | 156 if (offliner_ && is_busy_) { |
| 162 // TODO(dougarnett): Find current request and mark attempt aborted. | 157 // TODO(dougarnett): Find current request and mark attempt aborted. |
| 163 offliner_->Cancel(); | 158 offliner_->Cancel(); |
| 164 } | 159 } |
| 165 | 160 |
| 166 // Stopping offliner means it will not call callback. | 161 // Stopping offliner means it will not call callback. |
| 167 last_offlining_status_ = | 162 last_offlining_status_ = |
| 168 Offliner::RequestStatus::REQUEST_COORDINATOR_CANCELED; | 163 Offliner::RequestStatus::REQUEST_COORDINATOR_CANCELED; |
| (...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 303 updated_request.client_id())); | 298 updated_request.client_id())); |
| 304 | 299 |
| 305 } else if (status == Offliner::RequestStatus::SAVED || | 300 } else if (status == Offliner::RequestStatus::SAVED || |
| 306 request.completed_attempt_count() + 1 >= | 301 request.completed_attempt_count() + 1 >= |
| 307 policy_->GetMaxCompletedTries()) { | 302 policy_->GetMaxCompletedTries()) { |
| 308 // Remove the request from the queue if it either succeeded or exceeded the | 303 // 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 | 304 // max number of retries. The +1 represents the request that just |
| 310 // completed. Since we call MarkAttemptCompleted within the if branches, | 305 // completed. Since we call MarkAttemptCompleted within the if branches, |
| 311 // the completed_attempt_count has not yet been updated when we are checking | 306 // the completed_attempt_count has not yet been updated when we are checking |
| 312 // the if condition. | 307 // the if condition. |
| 313 queue_->RemoveRequest( | 308 std::vector<int64_t> remove_requests; |
| 314 request.request_id(), | 309 remove_requests.push_back(request.request_id()); |
| 315 base::Bind(&RequestCoordinator::UpdateRequestCallback, | 310 queue_->RemoveRequests( |
| 316 weak_ptr_factory_.GetWeakPtr(), request.client_id())); | 311 remove_requests, base::Bind(&RequestCoordinator::RemoveRequestsCallback, |
| 312 weak_ptr_factory_.GetWeakPtr())); |
| 317 } else { | 313 } else { |
| 318 // If we failed, but are not over the limit, update the request in the | 314 // If we failed, but are not over the limit, update the request in the |
| 319 // queue. | 315 // queue. |
| 320 SavePageRequest updated_request(request); | 316 SavePageRequest updated_request(request); |
| 321 updated_request.MarkAttemptCompleted(); | 317 updated_request.MarkAttemptCompleted(); |
| 322 queue_->UpdateRequest(updated_request, | 318 queue_->UpdateRequest(updated_request, |
| 323 base::Bind(&RequestCoordinator::UpdateRequestCallback, | 319 base::Bind(&RequestCoordinator::UpdateRequestCallback, |
| 324 weak_ptr_factory_.GetWeakPtr(), | 320 weak_ptr_factory_.GetWeakPtr(), |
| 325 updated_request.client_id())); | 321 updated_request.client_id())); |
| 326 } | 322 } |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 358 return trigger_conditions; | 354 return trigger_conditions; |
| 359 } | 355 } |
| 360 | 356 |
| 361 void RequestCoordinator::GetOffliner() { | 357 void RequestCoordinator::GetOffliner() { |
| 362 if (!offliner_) { | 358 if (!offliner_) { |
| 363 offliner_ = factory_->GetOffliner(policy_.get()); | 359 offliner_ = factory_->GetOffliner(policy_.get()); |
| 364 } | 360 } |
| 365 } | 361 } |
| 366 | 362 |
| 367 } // namespace offline_pages | 363 } // namespace offline_pages |
| OLD | NEW |