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_COORDINATOR_H_ | 5 #ifndef COMPONENTS_OFFLINE_PAGES_BACKGROUND_REQUEST_COORDINATOR_H_ |
6 #define COMPONENTS_OFFLINE_PAGES_BACKGROUND_REQUEST_COORDINATOR_H_ | 6 #define COMPONENTS_OFFLINE_PAGES_BACKGROUND_REQUEST_COORDINATOR_H_ |
7 | 7 |
8 #include <memory> | 8 #include <memory> |
9 | 9 |
10 #include "base/callback.h" | 10 #include "base/callback.h" |
11 #include "base/macros.h" | 11 #include "base/macros.h" |
12 #include "base/memory/weak_ptr.h" | 12 #include "base/memory/weak_ptr.h" |
| 13 #include "base/observer_list.h" |
13 #include "base/time/time.h" | 14 #include "base/time/time.h" |
14 #include "base/timer/timer.h" | 15 #include "base/timer/timer.h" |
15 #include "components/keyed_service/core/keyed_service.h" | 16 #include "components/keyed_service/core/keyed_service.h" |
16 #include "components/offline_pages/background/device_conditions.h" | 17 #include "components/offline_pages/background/device_conditions.h" |
17 #include "components/offline_pages/background/offliner.h" | 18 #include "components/offline_pages/background/offliner.h" |
18 #include "components/offline_pages/background/request_coordinator_event_logger.h
" | 19 #include "components/offline_pages/background/request_coordinator_event_logger.h
" |
19 #include "components/offline_pages/background/request_queue.h" | 20 #include "components/offline_pages/background/request_queue.h" |
20 #include "components/offline_pages/background/scheduler.h" | 21 #include "components/offline_pages/background/scheduler.h" |
21 #include "url/gurl.h" | 22 #include "url/gurl.h" |
22 | 23 |
23 namespace offline_pages { | 24 namespace offline_pages { |
24 | 25 |
25 struct ClientId; | 26 struct ClientId; |
26 class OfflinerPolicy; | 27 class OfflinerPolicy; |
27 class OfflinerFactory; | 28 class OfflinerFactory; |
28 class Offliner; | 29 class Offliner; |
29 class RequestPicker; | 30 class RequestPicker; |
30 class SavePageRequest; | 31 class SavePageRequest; |
31 class Scheduler; | 32 class Scheduler; |
32 | 33 |
33 // Coordinates queueing and processing save page later requests. | 34 // Coordinates queueing and processing save page later requests. |
34 class RequestCoordinator : public KeyedService { | 35 class RequestCoordinator : public KeyedService { |
35 public: | 36 public: |
| 37 // Status to return for failed notifications. |
| 38 enum class SavePageStatus { |
| 39 SUCCESS, |
| 40 PRERENDER_FAILURE, |
| 41 FOREGROUND_CANCELED, |
| 42 SAVE_FAILED, |
| 43 EXPIRED, |
| 44 RETRY_COUNT_EXCEEDED, |
| 45 REMOVED_BY_CLIENT, |
| 46 }; |
| 47 |
| 48 // Nested observer class. To make sure that no events are missed, the client |
| 49 // code should first register for notifications, then |GetAllRequests|, and |
| 50 // ignore all events before the return from |GetAllRequests|, and consume |
| 51 // events after the return callback from |GetAllRequests|. |
| 52 class Observer { |
| 53 public: |
| 54 virtual void OnAdded(const SavePageRequest& request) = 0; |
| 55 virtual void OnSucceeded(const SavePageRequest& request) = 0; |
| 56 virtual void OnFailed(const SavePageRequest& request, |
| 57 SavePageStatus status) = 0; |
| 58 virtual void OnChanged(const SavePageRequest& request) = 0; |
| 59 virtual void OnRemoved(const SavePageRequest& request, |
| 60 SavePageStatus status) = 0; |
| 61 // TODO(petewil): Merge OnSucceeded, OnFailed, and OnRemoved into a single |
| 62 // call. |
| 63 }; |
| 64 |
36 // Callback to report when the processing of a triggered task is complete. | 65 // Callback to report when the processing of a triggered task is complete. |
37 typedef base::Callback<void(const SavePageRequest& request)> | 66 typedef base::Callback<void(const SavePageRequest& request)> |
38 RequestPickedCallback; | 67 RequestPickedCallback; |
39 typedef base::Callback<void()> RequestQueueEmptyCallback; | 68 typedef base::Callback<void()> RequestQueueEmptyCallback; |
40 | 69 |
41 RequestCoordinator(std::unique_ptr<OfflinerPolicy> policy, | 70 RequestCoordinator(std::unique_ptr<OfflinerPolicy> policy, |
42 std::unique_ptr<OfflinerFactory> factory, | 71 std::unique_ptr<OfflinerFactory> factory, |
43 std::unique_ptr<RequestQueue> queue, | 72 std::unique_ptr<RequestQueue> queue, |
44 std::unique_ptr<Scheduler> scheduler); | 73 std::unique_ptr<Scheduler> scheduler); |
45 | 74 |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
84 // is stopped or complete. | 113 // is stopped or complete. |
85 void StopProcessing(); | 114 void StopProcessing(); |
86 | 115 |
87 const Scheduler::TriggerConditions GetTriggerConditionsForUserRequest(); | 116 const Scheduler::TriggerConditions GetTriggerConditionsForUserRequest(); |
88 | 117 |
89 // A way for tests to set the callback in use when an operation is over. | 118 // A way for tests to set the callback in use when an operation is over. |
90 void SetProcessingCallbackForTest(const base::Callback<void(bool)> callback) { | 119 void SetProcessingCallbackForTest(const base::Callback<void(bool)> callback) { |
91 scheduler_callback_ = callback; | 120 scheduler_callback_ = callback; |
92 } | 121 } |
93 | 122 |
| 123 // Observers implementing the RequestCoordinator::Observer interface can |
| 124 // register here to get notifications of changes to request state. This |
| 125 // pointer is not owned, and it is the callers responsibility to remove the |
| 126 // observer before the observer is deleted. |
| 127 void AddObserver(RequestCoordinator::Observer* observer); |
| 128 |
| 129 void RemoveObserver(RequestCoordinator::Observer* observer); |
| 130 |
94 // Returns the request queue used for requests. Coordinator keeps ownership. | 131 // Returns the request queue used for requests. Coordinator keeps ownership. |
95 RequestQueue* queue() { return queue_.get(); } | 132 RequestQueue* queue() { return queue_.get(); } |
96 | 133 |
97 // Return an unowned pointer to the Scheduler. | 134 // Return an unowned pointer to the Scheduler. |
98 Scheduler* scheduler() { return scheduler_.get(); } | 135 Scheduler* scheduler() { return scheduler_.get(); } |
99 | 136 |
100 // Returns the status of the most recent offlining. | 137 // Returns the status of the most recent offlining. |
101 Offliner::RequestStatus last_offlining_status() { | 138 Offliner::RequestStatus last_offlining_status() { |
102 return last_offlining_status_; | 139 return last_offlining_status_; |
103 } | 140 } |
(...skipping 20 matching lines...) Expand all Loading... |
124 const std::vector<SavePageRequest>& requests); | 161 const std::vector<SavePageRequest>& requests); |
125 | 162 |
126 // Receives the result of add requests to the request queue. | 163 // Receives the result of add requests to the request queue. |
127 void AddRequestResultCallback(RequestQueue::AddRequestResult result, | 164 void AddRequestResultCallback(RequestQueue::AddRequestResult result, |
128 const SavePageRequest& request); | 165 const SavePageRequest& request); |
129 | 166 |
130 // Receives the result of update and delete requests to the request queue. | 167 // Receives the result of update and delete requests to the request queue. |
131 void UpdateRequestCallback(const ClientId& client_id, | 168 void UpdateRequestCallback(const ClientId& client_id, |
132 RequestQueue::UpdateRequestResult result); | 169 RequestQueue::UpdateRequestResult result); |
133 | 170 |
134 void UpdateMultipleRequestCallback(RequestQueue::UpdateRequestResult result); | 171 void UpdateMultipleRequestsCallback( |
| 172 const RequestQueue::UpdateMultipleRequestResults& result, |
| 173 const std::vector<SavePageRequest>& requests); |
135 | 174 |
136 void RemoveRequestsCallback( | 175 void RemoveRequestsCallback( |
137 const RequestQueue::UpdateMultipleRequestResults& results); | 176 const RequestQueue::UpdateMultipleRequestResults& results, |
| 177 const std::vector<SavePageRequest>& requests); |
138 | 178 |
139 // Callback from the request picker when it has chosen our next request. | 179 // Callback from the request picker when it has chosen our next request. |
140 void RequestPicked(const SavePageRequest& request); | 180 void RequestPicked(const SavePageRequest& request); |
141 | 181 |
142 // Callback from the request picker when no more requests are in the queue. | 182 // Callback from the request picker when no more requests are in the queue. |
143 void RequestQueueEmpty(); | 183 void RequestQueueEmpty(); |
144 | 184 |
145 void SendRequestToOffliner(const SavePageRequest& request); | 185 void SendRequestToOffliner(const SavePageRequest& request); |
146 | 186 |
147 // Called by the offliner when an offlining request is completed. (and by | 187 // Called by the offliner when an offlining request is completed. (and by |
148 // tests). | 188 // tests). |
149 void OfflinerDoneCallback(const SavePageRequest& request, | 189 void OfflinerDoneCallback(const SavePageRequest& request, |
150 Offliner::RequestStatus status); | 190 Offliner::RequestStatus status); |
151 | 191 |
152 void TryNextRequest(); | 192 void TryNextRequest(); |
153 | 193 |
| 194 void NotifyAdded(const SavePageRequest& request); |
| 195 void NotifySucceeded(const SavePageRequest& request); |
| 196 void NotifyFailed(const SavePageRequest& request, SavePageStatus status); |
| 197 void NotifyChanged(const SavePageRequest& request); |
| 198 void NotifyRemoved(const SavePageRequest& request); |
| 199 |
154 // Returns the appropriate offliner to use, getting a new one from the factory | 200 // Returns the appropriate offliner to use, getting a new one from the factory |
155 // if needed. | 201 // if needed. |
156 void GetOffliner(); | 202 void GetOffliner(); |
157 | 203 |
158 void SetOfflinerTimeoutForTest(const base::TimeDelta& timeout) { | 204 void SetOfflinerTimeoutForTest(const base::TimeDelta& timeout) { |
159 offliner_timeout_ = timeout; | 205 offliner_timeout_ = timeout; |
160 } | 206 } |
161 | 207 |
162 void SetDeviceConditionsForTest(DeviceConditions& current_conditions) { | 208 void SetDeviceConditionsForTest(DeviceConditions& current_conditions) { |
163 current_conditions_.reset(new DeviceConditions(current_conditions)); | 209 current_conditions_.reset(new DeviceConditions(current_conditions)); |
164 } | 210 } |
165 | 211 |
166 friend class RequestCoordinatorTest; | 212 friend class RequestCoordinatorTest; |
167 | 213 |
168 // The offliner can only handle one request at a time - if the offliner is | 214 // The offliner can only handle one request at a time - if the offliner is |
169 // busy, prevent other requests. This flag marks whether the offliner is in | 215 // busy, prevent other requests. This flag marks whether the offliner is in |
170 // use. | 216 // use. |
171 bool is_busy_; | 217 bool is_busy_; |
172 // True if the current request has been canceled. | 218 // True if the current request has been canceled. |
173 bool is_canceled_; | 219 bool is_canceled_; |
174 // Unowned pointer to the current offliner, if any. | 220 // Unowned pointer to the current offliner, if any. |
175 Offliner* offliner_; | 221 Offliner* offliner_; |
176 base::Time operation_start_time_; | 222 base::Time operation_start_time_; |
| 223 // The observers. |
| 224 base::ObserverList<Observer> observers_; |
177 // Last known conditions for network, battery | 225 // Last known conditions for network, battery |
178 std::unique_ptr<DeviceConditions> current_conditions_; | 226 std::unique_ptr<DeviceConditions> current_conditions_; |
179 // RequestCoordinator takes over ownership of the policy | 227 // RequestCoordinator takes over ownership of the policy |
180 std::unique_ptr<OfflinerPolicy> policy_; | 228 std::unique_ptr<OfflinerPolicy> policy_; |
181 // OfflinerFactory. Used to create offline pages. Owned. | 229 // OfflinerFactory. Used to create offline pages. Owned. |
182 std::unique_ptr<OfflinerFactory> factory_; | 230 std::unique_ptr<OfflinerFactory> factory_; |
183 // RequestQueue. Used to store incoming requests. Owned. | 231 // RequestQueue. Used to store incoming requests. Owned. |
184 std::unique_ptr<RequestQueue> queue_; | 232 std::unique_ptr<RequestQueue> queue_; |
185 // Scheduler. Used to request a callback when network is available. Owned. | 233 // Scheduler. Used to request a callback when network is available. Owned. |
186 std::unique_ptr<Scheduler> scheduler_; | 234 std::unique_ptr<Scheduler> scheduler_; |
(...skipping 13 matching lines...) Expand all Loading... |
200 base::TimeDelta offliner_timeout_; | 248 base::TimeDelta offliner_timeout_; |
201 // Allows us to pass a weak pointer to callbacks. | 249 // Allows us to pass a weak pointer to callbacks. |
202 base::WeakPtrFactory<RequestCoordinator> weak_ptr_factory_; | 250 base::WeakPtrFactory<RequestCoordinator> weak_ptr_factory_; |
203 | 251 |
204 DISALLOW_COPY_AND_ASSIGN(RequestCoordinator); | 252 DISALLOW_COPY_AND_ASSIGN(RequestCoordinator); |
205 }; | 253 }; |
206 | 254 |
207 } // namespace offline_pages | 255 } // namespace offline_pages |
208 | 256 |
209 #endif // COMPONENTS_OFFLINE_PAGES_BACKGROUND_REQUEST_COORDINATOR_H_ | 257 #endif // COMPONENTS_OFFLINE_PAGES_BACKGROUND_REQUEST_COORDINATOR_H_ |
OLD | NEW |