| 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 11 matching lines...) Expand all Loading... |
| 22 } | 22 } |
| 23 | 23 |
| 24 namespace ntp_snippets { | 24 namespace ntp_snippets { |
| 25 | 25 |
| 26 class SnippetImageProto; | 26 class SnippetImageProto; |
| 27 class SnippetProto; | 27 class SnippetProto; |
| 28 | 28 |
| 29 class NTPSnippetsDatabase { | 29 class NTPSnippetsDatabase { |
| 30 public: | 30 public: |
| 31 using SnippetsCallback = base::Callback<void(NTPSnippet::PtrVector)>; | 31 using SnippetsCallback = base::Callback<void(NTPSnippet::PtrVector)>; |
| 32 using SnippetImageCallback = base::Callback<void(std::string)>; | 32 using SnippetImageCallback = base::Callback<void(std::string suggestion_id)>; |
| 33 | 33 |
| 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; |
| (...skipping 13 matching lines...) Expand all Loading... |
| 56 // Adds or updates all the given snippets. | 56 // Adds or updates all the given snippets. |
| 57 void SaveSnippets(const NTPSnippet::PtrVector& snippets); | 57 void SaveSnippets(const NTPSnippet::PtrVector& snippets); |
| 58 | 58 |
| 59 // Deletes the snippet with the given ID, and its image. | 59 // Deletes the snippet with the given ID, and its image. |
| 60 void DeleteSnippet(const std::string& snippet_id); | 60 void DeleteSnippet(const std::string& snippet_id); |
| 61 // Deletes all the given snippets (identified by their IDs) and their images. | 61 // Deletes all the given snippets (identified by their IDs) and their images. |
| 62 void DeleteSnippets(const NTPSnippet::PtrVector& snippets); | 62 void DeleteSnippets(const NTPSnippet::PtrVector& snippets); |
| 63 | 63 |
| 64 // Loads the image data for the snippet with the given ID and passes it to | 64 // Loads the image data for the snippet with the given ID and passes it to |
| 65 // |callback|. Passes an empty string if not found. | 65 // |callback|. Passes an empty string if not found. |
| 66 void LoadImage(const std::string& snippet_id, | 66 void LoadImage(const std::string& suggestion_id, |
| 67 const SnippetImageCallback& callback); | 67 const SnippetImageCallback& callback); |
| 68 | 68 |
| 69 // Adds or updates the image data for the given snippet ID. | 69 // Adds or updates the image data for the given snippet ID. |
| 70 void SaveImage(const std::string& snippet_id, const std::string& image_data); | 70 void SaveImage(const std::string& suggestion_id, |
| 71 const std::string& image_data); |
| 71 | 72 |
| 72 // Deletes the image data for the given snippet ID. | 73 // Deletes the image data for the given snippet ID. |
| 73 void DeleteImage(const std::string& snippet_id); | 74 void DeleteImage(const std::string& suggestion_id); |
| 74 | 75 |
| 75 private: | 76 private: |
| 76 friend class NTPSnippetsDatabaseTest; | 77 friend class NTPSnippetsDatabaseTest; |
| 77 | 78 |
| 78 using KeyEntryVector = | 79 using KeyEntryVector = |
| 79 leveldb_proto::ProtoDatabase<SnippetProto>::KeyEntryVector; | 80 leveldb_proto::ProtoDatabase<SnippetProto>::KeyEntryVector; |
| 80 | 81 |
| 81 using ImageKeyEntryVector = | 82 using ImageKeyEntryVector = |
| 82 leveldb_proto::ProtoDatabase<SnippetImageProto>::KeyEntryVector; | 83 leveldb_proto::ProtoDatabase<SnippetImageProto>::KeyEntryVector; |
| 83 | 84 |
| (...skipping 13 matching lines...) Expand all Loading... |
| 97 | 98 |
| 98 void OnDatabaseError(); | 99 void OnDatabaseError(); |
| 99 | 100 |
| 100 void ProcessPendingLoads(); | 101 void ProcessPendingLoads(); |
| 101 | 102 |
| 102 void LoadSnippetsImpl(const SnippetsCallback& callback); | 103 void LoadSnippetsImpl(const SnippetsCallback& callback); |
| 103 void SaveSnippetsImpl(std::unique_ptr<KeyEntryVector> entries_to_save); | 104 void SaveSnippetsImpl(std::unique_ptr<KeyEntryVector> entries_to_save); |
| 104 void DeleteSnippetsImpl( | 105 void DeleteSnippetsImpl( |
| 105 std::unique_ptr<std::vector<std::string>> keys_to_remove); | 106 std::unique_ptr<std::vector<std::string>> keys_to_remove); |
| 106 | 107 |
| 107 void LoadImageImpl(const std::string& snippet_id, | 108 void LoadImageImpl(const std::string& suggestion_id, |
| 108 const SnippetImageCallback& callback); | 109 const SnippetImageCallback& callback); |
| 109 void DeleteImagesImpl( | 110 void DeleteImagesImpl( |
| 110 std::unique_ptr<std::vector<std::string>> keys_to_remove); | 111 std::unique_ptr<std::vector<std::string>> keys_to_remove); |
| 111 | 112 |
| 112 std::unique_ptr<leveldb_proto::ProtoDatabase<SnippetProto>> database_; | 113 std::unique_ptr<leveldb_proto::ProtoDatabase<SnippetProto>> database_; |
| 113 bool database_initialized_; | 114 bool database_initialized_; |
| 114 std::vector<SnippetsCallback> pending_snippets_callbacks_; | 115 std::vector<SnippetsCallback> pending_snippets_callbacks_; |
| 115 | 116 |
| 116 std::unique_ptr<leveldb_proto::ProtoDatabase<SnippetImageProto>> | 117 std::unique_ptr<leveldb_proto::ProtoDatabase<SnippetImageProto>> |
| 117 image_database_; | 118 image_database_; |
| 118 bool image_database_initialized_; | 119 bool image_database_initialized_; |
| 119 std::vector<std::pair<std::string, SnippetImageCallback>> | 120 std::vector<std::pair<std::string, SnippetImageCallback>> |
| 120 pending_image_callbacks_; | 121 pending_image_callbacks_; |
| 121 | 122 |
| 122 base::Closure error_callback_; | 123 base::Closure error_callback_; |
| 123 | 124 |
| 124 base::WeakPtrFactory<NTPSnippetsDatabase> weak_ptr_factory_; | 125 base::WeakPtrFactory<NTPSnippetsDatabase> weak_ptr_factory_; |
| 125 | 126 |
| 126 DISALLOW_COPY_AND_ASSIGN(NTPSnippetsDatabase); | 127 DISALLOW_COPY_AND_ASSIGN(NTPSnippetsDatabase); |
| 127 }; | 128 }; |
| 128 | 129 |
| 129 } // namespace ntp_snippets | 130 } // namespace ntp_snippets |
| 130 | 131 |
| 131 #endif // COMPONENTS_NTP_SNIPPETS_NTP_SNIPPETS_DATABASE_H_ | 132 #endif // COMPONENTS_NTP_SNIPPETS_NTP_SNIPPETS_DATABASE_H_ |
| OLD | NEW |