| 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 "components/keyed_service/core/keyed_service.h" | 13 #include "components/keyed_service/core/keyed_service.h" |
| 14 #include "components/offline_pages/background/request_queue.h" | 14 #include "components/offline_pages/background/request_queue.h" |
| 15 #include "url/gurl.h" | 15 #include "url/gurl.h" |
| 16 | 16 |
| 17 namespace offline_pages { | 17 namespace offline_pages { |
| 18 | 18 |
| 19 struct ClientId; | 19 struct ClientId; |
| 20 class OfflinerPolicy; | 20 class OfflinerPolicy; |
| 21 class OfflinerFactory; | 21 class OfflinerFactory; |
| 22 class Offliner; | 22 class Offliner; |
| 23 class SavePageRequest; | 23 class SavePageRequest; |
| 24 class Scheduler; |
| 24 | 25 |
| 25 // Coordinates queueing and processing save page later requests. | 26 // Coordinates queueing and processing save page later requests. |
| 26 class RequestCoordinator : | 27 class RequestCoordinator : |
| 27 public KeyedService, | 28 public KeyedService, |
| 28 public base::SupportsWeakPtr<RequestCoordinator> { | 29 public base::SupportsWeakPtr<RequestCoordinator> { |
| 29 public: | 30 public: |
| 30 // Callback to report when the processing of a triggered task is complete. | 31 // Callback to report when the processing of a triggered task is complete. |
| 31 typedef base::Callback<void()> ProcessingDoneCallback; | 32 typedef base::Callback<void()> ProcessingDoneCallback; |
| 32 | 33 |
| 33 RequestCoordinator(std::unique_ptr<OfflinerPolicy> policy, | 34 RequestCoordinator(std::unique_ptr<OfflinerPolicy> policy, |
| 34 std::unique_ptr<OfflinerFactory> factory, | 35 std::unique_ptr<OfflinerFactory> factory, |
| 35 std::unique_ptr<RequestQueue> queue); | 36 std::unique_ptr<RequestQueue> queue, |
| 37 std::unique_ptr<Scheduler> scheduler); |
| 36 | 38 |
| 37 ~RequestCoordinator() override; | 39 ~RequestCoordinator() override; |
| 38 | 40 |
| 39 // Queues |request| to later load and save when system conditions allow. | 41 // Queues |request| to later load and save when system conditions allow. |
| 40 // Returns true if the page could be queued successfully. | 42 // Returns true if the page could be queued successfully. |
| 41 bool SavePageLater(const GURL& url, const ClientId& client_id); | 43 bool SavePageLater(const GURL& url, const ClientId& client_id); |
| 42 | 44 |
| 43 // Starts processing of one or more queued save page later requests. | 45 // Starts processing of one or more queued save page later requests. |
| 44 // Returns whether processing was started and that caller should expect | 46 // Returns whether processing was started and that caller should expect |
| 45 // a callback. If processing was already active, returns false. | 47 // a callback. If processing was already active, returns false. |
| 46 bool StartProcessing(const ProcessingDoneCallback& callback); | 48 bool StartProcessing(const ProcessingDoneCallback& callback); |
| 47 | 49 |
| 48 // Stops the current request processing if active. This is a way for | 50 // Stops the current request processing if active. This is a way for |
| 49 // caller to abort processing; otherwise, processing will complete on | 51 // caller to abort processing; otherwise, processing will complete on |
| 50 // its own. In either case, the callback will be called when processing | 52 // its own. In either case, the callback will be called when processing |
| 51 // is stopped or complete. | 53 // is stopped or complete. |
| 52 void StopProcessing(); | 54 void StopProcessing(); |
| 53 | 55 |
| 54 // Returns the request queue used for requests. Coordinator keeps ownership. | 56 // Returns the request queue used for requests. Coordinator keeps ownership. |
| 55 RequestQueue* GetQueue() { return queue_.get(); } | 57 RequestQueue* GetQueue() { return queue_.get(); } |
| 56 | 58 |
| 59 Scheduler* GetSchedulerForTesting() { return scheduler_.get(); } |
| 60 |
| 57 private: | 61 private: |
| 58 void AddRequestResultCallback(RequestQueue::AddRequestResult result, | 62 void AddRequestResultCallback(RequestQueue::AddRequestResult result, |
| 59 const SavePageRequest& request); | 63 const SavePageRequest& request); |
| 60 // OfflinerPolicy. Used to store policies. Owned. | 64 // OfflinerPolicy. Used to store policies. Owned. |
| 61 std::unique_ptr<OfflinerPolicy> policy_; | 65 std::unique_ptr<OfflinerPolicy> policy_; |
| 62 // OfflinerFactory. Used to create offline pages. Owned. | 66 // OfflinerFactory. Used to create offline pages. Owned. |
| 63 std::unique_ptr<OfflinerFactory> factory_; | 67 std::unique_ptr<OfflinerFactory> factory_; |
| 64 // RequestQueue. Used to store incoming requests. Owned. | 68 // RequestQueue. Used to store incoming requests. Owned. |
| 65 std::unique_ptr<RequestQueue> queue_; | 69 std::unique_ptr<RequestQueue> queue_; |
| 70 // Scheduler. Used to request a callback when network is available. Owned. |
| 71 std::unique_ptr<Scheduler> scheduler_; |
| 66 | 72 |
| 67 DISALLOW_COPY_AND_ASSIGN(RequestCoordinator); | 73 DISALLOW_COPY_AND_ASSIGN(RequestCoordinator); |
| 68 }; | 74 }; |
| 69 | 75 |
| 70 } // namespace offline_pages | 76 } // namespace offline_pages |
| 71 | 77 |
| 72 #endif // COMPONENTS_OFFLINE_PAGES_BACKGROUND_REQUEST_COORDINATOR_H_ | 78 #endif // COMPONENTS_OFFLINE_PAGES_BACKGROUND_REQUEST_COORDINATOR_H_ |
| OLD | NEW |