| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #ifndef COMPONENTS_OFFLINE_PAGES_BACKGROUND_PICK_REQUEST_TASK_H_ | |
| 6 #define COMPONENTS_OFFLINE_PAGES_BACKGROUND_PICK_REQUEST_TASK_H_ | |
| 7 | |
| 8 #include <set> | |
| 9 | |
| 10 #include "base/memory/weak_ptr.h" | |
| 11 #include "components/offline_pages/background/request_queue_results.h" | |
| 12 #include "components/offline_pages/background/save_page_request.h" | |
| 13 #include "components/offline_pages/core/task.h" | |
| 14 | |
| 15 namespace offline_pages { | |
| 16 | |
| 17 class DeviceConditions; | |
| 18 class OfflinerPolicy; | |
| 19 class PickRequestTask; | |
| 20 class RequestCoordinatorEventLogger; | |
| 21 class RequestNotifier; | |
| 22 class RequestQueueStore; | |
| 23 | |
| 24 typedef bool (PickRequestTask::*RequestCompareFunction)( | |
| 25 const SavePageRequest* left, | |
| 26 const SavePageRequest* right); | |
| 27 | |
| 28 class PickRequestTask : public Task { | |
| 29 public: | |
| 30 // Callback to report when a request was available. | |
| 31 typedef base::Callback<void(const SavePageRequest& request)> | |
| 32 RequestPickedCallback; | |
| 33 | |
| 34 // Callback to report when no request was available. | |
| 35 typedef base::Callback<void(bool)> RequestNotPickedCallback; | |
| 36 | |
| 37 // Callback to report available total and available queued request counts. | |
| 38 typedef base::Callback<void(size_t, size_t)> RequestCountCallback; | |
| 39 | |
| 40 PickRequestTask(RequestQueueStore* store, | |
| 41 OfflinerPolicy* policy, | |
| 42 RequestNotifier* notifier, | |
| 43 RequestCoordinatorEventLogger* event_logger, | |
| 44 RequestPickedCallback picked_callback, | |
| 45 RequestNotPickedCallback not_picked_callback, | |
| 46 RequestCountCallback request_count_callback, | |
| 47 DeviceConditions& device_conditions, | |
| 48 const std::set<int64_t>& disabled_requests); | |
| 49 | |
| 50 ~PickRequestTask() override; | |
| 51 | |
| 52 // TaskQueue::Task implementation, starts the async chain | |
| 53 void Run() override; | |
| 54 | |
| 55 private: | |
| 56 // Step 1, handle getting the results from the store. | |
| 57 void ChooseAndPrune(bool request, | |
| 58 std::vector<std::unique_ptr<SavePageRequest>> requests); | |
| 59 | |
| 60 // Step 2a. Handle choosing an entry, and calling the right picked callback | |
| 61 // and the request count callback. | |
| 62 void ChooseRequestAndCallback( | |
| 63 std::vector<std::unique_ptr<SavePageRequest>> valid_requests); | |
| 64 | |
| 65 // Step 2b. Handle deleting stale entries and notifying observers. | |
| 66 void RemoveStaleRequests(std::vector<int64_t> stale_request_ids); | |
| 67 | |
| 68 // Step 3. Send delete notifications for the expired requests. | |
| 69 void OnRequestsExpired(std::unique_ptr<UpdateRequestsResult> result); | |
| 70 | |
| 71 // Helper functions. | |
| 72 | |
| 73 // Split requests into valid and expired categories. | |
| 74 void SplitRequests( | |
| 75 std::vector<std::unique_ptr<SavePageRequest>> requests, | |
| 76 std::vector<std::unique_ptr<SavePageRequest>>* valid_requests, | |
| 77 std::vector<int64_t>* expired_request_ids); | |
| 78 | |
| 79 // Determine if this request has device conditions appropriate for running it. | |
| 80 bool RequestConditionsSatisfied(const SavePageRequest* request); | |
| 81 | |
| 82 // Determine if the new request is preferred under current policies. | |
| 83 bool IsNewRequestBetter(const SavePageRequest* oldRequest, | |
| 84 const SavePageRequest* newRequest, | |
| 85 RequestCompareFunction comparator); | |
| 86 | |
| 87 // Returns true if the left hand side is better. | |
| 88 bool RetryCountFirstCompareFunction(const SavePageRequest* left, | |
| 89 const SavePageRequest* right); | |
| 90 | |
| 91 // Returns true if the left hand side is better. | |
| 92 bool RecencyFirstCompareFunction(const SavePageRequest* left, | |
| 93 const SavePageRequest* right); | |
| 94 | |
| 95 // Compare left and right side, returning 1 if the left side is better | |
| 96 // (preferred by policy), 0 if the same, and -1 if the right side is better. | |
| 97 int CompareRetryCount(const SavePageRequest* left, | |
| 98 const SavePageRequest* right); | |
| 99 | |
| 100 // Compare left and right side, returning 1 if the left side is better | |
| 101 // (preferred by policy), 0 if the same, and -1 if the right side is better. | |
| 102 int CompareCreationTime(const SavePageRequest* left, | |
| 103 const SavePageRequest* right); | |
| 104 | |
| 105 // Member variables, all pointers are not owned here. | |
| 106 RequestQueueStore* store_; | |
| 107 OfflinerPolicy* policy_; | |
| 108 RequestNotifier* notifier_; | |
| 109 RequestCoordinatorEventLogger* event_logger_; | |
| 110 RequestPickedCallback picked_callback_; | |
| 111 RequestNotPickedCallback not_picked_callback_; | |
| 112 RequestCountCallback request_count_callback_; | |
| 113 std::unique_ptr<DeviceConditions> device_conditions_; | |
| 114 const std::set<int64_t>& disabled_requests_; | |
| 115 // Allows us to pass a weak pointer to callbacks. | |
| 116 base::WeakPtrFactory<PickRequestTask> weak_ptr_factory_; | |
| 117 }; | |
| 118 | |
| 119 } // namespace offline_pages | |
| 120 | |
| 121 #endif // COMPONENTS_OFFLINE_PAGES_BACKGROUND_PICK_REQUEST_TASK_H_ | |
| OLD | NEW |