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

Side by Side Diff: components/offline_pages/core/background/pick_request_task.cc

Issue 2729763002: [Offline Pages] Pick correct request when resuming. (Closed)
Patch Set: comments from Pete. Created 3 years, 9 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/core/background/pick_request_task.h" 5 #include "components/offline_pages/core/background/pick_request_task.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 "base/time/time.h" 9 #include "base/time/time.h"
10 #include "components/offline_pages/core/background/device_conditions.h" 10 #include "components/offline_pages/core/background/device_conditions.h"
(...skipping 17 matching lines...) Expand all
28 } // namespace 28 } // namespace
29 29
30 namespace offline_pages { 30 namespace offline_pages {
31 31
32 PickRequestTask::PickRequestTask(RequestQueueStore* store, 32 PickRequestTask::PickRequestTask(RequestQueueStore* store,
33 OfflinerPolicy* policy, 33 OfflinerPolicy* policy,
34 RequestPickedCallback picked_callback, 34 RequestPickedCallback picked_callback,
35 RequestNotPickedCallback not_picked_callback, 35 RequestNotPickedCallback not_picked_callback,
36 RequestCountCallback request_count_callback, 36 RequestCountCallback request_count_callback,
37 DeviceConditions& device_conditions, 37 DeviceConditions& device_conditions,
38 const std::set<int64_t>& disabled_requests) 38 const std::set<int64_t>& disabled_requests,
39 std::vector<int64_t>& prioritized_requests)
39 : store_(store), 40 : store_(store),
40 policy_(policy), 41 policy_(policy),
41 picked_callback_(picked_callback), 42 picked_callback_(picked_callback),
42 not_picked_callback_(not_picked_callback), 43 not_picked_callback_(not_picked_callback),
43 request_count_callback_(request_count_callback), 44 request_count_callback_(request_count_callback),
44 disabled_requests_(disabled_requests), 45 disabled_requests_(disabled_requests),
46 prioritized_requests_(prioritized_requests),
45 weak_ptr_factory_(this) { 47 weak_ptr_factory_(this) {
46 device_conditions_.reset(new DeviceConditions(device_conditions)); 48 device_conditions_.reset(new DeviceConditions(device_conditions));
47 } 49 }
48 50
49 PickRequestTask::~PickRequestTask() {} 51 PickRequestTask::~PickRequestTask() {}
50 52
51 void PickRequestTask::Run() { 53 void PickRequestTask::Run() {
52 GetRequests(); 54 GetRequests();
53 } 55 }
54 56
(...skipping 22 matching lines...) Expand all
77 // Choose which comparison function to use based on policy. 79 // Choose which comparison function to use based on policy.
78 if (policy_->RetryCountIsMoreImportantThanRecency()) 80 if (policy_->RetryCountIsMoreImportantThanRecency())
79 comparator = &PickRequestTask::RetryCountFirstCompareFunction; 81 comparator = &PickRequestTask::RetryCountFirstCompareFunction;
80 else 82 else
81 comparator = &PickRequestTask::RecencyFirstCompareFunction; 83 comparator = &PickRequestTask::RecencyFirstCompareFunction;
82 84
83 // TODO(petewil): Consider replacing this bool with a better named enum. 85 // TODO(petewil): Consider replacing this bool with a better named enum.
84 bool non_user_requested_tasks_remaining = false; 86 bool non_user_requested_tasks_remaining = false;
85 bool cleanup_needed = false; 87 bool cleanup_needed = false;
86 88
89 size_t request_size = requests.size();
87 size_t available_request_count = 0; 90 size_t available_request_count = 0;
88 91
89 // Iterate once through the requests, keeping track of best candidate. 92 std::vector<std::unique_ptr<SavePageRequest>> available_requests;
93
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++;
114 } 120 }
115 if (!RequestConditionsSatisfied(requests[i].get())) 121 available_requests.push_back(std::move(requests[i]));
116 continue;
117 if (IsNewRequestBetter(picked_request, requests[i].get(), comparator))
118 picked_request = requests[i].get();
119 } 122 }
123 // Report the request queue counts.
124 request_count_callback_.Run(request_size, available_requests.size());
120 125
121 // Report the request queue counts. 126 // Search for and pick the prioritized request which is not disabled and in
122 request_count_callback_.Run(requests.size(), available_request_count); 127 // the request queue. An assumption is that requests later in the vector have
128 // higher priority than earlier ones in the vector. Also remove the picked
Pete Williamson 2017/03/07 20:57:02 Why do we remove the picked request here instead o
129 // request from the prioritized list.
130 int idx_to_delete = -1;
131 for (int i = prioritized_requests_.size() - 1; !picked_request && i >= 0;
132 --i) {
133 for (unsigned j = 0; j < available_requests.size(); ++j) {
134 if (available_requests[j]->request_id() == prioritized_requests_[i]) {
135 picked_request = available_requests[j].get();
136 idx_to_delete = i;
137 break;
138 }
139 }
140 }
141 prioritized_requests_.erase(prioritized_requests_.begin() + idx_to_delete);
142
143 // If no request was found from the priority list, find the best request
144 // according to current policies.
145 if (!picked_request) {
146 for (unsigned i = 0; i < available_requests.size(); ++i) {
147 if (!RequestConditionsSatisfied(available_requests[i].get()))
148 continue;
149 if (IsNewRequestBetter(picked_request, available_requests[i].get(),
150 comparator)) {
151 picked_request = available_requests[i].get();
152 }
153 }
154 }
123 155
124 // If we have a best request to try next, get the request coodinator to 156 // If we have a best request to try next, get the request coodinator to
125 // start it. Otherwise return that we have no candidates. 157 // start it. Otherwise return that we have no candidates.
126 if (picked_request != nullptr) { 158 if (picked_request != nullptr) {
127 picked_callback_.Run(*picked_request, cleanup_needed); 159 picked_callback_.Run(*picked_request, cleanup_needed);
128 } else { 160 } else {
129 not_picked_callback_.Run(non_user_requested_tasks_remaining, 161 not_picked_callback_.Run(non_user_requested_tasks_remaining,
130 cleanup_needed); 162 cleanup_needed);
131 } 163 }
132 164
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after
246 int result = signum(difference.InMilliseconds()); 278 int result = signum(difference.InMilliseconds());
247 279
248 // Flip the direction of comparison if policy prefers fewer retries. 280 // Flip the direction of comparison if policy prefers fewer retries.
249 if (policy_->ShouldPreferEarlierRequests()) 281 if (policy_->ShouldPreferEarlierRequests())
250 result *= -1; 282 result *= -1;
251 283
252 return result; 284 return result;
253 } 285 }
254 286
255 } // namespace offline_pages 287 } // namespace offline_pages
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698