| OLD | NEW |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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/download/internal/model_impl.h" | 5 #include "components/download/internal/model_impl.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/memory/ptr_util.h" | 8 #include "base/memory/ptr_util.h" |
| 9 #include "components/download/internal/entry.h" | 9 #include "components/download/internal/entry.h" |
| 10 #include "components/download/internal/stats.h" |
| 10 | 11 |
| 11 namespace download { | 12 namespace download { |
| 12 | 13 |
| 13 ModelImpl::ModelImpl(Client* client, std::unique_ptr<Store> store) | 14 ModelImpl::ModelImpl(std::unique_ptr<Store> store) |
| 14 : client_(client), store_(std::move(store)), weak_ptr_factory_(this) { | 15 : client_(nullptr), store_(std::move(store)), weak_ptr_factory_(this) { |
| 15 DCHECK(client_); | |
| 16 DCHECK(store_); | 16 DCHECK(store_); |
| 17 } | 17 } |
| 18 | 18 |
| 19 ModelImpl::~ModelImpl() = default; | 19 ModelImpl::~ModelImpl() = default; |
| 20 | 20 |
| 21 void ModelImpl::Initialize() { | 21 void ModelImpl::Initialize(Client* client) { |
| 22 DCHECK(!client_); |
| 23 client_ = client; |
| 24 DCHECK(client_); |
| 25 |
| 22 DCHECK(!store_->IsInitialized()); | 26 DCHECK(!store_->IsInitialized()); |
| 23 store_->Initialize(base::BindOnce(&ModelImpl::OnInitializedFinished, | 27 store_->Initialize(base::Bind(&ModelImpl::OnInitializedFinished, |
| 24 weak_ptr_factory_.GetWeakPtr())); | 28 weak_ptr_factory_.GetWeakPtr())); |
| 25 } | 29 } |
| 26 | 30 |
| 27 void ModelImpl::Add(const Entry& entry) { | 31 void ModelImpl::Add(const Entry& entry) { |
| 28 DCHECK(store_->IsInitialized()); | 32 DCHECK(store_->IsInitialized()); |
| 29 DCHECK(entries_.find(entry.guid) == entries_.end()); | 33 DCHECK(entries_.find(entry.guid) == entries_.end()); |
| 30 | 34 |
| 31 entries_.emplace(entry.guid, base::MakeUnique<Entry>(entry)); | 35 entries_.emplace(entry.guid, base::MakeUnique<Entry>(entry)); |
| 32 | 36 |
| 33 store_->Update(entry, base::BindOnce(&ModelImpl::OnAddFinished, | 37 store_->Update(entry, base::BindOnce(&ModelImpl::OnAddFinished, |
| 34 weak_ptr_factory_.GetWeakPtr(), | 38 weak_ptr_factory_.GetWeakPtr(), |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 67 EntryList entries; | 71 EntryList entries; |
| 68 for (const auto& it : entries_) | 72 for (const auto& it : entries_) |
| 69 entries.push_back(it.second.get()); | 73 entries.push_back(it.second.get()); |
| 70 | 74 |
| 71 return entries; | 75 return entries; |
| 72 } | 76 } |
| 73 | 77 |
| 74 void ModelImpl::OnInitializedFinished( | 78 void ModelImpl::OnInitializedFinished( |
| 75 bool success, | 79 bool success, |
| 76 std::unique_ptr<std::vector<Entry>> entries) { | 80 std::unique_ptr<std::vector<Entry>> entries) { |
| 81 stats::LogModelOperationResult(stats::ModelAction::INITIALIZE, success); |
| 82 |
| 77 if (!success) { | 83 if (!success) { |
| 78 client_->OnInitialized(false); | 84 client_->OnModelReady(false); |
| 79 return; | 85 return; |
| 80 } | 86 } |
| 81 | 87 |
| 82 for (const auto& entry : *entries) | 88 for (const auto& entry : *entries) |
| 83 entries_.emplace(entry.guid, base::MakeUnique<Entry>(entry)); | 89 entries_.emplace(entry.guid, base::MakeUnique<Entry>(entry)); |
| 84 | 90 |
| 85 client_->OnInitialized(true); | 91 client_->OnModelReady(true); |
| 86 } | 92 } |
| 87 | 93 |
| 88 void ModelImpl::OnAddFinished(DownloadClient client, | 94 void ModelImpl::OnAddFinished(DownloadClient client, |
| 89 const std::string& guid, | 95 const std::string& guid, |
| 90 bool success) { | 96 bool success) { |
| 97 stats::LogModelOperationResult(stats::ModelAction::ADD, success); |
| 98 |
| 91 // Don't notify the Client if the entry was already removed. | 99 // Don't notify the Client if the entry was already removed. |
| 92 if (entries_.find(guid) == entries_.end()) | 100 if (entries_.find(guid) == entries_.end()) |
| 93 return; | 101 return; |
| 94 | 102 |
| 95 // Remove the entry from the map if the add failed. | 103 // Remove the entry from the map if the add failed. |
| 96 if (!success) | 104 if (!success) |
| 97 entries_.erase(guid); | 105 entries_.erase(guid); |
| 98 | 106 |
| 99 client_->OnItemAdded(success, client, guid); | 107 client_->OnItemAdded(success, client, guid); |
| 100 } | 108 } |
| 101 | 109 |
| 102 void ModelImpl::OnUpdateFinished(DownloadClient client, | 110 void ModelImpl::OnUpdateFinished(DownloadClient client, |
| 103 const std::string& guid, | 111 const std::string& guid, |
| 104 bool success) { | 112 bool success) { |
| 113 stats::LogModelOperationResult(stats::ModelAction::UPDATE, success); |
| 114 |
| 105 // Don't notify the Client if the entry was already removed. | 115 // Don't notify the Client if the entry was already removed. |
| 106 if (entries_.find(guid) == entries_.end()) | 116 if (entries_.find(guid) == entries_.end()) |
| 107 return; | 117 return; |
| 108 | 118 |
| 109 client_->OnItemUpdated(success, client, guid); | 119 client_->OnItemUpdated(success, client, guid); |
| 110 } | 120 } |
| 111 | 121 |
| 112 void ModelImpl::OnRemoveFinished(DownloadClient client, | 122 void ModelImpl::OnRemoveFinished(DownloadClient client, |
| 113 const std::string& guid, | 123 const std::string& guid, |
| 114 bool success) { | 124 bool success) { |
| 125 stats::LogModelOperationResult(stats::ModelAction::REMOVE, success); |
| 126 |
| 115 DCHECK(entries_.find(guid) == entries_.end()); | 127 DCHECK(entries_.find(guid) == entries_.end()); |
| 116 client_->OnItemRemoved(success, client, guid); | 128 client_->OnItemRemoved(success, client, guid); |
| 117 } | 129 } |
| 118 | 130 |
| 119 } // namespace download | 131 } // namespace download |
| OLD | NEW |