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