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 | |
| 38 // Data struct for nested observer. Add more items as needed. | |
| 39 struct RequestInfo { | |
| 40 ClientId client_id; | |
| 41 }; | |
| 42 | |
| 43 // Nested observer class | |
| 44 class Observer { | |
| 45 public: | |
| 46 virtual void OnAvailable(RequestInfo info) = 0; | |
|
Dmitry Titov
2016/08/09 20:14:59
Since this is an internal, most generic interface
Pete Williamson
2016/08/10 01:36:23
OK, changed to
OnAdded
OnSucceeded(item, offline_
| |
| 47 virtual void OnSucceeded(RequestInfo info) = 0; | |
| 48 virtual void OnFailed(RequestInfo info) = 0; | |
| 49 virtual void OnPaused(RequestInfo info) = 0; | |
| 50 }; | |
| 51 | |
| 36 // Callback to report when the processing of a triggered task is complete. | 52 // Callback to report when the processing of a triggered task is complete. |
| 37 typedef base::Callback<void(const SavePageRequest& request)> | 53 typedef base::Callback<void(const SavePageRequest& request)> |
| 38 RequestPickedCallback; | 54 RequestPickedCallback; |
| 39 typedef base::Callback<void()> RequestQueueEmptyCallback; | 55 typedef base::Callback<void()> RequestQueueEmptyCallback; |
| 40 | 56 |
| 41 RequestCoordinator(std::unique_ptr<OfflinerPolicy> policy, | 57 RequestCoordinator(std::unique_ptr<OfflinerPolicy> policy, |
| 42 std::unique_ptr<OfflinerFactory> factory, | 58 std::unique_ptr<OfflinerFactory> factory, |
| 43 std::unique_ptr<RequestQueue> queue, | 59 std::unique_ptr<RequestQueue> queue, |
| 44 std::unique_ptr<Scheduler> scheduler); | 60 std::unique_ptr<Scheduler> scheduler); |
| 45 | 61 |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 86 // is stopped or complete. | 102 // is stopped or complete. |
| 87 void StopProcessing(); | 103 void StopProcessing(); |
| 88 | 104 |
| 89 const Scheduler::TriggerConditions GetTriggerConditionsForUserRequest(); | 105 const Scheduler::TriggerConditions GetTriggerConditionsForUserRequest(); |
| 90 | 106 |
| 91 // A way for tests to set the callback in use when an operation is over. | 107 // A way for tests to set the callback in use when an operation is over. |
| 92 void SetProcessingCallbackForTest(const base::Callback<void(bool)> callback) { | 108 void SetProcessingCallbackForTest(const base::Callback<void(bool)> callback) { |
| 93 scheduler_callback_ = callback; | 109 scheduler_callback_ = callback; |
| 94 } | 110 } |
| 95 | 111 |
| 112 // Observers implementing the RequestCoordinator::Observer interface can | |
| 113 // register here to get notifications of changes to request state. This | |
| 114 // pointer is unowned, and it is the callers responsibility to remove the | |
| 115 // observer before the observer is deleted. | |
|
Dmitry Titov
2016/08/09 20:14:59
This comment should also mention what happens when
Pete Williamson
2016/08/10 01:36:23
Done.
| |
| 116 void AddObserver(RequestCoordinator::Observer* observer); | |
| 117 | |
| 118 void RemoveObserver(RequestCoordinator::Observer* observer); | |
| 119 | |
| 96 // Returns the request queue used for requests. Coordinator keeps ownership. | 120 // Returns the request queue used for requests. Coordinator keeps ownership. |
| 97 RequestQueue* queue() { return queue_.get(); } | 121 RequestQueue* queue() { return queue_.get(); } |
| 98 | 122 |
| 99 // Return an unowned pointer to the Scheduler. | 123 // Return an unowned pointer to the Scheduler. |
| 100 Scheduler* scheduler() { return scheduler_.get(); } | 124 Scheduler* scheduler() { return scheduler_.get(); } |
| 101 | 125 |
| 102 // Returns the status of the most recent offlining. | 126 // Returns the status of the most recent offlining. |
| 103 Offliner::RequestStatus last_offlining_status() { | 127 Offliner::RequestStatus last_offlining_status() { |
| 104 return last_offlining_status_; | 128 return last_offlining_status_; |
| 105 } | 129 } |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 144 | 168 |
| 145 void SendRequestToOffliner(const SavePageRequest& request); | 169 void SendRequestToOffliner(const SavePageRequest& request); |
| 146 | 170 |
| 147 // Called by the offliner when an offlining request is completed. (and by | 171 // Called by the offliner when an offlining request is completed. (and by |
| 148 // tests). | 172 // tests). |
| 149 void OfflinerDoneCallback(const SavePageRequest& request, | 173 void OfflinerDoneCallback(const SavePageRequest& request, |
| 150 Offliner::RequestStatus status); | 174 Offliner::RequestStatus status); |
| 151 | 175 |
| 152 void TryNextRequest(); | 176 void TryNextRequest(); |
| 153 | 177 |
| 178 void NotifyAvailable(ClientId client_id); | |
| 179 void NotifySucceeded(ClientId client_id); | |
| 180 void NotifyFailed(ClientId client_id); | |
| 181 void NotifyPaused(ClientId client_id); | |
| 182 | |
| 154 // Returns the appropriate offliner to use, getting a new one from the factory | 183 // Returns the appropriate offliner to use, getting a new one from the factory |
| 155 // if needed. | 184 // if needed. |
| 156 void GetOffliner(); | 185 void GetOffliner(); |
| 157 | 186 |
| 158 void SetOfflinerTimeoutForTest(const base::TimeDelta& timeout) { | 187 void SetOfflinerTimeoutForTest(const base::TimeDelta& timeout) { |
| 159 offliner_timeout_ = timeout; | 188 offliner_timeout_ = timeout; |
| 160 } | 189 } |
| 161 | 190 |
| 162 void SetDeviceConditionsForTest(DeviceConditions& current_conditions) { | 191 void SetDeviceConditionsForTest(DeviceConditions& current_conditions) { |
| 163 current_conditions_.reset(new DeviceConditions(current_conditions)); | 192 current_conditions_.reset(new DeviceConditions(current_conditions)); |
| 164 } | 193 } |
| 165 | 194 |
| 166 friend class RequestCoordinatorTest; | 195 friend class RequestCoordinatorTest; |
| 167 | 196 |
| 168 // The offliner can only handle one request at a time - if the offliner is | 197 // 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 | 198 // busy, prevent other requests. This flag marks whether the offliner is in |
| 170 // use. | 199 // use. |
| 171 bool is_busy_; | 200 bool is_busy_; |
| 172 // True if the current request has been canceled. | 201 // True if the current request has been canceled. |
| 173 bool is_canceled_; | 202 bool is_canceled_; |
| 174 // Unowned pointer to the current offliner, if any. | 203 // Unowned pointer to the current offliner, if any. |
| 175 Offliner* offliner_; | 204 Offliner* offliner_; |
| 176 base::Time operation_start_time_; | 205 base::Time operation_start_time_; |
| 206 // The observers. | |
| 207 base::ObserverList<Observer> observers_; | |
| 177 // Last known conditions for network, battery | 208 // Last known conditions for network, battery |
| 178 std::unique_ptr<DeviceConditions> current_conditions_; | 209 std::unique_ptr<DeviceConditions> current_conditions_; |
| 179 // RequestCoordinator takes over ownership of the policy | 210 // RequestCoordinator takes over ownership of the policy |
| 180 std::unique_ptr<OfflinerPolicy> policy_; | 211 std::unique_ptr<OfflinerPolicy> policy_; |
| 181 // OfflinerFactory. Used to create offline pages. Owned. | 212 // OfflinerFactory. Used to create offline pages. Owned. |
| 182 std::unique_ptr<OfflinerFactory> factory_; | 213 std::unique_ptr<OfflinerFactory> factory_; |
| 183 // RequestQueue. Used to store incoming requests. Owned. | 214 // RequestQueue. Used to store incoming requests. Owned. |
| 184 std::unique_ptr<RequestQueue> queue_; | 215 std::unique_ptr<RequestQueue> queue_; |
| 185 // Scheduler. Used to request a callback when network is available. Owned. | 216 // Scheduler. Used to request a callback when network is available. Owned. |
| 186 std::unique_ptr<Scheduler> scheduler_; | 217 std::unique_ptr<Scheduler> scheduler_; |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 200 base::TimeDelta offliner_timeout_; | 231 base::TimeDelta offliner_timeout_; |
| 201 // Allows us to pass a weak pointer to callbacks. | 232 // Allows us to pass a weak pointer to callbacks. |
| 202 base::WeakPtrFactory<RequestCoordinator> weak_ptr_factory_; | 233 base::WeakPtrFactory<RequestCoordinator> weak_ptr_factory_; |
| 203 | 234 |
| 204 DISALLOW_COPY_AND_ASSIGN(RequestCoordinator); | 235 DISALLOW_COPY_AND_ASSIGN(RequestCoordinator); |
| 205 }; | 236 }; |
| 206 | 237 |
| 207 } // namespace offline_pages | 238 } // namespace offline_pages |
| 208 | 239 |
| 209 #endif // COMPONENTS_OFFLINE_PAGES_BACKGROUND_REQUEST_COORDINATOR_H_ | 240 #endif // COMPONENTS_OFFLINE_PAGES_BACKGROUND_REQUEST_COORDINATOR_H_ |
| OLD | NEW |