Chromium Code Reviews| 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 // Nested observer class | |
| 38 class Observer { | |
| 39 public: | |
| 40 virtual void OnAdded(const SavePageRequest& request) = 0; | |
| 41 virtual void OnSucceeded( | |
| 42 const SavePageRequest& request, int64_t offline_id) = 0; | |
|
fgorski
2016/08/10 21:30:44
We should discuss making offline_id == request_id.
Pete Williamson
2016/08/11 00:08:36
TODO added (in the SavePageLater function)
| |
| 43 // TODO(petewil): Consider what the type of faliure_code should be. We | |
| 44 // could use a new enum, re-use the offliner status enum, or just an int. | |
| 45 virtual void OnFailed( | |
| 46 const SavePageRequest& request, int64_t failure_code) = 0; | |
| 47 virtual void OnChanged(const SavePageRequest& request) = 0; | |
|
fgorski
2016/08/10 21:30:44
Did we consider OnUpdated?
Pete Williamson
2016/08/11 00:08:36
We discussed this in person, and nobody had a stro
| |
| 48 }; | |
| 49 | |
| 36 // Callback to report when the processing of a triggered task is complete. | 50 // Callback to report when the processing of a triggered task is complete. |
| 37 typedef base::Callback<void(const SavePageRequest& request)> | 51 typedef base::Callback<void(const SavePageRequest& request)> |
| 38 RequestPickedCallback; | 52 RequestPickedCallback; |
| 39 typedef base::Callback<void()> RequestQueueEmptyCallback; | 53 typedef base::Callback<void()> RequestQueueEmptyCallback; |
| 40 | 54 |
| 41 RequestCoordinator(std::unique_ptr<OfflinerPolicy> policy, | 55 RequestCoordinator(std::unique_ptr<OfflinerPolicy> policy, |
| 42 std::unique_ptr<OfflinerFactory> factory, | 56 std::unique_ptr<OfflinerFactory> factory, |
| 43 std::unique_ptr<RequestQueue> queue, | 57 std::unique_ptr<RequestQueue> queue, |
| 44 std::unique_ptr<Scheduler> scheduler); | 58 std::unique_ptr<Scheduler> scheduler); |
| 45 | 59 |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 86 // is stopped or complete. | 100 // is stopped or complete. |
| 87 void StopProcessing(); | 101 void StopProcessing(); |
| 88 | 102 |
| 89 const Scheduler::TriggerConditions GetTriggerConditionsForUserRequest(); | 103 const Scheduler::TriggerConditions GetTriggerConditionsForUserRequest(); |
| 90 | 104 |
| 91 // A way for tests to set the callback in use when an operation is over. | 105 // A way for tests to set the callback in use when an operation is over. |
| 92 void SetProcessingCallbackForTest(const base::Callback<void(bool)> callback) { | 106 void SetProcessingCallbackForTest(const base::Callback<void(bool)> callback) { |
| 93 scheduler_callback_ = callback; | 107 scheduler_callback_ = callback; |
| 94 } | 108 } |
| 95 | 109 |
| 110 // Observers implementing the RequestCoordinator::Observer interface can | |
| 111 // register here to get notifications of changes to request state. This | |
| 112 // pointer is unowned, and it is the callers responsibility to remove the | |
|
fgorski
2016/08/10 21:30:44
nit: "not owned" sounds better here.
Pete Williamson
2016/08/11 00:08:36
Done.
| |
| 113 // observer before the observer is deleted. Add events after attaching an | |
| 114 // observer will be reported, even if the listener has not called | |
| 115 // GetQueuedRequests yet, so before GetQueuedRequests is called, the listener | |
|
fgorski
2016/08/10 21:30:44
I think I disagree with Dmitry about having this p
Pete Williamson
2016/08/11 00:08:36
Comment updated per our discussion, and moved to t
| |
| 116 // may need to ignore events. | |
| 117 void AddObserver(RequestCoordinator::Observer* observer); | |
| 118 | |
| 119 void RemoveObserver(RequestCoordinator::Observer* observer); | |
| 120 | |
| 96 // Returns the request queue used for requests. Coordinator keeps ownership. | 121 // Returns the request queue used for requests. Coordinator keeps ownership. |
| 97 RequestQueue* queue() { return queue_.get(); } | 122 RequestQueue* queue() { return queue_.get(); } |
| 98 | 123 |
| 99 // Return an unowned pointer to the Scheduler. | 124 // Return an unowned pointer to the Scheduler. |
| 100 Scheduler* scheduler() { return scheduler_.get(); } | 125 Scheduler* scheduler() { return scheduler_.get(); } |
| 101 | 126 |
| 102 // Returns the status of the most recent offlining. | 127 // Returns the status of the most recent offlining. |
| 103 Offliner::RequestStatus last_offlining_status() { | 128 Offliner::RequestStatus last_offlining_status() { |
| 104 return last_offlining_status_; | 129 return last_offlining_status_; |
| 105 } | 130 } |
| (...skipping 23 matching lines...) Expand all Loading... | |
| 129 // Receives the result of add requests to the request queue. | 154 // Receives the result of add requests to the request queue. |
| 130 void AddRequestResultCallback(RequestQueue::AddRequestResult result, | 155 void AddRequestResultCallback(RequestQueue::AddRequestResult result, |
| 131 const SavePageRequest& request); | 156 const SavePageRequest& request); |
| 132 | 157 |
| 133 // Receives the result of update and delete requests to the request queue. | 158 // Receives the result of update and delete requests to the request queue. |
| 134 void UpdateRequestCallback(const ClientId& client_id, | 159 void UpdateRequestCallback(const ClientId& client_id, |
| 135 RequestQueue::UpdateRequestResult result); | 160 RequestQueue::UpdateRequestResult result); |
| 136 | 161 |
| 137 void UpdateMultipleRequestCallback(RequestQueue::UpdateRequestResult result); | 162 void UpdateMultipleRequestCallback(RequestQueue::UpdateRequestResult result); |
| 138 | 163 |
| 164 void NotifyChangedListCallback(const std::vector<int64_t> request_ids, | |
|
fgorski
2016/08/10 21:30:44
is copying intended here?
Pete Williamson
2016/08/11 00:08:36
Done.
| |
| 165 RequestQueue::GetRequestsResult result, | |
| 166 const std::vector<SavePageRequest>& requests); | |
| 167 | |
| 139 // Callback from the request picker when it has chosen our next request. | 168 // Callback from the request picker when it has chosen our next request. |
| 140 void RequestPicked(const SavePageRequest& request); | 169 void RequestPicked(const SavePageRequest& request); |
| 141 | 170 |
| 142 // Callback from the request picker when no more requests are in the queue. | 171 // Callback from the request picker when no more requests are in the queue. |
| 143 void RequestQueueEmpty(); | 172 void RequestQueueEmpty(); |
| 144 | 173 |
| 145 void SendRequestToOffliner(const SavePageRequest& request); | 174 void SendRequestToOffliner(const SavePageRequest& request); |
| 146 | 175 |
| 147 // Called by the offliner when an offlining request is completed. (and by | 176 // Called by the offliner when an offlining request is completed. (and by |
| 148 // tests). | 177 // tests). |
| 149 void OfflinerDoneCallback(const SavePageRequest& request, | 178 void OfflinerDoneCallback(const SavePageRequest& request, |
| 150 Offliner::RequestStatus status); | 179 Offliner::RequestStatus status); |
| 151 | 180 |
| 152 void TryNextRequest(); | 181 void TryNextRequest(); |
| 153 | 182 |
| 183 void NotifyAdded(const SavePageRequest& request); | |
| 184 void NotifySucceeded(const SavePageRequest& request, int64_t offline_id); | |
| 185 void NotifyFailed(const SavePageRequest& request, int64_t failure_code); | |
| 186 void NotifyChanged(const SavePageRequest& request); | |
| 187 | |
| 188 // Sends a changed notification for each request_id in the list. | |
| 189 void SendNotifyChangeForRequestIdList(std::vector<int64_t> request_ids); | |
| 190 | |
| 154 // Returns the appropriate offliner to use, getting a new one from the factory | 191 // Returns the appropriate offliner to use, getting a new one from the factory |
| 155 // if needed. | 192 // if needed. |
| 156 void GetOffliner(); | 193 void GetOffliner(); |
| 157 | 194 |
| 158 void SetOfflinerTimeoutForTest(const base::TimeDelta& timeout) { | 195 void SetOfflinerTimeoutForTest(const base::TimeDelta& timeout) { |
| 159 offliner_timeout_ = timeout; | 196 offliner_timeout_ = timeout; |
| 160 } | 197 } |
| 161 | 198 |
| 162 void SetDeviceConditionsForTest(DeviceConditions& current_conditions) { | 199 void SetDeviceConditionsForTest(DeviceConditions& current_conditions) { |
| 163 current_conditions_.reset(new DeviceConditions(current_conditions)); | 200 current_conditions_.reset(new DeviceConditions(current_conditions)); |
| 164 } | 201 } |
| 165 | 202 |
| 166 friend class RequestCoordinatorTest; | 203 friend class RequestCoordinatorTest; |
| 167 | 204 |
| 168 // The offliner can only handle one request at a time - if the offliner is | 205 // 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 | 206 // busy, prevent other requests. This flag marks whether the offliner is in |
| 170 // use. | 207 // use. |
| 171 bool is_busy_; | 208 bool is_busy_; |
| 172 // True if the current request has been canceled. | 209 // True if the current request has been canceled. |
| 173 bool is_canceled_; | 210 bool is_canceled_; |
| 174 // Unowned pointer to the current offliner, if any. | 211 // Unowned pointer to the current offliner, if any. |
| 175 Offliner* offliner_; | 212 Offliner* offliner_; |
| 176 base::Time operation_start_time_; | 213 base::Time operation_start_time_; |
| 214 // The observers. | |
| 215 base::ObserverList<Observer> observers_; | |
| 177 // Last known conditions for network, battery | 216 // Last known conditions for network, battery |
| 178 std::unique_ptr<DeviceConditions> current_conditions_; | 217 std::unique_ptr<DeviceConditions> current_conditions_; |
| 179 // RequestCoordinator takes over ownership of the policy | 218 // RequestCoordinator takes over ownership of the policy |
| 180 std::unique_ptr<OfflinerPolicy> policy_; | 219 std::unique_ptr<OfflinerPolicy> policy_; |
| 181 // OfflinerFactory. Used to create offline pages. Owned. | 220 // OfflinerFactory. Used to create offline pages. Owned. |
| 182 std::unique_ptr<OfflinerFactory> factory_; | 221 std::unique_ptr<OfflinerFactory> factory_; |
| 183 // RequestQueue. Used to store incoming requests. Owned. | 222 // RequestQueue. Used to store incoming requests. Owned. |
| 184 std::unique_ptr<RequestQueue> queue_; | 223 std::unique_ptr<RequestQueue> queue_; |
| 185 // Scheduler. Used to request a callback when network is available. Owned. | 224 // Scheduler. Used to request a callback when network is available. Owned. |
| 186 std::unique_ptr<Scheduler> scheduler_; | 225 std::unique_ptr<Scheduler> scheduler_; |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 200 base::TimeDelta offliner_timeout_; | 239 base::TimeDelta offliner_timeout_; |
| 201 // Allows us to pass a weak pointer to callbacks. | 240 // Allows us to pass a weak pointer to callbacks. |
| 202 base::WeakPtrFactory<RequestCoordinator> weak_ptr_factory_; | 241 base::WeakPtrFactory<RequestCoordinator> weak_ptr_factory_; |
| 203 | 242 |
| 204 DISALLOW_COPY_AND_ASSIGN(RequestCoordinator); | 243 DISALLOW_COPY_AND_ASSIGN(RequestCoordinator); |
| 205 }; | 244 }; |
| 206 | 245 |
| 207 } // namespace offline_pages | 246 } // namespace offline_pages |
| 208 | 247 |
| 209 #endif // COMPONENTS_OFFLINE_PAGES_BACKGROUND_REQUEST_COORDINATOR_H_ | 248 #endif // COMPONENTS_OFFLINE_PAGES_BACKGROUND_REQUEST_COORDINATOR_H_ |
| OLD | NEW |