Chromium Code Reviews| Index: components/offline_pages/core/background/pick_request_task.cc |
| diff --git a/components/offline_pages/core/background/pick_request_task.cc b/components/offline_pages/core/background/pick_request_task.cc |
| index 006d08b47aff2211d377e6869f8b95e49273185f..f1a612c0c0553845057d7e9527079b071e290166 100644 |
| --- a/components/offline_pages/core/background/pick_request_task.cc |
| +++ b/components/offline_pages/core/background/pick_request_task.cc |
| @@ -4,6 +4,8 @@ |
| #include "components/offline_pages/core/background/pick_request_task.h" |
| +#include <set> |
| + |
| #include "base/bind.h" |
| #include "base/logging.h" |
| #include "base/time/time.h" |
| @@ -35,13 +37,15 @@ PickRequestTask::PickRequestTask(RequestQueueStore* store, |
| RequestNotPickedCallback not_picked_callback, |
| RequestCountCallback request_count_callback, |
| DeviceConditions& device_conditions, |
| - const std::set<int64_t>& disabled_requests) |
| + const std::set<int64_t>& disabled_requests, |
| + std::deque<int64_t>& prioritized_requests) |
| : store_(store), |
| policy_(policy), |
| picked_callback_(picked_callback), |
| not_picked_callback_(not_picked_callback), |
| request_count_callback_(request_count_callback), |
| disabled_requests_(disabled_requests), |
| + prioritized_requests_(prioritized_requests), |
| weak_ptr_factory_(this) { |
| device_conditions_.reset(new DeviceConditions(device_conditions)); |
| } |
| @@ -85,8 +89,11 @@ void PickRequestTask::Choose( |
| bool cleanup_needed = false; |
| size_t available_request_count = 0; |
| + std::set<int64_t> available_request_ids; |
|
fgorski
2017/03/08 17:33:23
Did you consider unordered_set?
romax
2017/03/08 21:43:58
Done.
No i didn't...and seems like a hashtable is
|
| - // Iterate once through the requests, keeping track of best candidate. |
| + // Iterate through the requests, filter out unavailable requests and get other |
| + // information (if cleanup is needed and number of non-user-requested |
| + // requests). |
| for (unsigned i = 0; i < requests.size(); ++i) { |
| // If the request is expired or has exceeded the retry count, skip it. |
| if (OfflinerPolicyUtils::CheckRequestExpirationStatus(requests[i].get(), |
| @@ -95,8 +102,7 @@ void PickRequestTask::Choose( |
| cleanup_needed = true; |
| continue; |
| } |
| - |
| - // If the request is on the disabled list, skip it. |
| + // If the request is on the disabled list, skip it. |
| auto search = disabled_requests_.find(requests[i]->request_id()); |
| if (search != disabled_requests_.end()) |
| continue; |
| @@ -114,13 +120,41 @@ void PickRequestTask::Choose( |
| } |
| if (!RequestConditionsSatisfied(requests[i].get())) |
| continue; |
| - if (IsNewRequestBetter(picked_request, requests[i].get(), comparator)) |
| - picked_request = requests[i].get(); |
| + available_request_ids.insert(requests[i]->request_id()); |
|
fgorski
2017/03/08 17:33:24
would it make sense to put this inside of a check:
romax
2017/03/08 21:43:58
it makes sense as a break-proof thing but not need
|
| } |
| - |
| // Report the request queue counts. |
| request_count_callback_.Run(requests.size(), available_request_count); |
|
fgorski
2017/03/08 17:33:24
available_request_ids.size()
and remove available
romax
2017/03/08 21:43:58
See above comment.
|
| + // Search for and pick the prioritized request which is not disabled and in |
|
fgorski
2017/03/08 17:33:24
Update comment to reflect what we discussed.
romax
2017/03/08 21:43:58
Done.
|
| + // the request queue. An assumption is that requests later in the vector have |
| + // higher priority than earlier ones in the vector. Also remove the picked |
| + // request from the prioritized list. |
| + while (!picked_request && !prioritized_requests_.empty()) { |
| + if (available_request_ids.find(prioritized_requests_.back()) != |
|
fgorski
2017/03/08 17:33:24
if (available_requests_ids.count(prioritized_reque
romax
2017/03/08 21:43:58
Done.
|
| + available_request_ids.end()) { |
| + for (unsigned i = 0; i < requests.size(); ++i) { |
|
fgorski
2017/03/08 17:33:24
please add a comment above why this is not an infi
romax
2017/03/08 21:43:58
Done.
|
| + if (requests[i]->request_id() == prioritized_requests_.back()) { |
| + picked_request = requests[i].get(); |
| + break; |
| + } |
| + } |
| + } else { |
| + prioritized_requests_.pop_back(); |
| + } |
| + } |
| + |
| + // If no request was found from the priority list, find the best request |
| + // according to current policies. |
| + if (!picked_request) { |
| + for (unsigned i = 0; i < requests.size(); ++i) { |
|
fgorski
2017/03/08 17:33:24
Perhaps an auto iter?
romax
2017/03/08 21:43:58
Done.
|
| + if ((available_request_ids.find(requests[i]->request_id()) != |
|
fgorski
2017/03/08 17:33:24
same count() comment.
romax
2017/03/08 21:43:58
Done.
|
| + available_request_ids.end()) && |
| + (IsNewRequestBetter(picked_request, requests[i].get(), comparator))) { |
| + picked_request = requests[i].get(); |
| + } |
| + } |
| + } |
| + |
| // If we have a best request to try next, get the request coodinator to |
| // start it. Otherwise return that we have no candidates. |
| if (picked_request != nullptr) { |