Chromium Code Reviews| 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..122e0956d7da88fc39a449f393a6a71eb30e32a3 |
| --- /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; if there was already a fetch ongoing, the |
| + // previous callback will be replaced. |
| + 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; |
|
Bernhard Bauer
2016/05/25 15:01:15
Out of curiosity: could we just use `using leveldb
Marc Treib
2016/05/27 14:03:12
No - apparently that only works if leveldb_proto::
|
| + |
| + // 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_inited_; |
|
Bernhard Bauer
2016/05/25 15:01:15
Nit: Can we use "initialized"?
Marc Treib
2016/05/27 14:03:12
Done.
|
| + |
| + SnippetsLoadedCallback callback_; |
| + |
| + base::WeakPtrFactory<NTPSnippetsDatabase> weak_ptr_factory_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(NTPSnippetsDatabase); |
| +}; |
| + |
| +} // namespace ntp_snippets |
| + |
| +#endif // COMPONENTS_NTP_SNIPPETS_NTP_SNIPPETS_DATABASE_H_ |