| 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 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 88 entries_to_save->emplace_back(snippet->id(), snippet->ToProto()); | 88 entries_to_save->emplace_back(snippet->id(), snippet->ToProto()); |
| 89 } | 89 } |
| 90 SaveSnippetsImpl(std::move(entries_to_save)); | 90 SaveSnippetsImpl(std::move(entries_to_save)); |
| 91 } | 91 } |
| 92 | 92 |
| 93 void NTPSnippetsDatabase::DeleteSnippet(const std::string& snippet_id) { | 93 void NTPSnippetsDatabase::DeleteSnippet(const std::string& snippet_id) { |
| 94 DeleteSnippets(base::MakeUnique<std::vector<std::string>>(1, snippet_id)); | 94 DeleteSnippets(base::MakeUnique<std::vector<std::string>>(1, snippet_id)); |
| 95 } | 95 } |
| 96 | 96 |
| 97 void NTPSnippetsDatabase::DeleteSnippets( | 97 void NTPSnippetsDatabase::DeleteSnippets( |
| 98 std::unique_ptr<std::vector<std::string>> keys_to_remove) { | 98 std::unique_ptr<std::vector<std::string>> snippet_ids) { |
| 99 DCHECK(IsInitialized()); | 99 DCHECK(IsInitialized()); |
| 100 | 100 |
| 101 std::unique_ptr<KeyEntryVector> entries_to_save(new KeyEntryVector()); | 101 std::unique_ptr<KeyEntryVector> entries_to_save(new KeyEntryVector()); |
| 102 database_->UpdateEntries(std::move(entries_to_save), | 102 database_->UpdateEntries(std::move(entries_to_save), std::move(snippet_ids), |
| 103 std::move(keys_to_remove), | |
| 104 base::Bind(&NTPSnippetsDatabase::OnDatabaseSaved, | 103 base::Bind(&NTPSnippetsDatabase::OnDatabaseSaved, |
| 105 weak_ptr_factory_.GetWeakPtr())); | 104 weak_ptr_factory_.GetWeakPtr())); |
| 106 } | 105 } |
| 107 | 106 |
| 108 void NTPSnippetsDatabase::LoadImage(const std::string& snippet_id, | 107 void NTPSnippetsDatabase::LoadImage(const std::string& snippet_id, |
| 109 const SnippetImageCallback& callback) { | 108 const SnippetImageCallback& callback) { |
| 110 if (IsInitialized()) | 109 if (IsInitialized()) |
| 111 LoadImageImpl(snippet_id, callback); | 110 LoadImageImpl(snippet_id, callback); |
| 112 else | 111 else |
| 113 pending_image_callbacks_.emplace_back(snippet_id, callback); | 112 pending_image_callbacks_.emplace_back(snippet_id, callback); |
| (...skipping 14 matching lines...) Expand all Loading... |
| 128 std::move(entries_to_save), base::MakeUnique<std::vector<std::string>>(), | 127 std::move(entries_to_save), base::MakeUnique<std::vector<std::string>>(), |
| 129 base::Bind(&NTPSnippetsDatabase::OnImageDatabaseSaved, | 128 base::Bind(&NTPSnippetsDatabase::OnImageDatabaseSaved, |
| 130 weak_ptr_factory_.GetWeakPtr())); | 129 weak_ptr_factory_.GetWeakPtr())); |
| 131 } | 130 } |
| 132 | 131 |
| 133 void NTPSnippetsDatabase::DeleteImage(const std::string& snippet_id) { | 132 void NTPSnippetsDatabase::DeleteImage(const std::string& snippet_id) { |
| 134 DeleteImages(base::MakeUnique<std::vector<std::string>>(1, snippet_id)); | 133 DeleteImages(base::MakeUnique<std::vector<std::string>>(1, snippet_id)); |
| 135 } | 134 } |
| 136 | 135 |
| 137 void NTPSnippetsDatabase::DeleteImages( | 136 void NTPSnippetsDatabase::DeleteImages( |
| 138 std::unique_ptr<std::vector<std::string>> keys_to_remove) { | 137 std::unique_ptr<std::vector<std::string>> snippet_ids) { |
| 139 DCHECK(IsInitialized()); | 138 DCHECK(IsInitialized()); |
| 140 image_database_->UpdateEntries( | 139 image_database_->UpdateEntries( |
| 141 base::MakeUnique<ImageKeyEntryVector>(), std::move(keys_to_remove), | 140 base::MakeUnique<ImageKeyEntryVector>(), std::move(snippet_ids), |
| 142 base::Bind(&NTPSnippetsDatabase::OnImageDatabaseSaved, | 141 base::Bind(&NTPSnippetsDatabase::OnImageDatabaseSaved, |
| 143 weak_ptr_factory_.GetWeakPtr())); | 142 weak_ptr_factory_.GetWeakPtr())); |
| 144 } | 143 } |
| 145 | 144 |
| 146 void NTPSnippetsDatabase::GarbageCollectImages( | 145 void NTPSnippetsDatabase::GarbageCollectImages( |
| 147 std::unique_ptr<std::set<std::string>> alive_snippet_ids) { | 146 std::unique_ptr<std::set<std::string>> alive_snippet_ids) { |
| 148 DCHECK(image_database_initialized_); | 147 DCHECK(image_database_initialized_); |
| 149 image_database_->LoadKeys( | 148 image_database_->LoadKeys( |
| 150 base::Bind(&NTPSnippetsDatabase::DeleteUnreferencedImages, | 149 base::Bind(&NTPSnippetsDatabase::DeleteUnreferencedImages, |
| 151 weak_ptr_factory_.GetWeakPtr(), | 150 weak_ptr_factory_.GetWeakPtr(), |
| (...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 306 for (const std::string& key : *image_keys) { | 305 for (const std::string& key : *image_keys) { |
| 307 if (references->count(key) == 0) { | 306 if (references->count(key) == 0) { |
| 308 keys_to_remove->emplace_back(key); | 307 keys_to_remove->emplace_back(key); |
| 309 } | 308 } |
| 310 } | 309 } |
| 311 DeleteImages(std::move(keys_to_remove)); | 310 DeleteImages(std::move(keys_to_remove)); |
| 312 } | 311 } |
| 313 | 312 |
| 314 | 313 |
| 315 } // namespace ntp_snippets | 314 } // namespace ntp_snippets |
| OLD | NEW |