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