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/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 const char kDatabaseClientName[] = "DownloadService"; | |
David Trainor- moved to gerrit
2017/05/23 06:18:00
put these in an anonymous namespace inside downloa
| |
17 | |
18 using KeyVector = std::vector<std::string>; | |
19 using ProtoEntryVector = std::vector<protodb::Entry>; | |
20 using KeyProtoEntryVector = std::vector<std::pair<std::string, protodb::Entry>>; | |
21 | |
22 DownloadStore::DownloadStore( | |
23 const base::FilePath& database_dir, | |
24 std::unique_ptr<leveldb_proto::ProtoDatabase<protodb::Entry>> db) | |
25 : db_(std::move(db)), | |
26 database_dir_(database_dir), | |
27 is_initialized_(false), | |
28 weak_factory_(this) {} | |
29 | |
30 DownloadStore::~DownloadStore() = default; | |
31 | |
32 bool DownloadStore::IsInitialized() { | |
33 return is_initialized_; | |
34 } | |
35 | |
36 void DownloadStore::Initialize(const InitCallback& callback) { | |
37 DCHECK(!IsInitialized()); | |
38 db_->InitWithOptions(kDatabaseClientName, | |
39 leveldb_proto::Options(database_dir_), | |
40 base::Bind(&DownloadStore::OnDatabaseInited, | |
41 weak_factory_.GetWeakPtr(), callback)); | |
42 } | |
43 | |
44 void DownloadStore::OnDatabaseInited(const InitCallback& callback, | |
45 bool success) { | |
46 if (!success) { | |
47 callback.Run(success, base::MakeUnique<std::vector<Entry>>()); | |
48 return; | |
49 } | |
50 | |
51 db_->LoadEntries(base::Bind(&DownloadStore::OnDatabaseLoaded, | |
52 weak_factory_.GetWeakPtr(), callback)); | |
53 } | |
54 | |
55 void DownloadStore::OnDatabaseLoaded(const InitCallback& callback, | |
56 bool success, | |
57 std::unique_ptr<ProtoEntryVector> protos) { | |
58 if (!success) { | |
59 callback.Run(success, base::MakeUnique<std::vector<Entry>>()); | |
60 return; | |
61 } | |
62 | |
63 auto entries = ProtoConversions::EntryVectorFromProto(std::move(protos)); | |
64 is_initialized_ = true; | |
65 callback.Run(success, std::move(entries)); | |
66 } | |
67 | |
68 void DownloadStore::Destroy(const StoreCallback& callback) { | |
69 is_initialized_ = false; | |
70 db_->Destroy(callback); | |
71 } | |
72 | |
73 void DownloadStore::Update(const Entry& entry, const StoreCallback& callback) { | |
74 DCHECK(IsInitialized()); | |
75 auto entries_to_save = base::MakeUnique<KeyProtoEntryVector>(); | |
76 protodb::Entry proto = ProtoConversions::EntryToProto(entry); | |
77 entries_to_save->emplace_back(entry.guid, std::move(proto)); | |
78 db_->UpdateEntries(std::move(entries_to_save), base::MakeUnique<KeyVector>(), | |
79 callback); | |
80 } | |
81 | |
82 void DownloadStore::Remove(const std::string& guid, | |
83 const StoreCallback& callback) { | |
84 DCHECK(IsInitialized()); | |
85 auto keys_to_remove = base::MakeUnique<KeyVector>(); | |
86 keys_to_remove->push_back(guid); | |
87 db_->UpdateEntries(base::MakeUnique<KeyProtoEntryVector>(), | |
88 std::move(keys_to_remove), callback); | |
89 } | |
90 | |
91 } // namespace download | |
OLD | NEW |