Chromium Code Reviews| 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 // This is used to compare requests to find the more desirable request under | |
| 18 // current policies. | |
| 19 enum ComparisonResult { | |
| 20 Worse = -1, Same = 0, Better = 1 | |
|
fgorski
2016/07/20 16:23:31
nit: there are some indentation issues here.
Pete Williamson
2016/07/20 19:50:44
Done. (danger of cut and paste coding, their stand
| |
| 21 }; | |
| 22 | |
| 23 | |
| 13 class RequestPicker { | 24 class RequestPicker { |
| 14 public: | 25 public: |
| 15 RequestPicker(RequestQueue* requestQueue); | 26 RequestPicker(RequestQueue* requestQueue, OfflinerPolicy* policy); |
| 16 | 27 |
| 17 ~RequestPicker(); | 28 ~RequestPicker(); |
| 18 | 29 |
| 19 // Choose which request we should process next based on the current | 30 // Choose which request we should process next based on the current |
| 20 // conditions, and call back to the RequestCoordinator when we have one. | 31 // conditions, and call back to the RequestCoordinator when we have one. |
| 21 void ChooseNextRequest( | 32 void ChooseNextRequest( |
| 22 RequestCoordinator::RequestPickedCallback picked_callback, | 33 RequestCoordinator::RequestPickedCallback picked_callback, |
| 23 RequestCoordinator::RequestQueueEmptyCallback empty_callback); | 34 RequestCoordinator::RequestQueueEmptyCallback empty_callback, |
| 35 DeviceConditions* device_conditions); | |
| 24 | 36 |
| 25 private: | 37 private: |
| 26 // Callback for the GetRequest results to be delivered. | 38 // Callback for the GetRequest results to be delivered. |
| 27 void GetRequestResultCallback(RequestQueue::GetRequestsResult result, | 39 void GetRequestResultCallback(RequestQueue::GetRequestsResult result, |
| 28 const std::vector<SavePageRequest>& results); | 40 const std::vector<SavePageRequest>& results); |
| 29 | 41 |
| 42 // Filter out requests that don't meet the current conditions. For instance, | |
| 43 // if this is a predictive request, and we are not on WiFi, it should be | |
| 44 // ignored this round. | |
| 45 bool RequestConditionsSatisfied(const SavePageRequest& request); | |
| 46 | |
| 47 // Using policies, decide if the new request is preferable to the best we have | |
| 48 // so far. | |
| 49 bool IsNewRequestBetter(const SavePageRequest* oldRequest, | |
| 50 const SavePageRequest* newRequest); | |
| 51 | |
| 52 // Is the new request preferable from the retry count standpoint? | |
| 53 ComparisonResult IsNewRequestRetryCountBetter( | |
| 54 const SavePageRequest* oldRequest, const SavePageRequest* newRequest); | |
| 55 | |
| 56 // Is the new request better from the recency standpoint? | |
| 57 ComparisonResult | |
| 58 IsNewRequestRecencyBetter(const SavePageRequest* oldRequest, | |
| 59 const SavePageRequest* newRequest); | |
| 60 | |
| 30 // unowned pointer to the request queue. | 61 // unowned pointer to the request queue. |
| 31 RequestQueue* queue_; | 62 RequestQueue* queue_; |
| 63 // unowned pointer to the policy object. | |
| 64 OfflinerPolicy* policy_; | |
| 65 // Current conditions on the device | |
| 66 std::unique_ptr<DeviceConditions> current_conditions_; | |
| 32 // Callback for when we are done picking a request to do next. | 67 // Callback for when we are done picking a request to do next. |
| 33 RequestCoordinator::RequestPickedCallback picked_callback_; | 68 RequestCoordinator::RequestPickedCallback picked_callback_; |
| 34 // Callback for when there are no more reqeusts to pick. | 69 // Callback for when there are no more reqeusts to pick. |
| 35 RequestCoordinator::RequestQueueEmptyCallback empty_callback_; | 70 RequestCoordinator::RequestQueueEmptyCallback empty_callback_; |
| 36 // Allows us to pass a weak pointer to callbacks. | 71 // Allows us to pass a weak pointer to callbacks. |
| 37 base::WeakPtrFactory<RequestPicker> weak_ptr_factory_; | 72 base::WeakPtrFactory<RequestPicker> weak_ptr_factory_; |
| 38 }; | 73 }; |
| 39 | 74 |
| 40 } // namespace offline_pages | 75 } // namespace offline_pages |
| 41 | 76 |
| 42 #endif // COMPONENTS_OFFLINE_PAGES_BACKGROUND_REQUEST_PICKER_H_ | 77 #endif // COMPONENTS_OFFLINE_PAGES_BACKGROUND_REQUEST_PICKER_H_ |
| OLD | NEW |