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 #include "components/ntp_snippets/ntp_snippets_database.h" | 5 #include "components/ntp_snippets/ntp_snippets_database.h" |
6 | 6 |
7 #include <utility> | 7 #include <utility> |
8 | 8 |
9 #include "base/files/file_path.h" | 9 #include "base/files/file_path.h" |
10 #include "components/leveldb_proto/proto_database_impl.h" | 10 #include "components/leveldb_proto/proto_database_impl.h" |
11 #include "components/ntp_snippets/proto/ntp_snippets.pb.h" | 11 #include "components/ntp_snippets/proto/ntp_snippets.pb.h" |
12 | 12 |
13 using leveldb_proto::ProtoDatabaseImpl; | 13 using leveldb_proto::ProtoDatabaseImpl; |
14 | 14 |
15 namespace { | 15 namespace { |
16 // Statistics are logged to UMA with this string as part of histogram name. They | 16 // Statistics are logged to UMA with this string as part of histogram name. They |
17 // can all be found under LevelDB.*.NTPSnippets. Changing this needs to | 17 // can all be found under LevelDB.*.NTPSnippets. Changing this needs to |
18 // synchronize with histograms.xml, AND will also become incompatible with older | 18 // synchronize with histograms.xml, AND will also become incompatible with older |
19 // browsers still reporting the previous values. | 19 // browsers still reporting the previous values. |
20 const char kDatabaseUMAClientName[] = "NTPSnippets"; | 20 const char kDatabaseUMAClientName[] = "NTPSnippets"; |
21 const char kImageDatabaseUMAClientName[] = "NTPSnippetImages"; | 21 const char kImageDatabaseUMAClientName[] = "NTPSnippetImages"; |
22 | 22 |
23 const char kSnippetDatabaseFolder[] = "snippets"; | 23 const char kSnippetDatabaseFolder[] = "snippets"; |
24 const char kImageDatabaseFolder[] = "images"; | 24 const char kImageDatabaseFolder[] = "images"; |
25 } | 25 } // namespace |
26 | 26 |
27 namespace ntp_snippets { | 27 namespace ntp_snippets { |
28 | 28 |
29 NTPSnippetsDatabase::NTPSnippetsDatabase( | 29 NTPSnippetsDatabase::NTPSnippetsDatabase( |
30 const base::FilePath& database_dir, | 30 const base::FilePath& database_dir, |
31 scoped_refptr<base::SequencedTaskRunner> file_task_runner) | 31 scoped_refptr<base::SequencedTaskRunner> file_task_runner) |
32 : database_( | 32 : database_( |
33 new ProtoDatabaseImpl<SnippetProto>(file_task_runner)), | 33 new ProtoDatabaseImpl<SnippetProto>(file_task_runner)), |
34 database_initialized_(false), | 34 database_initialized_(false), |
35 image_database_( | 35 image_database_( |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
118 image_database_->UpdateEntries( | 118 image_database_->UpdateEntries( |
119 std::move(entries_to_save), base::MakeUnique<std::vector<std::string>>(), | 119 std::move(entries_to_save), base::MakeUnique<std::vector<std::string>>(), |
120 base::Bind(&NTPSnippetsDatabase::OnImageDatabaseSaved, | 120 base::Bind(&NTPSnippetsDatabase::OnImageDatabaseSaved, |
121 weak_ptr_factory_.GetWeakPtr())); | 121 weak_ptr_factory_.GetWeakPtr())); |
122 } | 122 } |
123 | 123 |
124 void NTPSnippetsDatabase::DeleteImage(const std::string& snippet_id) { | 124 void NTPSnippetsDatabase::DeleteImage(const std::string& snippet_id) { |
125 DeleteImagesImpl(base::MakeUnique<std::vector<std::string>>(1, snippet_id)); | 125 DeleteImagesImpl(base::MakeUnique<std::vector<std::string>>(1, snippet_id)); |
126 } | 126 } |
127 | 127 |
| 128 void NTPSnippetsDatabase::DeleteImages(const NTPSnippet::PtrVector& snippets) { |
| 129 std::unique_ptr<std::vector<std::string>> keys_to_remove( |
| 130 new std::vector<std::string>()); |
| 131 for (const std::unique_ptr<NTPSnippet>& snippet : snippets) |
| 132 keys_to_remove->emplace_back(snippet->id()); |
| 133 DeleteImagesImpl(std::move(keys_to_remove)); |
| 134 } |
| 135 |
128 void NTPSnippetsDatabase::OnDatabaseInited(bool success) { | 136 void NTPSnippetsDatabase::OnDatabaseInited(bool success) { |
129 DCHECK(!database_initialized_); | 137 DCHECK(!database_initialized_); |
130 if (!success) { | 138 if (!success) { |
131 DVLOG(1) << "NTPSnippetsDatabase init failed."; | 139 DVLOG(1) << "NTPSnippetsDatabase init failed."; |
132 OnDatabaseError(); | 140 OnDatabaseError(); |
133 return; | 141 return; |
134 } | 142 } |
135 database_initialized_ = true; | 143 database_initialized_ = true; |
136 if (IsInitialized()) | 144 if (IsInitialized()) |
137 ProcessPendingLoads(); | 145 ProcessPendingLoads(); |
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
249 database_->UpdateEntries(std::move(entries_to_save), | 257 database_->UpdateEntries(std::move(entries_to_save), |
250 std::move(keys_to_remove), | 258 std::move(keys_to_remove), |
251 base::Bind(&NTPSnippetsDatabase::OnDatabaseSaved, | 259 base::Bind(&NTPSnippetsDatabase::OnDatabaseSaved, |
252 weak_ptr_factory_.GetWeakPtr())); | 260 weak_ptr_factory_.GetWeakPtr())); |
253 } | 261 } |
254 | 262 |
255 void NTPSnippetsDatabase::DeleteSnippetsImpl( | 263 void NTPSnippetsDatabase::DeleteSnippetsImpl( |
256 std::unique_ptr<std::vector<std::string>> keys_to_remove) { | 264 std::unique_ptr<std::vector<std::string>> keys_to_remove) { |
257 DCHECK(IsInitialized()); | 265 DCHECK(IsInitialized()); |
258 | 266 |
259 DeleteImagesImpl(base::MakeUnique<std::vector<std::string>>(*keys_to_remove)); | |
260 | |
261 std::unique_ptr<KeyEntryVector> entries_to_save(new KeyEntryVector()); | 267 std::unique_ptr<KeyEntryVector> entries_to_save(new KeyEntryVector()); |
262 database_->UpdateEntries(std::move(entries_to_save), | 268 database_->UpdateEntries(std::move(entries_to_save), |
263 std::move(keys_to_remove), | 269 std::move(keys_to_remove), |
264 base::Bind(&NTPSnippetsDatabase::OnDatabaseSaved, | 270 base::Bind(&NTPSnippetsDatabase::OnDatabaseSaved, |
265 weak_ptr_factory_.GetWeakPtr())); | 271 weak_ptr_factory_.GetWeakPtr())); |
266 } | 272 } |
267 | 273 |
268 void NTPSnippetsDatabase::LoadImageImpl(const std::string& snippet_id, | 274 void NTPSnippetsDatabase::LoadImageImpl(const std::string& snippet_id, |
269 const SnippetImageCallback& callback) { | 275 const SnippetImageCallback& callback) { |
270 DCHECK(IsInitialized()); | 276 DCHECK(IsInitialized()); |
271 image_database_->GetEntry( | 277 image_database_->GetEntry( |
272 snippet_id, | 278 snippet_id, |
273 base::Bind(&NTPSnippetsDatabase::OnImageDatabaseLoaded, | 279 base::Bind(&NTPSnippetsDatabase::OnImageDatabaseLoaded, |
274 weak_ptr_factory_.GetWeakPtr(), callback)); | 280 weak_ptr_factory_.GetWeakPtr(), callback)); |
275 } | 281 } |
276 | 282 |
277 void NTPSnippetsDatabase::DeleteImagesImpl( | 283 void NTPSnippetsDatabase::DeleteImagesImpl( |
278 std::unique_ptr<std::vector<std::string>> keys_to_remove) { | 284 std::unique_ptr<std::vector<std::string>> keys_to_remove) { |
279 DCHECK(IsInitialized()); | 285 DCHECK(IsInitialized()); |
280 | 286 |
281 image_database_->UpdateEntries( | 287 image_database_->UpdateEntries( |
282 base::MakeUnique<ImageKeyEntryVector>(), std::move(keys_to_remove), | 288 base::MakeUnique<ImageKeyEntryVector>(), std::move(keys_to_remove), |
283 base::Bind(&NTPSnippetsDatabase::OnImageDatabaseSaved, | 289 base::Bind(&NTPSnippetsDatabase::OnImageDatabaseSaved, |
284 weak_ptr_factory_.GetWeakPtr())); | 290 weak_ptr_factory_.GetWeakPtr())); |
285 } | 291 } |
286 | 292 |
287 } // namespace ntp_snippets | 293 } // namespace ntp_snippets |
OLD | NEW |