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

Side by Side Diff: components/ntp_snippets/ntp_snippets_database.cc

Issue 1987333003: [NTP Snippets] Persist snippets in a LevelDB instead of prefs (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: . Created 4 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 2016 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/ntp_snippets/ntp_snippets_database.h"
6
7 #include <utility>
8
9 #include "base/files/file_path.h"
10 #include "components/leveldb_proto/proto_database_impl.h"
11 #include "components/ntp_snippets/proto/ntp_snippets.pb.h"
12
13 using leveldb_proto::ProtoDatabaseImpl;
14
15 namespace {
16 // Statistics are logged to UMA with this string as part of histogram name. They
17 // can all be found under LevelDB.*.NTPSnippets. Changing this needs to
18 // synchronize with histograms.xml, AND will also become incompatible with older
19 // browsers still reporting the previous values.
20 const char kDatabaseUMAClientName[] = "NTPSnippets";
21 }
22
23 namespace ntp_snippets {
24
25 NTPSnippetsDatabase::NTPSnippetsDatabase(
26 const base::FilePath& database_dir,
27 scoped_refptr<base::SequencedTaskRunner> file_task_runner)
28 : database_(new ProtoDatabaseImpl<SnippetProto>(file_task_runner)),
Bernhard Bauer 2016/05/25 15:01:15 std::move the |file_task_runner|?
Marc Treib 2016/05/27 14:03:12 Done.
29 database_inited_(false),
30 weak_ptr_factory_(this) {
31 database_->Init(kDatabaseUMAClientName, database_dir,
32 base::Bind(&NTPSnippetsDatabase::OnDatabaseInited,
33 weak_ptr_factory_.GetWeakPtr()));
34 }
35
36 NTPSnippetsDatabase::~NTPSnippetsDatabase() {}
37
38 void NTPSnippetsDatabase::Load(const SnippetsLoadedCallback& callback) {
39 DCHECK(!callback.is_null());
40 bool callback_was_null = callback_.is_null();
Bernhard Bauer 2016/05/25 15:01:15 Couldn't we DCHECK instead that we only do one cal
Marc Treib 2016/05/27 14:03:12 Well, it changes the interface - the caller will h
41 callback_ = callback;
42 if (callback_was_null && database_ && database_inited_)
43 LoadImpl();
44 }
45
46 void NTPSnippetsDatabase::Save(const NTPSnippet& snippet) {
47 std::unique_ptr<KeyEntryVector> entries_to_save(new KeyEntryVector());
48 entries_to_save->emplace_back(snippet.id(), snippet.ToProto());
49 SaveImpl(std::move(entries_to_save));
50 }
51
52 void NTPSnippetsDatabase::Save(const NTPSnippet::PtrVector& snippets) {
53 std::unique_ptr<KeyEntryVector> entries_to_save(new KeyEntryVector());
54 for (const std::unique_ptr<NTPSnippet>& snippet : snippets)
55 entries_to_save->emplace_back(snippet->id(), snippet->ToProto());
56 SaveImpl(std::move(entries_to_save));
57 }
58
59 void NTPSnippetsDatabase::Delete(const std::string& snippet_id) {
60 DeleteImpl(base::WrapUnique(new std::vector<std::string>(1, snippet_id)));
61 }
62
63 void NTPSnippetsDatabase::Delete(const NTPSnippet::PtrVector& snippets) {
64 std::unique_ptr<std::vector<std::string>> keys_to_remove(
65 new std::vector<std::string>());
66 for (const std::unique_ptr<NTPSnippet>& snippet : snippets)
67 keys_to_remove->emplace_back(snippet->id());
68 DeleteImpl(std::move(keys_to_remove));
69 }
70
71 void NTPSnippetsDatabase::OnDatabaseInited(bool success) {
72 DCHECK(!database_inited_);
73 if (!success) {
74 DVLOG(1) << "NTPSnippetsDatabase init failed.";
75 database_.reset();
76 return;
77 }
78 database_inited_ = true;
79 if (!callback_.is_null())
80 LoadImpl();
81 }
82
83 void NTPSnippetsDatabase::OnDatabaseLoaded(
84 bool success,
85 std::unique_ptr<std::vector<SnippetProto>> entries) {
86 if (!success) {
87 DVLOG(1) << "NTPSnippetsDatabase load failed.";
88 database_.reset();
89 return;
90 }
91
92 std::unique_ptr<std::vector<std::string>> keys_to_remove(
93 new std::vector<std::string>());
94
95 NTPSnippet::PtrVector snippets;
96 for (const SnippetProto& proto : *entries) {
97 std::unique_ptr<NTPSnippet> snippet = NTPSnippet::CreateFromProto(proto);
98 if (snippet) {
99 snippets.emplace_back(std::move(snippet));
100 } else {
101 LOG(WARNING) << "Invalid proto from DB " << proto.id();
102 keys_to_remove->emplace_back(proto.id());
103 }
104 }
105
106 // We only start loading if we have a non-null callback, but it's possible
107 // that it's been cleared in the meantime.
108 DCHECK(!callback_.is_null());
109 callback_.Run(std::move(snippets));
110 callback_.Reset();
111
112 // If any of the snippet protos couldn't be converted to actual snippets,
113 // clean them up now.
114 if (!keys_to_remove->empty())
115 DeleteImpl(std::move(keys_to_remove));
116 }
117
118 void NTPSnippetsDatabase::OnDatabaseSaved(bool success) {
119 if (!success) {
120 DVLOG(1) << "NTPSnippetsDatabase save failed.";
121 database_.reset();
122 }
123 }
124
125 void NTPSnippetsDatabase::LoadImpl() {
126 DCHECK(database_);
127 DCHECK(database_inited_);
128 database_->LoadEntries(base::Bind(&NTPSnippetsDatabase::OnDatabaseLoaded,
129 weak_ptr_factory_.GetWeakPtr()));
130 }
131
132 void NTPSnippetsDatabase::SaveImpl(
133 std::unique_ptr<KeyEntryVector> entries_to_save) {
134 if (!database_ || !database_inited_)
135 return;
136
137 std::unique_ptr<std::vector<std::string>> keys_to_remove(
138 new std::vector<std::string>());
139 database_->UpdateEntries(std::move(entries_to_save),
Bernhard Bauer 2016/05/25 15:01:15 It sucks a bit that ProtoDatabase takes unique_ptr
Marc Treib 2016/05/27 14:03:12 Hm, I guess. But then it'd be easy to accidentally
140 std::move(keys_to_remove),
141 base::Bind(&NTPSnippetsDatabase::OnDatabaseSaved,
142 weak_ptr_factory_.GetWeakPtr()));
143 }
144
145 void NTPSnippetsDatabase::DeleteImpl(
146 std::unique_ptr<std::vector<std::string>> keys_to_remove) {
147 if (!database_ || !database_inited_)
148 return;
149
150 std::unique_ptr<KeyEntryVector> entries_to_save(new KeyEntryVector());
151 database_->UpdateEntries(std::move(entries_to_save),
152 std::move(keys_to_remove),
153 base::Bind(&NTPSnippetsDatabase::OnDatabaseSaved,
154 weak_ptr_factory_.GetWeakPtr()));
155 }
156
157 } // namespace ntp_snippets
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698