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_picker.h" | 5 #include "components/offline_pages/background/request_picker.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "components/offline_pages/background/save_page_request.h" | 9 #include "components/offline_pages/background/save_page_request.h" |
10 | 10 |
(...skipping 19 matching lines...) Expand all Loading... |
30 fewer_retries_better_(false), | 30 fewer_retries_better_(false), |
31 earlier_requests_better_(false), | 31 earlier_requests_better_(false), |
32 weak_ptr_factory_(this) {} | 32 weak_ptr_factory_(this) {} |
33 | 33 |
34 RequestPicker::~RequestPicker() {} | 34 RequestPicker::~RequestPicker() {} |
35 | 35 |
36 // Entry point for the async operation to choose the next request. | 36 // Entry point for the async operation to choose the next request. |
37 void RequestPicker::ChooseNextRequest( | 37 void RequestPicker::ChooseNextRequest( |
38 RequestCoordinator::RequestPickedCallback picked_callback, | 38 RequestCoordinator::RequestPickedCallback picked_callback, |
39 RequestCoordinator::RequestNotPickedCallback not_picked_callback, | 39 RequestCoordinator::RequestNotPickedCallback not_picked_callback, |
40 DeviceConditions* device_conditions) { | 40 DeviceConditions* device_conditions, |
| 41 const std::set<int64_t>& disabled_requests) { |
41 picked_callback_ = picked_callback; | 42 picked_callback_ = picked_callback; |
42 not_picked_callback_ = not_picked_callback; | 43 not_picked_callback_ = not_picked_callback; |
43 fewer_retries_better_ = policy_->ShouldPreferUntriedRequests(); | 44 fewer_retries_better_ = policy_->ShouldPreferUntriedRequests(); |
44 earlier_requests_better_ = policy_->ShouldPreferEarlierRequests(); | 45 earlier_requests_better_ = policy_->ShouldPreferEarlierRequests(); |
45 current_conditions_.reset(new DeviceConditions(*device_conditions)); | 46 current_conditions_.reset(new DeviceConditions(*device_conditions)); |
46 // Get all requests from queue (there is no filtering mechanism). | 47 // Get all requests from queue (there is no filtering mechanism). |
47 queue_->GetRequests(base::Bind(&RequestPicker::GetRequestResultCallback, | 48 queue_->GetRequests(base::Bind(&RequestPicker::GetRequestResultCallback, |
48 weak_ptr_factory_.GetWeakPtr())); | 49 weak_ptr_factory_.GetWeakPtr(), |
| 50 disabled_requests)); |
49 } | 51 } |
50 | 52 |
51 // When we get contents from the queue, use them to pick the next | 53 // When we get contents from the queue, use them to pick the next |
52 // request to operate on (if any). | 54 // request to operate on (if any). |
53 void RequestPicker::GetRequestResultCallback( | 55 void RequestPicker::GetRequestResultCallback( |
| 56 const std::set<int64_t>& disabled_requests, |
54 RequestQueue::GetRequestsResult, | 57 RequestQueue::GetRequestsResult, |
55 std::vector<std::unique_ptr<SavePageRequest>> requests) { | 58 std::vector<std::unique_ptr<SavePageRequest>> requests) { |
56 // If there is nothing to do, return right away. | 59 // If there is nothing to do, return right away. |
57 if (requests.size() == 0) { | 60 if (requests.size() == 0) { |
58 not_picked_callback_.Run(false); | 61 not_picked_callback_.Run(false); |
59 return; | 62 return; |
60 } | 63 } |
61 | 64 |
62 // Get the expired requests to be removed from the queue, and the valid ones | 65 // Get the expired requests to be removed from the queue, and the valid ones |
63 // from which to pick the next request. | 66 // from which to pick the next request. |
(...skipping 15 matching lines...) Expand all Loading... |
79 | 82 |
80 // Choose which comparison function to use based on policy. | 83 // Choose which comparison function to use based on policy. |
81 if (policy_->RetryCountIsMoreImportantThanRecency()) | 84 if (policy_->RetryCountIsMoreImportantThanRecency()) |
82 comparator = &RequestPicker::RetryCountFirstCompareFunction; | 85 comparator = &RequestPicker::RetryCountFirstCompareFunction; |
83 else | 86 else |
84 comparator = &RequestPicker::RecencyFirstCompareFunction; | 87 comparator = &RequestPicker::RecencyFirstCompareFunction; |
85 | 88 |
86 // Iterate once through the requests, keeping track of best candidate. | 89 // Iterate once through the requests, keeping track of best candidate. |
87 bool non_user_requested_tasks_remaining = false; | 90 bool non_user_requested_tasks_remaining = false; |
88 for (unsigned i = 0; i < valid_requests.size(); ++i) { | 91 for (unsigned i = 0; i < valid_requests.size(); ++i) { |
| 92 // If the request is on the disabled list, skip it. |
| 93 auto search = disabled_requests.find(valid_requests[i]->request_id()); |
| 94 if (search != disabled_requests.end()) { |
| 95 continue; |
| 96 } |
89 if (!valid_requests[i]->user_requested()) | 97 if (!valid_requests[i]->user_requested()) |
90 non_user_requested_tasks_remaining = true; | 98 non_user_requested_tasks_remaining = true; |
91 if (!RequestConditionsSatisfied(valid_requests[i].get())) | 99 if (!RequestConditionsSatisfied(valid_requests[i].get())) |
92 continue; | 100 continue; |
93 if (IsNewRequestBetter(picked_request, valid_requests[i].get(), comparator)) | 101 if (IsNewRequestBetter(picked_request, valid_requests[i].get(), comparator)) |
94 picked_request = valid_requests[i].get(); | 102 picked_request = valid_requests[i].get(); |
95 } | 103 } |
96 | 104 |
97 // If we have a best request to try next, get the request coodinator to | 105 // If we have a best request to try next, get the request coodinator to |
98 // start it. Otherwise return that we have no candidates. | 106 // start it. Otherwise return that we have no candidates. |
(...skipping 160 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
259 const RequestCoordinator::BackgroundSavePageResult save_page_result( | 267 const RequestCoordinator::BackgroundSavePageResult save_page_result( |
260 RequestCoordinator::BackgroundSavePageResult::EXPIRED); | 268 RequestCoordinator::BackgroundSavePageResult::EXPIRED); |
261 for (const auto& request : result->updated_items) { | 269 for (const auto& request : result->updated_items) { |
262 event_logger_->RecordDroppedSavePageRequest( | 270 event_logger_->RecordDroppedSavePageRequest( |
263 request.client_id().name_space, save_page_result, request.request_id()); | 271 request.client_id().name_space, save_page_result, request.request_id()); |
264 notifier_->NotifyCompleted(request, save_page_result); | 272 notifier_->NotifyCompleted(request, save_page_result); |
265 } | 273 } |
266 } | 274 } |
267 | 275 |
268 } // namespace offline_pages | 276 } // namespace offline_pages |
OLD | NEW |