OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "components/offline_pages/background/request_queue.h" | |
6 | |
7 #include <utility> | |
8 | |
9 #include "base/bind.h" | |
10 #include "base/location.h" | |
11 #include "base/threading/thread_task_runner_handle.h" | |
12 #include "components/offline_pages/background/change_requests_state_task.h" | |
13 #include "components/offline_pages/background/mark_attempt_aborted_task.h" | |
14 #include "components/offline_pages/background/mark_attempt_completed_task.h" | |
15 #include "components/offline_pages/background/mark_attempt_started_task.h" | |
16 #include "components/offline_pages/background/pick_request_task.h" | |
17 #include "components/offline_pages/background/pick_request_task_factory.h" | |
18 #include "components/offline_pages/background/remove_requests_task.h" | |
19 #include "components/offline_pages/background/request_queue_store.h" | |
20 #include "components/offline_pages/background/save_page_request.h" | |
21 | |
22 namespace offline_pages { | |
23 | |
24 namespace { | |
25 // Completes the get requests call. | |
26 void GetRequestsDone(const RequestQueue::GetRequestsCallback& callback, | |
27 bool success, | |
28 std::vector<std::unique_ptr<SavePageRequest>> requests) { | |
29 GetRequestsResult result = | |
30 success ? GetRequestsResult::SUCCESS : GetRequestsResult::STORE_FAILURE; | |
31 // TODO(fgorski): Filter out expired requests based on policy. | |
32 // This may trigger the purging if necessary. | |
33 // Also this may be turned into a method on the request queue or add a policy | |
34 // parameter in the process. | |
35 callback.Run(result, std::move(requests)); | |
36 } | |
37 | |
38 // Completes the add request call. | |
39 void AddRequestDone(const RequestQueue::AddRequestCallback& callback, | |
40 const SavePageRequest& request, | |
41 ItemActionStatus status) { | |
42 AddRequestResult result; | |
43 switch (status) { | |
44 case ItemActionStatus::SUCCESS: | |
45 result = AddRequestResult::SUCCESS; | |
46 break; | |
47 case ItemActionStatus::ALREADY_EXISTS: | |
48 result = AddRequestResult::ALREADY_EXISTS; | |
49 break; | |
50 case ItemActionStatus::STORE_ERROR: | |
51 result = AddRequestResult::STORE_FAILURE; | |
52 break; | |
53 case ItemActionStatus::NOT_FOUND: | |
54 default: | |
55 NOTREACHED(); | |
56 return; | |
57 } | |
58 callback.Run(result, request); | |
59 } | |
60 | |
61 } // namespace | |
62 | |
63 RequestQueue::RequestQueue(std::unique_ptr<RequestQueueStore> store) | |
64 : store_(std::move(store)), weak_ptr_factory_(this) {} | |
65 | |
66 RequestQueue::~RequestQueue() {} | |
67 | |
68 void RequestQueue::GetRequests(const GetRequestsCallback& callback) { | |
69 store_->GetRequests(base::Bind(&GetRequestsDone, callback)); | |
70 } | |
71 | |
72 void RequestQueue::AddRequest(const SavePageRequest& request, | |
73 const AddRequestCallback& callback) { | |
74 // TODO(fgorski): check that request makes sense. | |
75 // TODO(fgorski): check that request does not violate policy. | |
76 store_->AddRequest(request, base::Bind(&AddRequestDone, callback, request)); | |
77 } | |
78 | |
79 void RequestQueue::RemoveRequests(const std::vector<int64_t>& request_ids, | |
80 const UpdateCallback& callback) { | |
81 std::unique_ptr<Task> task( | |
82 new RemoveRequestsTask(store_.get(), request_ids, callback)); | |
83 task_queue_.AddTask(std::move(task)); | |
84 } | |
85 | |
86 void RequestQueue::ChangeRequestsState( | |
87 const std::vector<int64_t>& request_ids, | |
88 const SavePageRequest::RequestState new_state, | |
89 const RequestQueue::UpdateCallback& callback) { | |
90 std::unique_ptr<Task> task(new ChangeRequestsStateTask( | |
91 store_.get(), request_ids, new_state, callback)); | |
92 task_queue_.AddTask(std::move(task)); | |
93 } | |
94 | |
95 void RequestQueue::MarkAttemptStarted(int64_t request_id, | |
96 const UpdateCallback& callback) { | |
97 std::unique_ptr<Task> task( | |
98 new MarkAttemptStartedTask(store_.get(), request_id, callback)); | |
99 task_queue_.AddTask(std::move(task)); | |
100 } | |
101 | |
102 void RequestQueue::MarkAttemptAborted(int64_t request_id, | |
103 const UpdateCallback& callback) { | |
104 std::unique_ptr<Task> task( | |
105 new MarkAttemptAbortedTask(store_.get(), request_id, callback)); | |
106 task_queue_.AddTask(std::move(task)); | |
107 } | |
108 | |
109 void RequestQueue::MarkAttemptCompleted(int64_t request_id, | |
110 const UpdateCallback& callback) { | |
111 std::unique_ptr<Task> task( | |
112 new MarkAttemptCompletedTask(store_.get(), request_id, callback)); | |
113 task_queue_.AddTask(std::move(task)); | |
114 } | |
115 | |
116 void RequestQueue::PurgeRequests(const PurgeRequestsCallback& callback) {} | |
117 | |
118 void RequestQueue::PickNextRequest( | |
119 PickRequestTask::RequestPickedCallback picked_callback, | |
120 PickRequestTask::RequestNotPickedCallback not_picked_callback, | |
121 PickRequestTask::RequestCountCallback request_count_callback, | |
122 DeviceConditions& conditions, | |
123 std::set<int64_t>& disabled_requests) { | |
124 // Using the PickerContext, create a picker task. | |
125 std::unique_ptr<Task> task(picker_factory_->CreatePickerTask( | |
126 store_.get(), picked_callback, not_picked_callback, | |
127 request_count_callback, conditions, disabled_requests)); | |
128 | |
129 // Queue up the picking task, it will call one of the callbacks when it | |
130 // completes. | |
131 task_queue_.AddTask(std::move(task)); | |
132 } | |
133 | |
134 } // namespace offline_pages | |
OLD | NEW |