Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(100)

Side by Side Diff: components/offline_pages/background/request_coordinator.h

Issue 2219393004: Adds an observer for the request coordinator. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@resumeAPI
Patch Set: Second batch of FGorski CR fixes Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 };
46
47 // Nested observer class. To make sure that no events are missed, the client
48 // code should first register for notifications, then GetAllRequests, and
49 // ignore all events before the return from GetAllRequests, and consume events
50 // after the return callback from GetAlLRequests.
fgorski 2016/08/11 17:03:58 AlL -> All nit: I believe we use |GetAllRequests|
Pete Williamson 2016/08/13 03:20:34 Done.
51 class Observer {
52 public:
53 virtual void OnAdded(const SavePageRequest& request) = 0;
54 virtual void OnSucceeded(const SavePageRequest& request,
55 int64_t offline_id) = 0;
Dmitry Titov 2016/08/11 03:41:55 Once we agreed the id is inherited from request to
Pete Williamson 2016/08/13 03:20:34 Done.
56 virtual void OnFailed(const SavePageRequest& request,
57 SavePageStatus status) = 0;
58 virtual void OnChanged(const SavePageRequest& request) = 0;
59 };
60
36 // Callback to report when the processing of a triggered task is complete. 61 // Callback to report when the processing of a triggered task is complete.
37 typedef base::Callback<void(const SavePageRequest& request)> 62 typedef base::Callback<void(const SavePageRequest& request)>
38 RequestPickedCallback; 63 RequestPickedCallback;
39 typedef base::Callback<void()> RequestQueueEmptyCallback; 64 typedef base::Callback<void()> RequestQueueEmptyCallback;
40 65
41 RequestCoordinator(std::unique_ptr<OfflinerPolicy> policy, 66 RequestCoordinator(std::unique_ptr<OfflinerPolicy> policy,
42 std::unique_ptr<OfflinerFactory> factory, 67 std::unique_ptr<OfflinerFactory> factory,
43 std::unique_ptr<RequestQueue> queue, 68 std::unique_ptr<RequestQueue> queue,
44 std::unique_ptr<Scheduler> scheduler); 69 std::unique_ptr<Scheduler> scheduler);
45 70
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
86 // is stopped or complete. 111 // is stopped or complete.
87 void StopProcessing(); 112 void StopProcessing();
88 113
89 const Scheduler::TriggerConditions GetTriggerConditionsForUserRequest(); 114 const Scheduler::TriggerConditions GetTriggerConditionsForUserRequest();
90 115
91 // A way for tests to set the callback in use when an operation is over. 116 // A way for tests to set the callback in use when an operation is over.
92 void SetProcessingCallbackForTest(const base::Callback<void(bool)> callback) { 117 void SetProcessingCallbackForTest(const base::Callback<void(bool)> callback) {
93 scheduler_callback_ = callback; 118 scheduler_callback_ = callback;
94 } 119 }
95 120
121 // Observers implementing the RequestCoordinator::Observer interface can
122 // register here to get notifications of changes to request state. This
123 // pointer is not owned, and it is the callers responsibility to remove the
124 // observer before the observer is deleted.
125 void AddObserver(RequestCoordinator::Observer* observer);
126
127 void RemoveObserver(RequestCoordinator::Observer* observer);
128
96 // Returns the request queue used for requests. Coordinator keeps ownership. 129 // Returns the request queue used for requests. Coordinator keeps ownership.
97 RequestQueue* queue() { return queue_.get(); } 130 RequestQueue* queue() { return queue_.get(); }
98 131
99 // Return an unowned pointer to the Scheduler. 132 // Return an unowned pointer to the Scheduler.
100 Scheduler* scheduler() { return scheduler_.get(); } 133 Scheduler* scheduler() { return scheduler_.get(); }
101 134
102 // Returns the status of the most recent offlining. 135 // Returns the status of the most recent offlining.
103 Offliner::RequestStatus last_offlining_status() { 136 Offliner::RequestStatus last_offlining_status() {
104 return last_offlining_status_; 137 return last_offlining_status_;
105 } 138 }
(...skipping 21 matching lines...) Expand all
127 const std::vector<SavePageRequest>& requests); 160 const std::vector<SavePageRequest>& requests);
128 161
129 // Receives the result of add requests to the request queue. 162 // Receives the result of add requests to the request queue.
130 void AddRequestResultCallback(RequestQueue::AddRequestResult result, 163 void AddRequestResultCallback(RequestQueue::AddRequestResult result,
131 const SavePageRequest& request); 164 const SavePageRequest& request);
132 165
133 // Receives the result of update and delete requests to the request queue. 166 // Receives the result of update and delete requests to the request queue.
134 void UpdateRequestCallback(const ClientId& client_id, 167 void UpdateRequestCallback(const ClientId& client_id,
135 RequestQueue::UpdateRequestResult result); 168 RequestQueue::UpdateRequestResult result);
136 169
137 void UpdateMultipleRequestCallback(RequestQueue::UpdateRequestResult result); 170 void UpdateMultipleRequestCallback(const std::vector<int64_t>& request_ids,
171 RequestQueue::UpdateRequestResult result);
172
173 void NotifyChangedListCallback(const std::vector<int64_t>& request_ids,
174 RequestQueue::GetRequestsResult result,
175 const std::vector<SavePageRequest>& requests);
138 176
139 // Callback from the request picker when it has chosen our next request. 177 // Callback from the request picker when it has chosen our next request.
140 void RequestPicked(const SavePageRequest& request); 178 void RequestPicked(const SavePageRequest& request);
141 179
142 // Callback from the request picker when no more requests are in the queue. 180 // Callback from the request picker when no more requests are in the queue.
143 void RequestQueueEmpty(); 181 void RequestQueueEmpty();
144 182
145 void SendRequestToOffliner(const SavePageRequest& request); 183 void SendRequestToOffliner(const SavePageRequest& request);
146 184
147 // Called by the offliner when an offlining request is completed. (and by 185 // Called by the offliner when an offlining request is completed. (and by
148 // tests). 186 // tests).
149 void OfflinerDoneCallback(const SavePageRequest& request, 187 void OfflinerDoneCallback(const SavePageRequest& request,
150 Offliner::RequestStatus status); 188 Offliner::RequestStatus status);
151 189
152 void TryNextRequest(); 190 void TryNextRequest();
153 191
192 void NotifyAdded(const SavePageRequest& request);
193 void NotifySucceeded(const SavePageRequest& request, int64_t offline_id);
194 void NotifyFailed(const SavePageRequest& request, SavePageStatus status);
195 void NotifyChanged(const SavePageRequest& request);
196
197 // Sends a changed notification for each request_id in the list.
198 void SendNotifyChangeForRequestIdList(std::vector<int64_t> request_ids);
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
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_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698