Chromium Code Reviews| 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 <string> |
| 10 #include <vector> | 10 #include <vector> |
| 11 | 11 |
| 12 #include "base/callback.h" | 12 #include "base/callback.h" |
| 13 #include "base/macros.h" | 13 #include "base/macros.h" |
| 14 #include "base/memory/ref_counted.h" | 14 #include "base/memory/ref_counted.h" |
| 15 #include "base/memory/weak_ptr.h" | 15 #include "base/memory/weak_ptr.h" |
| 16 #include "base/sequenced_task_runner.h" | 16 #include "base/sequenced_task_runner.h" |
| 17 #include "components/leveldb_proto/proto_database.h" | 17 #include "components/leveldb_proto/proto_database.h" |
| 18 #include "components/ntp_snippets/ntp_snippet.h" | 18 #include "components/ntp_snippets/ntp_snippet.h" |
| 19 | 19 |
| 20 namespace base { | 20 namespace base { |
| 21 class FilePath; | 21 class FilePath; |
| 22 } | 22 } |
| 23 | 23 |
| 24 namespace ntp_snippets { | 24 namespace ntp_snippets { |
| 25 | 25 |
| 26 class SnippetImageProto; | |
| 26 class SnippetProto; | 27 class SnippetProto; |
| 27 | 28 |
| 28 class NTPSnippetsDatabase { | 29 class NTPSnippetsDatabase { |
| 29 public: | 30 public: |
| 30 using SnippetsLoadedCallback = base::Callback<void(NTPSnippet::PtrVector)>; | 31 using SnippetsCallback = base::Callback<void(NTPSnippet::PtrVector)>; |
| 32 using SnippetImageCallback = base::Callback<void(std::string)>; | |
| 31 | 33 |
| 32 NTPSnippetsDatabase( | 34 NTPSnippetsDatabase( |
| 33 const base::FilePath& database_dir, | 35 const base::FilePath& database_dir, |
| 34 scoped_refptr<base::SequencedTaskRunner> file_task_runner); | 36 scoped_refptr<base::SequencedTaskRunner> file_task_runner); |
| 35 ~NTPSnippetsDatabase(); | 37 ~NTPSnippetsDatabase(); |
| 36 | 38 |
| 39 // Returns whether the database has finished initialization. While this is | |
| 40 // false, loads may already be started (they'll be serviced after | |
| 41 // initialization finishes), but any updates are ignored. | |
| 42 bool IsInitialized() const; | |
| 43 | |
| 37 // Loads all snippets from storage and passes them to |callback|. | 44 // Loads all snippets from storage and passes them to |callback|. |
| 38 void Load(const SnippetsLoadedCallback& callback); | 45 void LoadSnippets(const SnippetsCallback& callback); |
| 39 | 46 |
| 40 // Adds or updates the given snippet. | 47 // Adds or updates the given snippet. |
| 41 void Save(const NTPSnippet& snippet); | 48 void SaveSnippet(const NTPSnippet& snippet); |
| 42 // Adds or updates all the given snippets. | 49 // Adds or updates all the given snippets. |
| 43 void Save(const NTPSnippet::PtrVector& snippets); | 50 void SaveSnippets(const NTPSnippet::PtrVector& snippets); |
| 44 | 51 |
| 45 // Deletes the snippet with the given ID. | 52 // Deletes the snippet with the given ID, and its image. |
| 46 void Delete(const std::string& snippet_id); | 53 void DeleteSnippet(const std::string& snippet_id); |
| 47 // Deletes all the given snippets (identified by their IDs). | 54 // Deletes all the given snippets (identified by their IDs) and their images. |
| 48 void Delete(const NTPSnippet::PtrVector& snippets); | 55 void DeleteSnippets(const NTPSnippet::PtrVector& snippets); |
| 56 | |
| 57 // Loads the image data for the snippet with the given ID and passes it to | |
| 58 // |callback|. Passes an empty string if not found. | |
| 59 void LoadImage(const std::string& snippet_id, | |
| 60 const SnippetImageCallback& callback); | |
| 61 | |
| 62 // Adds or updates the image data for the given snippet ID. | |
| 63 void SaveImage(const std::string& snippet_id, const std::string& image_data); | |
| 64 | |
| 65 // Deletes the image data for the given snippet ID. | |
| 66 void DeleteImage(const std::string& snippet_id); | |
| 49 | 67 |
| 50 private: | 68 private: |
| 51 friend class NTPSnippetsDatabaseTest; | 69 friend class NTPSnippetsDatabaseTest; |
| 52 | 70 |
| 53 using KeyEntryVector = | 71 using KeyEntryVector = |
| 54 leveldb_proto::ProtoDatabase<SnippetProto>::KeyEntryVector; | 72 leveldb_proto::ProtoDatabase<SnippetProto>::KeyEntryVector; |
| 55 | 73 |
| 56 // Callbacks for ProtoDatabase operations. | 74 using ImageKeyEntryVector = |
| 75 leveldb_proto::ProtoDatabase<SnippetImageProto>::KeyEntryVector; | |
| 76 | |
| 77 // Callbacks for ProtoDatabase<SnippetProto> operations. | |
| 57 void OnDatabaseInited(bool success); | 78 void OnDatabaseInited(bool success); |
| 58 void OnDatabaseLoaded(const SnippetsLoadedCallback& callback, | 79 void OnDatabaseLoaded(const SnippetsCallback& callback, |
| 59 bool success, | 80 bool success, |
| 60 std::unique_ptr<std::vector<SnippetProto>> entries); | 81 std::unique_ptr<std::vector<SnippetProto>> entries); |
| 61 void OnDatabaseSaved(bool success); | 82 void OnDatabaseSaved(bool success); |
| 62 | 83 |
| 63 void LoadImpl(const SnippetsLoadedCallback& callback); | 84 // Callbacks for ProtoDatabase<SnippetImageProto> operations. |
| 85 void OnImageDatabaseInited(bool success); | |
| 86 void OnImageDatabaseLoaded(const SnippetImageCallback& callback, | |
| 87 bool success, | |
| 88 std::unique_ptr<SnippetImageProto> entry); | |
| 89 void OnImageDatabaseSaved(bool success); | |
| 64 | 90 |
| 65 void SaveImpl(std::unique_ptr<KeyEntryVector> entries_to_save); | 91 void ProcessPendingLoads(); |
| 66 | 92 |
| 67 void DeleteImpl(std::unique_ptr<std::vector<std::string>> keys_to_remove); | 93 void LoadSnippetsImpl(const SnippetsCallback& callback); |
| 94 void SaveSnippetsImpl(std::unique_ptr<KeyEntryVector> entries_to_save); | |
| 95 void DeleteSnippetsImpl( | |
| 96 std::unique_ptr<std::vector<std::string>> keys_to_remove); | |
| 97 | |
| 98 void LoadImageImpl(const std::string& snippet_id, | |
| 99 const SnippetImageCallback& callback); | |
| 100 void DeleteImagesImpl( | |
| 101 std::unique_ptr<std::vector<std::string>> keys_to_remove); | |
| 68 | 102 |
| 69 std::unique_ptr<leveldb_proto::ProtoDatabase<SnippetProto>> database_; | 103 std::unique_ptr<leveldb_proto::ProtoDatabase<SnippetProto>> database_; |
| 70 bool database_initialized_; | 104 bool database_initialized_; |
| 105 std::vector<SnippetsCallback> pending_snippets_callbacks_; | |
| 71 | 106 |
| 72 std::vector<SnippetsLoadedCallback> pending_load_callbacks_; | 107 std::unique_ptr<leveldb_proto::ProtoDatabase<SnippetImageProto>> |
| 108 image_database_; | |
|
Bernhard Bauer
2016/06/15 16:11:43
Do we actually need to have these in the same clas
Marc Treib
2016/06/16 08:00:16
Well, "need" is such a strong word ;)
I do think i
| |
| 109 bool image_database_initialized_; | |
| 110 std::vector<std::pair<std::string, SnippetImageCallback>> | |
| 111 pending_image_callbacks_; | |
| 73 | 112 |
| 74 base::WeakPtrFactory<NTPSnippetsDatabase> weak_ptr_factory_; | 113 base::WeakPtrFactory<NTPSnippetsDatabase> weak_ptr_factory_; |
| 75 | 114 |
| 76 DISALLOW_COPY_AND_ASSIGN(NTPSnippetsDatabase); | 115 DISALLOW_COPY_AND_ASSIGN(NTPSnippetsDatabase); |
| 77 }; | 116 }; |
| 78 | 117 |
| 79 } // namespace ntp_snippets | 118 } // namespace ntp_snippets |
| 80 | 119 |
| 81 #endif // COMPONENTS_NTP_SNIPPETS_NTP_SNIPPETS_DATABASE_H_ | 120 #endif // COMPONENTS_NTP_SNIPPETS_NTP_SNIPPETS_DATABASE_H_ |
| OLD | NEW |