Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(27)

Side by Side Diff: components/offline_pages/background/request_queue.h

Issue 2473553004: Request Picker task (Closed)
Patch Set: CR fixes per DougArnett and FGorski Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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_builder.h"
21 #include "components/offline_pages/background/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 PickRequestTaskBuilder;
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(QueueResults::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(QueueResults::AddRequestResult,
43 const SavePageRequest& request)>
58 AddRequestCallback; 44 AddRequestCallback;
59 45
60 // Callback used by |ChangeRequestsState|. 46 // Callback used by |ChangeRequestsState|.
61 typedef base::Callback<void(std::unique_ptr<UpdateRequestsResult>)> 47 typedef base::Callback<void(
48 std::unique_ptr<QueueResults::UpdateRequestsResult>)>
62 UpdateCallback; 49 UpdateCallback;
63 50
64 // Callback used by |UdpateRequest|. 51 // Callback used by |UdpateRequest|.
65 typedef base::Callback<void(UpdateRequestResult)> UpdateRequestCallback; 52 typedef base::Callback<void(QueueResults::UpdateRequestResult)>
53 UpdateRequestCallback;
66 54
67 explicit RequestQueue(std::unique_ptr<RequestQueueStore> store); 55 explicit RequestQueue(std::unique_ptr<RequestQueueStore> store);
68 ~RequestQueue(); 56 ~RequestQueue();
69 57
70 // Gets all of the active requests from the store. Calling this method may 58 // Gets all of the active requests from the store. Calling this method may
71 // schedule purging of the request queue. 59 // schedule purging of the request queue.
72 void GetRequests(const GetRequestsCallback& callback); 60 void GetRequests(const GetRequestsCallback& callback);
73 61
74 // Adds |request| to the request queue. Result is returned through |callback|. 62 // Adds |request| to the request queue. Result is returned through |callback|.
75 // In case adding the request violates policy, the result will fail with 63 // In case adding the request violates policy, the result will fail with
(...skipping 20 matching lines...) Expand all
96 84
97 // Marks attempt with |request_id| as aborted. Results are returned through 85 // Marks attempt with |request_id| as aborted. Results are returned through
98 // |callback|. 86 // |callback|.
99 void MarkAttemptAborted(int64_t request_id, const UpdateCallback& callback); 87 void MarkAttemptAborted(int64_t request_id, const UpdateCallback& callback);
100 88
101 // Marks attempt with |request_id| as completed. The attempt may have 89 // Marks attempt with |request_id| as completed. The attempt may have
102 // completed with either success or failure (not denoted here). Results 90 // completed with either success or failure (not denoted here). Results
103 // are returned through |callback|. 91 // are returned through |callback|.
104 void MarkAttemptCompleted(int64_t request_id, const UpdateCallback& callback); 92 void MarkAttemptCompleted(int64_t request_id, const UpdateCallback& callback);
105 93
94 // Make a task to pick the next request, and report our choice to the
95 // callbacks.
96 void PickNextRequest(
97 PickRequestTask::RequestPickedCallback picked_callback,
98 PickRequestTask::RequestNotPickedCallback not_picked_callback,
99 DeviceConditions* conditions,
100 std::set<int64_t>& disabled_requests);
101
102 // Takes ownership of the builder.
103 void SetPickerBuilder(std::unique_ptr<PickRequestTaskBuilder>& builder) {
fgorski 2016/11/04 21:41:28 don't pass unique_ptr by ref, please
Pete Williamson 2016/11/08 00:36:45 Done.
104 picker_builder_.reset(builder.release());
fgorski 2016/11/04 21:41:28 picker_builder_ = std::move(builder); Is a common
Pete Williamson 2016/11/08 00:36:45 Done.
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(QueueResults::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<PickRequestTaskBuilder> picker_builder_;
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_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698