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 RequestQueueStore; | |
21 | |
22 typedef bool (PickRequestTask::*RequestCompareFunction)( | |
23 const SavePageRequest* left, | |
24 const SavePageRequest* right); | |
25 | |
26 class PickRequestTask : public Task { | |
27 public: | |
28 // Callback to report when a request was available. | |
29 typedef base::Callback<void(const SavePageRequest& request, | |
30 bool cleanup_needed)> | |
31 RequestPickedCallback; | |
32 | |
33 // Callback to report when no request was available. | |
34 typedef base::Callback<void(bool non_user_requests, bool cleanup_needed)> | |
35 RequestNotPickedCallback; | |
36 | |
37 // Callback to report available total and available queued request counts. | |
38 typedef base::Callback<void(size_t, size_t)> RequestCountCallback; | |
39 | |
40 PickRequestTask(RequestQueueStore* store, | |
41 OfflinerPolicy* policy, | |
42 RequestPickedCallback picked_callback, | |
43 RequestNotPickedCallback not_picked_callback, | |
44 RequestCountCallback request_count_callback, | |
45 DeviceConditions& device_conditions, | |
46 const std::set<int64_t>& disabled_requests); | |
47 | |
48 ~PickRequestTask() override; | |
49 | |
50 // TaskQueue::Task implementation, starts the async chain | |
51 void Run() override; | |
52 | |
53 private: | |
54 // Step 1. get the requests | |
55 void GetRequests(); | |
56 | |
57 // Step 2. pick a request that we like best from available requests. | |
58 void Choose(bool get_succeeded, | |
59 std::vector<std::unique_ptr<SavePageRequest>> requests); | |
60 | |
61 // Helper functions. | |
62 | |
63 // Determine if this request has device conditions appropriate for running it. | |
64 bool RequestConditionsSatisfied(const SavePageRequest* request); | |
65 | |
66 // Determine if the new request is preferred under current policies. | |
67 bool IsNewRequestBetter(const SavePageRequest* oldRequest, | |
68 const SavePageRequest* newRequest, | |
69 RequestCompareFunction comparator); | |
70 | |
71 // Returns true if the left hand side is better. | |
72 bool RetryCountFirstCompareFunction(const SavePageRequest* left, | |
73 const SavePageRequest* right); | |
74 | |
75 // Returns true if the left hand side is better. | |
76 bool RecencyFirstCompareFunction(const SavePageRequest* left, | |
77 const SavePageRequest* right); | |
78 | |
79 // Compare left and right side, returning 1 if the left side is better | |
80 // (preferred by policy), 0 if the same, and -1 if the right side is better. | |
81 int CompareRetryCount(const SavePageRequest* left, | |
82 const SavePageRequest* right); | |
83 | |
84 // Compare left and right side, returning 1 if the left side is better | |
85 // (preferred by policy), 0 if the same, and -1 if the right side is better. | |
86 int CompareCreationTime(const SavePageRequest* left, | |
87 const SavePageRequest* right); | |
88 | |
89 // Member variables, all pointers are not owned here. | |
90 RequestQueueStore* store_; | |
91 OfflinerPolicy* policy_; | |
92 RequestPickedCallback picked_callback_; | |
93 RequestNotPickedCallback not_picked_callback_; | |
94 RequestCountCallback request_count_callback_; | |
95 std::unique_ptr<DeviceConditions> device_conditions_; | |
96 const std::set<int64_t>& disabled_requests_; | |
97 // Allows us to pass a weak pointer to callbacks. | |
98 base::WeakPtrFactory<PickRequestTask> weak_ptr_factory_; | |
99 }; | |
100 | |
101 } // namespace offline_pages | |
102 | |
103 #endif // COMPONENTS_OFFLINE_PAGES_BACKGROUND_PICK_REQUEST_TASK_H_ | |
OLD | NEW |