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

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: CR fixes per DougArnett 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 offline_pages { 11 namespace offline_pages {
12 12
13 RequestPicker::RequestPicker( 13 RequestPicker::RequestPicker(
14 RequestQueue* requestQueue) 14 RequestQueue* requestQueue, OfflinerPolicy* policy)
15 : queue_(requestQueue), 15 : queue_(requestQueue),
16 policy_(policy),
16 weak_ptr_factory_(this) {} 17 weak_ptr_factory_(this) {}
17 18
18 RequestPicker::~RequestPicker() {} 19 RequestPicker::~RequestPicker() {}
19 20
21 // Entry point for the async operation to choose the next request.
20 void RequestPicker::ChooseNextRequest( 22 void RequestPicker::ChooseNextRequest(
21 RequestCoordinator::RequestPickedCallback picked_callback, 23 RequestCoordinator::RequestPickedCallback picked_callback,
22 RequestCoordinator::RequestQueueEmptyCallback empty_callback) { 24 RequestCoordinator::RequestQueueEmptyCallback empty_callback,
25 DeviceConditions* device_conditions) {
23 picked_callback_ = picked_callback; 26 picked_callback_ = picked_callback;
24 empty_callback_ = empty_callback; 27 empty_callback_ = empty_callback;
28 current_conditions_.reset(new DeviceConditions(*device_conditions));
25 // Get all requests from queue (there is no filtering mechanism). 29 // Get all requests from queue (there is no filtering mechanism).
26 queue_->GetRequests(base::Bind(&RequestPicker::GetRequestResultCallback, 30 queue_->GetRequests(base::Bind(&RequestPicker::GetRequestResultCallback,
27 weak_ptr_factory_.GetWeakPtr())); 31 weak_ptr_factory_.GetWeakPtr()));
28 } 32 }
29 33
34 // When we get contents from the queue, use them to pick the next
35 // request to operate on (if any).
30 void RequestPicker::GetRequestResultCallback( 36 void RequestPicker::GetRequestResultCallback(
31 RequestQueue::GetRequestsResult, 37 RequestQueue::GetRequestsResult,
32 const std::vector<SavePageRequest>& requests) { 38 const std::vector<SavePageRequest>& requests) {
33 // If there is nothing to do, return right away. 39 // If there is nothing to do, return right away.
34 if (requests.size() == 0) { 40 if (requests.size() == 0) {
35 empty_callback_.Run(); 41 empty_callback_.Run();
36 return; 42 return;
37 } 43 }
38 44
39 // Pick the most deserving request for our conditions. 45 // Pick the most deserving request for our conditions.
40 const SavePageRequest& picked_request = requests[0]; 46 const SavePageRequest* picked_request = nullptr;
41 47
42 // When we have a best request to try next, get the request coodinator to 48 // Handle each request only once, replacing the best reqeust candidate if it
43 // start it. 49 // is better.
44 picked_callback_.Run(picked_request); 50 for (unsigned i = 0; i < requests.size(); ++i) {
51 if (!RequestConditionsSatisfied(requests[i])) {
52 continue;
53 }
54 if (IsNewRequestBetter(picked_request, &(requests[i])))
55 picked_request = &(requests[i]);
56 }
57
58 // If we have a best request to try next, get the request coodinator to
59 // start it. Otherwise return that we have no candidates.
60 if (picked_request != nullptr) {
61 picked_callback_.Run(*picked_request);
62 } else {
63 empty_callback_.Run();
64 }
65 }
66
67 // Filter out requests that don't meet the current conditions. For instance, if
68 // this is a predictive request, and we are not on WiFi, it should be ignored
69 // this round.
70 bool RequestPicker::RequestConditionsSatisfied(const SavePageRequest& request) {
71 // If the user did not request the page directly, make sure we are connected
72 // to power and have WiFi and sufficient battery remaining before we take this
73 // reqeust.
74 // TODO(petewil): We may later want to configure whether to allow 2G for non
75 // user_requested items, add that to policy.
76 if (!request.user_requested()) {
77 if (!current_conditions_->IsPowerConnected())
78 return false;
79
80 if (current_conditions_->GetNetConnectionType() !=
81 net::NetworkChangeNotifier::ConnectionType::CONNECTION_WIFI) {
82 return false;
83 }
84
85 if (current_conditions_->GetBatteryPercentage() <
86 policy_->GetBatteryRequiredForSpeculativeOfflining()) {
87 return false;
88 }
89 }
90
91 // If we have already tried this page the max number of times, it is not
92 // eligible to try again.
93 // TODO(petewil): Instead, we should have code to remove the page from the
94 // queue after the last retry.
95 if (request.attempt_count() >= policy_->GetMaxRetries())
96 return false;
97
98 // If this request is not active yet, return false.
99 if (request.activation_time() > base::Time::Now())
100 return false;
101
102 return true;
103 }
104
105 // Look at policies to decide if we prefer more-tried or less tried requests.
106 bool RequestPicker::IsNewRequestBetter(
107 const SavePageRequest* oldRequest, const SavePageRequest* newRequest) {
108
109 // If there is no old request, the new one is better.
110 if (oldRequest == nullptr)
111 return true;
112
113 // User requested pages get priority.
114 if (newRequest->user_requested() && !oldRequest->user_requested())
115 return true;
116
117 // First, see if we can decide based on the retry count.
118 if (policy_->ShouldPreferTriedRequests()) {
119 // We prefer more-tried requests.
120 if (newRequest->attempt_count() > oldRequest->attempt_count())
121 return true;
122 } else {
123 // We prefer less-tried requests.
124 if (newRequest->attempt_count() < oldRequest->attempt_count())
125 return true;
126 }
127
128 // If we found that this wasn't as good in the area of request count,
dougarnett 2016/07/19 00:20:44 I'm not sure I understand this initially. Is this
Pete Williamson 2016/07/19 19:48:08 I was trying to make the logic above smaller by co
129 // then we prefer the old request, and exit now.
130 if (newRequest->attempt_count() != oldRequest->attempt_count())
131 return false;
132
133 // Try counts are the same, so look at other criteria.
134
135 // Should we prefer earlier requests or later ones?
136 if (policy_->ShouldPreferEarlierRequests()) {
137 // We prefer requests made earlier.
138 if (newRequest->creation_time() < oldRequest->creation_time()) {
dougarnett 2016/07/19 00:20:44 So we make no assumptions about queue ordering whi
Pete Williamson 2016/07/19 19:48:08 The "queue" doesn't do any ordering for us in spit
139 return true;
140 }
141 } else {
142 // We prefer requests made more recently.
143 if (newRequest->creation_time() < oldRequest->creation_time())
144 return true;
145 }
146
147 return false;
45 } 148 }
46 149
47 } // namespace offline_pages 150 } // namespace offline_pages
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698