Chromium Code Reviews| 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/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 queue contents from the queue, use them to pick the next | |
|
dougarnett
2016/07/18 21:45:44
remove one of the "queue" references on this line
Pete Williamson
2016/07/18 22:52:09
Done.
| |
| 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 |
|
dougarnett
2016/07/18 21:45:44
Consider if better: Iterate once through the reque
Pete Williamson
2016/07/18 22:52:09
I think that is what we are doing. We iterate onc
dougarnett
2016/07/19 00:20:44
Was proposing different comment wording. I found "
Pete Williamson
2016/07/19 19:48:08
Done.
| |
| 43 // start it. | 49 // is better. |
| 44 picked_callback_.Run(picked_request); | 50 for (unsigned i = 0; i < requests.size(); ++i) { |
| 51 if (!RequestMeetsConditions(requests[i])) { | |
|
dougarnett
2016/07/18 21:45:44
ConditionsSatisyRequest() ?
Pete Williamson
2016/07/18 22:52:09
ConditionsSatisfyRequest seems a bit ambiguous to
dougarnett
2016/07/19 00:20:44
good
Pete Williamson
2016/07/19 19:48:08
Done.
| |
| 52 DVLOG(0) << "@@@@@@ request did not meet."; | |
|
dougarnett
2016/07/18 21:45:44
clean up these before landing?
Pete Williamson
2016/07/18 22:52:09
Oops, though they were already gone. Removed now.
| |
| 53 continue; | |
| 54 } | |
| 55 DVLOG(0) << "@@@@@@ request met."; | |
| 56 if (IsNewRequestBetter(picked_request, &(requests[i]))) | |
| 57 picked_request = &(requests[i]); | |
| 58 } | |
| 59 | |
| 60 // If we have a best request to try next, get the request coodinator to | |
| 61 // start it. Otherwise return that we have no candidates. | |
| 62 if (picked_request != nullptr) { | |
| 63 picked_callback_.Run(*picked_request); | |
| 64 } else { | |
| 65 empty_callback_.Run(); | |
| 66 } | |
| 67 } | |
| 68 | |
| 69 // Filter out requests that don't meet the current conditions. For instance, if | |
| 70 // this is a predictive request, and we are not on WiFi, it should be ignored | |
| 71 // this round. | |
| 72 bool RequestPicker::RequestMeetsConditions(const SavePageRequest& request) { | |
|
dougarnett
2016/07/18 21:45:44
might be nicer to pass in conditions rather then u
Pete Williamson
2016/07/18 22:52:09
I'm not sure it helps much, we typically make a ne
dougarnett
2016/07/19 00:20:44
Just was idea to consider. It seems like pure logi
| |
| 73 // If the user did not request the page directly, make sure we are connected | |
| 74 // to power and have WiFi and sufficient battery remaining before we take this | |
| 75 // reqeust. | |
| 76 // TODO(petewil): We may later want to configure whether to allow 2G for non | |
| 77 // user_requested items, add that to policy. | |
| 78 if (!request.user_requested()) { | |
| 79 if (!current_conditions_->IsPowerConnected()) | |
| 80 return false; | |
| 81 | |
| 82 if (current_conditions_->GetNetConnectionType() != | |
| 83 net::NetworkChangeNotifier::ConnectionType::CONNECTION_WIFI) { | |
| 84 return false; | |
| 85 } | |
| 86 | |
| 87 if (current_conditions_->GetBatteryPercentage() < | |
| 88 policy_->GetBatteryRequiredForSpeculativeOfflining()) { | |
| 89 return false; | |
| 90 } | |
| 91 } | |
| 92 | |
| 93 // If we have already tried this page the max number of times, it is not | |
| 94 // eligible to try again. | |
| 95 // TODO(petewil): Instead, we should have code to remove the page from the | |
| 96 // queue after the last retry. | |
| 97 if (request.attempt_count() >= policy_->GetMaxRetries()) | |
| 98 return false; | |
| 99 | |
| 100 // If this request is not active yet, return false. | |
| 101 if (request.activation_time() > base::Time::Now()) | |
| 102 return false; | |
| 103 | |
| 104 return true; | |
| 105 } | |
| 106 | |
| 107 // Look at policies to decide if we prefer more-tried or less tried requests. | |
|
Pete Williamson
2016/07/18 17:41:06
Reviewers: Please think about if we got the right
dougarnett
2016/07/19 15:49:49
Down the road, I expect we may want to consider la
Pete Williamson
2016/07/19 19:48:08
Per our discussion, I added a new policy to pick w
| |
| 108 bool RequestPicker::IsNewRequestBetter( | |
| 109 const SavePageRequest* oldRequest, const SavePageRequest* newRequest) { | |
| 110 | |
| 111 // If there is no old request, the new one is better. | |
| 112 if (oldRequest == nullptr) | |
| 113 return true; | |
| 114 | |
| 115 // User requested pages get priority. | |
| 116 if (newRequest->user_requested() && !oldRequest->user_requested()) | |
| 117 return true; | |
| 118 | |
| 119 // First, see if we can decide based on the retry count. | |
| 120 if (policy_->ShouldPreferTriedRequests()) { | |
| 121 // We prefer more-tried requests. | |
| 122 if (newRequest->attempt_count() > oldRequest->attempt_count()) | |
| 123 return true; | |
| 124 } else { | |
| 125 // We prefer less-tried requests. | |
| 126 if (newRequest->attempt_count() < oldRequest->attempt_count()) | |
| 127 return true; | |
| 128 } | |
| 129 | |
| 130 // If we found that this wasn't as good in the area of request count, | |
| 131 // then we prefer the old request, and exit now. | |
| 132 if (newRequest->attempt_count() != oldRequest->attempt_count()) | |
| 133 return false; | |
| 134 | |
| 135 // Try counts are the same, so look at other criteria. | |
| 136 | |
| 137 // Should we prefer earlier requests or later ones? | |
| 138 if (policy_->ShouldPreferEarlierRequests()) { | |
| 139 // We prefer requests made earlier. | |
| 140 if (newRequest->creation_time() < oldRequest->creation_time()) { | |
| 141 return true; | |
| 142 } | |
| 143 } else { | |
| 144 // We prefer requests made more recently. | |
| 145 if (newRequest->creation_time() < oldRequest->creation_time()) | |
| 146 return true; | |
| 147 } | |
| 148 | |
| 149 return false; | |
| 45 } | 150 } |
| 46 | 151 |
| 47 } // namespace offline_pages | 152 } // namespace offline_pages |
| OLD | NEW |