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

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: more comments. 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 <set>
fgorski 2017/03/09 21:18:35 #include <unordered_set> Check if you still need
8
7 #include "base/bind.h" 9 #include "base/bind.h"
8 #include "base/logging.h" 10 #include "base/logging.h"
9 #include "base/time/time.h" 11 #include "base/time/time.h"
10 #include "components/offline_pages/core/background/device_conditions.h" 12 #include "components/offline_pages/core/background/device_conditions.h"
11 #include "components/offline_pages/core/background/offliner_policy.h" 13 #include "components/offline_pages/core/background/offliner_policy.h"
12 #include "components/offline_pages/core/background/offliner_policy_utils.h" 14 #include "components/offline_pages/core/background/offliner_policy_utils.h"
13 #include "components/offline_pages/core/background/request_coordinator_event_log ger.h" 15 #include "components/offline_pages/core/background/request_coordinator_event_log ger.h"
14 #include "components/offline_pages/core/background/request_notifier.h" 16 #include "components/offline_pages/core/background/request_notifier.h"
15 #include "components/offline_pages/core/background/request_queue_store.h" 17 #include "components/offline_pages/core/background/request_queue_store.h"
16 #include "components/offline_pages/core/background/save_page_request.h" 18 #include "components/offline_pages/core/background/save_page_request.h"
(...skipping 11 matching lines...) Expand all
28 } // namespace 30 } // namespace
29 31
30 namespace offline_pages { 32 namespace offline_pages {
31 33
32 PickRequestTask::PickRequestTask(RequestQueueStore* store, 34 PickRequestTask::PickRequestTask(RequestQueueStore* store,
33 OfflinerPolicy* policy, 35 OfflinerPolicy* policy,
34 RequestPickedCallback picked_callback, 36 RequestPickedCallback picked_callback,
35 RequestNotPickedCallback not_picked_callback, 37 RequestNotPickedCallback not_picked_callback,
36 RequestCountCallback request_count_callback, 38 RequestCountCallback request_count_callback,
37 DeviceConditions& device_conditions, 39 DeviceConditions& device_conditions,
38 const std::set<int64_t>& disabled_requests) 40 const std::set<int64_t>& disabled_requests,
41 std::deque<int64_t>& prioritized_requests)
39 : store_(store), 42 : store_(store),
40 policy_(policy), 43 policy_(policy),
41 picked_callback_(picked_callback), 44 picked_callback_(picked_callback),
42 not_picked_callback_(not_picked_callback), 45 not_picked_callback_(not_picked_callback),
43 request_count_callback_(request_count_callback), 46 request_count_callback_(request_count_callback),
44 disabled_requests_(disabled_requests), 47 disabled_requests_(disabled_requests),
48 prioritized_requests_(prioritized_requests),
45 weak_ptr_factory_(this) { 49 weak_ptr_factory_(this) {
46 device_conditions_.reset(new DeviceConditions(device_conditions)); 50 device_conditions_.reset(new DeviceConditions(device_conditions));
47 } 51 }
48 52
49 PickRequestTask::~PickRequestTask() {} 53 PickRequestTask::~PickRequestTask() {}
50 54
51 void PickRequestTask::Run() { 55 void PickRequestTask::Run() {
52 GetRequests(); 56 GetRequests();
53 } 57 }
54 58
(...skipping 23 matching lines...) Expand all
78 if (policy_->RetryCountIsMoreImportantThanRecency()) 82 if (policy_->RetryCountIsMoreImportantThanRecency())
79 comparator = &PickRequestTask::RetryCountFirstCompareFunction; 83 comparator = &PickRequestTask::RetryCountFirstCompareFunction;
80 else 84 else
81 comparator = &PickRequestTask::RecencyFirstCompareFunction; 85 comparator = &PickRequestTask::RecencyFirstCompareFunction;
82 86
83 // TODO(petewil): Consider replacing this bool with a better named enum. 87 // TODO(petewil): Consider replacing this bool with a better named enum.
84 bool non_user_requested_tasks_remaining = false; 88 bool non_user_requested_tasks_remaining = false;
85 bool cleanup_needed = false; 89 bool cleanup_needed = false;
86 90
87 size_t available_request_count = 0; 91 size_t available_request_count = 0;
92 // Request ids which are available for picking.
93 std::unordered_set<int64_t> available_request_ids;
88 94
89 // Iterate once through the requests, keeping track of best candidate. 95 // Iterate through the requests, filter out unavailable requests and get other
90 for (unsigned i = 0; i < requests.size(); ++i) { 96 // information (if cleanup is needed and number of non-user-requested
97 // requests).
98 for (const auto& request : requests) {
91 // If the request is expired or has exceeded the retry count, skip it. 99 // If the request is expired or has exceeded the retry count, skip it.
92 if (OfflinerPolicyUtils::CheckRequestExpirationStatus(requests[i].get(), 100 if (OfflinerPolicyUtils::CheckRequestExpirationStatus(request.get(),
93 policy_) != 101 policy_) !=
94 OfflinerPolicyUtils::RequestExpirationStatus::VALID) { 102 OfflinerPolicyUtils::RequestExpirationStatus::VALID) {
95 cleanup_needed = true; 103 cleanup_needed = true;
96 continue; 104 continue;
97 } 105 }
98 106 // If the request is on the disabled list, skip it.
99 // If the request is on the disabled list, skip it. 107 auto search = disabled_requests_.find(request->request_id());
100 auto search = disabled_requests_.find(requests[i]->request_id());
101 if (search != disabled_requests_.end()) 108 if (search != disabled_requests_.end())
102 continue; 109 continue;
103 110
104 // If there are non-user-requested tasks remaining, we need to make sure 111 // 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 112 // 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 113 // 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 114 // 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. 115 // non-user-requested items, which have different network and power needs.
109 if (!requests[i]->user_requested()) 116 if (!request->user_requested())
110 non_user_requested_tasks_remaining = true; 117 non_user_requested_tasks_remaining = true;
111 if (requests[i]->request_state() == 118 if (request->request_state() == SavePageRequest::RequestState::AVAILABLE) {
112 SavePageRequest::RequestState::AVAILABLE) {
113 available_request_count++; 119 available_request_count++;
114 } 120 }
115 if (!RequestConditionsSatisfied(requests[i].get())) 121 if (!RequestConditionsSatisfied(request.get()))
116 continue; 122 continue;
117 if (IsNewRequestBetter(picked_request, requests[i].get(), comparator)) 123 available_request_ids.insert(request->request_id());
118 picked_request = requests[i].get(); 124 }
125 // Report the request queue counts.
126 request_count_callback_.Run(requests.size(), available_request_count);
127
128 // Search for and pick the prioritized request which is available for picking
129 // from |available_request_ids|, the closer to the end means higher priority.
130 // Also if a request in |prioritized_requests_| doesn't exist in |requests|
131 // we're going to remove it.
132 // For every ID in |available_request_ids|, there exists a corresponding
133 // request in |requests|, so this won't be an infinite loop: either we pick a
134 // request, or there's a request being poped from |prioritized_requests_|.
135 while (!picked_request && !prioritized_requests_.empty()) {
136 if (available_request_ids.count(prioritized_requests_.back()) > 0) {
137 for (const auto& request : requests) {
138 if (request->request_id() == prioritized_requests_.back()) {
139 picked_request = request.get();
140 break;
141 }
142 }
143 DCHECK(picked_request);
144 } else {
145 prioritized_requests_.pop_back();
146 }
119 } 147 }
120 148
121 // Report the request queue counts. 149 // If no request was found from the priority list, find the best request
122 request_count_callback_.Run(requests.size(), available_request_count); 150 // according to current policies.
151 if (!picked_request) {
152 for (const auto& request : requests) {
153 if ((available_request_ids.count(request->request_id()) > 0) &&
154 (IsNewRequestBetter(picked_request, request.get(), comparator))) {
155 picked_request = request.get();
156 }
157 }
158 }
123 159
124 // If we have a best request to try next, get the request coodinator to 160 // If we have a best request to try next, get the request coodinator to
125 // start it. Otherwise return that we have no candidates. 161 // start it. Otherwise return that we have no candidates.
126 if (picked_request != nullptr) { 162 if (picked_request != nullptr) {
127 picked_callback_.Run(*picked_request, cleanup_needed); 163 picked_callback_.Run(*picked_request, cleanup_needed);
128 } else { 164 } else {
129 not_picked_callback_.Run(non_user_requested_tasks_remaining, 165 not_picked_callback_.Run(non_user_requested_tasks_remaining,
130 cleanup_needed); 166 cleanup_needed);
131 } 167 }
132 168
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after
246 int result = signum(difference.InMilliseconds()); 282 int result = signum(difference.InMilliseconds());
247 283
248 // Flip the direction of comparison if policy prefers fewer retries. 284 // Flip the direction of comparison if policy prefers fewer retries.
249 if (policy_->ShouldPreferEarlierRequests()) 285 if (policy_->ShouldPreferEarlierRequests())
250 result *= -1; 286 result *= -1;
251 287
252 return result; 288 return result;
253 } 289 }
254 290
255 } // namespace offline_pages 291 } // namespace offline_pages
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698