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)( | |
fgorski
2016/07/21 17:01:20
nit: RequestLessThanFunction perhaps?
Pete Williamson
2016/07/21 19:48:46
I'd prefer to leave it as is. It isn't really a l
| |
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 | |
30 // unowned pointer to the request queue. | 58 // unowned pointer to the request queue. |
31 RequestQueue* queue_; | 59 RequestQueue* queue_; |
60 // unowned pointer to the policy object. | |
61 OfflinerPolicy* policy_; | |
62 // Current conditions on the device | |
63 std::unique_ptr<DeviceConditions> current_conditions_; | |
64 // True if we prefer less-tried requests | |
65 bool fewer_retries_better_; | |
66 // True if we prefer requests submitted more recently | |
67 bool earlier_requests_better_; | |
32 // Callback for when we are done picking a request to do next. | 68 // Callback for when we are done picking a request to do next. |
33 RequestCoordinator::RequestPickedCallback picked_callback_; | 69 RequestCoordinator::RequestPickedCallback picked_callback_; |
34 // Callback for when there are no more reqeusts to pick. | 70 // Callback for when there are no more reqeusts to pick. |
35 RequestCoordinator::RequestQueueEmptyCallback empty_callback_; | 71 RequestCoordinator::RequestQueueEmptyCallback empty_callback_; |
36 // Allows us to pass a weak pointer to callbacks. | 72 // Allows us to pass a weak pointer to callbacks. |
37 base::WeakPtrFactory<RequestPicker> weak_ptr_factory_; | 73 base::WeakPtrFactory<RequestPicker> weak_ptr_factory_; |
38 }; | 74 }; |
39 | 75 |
40 } // namespace offline_pages | 76 } // namespace offline_pages |
41 | 77 |
42 #endif // COMPONENTS_OFFLINE_PAGES_BACKGROUND_REQUEST_PICKER_H_ | 78 #endif // COMPONENTS_OFFLINE_PAGES_BACKGROUND_REQUEST_PICKER_H_ |
OLD | NEW |