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

Side by Side Diff: components/offline_pages/background/request_queue_in_memory_store.cc

Issue 2489443002: Move all components/offline_pages/ files into component/offline_pages/core (Closed)
Patch Set: more rebase Created 4 years 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
(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_in_memory_store.h"
6
7 #include <unordered_set>
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/save_page_request.h"
13
14 namespace offline_pages {
15
16 RequestQueueInMemoryStore::RequestQueueInMemoryStore() {}
17
18 RequestQueueInMemoryStore::~RequestQueueInMemoryStore() {}
19
20 void RequestQueueInMemoryStore::GetRequests(
21 const GetRequestsCallback& callback) {
22 std::vector<std::unique_ptr<SavePageRequest>> result_requests;
23 for (const auto& id_request_pair : requests_) {
24 std::unique_ptr<SavePageRequest> request(
25 new SavePageRequest(id_request_pair.second));
26 result_requests.push_back(std::move(request));
27 }
28 base::ThreadTaskRunnerHandle::Get()->PostTask(
29 FROM_HERE,
30 base::Bind(callback, true, base::Passed(std::move(result_requests))));
31 }
32
33 void RequestQueueInMemoryStore::GetRequestsByIds(
34 const std::vector<int64_t>& request_ids,
35 const UpdateCallback& callback) {
36 std::unique_ptr<UpdateRequestsResult> result(
37 new UpdateRequestsResult(state()));
38
39 ItemActionStatus status;
40 // Make sure not to include the same request multiple times, while preserving
41 // the order of non-duplicated IDs in the result.
42 std::unordered_set<int64_t> processed_ids;
43 for (const auto& request_id : request_ids) {
44 if (!processed_ids.insert(request_id).second)
45 continue;
46 RequestsMap::iterator iter = requests_.find(request_id);
47 if (iter != requests_.end()) {
48 status = ItemActionStatus::SUCCESS;
49 result->updated_items.push_back(iter->second);
50 } else {
51 status = ItemActionStatus::NOT_FOUND;
52 }
53 result->item_statuses.push_back(std::make_pair(request_id, status));
54 }
55
56 base::ThreadTaskRunnerHandle::Get()->PostTask(
57 FROM_HERE, base::Bind(callback, base::Passed(&result)));
58 }
59
60 void RequestQueueInMemoryStore::AddRequest(const SavePageRequest& request,
61 const AddCallback& callback) {
62 RequestsMap::iterator iter = requests_.find(request.request_id());
63 ItemActionStatus status;
64 if (iter == requests_.end()) {
65 requests_.insert(iter, std::make_pair(request.request_id(), request));
66 status = ItemActionStatus::SUCCESS;
67 } else {
68 status = ItemActionStatus::ALREADY_EXISTS;
69 }
70
71 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE,
72 base::Bind(callback, status));
73 }
74
75 void RequestQueueInMemoryStore::UpdateRequests(
76 const std::vector<SavePageRequest>& requests,
77 const RequestQueue::UpdateCallback& callback) {
78 std::unique_ptr<UpdateRequestsResult> result(
79 new UpdateRequestsResult(state()));
80
81 ItemActionStatus status;
82 for (const auto& request : requests) {
83 RequestsMap::iterator iter = requests_.find(request.request_id());
84 if (iter != requests_.end()) {
85 status = ItemActionStatus::SUCCESS;
86 iter->second = request;
87 result->updated_items.push_back(request);
88 } else {
89 status = ItemActionStatus::NOT_FOUND;
90 }
91 result->item_statuses.push_back(
92 std::make_pair(request.request_id(), status));
93 }
94
95 base::ThreadTaskRunnerHandle::Get()->PostTask(
96 FROM_HERE, base::Bind(callback, base::Passed(&result)));
97 }
98
99 void RequestQueueInMemoryStore::RemoveRequests(
100 const std::vector<int64_t>& request_ids,
101 const UpdateCallback& callback) {
102 std::unique_ptr<UpdateRequestsResult> result(
103 new UpdateRequestsResult(StoreState::LOADED));
104
105 ItemActionStatus status;
106 // If we find a request, mark it as succeeded, and put it in the request list.
107 // Otherwise mark it as failed.
108 for (auto request_id : request_ids) {
109 RequestsMap::iterator iter = requests_.find(request_id);
110 if (iter != requests_.end()) {
111 status = ItemActionStatus::SUCCESS;
112 result->updated_items.push_back(iter->second);
113 requests_.erase(iter);
114 } else {
115 status = ItemActionStatus::NOT_FOUND;
116 }
117 result->item_statuses.push_back(std::make_pair(request_id, status));
118 }
119
120 base::ThreadTaskRunnerHandle::Get()->PostTask(
121 FROM_HERE, base::Bind(callback, base::Passed(&result)));
122 }
123
124 void RequestQueueInMemoryStore::Reset(const ResetCallback& callback) {
125 requests_.clear();
126 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE,
127 base::Bind(callback, true));
128 }
129
130 StoreState RequestQueueInMemoryStore::state() const {
131 return StoreState::LOADED;
132 }
133
134 } // namespace offline_pages
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698