| 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 <string> | 10 #include <string> |
| (...skipping 16 matching lines...) Expand all Loading... |
| 27 #include "net/nqe/network_quality_estimator.h" | 27 #include "net/nqe/network_quality_estimator.h" |
| 28 #include "url/gurl.h" | 28 #include "url/gurl.h" |
| 29 | 29 |
| 30 namespace offline_pages { | 30 namespace offline_pages { |
| 31 | 31 |
| 32 struct ClientId; | 32 struct ClientId; |
| 33 class OfflinerPolicy; | 33 class OfflinerPolicy; |
| 34 class OfflinerFactory; | 34 class OfflinerFactory; |
| 35 class Offliner; | 35 class Offliner; |
| 36 class RequestPicker; | 36 class RequestPicker; |
| 37 class RequestQueue; |
| 37 class SavePageRequest; | 38 class SavePageRequest; |
| 38 class Scheduler; | 39 class Scheduler; |
| 39 class ClientPolicyController; | 40 class ClientPolicyController; |
| 40 | 41 |
| 41 // Coordinates queueing and processing save page later requests. | 42 // Coordinates queueing and processing save page later requests. |
| 42 class RequestCoordinator : public KeyedService, | 43 class RequestCoordinator : public KeyedService, |
| 43 public RequestNotifier, | 44 public RequestNotifier, |
| 44 public base::SupportsUserData { | 45 public base::SupportsUserData { |
| 45 public: | 46 public: |
| 46 // Nested observer class. To make sure that no events are missed, the client | 47 // Nested observer class. To make sure that no events are missed, the client |
| 47 // code should first register for notifications, then |GetAllRequests|, and | 48 // code should first register for notifications, then |GetAllRequests|, and |
| 48 // ignore all events before the return from |GetAllRequests|, and consume | 49 // ignore all events before the return from |GetAllRequests|, and consume |
| 49 // events after the return callback from |GetAllRequests|. | 50 // events after the return callback from |GetAllRequests|. |
| 50 class Observer { | 51 class Observer { |
| 51 public: | 52 public: |
| 52 virtual ~Observer() = default; | 53 virtual ~Observer() = default; |
| 53 | 54 |
| 54 virtual void OnAdded(const SavePageRequest& request) = 0; | 55 virtual void OnAdded(const SavePageRequest& request) = 0; |
| 55 virtual void OnCompleted( | 56 virtual void OnCompleted( |
| 56 const SavePageRequest& request, | 57 const SavePageRequest& request, |
| 57 RequestNotifier::BackgroundSavePageResult status) = 0; | 58 RequestNotifier::BackgroundSavePageResult status) = 0; |
| 58 virtual void OnChanged(const SavePageRequest& request) = 0; | 59 virtual void OnChanged(const SavePageRequest& request) = 0; |
| 59 }; | 60 }; |
| 60 | 61 |
| 61 enum class RequestAvailability { | 62 enum class RequestAvailability { |
| 62 ENABLED_FOR_OFFLINER, | 63 ENABLED_FOR_OFFLINER, |
| 63 DISABLED_FOR_OFFLINER, | 64 DISABLED_FOR_OFFLINER, |
| 64 }; | 65 }; |
| 65 | 66 |
| 66 // Callback to report when a request was available. | |
| 67 typedef base::Callback<void(const SavePageRequest& request)> | |
| 68 RequestPickedCallback; | |
| 69 | |
| 70 // Callback to report when no request was available. | |
| 71 typedef base::Callback<void(bool)> RequestNotPickedCallback; | |
| 72 | |
| 73 // Callback specifying which request IDs were actually removed. | 67 // Callback specifying which request IDs were actually removed. |
| 74 typedef base::Callback<void(const MultipleItemStatuses&)> | 68 typedef base::Callback<void(const MultipleItemStatuses&)> |
| 75 RemoveRequestsCallback; | 69 RemoveRequestsCallback; |
| 76 | 70 |
| 77 // Callback that receives the response for GetAllRequests. | 71 // Callback that receives the response for GetAllRequests. |
| 78 typedef base::Callback<void(std::vector<std::unique_ptr<SavePageRequest>>)> | 72 typedef base::Callback<void(std::vector<std::unique_ptr<SavePageRequest>>)> |
| 79 GetRequestsCallback; | 73 GetRequestsCallback; |
| 80 | 74 |
| 81 RequestCoordinator(std::unique_ptr<OfflinerPolicy> policy, | 75 RequestCoordinator(std::unique_ptr<OfflinerPolicy> policy, |
| 82 std::unique_ptr<OfflinerFactory> factory, | 76 std::unique_ptr<OfflinerFactory> factory, |
| (...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 185 // Returns whether processing is starting (before it is decided to actually | 179 // Returns whether processing is starting (before it is decided to actually |
| 186 // process a request (is_busy()) at this time or not. | 180 // process a request (is_busy()) at this time or not. |
| 187 bool is_starting() { return is_starting_; } | 181 bool is_starting() { return is_starting_; } |
| 188 | 182 |
| 189 // Tracks whether the last offlining attempt got canceled. This is reset by | 183 // Tracks whether the last offlining attempt got canceled. This is reset by |
| 190 // the next StartProcessing() call. | 184 // the next StartProcessing() call. |
| 191 bool is_canceled() { | 185 bool is_canceled() { |
| 192 return processing_state_ == ProcessingWindowState::STOPPED; | 186 return processing_state_ == ProcessingWindowState::STOPPED; |
| 193 } | 187 } |
| 194 | 188 |
| 195 OfflineEventLogger* GetLogger() { | 189 RequestCoordinatorEventLogger* GetLogger() { return &event_logger_; } |
| 196 return &event_logger_; | |
| 197 } | |
| 198 | 190 |
| 199 private: | 191 private: |
| 200 // Immediate start attempt status code for UMA. | 192 // Immediate start attempt status code for UMA. |
| 201 // These values are written to logs. New enum values can be added, but | 193 // These values are written to logs. New enum values can be added, but |
| 202 // existing enums must never be renumbered or deleted and reused. | 194 // existing enums must never be renumbered or deleted and reused. |
| 203 // For any additions, also update corresponding histogram in histograms.xml. | 195 // For any additions, also update corresponding histogram in histograms.xml. |
| 204 enum OfflinerImmediateStartStatus { | 196 enum OfflinerImmediateStartStatus { |
| 205 // Did start processing request. | 197 // Did start processing request. |
| 206 STARTED = 0, | 198 STARTED = 0, |
| 207 // Already busy processing a request. | 199 // Already busy processing a request. |
| (...skipping 14 matching lines...) Expand all Loading... |
| 222 enum class ProcessingWindowState { | 214 enum class ProcessingWindowState { |
| 223 STOPPED, | 215 STOPPED, |
| 224 SCHEDULED_WINDOW, | 216 SCHEDULED_WINDOW, |
| 225 IMMEDIATE_WINDOW, | 217 IMMEDIATE_WINDOW, |
| 226 }; | 218 }; |
| 227 | 219 |
| 228 // Receives the results of a get from the request queue, and turns that into | 220 // Receives the results of a get from the request queue, and turns that into |
| 229 // SavePageRequest objects for the caller of GetQueuedRequests. | 221 // SavePageRequest objects for the caller of GetQueuedRequests. |
| 230 void GetQueuedRequestsCallback( | 222 void GetQueuedRequestsCallback( |
| 231 const GetRequestsCallback& callback, | 223 const GetRequestsCallback& callback, |
| 232 RequestQueue::GetRequestsResult result, | 224 GetRequestsResult result, |
| 233 std::vector<std::unique_ptr<SavePageRequest>> requests); | 225 std::vector<std::unique_ptr<SavePageRequest>> requests); |
| 234 | 226 |
| 235 // Receives the results of a get from the request queue, and turns that into | 227 // Receives the results of a get from the request queue, and turns that into |
| 236 // SavePageRequest objects for the caller of GetQueuedRequests. | 228 // SavePageRequest objects for the caller of GetQueuedRequests. |
| 237 void GetRequestsForSchedulingCallback( | 229 void GetRequestsForSchedulingCallback( |
| 238 RequestQueue::GetRequestsResult result, | 230 GetRequestsResult result, |
| 239 std::vector<std::unique_ptr<SavePageRequest>> requests); | 231 std::vector<std::unique_ptr<SavePageRequest>> requests); |
| 240 | 232 |
| 241 // Receives the result of add requests to the request queue. | 233 // Receives the result of add requests to the request queue. |
| 242 void AddRequestResultCallback(RequestQueue::AddRequestResult result, | 234 void AddRequestResultCallback(AddRequestResult result, |
| 243 const SavePageRequest& request); | 235 const SavePageRequest& request); |
| 244 | 236 |
| 245 // Receives the result of mark attempt completed requests. | 237 // Receives the result of mark attempt completed requests. |
| 246 void MarkAttemptCompletedDoneCallback( | 238 void MarkAttemptCompletedDoneCallback( |
| 247 int64_t request_id, | 239 int64_t request_id, |
| 248 const ClientId& client_id, | 240 const ClientId& client_id, |
| 249 std::unique_ptr<UpdateRequestsResult> result); | 241 std::unique_ptr<UpdateRequestsResult> result); |
| 250 | 242 |
| 251 void UpdateMultipleRequestsCallback( | 243 void UpdateMultipleRequestsCallback( |
| 252 std::unique_ptr<UpdateRequestsResult> result); | 244 std::unique_ptr<UpdateRequestsResult> result); |
| 253 | 245 |
| 254 void CompletedRequestCallback(const MultipleItemStatuses& status); | 246 void CompletedRequestCallback(const MultipleItemStatuses& status); |
| 255 | 247 |
| 256 void HandleRemovedRequestsAndCallback( | 248 void HandleRemovedRequestsAndCallback( |
| 257 const RemoveRequestsCallback& callback, | 249 const RemoveRequestsCallback& callback, |
| 258 BackgroundSavePageResult status, | 250 RequestNotifier::BackgroundSavePageResult status, |
| 259 std::unique_ptr<UpdateRequestsResult> result); | 251 std::unique_ptr<UpdateRequestsResult> result); |
| 260 | 252 |
| 261 void HandleRemovedRequests(BackgroundSavePageResult status, | 253 void HandleRemovedRequests(RequestNotifier::BackgroundSavePageResult status, |
| 262 std::unique_ptr<UpdateRequestsResult> result); | 254 std::unique_ptr<UpdateRequestsResult> result); |
| 263 | 255 |
| 264 bool StartProcessingInternal(const ProcessingWindowState processing_state, | 256 bool StartProcessingInternal(const ProcessingWindowState processing_state, |
| 265 const DeviceConditions& device_conditions, | 257 const DeviceConditions& device_conditions, |
| 266 const base::Callback<void(bool)>& callback); | 258 const base::Callback<void(bool)>& callback); |
| 267 | 259 |
| 268 // Start processing now if connected (but with conservative assumption | 260 // Start processing now if connected (but with conservative assumption |
| 269 // as to other device conditions). | 261 // as to other device conditions). |
| 270 void StartImmediatelyIfConnected(); | 262 void StartImmediatelyIfConnected(); |
| 271 | 263 |
| (...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 377 std::unique_ptr<Scheduler> scheduler_; | 369 std::unique_ptr<Scheduler> scheduler_; |
| 378 // Controller of client policies. Owned. | 370 // Controller of client policies. Owned. |
| 379 std::unique_ptr<ClientPolicyController> policy_controller_; | 371 std::unique_ptr<ClientPolicyController> policy_controller_; |
| 380 // Unowned pointer to the Network Quality Estimator. | 372 // Unowned pointer to the Network Quality Estimator. |
| 381 net::NetworkQualityEstimator::NetworkQualityProvider* | 373 net::NetworkQualityEstimator::NetworkQualityProvider* |
| 382 network_quality_estimator_; | 374 network_quality_estimator_; |
| 383 // Holds copy of the active request, if any. | 375 // Holds copy of the active request, if any. |
| 384 std::unique_ptr<SavePageRequest> active_request_; | 376 std::unique_ptr<SavePageRequest> active_request_; |
| 385 // Status of the most recent offlining. | 377 // Status of the most recent offlining. |
| 386 Offliner::RequestStatus last_offlining_status_; | 378 Offliner::RequestStatus last_offlining_status_; |
| 387 // Class to choose which request to schedule next | |
| 388 std::unique_ptr<RequestPicker> picker_; | |
| 389 // A set of request_ids that we are holding off until the download manager is | 379 // A set of request_ids that we are holding off until the download manager is |
| 390 // done with them. | 380 // done with them. |
| 391 std::set<int64_t> disabled_requests_; | 381 std::set<int64_t> disabled_requests_; |
| 392 // Calling this returns to the scheduler across the JNI bridge. | 382 // Calling this returns to the scheduler across the JNI bridge. |
| 393 base::Callback<void(bool)> scheduler_callback_; | 383 base::Callback<void(bool)> scheduler_callback_; |
| 394 // Logger to record events. | 384 // Logger to record events. |
| 395 RequestCoordinatorEventLogger event_logger_; | 385 RequestCoordinatorEventLogger event_logger_; |
| 396 // Timer to watch for pre-render attempts running too long. | 386 // Timer to watch for pre-render attempts running too long. |
| 397 base::OneShotTimer watchdog_timer_; | 387 base::OneShotTimer watchdog_timer_; |
| 398 // Callback invoked when an immediate request is done (default empty). | 388 // Callback invoked when an immediate request is done (default empty). |
| 399 base::Callback<void(bool)> immediate_schedule_callback_; | 389 base::Callback<void(bool)> immediate_schedule_callback_; |
| 400 // Allows us to pass a weak pointer to callbacks. | 390 // Allows us to pass a weak pointer to callbacks. |
| 401 base::WeakPtrFactory<RequestCoordinator> weak_ptr_factory_; | 391 base::WeakPtrFactory<RequestCoordinator> weak_ptr_factory_; |
| 402 | 392 |
| 403 DISALLOW_COPY_AND_ASSIGN(RequestCoordinator); | 393 DISALLOW_COPY_AND_ASSIGN(RequestCoordinator); |
| 404 }; | 394 }; |
| 405 | 395 |
| 406 } // namespace offline_pages | 396 } // namespace offline_pages |
| 407 | 397 |
| 408 #endif // COMPONENTS_OFFLINE_PAGES_BACKGROUND_REQUEST_COORDINATOR_H_ | 398 #endif // COMPONENTS_OFFLINE_PAGES_BACKGROUND_REQUEST_COORDINATOR_H_ |
| OLD | NEW |