| OLD | NEW |
| (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 #ifndef COMPONENTS_NTP_SNIPPETS_NTP_SNIPPETS_DATABASE_H_ |
| 6 #define COMPONENTS_NTP_SNIPPETS_NTP_SNIPPETS_DATABASE_H_ |
| 7 |
| 8 #include <memory> |
| 9 #include <vector> |
| 10 |
| 11 #include "base/callback.h" |
| 12 #include "base/macros.h" |
| 13 #include "base/memory/ref_counted.h" |
| 14 #include "base/memory/weak_ptr.h" |
| 15 #include "base/sequenced_task_runner.h" |
| 16 #include "components/leveldb_proto/proto_database.h" |
| 17 #include "components/ntp_snippets/ntp_snippet.h" |
| 18 |
| 19 namespace base { |
| 20 class FilePath; |
| 21 } |
| 22 |
| 23 namespace ntp_snippets { |
| 24 |
| 25 class SnippetProto; |
| 26 |
| 27 class NTPSnippetsDatabase { |
| 28 public: |
| 29 using SnippetsLoadedCallback = base::Callback<void(NTPSnippet::PtrVector)>; |
| 30 |
| 31 NTPSnippetsDatabase( |
| 32 const base::FilePath& database_dir, |
| 33 scoped_refptr<base::SequencedTaskRunner> file_task_runner); |
| 34 ~NTPSnippetsDatabase(); |
| 35 |
| 36 // Loads all snippets from storage and passes them to |callback|. Only one |
| 37 // concurrent fetch is supported; if there was already a fetch ongoing, the |
| 38 // previous callback will be replaced. |
| 39 void Load(const SnippetsLoadedCallback& callback); |
| 40 |
| 41 // Adds or updates the given snippet. |
| 42 void Save(const NTPSnippet& snippet); |
| 43 // Adds or updates all the given snippets. |
| 44 void Save(const NTPSnippet::PtrVector& snippets); |
| 45 |
| 46 // Deletes the snippet with the given ID. |
| 47 void Delete(const std::string& snippet_id); |
| 48 // Deletes all the given snippets (identified by their IDs). |
| 49 void Delete(const NTPSnippet::PtrVector& snippets); |
| 50 |
| 51 private: |
| 52 friend class NTPSnippetsDatabaseTest; |
| 53 |
| 54 using KeyEntryVector = |
| 55 leveldb_proto::ProtoDatabase<SnippetProto>::KeyEntryVector; |
| 56 |
| 57 // Callbacks for ProtoDatabase operations. |
| 58 void OnDatabaseInited(bool success); |
| 59 void OnDatabaseLoaded(bool success, |
| 60 std::unique_ptr<std::vector<SnippetProto>> entries); |
| 61 void OnDatabaseSaved(bool success); |
| 62 |
| 63 void LoadImpl(); |
| 64 |
| 65 void SaveImpl(std::unique_ptr<KeyEntryVector> entries_to_save); |
| 66 |
| 67 void DeleteImpl(std::unique_ptr<std::vector<std::string>> keys_to_remove); |
| 68 |
| 69 std::unique_ptr<leveldb_proto::ProtoDatabase<SnippetProto>> database_; |
| 70 bool database_inited_; |
| 71 |
| 72 SnippetsLoadedCallback callback_; |
| 73 |
| 74 base::WeakPtrFactory<NTPSnippetsDatabase> weak_ptr_factory_; |
| 75 |
| 76 DISALLOW_COPY_AND_ASSIGN(NTPSnippetsDatabase); |
| 77 }; |
| 78 |
| 79 } // namespace ntp_snippets |
| 80 |
| 81 #endif // COMPONENTS_NTP_SNIPPETS_NTP_SNIPPETS_DATABASE_H_ |
| OLD | NEW |