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 <set> | |
8 | |
7 #include "base/bind.h" | 9 #include "base/bind.h" |
8 #include "base/logging.h" | 10 #include "base/logging.h" |
9 #include "base/time/time.h" | 11 #include "base/time/time.h" |
10 #include "components/offline_pages/core/background/device_conditions.h" | 12 #include "components/offline_pages/core/background/device_conditions.h" |
11 #include "components/offline_pages/core/background/offliner_policy.h" | 13 #include "components/offline_pages/core/background/offliner_policy.h" |
12 #include "components/offline_pages/core/background/offliner_policy_utils.h" | 14 #include "components/offline_pages/core/background/offliner_policy_utils.h" |
13 #include "components/offline_pages/core/background/request_coordinator_event_log ger.h" | 15 #include "components/offline_pages/core/background/request_coordinator_event_log ger.h" |
14 #include "components/offline_pages/core/background/request_notifier.h" | 16 #include "components/offline_pages/core/background/request_notifier.h" |
15 #include "components/offline_pages/core/background/request_queue_store.h" | 17 #include "components/offline_pages/core/background/request_queue_store.h" |
16 #include "components/offline_pages/core/background/save_page_request.h" | 18 #include "components/offline_pages/core/background/save_page_request.h" |
(...skipping 11 matching lines...) Expand all Loading... | |
28 } // namespace | 30 } // namespace |
29 | 31 |
30 namespace offline_pages { | 32 namespace offline_pages { |
31 | 33 |
32 PickRequestTask::PickRequestTask(RequestQueueStore* store, | 34 PickRequestTask::PickRequestTask(RequestQueueStore* store, |
33 OfflinerPolicy* policy, | 35 OfflinerPolicy* policy, |
34 RequestPickedCallback picked_callback, | 36 RequestPickedCallback picked_callback, |
35 RequestNotPickedCallback not_picked_callback, | 37 RequestNotPickedCallback not_picked_callback, |
36 RequestCountCallback request_count_callback, | 38 RequestCountCallback request_count_callback, |
37 DeviceConditions& device_conditions, | 39 DeviceConditions& device_conditions, |
38 const std::set<int64_t>& disabled_requests) | 40 const std::set<int64_t>& disabled_requests, |
41 std::deque<int64_t>& prioritized_requests) | |
39 : store_(store), | 42 : store_(store), |
40 policy_(policy), | 43 policy_(policy), |
41 picked_callback_(picked_callback), | 44 picked_callback_(picked_callback), |
42 not_picked_callback_(not_picked_callback), | 45 not_picked_callback_(not_picked_callback), |
43 request_count_callback_(request_count_callback), | 46 request_count_callback_(request_count_callback), |
44 disabled_requests_(disabled_requests), | 47 disabled_requests_(disabled_requests), |
48 prioritized_requests_(prioritized_requests), | |
45 weak_ptr_factory_(this) { | 49 weak_ptr_factory_(this) { |
46 device_conditions_.reset(new DeviceConditions(device_conditions)); | 50 device_conditions_.reset(new DeviceConditions(device_conditions)); |
47 } | 51 } |
48 | 52 |
49 PickRequestTask::~PickRequestTask() {} | 53 PickRequestTask::~PickRequestTask() {} |
50 | 54 |
51 void PickRequestTask::Run() { | 55 void PickRequestTask::Run() { |
52 GetRequests(); | 56 GetRequests(); |
53 } | 57 } |
54 | 58 |
(...skipping 23 matching lines...) Expand all Loading... | |
78 if (policy_->RetryCountIsMoreImportantThanRecency()) | 82 if (policy_->RetryCountIsMoreImportantThanRecency()) |
79 comparator = &PickRequestTask::RetryCountFirstCompareFunction; | 83 comparator = &PickRequestTask::RetryCountFirstCompareFunction; |
80 else | 84 else |
81 comparator = &PickRequestTask::RecencyFirstCompareFunction; | 85 comparator = &PickRequestTask::RecencyFirstCompareFunction; |
82 | 86 |
83 // TODO(petewil): Consider replacing this bool with a better named enum. | 87 // TODO(petewil): Consider replacing this bool with a better named enum. |
84 bool non_user_requested_tasks_remaining = false; | 88 bool non_user_requested_tasks_remaining = false; |
85 bool cleanup_needed = false; | 89 bool cleanup_needed = false; |
86 | 90 |
87 size_t available_request_count = 0; | 91 size_t available_request_count = 0; |
92 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
| |
88 | 93 |
89 // Iterate once through the requests, keeping track of best candidate. | 94 // Iterate through the requests, filter out unavailable requests and get other |
95 // information (if cleanup is needed and number of non-user-requested | |
96 // requests). | |
90 for (unsigned i = 0; i < requests.size(); ++i) { | 97 for (unsigned i = 0; i < requests.size(); ++i) { |
91 // If the request is expired or has exceeded the retry count, skip it. | 98 // If the request is expired or has exceeded the retry count, skip it. |
92 if (OfflinerPolicyUtils::CheckRequestExpirationStatus(requests[i].get(), | 99 if (OfflinerPolicyUtils::CheckRequestExpirationStatus(requests[i].get(), |
93 policy_) != | 100 policy_) != |
94 OfflinerPolicyUtils::RequestExpirationStatus::VALID) { | 101 OfflinerPolicyUtils::RequestExpirationStatus::VALID) { |
95 cleanup_needed = true; | 102 cleanup_needed = true; |
96 continue; | 103 continue; |
97 } | 104 } |
98 | 105 // If the request is on the disabled list, skip it. |
99 // If the request is on the disabled list, skip it. | |
100 auto search = disabled_requests_.find(requests[i]->request_id()); | 106 auto search = disabled_requests_.find(requests[i]->request_id()); |
101 if (search != disabled_requests_.end()) | 107 if (search != disabled_requests_.end()) |
102 continue; | 108 continue; |
103 | 109 |
104 // If there are non-user-requested tasks remaining, we need to make sure | 110 // 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 | 111 // 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 | 112 // 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 | 113 // 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. | 114 // non-user-requested items, which have different network and power needs. |
109 if (!requests[i]->user_requested()) | 115 if (!requests[i]->user_requested()) |
110 non_user_requested_tasks_remaining = true; | 116 non_user_requested_tasks_remaining = true; |
111 if (requests[i]->request_state() == | 117 if (requests[i]->request_state() == |
112 SavePageRequest::RequestState::AVAILABLE) { | 118 SavePageRequest::RequestState::AVAILABLE) { |
113 available_request_count++; | 119 available_request_count++; |
fgorski
2017/03/08 17:33:24
since you have one more if after this, the request
romax
2017/03/08 21:43:58
i thought about the same thing but ended up keepin
| |
114 } | 120 } |
115 if (!RequestConditionsSatisfied(requests[i].get())) | 121 if (!RequestConditionsSatisfied(requests[i].get())) |
116 continue; | 122 continue; |
117 if (IsNewRequestBetter(picked_request, requests[i].get(), comparator)) | 123 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
| |
118 picked_request = requests[i].get(); | 124 } |
125 // Report the request queue counts. | |
126 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.
| |
127 | |
128 // 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.
| |
129 // the request queue. An assumption is that requests later in the vector have | |
130 // higher priority than earlier ones in the vector. Also remove the picked | |
131 // request from the prioritized list. | |
132 while (!picked_request && !prioritized_requests_.empty()) { | |
133 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.
| |
134 available_request_ids.end()) { | |
135 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.
| |
136 if (requests[i]->request_id() == prioritized_requests_.back()) { | |
137 picked_request = requests[i].get(); | |
138 break; | |
139 } | |
140 } | |
141 } else { | |
142 prioritized_requests_.pop_back(); | |
143 } | |
119 } | 144 } |
120 | 145 |
121 // Report the request queue counts. | 146 // If no request was found from the priority list, find the best request |
122 request_count_callback_.Run(requests.size(), available_request_count); | 147 // according to current policies. |
148 if (!picked_request) { | |
149 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.
| |
150 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.
| |
151 available_request_ids.end()) && | |
152 (IsNewRequestBetter(picked_request, requests[i].get(), comparator))) { | |
153 picked_request = requests[i].get(); | |
154 } | |
155 } | |
156 } | |
123 | 157 |
124 // If we have a best request to try next, get the request coodinator to | 158 // If we have a best request to try next, get the request coodinator to |
125 // start it. Otherwise return that we have no candidates. | 159 // start it. Otherwise return that we have no candidates. |
126 if (picked_request != nullptr) { | 160 if (picked_request != nullptr) { |
127 picked_callback_.Run(*picked_request, cleanup_needed); | 161 picked_callback_.Run(*picked_request, cleanup_needed); |
128 } else { | 162 } else { |
129 not_picked_callback_.Run(non_user_requested_tasks_remaining, | 163 not_picked_callback_.Run(non_user_requested_tasks_remaining, |
130 cleanup_needed); | 164 cleanup_needed); |
131 } | 165 } |
132 | 166 |
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
246 int result = signum(difference.InMilliseconds()); | 280 int result = signum(difference.InMilliseconds()); |
247 | 281 |
248 // Flip the direction of comparison if policy prefers fewer retries. | 282 // Flip the direction of comparison if policy prefers fewer retries. |
249 if (policy_->ShouldPreferEarlierRequests()) | 283 if (policy_->ShouldPreferEarlierRequests()) |
250 result *= -1; | 284 result *= -1; |
251 | 285 |
252 return result; | 286 return result; |
253 } | 287 } |
254 | 288 |
255 } // namespace offline_pages | 289 } // namespace offline_pages |
OLD | NEW |