OLD | NEW |
---|---|
(Empty) | |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "components/download/internal/model_impl.h" | |
6 | |
7 #include "base/bind.h" | |
8 #include "base/memory/ptr_util.h" | |
9 #include "components/download/internal/entry.h" | |
10 | |
11 namespace download { | |
12 | |
13 ModelImpl::ModelImpl(Client* client, std::unique_ptr<Store> store) | |
14 : client_(client), store_(std::move(store)), weak_ptr_factory_(this) { | |
15 DCHECK(client_); | |
16 DCHECK(store_); | |
17 } | |
18 | |
19 ModelImpl::~ModelImpl() = default; | |
20 | |
21 void ModelImpl::Initialize() { | |
22 DCHECK(!store_->IsInitialized()); | |
23 store_->Initialize(base::Bind(&ModelImpl::OnInitializedFinished, | |
24 weak_ptr_factory_.GetWeakPtr())); | |
25 } | |
26 | |
27 void ModelImpl::Destroy() { | |
28 store_->Destroy(base::Bind(&ModelImpl::OnDestroyFinished, | |
29 weak_ptr_factory_.GetWeakPtr())); | |
30 } | |
31 | |
32 void ModelImpl::Add(const Entry& entry) { | |
33 DCHECK(entries_.find(entry.guid) == entries_.end()); | |
34 | |
35 entries_.insert(OwnedEntryPair(entry.guid, base::MakeUnique<Entry>(entry))); | |
xingliu
2017/05/09 06:11:41
nit: maybe consider std::map::emplace.
David Trainor- moved to gerrit
2017/05/15 15:59:51
Done.
| |
36 store_->Update(entry, base::Bind(&ModelImpl::OnAddFinished, | |
37 weak_ptr_factory_.GetWeakPtr(), entry.client, | |
38 entry.guid)); | |
39 } | |
40 | |
41 void ModelImpl::Update(const Entry& entry) { | |
42 // TODO(dtrainor): Should we validate that no updates can occur to an entry | |
43 // that is currently in the process of being added? | |
Peter Beverloo
2017/05/10 12:44:45
While the Client definitely should wait for the ac
David Trainor- moved to gerrit
2017/05/15 15:59:51
Yeah I can probably remove this now that it's simp
| |
44 DCHECK(entries_.find(entry.guid) != entries_.end()); | |
45 | |
46 entries_[entry.guid] = base::MakeUnique<Entry>(entry); | |
47 store_->Update(entry, base::Bind(&ModelImpl::OnUpdateFinished, | |
48 weak_ptr_factory_.GetWeakPtr(), entry.client, | |
49 entry.guid)); | |
50 } | |
51 | |
52 void ModelImpl::Remove(const std::string& guid) { | |
53 const auto& it = entries_.find(guid); | |
54 DCHECK(it != entries_.end()); | |
55 | |
56 DownloadClient client = it->second->client; | |
57 entries_.erase(it); | |
58 store_->Remove(guid, | |
59 base::Bind(&ModelImpl::OnRemoveFinished, | |
60 weak_ptr_factory_.GetWeakPtr(), client, guid)); | |
61 } | |
62 | |
63 Entry* ModelImpl::Get(const std::string& guid) { | |
64 const auto& it = entries_.find(guid); | |
65 return it == entries_.end() ? nullptr : it->second.get(); | |
66 } | |
67 | |
68 Model::EntryList ModelImpl::PeekEntries() { | |
69 EntryList entries; | |
70 for (const auto& it : entries_) | |
71 entries.push_back(it.second.get()); | |
72 | |
73 return entries; | |
74 } | |
75 | |
76 void ModelImpl::OnInitializedFinished( | |
77 bool success, | |
78 std::unique_ptr<std::vector<Entry>> entries) { | |
79 if (!success) { | |
80 client_->OnInitialized(false); | |
81 return; | |
82 } | |
83 | |
84 for (const auto& entry : *entries) { | |
85 entries_.insert(OwnedEntryPair(entry.guid, base::MakeUnique<Entry>(entry))); | |
86 } | |
87 client_->OnInitialized(true); | |
88 } | |
89 | |
90 void ModelImpl::OnDestroyFinished(bool success) { | |
91 store_.reset(); | |
92 entries_.clear(); | |
93 client_->OnDestroyed(success); | |
94 } | |
95 | |
96 void ModelImpl::OnAddFinished(DownloadClient client, | |
97 const std::string& guid, | |
98 bool success) { | |
99 // TODO(dtrainor): If the add fails, should we automatically remove it from | |
100 // this map? | |
Peter Beverloo
2017/05/10 12:44:45
I'd argue so. Current behaviour is:
auto model =
David Trainor- moved to gerrit
2017/05/15 15:59:51
I think the hard part is:
model->Add(entry) --> as
| |
101 client_->OnItemAdded(success, client, guid); | |
102 } | |
103 | |
104 void ModelImpl::OnUpdateFinished(DownloadClient client, | |
105 const std::string& guid, | |
106 bool success) { | |
107 if (!success) | |
108 client_->OnStoreUpdateFailed(client, guid); | |
109 } | |
110 | |
111 void ModelImpl::OnRemoveFinished(DownloadClient client, | |
112 const std::string& guid, | |
113 bool success) { | |
114 if (!success) | |
115 client_->OnStoreUpdateFailed(client, guid); | |
116 } | |
117 | |
118 } // namespace download | |
OLD | NEW |