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 #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_snippets) { | |
| 141 DCHECK(image_database_initialized_); | |
| 142 image_database_->LoadKeys(base::Bind( | |
| 143 &NTPSnippetsDatabase::DeleteUnreferencedImages, | |
| 144 weak_ptr_factory_.GetWeakPtr(), base::Passed(std::move(alive_snippets)))); | |
| 134 } | 145 } |
| 135 | 146 |
| 136 void NTPSnippetsDatabase::OnDatabaseInited(bool success) { | 147 void NTPSnippetsDatabase::OnDatabaseInited(bool success) { |
| 137 DCHECK(!database_initialized_); | 148 DCHECK(!database_initialized_); |
| 138 if (!success) { | 149 if (!success) { |
| 139 DVLOG(1) << "NTPSnippetsDatabase init failed."; | 150 DVLOG(1) << "NTPSnippetsDatabase init failed."; |
| 140 OnDatabaseError(); | 151 OnDatabaseError(); |
| 141 return; | 152 return; |
| 142 } | 153 } |
| 143 database_initialized_ = true; | 154 database_initialized_ = true; |
| (...skipping 23 matching lines...) Expand all Loading... | |
| 167 LOG(WARNING) << "Invalid proto from DB " << proto.id(); | 178 LOG(WARNING) << "Invalid proto from DB " << proto.id(); |
| 168 keys_to_remove->emplace_back(proto.id()); | 179 keys_to_remove->emplace_back(proto.id()); |
| 169 } | 180 } |
| 170 } | 181 } |
| 171 | 182 |
| 172 callback.Run(std::move(snippets)); | 183 callback.Run(std::move(snippets)); |
| 173 | 184 |
| 174 // If any of the snippet protos couldn't be converted to actual snippets, | 185 // If any of the snippet protos couldn't be converted to actual snippets, |
| 175 // clean them up now. | 186 // clean them up now. |
| 176 if (!keys_to_remove->empty()) | 187 if (!keys_to_remove->empty()) |
| 177 DeleteSnippetsImpl(std::move(keys_to_remove)); | 188 DeleteSnippets(std::move(keys_to_remove)); |
| 178 } | 189 } |
| 179 | 190 |
| 180 void NTPSnippetsDatabase::OnDatabaseSaved(bool success) { | 191 void NTPSnippetsDatabase::OnDatabaseSaved(bool success) { |
| 181 if (!success) { | 192 if (!success) { |
| 182 DVLOG(1) << "NTPSnippetsDatabase save failed."; | 193 DVLOG(1) << "NTPSnippetsDatabase save failed."; |
| 183 OnDatabaseError(); | 194 OnDatabaseError(); |
| 184 } | 195 } |
| 185 } | 196 } |
| 186 | 197 |
| 187 void NTPSnippetsDatabase::OnImageDatabaseInited(bool success) { | 198 void NTPSnippetsDatabase::OnImageDatabaseInited(bool success) { |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 253 DCHECK(IsInitialized()); | 264 DCHECK(IsInitialized()); |
| 254 | 265 |
| 255 std::unique_ptr<std::vector<std::string>> keys_to_remove( | 266 std::unique_ptr<std::vector<std::string>> keys_to_remove( |
| 256 new std::vector<std::string>()); | 267 new std::vector<std::string>()); |
| 257 database_->UpdateEntries(std::move(entries_to_save), | 268 database_->UpdateEntries(std::move(entries_to_save), |
| 258 std::move(keys_to_remove), | 269 std::move(keys_to_remove), |
| 259 base::Bind(&NTPSnippetsDatabase::OnDatabaseSaved, | 270 base::Bind(&NTPSnippetsDatabase::OnDatabaseSaved, |
| 260 weak_ptr_factory_.GetWeakPtr())); | 271 weak_ptr_factory_.GetWeakPtr())); |
| 261 } | 272 } |
| 262 | 273 |
| 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, | 274 void NTPSnippetsDatabase::LoadImageImpl(const std::string& snippet_id, |
| 275 const SnippetImageCallback& callback) { | 275 const SnippetImageCallback& callback) { |
| 276 DCHECK(IsInitialized()); | 276 DCHECK(IsInitialized()); |
| 277 image_database_->GetEntry( | 277 image_database_->GetEntry( |
| 278 snippet_id, | 278 snippet_id, |
| 279 base::Bind(&NTPSnippetsDatabase::OnImageDatabaseLoaded, | 279 base::Bind(&NTPSnippetsDatabase::OnImageDatabaseLoaded, |
| 280 weak_ptr_factory_.GetWeakPtr(), callback)); | 280 weak_ptr_factory_.GetWeakPtr(), callback)); |
| 281 } | 281 } |
| 282 | 282 |
| 283 void NTPSnippetsDatabase::DeleteImagesImpl( | 283 void NTPSnippetsDatabase::DeleteUnreferencedImages( |
| 284 std::unique_ptr<std::vector<std::string>> keys_to_remove) { | 284 std::unique_ptr<std::set<std::string>> references, |
| 285 DCHECK(IsInitialized()); | 285 bool load_keys_success, |
| 286 | 286 std::unique_ptr<std::vector<std::string>> image_keys) { |
| 287 image_database_->UpdateEntries( | 287 if (!load_keys_success) { |
| 288 base::MakeUnique<ImageKeyEntryVector>(), std::move(keys_to_remove), | 288 DVLOG(1) << "NTPSnippetsDatabase garbage collection failed."; |
| 289 base::Bind(&NTPSnippetsDatabase::OnImageDatabaseSaved, | 289 OnDatabaseError(); |
| 290 weak_ptr_factory_.GetWeakPtr())); | 290 return; |
| 291 } | |
| 292 auto keys_to_remove = base::MakeUnique<std::vector<std::string>>(); | |
| 293 for (const std::string& key : *image_keys) { | |
| 294 if (references->count(key) == 0) { | |
| 295 keys_to_remove->emplace_back(key); | |
| 296 } | |
| 297 } | |
| 298 DeleteImages(std::move(keys_to_remove)); | |
| 291 } | 299 } |
| 292 | 300 |
| 301 | |
|
Marc Treib
2016/10/06 12:47:35
nit: remove the extra empty line
| |
| 293 } // namespace ntp_snippets | 302 } // namespace ntp_snippets |
| OLD | NEW |