| 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_QUEUE_H_ | 5 #ifndef COMPONENTS_OFFLINE_PAGES_BACKGROUND_REQUEST_QUEUE_H_ |
| 6 #define COMPONENTS_OFFLINE_PAGES_BACKGROUND_REQUEST_QUEUE_H_ | 6 #define COMPONENTS_OFFLINE_PAGES_BACKGROUND_REQUEST_QUEUE_H_ |
| 7 | 7 |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 | 9 |
| 10 #include <memory> | 10 #include <memory> |
| 11 #include <set> |
| 11 #include <string> | 12 #include <string> |
| 12 #include <vector> | 13 #include <vector> |
| 13 | 14 |
| 14 #include "base/callback.h" | 15 #include "base/callback.h" |
| 15 #include "base/macros.h" | 16 #include "base/macros.h" |
| 16 #include "base/memory/weak_ptr.h" | 17 #include "base/memory/weak_ptr.h" |
| 18 #include "components/offline_pages/background/device_conditions.h" |
| 19 #include "components/offline_pages/background/pick_request_task.h" |
| 20 #include "components/offline_pages/background/pick_request_task_factory.h" |
| 21 #include "components/offline_pages/background/request_queue_results.h" |
| 17 #include "components/offline_pages/background/save_page_request.h" | 22 #include "components/offline_pages/background/save_page_request.h" |
| 18 #include "components/offline_pages/core/task_queue.h" | 23 #include "components/offline_pages/core/task_queue.h" |
| 19 #include "components/offline_pages/offline_page_item.h" | 24 #include "components/offline_pages/offline_page_item.h" |
| 20 #include "components/offline_pages/offline_store_types.h" | 25 #include "components/offline_pages/offline_store_types.h" |
| 21 | 26 |
| 22 namespace offline_pages { | 27 namespace offline_pages { |
| 23 | 28 |
| 24 class RequestQueueStore; | 29 class RequestQueueStore; |
| 25 typedef StoreUpdateResult<SavePageRequest> UpdateRequestsResult; | 30 class PickRequestTaskFactory; |
| 26 | 31 |
| 27 // Class responsible for managing save page requests. | 32 // Class responsible for managing save page requests. |
| 28 class RequestQueue { | 33 class RequestQueue { |
| 29 public: | 34 public: |
| 30 enum class GetRequestsResult { | |
| 31 SUCCESS, | |
| 32 STORE_FAILURE, | |
| 33 }; | |
| 34 | |
| 35 enum class AddRequestResult { | |
| 36 SUCCESS, | |
| 37 STORE_FAILURE, | |
| 38 ALREADY_EXISTS, | |
| 39 REQUEST_QUOTA_HIT, // Cannot add a request with this namespace, as it has | |
| 40 // reached a quota of active requests. | |
| 41 }; | |
| 42 | |
| 43 // GENERATED_JAVA_ENUM_PACKAGE:org.chromium.components.offlinepages.background | |
| 44 enum class UpdateRequestResult { | |
| 45 SUCCESS, | |
| 46 STORE_FAILURE, | |
| 47 REQUEST_DOES_NOT_EXIST, // Failed to delete the request because it does not | |
| 48 // exist. | |
| 49 }; | |
| 50 | 35 |
| 51 // Callback used for |GetRequests|. | 36 // Callback used for |GetRequests|. |
| 52 typedef base::Callback<void(GetRequestsResult, | 37 typedef base::Callback<void(GetRequestsResult, |
| 53 std::vector<std::unique_ptr<SavePageRequest>>)> | 38 std::vector<std::unique_ptr<SavePageRequest>>)> |
| 54 GetRequestsCallback; | 39 GetRequestsCallback; |
| 55 | 40 |
| 56 // Callback used for |AddRequest|. | 41 // Callback used for |AddRequest|. |
| 57 typedef base::Callback<void(AddRequestResult, const SavePageRequest& request)> | 42 typedef base::Callback<void(AddRequestResult, const SavePageRequest& request)> |
| 58 AddRequestCallback; | 43 AddRequestCallback; |
| 59 | 44 |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 96 | 81 |
| 97 // Marks attempt with |request_id| as aborted. Results are returned through | 82 // Marks attempt with |request_id| as aborted. Results are returned through |
| 98 // |callback|. | 83 // |callback|. |
| 99 void MarkAttemptAborted(int64_t request_id, const UpdateCallback& callback); | 84 void MarkAttemptAborted(int64_t request_id, const UpdateCallback& callback); |
| 100 | 85 |
| 101 // Marks attempt with |request_id| as completed. The attempt may have | 86 // Marks attempt with |request_id| as completed. The attempt may have |
| 102 // completed with either success or failure (not denoted here). Results | 87 // completed with either success or failure (not denoted here). Results |
| 103 // are returned through |callback|. | 88 // are returned through |callback|. |
| 104 void MarkAttemptCompleted(int64_t request_id, const UpdateCallback& callback); | 89 void MarkAttemptCompleted(int64_t request_id, const UpdateCallback& callback); |
| 105 | 90 |
| 91 // Make a task to pick the next request, and report our choice to the |
| 92 // callbacks. |
| 93 void PickNextRequest( |
| 94 PickRequestTask::RequestPickedCallback picked_callback, |
| 95 PickRequestTask::RequestNotPickedCallback not_picked_callback, |
| 96 DeviceConditions& conditions, |
| 97 std::set<int64_t>& disabled_requests); |
| 98 |
| 99 // Takes ownership of the factory. We use a setter to allow users of the |
| 100 // request queue to not need a PickerFactory to create it, since we have lots |
| 101 // of code using the request queue. The request coordinator will set a |
| 102 // factory before calling PickNextRequest. |
| 103 void SetPickerFactory(std::unique_ptr<PickRequestTaskFactory> factory) { |
| 104 picker_factory_ = std::move(factory); |
| 105 } |
| 106 |
| 106 private: | 107 private: |
| 107 // Callback used by |PurgeRequests|. | 108 // Callback used by |PurgeRequests|. |
| 108 typedef base::Callback<void(UpdateRequestResult, | 109 typedef base::Callback<void(UpdateRequestResult, |
| 109 int /* removed requests count */)> | 110 int /* removed requests count */)> |
| 110 PurgeRequestsCallback; | 111 PurgeRequestsCallback; |
| 111 | 112 |
| 112 // Purges the queue, removing the requests that are no longer relevant, e.g. | 113 // Purges the queue, removing the requests that are no longer relevant, e.g. |
| 113 // expired request. Result is returned through |callback| carries the number | 114 // expired request. Result is returned through |callback| carries the number |
| 114 // of removed requests. | 115 // of removed requests. |
| 115 void PurgeRequests(const PurgeRequestsCallback& callback); | 116 void PurgeRequests(const PurgeRequestsCallback& callback); |
| 116 | 117 |
| 117 std::unique_ptr<RequestQueueStore> store_; | 118 std::unique_ptr<RequestQueueStore> store_; |
| 118 | 119 |
| 119 // Task queue to serialize store access. | 120 // Task queue to serialize store access. |
| 120 TaskQueue task_queue_; | 121 TaskQueue task_queue_; |
| 121 | 122 |
| 123 // Builds PickRequestTask objects. |
| 124 std::unique_ptr<PickRequestTaskFactory> picker_factory_; |
| 125 |
| 122 // Allows us to pass a weak pointer to callbacks. | 126 // Allows us to pass a weak pointer to callbacks. |
| 123 base::WeakPtrFactory<RequestQueue> weak_ptr_factory_; | 127 base::WeakPtrFactory<RequestQueue> weak_ptr_factory_; |
| 124 | 128 |
| 125 DISALLOW_COPY_AND_ASSIGN(RequestQueue); | 129 DISALLOW_COPY_AND_ASSIGN(RequestQueue); |
| 126 }; | 130 }; |
| 127 | 131 |
| 128 } // namespace offline_pages | 132 } // namespace offline_pages |
| 129 | 133 |
| 130 #endif // COMPONENTS_OFFLINE_PAGES_BACKGROUND_REQUEST_QUEUE_H_ | 134 #endif // COMPONENTS_OFFLINE_PAGES_BACKGROUND_REQUEST_QUEUE_H_ |
| OLD | NEW |