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

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

Issue 2881173003: Download Service : Added leveldb proto layer (Closed)
Patch Set: Removed Model::Destroy Created 3 years, 6 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/download_store.h"
6
7 #include "base/bind.h"
8 #include "base/callback_helpers.h"
9 #include "base/memory/ptr_util.h"
10 #include "components/download/internal/entry.h"
11 #include "components/download/internal/proto/entry.pb.h"
12 #include "components/download/internal/proto_conversions.h"
13 #include "components/leveldb_proto/proto_database_impl.h"
14
15 namespace download {
16
17 namespace {
18
19 const char kDatabaseClientName[] = "DownloadService";
20 using KeyVector = std::vector<std::string>;
21 using ProtoEntryVector = std::vector<protodb::Entry>;
22 using KeyProtoEntryVector = std::vector<std::pair<std::string, protodb::Entry>>;
23
24 } // namespace
25
26 DownloadStore::DownloadStore(
27 const base::FilePath& database_dir,
28 std::unique_ptr<leveldb_proto::ProtoDatabase<protodb::Entry>> db)
29 : db_(std::move(db)),
30 database_dir_(database_dir),
31 is_initialized_(false),
32 weak_factory_(this) {}
33
34 DownloadStore::~DownloadStore() = default;
35
36 bool DownloadStore::IsInitialized() {
37 return is_initialized_;
38 }
39
40 void DownloadStore::Initialize(InitCallback callback) {
41 DCHECK(!IsInitialized());
42 db_->InitWithOptions(
43 kDatabaseClientName, leveldb_proto::Options(database_dir_),
44 base::BindOnce(&DownloadStore::OnDatabaseInited,
45 weak_factory_.GetWeakPtr(), std::move(callback)));
46 }
47
48 void DownloadStore::OnDatabaseInited(InitCallback callback, bool success) {
49 if (!success) {
50 std::move(callback).Run(success, base::MakeUnique<std::vector<Entry>>());
51 return;
52 }
53
54 db_->LoadEntries(base::BindOnce(&DownloadStore::OnDatabaseLoaded,
55 weak_factory_.GetWeakPtr(),
56 std::move(callback)));
57 }
58
59 void DownloadStore::OnDatabaseLoaded(InitCallback callback,
60 bool success,
61 std::unique_ptr<ProtoEntryVector> protos) {
62 if (!success) {
63 std::move(callback).Run(success, base::MakeUnique<std::vector<Entry>>());
64 return;
65 }
66
67 auto entries = ProtoConversions::EntryVectorFromProto(std::move(protos));
68 is_initialized_ = true;
69 std::move(callback).Run(success, std::move(entries));
70 }
71
72 void DownloadStore::Update(const Entry& entry, StoreCallback callback) {
73 DCHECK(IsInitialized());
74 auto entries_to_save = base::MakeUnique<KeyProtoEntryVector>();
75 protodb::Entry proto = ProtoConversions::EntryToProto(entry);
76 entries_to_save->emplace_back(entry.guid, std::move(proto));
77 db_->UpdateEntries(std::move(entries_to_save), base::MakeUnique<KeyVector>(),
78 std::move(callback));
79 }
80
81 void DownloadStore::Remove(const std::string& guid, StoreCallback callback) {
82 DCHECK(IsInitialized());
83 auto keys_to_remove = base::MakeUnique<KeyVector>();
84 keys_to_remove->push_back(guid);
85 db_->UpdateEntries(base::MakeUnique<KeyProtoEntryVector>(),
86 std::move(keys_to_remove), std::move(callback));
87 }
88
89 } // namespace download
OLDNEW
« no previous file with comments | « components/download/internal/download_store.h ('k') | components/download/internal/download_store_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698