| 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 #include <set> | 9 #include <set> |
| 10 #include <vector> |
| 10 | 11 |
| 11 #include "base/callback.h" | 12 #include "base/callback.h" |
| 12 #include "base/macros.h" | 13 #include "base/macros.h" |
| 13 #include "base/memory/weak_ptr.h" | 14 #include "base/memory/weak_ptr.h" |
| 14 #include "base/observer_list.h" | 15 #include "base/observer_list.h" |
| 15 #include "base/supports_user_data.h" | 16 #include "base/supports_user_data.h" |
| 16 #include "base/time/time.h" | 17 #include "base/time/time.h" |
| 17 #include "base/timer/timer.h" | 18 #include "base/timer/timer.h" |
| 18 #include "components/keyed_service/core/keyed_service.h" | 19 #include "components/keyed_service/core/keyed_service.h" |
| 19 #include "components/offline_pages/background/device_conditions.h" | 20 #include "components/offline_pages/background/device_conditions.h" |
| (...skipping 14 matching lines...) Expand all Loading... |
| 34 class RequestPicker; | 35 class RequestPicker; |
| 35 class SavePageRequest; | 36 class SavePageRequest; |
| 36 class Scheduler; | 37 class Scheduler; |
| 37 class ClientPolicyController; | 38 class ClientPolicyController; |
| 38 | 39 |
| 39 // Coordinates queueing and processing save page later requests. | 40 // Coordinates queueing and processing save page later requests. |
| 40 class RequestCoordinator : public KeyedService, | 41 class RequestCoordinator : public KeyedService, |
| 41 public RequestNotifier, | 42 public RequestNotifier, |
| 42 public base::SupportsUserData { | 43 public base::SupportsUserData { |
| 43 public: | 44 public: |
| 44 | |
| 45 // Nested observer class. To make sure that no events are missed, the client | 45 // Nested observer class. To make sure that no events are missed, the client |
| 46 // code should first register for notifications, then |GetAllRequests|, and | 46 // code should first register for notifications, then |GetAllRequests|, and |
| 47 // ignore all events before the return from |GetAllRequests|, and consume | 47 // ignore all events before the return from |GetAllRequests|, and consume |
| 48 // events after the return callback from |GetAllRequests|. | 48 // events after the return callback from |GetAllRequests|. |
| 49 class Observer { | 49 class Observer { |
| 50 public: | 50 public: |
| 51 virtual ~Observer() = default; | 51 virtual ~Observer() = default; |
| 52 | 52 |
| 53 virtual void OnAdded(const SavePageRequest& request) = 0; | 53 virtual void OnAdded(const SavePageRequest& request) = 0; |
| 54 virtual void OnCompleted( | 54 virtual void OnCompleted( |
| (...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 171 return is_busy_; | 171 return is_busy_; |
| 172 } | 172 } |
| 173 | 173 |
| 174 // Returns whether processing is starting (before it is decided to actually | 174 // Returns whether processing is starting (before it is decided to actually |
| 175 // process a request (is_busy()) at this time or not. | 175 // process a request (is_busy()) at this time or not. |
| 176 bool is_starting() { return is_starting_; } | 176 bool is_starting() { return is_starting_; } |
| 177 | 177 |
| 178 // Tracks whether the last offlining attempt got canceled. This is reset by | 178 // Tracks whether the last offlining attempt got canceled. This is reset by |
| 179 // the next StartProcessing() call. | 179 // the next StartProcessing() call. |
| 180 bool is_canceled() { | 180 bool is_canceled() { |
| 181 return is_stopped_; | 181 return processing_state_ == ProcessingWindowState::STOPPED; |
| 182 } | 182 } |
| 183 | 183 |
| 184 OfflineEventLogger* GetLogger() { | 184 OfflineEventLogger* GetLogger() { |
| 185 return &event_logger_; | 185 return &event_logger_; |
| 186 } | 186 } |
| 187 | 187 |
| 188 private: | 188 private: |
| 189 // Immediate start attempt status code for UMA. | 189 // Immediate start attempt status code for UMA. |
| 190 // These values are written to logs. New enum values can be added, but | 190 // These values are written to logs. New enum values can be added, but |
| 191 // existing enums must never be renumbered or deleted and reused. | 191 // existing enums must never be renumbered or deleted and reused. |
| 192 // For any additions, also update corresponding histogram in histograms.xml. | 192 // For any additions, also update corresponding histogram in histograms.xml. |
| 193 enum OfflinerImmediateStartStatus { | 193 enum OfflinerImmediateStartStatus { |
| 194 // Did start processing request. | 194 // Did start processing request. |
| 195 STARTED = 0, | 195 STARTED = 0, |
| 196 // Already busy processing a request. | 196 // Already busy processing a request. |
| 197 BUSY = 1, | 197 BUSY = 1, |
| 198 // The Offliner did not accept processing the request. | 198 // The Offliner did not accept processing the request. |
| 199 NOT_ACCEPTED = 2, | 199 NOT_ACCEPTED = 2, |
| 200 // No current network connection. | 200 // No current network connection. |
| 201 NO_CONNECTION = 3, | 201 NO_CONNECTION = 3, |
| 202 // Weak network connection (worse than 2G speed) | 202 // Weak network connection (worse than 2G speed) |
| 203 // according to network quality estimator. | 203 // according to network quality estimator. |
| 204 WEAK_CONNECTION = 4, | 204 WEAK_CONNECTION = 4, |
| 205 // Did not start because this is svelte device. | 205 // Did not start because this is svelte device. |
| 206 NOT_STARTED_ON_SVELTE = 5, | 206 NOT_STARTED_ON_SVELTE = 5, |
| 207 // NOTE: insert new values above this line and update histogram enum too. | 207 // NOTE: insert new values above this line and update histogram enum too. |
| 208 STATUS_COUNT = 6, | 208 STATUS_COUNT = 6, |
| 209 }; | 209 }; |
| 210 | 210 |
| 211 enum class ProcessingWindowState { |
| 212 STOPPED, |
| 213 SCHEDULED_WINDOW, |
| 214 IMMEDIATE_WINDOW, |
| 215 }; |
| 216 |
| 211 // Receives the results of a get from the request queue, and turns that into | 217 // Receives the results of a get from the request queue, and turns that into |
| 212 // SavePageRequest objects for the caller of GetQueuedRequests. | 218 // SavePageRequest objects for the caller of GetQueuedRequests. |
| 213 void GetQueuedRequestsCallback( | 219 void GetQueuedRequestsCallback( |
| 214 const GetRequestsCallback& callback, | 220 const GetRequestsCallback& callback, |
| 215 RequestQueue::GetRequestsResult result, | 221 RequestQueue::GetRequestsResult result, |
| 216 std::vector<std::unique_ptr<SavePageRequest>> requests); | 222 std::vector<std::unique_ptr<SavePageRequest>> requests); |
| 217 | 223 |
| 218 // Receives the results of a get from the request queue, and turns that into | 224 // Receives the results of a get from the request queue, and turns that into |
| 219 // SavePageRequest objects for the caller of GetQueuedRequests. | 225 // SavePageRequest objects for the caller of GetQueuedRequests. |
| 220 void GetRequestsForSchedulingCallback( | 226 void GetRequestsForSchedulingCallback( |
| (...skipping 14 matching lines...) Expand all Loading... |
| 235 void CompletedRequestCallback(const MultipleItemStatuses& status); | 241 void CompletedRequestCallback(const MultipleItemStatuses& status); |
| 236 | 242 |
| 237 void HandleRemovedRequestsAndCallback( | 243 void HandleRemovedRequestsAndCallback( |
| 238 const RemoveRequestsCallback& callback, | 244 const RemoveRequestsCallback& callback, |
| 239 BackgroundSavePageResult status, | 245 BackgroundSavePageResult status, |
| 240 std::unique_ptr<UpdateRequestsResult> result); | 246 std::unique_ptr<UpdateRequestsResult> result); |
| 241 | 247 |
| 242 void HandleRemovedRequests(BackgroundSavePageResult status, | 248 void HandleRemovedRequests(BackgroundSavePageResult status, |
| 243 std::unique_ptr<UpdateRequestsResult> result); | 249 std::unique_ptr<UpdateRequestsResult> result); |
| 244 | 250 |
| 251 bool StartProcessingInternal(const ProcessingWindowState processing_state, |
| 252 const DeviceConditions& device_conditions, |
| 253 const base::Callback<void(bool)>& callback); |
| 254 |
| 245 // Start processing now if connected (but with conservative assumption | 255 // Start processing now if connected (but with conservative assumption |
| 246 // as to other device conditions). | 256 // as to other device conditions). |
| 247 void StartProcessingIfConnected(); | 257 void StartImmediatelyIfConnected(); |
| 248 | 258 |
| 249 OfflinerImmediateStartStatus TryImmediateStart(); | 259 OfflinerImmediateStartStatus TryImmediateStart(); |
| 250 | 260 |
| 251 // Check the request queue, and schedule a task corresponding | 261 // Check the request queue, and schedule a task corresponding |
| 252 // to the least restrictive type of request in the queue. | 262 // to the least restrictive type of request in the queue. |
| 253 void ScheduleAsNeeded(); | 263 void ScheduleAsNeeded(); |
| 254 | 264 |
| 255 // Callback from the request picker when it has chosen our next request. | 265 // Callback from the request picker when it has chosen our next request. |
| 256 void RequestPicked(const SavePageRequest& request); | 266 void RequestPicked(const SavePageRequest& request); |
| 257 | 267 |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 293 // Method to wrap calls to getting the connection type so it can be | 303 // Method to wrap calls to getting the connection type so it can be |
| 294 // changed for tests. | 304 // changed for tests. |
| 295 net::NetworkChangeNotifier::ConnectionType GetConnectionType(); | 305 net::NetworkChangeNotifier::ConnectionType GetConnectionType(); |
| 296 | 306 |
| 297 void SetNetworkConditionsForTest( | 307 void SetNetworkConditionsForTest( |
| 298 net::NetworkChangeNotifier::ConnectionType connection) { | 308 net::NetworkChangeNotifier::ConnectionType connection) { |
| 299 use_test_connection_type_ = true; | 309 use_test_connection_type_ = true; |
| 300 test_connection_type_ = connection; | 310 test_connection_type_ = connection; |
| 301 } | 311 } |
| 302 | 312 |
| 303 void SetOfflinerTimeoutForTest(const base::TimeDelta& timeout) { | 313 void SetDeviceConditionsForTest(const DeviceConditions& current_conditions) { |
| 304 offliner_timeout_ = timeout; | |
| 305 } | |
| 306 | |
| 307 void SetDeviceConditionsForTest(DeviceConditions& current_conditions) { | |
| 308 current_conditions_.reset(new DeviceConditions(current_conditions)); | 314 current_conditions_.reset(new DeviceConditions(current_conditions)); |
| 309 } | 315 } |
| 310 | 316 |
| 311 // KeyedService implementation: | 317 // KeyedService implementation: |
| 312 void Shutdown() override; | 318 void Shutdown() override; |
| 313 | 319 |
| 314 friend class RequestCoordinatorTest; | 320 friend class RequestCoordinatorTest; |
| 315 | 321 |
| 316 // The offliner can only handle one request at a time - if the offliner is | 322 // The offliner can only handle one request at a time - if the offliner is |
| 317 // busy, prevent other requests. This flag marks whether the offliner is in | 323 // busy, prevent other requests. This flag marks whether the offliner is in |
| 318 // use. | 324 // use. |
| 319 bool is_busy_; | 325 bool is_busy_; |
| 320 // There is more than one path to start processing so this flag is used | 326 // There is more than one path to start processing so this flag is used |
| 321 // to avoid race conditions before is_busy_ is established. | 327 // to avoid race conditions before is_busy_ is established. |
| 322 bool is_starting_; | 328 bool is_starting_; |
| 323 // True if the current processing window has been canceled. | 329 // Identifies the type of current processing window or if processing stopped. |
| 324 bool is_stopped_; | 330 ProcessingWindowState processing_state_; |
| 325 // True if we should use the test connection type instead of the actual type. | 331 // True if we should use the test connection type instead of the actual type. |
| 326 bool use_test_connection_type_; | 332 bool use_test_connection_type_; |
| 327 // For use by tests, a fake network connection type | 333 // For use by tests, a fake network connection type |
| 328 net::NetworkChangeNotifier::ConnectionType test_connection_type_; | 334 net::NetworkChangeNotifier::ConnectionType test_connection_type_; |
| 329 // Unowned pointer to the current offliner, if any. | 335 // Unowned pointer to the current offliner, if any. |
| 330 Offliner* offliner_; | 336 Offliner* offliner_; |
| 331 base::Time operation_start_time_; | 337 base::Time operation_start_time_; |
| 332 // The observers. | 338 // The observers. |
| 333 base::ObserverList<Observer> observers_; | 339 base::ObserverList<Observer> observers_; |
| 334 // Last known conditions for network, battery | 340 // Last known conditions for network, battery |
| (...skipping 19 matching lines...) Expand all Loading... |
| 354 std::unique_ptr<RequestPicker> picker_; | 360 std::unique_ptr<RequestPicker> picker_; |
| 355 // A set of request_ids that we are holding off until the download manager is | 361 // A set of request_ids that we are holding off until the download manager is |
| 356 // done with them. | 362 // done with them. |
| 357 std::set<int64_t> disabled_requests_; | 363 std::set<int64_t> disabled_requests_; |
| 358 // Calling this returns to the scheduler across the JNI bridge. | 364 // Calling this returns to the scheduler across the JNI bridge. |
| 359 base::Callback<void(bool)> scheduler_callback_; | 365 base::Callback<void(bool)> scheduler_callback_; |
| 360 // Logger to record events. | 366 // Logger to record events. |
| 361 RequestCoordinatorEventLogger event_logger_; | 367 RequestCoordinatorEventLogger event_logger_; |
| 362 // Timer to watch for pre-render attempts running too long. | 368 // Timer to watch for pre-render attempts running too long. |
| 363 base::OneShotTimer watchdog_timer_; | 369 base::OneShotTimer watchdog_timer_; |
| 364 // How long to wait for an offliner request before giving up. | |
| 365 base::TimeDelta offliner_timeout_; | |
| 366 // Allows us to pass a weak pointer to callbacks. | 370 // Allows us to pass a weak pointer to callbacks. |
| 367 base::WeakPtrFactory<RequestCoordinator> weak_ptr_factory_; | 371 base::WeakPtrFactory<RequestCoordinator> weak_ptr_factory_; |
| 368 | 372 |
| 369 DISALLOW_COPY_AND_ASSIGN(RequestCoordinator); | 373 DISALLOW_COPY_AND_ASSIGN(RequestCoordinator); |
| 370 }; | 374 }; |
| 371 | 375 |
| 372 } // namespace offline_pages | 376 } // namespace offline_pages |
| 373 | 377 |
| 374 #endif // COMPONENTS_OFFLINE_PAGES_BACKGROUND_REQUEST_COORDINATOR_H_ | 378 #endif // COMPONENTS_OFFLINE_PAGES_BACKGROUND_REQUEST_COORDINATOR_H_ |
| OLD | NEW |