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/change_requests_state_task.h" | |
6 | |
7 #include "base/bind.h" | |
8 | |
9 namespace offline_pages { | |
10 | |
11 ChangeRequestsStateTask::ChangeRequestsStateTask( | |
12 RequestQueueStore* store, | |
13 const std::vector<int64_t>& request_ids, | |
14 const SavePageRequest::RequestState new_state, | |
15 const RequestQueueStore::UpdateCallback& callback) | |
16 : store_(store), | |
17 request_ids_(request_ids.begin(), request_ids.end()), | |
18 new_state_(new_state), | |
19 callback_(callback), | |
20 weak_ptr_factory_(this) {} | |
21 | |
22 ChangeRequestsStateTask::~ChangeRequestsStateTask() {} | |
23 | |
24 void ChangeRequestsStateTask::Run() { | |
25 ReadRequests(); | |
26 } | |
27 | |
28 void ChangeRequestsStateTask::ReadRequests() { | |
29 std::vector<int64_t> request_ids(request_ids_.begin(), request_ids_.end()); | |
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)); | |
40 return; | |
41 } | |
42 | |
43 // We are only going to make an update to the items that were found. Statuses | |
44 // of the missing items will be added at the end. | |
45 std::vector<SavePageRequest> items_to_update; | |
46 for (auto request : read_result->updated_items) { | |
47 request.set_request_state(new_state_); | |
48 items_to_update.push_back(request); | |
49 } | |
50 | |
51 store_->UpdateRequests(items_to_update, | |
52 base::Bind(&ChangeRequestsStateTask::UpdateCompleted, | |
53 weak_ptr_factory_.GetWeakPtr())); | |
54 } | |
55 | |
56 void ChangeRequestsStateTask::UpdateCompleted( | |
57 std::unique_ptr<UpdateRequestsResult> update_result) { | |
58 // Because the first step might not have found some of the items, we should | |
59 // look their IDs now and include in the final result as not found. | |
60 | |
61 // Look up the missing items by removing the items present in the result | |
62 // statuses from original list of request IDs. | |
63 for (const auto& id_status_pair : update_result->item_statuses) | |
64 request_ids_.erase(id_status_pair.first); | |
65 | |
66 // Update the final result for the items that are left in |request_ids_|, as | |
67 // these are identified as being missing from the final result. | |
68 for (int64_t request_id : request_ids_) { | |
69 update_result->item_statuses.push_back( | |
70 std::make_pair(request_id, ItemActionStatus::NOT_FOUND)); | |
71 } | |
72 | |
73 callback_.Run(std::move(update_result)); | |
74 TaskComplete(); | |
75 } | |
76 | |
77 } // namespace offline_pages | |
OLD | NEW |