| 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/observer_list.h" |
| 14 #include "base/time/time.h" | 14 #include "base/time/time.h" |
| 15 #include "base/timer/timer.h" | 15 #include "base/timer/timer.h" |
| 16 #include "components/keyed_service/core/keyed_service.h" | 16 #include "components/keyed_service/core/keyed_service.h" |
| 17 #include "components/offline_pages/background/device_conditions.h" | 17 #include "components/offline_pages/background/device_conditions.h" |
| 18 #include "components/offline_pages/background/offliner.h" | 18 #include "components/offline_pages/background/offliner.h" |
| 19 #include "components/offline_pages/background/request_coordinator_event_logger.h
" | 19 #include "components/offline_pages/background/request_coordinator_event_logger.h
" |
| 20 #include "components/offline_pages/background/request_notifier.h" |
| 20 #include "components/offline_pages/background/request_queue.h" | 21 #include "components/offline_pages/background/request_queue.h" |
| 21 #include "components/offline_pages/background/scheduler.h" | 22 #include "components/offline_pages/background/scheduler.h" |
| 22 #include "url/gurl.h" | 23 #include "url/gurl.h" |
| 23 | 24 |
| 24 namespace offline_pages { | 25 namespace offline_pages { |
| 25 | 26 |
| 26 struct ClientId; | 27 struct ClientId; |
| 27 class OfflinerPolicy; | 28 class OfflinerPolicy; |
| 28 class OfflinerFactory; | 29 class OfflinerFactory; |
| 29 class Offliner; | 30 class Offliner; |
| 30 class RequestPicker; | 31 class RequestPicker; |
| 31 class SavePageRequest; | 32 class SavePageRequest; |
| 32 class Scheduler; | 33 class Scheduler; |
| 33 | 34 |
| 34 // Coordinates queueing and processing save page later requests. | 35 // Coordinates queueing and processing save page later requests. |
| 35 class RequestCoordinator : public KeyedService { | 36 class RequestCoordinator : public KeyedService, public RequestNotifier { |
| 36 public: | 37 public: |
| 37 // Status to return for failed notifications. | |
| 38 // TODO(petewil): Can we find a better name for this enum? | |
| 39 enum class SavePageStatus { | |
| 40 SUCCESS, | |
| 41 PRERENDER_FAILURE, | |
| 42 FOREGROUND_CANCELED, | |
| 43 SAVE_FAILED, | |
| 44 EXPIRED, | |
| 45 RETRY_COUNT_EXCEEDED, | |
| 46 REMOVED, | |
| 47 }; | |
| 48 | 38 |
| 49 // Nested observer class. To make sure that no events are missed, the client | 39 // Nested observer class. To make sure that no events are missed, the client |
| 50 // code should first register for notifications, then |GetAllRequests|, and | 40 // code should first register for notifications, then |GetAllRequests|, and |
| 51 // ignore all events before the return from |GetAllRequests|, and consume | 41 // ignore all events before the return from |GetAllRequests|, and consume |
| 52 // events after the return callback from |GetAllRequests|. | 42 // events after the return callback from |GetAllRequests|. |
| 53 class Observer { | 43 class Observer { |
| 54 public: | 44 public: |
| 55 virtual void OnAdded(const SavePageRequest& request) = 0; | 45 virtual void OnAdded(const SavePageRequest& request) = 0; |
| 56 virtual void OnCompleted(const SavePageRequest& request, | 46 virtual void OnCompleted(const SavePageRequest& request, |
| 57 SavePageStatus status) = 0; | 47 RequestNotifier::SavePageStatus status) = 0; |
| 58 virtual void OnChanged(const SavePageRequest& request) = 0; | 48 virtual void OnChanged(const SavePageRequest& request) = 0; |
| 59 }; | 49 }; |
| 60 | 50 |
| 61 // 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. |
| 62 typedef base::Callback<void(const SavePageRequest& request)> | 52 typedef base::Callback<void(const SavePageRequest& request)> |
| 63 RequestPickedCallback; | 53 RequestPickedCallback; |
| 64 typedef base::Callback<void()> RequestQueueEmptyCallback; | 54 typedef base::Callback<void()> RequestQueueEmptyCallback; |
| 65 | 55 |
| 66 RequestCoordinator(std::unique_ptr<OfflinerPolicy> policy, | 56 RequestCoordinator(std::unique_ptr<OfflinerPolicy> policy, |
| 67 std::unique_ptr<OfflinerFactory> factory, | 57 std::unique_ptr<OfflinerFactory> factory, |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 117 } | 107 } |
| 118 | 108 |
| 119 // Observers implementing the RequestCoordinator::Observer interface can | 109 // Observers implementing the RequestCoordinator::Observer interface can |
| 120 // register here to get notifications of changes to request state. This | 110 // register here to get notifications of changes to request state. This |
| 121 // pointer is not owned, and it is the callers responsibility to remove the | 111 // pointer is not owned, and it is the callers responsibility to remove the |
| 122 // observer before the observer is deleted. | 112 // observer before the observer is deleted. |
| 123 void AddObserver(RequestCoordinator::Observer* observer); | 113 void AddObserver(RequestCoordinator::Observer* observer); |
| 124 | 114 |
| 125 void RemoveObserver(RequestCoordinator::Observer* observer); | 115 void RemoveObserver(RequestCoordinator::Observer* observer); |
| 126 | 116 |
| 117 // Implement RequestNotifier |
| 118 void NotifyAdded(const SavePageRequest& request) override; |
| 119 void NotifyCompleted(const SavePageRequest& request, |
| 120 RequestNotifier::SavePageStatus status) override; |
| 121 void NotifyChanged(const SavePageRequest& request) override; |
| 122 |
| 127 // Returns the request queue used for requests. Coordinator keeps ownership. | 123 // Returns the request queue used for requests. Coordinator keeps ownership. |
| 128 RequestQueue* queue() { return queue_.get(); } | 124 RequestQueue* queue() { return queue_.get(); } |
| 129 | 125 |
| 130 // Return an unowned pointer to the Scheduler. | 126 // Return an unowned pointer to the Scheduler. |
| 131 Scheduler* scheduler() { return scheduler_.get(); } | 127 Scheduler* scheduler() { return scheduler_.get(); } |
| 132 | 128 |
| 133 // Returns the status of the most recent offlining. | 129 // Returns the status of the most recent offlining. |
| 134 Offliner::RequestStatus last_offlining_status() { | 130 Offliner::RequestStatus last_offlining_status() { |
| 135 return last_offlining_status_; | 131 return last_offlining_status_; |
| 136 } | 132 } |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 180 | 176 |
| 181 void SendRequestToOffliner(const SavePageRequest& request); | 177 void SendRequestToOffliner(const SavePageRequest& request); |
| 182 | 178 |
| 183 // Called by the offliner when an offlining request is completed. (and by | 179 // Called by the offliner when an offlining request is completed. (and by |
| 184 // tests). | 180 // tests). |
| 185 void OfflinerDoneCallback(const SavePageRequest& request, | 181 void OfflinerDoneCallback(const SavePageRequest& request, |
| 186 Offliner::RequestStatus status); | 182 Offliner::RequestStatus status); |
| 187 | 183 |
| 188 void TryNextRequest(); | 184 void TryNextRequest(); |
| 189 | 185 |
| 190 void NotifyAdded(const SavePageRequest& request); | |
| 191 void NotifyCompleted(const SavePageRequest& request, SavePageStatus status); | |
| 192 void NotifyChanged(const SavePageRequest& request); | |
| 193 | |
| 194 // Returns the appropriate offliner to use, getting a new one from the factory | 186 // Returns the appropriate offliner to use, getting a new one from the factory |
| 195 // if needed. | 187 // if needed. |
| 196 void GetOffliner(); | 188 void GetOffliner(); |
| 197 | 189 |
| 198 void SetOfflinerTimeoutForTest(const base::TimeDelta& timeout) { | 190 void SetOfflinerTimeoutForTest(const base::TimeDelta& timeout) { |
| 199 offliner_timeout_ = timeout; | 191 offliner_timeout_ = timeout; |
| 200 } | 192 } |
| 201 | 193 |
| 202 void SetDeviceConditionsForTest(DeviceConditions& current_conditions) { | 194 void SetDeviceConditionsForTest(DeviceConditions& current_conditions) { |
| 203 current_conditions_.reset(new DeviceConditions(current_conditions)); | 195 current_conditions_.reset(new DeviceConditions(current_conditions)); |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 242 base::TimeDelta offliner_timeout_; | 234 base::TimeDelta offliner_timeout_; |
| 243 // Allows us to pass a weak pointer to callbacks. | 235 // Allows us to pass a weak pointer to callbacks. |
| 244 base::WeakPtrFactory<RequestCoordinator> weak_ptr_factory_; | 236 base::WeakPtrFactory<RequestCoordinator> weak_ptr_factory_; |
| 245 | 237 |
| 246 DISALLOW_COPY_AND_ASSIGN(RequestCoordinator); | 238 DISALLOW_COPY_AND_ASSIGN(RequestCoordinator); |
| 247 }; | 239 }; |
| 248 | 240 |
| 249 } // namespace offline_pages | 241 } // namespace offline_pages |
| 250 | 242 |
| 251 #endif // COMPONENTS_OFFLINE_PAGES_BACKGROUND_REQUEST_COORDINATOR_H_ | 243 #endif // COMPONENTS_OFFLINE_PAGES_BACKGROUND_REQUEST_COORDINATOR_H_ |
| OLD | NEW |