| 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" |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 90 | 90 |
| 91 void NTPSnippetsDatabase::DeleteSnippets( | 91 void NTPSnippetsDatabase::DeleteSnippets( |
| 92 const NTPSnippet::PtrVector& snippets) { | 92 const NTPSnippet::PtrVector& snippets) { |
| 93 std::unique_ptr<std::vector<std::string>> keys_to_remove( | 93 std::unique_ptr<std::vector<std::string>> keys_to_remove( |
| 94 new std::vector<std::string>()); | 94 new std::vector<std::string>()); |
| 95 for (const std::unique_ptr<NTPSnippet>& snippet : snippets) | 95 for (const std::unique_ptr<NTPSnippet>& snippet : snippets) |
| 96 keys_to_remove->emplace_back(snippet->id()); | 96 keys_to_remove->emplace_back(snippet->id()); |
| 97 DeleteSnippetsImpl(std::move(keys_to_remove)); | 97 DeleteSnippetsImpl(std::move(keys_to_remove)); |
| 98 } | 98 } |
| 99 | 99 |
| 100 void NTPSnippetsDatabase::LoadImage(const std::string& snippet_id, | 100 void NTPSnippetsDatabase::LoadImage(const std::string& suggestion_id, |
| 101 const SnippetImageCallback& callback) { | 101 const SnippetImageCallback& callback) { |
| 102 if (IsInitialized()) | 102 if (IsInitialized()) |
| 103 LoadImageImpl(snippet_id, callback); | 103 LoadImageImpl(suggestion_id, callback); |
| 104 else | 104 else |
| 105 pending_image_callbacks_.emplace_back(snippet_id, callback); | 105 pending_image_callbacks_.emplace_back(suggestion_id, callback); |
| 106 } | 106 } |
| 107 | 107 |
| 108 void NTPSnippetsDatabase::SaveImage(const std::string& snippet_id, | 108 void NTPSnippetsDatabase::SaveImage(const std::string& suggestion_id, |
| 109 const std::string& image_data) { | 109 const std::string& image_data) { |
| 110 DCHECK(IsInitialized()); | 110 DCHECK(IsInitialized()); |
| 111 | 111 |
| 112 SnippetImageProto image_proto; | 112 SnippetImageProto image_proto; |
| 113 image_proto.set_data(image_data); | 113 image_proto.set_data(image_data); |
| 114 | 114 |
| 115 std::unique_ptr<ImageKeyEntryVector> entries_to_save( | 115 std::unique_ptr<ImageKeyEntryVector> entries_to_save( |
| 116 new ImageKeyEntryVector()); | 116 new ImageKeyEntryVector()); |
| 117 entries_to_save->emplace_back(snippet_id, std::move(image_proto)); | 117 entries_to_save->emplace_back(suggestion_id, std::move(image_proto)); |
| 118 | 118 |
| 119 image_database_->UpdateEntries( | 119 image_database_->UpdateEntries( |
| 120 std::move(entries_to_save), | 120 std::move(entries_to_save), |
| 121 base::WrapUnique(new std::vector<std::string>()), | 121 base::WrapUnique(new std::vector<std::string>()), |
| 122 base::Bind(&NTPSnippetsDatabase::OnImageDatabaseSaved, | 122 base::Bind(&NTPSnippetsDatabase::OnImageDatabaseSaved, |
| 123 weak_ptr_factory_.GetWeakPtr())); | 123 weak_ptr_factory_.GetWeakPtr())); |
| 124 } | 124 } |
| 125 | 125 |
| 126 void NTPSnippetsDatabase::DeleteImage(const std::string& snippet_id) { | 126 void NTPSnippetsDatabase::DeleteImage(const std::string& suggestion_id) { |
| 127 DeleteImagesImpl( | 127 DeleteImagesImpl( |
| 128 base::WrapUnique(new std::vector<std::string>(1, snippet_id))); | 128 base::WrapUnique(new std::vector<std::string>(1, suggestion_id))); |
| 129 } | 129 } |
| 130 | 130 |
| 131 void NTPSnippetsDatabase::OnDatabaseInited(bool success) { | 131 void NTPSnippetsDatabase::OnDatabaseInited(bool success) { |
| 132 DCHECK(!database_initialized_); | 132 DCHECK(!database_initialized_); |
| 133 if (!success) { | 133 if (!success) { |
| 134 DVLOG(1) << "NTPSnippetsDatabase init failed."; | 134 DVLOG(1) << "NTPSnippetsDatabase init failed."; |
| 135 OnDatabaseError(); | 135 OnDatabaseError(); |
| 136 return; | 136 return; |
| 137 } | 137 } |
| 138 database_initialized_ = true; | 138 database_initialized_ = true; |
| (...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 262 DeleteImagesImpl( | 262 DeleteImagesImpl( |
| 263 base::WrapUnique(new std::vector<std::string>(*keys_to_remove))); | 263 base::WrapUnique(new std::vector<std::string>(*keys_to_remove))); |
| 264 | 264 |
| 265 std::unique_ptr<KeyEntryVector> entries_to_save(new KeyEntryVector()); | 265 std::unique_ptr<KeyEntryVector> entries_to_save(new KeyEntryVector()); |
| 266 database_->UpdateEntries(std::move(entries_to_save), | 266 database_->UpdateEntries(std::move(entries_to_save), |
| 267 std::move(keys_to_remove), | 267 std::move(keys_to_remove), |
| 268 base::Bind(&NTPSnippetsDatabase::OnDatabaseSaved, | 268 base::Bind(&NTPSnippetsDatabase::OnDatabaseSaved, |
| 269 weak_ptr_factory_.GetWeakPtr())); | 269 weak_ptr_factory_.GetWeakPtr())); |
| 270 } | 270 } |
| 271 | 271 |
| 272 void NTPSnippetsDatabase::LoadImageImpl(const std::string& snippet_id, | 272 void NTPSnippetsDatabase::LoadImageImpl(const std::string& suggestion_id, |
| 273 const SnippetImageCallback& callback) { | 273 const SnippetImageCallback& callback) { |
| 274 DCHECK(IsInitialized()); | 274 DCHECK(IsInitialized()); |
| 275 image_database_->GetEntry( | 275 image_database_->GetEntry( |
| 276 snippet_id, | 276 suggestion_id, base::Bind(&NTPSnippetsDatabase::OnImageDatabaseLoaded, |
| 277 base::Bind(&NTPSnippetsDatabase::OnImageDatabaseLoaded, | 277 weak_ptr_factory_.GetWeakPtr(), callback)); |
| 278 weak_ptr_factory_.GetWeakPtr(), callback)); | |
| 279 } | 278 } |
| 280 | 279 |
| 281 void NTPSnippetsDatabase::DeleteImagesImpl( | 280 void NTPSnippetsDatabase::DeleteImagesImpl( |
| 282 std::unique_ptr<std::vector<std::string>> keys_to_remove) { | 281 std::unique_ptr<std::vector<std::string>> keys_to_remove) { |
| 283 DCHECK(IsInitialized()); | 282 DCHECK(IsInitialized()); |
| 284 | 283 |
| 285 image_database_->UpdateEntries( | 284 image_database_->UpdateEntries( |
| 286 base::WrapUnique(new ImageKeyEntryVector()), | 285 base::WrapUnique(new ImageKeyEntryVector()), |
| 287 std::move(keys_to_remove), | 286 std::move(keys_to_remove), |
| 288 base::Bind(&NTPSnippetsDatabase::OnImageDatabaseSaved, | 287 base::Bind(&NTPSnippetsDatabase::OnImageDatabaseSaved, |
| 289 weak_ptr_factory_.GetWeakPtr())); | 288 weak_ptr_factory_.GetWeakPtr())); |
| 290 } | 289 } |
| 291 | 290 |
| 292 } // namespace ntp_snippets | 291 } // namespace ntp_snippets |
| OLD | NEW |