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

Unified Diff: components/ntp_snippets/ntp_snippets_database.h

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: fix test memleaks 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « components/ntp_snippets/ntp_snippets_constants.cc ('k') | components/ntp_snippets/ntp_snippets_database.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: components/ntp_snippets/ntp_snippets_database.h
diff --git a/components/ntp_snippets/ntp_snippets_database.h b/components/ntp_snippets/ntp_snippets_database.h
new file mode 100644
index 0000000000000000000000000000000000000000..24e98d7b1e513897c350a61a6deeb35e43334d70
--- /dev/null
+++ b/components/ntp_snippets/ntp_snippets_database.h
@@ -0,0 +1,81 @@
+// Copyright 2016 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.
+
+#ifndef COMPONENTS_NTP_SNIPPETS_NTP_SNIPPETS_DATABASE_H_
+#define COMPONENTS_NTP_SNIPPETS_NTP_SNIPPETS_DATABASE_H_
+
+#include <memory>
+#include <vector>
+
+#include "base/callback.h"
+#include "base/macros.h"
+#include "base/memory/ref_counted.h"
+#include "base/memory/weak_ptr.h"
+#include "base/sequenced_task_runner.h"
+#include "components/leveldb_proto/proto_database.h"
+#include "components/ntp_snippets/ntp_snippet.h"
+
+namespace base {
+class FilePath;
+}
+
+namespace ntp_snippets {
+
+class SnippetProto;
+
+class NTPSnippetsDatabase {
+ public:
+ using SnippetsLoadedCallback = base::Callback<void(NTPSnippet::PtrVector)>;
+
+ NTPSnippetsDatabase(
+ const base::FilePath& database_dir,
+ scoped_refptr<base::SequencedTaskRunner> file_task_runner);
+ ~NTPSnippetsDatabase();
+
+ // Loads all snippets from storage and passes them to |callback|. Only one
+ // concurrent fetch is supported; it is only legal to call this again after
+ // the first load has completed.
+ void Load(const SnippetsLoadedCallback& callback);
+
+ // Adds or updates the given snippet.
+ void Save(const NTPSnippet& snippet);
+ // Adds or updates all the given snippets.
+ void Save(const NTPSnippet::PtrVector& snippets);
+
+ // Deletes the snippet with the given ID.
+ void Delete(const std::string& snippet_id);
+ // Deletes all the given snippets (identified by their IDs).
+ void Delete(const NTPSnippet::PtrVector& snippets);
+
+ private:
+ friend class NTPSnippetsDatabaseTest;
+
+ using KeyEntryVector =
+ leveldb_proto::ProtoDatabase<SnippetProto>::KeyEntryVector;
+
+ // Callbacks for ProtoDatabase operations.
+ void OnDatabaseInited(bool success);
+ void OnDatabaseLoaded(bool success,
+ std::unique_ptr<std::vector<SnippetProto>> entries);
+ void OnDatabaseSaved(bool success);
+
+ void LoadImpl();
+
+ void SaveImpl(std::unique_ptr<KeyEntryVector> entries_to_save);
+
+ void DeleteImpl(std::unique_ptr<std::vector<std::string>> keys_to_remove);
+
+ std::unique_ptr<leveldb_proto::ProtoDatabase<SnippetProto>> database_;
+ bool database_initialized_;
+
+ SnippetsLoadedCallback callback_;
+
+ base::WeakPtrFactory<NTPSnippetsDatabase> weak_ptr_factory_;
+
+ DISALLOW_COPY_AND_ASSIGN(NTPSnippetsDatabase);
+};
+
+} // namespace ntp_snippets
+
+#endif // COMPONENTS_NTP_SNIPPETS_NTP_SNIPPETS_DATABASE_H_
« no previous file with comments | « components/ntp_snippets/ntp_snippets_constants.cc ('k') | components/ntp_snippets/ntp_snippets_database.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698