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

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

Issue 2541423002: [Offline pages] Switching ChangeRequestsStateTask to use RequestQueueStore::GetRequestsByIds (Closed)
Patch Set: Fixing the comment 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
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 #include "components/offline_pages/background/change_requests_state_task.h" 5 #include "components/offline_pages/background/change_requests_state_task.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 8
9 namespace offline_pages { 9 namespace offline_pages {
10 10
11 ChangeRequestsStateTask::ChangeRequestsStateTask( 11 ChangeRequestsStateTask::ChangeRequestsStateTask(
12 RequestQueueStore* store, 12 RequestQueueStore* store,
13 const std::vector<int64_t>& request_ids, 13 const std::vector<int64_t>& request_ids,
14 const SavePageRequest::RequestState new_state, 14 const SavePageRequest::RequestState new_state,
15 const RequestQueueStore::UpdateCallback& callback) 15 const RequestQueueStore::UpdateCallback& callback)
16 : store_(store), 16 : store_(store),
17 request_ids_(request_ids.begin(), request_ids.end()), 17 request_ids_(request_ids.begin(), request_ids.end()),
18 new_state_(new_state), 18 new_state_(new_state),
19 callback_(callback), 19 callback_(callback),
20 weak_ptr_factory_(this) {} 20 weak_ptr_factory_(this) {}
21 21
22 ChangeRequestsStateTask::~ChangeRequestsStateTask() {} 22 ChangeRequestsStateTask::~ChangeRequestsStateTask() {}
23 23
24 void ChangeRequestsStateTask::Run() { 24 void ChangeRequestsStateTask::Run() {
25 ReadRequests(); 25 ReadRequests();
26 } 26 }
27 27
28 void ChangeRequestsStateTask::ReadRequests() { 28 void ChangeRequestsStateTask::ReadRequests() {
29 if (request_ids_.empty()) { 29 std::vector<int64_t> request_ids(request_ids_.begin(), request_ids_.end());
30 CompleteEarly(ItemActionStatus::NOT_FOUND); 30 store_->GetRequestsByIds(request_ids,
31 base::Bind(&ChangeRequestsStateTask::UpdateRequests,
32 weak_ptr_factory_.GetWeakPtr()));
33 }
34
35 void ChangeRequestsStateTask::UpdateRequests(
36 std::unique_ptr<UpdateRequestsResult> read_result) {
37 if (read_result->store_state != StoreState::LOADED ||
38 read_result->updated_items.empty()) {
39 UpdateCompleted(std::move(read_result));
31 return; 40 return;
32 } 41 }
33 42
34 store_->GetRequests(base::Bind(&ChangeRequestsStateTask::SelectItemsToUpdate, 43 // We are only going to make an update to the items that were found. Statuses
35 weak_ptr_factory_.GetWeakPtr())); 44 // of the missing items will be added at the end.
36 }
37
38 void ChangeRequestsStateTask::SelectItemsToUpdate(
39 bool success,
40 std::vector<std::unique_ptr<SavePageRequest>> requests) {
41 if (!success) {
42 CompleteEarly(ItemActionStatus::STORE_ERROR);
43 return;
44 }
45
46 std::vector<SavePageRequest> items_to_update; 45 std::vector<SavePageRequest> items_to_update;
47 for (const auto& request : requests) { 46 for (auto request : read_result->updated_items) {
48 // If this request is in our list, update it. 47 request.set_request_state(new_state_);
49 if (request_ids_.count(request->request_id()) > 0) { 48 items_to_update.push_back(request);
50 request->set_request_state(new_state_);
51 items_to_update.push_back(*request);
52 // Items that are missing before the update will be marked as not found
53 // before the callback.
54 request_ids_.erase(request->request_id());
55 }
56 }
57
58 if (items_to_update.empty()) {
59 CompleteEarly(ItemActionStatus::NOT_FOUND);
60 return;
61 } 49 }
62 50
63 store_->UpdateRequests(items_to_update, 51 store_->UpdateRequests(items_to_update,
64 base::Bind(&ChangeRequestsStateTask::UpdateCompleted, 52 base::Bind(&ChangeRequestsStateTask::UpdateCompleted,
65 weak_ptr_factory_.GetWeakPtr())); 53 weak_ptr_factory_.GetWeakPtr()));
66 } 54 }
67 55
68 void ChangeRequestsStateTask::UpdateCompleted( 56 void ChangeRequestsStateTask::UpdateCompleted(
69 std::unique_ptr<UpdateRequestsResult> update_result) { 57 std::unique_ptr<UpdateRequestsResult> update_result) {
70 CompleteWithStatus(std::move(update_result), ItemActionStatus::NOT_FOUND); 58 // Because the first step might not have found some of the items, we should
71 } 59 // look their IDs now and include in the final result as not found.
72 60
73 void ChangeRequestsStateTask::CompleteEarly(ItemActionStatus status) { 61 // Look up the missing items by removing the items present in the result
74 // TODO(fgorski): store_->state() once implemented 62 // statuses from original list of request IDs.
75 std::unique_ptr<UpdateRequestsResult> result( 63 for (const auto& id_status_pair : update_result->item_statuses)
76 new UpdateRequestsResult(StoreState::LOADED)); 64 request_ids_.erase(id_status_pair.first);
77 CompleteWithStatus(std::move(result), status);
78 }
79 65
80 void ChangeRequestsStateTask::CompleteWithStatus( 66 // Update the final result for the items that are left in |request_ids_|, as
81 std::unique_ptr<UpdateRequestsResult> result, 67 // these are identified as being missing from the final result.
82 ItemActionStatus status) { 68 for (int64_t request_id : request_ids_) {
83 // Mark items as not found, if they are still in the request IDs set. 69 update_result->item_statuses.push_back(
84 for (int64_t request_id : request_ids_) 70 std::make_pair(request_id, ItemActionStatus::NOT_FOUND));
85 result->item_statuses.push_back(std::make_pair(request_id, status)); 71 }
86 callback_.Run(std::move(result)); 72
73 callback_.Run(std::move(update_result));
87 TaskComplete(); 74 TaskComplete();
88 } 75 }
89 76
90 } // namespace offline_pages 77 } // namespace offline_pages
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698