| 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/remote/ntp_snippets_database.h" | 5 #include "components/ntp_snippets/remote/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 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 77 } | 77 } |
| 78 | 78 |
| 79 void NTPSnippetsDatabase::SaveSnippets(const NTPSnippet::PtrVector& snippets) { | 79 void NTPSnippetsDatabase::SaveSnippets(const NTPSnippet::PtrVector& snippets) { |
| 80 std::unique_ptr<KeyEntryVector> entries_to_save(new KeyEntryVector()); | 80 std::unique_ptr<KeyEntryVector> entries_to_save(new KeyEntryVector()); |
| 81 for (const std::unique_ptr<NTPSnippet>& snippet : snippets) | 81 for (const std::unique_ptr<NTPSnippet>& snippet : snippets) |
| 82 entries_to_save->emplace_back(snippet->id(), snippet->ToProto()); | 82 entries_to_save->emplace_back(snippet->id(), snippet->ToProto()); |
| 83 SaveSnippetsImpl(std::move(entries_to_save)); | 83 SaveSnippetsImpl(std::move(entries_to_save)); |
| 84 } | 84 } |
| 85 | 85 |
| 86 void NTPSnippetsDatabase::DeleteSnippet(const std::string& snippet_id) { | 86 void NTPSnippetsDatabase::DeleteSnippet(const std::string& snippet_id) { |
| 87 DeleteSnippetsImpl(base::MakeUnique<std::vector<std::string>>(1, snippet_id)); | 87 DeleteSnippets(base::MakeUnique<std::vector<std::string>>(1, snippet_id)); |
| 88 } | 88 } |
| 89 | 89 |
| 90 void NTPSnippetsDatabase::DeleteSnippets( | 90 void NTPSnippetsDatabase::DeleteSnippets( |
| 91 const NTPSnippet::PtrVector& snippets) { | 91 std::unique_ptr<std::vector<std::string>> keys_to_remove) { |
| 92 std::unique_ptr<std::vector<std::string>> keys_to_remove( | 92 DCHECK(IsInitialized()); |
| 93 new std::vector<std::string>()); | 93 |
| 94 for (const std::unique_ptr<NTPSnippet>& snippet : snippets) | 94 std::unique_ptr<KeyEntryVector> entries_to_save(new KeyEntryVector()); |
| 95 keys_to_remove->emplace_back(snippet->id()); | 95 database_->UpdateEntries(std::move(entries_to_save), |
| 96 DeleteSnippetsImpl(std::move(keys_to_remove)); | 96 std::move(keys_to_remove), |
| 97 base::Bind(&NTPSnippetsDatabase::OnDatabaseSaved, |
| 98 weak_ptr_factory_.GetWeakPtr())); |
| 97 } | 99 } |
| 98 | 100 |
| 99 void NTPSnippetsDatabase::LoadImage(const std::string& snippet_id, | 101 void NTPSnippetsDatabase::LoadImage(const std::string& snippet_id, |
| 100 const SnippetImageCallback& callback) { | 102 const SnippetImageCallback& callback) { |
| 101 if (IsInitialized()) | 103 if (IsInitialized()) |
| 102 LoadImageImpl(snippet_id, callback); | 104 LoadImageImpl(snippet_id, callback); |
| 103 else | 105 else |
| 104 pending_image_callbacks_.emplace_back(snippet_id, callback); | 106 pending_image_callbacks_.emplace_back(snippet_id, callback); |
| 105 } | 107 } |
| 106 | 108 |
| 107 void NTPSnippetsDatabase::SaveImage(const std::string& snippet_id, | 109 void NTPSnippetsDatabase::SaveImage(const std::string& snippet_id, |
| 108 const std::string& image_data) { | 110 const std::string& image_data) { |
| 109 DCHECK(IsInitialized()); | 111 DCHECK(IsInitialized()); |
| 110 | 112 |
| 111 SnippetImageProto image_proto; | 113 SnippetImageProto image_proto; |
| 112 image_proto.set_data(image_data); | 114 image_proto.set_data(image_data); |
| 113 | 115 |
| 114 std::unique_ptr<ImageKeyEntryVector> entries_to_save( | 116 std::unique_ptr<ImageKeyEntryVector> entries_to_save( |
| 115 new ImageKeyEntryVector()); | 117 new ImageKeyEntryVector()); |
| 116 entries_to_save->emplace_back(snippet_id, std::move(image_proto)); | 118 entries_to_save->emplace_back(snippet_id, std::move(image_proto)); |
| 117 | 119 |
| 118 image_database_->UpdateEntries( | 120 image_database_->UpdateEntries( |
| 119 std::move(entries_to_save), base::MakeUnique<std::vector<std::string>>(), | 121 std::move(entries_to_save), base::MakeUnique<std::vector<std::string>>(), |
| 120 base::Bind(&NTPSnippetsDatabase::OnImageDatabaseSaved, | 122 base::Bind(&NTPSnippetsDatabase::OnImageDatabaseSaved, |
| 121 weak_ptr_factory_.GetWeakPtr())); | 123 weak_ptr_factory_.GetWeakPtr())); |
| 122 } | 124 } |
| 123 | 125 |
| 124 void NTPSnippetsDatabase::DeleteImage(const std::string& snippet_id) { | 126 void NTPSnippetsDatabase::DeleteImage(const std::string& snippet_id) { |
| 125 DeleteImagesImpl(base::MakeUnique<std::vector<std::string>>(1, snippet_id)); | 127 DeleteImages(base::MakeUnique<std::vector<std::string>>(1, snippet_id)); |
| 126 } | 128 } |
| 127 | 129 |
| 128 void NTPSnippetsDatabase::DeleteImages(const NTPSnippet::PtrVector& snippets) { | 130 void NTPSnippetsDatabase::DeleteImages( |
| 129 std::unique_ptr<std::vector<std::string>> keys_to_remove( | 131 std::unique_ptr<std::vector<std::string>> keys_to_remove) { |
| 130 new std::vector<std::string>()); | 132 DCHECK(IsInitialized()); |
| 131 for (const std::unique_ptr<NTPSnippet>& snippet : snippets) | 133 image_database_->UpdateEntries( |
| 132 keys_to_remove->emplace_back(snippet->id()); | 134 base::MakeUnique<ImageKeyEntryVector>(), std::move(keys_to_remove), |
| 133 DeleteImagesImpl(std::move(keys_to_remove)); | 135 base::Bind(&NTPSnippetsDatabase::OnImageDatabaseSaved, |
| 136 weak_ptr_factory_.GetWeakPtr())); |
| 137 } |
| 138 |
| 139 void NTPSnippetsDatabase::GarbageCollectImages( |
| 140 std::unique_ptr<std::set<std::string>> alive_snippet_ids) { |
| 141 DCHECK(image_database_initialized_); |
| 142 image_database_->LoadKeys( |
| 143 base::Bind(&NTPSnippetsDatabase::DeleteUnreferencedImages, |
| 144 weak_ptr_factory_.GetWeakPtr(), |
| 145 base::Passed(std::move(alive_snippet_ids)))); |
| 134 } | 146 } |
| 135 | 147 |
| 136 void NTPSnippetsDatabase::OnDatabaseInited(bool success) { | 148 void NTPSnippetsDatabase::OnDatabaseInited(bool success) { |
| 137 DCHECK(!database_initialized_); | 149 DCHECK(!database_initialized_); |
| 138 if (!success) { | 150 if (!success) { |
| 139 DVLOG(1) << "NTPSnippetsDatabase init failed."; | 151 DVLOG(1) << "NTPSnippetsDatabase init failed."; |
| 140 OnDatabaseError(); | 152 OnDatabaseError(); |
| 141 return; | 153 return; |
| 142 } | 154 } |
| 143 database_initialized_ = true; | 155 database_initialized_ = true; |
| (...skipping 23 matching lines...) Expand all Loading... |
| 167 LOG(WARNING) << "Invalid proto from DB " << proto.id(); | 179 LOG(WARNING) << "Invalid proto from DB " << proto.id(); |
| 168 keys_to_remove->emplace_back(proto.id()); | 180 keys_to_remove->emplace_back(proto.id()); |
| 169 } | 181 } |
| 170 } | 182 } |
| 171 | 183 |
| 172 callback.Run(std::move(snippets)); | 184 callback.Run(std::move(snippets)); |
| 173 | 185 |
| 174 // If any of the snippet protos couldn't be converted to actual snippets, | 186 // If any of the snippet protos couldn't be converted to actual snippets, |
| 175 // clean them up now. | 187 // clean them up now. |
| 176 if (!keys_to_remove->empty()) | 188 if (!keys_to_remove->empty()) |
| 177 DeleteSnippetsImpl(std::move(keys_to_remove)); | 189 DeleteSnippets(std::move(keys_to_remove)); |
| 178 } | 190 } |
| 179 | 191 |
| 180 void NTPSnippetsDatabase::OnDatabaseSaved(bool success) { | 192 void NTPSnippetsDatabase::OnDatabaseSaved(bool success) { |
| 181 if (!success) { | 193 if (!success) { |
| 182 DVLOG(1) << "NTPSnippetsDatabase save failed."; | 194 DVLOG(1) << "NTPSnippetsDatabase save failed."; |
| 183 OnDatabaseError(); | 195 OnDatabaseError(); |
| 184 } | 196 } |
| 185 } | 197 } |
| 186 | 198 |
| 187 void NTPSnippetsDatabase::OnImageDatabaseInited(bool success) { | 199 void NTPSnippetsDatabase::OnImageDatabaseInited(bool success) { |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 253 DCHECK(IsInitialized()); | 265 DCHECK(IsInitialized()); |
| 254 | 266 |
| 255 std::unique_ptr<std::vector<std::string>> keys_to_remove( | 267 std::unique_ptr<std::vector<std::string>> keys_to_remove( |
| 256 new std::vector<std::string>()); | 268 new std::vector<std::string>()); |
| 257 database_->UpdateEntries(std::move(entries_to_save), | 269 database_->UpdateEntries(std::move(entries_to_save), |
| 258 std::move(keys_to_remove), | 270 std::move(keys_to_remove), |
| 259 base::Bind(&NTPSnippetsDatabase::OnDatabaseSaved, | 271 base::Bind(&NTPSnippetsDatabase::OnDatabaseSaved, |
| 260 weak_ptr_factory_.GetWeakPtr())); | 272 weak_ptr_factory_.GetWeakPtr())); |
| 261 } | 273 } |
| 262 | 274 |
| 263 void NTPSnippetsDatabase::DeleteSnippetsImpl( | |
| 264 std::unique_ptr<std::vector<std::string>> keys_to_remove) { | |
| 265 DCHECK(IsInitialized()); | |
| 266 | |
| 267 std::unique_ptr<KeyEntryVector> entries_to_save(new KeyEntryVector()); | |
| 268 database_->UpdateEntries(std::move(entries_to_save), | |
| 269 std::move(keys_to_remove), | |
| 270 base::Bind(&NTPSnippetsDatabase::OnDatabaseSaved, | |
| 271 weak_ptr_factory_.GetWeakPtr())); | |
| 272 } | |
| 273 | |
| 274 void NTPSnippetsDatabase::LoadImageImpl(const std::string& snippet_id, | 275 void NTPSnippetsDatabase::LoadImageImpl(const std::string& snippet_id, |
| 275 const SnippetImageCallback& callback) { | 276 const SnippetImageCallback& callback) { |
| 276 DCHECK(IsInitialized()); | 277 DCHECK(IsInitialized()); |
| 277 image_database_->GetEntry( | 278 image_database_->GetEntry( |
| 278 snippet_id, | 279 snippet_id, |
| 279 base::Bind(&NTPSnippetsDatabase::OnImageDatabaseLoaded, | 280 base::Bind(&NTPSnippetsDatabase::OnImageDatabaseLoaded, |
| 280 weak_ptr_factory_.GetWeakPtr(), callback)); | 281 weak_ptr_factory_.GetWeakPtr(), callback)); |
| 281 } | 282 } |
| 282 | 283 |
| 283 void NTPSnippetsDatabase::DeleteImagesImpl( | 284 void NTPSnippetsDatabase::DeleteUnreferencedImages( |
| 284 std::unique_ptr<std::vector<std::string>> keys_to_remove) { | 285 std::unique_ptr<std::set<std::string>> references, |
| 285 DCHECK(IsInitialized()); | 286 bool load_keys_success, |
| 286 | 287 std::unique_ptr<std::vector<std::string>> image_keys) { |
| 287 image_database_->UpdateEntries( | 288 if (!load_keys_success) { |
| 288 base::MakeUnique<ImageKeyEntryVector>(), std::move(keys_to_remove), | 289 DVLOG(1) << "NTPSnippetsDatabase garbage collection failed."; |
| 289 base::Bind(&NTPSnippetsDatabase::OnImageDatabaseSaved, | 290 OnDatabaseError(); |
| 290 weak_ptr_factory_.GetWeakPtr())); | 291 return; |
| 292 } |
| 293 auto keys_to_remove = base::MakeUnique<std::vector<std::string>>(); |
| 294 for (const std::string& key : *image_keys) { |
| 295 if (references->count(key) == 0) { |
| 296 keys_to_remove->emplace_back(key); |
| 297 } |
| 298 } |
| 299 DeleteImages(std::move(keys_to_remove)); |
| 291 } | 300 } |
| 292 | 301 |
| 302 |
| 293 } // namespace ntp_snippets | 303 } // namespace ntp_snippets |
| OLD | NEW |