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_REMOTE_NTP_SNIPPETS_DATABASE_H_ | 5 #ifndef COMPONENTS_NTP_SNIPPETS_REMOTE_NTP_SNIPPETS_DATABASE_H_ |
| 6 #define COMPONENTS_NTP_SNIPPETS_REMOTE_NTP_SNIPPETS_DATABASE_H_ | 6 #define COMPONENTS_NTP_SNIPPETS_REMOTE_NTP_SNIPPETS_DATABASE_H_ |
| 7 | 7 |
| 8 #include <memory> | 8 #include <memory> |
| 9 #include <set> | |
| 9 #include <string> | 10 #include <string> |
| 10 #include <utility> | 11 #include <utility> |
| 11 #include <vector> | 12 #include <vector> |
| 12 | 13 |
| 13 #include "base/callback.h" | 14 #include "base/callback.h" |
| 14 #include "base/macros.h" | 15 #include "base/macros.h" |
| 15 #include "base/memory/ref_counted.h" | 16 #include "base/memory/ref_counted.h" |
| 16 #include "base/memory/weak_ptr.h" | 17 #include "base/memory/weak_ptr.h" |
| 17 #include "base/sequenced_task_runner.h" | 18 #include "base/sequenced_task_runner.h" |
| 18 #include "components/leveldb_proto/proto_database.h" | 19 #include "components/leveldb_proto/proto_database.h" |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 53 void LoadSnippets(const SnippetsCallback& callback); | 54 void LoadSnippets(const SnippetsCallback& callback); |
| 54 | 55 |
| 55 // Adds or updates the given snippet. | 56 // Adds or updates the given snippet. |
| 56 void SaveSnippet(const NTPSnippet& snippet); | 57 void SaveSnippet(const NTPSnippet& snippet); |
| 57 // Adds or updates all the given snippets. | 58 // Adds or updates all the given snippets. |
| 58 void SaveSnippets(const NTPSnippet::PtrVector& snippets); | 59 void SaveSnippets(const NTPSnippet::PtrVector& snippets); |
| 59 | 60 |
| 60 // Deletes the snippet with the given ID. | 61 // Deletes the snippet with the given ID. |
| 61 void DeleteSnippet(const std::string& snippet_id); | 62 void DeleteSnippet(const std::string& snippet_id); |
| 62 // Deletes all the given snippets (identified by their IDs). | 63 // Deletes all the given snippets (identified by their IDs). |
| 63 void DeleteSnippets(const NTPSnippet::PtrVector& snippets); | 64 void DeleteSnippets(std::unique_ptr<std::vector<std::string>> keys_to_remove); |
| 64 | 65 |
| 65 // Loads the image data for the snippet with the given ID and passes it to | 66 // Loads the image data for the snippet with the given ID and passes it to |
| 66 // |callback|. Passes an empty string if not found. | 67 // |callback|. Passes an empty string if not found. |
| 67 void LoadImage(const std::string& snippet_id, | 68 void LoadImage(const std::string& snippet_id, |
| 68 const SnippetImageCallback& callback); | 69 const SnippetImageCallback& callback); |
| 69 | 70 |
| 70 // Adds or updates the image data for the given snippet ID. | 71 // Adds or updates the image data for the given snippet ID. |
| 71 void SaveImage(const std::string& snippet_id, const std::string& image_data); | 72 void SaveImage(const std::string& snippet_id, const std::string& image_data); |
| 72 | 73 |
| 73 // Deletes the image data for the given snippet ID. | 74 // Deletes the image data for the given snippet ID. |
| 74 void DeleteImage(const std::string& snippet_id); | 75 void DeleteImage(const std::string& snippet_id); |
| 75 // Deletes the image data for the given snippets (identified by their IDs). | 76 // Deletes the image data for the given snippets (identified by their IDs). |
| 76 void DeleteImages(const NTPSnippet::PtrVector& snippets); | 77 void DeleteImages(std::unique_ptr<std::vector<std::string>> snippet_ids); |
| 78 // Deletes all images which are not associated with any of the provided | |
| 79 // snippets. | |
| 80 void GarbageCollectImages( | |
| 81 std::unique_ptr<std::set<std::string>> alive_snippets); | |
|
Marc Treib
2016/10/06 12:47:35
Hm, now this one takes a set, but all the others t
tschumann
2016/10/06 13:21:36
renamed.
:-) IMO that's fine. If we'd ever need t
| |
| 77 | 82 |
| 78 private: | 83 private: |
| 79 friend class NTPSnippetsDatabaseTest; | 84 friend class NTPSnippetsDatabaseTest; |
| 80 | 85 |
| 81 using KeyEntryVector = | 86 using KeyEntryVector = |
| 82 leveldb_proto::ProtoDatabase<SnippetProto>::KeyEntryVector; | 87 leveldb_proto::ProtoDatabase<SnippetProto>::KeyEntryVector; |
| 83 | 88 |
| 84 using ImageKeyEntryVector = | 89 using ImageKeyEntryVector = |
| 85 leveldb_proto::ProtoDatabase<SnippetImageProto>::KeyEntryVector; | 90 leveldb_proto::ProtoDatabase<SnippetImageProto>::KeyEntryVector; |
| 86 | 91 |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 97 bool success, | 102 bool success, |
| 98 std::unique_ptr<SnippetImageProto> entry); | 103 std::unique_ptr<SnippetImageProto> entry); |
| 99 void OnImageDatabaseSaved(bool success); | 104 void OnImageDatabaseSaved(bool success); |
| 100 | 105 |
| 101 void OnDatabaseError(); | 106 void OnDatabaseError(); |
| 102 | 107 |
| 103 void ProcessPendingLoads(); | 108 void ProcessPendingLoads(); |
| 104 | 109 |
| 105 void LoadSnippetsImpl(const SnippetsCallback& callback); | 110 void LoadSnippetsImpl(const SnippetsCallback& callback); |
| 106 void SaveSnippetsImpl(std::unique_ptr<KeyEntryVector> entries_to_save); | 111 void SaveSnippetsImpl(std::unique_ptr<KeyEntryVector> entries_to_save); |
| 107 void DeleteSnippetsImpl( | |
| 108 std::unique_ptr<std::vector<std::string>> keys_to_remove); | |
| 109 | 112 |
| 110 void LoadImageImpl(const std::string& snippet_id, | 113 void LoadImageImpl(const std::string& snippet_id, |
| 111 const SnippetImageCallback& callback); | 114 const SnippetImageCallback& callback); |
| 112 void DeleteImagesImpl( | 115 void DeleteUnreferencedImages( |
| 113 std::unique_ptr<std::vector<std::string>> keys_to_remove); | 116 std::unique_ptr<std::set<std::string>> references, |
| 117 bool load_keys_success, | |
| 118 std::unique_ptr<std::vector<std::string>> image_keys); | |
| 114 | 119 |
| 115 std::unique_ptr<leveldb_proto::ProtoDatabase<SnippetProto>> database_; | 120 std::unique_ptr<leveldb_proto::ProtoDatabase<SnippetProto>> database_; |
| 116 bool database_initialized_; | 121 bool database_initialized_; |
| 117 std::vector<SnippetsCallback> pending_snippets_callbacks_; | 122 std::vector<SnippetsCallback> pending_snippets_callbacks_; |
| 118 | 123 |
| 119 std::unique_ptr<leveldb_proto::ProtoDatabase<SnippetImageProto>> | 124 std::unique_ptr<leveldb_proto::ProtoDatabase<SnippetImageProto>> |
| 120 image_database_; | 125 image_database_; |
| 121 bool image_database_initialized_; | 126 bool image_database_initialized_; |
| 122 std::vector<std::pair<std::string, SnippetImageCallback>> | 127 std::vector<std::pair<std::string, SnippetImageCallback>> |
| 123 pending_image_callbacks_; | 128 pending_image_callbacks_; |
| 124 | 129 |
| 125 base::Closure error_callback_; | 130 base::Closure error_callback_; |
| 126 | 131 |
| 127 base::WeakPtrFactory<NTPSnippetsDatabase> weak_ptr_factory_; | 132 base::WeakPtrFactory<NTPSnippetsDatabase> weak_ptr_factory_; |
| 128 | 133 |
| 129 DISALLOW_COPY_AND_ASSIGN(NTPSnippetsDatabase); | 134 DISALLOW_COPY_AND_ASSIGN(NTPSnippetsDatabase); |
| 130 }; | 135 }; |
| 131 | 136 |
| 132 } // namespace ntp_snippets | 137 } // namespace ntp_snippets |
| 133 | 138 |
| 134 #endif // COMPONENTS_NTP_SNIPPETS_REMOTE_NTP_SNIPPETS_DATABASE_H_ | 139 #endif // COMPONENTS_NTP_SNIPPETS_REMOTE_NTP_SNIPPETS_DATABASE_H_ |
| OLD | NEW |