| 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::Bind(&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::Destroy() { | 31 void ModelImpl::Destroy() { |
| 28 store_->Destroy(base::Bind(&ModelImpl::OnDestroyFinished, | 32 store_->Destroy(base::Bind(&ModelImpl::OnDestroyFinished, |
| 29 weak_ptr_factory_.GetWeakPtr())); | 33 weak_ptr_factory_.GetWeakPtr())); |
| 30 } | 34 } |
| 31 | 35 |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 72 EntryList entries; | 76 EntryList entries; |
| 73 for (const auto& it : entries_) | 77 for (const auto& it : entries_) |
| 74 entries.push_back(it.second.get()); | 78 entries.push_back(it.second.get()); |
| 75 | 79 |
| 76 return entries; | 80 return entries; |
| 77 } | 81 } |
| 78 | 82 |
| 79 void ModelImpl::OnInitializedFinished( | 83 void ModelImpl::OnInitializedFinished( |
| 80 bool success, | 84 bool success, |
| 81 std::unique_ptr<std::vector<Entry>> entries) { | 85 std::unique_ptr<std::vector<Entry>> entries) { |
| 86 stats::LogModelOperationResult(stats::ModelAction::INITIALIZE, success); |
| 87 |
| 82 if (!success) { | 88 if (!success) { |
| 83 client_->OnInitialized(false); | 89 client_->OnModelReady(false); |
| 84 return; | 90 return; |
| 85 } | 91 } |
| 86 | 92 |
| 87 for (const auto& entry : *entries) | 93 for (const auto& entry : *entries) |
| 88 entries_.emplace(entry.guid, base::MakeUnique<Entry>(entry)); | 94 entries_.emplace(entry.guid, base::MakeUnique<Entry>(entry)); |
| 89 | 95 |
| 90 client_->OnInitialized(true); | 96 client_->OnModelReady(true); |
| 91 } | 97 } |
| 92 | 98 |
| 93 void ModelImpl::OnDestroyFinished(bool success) { | 99 void ModelImpl::OnDestroyFinished(bool success) { |
| 94 store_.reset(); | 100 store_.reset(); |
| 95 entries_.clear(); | 101 entries_.clear(); |
| 96 client_->OnDestroyed(success); | 102 client_->OnModelDestroyed(success); |
| 97 } | 103 } |
| 98 | 104 |
| 99 void ModelImpl::OnAddFinished(DownloadClient client, | 105 void ModelImpl::OnAddFinished(DownloadClient client, |
| 100 const std::string& guid, | 106 const std::string& guid, |
| 101 bool success) { | 107 bool success) { |
| 108 stats::LogModelOperationResult(stats::ModelAction::ADD, success); |
| 109 |
| 102 // Don't notify the Client if the entry was already removed. | 110 // Don't notify the Client if the entry was already removed. |
| 103 if (entries_.find(guid) == entries_.end()) | 111 if (entries_.find(guid) == entries_.end()) |
| 104 return; | 112 return; |
| 105 | 113 |
| 106 // Remove the entry from the map if the add failed. | 114 // Remove the entry from the map if the add failed. |
| 107 if (!success) | 115 if (!success) |
| 108 entries_.erase(guid); | 116 entries_.erase(guid); |
| 109 | 117 |
| 110 client_->OnItemAdded(success, client, guid); | 118 client_->OnItemAdded(success, client, guid); |
| 111 } | 119 } |
| 112 | 120 |
| 113 void ModelImpl::OnUpdateFinished(DownloadClient client, | 121 void ModelImpl::OnUpdateFinished(DownloadClient client, |
| 114 const std::string& guid, | 122 const std::string& guid, |
| 115 bool success) { | 123 bool success) { |
| 124 stats::LogModelOperationResult(stats::ModelAction::UPDATE, success); |
| 125 |
| 116 // Don't notify the Client if the entry was already removed. | 126 // Don't notify the Client if the entry was already removed. |
| 117 if (entries_.find(guid) == entries_.end()) | 127 if (entries_.find(guid) == entries_.end()) |
| 118 return; | 128 return; |
| 119 | 129 |
| 120 client_->OnItemUpdated(success, client, guid); | 130 client_->OnItemUpdated(success, client, guid); |
| 121 } | 131 } |
| 122 | 132 |
| 123 void ModelImpl::OnRemoveFinished(DownloadClient client, | 133 void ModelImpl::OnRemoveFinished(DownloadClient client, |
| 124 const std::string& guid, | 134 const std::string& guid, |
| 125 bool success) { | 135 bool success) { |
| 136 stats::LogModelOperationResult(stats::ModelAction::REMOVE, success); |
| 137 |
| 126 DCHECK(entries_.find(guid) == entries_.end()); | 138 DCHECK(entries_.find(guid) == entries_.end()); |
| 127 client_->OnItemRemoved(success, client, guid); | 139 client_->OnItemRemoved(success, client, guid); |
| 128 } | 140 } |
| 129 | 141 |
| 130 } // namespace download | 142 } // namespace download |
| OLD | NEW |