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