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 30 matching lines...) Expand all Loading... | |
| 41 // ignore all events before the return from |GetAllRequests|, and consume | 41 // ignore all events before the return from |GetAllRequests|, and consume |
| 42 // events after the return callback from |GetAllRequests|. | 42 // events after the return callback from |GetAllRequests|. |
| 43 class Observer { | 43 class Observer { |
| 44 public: | 44 public: |
| 45 virtual void OnAdded(const SavePageRequest& request) = 0; | 45 virtual void OnAdded(const SavePageRequest& request) = 0; |
| 46 virtual void OnCompleted(const SavePageRequest& request, | 46 virtual void OnCompleted(const SavePageRequest& request, |
| 47 RequestNotifier::SavePageStatus status) = 0; | 47 RequestNotifier::SavePageStatus status) = 0; |
| 48 virtual void OnChanged(const SavePageRequest& request) = 0; | 48 virtual void OnChanged(const SavePageRequest& request) = 0; |
| 49 }; | 49 }; |
| 50 | 50 |
| 51 // Callback to report when the processing of a triggered task is complete. | 51 // Callback to report when the processing of a triggered task is complete. |
|
dougarnett
2016/08/29 15:47:22
comment doesn't seem to match
Pete Williamson
2016/09/01 00:10:31
Done.
| |
| 52 typedef base::Callback<void(const SavePageRequest& request)> | 52 typedef base::Callback<void(const SavePageRequest& request)> |
| 53 RequestPickedCallback; | 53 RequestPickedCallback; |
| 54 typedef base::Callback<void()> RequestQueueEmptyCallback; | 54 typedef base::Callback<void(bool)> RequestNotPickedCallback; |
| 55 | 55 |
| 56 RequestCoordinator(std::unique_ptr<OfflinerPolicy> policy, | 56 RequestCoordinator(std::unique_ptr<OfflinerPolicy> policy, |
| 57 std::unique_ptr<OfflinerFactory> factory, | 57 std::unique_ptr<OfflinerFactory> factory, |
| 58 std::unique_ptr<RequestQueue> queue, | 58 std::unique_ptr<RequestQueue> queue, |
| 59 std::unique_ptr<Scheduler> scheduler); | 59 std::unique_ptr<Scheduler> scheduler); |
| 60 | 60 |
| 61 ~RequestCoordinator() override; | 61 ~RequestCoordinator() override; |
| 62 | 62 |
| 63 // Queues |request| to later load and save when system conditions allow. | 63 // Queues |request| to later load and save when system conditions allow. |
| 64 // Returns true if the page could be queued successfully. | 64 // Returns true if the page could be queued successfully. |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 98 // a callback. If processing was already active, returns false. | 98 // a callback. If processing was already active, returns false. |
| 99 bool StartProcessing(const DeviceConditions& device_conditions, | 99 bool StartProcessing(const DeviceConditions& device_conditions, |
| 100 const base::Callback<void(bool)>& callback); | 100 const base::Callback<void(bool)>& callback); |
| 101 | 101 |
| 102 // Stops the current request processing if active. This is a way for | 102 // Stops the current request processing if active. This is a way for |
| 103 // caller to abort processing; otherwise, processing will complete on | 103 // caller to abort processing; otherwise, processing will complete on |
| 104 // its own. In either case, the callback will be called when processing | 104 // its own. In either case, the callback will be called when processing |
| 105 // is stopped or complete. | 105 // is stopped or complete. |
| 106 void StopProcessing(); | 106 void StopProcessing(); |
| 107 | 107 |
| 108 const Scheduler::TriggerConditions GetTriggerConditionsForUserRequest(); | 108 const Scheduler::TriggerConditions GetTriggerConditions( |
| 109 const bool user_requested); | |
| 109 | 110 |
| 110 // A way for tests to set the callback in use when an operation is over. | 111 // A way for tests to set the callback in use when an operation is over. |
| 111 void SetProcessingCallbackForTest(const base::Callback<void(bool)> callback) { | 112 void SetProcessingCallbackForTest(const base::Callback<void(bool)> callback) { |
| 112 scheduler_callback_ = callback; | 113 scheduler_callback_ = callback; |
| 113 } | 114 } |
| 114 | 115 |
| 115 // Observers implementing the RequestCoordinator::Observer interface can | 116 // Observers implementing the RequestCoordinator::Observer interface can |
| 116 // register here to get notifications of changes to request state. This | 117 // register here to get notifications of changes to request state. This |
| 117 // pointer is not owned, and it is the callers responsibility to remove the | 118 // pointer is not owned, and it is the callers responsibility to remove the |
| 118 // observer before the observer is deleted. | 119 // observer before the observer is deleted. |
| 119 void AddObserver(RequestCoordinator::Observer* observer); | 120 void AddObserver(RequestCoordinator::Observer* observer); |
| 120 | 121 |
| 121 void RemoveObserver(RequestCoordinator::Observer* observer); | 122 void RemoveObserver(RequestCoordinator::Observer* observer); |
| 122 | 123 |
| 123 // Implement RequestNotifier | 124 // Implement RequestNotifier |
| 124 void NotifyAdded(const SavePageRequest& request) override; | 125 void NotifyAdded(const SavePageRequest& request) override; |
| 125 void NotifyCompleted(const SavePageRequest& request, | 126 void NotifyCompleted(const SavePageRequest& request, |
| 126 RequestNotifier::SavePageStatus status) override; | 127 RequestNotifier::SavePageStatus status) override; |
| 127 void NotifyChanged(const SavePageRequest& request) override; | 128 void NotifyChanged(const SavePageRequest& request) override; |
| 128 | 129 |
| 129 // Returns the request queue used for requests. Coordinator keeps ownership. | 130 // Returns the request queue used for requests. Coordinator keeps ownership. |
| 130 RequestQueue* queue() { return queue_.get(); } | 131 RequestQueue* queue() { return queue_.get(); } |
| 131 | 132 |
| 132 // Return an unowned pointer to the Scheduler. | 133 // Return an unowned pointer to the Scheduler. |
| 133 Scheduler* scheduler() { return scheduler_.get(); } | 134 Scheduler* scheduler() { return scheduler_.get(); } |
| 134 | 135 |
| 136 OfflinerPolicy* policy() { return policy_.get(); } | |
| 137 | |
| 135 // Returns the status of the most recent offlining. | 138 // Returns the status of the most recent offlining. |
| 136 Offliner::RequestStatus last_offlining_status() { | 139 Offliner::RequestStatus last_offlining_status() { |
| 137 return last_offlining_status_; | 140 return last_offlining_status_; |
| 138 } | 141 } |
| 139 | 142 |
| 140 bool is_busy() { | 143 bool is_busy() { |
| 141 return is_busy_; | 144 return is_busy_; |
| 142 } | 145 } |
| 143 | 146 |
| 144 // Tracks whether the last offlining attempt got canceled. This is reset by | 147 // Tracks whether the last offlining attempt got canceled. This is reset by |
| 145 // the next StartProcessing() call. | 148 // the next StartProcessing() call. |
| 146 bool is_canceled() { | 149 bool is_canceled() { |
| 147 return is_stopped_; | 150 return is_stopped_; |
| 148 } | 151 } |
| 149 | 152 |
| 150 OfflineEventLogger* GetLogger() { | 153 OfflineEventLogger* GetLogger() { |
| 151 return &event_logger_; | 154 return &event_logger_; |
| 152 } | 155 } |
| 153 | 156 |
| 154 private: | 157 private: |
| 155 // Receives the results of a get from the request queue, and turns that into | 158 // Receives the results of a get from the request queue, and turns that into |
| 156 // SavePageRequest objects for the caller of GetQueuedRequests. | 159 // SavePageRequest objects for the caller of GetQueuedRequests. |
| 157 void GetQueuedRequestsCallback(const GetRequestsCallback& callback, | 160 void GetQueuedRequestsCallback(const GetRequestsCallback& callback, |
| 158 RequestQueue::GetRequestsResult result, | 161 RequestQueue::GetRequestsResult result, |
| 159 const std::vector<SavePageRequest>& requests); | 162 const std::vector<SavePageRequest>& requests); |
| 160 | 163 |
| 164 // Receives the results of a get from the request queue, and turns that into | |
| 165 // SavePageRequest objects for the caller of GetQueuedRequests. | |
| 166 void GetRequestsForSchedulingCallback( | |
| 167 RequestQueue::GetRequestsResult result, | |
| 168 const std::vector<SavePageRequest>& requests); | |
| 169 | |
| 161 // Receives the result of add requests to the request queue. | 170 // Receives the result of add requests to the request queue. |
| 162 void AddRequestResultCallback(RequestQueue::AddRequestResult result, | 171 void AddRequestResultCallback(RequestQueue::AddRequestResult result, |
| 163 const SavePageRequest& request); | 172 const SavePageRequest& request); |
| 164 | 173 |
| 165 // Receives the result of update and delete requests to the request queue. | 174 // Receives the result of update and delete requests to the request queue. |
| 166 void UpdateRequestCallback(const ClientId& client_id, | 175 void UpdateRequestCallback(const ClientId& client_id, |
| 167 RequestQueue::UpdateRequestResult result); | 176 RequestQueue::UpdateRequestResult result); |
| 168 | 177 |
| 169 void UpdateMultipleRequestsCallback( | 178 void UpdateMultipleRequestsCallback( |
| 170 const RequestQueue::UpdateMultipleRequestResults& result, | 179 const RequestQueue::UpdateMultipleRequestResults& result, |
| 171 const std::vector<SavePageRequest>& requests); | 180 const std::vector<SavePageRequest>& requests); |
| 172 | 181 |
| 173 void HandleRemovedRequestsAndCallback( | 182 void HandleRemovedRequestsAndCallback( |
| 174 const RemoveRequestsCallback& callback, | 183 const RemoveRequestsCallback& callback, |
| 175 const RequestQueue::UpdateMultipleRequestResults& results, | 184 const RequestQueue::UpdateMultipleRequestResults& results, |
| 176 const std::vector<SavePageRequest>& requests); | 185 const std::vector<SavePageRequest>& requests); |
| 177 | 186 |
| 178 void HandleRemovedRequests( | 187 void HandleRemovedRequests( |
| 179 const RequestQueue::UpdateMultipleRequestResults& results, | 188 const RequestQueue::UpdateMultipleRequestResults& results, |
| 180 const std::vector<SavePageRequest>& requests); | 189 const std::vector<SavePageRequest>& requests); |
| 181 | 190 |
| 191 // Check the request queue, and schedule a task corresponding | |
| 192 // to the least restrictive type of request in the queue. | |
| 193 void ScheduleAsNeeded(); | |
| 194 | |
| 182 // Callback from the request picker when it has chosen our next request. | 195 // Callback from the request picker when it has chosen our next request. |
| 183 void RequestPicked(const SavePageRequest& request); | 196 void RequestPicked(const SavePageRequest& request); |
| 184 | 197 |
| 185 // Callback from the request picker when no more requests are in the queue. | 198 // Callback from the request picker when no more requests are in the queue. |
| 186 void RequestQueueEmpty(); | 199 void RequestNotPicked(bool non_user_requested_tasks_remaining); |
|
dougarnett
2016/08/29 15:47:22
I wonder if this should be something like least_re
Pete Williamson
2016/09/01 00:10:31
While it would be useful to have something like Tr
dougarnett
2016/09/01 18:24:13
Ok, please comment the point of the arg so a bit e
Pete Williamson
2016/09/01 21:56:08
Done.
| |
| 187 | 200 |
| 188 // Cancels an in progress pre-rendering, and updates state appropriately. | 201 // Cancels an in progress pre-rendering, and updates state appropriately. |
| 189 void StopPrerendering(); | 202 void StopPrerendering(); |
| 190 | 203 |
| 191 void SendRequestToOffliner(const SavePageRequest& request); | 204 void SendRequestToOffliner(const SavePageRequest& request); |
| 192 | 205 |
| 193 // Called by the offliner when an offlining request is completed. (and by | 206 // Called by the offliner when an offlining request is completed. (and by |
| 194 // tests). | 207 // tests). |
| 195 void OfflinerDoneCallback(const SavePageRequest& request, | 208 void OfflinerDoneCallback(const SavePageRequest& request, |
| 196 Offliner::RequestStatus status); | 209 Offliner::RequestStatus status); |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 251 base::TimeDelta offliner_timeout_; | 264 base::TimeDelta offliner_timeout_; |
| 252 // Allows us to pass a weak pointer to callbacks. | 265 // Allows us to pass a weak pointer to callbacks. |
| 253 base::WeakPtrFactory<RequestCoordinator> weak_ptr_factory_; | 266 base::WeakPtrFactory<RequestCoordinator> weak_ptr_factory_; |
| 254 | 267 |
| 255 DISALLOW_COPY_AND_ASSIGN(RequestCoordinator); | 268 DISALLOW_COPY_AND_ASSIGN(RequestCoordinator); |
| 256 }; | 269 }; |
| 257 | 270 |
| 258 } // namespace offline_pages | 271 } // namespace offline_pages |
| 259 | 272 |
| 260 #endif // COMPONENTS_OFFLINE_PAGES_BACKGROUND_REQUEST_COORDINATOR_H_ | 273 #endif // COMPONENTS_OFFLINE_PAGES_BACKGROUND_REQUEST_COORDINATOR_H_ |
| OLD | NEW |