| 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/core/background/reconcile_task.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/logging.h" |
| 9 #include "components/offline_pages/core/background/offliner_policy.h" |
| 10 #include "components/offline_pages/core/background/request_coordinator_event_log
ger.h" |
| 11 #include "components/offline_pages/core/background/request_notifier.h" |
| 12 #include "components/offline_pages/core/background/request_queue_store.h" |
| 13 #include "components/offline_pages/core/background/save_page_request.h" |
| 14 |
| 15 namespace offline_pages { |
| 16 |
| 17 ReconcileTask::ReconcileTask(RequestQueueStore* store, |
| 18 const RequestQueueStore::UpdateCallback& callback) |
| 19 : store_(store), callback_(callback), weak_ptr_factory_(this) {} |
| 20 |
| 21 ReconcileTask::~ReconcileTask() {} |
| 22 |
| 23 void ReconcileTask::Run() { |
| 24 GetRequests(); |
| 25 } |
| 26 |
| 27 void ReconcileTask::GetRequests() { |
| 28 // Get all the requests from the queue, we will reconcile them in the |
| 29 // callback. |
| 30 store_->GetRequests( |
| 31 base::Bind(&ReconcileTask::Reconcile, weak_ptr_factory_.GetWeakPtr())); |
| 32 } |
| 33 |
| 34 void ReconcileTask::Reconcile( |
| 35 bool success, |
| 36 std::vector<std::unique_ptr<SavePageRequest>> requests) { |
| 37 // If there is nothing to do, return right away, no need to call the callback, |
| 38 // since the state of the notifications did not change. |
| 39 if (requests.empty()) { |
| 40 TaskComplete(); |
| 41 return; |
| 42 } |
| 43 |
| 44 // Check for tasks in the OFFLINING state, and change the state back to |
| 45 // AVAILABLE. |
| 46 std::vector<SavePageRequest> items_to_update; |
| 47 for (auto& request : requests) { |
| 48 if (request->request_state() == SavePageRequest::RequestState::OFFLINING) { |
| 49 request->set_request_state(SavePageRequest::RequestState::AVAILABLE); |
| 50 items_to_update.push_back(*request.get()); |
| 51 // TODO(petewil): Consider adding UMA to see how often chrome gets killed |
| 52 // while processing a request. |
| 53 } |
| 54 } |
| 55 |
| 56 // If there is no work (most common case), just return, no need for a |
| 57 // callback. |
| 58 if (items_to_update.empty()) { |
| 59 TaskComplete(); |
| 60 return; |
| 61 } |
| 62 |
| 63 store_->UpdateRequests(items_to_update, |
| 64 base::Bind(&ReconcileTask::UpdateCompleted, |
| 65 weak_ptr_factory_.GetWeakPtr())); |
| 66 } |
| 67 |
| 68 void ReconcileTask::UpdateCompleted( |
| 69 std::unique_ptr<UpdateRequestsResult> update_result) { |
| 70 // Send a notification to the UI that these items have updated. |
| 71 callback_.Run(std::move(update_result)); |
| 72 TaskComplete(); |
| 73 } |
| 74 |
| 75 } // namespace offline_pages |
| OLD | NEW |