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_REQUEST_QUEUE_H_ | |
6 #define COMPONENTS_OFFLINE_PAGES_BACKGROUND_REQUEST_QUEUE_H_ | |
7 | |
8 #include <stdint.h> | |
9 | |
10 #include <memory> | |
11 #include <set> | |
12 #include <string> | |
13 #include <vector> | |
14 | |
15 #include "base/callback.h" | |
16 #include "base/macros.h" | |
17 #include "base/memory/weak_ptr.h" | |
18 #include "components/offline_pages/background/device_conditions.h" | |
19 #include "components/offline_pages/background/pick_request_task.h" | |
20 #include "components/offline_pages/background/pick_request_task_factory.h" | |
21 #include "components/offline_pages/background/request_queue_results.h" | |
22 #include "components/offline_pages/background/save_page_request.h" | |
23 #include "components/offline_pages/core/task_queue.h" | |
24 #include "components/offline_pages/offline_page_item.h" | |
25 #include "components/offline_pages/offline_store_types.h" | |
26 | |
27 namespace offline_pages { | |
28 | |
29 class RequestQueueStore; | |
30 class PickRequestTaskFactory; | |
31 | |
32 // Class responsible for managing save page requests. | |
33 class RequestQueue { | |
34 public: | |
35 | |
36 // Callback used for |GetRequests|. | |
37 typedef base::Callback<void(GetRequestsResult, | |
38 std::vector<std::unique_ptr<SavePageRequest>>)> | |
39 GetRequestsCallback; | |
40 | |
41 // Callback used for |AddRequest|. | |
42 typedef base::Callback<void(AddRequestResult, const SavePageRequest& request)> | |
43 AddRequestCallback; | |
44 | |
45 // Callback used by |ChangeRequestsState|. | |
46 typedef base::Callback<void(std::unique_ptr<UpdateRequestsResult>)> | |
47 UpdateCallback; | |
48 | |
49 // Callback used by |UdpateRequest|. | |
50 typedef base::Callback<void(UpdateRequestResult)> UpdateRequestCallback; | |
51 | |
52 explicit RequestQueue(std::unique_ptr<RequestQueueStore> store); | |
53 ~RequestQueue(); | |
54 | |
55 // Gets all of the active requests from the store. Calling this method may | |
56 // schedule purging of the request queue. | |
57 void GetRequests(const GetRequestsCallback& callback); | |
58 | |
59 // Adds |request| to the request queue. Result is returned through |callback|. | |
60 // In case adding the request violates policy, the result will fail with | |
61 // appropriate result. Callback will also return a copy of a request with all | |
62 // fields set. | |
63 void AddRequest(const SavePageRequest& request, | |
64 const AddRequestCallback& callback); | |
65 | |
66 // Removes the requests matching the |request_ids|. Result is returned through | |
67 // |callback|. If a request id cannot be removed, this will still remove the | |
68 // others. | |
69 void RemoveRequests(const std::vector<int64_t>& request_ids, | |
70 const UpdateCallback& callback); | |
71 | |
72 // Changes the state to |new_state| for requests matching the | |
73 // |request_ids|. Results are returned through |callback|. | |
74 void ChangeRequestsState(const std::vector<int64_t>& request_ids, | |
75 const SavePageRequest::RequestState new_state, | |
76 const UpdateCallback& callback); | |
77 | |
78 // Marks attempt with |request_id| as started. Results are returned through | |
79 // |callback|. | |
80 void MarkAttemptStarted(int64_t request_id, const UpdateCallback& callback); | |
81 | |
82 // Marks attempt with |request_id| as aborted. Results are returned through | |
83 // |callback|. | |
84 void MarkAttemptAborted(int64_t request_id, const UpdateCallback& callback); | |
85 | |
86 // Marks attempt with |request_id| as completed. The attempt may have | |
87 // completed with either success or failure (not denoted here). Results | |
88 // are returned through |callback|. | |
89 void MarkAttemptCompleted(int64_t request_id, const UpdateCallback& callback); | |
90 | |
91 // Make a task to pick the next request, and report our choice to the | |
92 // callbacks. | |
93 void PickNextRequest( | |
94 PickRequestTask::RequestPickedCallback picked_callback, | |
95 PickRequestTask::RequestNotPickedCallback not_picked_callback, | |
96 PickRequestTask::RequestCountCallback request_count_callback, | |
97 DeviceConditions& conditions, | |
98 std::set<int64_t>& disabled_requests); | |
99 | |
100 // Takes ownership of the factory. We use a setter to allow users of the | |
101 // request queue to not need a PickerFactory to create it, since we have lots | |
102 // of code using the request queue. The request coordinator will set a | |
103 // factory before calling PickNextRequest. | |
104 void SetPickerFactory(std::unique_ptr<PickRequestTaskFactory> factory) { | |
105 picker_factory_ = std::move(factory); | |
106 } | |
107 | |
108 private: | |
109 // Callback used by |PurgeRequests|. | |
110 typedef base::Callback<void(UpdateRequestResult, | |
111 int /* removed requests count */)> | |
112 PurgeRequestsCallback; | |
113 | |
114 // Purges the queue, removing the requests that are no longer relevant, e.g. | |
115 // expired request. Result is returned through |callback| carries the number | |
116 // of removed requests. | |
117 void PurgeRequests(const PurgeRequestsCallback& callback); | |
118 | |
119 std::unique_ptr<RequestQueueStore> store_; | |
120 | |
121 // Task queue to serialize store access. | |
122 TaskQueue task_queue_; | |
123 | |
124 // Builds PickRequestTask objects. | |
125 std::unique_ptr<PickRequestTaskFactory> picker_factory_; | |
126 | |
127 // Allows us to pass a weak pointer to callbacks. | |
128 base::WeakPtrFactory<RequestQueue> weak_ptr_factory_; | |
129 | |
130 DISALLOW_COPY_AND_ASSIGN(RequestQueue); | |
131 }; | |
132 | |
133 } // namespace offline_pages | |
134 | |
135 #endif // COMPONENTS_OFFLINE_PAGES_BACKGROUND_REQUEST_QUEUE_H_ | |
OLD | NEW |