| 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_REQUEST_PICKER_H_ | |
| 6 #define COMPONENTS_OFFLINE_PAGES_BACKGROUND_REQUEST_PICKER_H_ | |
| 7 | |
| 8 #include <memory> | |
| 9 #include <set> | |
| 10 | |
| 11 #include "base/memory/weak_ptr.h" | |
| 12 #include "components/offline_pages/background/device_conditions.h" | |
| 13 #include "components/offline_pages/background/offliner_policy.h" | |
| 14 #include "components/offline_pages/background/request_coordinator.h" | |
| 15 #include "components/offline_pages/background/request_coordinator_event_logger.h
" | |
| 16 #include "components/offline_pages/background/request_queue.h" | |
| 17 | |
| 18 namespace offline_pages { | |
| 19 | |
| 20 class RequestNotifier; | |
| 21 | |
| 22 typedef bool (RequestPicker::*RequestCompareFunction)( | |
| 23 const SavePageRequest* left, const SavePageRequest* right); | |
| 24 | |
| 25 class RequestPicker { | |
| 26 public: | |
| 27 RequestPicker(RequestQueue* requestQueue, | |
| 28 OfflinerPolicy* policy, | |
| 29 RequestNotifier* notifier, | |
| 30 RequestCoordinatorEventLogger* event_logger); | |
| 31 | |
| 32 ~RequestPicker(); | |
| 33 | |
| 34 // Choose which request we should process next based on the current | |
| 35 // conditions, and call back to the RequestCoordinator when we have one. | |
| 36 void ChooseNextRequest( | |
| 37 RequestCoordinator::RequestPickedCallback picked_callback, | |
| 38 RequestCoordinator::RequestNotPickedCallback not_picked_callback, | |
| 39 DeviceConditions* device_conditions, | |
| 40 const std::set<int64_t>& disabled_requests); | |
| 41 | |
| 42 private: | |
| 43 // Callback for the GetRequest results to be delivered. | |
| 44 void GetRequestResultCallback( | |
| 45 const std::set<int64_t>& disabled_requests, | |
| 46 RequestQueue::GetRequestsResult result, | |
| 47 std::vector<std::unique_ptr<SavePageRequest>> results); | |
| 48 | |
| 49 // Filter out requests that don't meet the current conditions. For instance, | |
| 50 // if this is a predictive request, and we are not on WiFi, it should be | |
| 51 // ignored this round. | |
| 52 bool RequestConditionsSatisfied(const SavePageRequest* request); | |
| 53 | |
| 54 // Using policies, decide if the new request is preferable to the best we have | |
| 55 // so far. | |
| 56 bool IsNewRequestBetter(const SavePageRequest* oldRequest, | |
| 57 const SavePageRequest* newRequest, | |
| 58 RequestCompareFunction comparator); | |
| 59 | |
| 60 // Is the new request preferable from the retry count first standpoint? | |
| 61 bool RetryCountFirstCompareFunction(const SavePageRequest* left, | |
| 62 const SavePageRequest* right); | |
| 63 | |
| 64 // Is the new request better from the recency first standpoint? | |
| 65 bool RecencyFirstCompareFunction(const SavePageRequest* left, | |
| 66 const SavePageRequest* right); | |
| 67 | |
| 68 // Does the new request have better retry count? | |
| 69 int CompareRetryCount(const SavePageRequest* left, | |
| 70 const SavePageRequest* right); | |
| 71 | |
| 72 // Does the new request have better creation time? | |
| 73 int CompareCreationTime(const SavePageRequest* left, | |
| 74 const SavePageRequest* right); | |
| 75 | |
| 76 // Split all requests into expired ones and still valid ones. Takes ownership | |
| 77 // of the requests, and moves them into either valid or expired requests. | |
| 78 void SplitRequests( | |
| 79 std::vector<std::unique_ptr<SavePageRequest>> requests, | |
| 80 std::vector<std::unique_ptr<SavePageRequest>>* valid_requests, | |
| 81 std::vector<std::unique_ptr<SavePageRequest>>* expired_requests); | |
| 82 | |
| 83 // Callback used after requests get expired. | |
| 84 void OnRequestExpired(std::unique_ptr<UpdateRequestsResult> result); | |
| 85 | |
| 86 // Unowned pointer to the request queue. | |
| 87 RequestQueue* queue_; | |
| 88 // Unowned pointer to the policy object. | |
| 89 OfflinerPolicy* policy_; | |
| 90 // Unowned pointer to the request coordinator. | |
| 91 RequestNotifier* notifier_; | |
| 92 // Unowned pointer to the event logger. | |
| 93 RequestCoordinatorEventLogger* event_logger_; | |
| 94 // Current conditions on the device | |
| 95 std::unique_ptr<DeviceConditions> current_conditions_; | |
| 96 // True if we prefer less-tried requests | |
| 97 bool fewer_retries_better_; | |
| 98 // True if we prefer requests submitted more recently | |
| 99 bool earlier_requests_better_; | |
| 100 // Callback for when we are done picking a request to do next. | |
| 101 RequestCoordinator::RequestPickedCallback picked_callback_; | |
| 102 // Callback for when there are no more reqeusts to pick. | |
| 103 RequestCoordinator::RequestNotPickedCallback not_picked_callback_; | |
| 104 // Allows us to pass a weak pointer to callbacks. | |
| 105 base::WeakPtrFactory<RequestPicker> weak_ptr_factory_; | |
| 106 }; | |
| 107 | |
| 108 } // namespace offline_pages | |
| 109 | |
| 110 #endif // COMPONENTS_OFFLINE_PAGES_BACKGROUND_REQUEST_PICKER_H_ | |
| OLD | NEW |