Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(506)

Side by Side Diff: components/download/internal/model_impl.cc

Issue 2870753003: Add a Model to the download component. (Closed)
Patch Set: Renamed test base class Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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(store_->IsInitialized());
34 DCHECK(entries_.find(entry.guid) == entries_.end());
35
36 entries_.emplace(entry.guid, base::MakeUnique<Entry>(entry));
37
38 store_->Update(entry, base::Bind(&ModelImpl::OnAddFinished,
39 weak_ptr_factory_.GetWeakPtr(), entry.client,
40 entry.guid));
41 }
42
43 void ModelImpl::Update(const Entry& entry) {
44 DCHECK(store_->IsInitialized());
45 DCHECK(entries_.find(entry.guid) != entries_.end());
46
47 entries_[entry.guid] = base::MakeUnique<Entry>(entry);
48 store_->Update(entry, base::Bind(&ModelImpl::OnUpdateFinished,
49 weak_ptr_factory_.GetWeakPtr(), entry.client,
50 entry.guid));
51 }
52
53 void ModelImpl::Remove(const std::string& guid) {
54 DCHECK(store_->IsInitialized());
55
56 const auto& it = entries_.find(guid);
57 DCHECK(it != entries_.end());
58
59 DownloadClient client = it->second->client;
60 entries_.erase(it);
61 store_->Remove(guid,
62 base::Bind(&ModelImpl::OnRemoveFinished,
63 weak_ptr_factory_.GetWeakPtr(), client, guid));
64 }
65
66 Entry* ModelImpl::Get(const std::string& guid) {
67 const auto& it = entries_.find(guid);
68 return it == entries_.end() ? nullptr : it->second.get();
69 }
70
71 Model::EntryList ModelImpl::PeekEntries() {
72 EntryList entries;
73 for (const auto& it : entries_)
74 entries.push_back(it.second.get());
75
76 return entries;
77 }
78
79 void ModelImpl::OnInitializedFinished(
80 bool success,
81 std::unique_ptr<std::vector<Entry>> entries) {
82 if (!success) {
83 client_->OnInitialized(false);
84 return;
85 }
86
87 for (const auto& entry : *entries)
88 entries_.emplace(entry.guid, base::MakeUnique<Entry>(entry));
89
90 client_->OnInitialized(true);
91 }
92
93 void ModelImpl::OnDestroyFinished(bool success) {
94 store_.reset();
95 entries_.clear();
96 client_->OnDestroyed(success);
97 }
98
99 void ModelImpl::OnAddFinished(DownloadClient client,
100 const std::string& guid,
101 bool success) {
102 // Don't notify the Client if the entry was already removed.
103 if (entries_.find(guid) == entries_.end())
104 return;
105
106 // Remove the entry from the map if the add failed.
107 if (!success)
108 entries_.erase(guid);
109
110 client_->OnItemAdded(success, client, guid);
111 }
112
113 void ModelImpl::OnUpdateFinished(DownloadClient client,
114 const std::string& guid,
115 bool success) {
116 // Don't notify the Client if the entry was already removed.
117 if (entries_.find(guid) == entries_.end())
118 return;
119
120 client_->OnItemUpdated(success, client, guid);
121 }
122
123 void ModelImpl::OnRemoveFinished(DownloadClient client,
124 const std::string& guid,
125 bool success) {
126 DCHECK(entries_.find(guid) == entries_.end());
127 client_->OnItemRemoved(success, client, guid);
128 }
129
130 } // namespace download
OLDNEW
« no previous file with comments | « components/download/internal/model_impl.h ('k') | components/download/internal/model_impl_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698