Chromium Code Reviews| 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 | 10 |
| 11 namespace download { | 11 namespace download { |
| 12 | 12 |
| 13 ModelImpl::ModelImpl(Client* client, std::unique_ptr<Store> store) | 13 ModelImpl::ModelImpl(Client* client, std::unique_ptr<Store> store) |
| 14 : client_(client), store_(std::move(store)), weak_ptr_factory_(this) { | 14 : client_(client), store_(std::move(store)), weak_ptr_factory_(this) { |
| 15 DCHECK(client_); | 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() { |
| 22 DCHECK(!store_->IsInitialized()); | 22 DCHECK(!store_->IsInitialized()); |
| 23 store_->Initialize(base::Bind(&ModelImpl::OnInitializedFinished, | 23 store_->Initialize(base::BindOnce(&ModelImpl::OnInitializedFinished, |
| 24 weak_ptr_factory_.GetWeakPtr())); | 24 weak_ptr_factory_.GetWeakPtr())); |
| 25 } | 25 } |
| 26 | 26 |
| 27 void ModelImpl::Destroy() { | 27 void ModelImpl::Destroy() { |
|
David Trainor- moved to gerrit
2017/05/24 19:17:31
Just get rid of this as well!
| |
| 28 store_->Destroy(base::Bind(&ModelImpl::OnDestroyFinished, | 28 store_.reset(); |
| 29 weak_ptr_factory_.GetWeakPtr())); | 29 entries_.clear(); |
| 30 client_->OnDestroyed(); | |
| 30 } | 31 } |
| 31 | 32 |
| 32 void ModelImpl::Add(const Entry& entry) { | 33 void ModelImpl::Add(const Entry& entry) { |
| 33 DCHECK(store_->IsInitialized()); | 34 DCHECK(store_->IsInitialized()); |
| 34 DCHECK(entries_.find(entry.guid) == entries_.end()); | 35 DCHECK(entries_.find(entry.guid) == entries_.end()); |
| 35 | 36 |
| 36 entries_.emplace(entry.guid, base::MakeUnique<Entry>(entry)); | 37 entries_.emplace(entry.guid, base::MakeUnique<Entry>(entry)); |
| 37 | 38 |
| 38 store_->Update(entry, base::Bind(&ModelImpl::OnAddFinished, | 39 store_->Update(entry, base::BindOnce(&ModelImpl::OnAddFinished, |
| 39 weak_ptr_factory_.GetWeakPtr(), entry.client, | 40 weak_ptr_factory_.GetWeakPtr(), |
| 40 entry.guid)); | 41 entry.client, entry.guid)); |
| 41 } | 42 } |
| 42 | 43 |
| 43 void ModelImpl::Update(const Entry& entry) { | 44 void ModelImpl::Update(const Entry& entry) { |
| 44 DCHECK(store_->IsInitialized()); | 45 DCHECK(store_->IsInitialized()); |
| 45 DCHECK(entries_.find(entry.guid) != entries_.end()); | 46 DCHECK(entries_.find(entry.guid) != entries_.end()); |
| 46 | 47 |
| 47 entries_[entry.guid] = base::MakeUnique<Entry>(entry); | 48 entries_[entry.guid] = base::MakeUnique<Entry>(entry); |
| 48 store_->Update(entry, base::Bind(&ModelImpl::OnUpdateFinished, | 49 store_->Update(entry, base::BindOnce(&ModelImpl::OnUpdateFinished, |
| 49 weak_ptr_factory_.GetWeakPtr(), entry.client, | 50 weak_ptr_factory_.GetWeakPtr(), |
| 50 entry.guid)); | 51 entry.client, entry.guid)); |
| 51 } | 52 } |
| 52 | 53 |
| 53 void ModelImpl::Remove(const std::string& guid) { | 54 void ModelImpl::Remove(const std::string& guid) { |
| 54 DCHECK(store_->IsInitialized()); | 55 DCHECK(store_->IsInitialized()); |
| 55 | 56 |
| 56 const auto& it = entries_.find(guid); | 57 const auto& it = entries_.find(guid); |
| 57 DCHECK(it != entries_.end()); | 58 DCHECK(it != entries_.end()); |
| 58 | 59 |
| 59 DownloadClient client = it->second->client; | 60 DownloadClient client = it->second->client; |
| 60 entries_.erase(it); | 61 entries_.erase(it); |
| 61 store_->Remove(guid, | 62 store_->Remove(guid, |
| 62 base::Bind(&ModelImpl::OnRemoveFinished, | 63 base::BindOnce(&ModelImpl::OnRemoveFinished, |
| 63 weak_ptr_factory_.GetWeakPtr(), client, guid)); | 64 weak_ptr_factory_.GetWeakPtr(), client, guid)); |
| 64 } | 65 } |
| 65 | 66 |
| 66 Entry* ModelImpl::Get(const std::string& guid) { | 67 Entry* ModelImpl::Get(const std::string& guid) { |
| 67 const auto& it = entries_.find(guid); | 68 const auto& it = entries_.find(guid); |
| 68 return it == entries_.end() ? nullptr : it->second.get(); | 69 return it == entries_.end() ? nullptr : it->second.get(); |
| 69 } | 70 } |
| 70 | 71 |
| 71 Model::EntryList ModelImpl::PeekEntries() { | 72 Model::EntryList ModelImpl::PeekEntries() { |
| 72 EntryList entries; | 73 EntryList entries; |
| 73 for (const auto& it : entries_) | 74 for (const auto& it : entries_) |
| 74 entries.push_back(it.second.get()); | 75 entries.push_back(it.second.get()); |
| 75 | 76 |
| 76 return entries; | 77 return entries; |
| 77 } | 78 } |
| 78 | 79 |
| 79 void ModelImpl::OnInitializedFinished( | 80 void ModelImpl::OnInitializedFinished( |
| 80 bool success, | 81 bool success, |
| 81 std::unique_ptr<std::vector<Entry>> entries) { | 82 std::unique_ptr<std::vector<Entry>> entries) { |
| 82 if (!success) { | 83 if (!success) { |
| 83 client_->OnInitialized(false); | 84 client_->OnInitialized(false); |
| 84 return; | 85 return; |
| 85 } | 86 } |
| 86 | 87 |
| 87 for (const auto& entry : *entries) | 88 for (const auto& entry : *entries) |
| 88 entries_.emplace(entry.guid, base::MakeUnique<Entry>(entry)); | 89 entries_.emplace(entry.guid, base::MakeUnique<Entry>(entry)); |
| 89 | 90 |
| 90 client_->OnInitialized(true); | 91 client_->OnInitialized(true); |
| 91 } | 92 } |
| 92 | 93 |
| 93 void ModelImpl::OnDestroyFinished(bool success) { | |
| 94 store_.reset(); | |
| 95 entries_.clear(); | |
| 96 client_->OnDestroyed(success); | |
| 97 } | |
| 98 | |
| 99 void ModelImpl::OnAddFinished(DownloadClient client, | 94 void ModelImpl::OnAddFinished(DownloadClient client, |
| 100 const std::string& guid, | 95 const std::string& guid, |
| 101 bool success) { | 96 bool success) { |
| 102 // Don't notify the Client if the entry was already removed. | 97 // Don't notify the Client if the entry was already removed. |
| 103 if (entries_.find(guid) == entries_.end()) | 98 if (entries_.find(guid) == entries_.end()) |
| 104 return; | 99 return; |
| 105 | 100 |
| 106 // Remove the entry from the map if the add failed. | 101 // Remove the entry from the map if the add failed. |
| 107 if (!success) | 102 if (!success) |
| 108 entries_.erase(guid); | 103 entries_.erase(guid); |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 121 } | 116 } |
| 122 | 117 |
| 123 void ModelImpl::OnRemoveFinished(DownloadClient client, | 118 void ModelImpl::OnRemoveFinished(DownloadClient client, |
| 124 const std::string& guid, | 119 const std::string& guid, |
| 125 bool success) { | 120 bool success) { |
| 126 DCHECK(entries_.find(guid) == entries_.end()); | 121 DCHECK(entries_.find(guid) == entries_.end()); |
| 127 client_->OnItemRemoved(success, client, guid); | 122 client_->OnItemRemoved(success, client, guid); |
| 128 } | 123 } |
| 129 | 124 |
| 130 } // namespace download | 125 } // namespace download |
| OLD | NEW |