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 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
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(const NTPSnippet::PtrVector& snippets); |
| 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); |
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 17 matching lines...) Expand all Loading... |
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( | 112 void DeleteSnippetsImpl( |
108 std::unique_ptr<std::vector<std::string>> keys_to_remove); | 113 std::unique_ptr<std::vector<std::string>> keys_to_remove); |
109 | 114 |
110 void LoadImageImpl(const std::string& snippet_id, | 115 void LoadImageImpl(const std::string& snippet_id, |
111 const SnippetImageCallback& callback); | 116 const SnippetImageCallback& callback); |
112 void DeleteImagesImpl( | 117 void DeleteImagesImpl( |
113 std::unique_ptr<std::vector<std::string>> keys_to_remove); | 118 std::unique_ptr<std::vector<std::string>> keys_to_remove); |
| 119 void DeleteUnreferencedImages( |
| 120 std::unique_ptr<std::set<std::string>> references, |
| 121 bool success, |
| 122 std::unique_ptr<std::vector<std::string>> keys); |
114 | 123 |
115 std::unique_ptr<leveldb_proto::ProtoDatabase<SnippetProto>> database_; | 124 std::unique_ptr<leveldb_proto::ProtoDatabase<SnippetProto>> database_; |
116 bool database_initialized_; | 125 bool database_initialized_; |
117 std::vector<SnippetsCallback> pending_snippets_callbacks_; | 126 std::vector<SnippetsCallback> pending_snippets_callbacks_; |
118 | 127 |
119 std::unique_ptr<leveldb_proto::ProtoDatabase<SnippetImageProto>> | 128 std::unique_ptr<leveldb_proto::ProtoDatabase<SnippetImageProto>> |
120 image_database_; | 129 image_database_; |
121 bool image_database_initialized_; | 130 bool image_database_initialized_; |
122 std::vector<std::pair<std::string, SnippetImageCallback>> | 131 std::vector<std::pair<std::string, SnippetImageCallback>> |
123 pending_image_callbacks_; | 132 pending_image_callbacks_; |
124 | 133 |
125 base::Closure error_callback_; | 134 base::Closure error_callback_; |
126 | 135 |
127 base::WeakPtrFactory<NTPSnippetsDatabase> weak_ptr_factory_; | 136 base::WeakPtrFactory<NTPSnippetsDatabase> weak_ptr_factory_; |
128 | 137 |
129 DISALLOW_COPY_AND_ASSIGN(NTPSnippetsDatabase); | 138 DISALLOW_COPY_AND_ASSIGN(NTPSnippetsDatabase); |
130 }; | 139 }; |
131 | 140 |
132 } // namespace ntp_snippets | 141 } // namespace ntp_snippets |
133 | 142 |
134 #endif // COMPONENTS_NTP_SNIPPETS_REMOTE_NTP_SNIPPETS_DATABASE_H_ | 143 #endif // COMPONENTS_NTP_SNIPPETS_REMOTE_NTP_SNIPPETS_DATABASE_H_ |
OLD | NEW |