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

Unified Diff: components/offline_pages/core/background/pick_request_task.cc

Issue 2729763002: [Offline Pages] Pick correct request when resuming. (Closed)
Patch Set: After offline discussion. 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 side-by-side diff with in-line comments
Download patch
Index: components/offline_pages/core/background/pick_request_task.cc
diff --git a/components/offline_pages/core/background/pick_request_task.cc b/components/offline_pages/core/background/pick_request_task.cc
index 006d08b47aff2211d377e6869f8b95e49273185f..f1a612c0c0553845057d7e9527079b071e290166 100644
--- a/components/offline_pages/core/background/pick_request_task.cc
+++ b/components/offline_pages/core/background/pick_request_task.cc
@@ -4,6 +4,8 @@
#include "components/offline_pages/core/background/pick_request_task.h"
+#include <set>
+
#include "base/bind.h"
#include "base/logging.h"
#include "base/time/time.h"
@@ -35,13 +37,15 @@ PickRequestTask::PickRequestTask(RequestQueueStore* store,
RequestNotPickedCallback not_picked_callback,
RequestCountCallback request_count_callback,
DeviceConditions& device_conditions,
- const std::set<int64_t>& disabled_requests)
+ const std::set<int64_t>& disabled_requests,
+ std::deque<int64_t>& prioritized_requests)
: store_(store),
policy_(policy),
picked_callback_(picked_callback),
not_picked_callback_(not_picked_callback),
request_count_callback_(request_count_callback),
disabled_requests_(disabled_requests),
+ prioritized_requests_(prioritized_requests),
weak_ptr_factory_(this) {
device_conditions_.reset(new DeviceConditions(device_conditions));
}
@@ -85,8 +89,11 @@ void PickRequestTask::Choose(
bool cleanup_needed = false;
size_t available_request_count = 0;
+ std::set<int64_t> available_request_ids;
- // Iterate once through the requests, keeping track of best candidate.
+ // Iterate through the requests, filter out unavailable requests and get other
+ // information (if cleanup is needed and number of non-user-requested
+ // requests).
for (unsigned i = 0; i < requests.size(); ++i) {
// If the request is expired or has exceeded the retry count, skip it.
if (OfflinerPolicyUtils::CheckRequestExpirationStatus(requests[i].get(),
@@ -95,8 +102,7 @@ void PickRequestTask::Choose(
cleanup_needed = true;
continue;
}
-
- // If the request is on the disabled list, skip it.
+ // If the request is on the disabled list, skip it.
auto search = disabled_requests_.find(requests[i]->request_id());
if (search != disabled_requests_.end())
continue;
@@ -114,13 +120,41 @@ void PickRequestTask::Choose(
}
if (!RequestConditionsSatisfied(requests[i].get()))
continue;
- if (IsNewRequestBetter(picked_request, requests[i].get(), comparator))
- picked_request = requests[i].get();
+ available_request_ids.insert(requests[i]->request_id());
}
-
// Report the request queue counts.
request_count_callback_.Run(requests.size(), available_request_count);
+ // Search for and pick the prioritized request which is not disabled and in
+ // the request queue. An assumption is that requests later in the vector have
+ // higher priority than earlier ones in the vector. Also remove the picked
+ // request from the prioritized list.
+ while (!picked_request && !prioritized_requests_.empty()) {
+ if (available_request_ids.find(prioritized_requests_.back()) !=
+ available_request_ids.end()) {
+ for (unsigned i = 0; i < requests.size(); ++i) {
+ if (requests[i]->request_id() == prioritized_requests_.back()) {
+ picked_request = requests[i].get();
+ break;
+ }
+ }
+ } else {
+ prioritized_requests_.pop_back();
+ }
+ }
+
+ // If no request was found from the priority list, find the best request
+ // according to current policies.
+ if (!picked_request) {
+ for (unsigned i = 0; i < requests.size(); ++i) {
+ if ((available_request_ids.find(requests[i]->request_id()) !=
+ available_request_ids.end()) &&
+ (IsNewRequestBetter(picked_request, requests[i].get(), comparator))) {
+ picked_request = requests[i].get();
+ }
+ }
+ }
+
// If we have a best request to try next, get the request coodinator to
// start it. Otherwise return that we have no candidates.
if (picked_request != nullptr) {

Powered by Google App Engine
This is Rietveld 408576698