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" |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 46 class Observer { | 46 class Observer { |
| 47 public: | 47 public: |
| 48 virtual ~Observer() = default; | 48 virtual ~Observer() = default; |
| 49 | 49 |
| 50 virtual void OnAdded(const SavePageRequest& request) = 0; | 50 virtual void OnAdded(const SavePageRequest& request) = 0; |
| 51 virtual void OnCompleted(const SavePageRequest& request, | 51 virtual void OnCompleted(const SavePageRequest& request, |
| 52 RequestNotifier::SavePageStatus status) = 0; | 52 RequestNotifier::SavePageStatus status) = 0; |
| 53 virtual void OnChanged(const SavePageRequest& request) = 0; | 53 virtual void OnChanged(const SavePageRequest& request) = 0; |
| 54 }; | 54 }; |
| 55 | 55 |
| 56 // Callback to report when the processing of a triggered task is complete. | 56 // Callback to report when no request was available. |
|
dougarnett
2016/09/01 18:24:13
Could copy this comment to above RequestNotPickedC
Pete Williamson
2016/09/01 21:56:08
Done.
| |
| 57 typedef base::Callback<void(const SavePageRequest& request)> | 57 typedef base::Callback<void(const SavePageRequest& request)> |
| 58 RequestPickedCallback; | 58 RequestPickedCallback; |
| 59 typedef base::Callback<void()> RequestQueueEmptyCallback; | 59 typedef base::Callback<void(bool)> RequestNotPickedCallback; |
| 60 | 60 |
| 61 RequestCoordinator(std::unique_ptr<OfflinerPolicy> policy, | 61 RequestCoordinator(std::unique_ptr<OfflinerPolicy> policy, |
| 62 std::unique_ptr<OfflinerFactory> factory, | 62 std::unique_ptr<OfflinerFactory> factory, |
| 63 std::unique_ptr<RequestQueue> queue, | 63 std::unique_ptr<RequestQueue> queue, |
| 64 std::unique_ptr<Scheduler> scheduler); | 64 std::unique_ptr<Scheduler> scheduler); |
| 65 | 65 |
| 66 ~RequestCoordinator() override; | 66 ~RequestCoordinator() override; |
| 67 | 67 |
| 68 // Queues |request| to later load and save when system conditions allow. | 68 // Queues |request| to later load and save when system conditions allow. |
| 69 // Returns true if the page could be queued successfully. | 69 // Returns true if the page could be queued successfully. |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 103 // a callback. If processing was already active, returns false. | 103 // a callback. If processing was already active, returns false. |
| 104 bool StartProcessing(const DeviceConditions& device_conditions, | 104 bool StartProcessing(const DeviceConditions& device_conditions, |
| 105 const base::Callback<void(bool)>& callback); | 105 const base::Callback<void(bool)>& callback); |
| 106 | 106 |
| 107 // Stops the current request processing if active. This is a way for | 107 // Stops the current request processing if active. This is a way for |
| 108 // caller to abort processing; otherwise, processing will complete on | 108 // caller to abort processing; otherwise, processing will complete on |
| 109 // its own. In either case, the callback will be called when processing | 109 // its own. In either case, the callback will be called when processing |
| 110 // is stopped or complete. | 110 // is stopped or complete. |
| 111 void StopProcessing(); | 111 void StopProcessing(); |
| 112 | 112 |
| 113 const Scheduler::TriggerConditions GetTriggerConditionsForUserRequest(); | 113 const Scheduler::TriggerConditions GetTriggerConditions( |
| 114 const bool user_requested); | |
| 114 | 115 |
| 115 // 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. |
| 116 void SetProcessingCallbackForTest(const base::Callback<void(bool)> callback) { | 117 void SetProcessingCallbackForTest(const base::Callback<void(bool)> callback) { |
| 117 scheduler_callback_ = callback; | 118 scheduler_callback_ = callback; |
| 118 } | 119 } |
| 119 | 120 |
| 120 // Observers implementing the RequestCoordinator::Observer interface can | 121 // Observers implementing the RequestCoordinator::Observer interface can |
| 121 // register here to get notifications of changes to request state. This | 122 // register here to get notifications of changes to request state. This |
| 122 // pointer is not owned, and it is the callers responsibility to remove the | 123 // pointer is not owned, and it is the callers responsibility to remove the |
| 123 // observer before the observer is deleted. | 124 // observer before the observer is deleted. |
| 124 void AddObserver(RequestCoordinator::Observer* observer); | 125 void AddObserver(RequestCoordinator::Observer* observer); |
| 125 | 126 |
| 126 void RemoveObserver(RequestCoordinator::Observer* observer); | 127 void RemoveObserver(RequestCoordinator::Observer* observer); |
| 127 | 128 |
| 128 // Implement RequestNotifier | 129 // Implement RequestNotifier |
| 129 void NotifyAdded(const SavePageRequest& request) override; | 130 void NotifyAdded(const SavePageRequest& request) override; |
| 130 void NotifyCompleted(const SavePageRequest& request, | 131 void NotifyCompleted(const SavePageRequest& request, |
| 131 RequestNotifier::SavePageStatus status) override; | 132 RequestNotifier::SavePageStatus status) override; |
| 132 void NotifyChanged(const SavePageRequest& request) override; | 133 void NotifyChanged(const SavePageRequest& request) override; |
| 133 | 134 |
| 134 // Returns the request queue used for requests. Coordinator keeps ownership. | 135 // Returns the request queue used for requests. Coordinator keeps ownership. |
| 135 RequestQueue* queue() { return queue_.get(); } | 136 RequestQueue* queue() { return queue_.get(); } |
| 136 | 137 |
| 137 // Return an unowned pointer to the Scheduler. | 138 // Return an unowned pointer to the Scheduler. |
| 138 Scheduler* scheduler() { return scheduler_.get(); } | 139 Scheduler* scheduler() { return scheduler_.get(); } |
| 139 | 140 |
| 141 OfflinerPolicy* policy() { return policy_.get(); } | |
| 142 | |
| 140 // Returns the status of the most recent offlining. | 143 // Returns the status of the most recent offlining. |
| 141 Offliner::RequestStatus last_offlining_status() { | 144 Offliner::RequestStatus last_offlining_status() { |
| 142 return last_offlining_status_; | 145 return last_offlining_status_; |
| 143 } | 146 } |
| 144 | 147 |
| 145 bool is_busy() { | 148 bool is_busy() { |
| 146 return is_busy_; | 149 return is_busy_; |
| 147 } | 150 } |
| 148 | 151 |
| 149 // Tracks whether the last offlining attempt got canceled. This is reset by | 152 // Tracks whether the last offlining attempt got canceled. This is reset by |
| 150 // the next StartProcessing() call. | 153 // the next StartProcessing() call. |
| 151 bool is_canceled() { | 154 bool is_canceled() { |
| 152 return is_stopped_; | 155 return is_stopped_; |
| 153 } | 156 } |
| 154 | 157 |
| 155 OfflineEventLogger* GetLogger() { | 158 OfflineEventLogger* GetLogger() { |
| 156 return &event_logger_; | 159 return &event_logger_; |
| 157 } | 160 } |
| 158 | 161 |
| 159 private: | 162 private: |
| 160 // Receives the results of a get from the request queue, and turns that into | 163 // Receives the results of a get from the request queue, and turns that into |
| 161 // SavePageRequest objects for the caller of GetQueuedRequests. | 164 // SavePageRequest objects for the caller of GetQueuedRequests. |
| 162 void GetQueuedRequestsCallback(const GetRequestsCallback& callback, | 165 void GetQueuedRequestsCallback(const GetRequestsCallback& callback, |
| 163 RequestQueue::GetRequestsResult result, | 166 RequestQueue::GetRequestsResult result, |
| 164 const std::vector<SavePageRequest>& requests); | 167 const std::vector<SavePageRequest>& requests); |
| 165 | 168 |
| 169 // Receives the results of a get from the request queue, and turns that into | |
| 170 // SavePageRequest objects for the caller of GetQueuedRequests. | |
| 171 void GetRequestsForSchedulingCallback( | |
| 172 RequestQueue::GetRequestsResult result, | |
| 173 const std::vector<SavePageRequest>& requests); | |
| 174 | |
| 166 // Receives the result of add requests to the request queue. | 175 // Receives the result of add requests to the request queue. |
| 167 void AddRequestResultCallback(RequestQueue::AddRequestResult result, | 176 void AddRequestResultCallback(RequestQueue::AddRequestResult result, |
| 168 const SavePageRequest& request); | 177 const SavePageRequest& request); |
| 169 | 178 |
| 170 // Receives the result of update and delete requests to the request queue. | 179 // Receives the result of update and delete requests to the request queue. |
| 171 void UpdateRequestCallback(const ClientId& client_id, | 180 void UpdateRequestCallback(const ClientId& client_id, |
| 172 RequestQueue::UpdateRequestResult result); | 181 RequestQueue::UpdateRequestResult result); |
| 173 | 182 |
| 174 void UpdateMultipleRequestsCallback( | 183 void UpdateMultipleRequestsCallback( |
| 175 const RequestQueue::UpdateMultipleRequestResults& result, | 184 const RequestQueue::UpdateMultipleRequestResults& result, |
| 176 const std::vector<SavePageRequest>& requests); | 185 const std::vector<SavePageRequest>& requests); |
| 177 | 186 |
| 178 void HandleRemovedRequestsAndCallback( | 187 void HandleRemovedRequestsAndCallback( |
| 179 const RemoveRequestsCallback& callback, | 188 const RemoveRequestsCallback& callback, |
| 180 SavePageStatus status, | 189 SavePageStatus status, |
| 181 const RequestQueue::UpdateMultipleRequestResults& results, | 190 const RequestQueue::UpdateMultipleRequestResults& results, |
| 182 const std::vector<SavePageRequest>& requests); | 191 const std::vector<SavePageRequest>& requests); |
| 183 | 192 |
| 184 void HandleRemovedRequests( | 193 void HandleRemovedRequests( |
| 185 SavePageStatus status, | 194 SavePageStatus status, |
| 186 const RequestQueue::UpdateMultipleRequestResults& results, | 195 const RequestQueue::UpdateMultipleRequestResults& results, |
| 187 const std::vector<SavePageRequest>& requests); | 196 const std::vector<SavePageRequest>& requests); |
| 188 | 197 |
| 189 // Start processing now if connected (but with conservative assumption | 198 // Start processing now if connected (but with conservative assumption |
| 190 // as to other device conditions). | 199 // as to other device conditions). |
| 191 void StartProcessingIfConnected(); | 200 void StartProcessingIfConnected(); |
| 192 | 201 |
| 202 // Check the request queue, and schedule a task corresponding | |
| 203 // to the least restrictive type of request in the queue. | |
| 204 void ScheduleAsNeeded(); | |
| 205 | |
| 193 // Callback from the request picker when it has chosen our next request. | 206 // Callback from the request picker when it has chosen our next request. |
| 194 void RequestPicked(const SavePageRequest& request); | 207 void RequestPicked(const SavePageRequest& request); |
| 195 | 208 |
| 196 // Callback from the request picker when no more requests are in the queue. | 209 // Callback from the request picker when no more requests are in the queue. |
| 197 void RequestQueueEmpty(); | 210 void RequestNotPicked(bool non_user_requested_tasks_remaining); |
| 198 | 211 |
| 199 // Cancels an in progress pre-rendering, and updates state appropriately. | 212 // Cancels an in progress pre-rendering, and updates state appropriately. |
| 200 void StopPrerendering(); | 213 void StopPrerendering(); |
| 201 | 214 |
| 202 void SendRequestToOffliner(const SavePageRequest& request); | 215 void SendRequestToOffliner(const SavePageRequest& request); |
| 203 | 216 |
| 204 // Called by the offliner when an offlining request is completed. (and by | 217 // Called by the offliner when an offlining request is completed. (and by |
| 205 // tests). | 218 // tests). |
| 206 void OfflinerDoneCallback(const SavePageRequest& request, | 219 void OfflinerDoneCallback(const SavePageRequest& request, |
| 207 Offliner::RequestStatus status); | 220 Offliner::RequestStatus status); |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 276 base::TimeDelta offliner_timeout_; | 289 base::TimeDelta offliner_timeout_; |
| 277 // Allows us to pass a weak pointer to callbacks. | 290 // Allows us to pass a weak pointer to callbacks. |
| 278 base::WeakPtrFactory<RequestCoordinator> weak_ptr_factory_; | 291 base::WeakPtrFactory<RequestCoordinator> weak_ptr_factory_; |
| 279 | 292 |
| 280 DISALLOW_COPY_AND_ASSIGN(RequestCoordinator); | 293 DISALLOW_COPY_AND_ASSIGN(RequestCoordinator); |
| 281 }; | 294 }; |
| 282 | 295 |
| 283 } // namespace offline_pages | 296 } // namespace offline_pages |
| 284 | 297 |
| 285 #endif // COMPONENTS_OFFLINE_PAGES_BACKGROUND_REQUEST_COORDINATOR_H_ | 298 #endif // COMPONENTS_OFFLINE_PAGES_BACKGROUND_REQUEST_COORDINATOR_H_ |
| OLD | NEW |