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> |
(...skipping 23 matching lines...) Expand all Loading... | |
34 NTPSnippetsDatabase( | 34 NTPSnippetsDatabase( |
35 const base::FilePath& database_dir, | 35 const base::FilePath& database_dir, |
36 scoped_refptr<base::SequencedTaskRunner> file_task_runner); | 36 scoped_refptr<base::SequencedTaskRunner> file_task_runner); |
37 ~NTPSnippetsDatabase(); | 37 ~NTPSnippetsDatabase(); |
38 | 38 |
39 // Returns whether the database has finished initialization. While this is | 39 // Returns whether the database has finished initialization. While this is |
40 // false, loads may already be started (they'll be serviced after | 40 // false, loads may already be started (they'll be serviced after |
41 // initialization finishes), but no updates are allowed. | 41 // initialization finishes), but no updates are allowed. |
42 bool IsInitialized() const; | 42 bool IsInitialized() const; |
43 | 43 |
44 // Returns whether the database is in an (unrecoverable) error state. If this | |
45 // is true, the database must not be used anymore | |
46 bool IsErrorState() const; | |
47 | |
48 // Set a callback to be called when the database enters an error state. | |
49 void SetErrorCallback(const base::Closure& error_callback); | |
50 | |
44 // Loads all snippets from storage and passes them to |callback|. | 51 // Loads all snippets from storage and passes them to |callback|. |
45 void LoadSnippets(const SnippetsCallback& callback); | 52 void LoadSnippets(const SnippetsCallback& callback); |
46 | 53 |
47 // Adds or updates the given snippet. | 54 // Adds or updates the given snippet. |
48 void SaveSnippet(const NTPSnippet& snippet); | 55 void SaveSnippet(const NTPSnippet& snippet); |
49 // Adds or updates all the given snippets. | 56 // Adds or updates all the given snippets. |
50 void SaveSnippets(const NTPSnippet::PtrVector& snippets); | 57 void SaveSnippets(const NTPSnippet::PtrVector& snippets); |
51 | 58 |
52 // Deletes the snippet with the given ID, and its image. | 59 // Deletes the snippet with the given ID, and its image. |
53 void DeleteSnippet(const std::string& snippet_id); | 60 void DeleteSnippet(const std::string& snippet_id); |
(...skipping 27 matching lines...) Expand all Loading... | |
81 std::unique_ptr<std::vector<SnippetProto>> entries); | 88 std::unique_ptr<std::vector<SnippetProto>> entries); |
82 void OnDatabaseSaved(bool success); | 89 void OnDatabaseSaved(bool success); |
83 | 90 |
84 // Callbacks for ProtoDatabase<SnippetImageProto> operations. | 91 // Callbacks for ProtoDatabase<SnippetImageProto> operations. |
85 void OnImageDatabaseInited(bool success); | 92 void OnImageDatabaseInited(bool success); |
86 void OnImageDatabaseLoaded(const SnippetImageCallback& callback, | 93 void OnImageDatabaseLoaded(const SnippetImageCallback& callback, |
87 bool success, | 94 bool success, |
88 std::unique_ptr<SnippetImageProto> entry); | 95 std::unique_ptr<SnippetImageProto> entry); |
89 void OnImageDatabaseSaved(bool success); | 96 void OnImageDatabaseSaved(bool success); |
90 | 97 |
98 void OnDatabaseError(); | |
99 | |
91 void ProcessPendingLoads(); | 100 void ProcessPendingLoads(); |
92 | 101 |
93 void LoadSnippetsImpl(const SnippetsCallback& callback); | 102 void LoadSnippetsImpl(const SnippetsCallback& callback); |
94 void SaveSnippetsImpl(std::unique_ptr<KeyEntryVector> entries_to_save); | 103 void SaveSnippetsImpl(std::unique_ptr<KeyEntryVector> entries_to_save); |
95 void DeleteSnippetsImpl( | 104 void DeleteSnippetsImpl( |
96 std::unique_ptr<std::vector<std::string>> keys_to_remove); | 105 std::unique_ptr<std::vector<std::string>> keys_to_remove); |
97 | 106 |
98 void LoadImageImpl(const std::string& snippet_id, | 107 void LoadImageImpl(const std::string& snippet_id, |
99 const SnippetImageCallback& callback); | 108 const SnippetImageCallback& callback); |
100 void DeleteImagesImpl( | 109 void DeleteImagesImpl( |
101 std::unique_ptr<std::vector<std::string>> keys_to_remove); | 110 std::unique_ptr<std::vector<std::string>> keys_to_remove); |
102 | 111 |
103 void ResetDatabases(); | |
104 | |
105 std::unique_ptr<leveldb_proto::ProtoDatabase<SnippetProto>> database_; | 112 std::unique_ptr<leveldb_proto::ProtoDatabase<SnippetProto>> database_; |
106 bool database_initialized_; | 113 bool database_initialized_; |
107 std::vector<SnippetsCallback> pending_snippets_callbacks_; | 114 std::vector<SnippetsCallback> pending_snippets_callbacks_; |
108 | 115 |
109 std::unique_ptr<leveldb_proto::ProtoDatabase<SnippetImageProto>> | 116 std::unique_ptr<leveldb_proto::ProtoDatabase<SnippetImageProto>> |
110 image_database_; | 117 image_database_; |
111 bool image_database_initialized_; | 118 bool image_database_initialized_; |
112 std::vector<std::pair<std::string, SnippetImageCallback>> | 119 std::vector<std::pair<std::string, SnippetImageCallback>> |
113 pending_image_callbacks_; | 120 pending_image_callbacks_; |
114 | 121 |
122 base::Closure error_callback_; | |
Bernhard Bauer
2016/06/20 17:37:46
At some point you could think about switching to a
Marc Treib
2016/06/21 08:27:48
Hm, true. I generally like explicit callbacks beca
| |
123 | |
115 base::WeakPtrFactory<NTPSnippetsDatabase> weak_ptr_factory_; | 124 base::WeakPtrFactory<NTPSnippetsDatabase> weak_ptr_factory_; |
116 | 125 |
117 DISALLOW_COPY_AND_ASSIGN(NTPSnippetsDatabase); | 126 DISALLOW_COPY_AND_ASSIGN(NTPSnippetsDatabase); |
118 }; | 127 }; |
119 | 128 |
120 } // namespace ntp_snippets | 129 } // namespace ntp_snippets |
121 | 130 |
122 #endif // COMPONENTS_NTP_SNIPPETS_NTP_SNIPPETS_DATABASE_H_ | 131 #endif // COMPONENTS_NTP_SNIPPETS_NTP_SNIPPETS_DATABASE_H_ |
OLD | NEW |