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_REQUEST_PICKER_H_ | 5 #ifndef COMPONENTS_OFFLINE_PAGES_BACKGROUND_REQUEST_PICKER_H_ |
6 #define COMPONENTS_OFFLINE_PAGES_BACKGROUND_REQUEST_PICKER_H_ | 6 #define COMPONENTS_OFFLINE_PAGES_BACKGROUND_REQUEST_PICKER_H_ |
7 | 7 |
| 8 #include <memory> |
| 9 |
8 #include "base/memory/weak_ptr.h" | 10 #include "base/memory/weak_ptr.h" |
| 11 #include "components/offline_pages/background/device_conditions.h" |
| 12 #include "components/offline_pages/background/offliner_policy.h" |
9 #include "components/offline_pages/background/request_coordinator.h" | 13 #include "components/offline_pages/background/request_coordinator.h" |
10 #include "components/offline_pages/background/request_queue.h" | 14 #include "components/offline_pages/background/request_queue.h" |
11 | 15 |
12 namespace offline_pages { | 16 namespace offline_pages { |
| 17 |
| 18 typedef bool (RequestPicker::*RequestCompareFunction)( |
| 19 const SavePageRequest* left, const SavePageRequest* right); |
| 20 |
13 class RequestPicker { | 21 class RequestPicker { |
14 public: | 22 public: |
15 RequestPicker(RequestQueue* requestQueue); | 23 RequestPicker(RequestQueue* requestQueue, OfflinerPolicy* policy); |
16 | 24 |
17 ~RequestPicker(); | 25 ~RequestPicker(); |
18 | 26 |
19 // Choose which request we should process next based on the current | 27 // Choose which request we should process next based on the current |
20 // conditions, and call back to the RequestCoordinator when we have one. | 28 // conditions, and call back to the RequestCoordinator when we have one. |
21 void ChooseNextRequest( | 29 void ChooseNextRequest( |
22 RequestCoordinator::RequestPickedCallback picked_callback, | 30 RequestCoordinator::RequestPickedCallback picked_callback, |
23 RequestCoordinator::RequestQueueEmptyCallback empty_callback); | 31 RequestCoordinator::RequestQueueEmptyCallback empty_callback, |
| 32 DeviceConditions* device_conditions); |
24 | 33 |
25 private: | 34 private: |
26 // Callback for the GetRequest results to be delivered. | 35 // Callback for the GetRequest results to be delivered. |
27 void GetRequestResultCallback(RequestQueue::GetRequestsResult result, | 36 void GetRequestResultCallback(RequestQueue::GetRequestsResult result, |
28 const std::vector<SavePageRequest>& results); | 37 const std::vector<SavePageRequest>& results); |
29 | 38 |
| 39 // Filter out requests that don't meet the current conditions. For instance, |
| 40 // if this is a predictive request, and we are not on WiFi, it should be |
| 41 // ignored this round. |
| 42 bool RequestConditionsSatisfied(const SavePageRequest& request); |
| 43 |
| 44 // Using policies, decide if the new request is preferable to the best we have |
| 45 // so far. |
| 46 bool IsNewRequestBetter(const SavePageRequest* oldRequest, |
| 47 const SavePageRequest* newRequest, |
| 48 RequestCompareFunction comparator); |
| 49 |
| 50 // Is the new request preferable from the retry count first standpoint? |
| 51 bool RetryCountFirstCompareFunction(const SavePageRequest* left, |
| 52 const SavePageRequest* right); |
| 53 |
| 54 // Is the new request better from the recency first standpoint? |
| 55 bool RecencyFirstCompareFunction(const SavePageRequest* left, |
| 56 const SavePageRequest* right); |
| 57 |
| 58 // Does the new request have better retry count? |
| 59 int CompareRetryCount(const SavePageRequest* left, |
| 60 const SavePageRequest* right); |
| 61 |
| 62 // Does the new request have better creation time? |
| 63 int CompareCreationTime(const SavePageRequest* left, |
| 64 const SavePageRequest* right); |
| 65 |
30 // unowned pointer to the request queue. | 66 // unowned pointer to the request queue. |
31 RequestQueue* queue_; | 67 RequestQueue* queue_; |
| 68 // unowned pointer to the policy object. |
| 69 OfflinerPolicy* policy_; |
| 70 // Current conditions on the device |
| 71 std::unique_ptr<DeviceConditions> current_conditions_; |
| 72 // True if we prefer less-tried requests |
| 73 bool fewer_retries_better_; |
| 74 // True if we prefer requests submitted more recently |
| 75 bool earlier_requests_better_; |
32 // Callback for when we are done picking a request to do next. | 76 // Callback for when we are done picking a request to do next. |
33 RequestCoordinator::RequestPickedCallback picked_callback_; | 77 RequestCoordinator::RequestPickedCallback picked_callback_; |
34 // Callback for when there are no more reqeusts to pick. | 78 // Callback for when there are no more reqeusts to pick. |
35 RequestCoordinator::RequestQueueEmptyCallback empty_callback_; | 79 RequestCoordinator::RequestQueueEmptyCallback empty_callback_; |
36 // Allows us to pass a weak pointer to callbacks. | 80 // Allows us to pass a weak pointer to callbacks. |
37 base::WeakPtrFactory<RequestPicker> weak_ptr_factory_; | 81 base::WeakPtrFactory<RequestPicker> weak_ptr_factory_; |
38 }; | 82 }; |
39 | 83 |
40 } // namespace offline_pages | 84 } // namespace offline_pages |
41 | 85 |
42 #endif // COMPONENTS_OFFLINE_PAGES_BACKGROUND_REQUEST_PICKER_H_ | 86 #endif // COMPONENTS_OFFLINE_PAGES_BACKGROUND_REQUEST_PICKER_H_ |
OLD | NEW |