Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(72)

Side by Side Diff: components/offline_pages/background/request_picker.cc

Issue 2113383002: More detailed implementation of the RequestPicker (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix merge Created 4 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
11 namespace {
12 template <typename T>
13 int signum(T t) {
14 return (T(0) < t) - (t < T(0));
15 }
16
17 #define CALL_MEMBER_FUNCTION(object,ptrToMember) ((object)->*(ptrToMember))
18 } // namespace
19
11 namespace offline_pages { 20 namespace offline_pages {
12 21
13 RequestPicker::RequestPicker( 22 RequestPicker::RequestPicker(
14 RequestQueue* requestQueue) 23 RequestQueue* requestQueue, OfflinerPolicy* policy)
15 : queue_(requestQueue), 24 : queue_(requestQueue),
25 policy_(policy),
26 fewer_retries_better_(false),
27 earlier_requests_better_(false),
16 weak_ptr_factory_(this) {} 28 weak_ptr_factory_(this) {}
17 29
18 RequestPicker::~RequestPicker() {} 30 RequestPicker::~RequestPicker() {}
19 31
32 // Entry point for the async operation to choose the next request.
20 void RequestPicker::ChooseNextRequest( 33 void RequestPicker::ChooseNextRequest(
21 RequestCoordinator::RequestPickedCallback picked_callback, 34 RequestCoordinator::RequestPickedCallback picked_callback,
22 RequestCoordinator::RequestQueueEmptyCallback empty_callback) { 35 RequestCoordinator::RequestQueueEmptyCallback empty_callback,
36 DeviceConditions* device_conditions) {
23 picked_callback_ = picked_callback; 37 picked_callback_ = picked_callback;
24 empty_callback_ = empty_callback; 38 empty_callback_ = empty_callback;
39 fewer_retries_better_ = policy_->ShouldPreferUntriedRequests();
40 earlier_requests_better_ = policy_->ShouldPreferEarlierRequests();
41 current_conditions_.reset(new DeviceConditions(*device_conditions));
25 // Get all requests from queue (there is no filtering mechanism). 42 // Get all requests from queue (there is no filtering mechanism).
26 queue_->GetRequests(base::Bind(&RequestPicker::GetRequestResultCallback, 43 queue_->GetRequests(base::Bind(&RequestPicker::GetRequestResultCallback,
27 weak_ptr_factory_.GetWeakPtr())); 44 weak_ptr_factory_.GetWeakPtr()));
28 } 45 }
29 46
47 // When we get contents from the queue, use them to pick the next
48 // request to operate on (if any).
30 void RequestPicker::GetRequestResultCallback( 49 void RequestPicker::GetRequestResultCallback(
31 RequestQueue::GetRequestsResult, 50 RequestQueue::GetRequestsResult,
32 const std::vector<SavePageRequest>& requests) { 51 const std::vector<SavePageRequest>& requests) {
33 // If there is nothing to do, return right away. 52 // If there is nothing to do, return right away.
34 if (requests.size() == 0) { 53 if (requests.size() == 0) {
35 empty_callback_.Run(); 54 empty_callback_.Run();
36 return; 55 return;
37 } 56 }
38 57
39 // Pick the most deserving request for our conditions. 58 // Pick the most deserving request for our conditions.
40 const SavePageRequest& picked_request = requests[0]; 59 const SavePageRequest* picked_request = nullptr;
41 60
42 // When we have a best request to try next, get the request coodinator to 61 RequestCompareFunction comparator = nullptr;
43 // start it. 62
44 picked_callback_.Run(picked_request); 63 // Choose which comparison function to use based on policy.
64 if (policy_->RetryCountIsMoreImportantThanRecency())
65 comparator = &RequestPicker::RetryCountFirstCompareFunction;
66 else
67 comparator = &RequestPicker::RecencyFirstCompareFunction;
68
69 // Iterate once through the requests, keeping track of best candidate.
70 for (unsigned i = 0; i < requests.size(); ++i) {
71 if (!RequestConditionsSatisfied(requests[i]))
72 continue;
73 if (IsNewRequestBetter(picked_request, &(requests[i]), comparator))
74 picked_request = &(requests[i]);
75 }
76
77 // If we have a best request to try next, get the request coodinator to
78 // start it. Otherwise return that we have no candidates.
79 if (picked_request != nullptr) {
80 picked_callback_.Run(*picked_request);
81 } else {
82 empty_callback_.Run();
83 }
84 }
85
86 // Filter out requests that don't meet the current conditions. For instance, if
87 // this is a predictive request, and we are not on WiFi, it should be ignored
88 // this round.
89 bool RequestPicker::RequestConditionsSatisfied(const SavePageRequest& request) {
90 // If the user did not request the page directly, make sure we are connected
91 // to power and have WiFi and sufficient battery remaining before we take this
92 // reqeust.
93 // TODO(petewil): We may later want to configure whether to allow 2G for non
94 // user_requested items, add that to policy.
95 if (!request.user_requested()) {
96 if (!current_conditions_->IsPowerConnected())
97 return false;
98
99 if (current_conditions_->GetNetConnectionType() !=
100 net::NetworkChangeNotifier::ConnectionType::CONNECTION_WIFI) {
101 return false;
102 }
103
104 if (current_conditions_->GetBatteryPercentage() <
105 policy_->GetMinimumBatteryPercentageForNonUserRequestOfflining()) {
106 return false;
107 }
108 }
109
110 // If we have already tried this page the max number of times, it is not
111 // eligible to try again.
112 // TODO(petewil): Instead, we should have code to remove the page from the
113 // queue after the last retry.
114 if (request.attempt_count() >= policy_->GetMaxRetries())
115 return false;
116
117 // If this request is not active yet, return false.
118 // TODO(petewil): If the only reason we return nothing to do is that we have
119 // inactive requests, we still want to try again later after their activation
120 // time elapses, we shouldn't take ourselves completely off the scheduler.
121 if (request.activation_time() > base::Time::Now())
122 return false;
123
124 return true;
125 }
126
127 // Look at policies to decide which requests to prefer.
128 bool RequestPicker::IsNewRequestBetter(const SavePageRequest* oldRequest,
129 const SavePageRequest* newRequest,
130 RequestCompareFunction comparator) {
131
132 // If there is no old request, the new one is better.
133 if (oldRequest == nullptr)
134 return true;
135
136 // User requested pages get priority.
137 if (newRequest->user_requested() && !oldRequest->user_requested())
138 return true;
139
140 // Otherwise, use the comparison function for the current policy, which
141 // returns true if the older request is better.
142 return !(CALL_MEMBER_FUNCTION(this, comparator)(oldRequest, newRequest));
143 }
144
145 // Compare the results, checking request count before recency. Returns true if
146 // left hand side is better, false otherwise.
147 bool RequestPicker::RetryCountFirstCompareFunction(
148 const SavePageRequest* left, const SavePageRequest* right) {
149 // Check the attempt count.
150 int result = CompareRetryCount(left, right);
151
152 if (result != 0)
153 return (result > 0);
154
155 // If we get here, the attempt counts were the same, so check recency.
156 result = CompareCreationTime(left, right);
157
158 return (result > 0);
159 }
160
161 // Compare the results, checking recency before request count. Returns true if
162 // left hand side is better, false otherwise.
163 bool RequestPicker::RecencyFirstCompareFunction(
164 const SavePageRequest* left, const SavePageRequest* right) {
165 // Check the recency.
166 int result = CompareCreationTime(left, right);
167
168 if (result != 0)
169 return (result > 0);
170
171 // If we get here, the recency was the same, so check the attempt count.
172 result = CompareRetryCount(left, right);
173
174 return (result > 0);
175 }
176
177 // Compare left and right side, returning 1 if the left side is better
178 // (preferred by policy), 0 if the same, and -1 if the right side is better.
179 int RequestPicker::CompareRetryCount(
180 const SavePageRequest* left, const SavePageRequest* right) {
181 // Check the attempt count.
182 int result = signum(left->attempt_count() - right->attempt_count());
183
184 // Flip the direction of comparison if policy prefers fewer retries.
185 if (fewer_retries_better_)
186 result *= -1;
187
188 return result;
189 }
190
191 // Compare left and right side, returning 1 if the left side is better
192 // (preferred by policy), 0 if the same, and -1 if the right side is better.
193 int RequestPicker::CompareCreationTime(
194 const SavePageRequest* left, const SavePageRequest* right) {
195 // Check the recency.
196 base::TimeDelta difference = left->creation_time() - right->creation_time();
197 int result = signum(difference.InMilliseconds());
198
199 // Flip the direction of comparison if policy prefers fewer retries.
200 if (earlier_requests_better_)
201 result *= -1;
202
203 return result;
45 } 204 }
46 205
47 } // namespace offline_pages 206 } // namespace offline_pages
OLDNEW
« no previous file with comments | « components/offline_pages/background/request_picker.h ('k') | components/offline_pages/background/request_picker_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698