| 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/mark_attempt_started_task.h" |
| 6 |
| 7 #include <vector> |
| 8 |
| 9 #include "base/bind.h" |
| 10 #include "base/time/time.h" |
| 11 |
| 12 namespace offline_pages { |
| 13 |
| 14 MarkAttemptStartedTask::MarkAttemptStartedTask( |
| 15 RequestQueueStore* store, |
| 16 int64_t request_id, |
| 17 const RequestQueueStore::UpdateCallback& callback) |
| 18 : store_(store), |
| 19 request_id_(request_id), |
| 20 callback_(callback), |
| 21 weak_ptr_factory_(this) {} |
| 22 |
| 23 MarkAttemptStartedTask::~MarkAttemptStartedTask() {} |
| 24 |
| 25 void MarkAttemptStartedTask::Run() { |
| 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) { |
| 38 if (read_result->store_state != StoreState::LOADED || |
| 39 read_result->item_statuses.front().second != ItemActionStatus::SUCCESS || |
| 40 read_result->updated_items.size() != 1) { |
| 41 CompleteWithResult(std::move(read_result)); |
| 42 return; |
| 43 } |
| 44 |
| 45 // 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. |
| 47 read_result->updated_items[0].MarkAttemptStarted(base::Time::Now()); |
| 48 store_->UpdateRequests(read_result->updated_items, |
| 49 base::Bind(&MarkAttemptStartedTask::CompleteWithResult, |
| 50 weak_ptr_factory_.GetWeakPtr())); |
| 51 } |
| 52 |
| 53 void MarkAttemptStartedTask::CompleteWithResult( |
| 54 std::unique_ptr<UpdateRequestsResult> result) { |
| 55 callback_.Run(std::move(result)); |
| 56 TaskComplete(); |
| 57 } |
| 58 |
| 59 } // namespace offline_pages |
| OLD | NEW |