| OLD | NEW |
| 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/mark_attempt_started_task.h" | 5 #include "components/offline_pages/background/mark_attempt_started_task.h" |
| 6 | 6 |
| 7 #include <vector> | |
| 8 | |
| 9 #include "base/bind.h" | 7 #include "base/bind.h" |
| 10 #include "base/time/time.h" | 8 #include "base/time/time.h" |
| 11 | 9 |
| 12 namespace offline_pages { | 10 namespace offline_pages { |
| 13 | 11 |
| 14 MarkAttemptStartedTask::MarkAttemptStartedTask( | 12 MarkAttemptStartedTask::MarkAttemptStartedTask( |
| 15 RequestQueueStore* store, | 13 RequestQueueStore* store, |
| 16 int64_t request_id, | 14 int64_t request_id, |
| 17 const RequestQueueStore::UpdateCallback& callback) | 15 const RequestQueueStore::UpdateCallback& callback) |
| 18 : store_(store), | 16 : UpdateRequestTask(store, request_id, callback) {} |
| 19 request_id_(request_id), | |
| 20 callback_(callback), | |
| 21 weak_ptr_factory_(this) {} | |
| 22 | 17 |
| 23 MarkAttemptStartedTask::~MarkAttemptStartedTask() {} | 18 MarkAttemptStartedTask::~MarkAttemptStartedTask() {} |
| 24 | 19 |
| 25 void MarkAttemptStartedTask::Run() { | 20 void MarkAttemptStartedTask::UpdateRequestImpl( |
| 26 ReadRequest(); | |
| 27 } | |
| 28 | |
| 29 void MarkAttemptStartedTask::ReadRequest() { | |
| 30 std::vector<int64_t> request_ids{request_id_}; | |
| 31 store_->GetRequestsByIds( | |
| 32 request_ids, base::Bind(&MarkAttemptStartedTask::MarkAttemptStarted, | |
| 33 weak_ptr_factory_.GetWeakPtr())); | |
| 34 } | |
| 35 | |
| 36 void MarkAttemptStartedTask::MarkAttemptStarted( | |
| 37 std::unique_ptr<UpdateRequestsResult> read_result) { | 21 std::unique_ptr<UpdateRequestsResult> read_result) { |
| 38 if (read_result->store_state != StoreState::LOADED || | 22 if (!ValidateReadResult(read_result.get())) { |
| 39 read_result->item_statuses.front().second != ItemActionStatus::SUCCESS || | |
| 40 read_result->updated_items.size() != 1) { | |
| 41 CompleteWithResult(std::move(read_result)); | 23 CompleteWithResult(std::move(read_result)); |
| 42 return; | 24 return; |
| 43 } | 25 } |
| 44 | 26 |
| 45 // It is perfectly fine to reuse the read_result->updated_items collection, as | 27 // It is perfectly fine to reuse the read_result->updated_items collection, as |
| 46 // it is owned by this callback and will be destroyed when out of scope. | 28 // it is owned by this callback and will be destroyed when out of scope. |
| 47 read_result->updated_items[0].MarkAttemptStarted(base::Time::Now()); | 29 read_result->updated_items[0].MarkAttemptStarted(base::Time::Now()); |
| 48 store_->UpdateRequests(read_result->updated_items, | 30 store()->UpdateRequests( |
| 49 base::Bind(&MarkAttemptStartedTask::CompleteWithResult, | 31 read_result->updated_items, |
| 50 weak_ptr_factory_.GetWeakPtr())); | 32 base::Bind(&MarkAttemptStartedTask::CompleteWithResult, GetWeakPtr())); |
| 51 } | |
| 52 | |
| 53 void MarkAttemptStartedTask::CompleteWithResult( | |
| 54 std::unique_ptr<UpdateRequestsResult> result) { | |
| 55 callback_.Run(std::move(result)); | |
| 56 TaskComplete(); | |
| 57 } | 33 } |
| 58 | 34 |
| 59 } // namespace offline_pages | 35 } // namespace offline_pages |
| OLD | NEW |