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