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

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

Issue 2218403002: Change database scheme - add state and start tracking (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Delete old database if any Created 4 years, 4 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
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after
82 empty_callback_.Run(); 82 empty_callback_.Run();
83 } 83 }
84 } 84 }
85 85
86 // Filter out requests that don't meet the current conditions. For instance, if 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 87 // this is a predictive request, and we are not on WiFi, it should be ignored
88 // this round. 88 // this round.
89 bool RequestPicker::RequestConditionsSatisfied(const SavePageRequest& request) { 89 bool RequestPicker::RequestConditionsSatisfied(const SavePageRequest& request) {
90 // If the user did not request the page directly, make sure we are connected 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 91 // to power and have WiFi and sufficient battery remaining before we take this
92 // reqeust. 92 // reqeust.
chili 2016/08/08 18:17:01 tiny nit - (since this file's touched anyway....)
Pete Williamson 2016/08/08 18:44:28 Done.
93 // TODO(petewil): We may later want to configure whether to allow 2G for non 93 // TODO(petewil): We may later want to configure whether to allow 2G for non
94 // user_requested items, add that to policy. 94 // user_requested items, add that to policy.
95 if (!request.user_requested()) { 95 if (!request.user_requested()) {
96 if (!current_conditions_->IsPowerConnected()) 96 if (!current_conditions_->IsPowerConnected())
97 return false; 97 return false;
98 98
99 if (current_conditions_->GetNetConnectionType() != 99 if (current_conditions_->GetNetConnectionType() !=
100 net::NetworkChangeNotifier::ConnectionType::CONNECTION_WIFI) { 100 net::NetworkChangeNotifier::ConnectionType::CONNECTION_WIFI) {
101 return false; 101 return false;
102 } 102 }
103 103
104 if (current_conditions_->GetBatteryPercentage() < 104 if (current_conditions_->GetBatteryPercentage() <
105 policy_->GetMinimumBatteryPercentageForNonUserRequestOfflining()) { 105 policy_->GetMinimumBatteryPercentageForNonUserRequestOfflining()) {
106 return false; 106 return false;
107 } 107 }
108 } 108 }
109 109
110 // If we have already started this page the max number of times, it is not
111 // eligible to try again.
112 // TODO(petewil): We should have code to remove the page from the
113 // queue after the last retry.
114 if (request.started_attempt_count() >= policy_->GetMaxStartedTries())
115 return false;
116
110 // If we have already tried this page the max number of times, it is not 117 // If we have already tried this page the max number of times, it is not
111 // eligible to try again. 118 // eligible to try again.
chili 2016/08/08 18:17:01 nit: this comment is the same as the one above. P
Pete Williamson 2016/08/08 18:44:28 comment updated.
112 // TODO(petewil): Instead, we should have code to remove the page from the 119 // TODO(petewil): We should have code to remove the page from the
113 // queue after the last retry. 120 // queue after the last retry.
114 if (request.attempt_count() > policy_->GetMaxTries()) 121 if (request.completed_attempt_count() >= policy_->GetMaxCompletedTries())
115 return false; 122 return false;
116 123
117 // If the request is expired, do not consider it. 124 // If the request is expired, do not consider it.
118 // TODO(petewil): We need to remove this from the queue. 125 // TODO(petewil): We need to remove this from the queue.
119 base::TimeDelta requestAge = base::Time::Now() - request.creation_time(); 126 base::TimeDelta requestAge = base::Time::Now() - request.creation_time();
120 if (requestAge > 127 if (requestAge >
121 base::TimeDelta::FromSeconds( 128 base::TimeDelta::FromSeconds(
122 policy_->GetRequestExpirationTimeInSeconds())) 129 policy_->GetRequestExpirationTimeInSeconds()))
123 return false; 130 return false;
124 131
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
180 result = CompareRetryCount(left, right); 187 result = CompareRetryCount(left, right);
181 188
182 return (result > 0); 189 return (result > 0);
183 } 190 }
184 191
185 // Compare left and right side, returning 1 if the left side is better 192 // Compare left and right side, returning 1 if the left side is better
186 // (preferred by policy), 0 if the same, and -1 if the right side is better. 193 // (preferred by policy), 0 if the same, and -1 if the right side is better.
187 int RequestPicker::CompareRetryCount( 194 int RequestPicker::CompareRetryCount(
188 const SavePageRequest* left, const SavePageRequest* right) { 195 const SavePageRequest* left, const SavePageRequest* right) {
189 // Check the attempt count. 196 // Check the attempt count.
190 int result = signum(left->attempt_count() - right->attempt_count()); 197 int result = signum(left->completed_attempt_count() -
198 right->completed_attempt_count());
191 199
192 // Flip the direction of comparison if policy prefers fewer retries. 200 // Flip the direction of comparison if policy prefers fewer retries.
193 if (fewer_retries_better_) 201 if (fewer_retries_better_)
194 result *= -1; 202 result *= -1;
195 203
196 return result; 204 return result;
197 } 205 }
198 206
199 // Compare left and right side, returning 1 if the left side is better 207 // Compare left and right side, returning 1 if the left side is better
200 // (preferred by policy), 0 if the same, and -1 if the right side is better. 208 // (preferred by policy), 0 if the same, and -1 if the right side is better.
201 int RequestPicker::CompareCreationTime( 209 int RequestPicker::CompareCreationTime(
202 const SavePageRequest* left, const SavePageRequest* right) { 210 const SavePageRequest* left, const SavePageRequest* right) {
203 // Check the recency. 211 // Check the recency.
204 base::TimeDelta difference = left->creation_time() - right->creation_time(); 212 base::TimeDelta difference = left->creation_time() - right->creation_time();
205 int result = signum(difference.InMilliseconds()); 213 int result = signum(difference.InMilliseconds());
206 214
207 // Flip the direction of comparison if policy prefers fewer retries. 215 // Flip the direction of comparison if policy prefers fewer retries.
208 if (earlier_requests_better_) 216 if (earlier_requests_better_)
209 result *= -1; 217 result *= -1;
210 218
211 return result; 219 return result;
212 } 220 }
213 221
214 } // namespace offline_pages 222 } // namespace offline_pages
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698