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

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

Issue 2233493003: [Offline Pages] Remove expired requests from the queue. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fixing patching error. 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
11 namespace { 11 namespace {
12 template <typename T> 12 template <typename T>
13 int signum(T t) { 13 int signum(T t) {
14 return (T(0) < t) - (t < T(0)); 14 return (T(0) < t) - (t < T(0));
15 } 15 }
16 16
17 #define CALL_MEMBER_FUNCTION(object,ptrToMember) ((object)->*(ptrToMember)) 17 #define CALL_MEMBER_FUNCTION(object,ptrToMember) ((object)->*(ptrToMember))
18 } // namespace 18 } // namespace
19 19
20 namespace offline_pages { 20 namespace offline_pages {
21 21
22 RequestPicker::RequestPicker( 22 RequestPicker::RequestPicker(RequestQueue* requestQueue,
23 RequestQueue* requestQueue, OfflinerPolicy* policy) 23 OfflinerPolicy* policy,
24 RequestCoordinator* coordinator)
24 : queue_(requestQueue), 25 : queue_(requestQueue),
25 policy_(policy), 26 policy_(policy),
27 coordinator_(coordinator),
26 fewer_retries_better_(false), 28 fewer_retries_better_(false),
27 earlier_requests_better_(false), 29 earlier_requests_better_(false),
28 weak_ptr_factory_(this) {} 30 weak_ptr_factory_(this) {}
29 31
30 RequestPicker::~RequestPicker() {} 32 RequestPicker::~RequestPicker() {}
31 33
32 // Entry point for the async operation to choose the next request. 34 // Entry point for the async operation to choose the next request.
33 void RequestPicker::ChooseNextRequest( 35 void RequestPicker::ChooseNextRequest(
34 RequestCoordinator::RequestPickedCallback picked_callback, 36 RequestCoordinator::RequestPickedCallback picked_callback,
35 RequestCoordinator::RequestQueueEmptyCallback empty_callback, 37 RequestCoordinator::RequestQueueEmptyCallback empty_callback,
(...skipping 12 matching lines...) Expand all
48 // request to operate on (if any). 50 // request to operate on (if any).
49 void RequestPicker::GetRequestResultCallback( 51 void RequestPicker::GetRequestResultCallback(
50 RequestQueue::GetRequestsResult, 52 RequestQueue::GetRequestsResult,
51 const std::vector<SavePageRequest>& requests) { 53 const std::vector<SavePageRequest>& requests) {
52 // If there is nothing to do, return right away. 54 // If there is nothing to do, return right away.
53 if (requests.size() == 0) { 55 if (requests.size() == 0) {
54 empty_callback_.Run(); 56 empty_callback_.Run();
55 return; 57 return;
56 } 58 }
57 59
60 // Get the expired requests to be removed from the queue, and the valid ones
61 // from which to pick the next request.
62 std::vector<SavePageRequest> valid_requests;
63 std::vector<SavePageRequest> expired_requests;
64 SplitRequests(requests, valid_requests, expired_requests);
65 std::vector<int64_t> expired_request_ids;
66 for (auto request : expired_requests)
67 expired_request_ids.push_back(request.request_id());
68
69 queue_->RemoveRequestsById(
70 expired_request_ids,
71 base::Bind(&RequestPicker::OnRequestExpired,
72 weak_ptr_factory_.GetWeakPtr(), expired_requests));
73
58 // Pick the most deserving request for our conditions. 74 // Pick the most deserving request for our conditions.
59 const SavePageRequest* picked_request = nullptr; 75 const SavePageRequest* picked_request = nullptr;
60 76
61 RequestCompareFunction comparator = nullptr; 77 RequestCompareFunction comparator = nullptr;
62 78
63 // Choose which comparison function to use based on policy. 79 // Choose which comparison function to use based on policy.
64 if (policy_->RetryCountIsMoreImportantThanRecency()) 80 if (policy_->RetryCountIsMoreImportantThanRecency())
65 comparator = &RequestPicker::RetryCountFirstCompareFunction; 81 comparator = &RequestPicker::RetryCountFirstCompareFunction;
66 else 82 else
67 comparator = &RequestPicker::RecencyFirstCompareFunction; 83 comparator = &RequestPicker::RecencyFirstCompareFunction;
68 84
69 // Iterate once through the requests, keeping track of best candidate. 85 // Iterate once through the requests, keeping track of best candidate.
70 for (unsigned i = 0; i < requests.size(); ++i) { 86 for (unsigned i = 0; i < valid_requests.size(); ++i) {
71 if (!RequestConditionsSatisfied(requests[i])) 87 if (!RequestConditionsSatisfied(valid_requests[i]))
72 continue; 88 continue;
73 if (IsNewRequestBetter(picked_request, &(requests[i]), comparator)) 89 if (IsNewRequestBetter(picked_request, &(valid_requests[i]), comparator))
74 picked_request = &(requests[i]); 90 picked_request = &(valid_requests[i]);
75 } 91 }
76 92
77 // If we have a best request to try next, get the request coodinator to 93 // If we have a best request to try next, get the request coodinator to
78 // start it. Otherwise return that we have no candidates. 94 // start it. Otherwise return that we have no candidates.
79 if (picked_request != nullptr) { 95 if (picked_request != nullptr) {
80 picked_callback_.Run(*picked_request); 96 picked_callback_.Run(*picked_request);
81 } else { 97 } else {
82 empty_callback_.Run(); 98 empty_callback_.Run();
83 } 99 }
84 } 100 }
(...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after
212 base::TimeDelta difference = left->creation_time() - right->creation_time(); 228 base::TimeDelta difference = left->creation_time() - right->creation_time();
213 int result = signum(difference.InMilliseconds()); 229 int result = signum(difference.InMilliseconds());
214 230
215 // Flip the direction of comparison if policy prefers fewer retries. 231 // Flip the direction of comparison if policy prefers fewer retries.
216 if (earlier_requests_better_) 232 if (earlier_requests_better_)
217 result *= -1; 233 result *= -1;
218 234
219 return result; 235 return result;
220 } 236 }
221 237
238 // Split all requests into expired ones and still valid ones.
239 void RequestPicker::SplitRequests(
240 const std::vector<SavePageRequest>& requests,
241 std::vector<SavePageRequest>& valid_requests,
242 std::vector<SavePageRequest>& expired_requests) {
243 for (SavePageRequest request : requests) {
244 if (base::Time::Now() - request.creation_time() >=
245 base::TimeDelta::FromSeconds(kRequestExpirationTimeInSeconds)) {
246 expired_requests.push_back(request);
247 } else {
248 valid_requests.push_back(request);
249 }
250 }
251 }
252
253 // Callback used after expired requests are deleted from the queue and notifies
254 // the coordinator.
255 void RequestPicker::OnRequestExpired(
256 const std::vector<SavePageRequest>& expired_requests,
257 RequestQueue::UpdateRequestResult result) {
258 coordinator_->NotifyRequestsExpired(expired_requests, result);
Pete Williamson 2016/08/10 17:15:11 When the observer changelist lands, this should be
romax 2016/08/16 23:20:56 Done.
259 }
260
222 } // namespace offline_pages 261 } // namespace offline_pages
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698