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/request.pb.h" | |
12 #include "components/download/internal/proto_conversions.h" | |
13 #include "components/leveldb_proto/proto_database_impl.h" | |
14 | |
15 namespace { | |
David Trainor- moved to gerrit
2017/05/18 19:41:00
Put this inside namespace download too?
shaktisahu
2017/05/19 04:54:56
Done.
| |
16 const char kDatabaseClientName[] = "DownloadService"; | |
17 | |
18 using KeyVector = std::vector<std::string>; | |
19 using EntryVector = std::vector<protodb::Entry>; | |
David Trainor- moved to gerrit
2017/05/18 19:41:00
ProtoEntryVector? EntryVector sounds like std::ve
shaktisahu
2017/05/19 04:54:56
Done.
| |
20 using KeyEntryVector = std::vector<std::pair<std::string, protodb::Entry>>; | |
21 | |
22 } // namespace | |
23 | |
24 namespace download { | |
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(const InitCallback& callback) { | |
41 DCHECK(!IsInitialized()); | |
42 db_->InitWithOptions(kDatabaseClientName, | |
43 leveldb_proto::Options(database_dir_), | |
44 base::Bind(&DownloadStore::OnDatabaseInited, | |
45 weak_factory_.GetWeakPtr(), callback)); | |
46 } | |
47 | |
48 void DownloadStore::OnDatabaseInited(const InitCallback& callback, | |
49 bool success) { | |
50 if (!success) { | |
51 callback.Run(success, base::MakeUnique<std::vector<Entry>>()); | |
52 return; | |
53 } | |
54 | |
55 db_->LoadEntries(base::Bind(&DownloadStore::OnDatabaseLoaded, | |
56 weak_factory_.GetWeakPtr(), callback)); | |
57 } | |
58 | |
59 void DownloadStore::OnDatabaseLoaded(const InitCallback& callback, | |
60 bool success, | |
61 std::unique_ptr<EntryVector> protos) { | |
62 if (!success) { | |
63 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 callback.Run(success, std::move(entries)); | |
70 } | |
71 | |
72 void DownloadStore::Destroy(const StoreCallback& callback) { | |
73 is_initialized_ = false; | |
74 db_->Destroy(callback); | |
75 } | |
76 | |
77 void DownloadStore::Update(const Entry& entry, const StoreCallback& callback) { | |
78 DCHECK(IsInitialized()); | |
79 auto entries_to_save = base::MakeUnique<KeyEntryVector>(); | |
80 protodb::Entry proto = ProtoConversions::EntryToProto(entry); | |
81 entries_to_save->emplace_back(entry.guid, std::move(proto)); | |
82 db_->UpdateEntries(std::move(entries_to_save), base::MakeUnique<KeyVector>(), | |
83 callback); | |
84 } | |
85 | |
86 void DownloadStore::Remove(const std::string& guid, | |
87 const StoreCallback& callback) { | |
88 DCHECK(IsInitialized()); | |
89 auto keys_to_remove = base::MakeUnique<KeyVector>(); | |
90 keys_to_remove->push_back(guid); | |
91 db_->UpdateEntries(base::MakeUnique<KeyEntryVector>(), | |
92 std::move(keys_to_remove), callback); | |
93 } | |
94 | |
95 } // namespace download | |
OLD | NEW |