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