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

Unified Diff: components/download/internal/model_impl.cc

Issue 2870753003: Add a Model to the download component. (Closed)
Patch Set: 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 side-by-side diff with in-line comments
Download patch
Index: components/download/internal/model_impl.cc
diff --git a/components/download/internal/model_impl.cc b/components/download/internal/model_impl.cc
new file mode 100644
index 0000000000000000000000000000000000000000..d8137fcc8bf6063fa34ae11726bef563effbea6f
--- /dev/null
+++ b/components/download/internal/model_impl.cc
@@ -0,0 +1,118 @@
+// Copyright 2017 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "components/download/internal/model_impl.h"
+
+#include "base/bind.h"
+#include "base/memory/ptr_util.h"
+#include "components/download/internal/entry.h"
+
+namespace download {
+
+ModelImpl::ModelImpl(Client* client, std::unique_ptr<Store> store)
+ : client_(client), store_(std::move(store)), weak_ptr_factory_(this) {
+ DCHECK(client_);
+ DCHECK(store_);
+}
+
+ModelImpl::~ModelImpl() = default;
+
+void ModelImpl::Initialize() {
+ DCHECK(!store_->IsInitialized());
+ store_->Initialize(base::Bind(&ModelImpl::OnInitializedFinished,
+ weak_ptr_factory_.GetWeakPtr()));
+}
+
+void ModelImpl::Destroy() {
+ store_->Destroy(base::Bind(&ModelImpl::OnDestroyFinished,
+ weak_ptr_factory_.GetWeakPtr()));
+}
+
+void ModelImpl::Add(const Entry& entry) {
+ DCHECK(entries_.find(entry.guid) == entries_.end());
+
+ 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.
+ store_->Update(entry, base::Bind(&ModelImpl::OnAddFinished,
+ weak_ptr_factory_.GetWeakPtr(), entry.client,
+ entry.guid));
+}
+
+void ModelImpl::Update(const Entry& entry) {
+ // TODO(dtrainor): Should we validate that no updates can occur to an entry
+ // 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
+ DCHECK(entries_.find(entry.guid) != entries_.end());
+
+ entries_[entry.guid] = base::MakeUnique<Entry>(entry);
+ store_->Update(entry, base::Bind(&ModelImpl::OnUpdateFinished,
+ weak_ptr_factory_.GetWeakPtr(), entry.client,
+ entry.guid));
+}
+
+void ModelImpl::Remove(const std::string& guid) {
+ const auto& it = entries_.find(guid);
+ DCHECK(it != entries_.end());
+
+ DownloadClient client = it->second->client;
+ entries_.erase(it);
+ store_->Remove(guid,
+ base::Bind(&ModelImpl::OnRemoveFinished,
+ weak_ptr_factory_.GetWeakPtr(), client, guid));
+}
+
+Entry* ModelImpl::Get(const std::string& guid) {
+ const auto& it = entries_.find(guid);
+ return it == entries_.end() ? nullptr : it->second.get();
+}
+
+Model::EntryList ModelImpl::PeekEntries() {
+ EntryList entries;
+ for (const auto& it : entries_)
+ entries.push_back(it.second.get());
+
+ return entries;
+}
+
+void ModelImpl::OnInitializedFinished(
+ bool success,
+ std::unique_ptr<std::vector<Entry>> entries) {
+ if (!success) {
+ client_->OnInitialized(false);
+ return;
+ }
+
+ for (const auto& entry : *entries) {
+ entries_.insert(OwnedEntryPair(entry.guid, base::MakeUnique<Entry>(entry)));
+ }
+ client_->OnInitialized(true);
+}
+
+void ModelImpl::OnDestroyFinished(bool success) {
+ store_.reset();
+ entries_.clear();
+ client_->OnDestroyed(success);
+}
+
+void ModelImpl::OnAddFinished(DownloadClient client,
+ const std::string& guid,
+ bool success) {
+ // TODO(dtrainor): If the add fails, should we automatically remove it from
+ // 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
+ client_->OnItemAdded(success, client, guid);
+}
+
+void ModelImpl::OnUpdateFinished(DownloadClient client,
+ const std::string& guid,
+ bool success) {
+ if (!success)
+ client_->OnStoreUpdateFailed(client, guid);
+}
+
+void ModelImpl::OnRemoveFinished(DownloadClient client,
+ const std::string& guid,
+ bool success) {
+ if (!success)
+ client_->OnStoreUpdateFailed(client, guid);
+}
+
+} // namespace download

Powered by Google App Engine
This is Rietveld 408576698