Chromium Code Reviews| 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. | |
|
fgorski
2016/11/09 17:58:07
ownership transfer is implied by the parameter typ
Pete Williamson
2016/11/09 23:53:56
Done.
| |
| 100 void SetPickerFactory(std::unique_ptr<PickRequestTaskFactory> factory) { | |
|
fgorski
2016/11/09 17:58:07
why is the factory injected by setter?
Pete Williamson
2016/11/09 23:53:56
I made a design choice to not require a PickerFact
| |
| 101 picker_factory_ = std::move(factory); | |
| 102 } | |
| 103 | |
| 106 private: | 104 private: |
| 107 // Callback used by |PurgeRequests|. | 105 // Callback used by |PurgeRequests|. |
| 108 typedef base::Callback<void(UpdateRequestResult, | 106 typedef base::Callback<void(UpdateRequestResult, |
| 109 int /* removed requests count */)> | 107 int /* removed requests count */)> |
| 110 PurgeRequestsCallback; | 108 PurgeRequestsCallback; |
| 111 | 109 |
| 112 // Purges the queue, removing the requests that are no longer relevant, e.g. | 110 // Purges the queue, removing the requests that are no longer relevant, e.g. |
| 113 // expired request. Result is returned through |callback| carries the number | 111 // expired request. Result is returned through |callback| carries the number |
| 114 // of removed requests. | 112 // of removed requests. |
| 115 void PurgeRequests(const PurgeRequestsCallback& callback); | 113 void PurgeRequests(const PurgeRequestsCallback& callback); |
| 116 | 114 |
| 117 std::unique_ptr<RequestQueueStore> store_; | 115 std::unique_ptr<RequestQueueStore> store_; |
| 118 | 116 |
| 119 // Task queue to serialize store access. | 117 // Task queue to serialize store access. |
| 120 TaskQueue task_queue_; | 118 TaskQueue task_queue_; |
| 121 | 119 |
| 120 // Builds PickRequestTask objects | |
|
fgorski
2016/11/09 17:58:07
nit: please add period at the end.
Pete Williamson
2016/11/09 23:53:56
Done.
| |
| 121 std::unique_ptr<PickRequestTaskFactory> picker_factory_; | |
| 122 | |
| 122 // Allows us to pass a weak pointer to callbacks. | 123 // Allows us to pass a weak pointer to callbacks. |
| 123 base::WeakPtrFactory<RequestQueue> weak_ptr_factory_; | 124 base::WeakPtrFactory<RequestQueue> weak_ptr_factory_; |
| 124 | 125 |
| 125 DISALLOW_COPY_AND_ASSIGN(RequestQueue); | 126 DISALLOW_COPY_AND_ASSIGN(RequestQueue); |
| 126 }; | 127 }; |
| 127 | 128 |
| 128 } // namespace offline_pages | 129 } // namespace offline_pages |
| 129 | 130 |
| 130 #endif // COMPONENTS_OFFLINE_PAGES_BACKGROUND_REQUEST_QUEUE_H_ | 131 #endif // COMPONENTS_OFFLINE_PAGES_BACKGROUND_REQUEST_QUEUE_H_ |
| OLD | NEW |