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/core/background/pick_request_task.h" | 5 #include "components/offline_pages/core/background/pick_request_task.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 "base/time/time.h" | 9 #include "base/time/time.h" |
| 10 #include "components/offline_pages/core/background/device_conditions.h" | 10 #include "components/offline_pages/core/background/device_conditions.h" |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 28 } // namespace | 28 } // namespace |
| 29 | 29 |
| 30 namespace offline_pages { | 30 namespace offline_pages { |
| 31 | 31 |
| 32 PickRequestTask::PickRequestTask(RequestQueueStore* store, | 32 PickRequestTask::PickRequestTask(RequestQueueStore* store, |
| 33 OfflinerPolicy* policy, | 33 OfflinerPolicy* policy, |
| 34 RequestPickedCallback picked_callback, | 34 RequestPickedCallback picked_callback, |
| 35 RequestNotPickedCallback not_picked_callback, | 35 RequestNotPickedCallback not_picked_callback, |
| 36 RequestCountCallback request_count_callback, | 36 RequestCountCallback request_count_callback, |
| 37 DeviceConditions& device_conditions, | 37 DeviceConditions& device_conditions, |
| 38 const std::set<int64_t>& disabled_requests) | 38 const std::set<int64_t>& disabled_requests, |
| 39 std::list<int64_t>& priortized_requests) | |
| 39 : store_(store), | 40 : store_(store), |
| 40 policy_(policy), | 41 policy_(policy), |
| 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 request_count_callback_(request_count_callback), | 44 request_count_callback_(request_count_callback), |
| 44 disabled_requests_(disabled_requests), | 45 disabled_requests_(disabled_requests), |
| 46 priortized_requests_(priortized_requests), | |
| 45 weak_ptr_factory_(this) { | 47 weak_ptr_factory_(this) { |
| 46 device_conditions_.reset(new DeviceConditions(device_conditions)); | 48 device_conditions_.reset(new DeviceConditions(device_conditions)); |
| 47 } | 49 } |
| 48 | 50 |
| 49 PickRequestTask::~PickRequestTask() {} | 51 PickRequestTask::~PickRequestTask() {} |
| 50 | 52 |
| 51 void PickRequestTask::Run() { | 53 void PickRequestTask::Run() { |
| 52 GetRequests(); | 54 GetRequests(); |
| 53 } | 55 } |
| 54 | 56 |
| (...skipping 21 matching lines...) Expand all Loading... | |
| 76 | 78 |
| 77 // Choose which comparison function to use based on policy. | 79 // Choose which comparison function to use based on policy. |
| 78 if (policy_->RetryCountIsMoreImportantThanRecency()) | 80 if (policy_->RetryCountIsMoreImportantThanRecency()) |
| 79 comparator = &PickRequestTask::RetryCountFirstCompareFunction; | 81 comparator = &PickRequestTask::RetryCountFirstCompareFunction; |
| 80 else | 82 else |
| 81 comparator = &PickRequestTask::RecencyFirstCompareFunction; | 83 comparator = &PickRequestTask::RecencyFirstCompareFunction; |
| 82 | 84 |
| 83 // TODO(petewil): Consider replacing this bool with a better named enum. | 85 // TODO(petewil): Consider replacing this bool with a better named enum. |
| 84 bool non_user_requested_tasks_remaining = false; | 86 bool non_user_requested_tasks_remaining = false; |
| 85 bool cleanup_needed = false; | 87 bool cleanup_needed = false; |
| 88 bool is_priortized_request_picked = false; | |
|
Pete Williamson
2017/03/06 18:30:57
nit: was_prioritized_request_picked might be bette
romax
2017/03/07 18:40:44
Done.
| |
| 86 | 89 |
| 87 size_t available_request_count = 0; | 90 size_t available_request_count = 0; |
| 88 | 91 |
| 89 // Iterate once through the requests, keeping track of best candidate. | 92 // Search for the priortized requests before iterating all the requests. |
|
Pete Williamson
2017/03/06 18:30:57
There is an implicit assumption here that we shoul
romax
2017/03/07 18:40:44
Done.
| |
| 93 while (!picked_request && !priortized_requests_.empty()) { | |
| 94 int64_t id = priortized_requests_.front(); | |
| 95 for (unsigned i = 0; i < requests.size(); ++i) { | |
| 96 if (requests[i]->request_id() == id) { | |
| 97 picked_request = requests[i].get(); | |
| 98 is_priortized_request_picked = true; | |
| 99 break; | |
| 100 } | |
| 101 } | |
| 102 // Remove the priortized requests which are not in request queue. | |
| 103 priortized_requests_.pop_front(); | |
| 104 } | |
| 105 | |
| 106 // Iterate once through the requests, keeping track of best candidate if no | |
| 107 // priortized request has been chosen. | |
|
fgorski
2017/03/06 18:12:45
if statement should probably happen outside of the
Pete Williamson
2017/03/06 18:30:57
Agreed, if a prioritized request is found, we shou
romax
2017/03/07 18:40:44
Refactored a little bit. Not sure if it's necessar
| |
| 90 for (unsigned i = 0; i < requests.size(); ++i) { | 108 for (unsigned i = 0; i < requests.size(); ++i) { |
| 91 // If the request is expired or has exceeded the retry count, skip it. | 109 // If the request is expired or has exceeded the retry count, skip it. |
| 92 if (OfflinerPolicyUtils::CheckRequestExpirationStatus(requests[i].get(), | 110 if (OfflinerPolicyUtils::CheckRequestExpirationStatus(requests[i].get(), |
| 93 policy_) != | 111 policy_) != |
| 94 OfflinerPolicyUtils::RequestExpirationStatus::VALID) { | 112 OfflinerPolicyUtils::RequestExpirationStatus::VALID) { |
| 95 cleanup_needed = true; | 113 cleanup_needed = true; |
| 96 continue; | 114 continue; |
| 97 } | 115 } |
| 98 | 116 |
| 99 // If the request is on the disabled list, skip it. | 117 // If the request is on the disabled list, skip it. |
| 100 auto search = disabled_requests_.find(requests[i]->request_id()); | 118 auto search = disabled_requests_.find(requests[i]->request_id()); |
| 101 if (search != disabled_requests_.end()) | 119 if (search != disabled_requests_.end()) |
|
fgorski
2017/03/06 18:12:45
Is there a chance that prioritized request is on t
romax
2017/03/07 18:40:44
yeah there might be. added a check
| |
| 102 continue; | 120 continue; |
| 103 | 121 |
| 104 // If there are non-user-requested tasks remaining, we need to make sure | 122 // If there are non-user-requested tasks remaining, we need to make sure |
| 105 // that they are scheduled after we run out of user requested tasks. Here we | 123 // that they are scheduled after we run out of user requested tasks. Here we |
| 106 // detect if any exist. If we don't find any user-requested tasks, we will | 124 // detect if any exist. If we don't find any user-requested tasks, we will |
| 107 // inform the "not_picked_callback_" that it needs to schedule a task for | 125 // inform the "not_picked_callback_" that it needs to schedule a task for |
| 108 // non-user-requested items, which have different network and power needs. | 126 // non-user-requested items, which have different network and power needs. |
| 109 if (!requests[i]->user_requested()) | 127 if (!requests[i]->user_requested()) |
| 110 non_user_requested_tasks_remaining = true; | 128 non_user_requested_tasks_remaining = true; |
| 111 if (requests[i]->request_state() == | 129 if (requests[i]->request_state() == |
| 112 SavePageRequest::RequestState::AVAILABLE) { | 130 SavePageRequest::RequestState::AVAILABLE) { |
| 113 available_request_count++; | 131 available_request_count++; |
| 114 } | 132 } |
| 115 if (!RequestConditionsSatisfied(requests[i].get())) | 133 |
| 116 continue; | 134 if (!is_priortized_request_picked) { |
| 117 if (IsNewRequestBetter(picked_request, requests[i].get(), comparator)) | 135 if (!RequestConditionsSatisfied(requests[i].get())) |
| 118 picked_request = requests[i].get(); | 136 continue; |
| 137 if (IsNewRequestBetter(picked_request, requests[i].get(), comparator)) | |
| 138 picked_request = requests[i].get(); | |
| 139 } | |
| 119 } | 140 } |
| 120 | 141 |
| 121 // Report the request queue counts. | 142 // Report the request queue counts. |
| 122 request_count_callback_.Run(requests.size(), available_request_count); | 143 request_count_callback_.Run(requests.size(), available_request_count); |
| 123 | 144 |
| 124 // If we have a best request to try next, get the request coodinator to | 145 // If we have a best request to try next, get the request coodinator to |
| 125 // start it. Otherwise return that we have no candidates. | 146 // start it. Otherwise return that we have no candidates. |
| 126 if (picked_request != nullptr) { | 147 if (picked_request != nullptr) { |
| 127 picked_callback_.Run(*picked_request, cleanup_needed); | 148 picked_callback_.Run(*picked_request, cleanup_needed); |
| 128 } else { | 149 } else { |
| (...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 246 int result = signum(difference.InMilliseconds()); | 267 int result = signum(difference.InMilliseconds()); |
| 247 | 268 |
| 248 // Flip the direction of comparison if policy prefers fewer retries. | 269 // Flip the direction of comparison if policy prefers fewer retries. |
| 249 if (policy_->ShouldPreferEarlierRequests()) | 270 if (policy_->ShouldPreferEarlierRequests()) |
| 250 result *= -1; | 271 result *= -1; |
| 251 | 272 |
| 252 return result; | 273 return result; |
| 253 } | 274 } |
| 254 | 275 |
| 255 } // namespace offline_pages | 276 } // namespace offline_pages |
| OLD | NEW |