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 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
126 } | 126 } |
127 | 127 |
128 void NTPSnippetsDatabase::DeleteImages(const NTPSnippet::PtrVector& snippets) { | 128 void NTPSnippetsDatabase::DeleteImages(const NTPSnippet::PtrVector& snippets) { |
129 std::unique_ptr<std::vector<std::string>> keys_to_remove( | 129 std::unique_ptr<std::vector<std::string>> keys_to_remove( |
130 new std::vector<std::string>()); | 130 new std::vector<std::string>()); |
131 for (const std::unique_ptr<NTPSnippet>& snippet : snippets) | 131 for (const std::unique_ptr<NTPSnippet>& snippet : snippets) |
132 keys_to_remove->emplace_back(snippet->id()); | 132 keys_to_remove->emplace_back(snippet->id()); |
133 DeleteImagesImpl(std::move(keys_to_remove)); | 133 DeleteImagesImpl(std::move(keys_to_remove)); |
134 } | 134 } |
135 | 135 |
| 136 void NTPSnippetsDatabase::DeleteUnreferencedImages( |
| 137 std::unique_ptr<std::set<std::string>> references, |
| 138 bool success, |
| 139 std::unique_ptr<std::vector<std::string>> keys) { |
| 140 if (!success) { |
| 141 DVLOG(1) << "NTPSnippetsDatabase garbage collection failed."; |
| 142 OnDatabaseError(); |
| 143 return; |
| 144 } |
| 145 auto keys_to_remove = base::MakeUnique<std::vector<std::string>>(); |
| 146 for (const std::string& key : *keys) { |
| 147 if (references->count(key) == 0) { |
| 148 keys_to_remove->emplace_back(key); |
| 149 } |
| 150 } |
| 151 DeleteImagesImpl(std::move(keys_to_remove)); |
| 152 } |
| 153 |
| 154 void NTPSnippetsDatabase::GarbageCollectImages( |
| 155 std::unique_ptr<std::set<std::string>> alive_snippets) { |
| 156 DCHECK(image_database_initialized_); |
| 157 image_database_->LoadKeys(base::Bind( |
| 158 &NTPSnippetsDatabase::DeleteUnreferencedImages, |
| 159 weak_ptr_factory_.GetWeakPtr(), base::Passed(std::move(alive_snippets)))); |
| 160 } |
| 161 |
136 void NTPSnippetsDatabase::OnDatabaseInited(bool success) { | 162 void NTPSnippetsDatabase::OnDatabaseInited(bool success) { |
137 DCHECK(!database_initialized_); | 163 DCHECK(!database_initialized_); |
138 if (!success) { | 164 if (!success) { |
139 DVLOG(1) << "NTPSnippetsDatabase init failed."; | 165 DVLOG(1) << "NTPSnippetsDatabase init failed."; |
140 OnDatabaseError(); | 166 OnDatabaseError(); |
141 return; | 167 return; |
142 } | 168 } |
143 database_initialized_ = true; | 169 database_initialized_ = true; |
144 if (IsInitialized()) | 170 if (IsInitialized()) |
145 ProcessPendingLoads(); | 171 ProcessPendingLoads(); |
(...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
284 std::unique_ptr<std::vector<std::string>> keys_to_remove) { | 310 std::unique_ptr<std::vector<std::string>> keys_to_remove) { |
285 DCHECK(IsInitialized()); | 311 DCHECK(IsInitialized()); |
286 | 312 |
287 image_database_->UpdateEntries( | 313 image_database_->UpdateEntries( |
288 base::MakeUnique<ImageKeyEntryVector>(), std::move(keys_to_remove), | 314 base::MakeUnique<ImageKeyEntryVector>(), std::move(keys_to_remove), |
289 base::Bind(&NTPSnippetsDatabase::OnImageDatabaseSaved, | 315 base::Bind(&NTPSnippetsDatabase::OnImageDatabaseSaved, |
290 weak_ptr_factory_.GetWeakPtr())); | 316 weak_ptr_factory_.GetWeakPtr())); |
291 } | 317 } |
292 | 318 |
293 } // namespace ntp_snippets | 319 } // namespace ntp_snippets |
OLD | NEW |