| 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/remote_suggestions_database.h" | 5 #include "components/ntp_snippets/remote/remote_suggestions_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 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 57 bool RemoteSuggestionsDatabase::IsErrorState() const { | 57 bool RemoteSuggestionsDatabase::IsErrorState() const { |
| 58 return !database_ || !image_database_; | 58 return !database_ || !image_database_; |
| 59 } | 59 } |
| 60 | 60 |
| 61 void RemoteSuggestionsDatabase::SetErrorCallback( | 61 void RemoteSuggestionsDatabase::SetErrorCallback( |
| 62 const base::Closure& error_callback) { | 62 const base::Closure& error_callback) { |
| 63 error_callback_ = error_callback; | 63 error_callback_ = error_callback; |
| 64 } | 64 } |
| 65 | 65 |
| 66 void RemoteSuggestionsDatabase::LoadSnippets(const SnippetsCallback& callback) { | 66 void RemoteSuggestionsDatabase::LoadSnippets(const SnippetsCallback& callback) { |
| 67 if (IsInitialized()) | 67 if (IsInitialized()) { |
| 68 LoadSnippetsImpl(callback); | 68 LoadSnippetsImpl(callback); |
| 69 else | 69 } else { |
| 70 pending_snippets_callbacks_.emplace_back(callback); | 70 pending_snippets_callbacks_.emplace_back(callback); |
| 71 } |
| 71 } | 72 } |
| 72 | 73 |
| 73 void RemoteSuggestionsDatabase::SaveSnippet(const NTPSnippet& snippet) { | 74 void RemoteSuggestionsDatabase::SaveSnippet(const NTPSnippet& snippet) { |
| 74 std::unique_ptr<KeyEntryVector> entries_to_save(new KeyEntryVector()); | 75 std::unique_ptr<KeyEntryVector> entries_to_save(new KeyEntryVector()); |
| 75 // OnDatabaseLoaded relies on the detail that the primary snippet id goes | 76 // OnDatabaseLoaded relies on the detail that the primary snippet id goes |
| 76 // first in the protocol representation. | 77 // first in the protocol representation. |
| 77 DCHECK_EQ(snippet.ToProto().ids(0), snippet.id()); | 78 DCHECK_EQ(snippet.ToProto().ids(0), snippet.id()); |
| 78 entries_to_save->emplace_back(snippet.id(), snippet.ToProto()); | 79 entries_to_save->emplace_back(snippet.id(), snippet.ToProto()); |
| 79 SaveSnippetsImpl(std::move(entries_to_save)); | 80 SaveSnippetsImpl(std::move(entries_to_save)); |
| 80 } | 81 } |
| (...skipping 21 matching lines...) Expand all Loading... |
| 102 std::unique_ptr<KeyEntryVector> entries_to_save(new KeyEntryVector()); | 103 std::unique_ptr<KeyEntryVector> entries_to_save(new KeyEntryVector()); |
| 103 database_->UpdateEntries( | 104 database_->UpdateEntries( |
| 104 std::move(entries_to_save), std::move(snippet_ids), | 105 std::move(entries_to_save), std::move(snippet_ids), |
| 105 base::Bind(&RemoteSuggestionsDatabase::OnDatabaseSaved, | 106 base::Bind(&RemoteSuggestionsDatabase::OnDatabaseSaved, |
| 106 weak_ptr_factory_.GetWeakPtr())); | 107 weak_ptr_factory_.GetWeakPtr())); |
| 107 } | 108 } |
| 108 | 109 |
| 109 void RemoteSuggestionsDatabase::LoadImage( | 110 void RemoteSuggestionsDatabase::LoadImage( |
| 110 const std::string& snippet_id, | 111 const std::string& snippet_id, |
| 111 const SnippetImageCallback& callback) { | 112 const SnippetImageCallback& callback) { |
| 112 if (IsInitialized()) | 113 if (IsInitialized()) { |
| 113 LoadImageImpl(snippet_id, callback); | 114 LoadImageImpl(snippet_id, callback); |
| 114 else | 115 } else { |
| 115 pending_image_callbacks_.emplace_back(snippet_id, callback); | 116 pending_image_callbacks_.emplace_back(snippet_id, callback); |
| 117 } |
| 116 } | 118 } |
| 117 | 119 |
| 118 void RemoteSuggestionsDatabase::SaveImage(const std::string& snippet_id, | 120 void RemoteSuggestionsDatabase::SaveImage(const std::string& snippet_id, |
| 119 const std::string& image_data) { | 121 const std::string& image_data) { |
| 120 DCHECK(IsInitialized()); | 122 DCHECK(IsInitialized()); |
| 121 | 123 |
| 122 SnippetImageProto image_proto; | 124 SnippetImageProto image_proto; |
| 123 image_proto.set_data(image_data); | 125 image_proto.set_data(image_data); |
| 124 | 126 |
| 125 std::unique_ptr<ImageKeyEntryVector> entries_to_save( | 127 std::unique_ptr<ImageKeyEntryVector> entries_to_save( |
| (...skipping 29 matching lines...) Expand all Loading... |
| 155 } | 157 } |
| 156 | 158 |
| 157 void RemoteSuggestionsDatabase::OnDatabaseInited(bool success) { | 159 void RemoteSuggestionsDatabase::OnDatabaseInited(bool success) { |
| 158 DCHECK(!database_initialized_); | 160 DCHECK(!database_initialized_); |
| 159 if (!success) { | 161 if (!success) { |
| 160 DVLOG(1) << "RemoteSuggestionsDatabase init failed."; | 162 DVLOG(1) << "RemoteSuggestionsDatabase init failed."; |
| 161 OnDatabaseError(); | 163 OnDatabaseError(); |
| 162 return; | 164 return; |
| 163 } | 165 } |
| 164 database_initialized_ = true; | 166 database_initialized_ = true; |
| 165 if (IsInitialized()) | 167 if (IsInitialized()) { |
| 166 ProcessPendingLoads(); | 168 ProcessPendingLoads(); |
| 169 } |
| 167 } | 170 } |
| 168 | 171 |
| 169 void RemoteSuggestionsDatabase::OnDatabaseLoaded( | 172 void RemoteSuggestionsDatabase::OnDatabaseLoaded( |
| 170 const SnippetsCallback& callback, | 173 const SnippetsCallback& callback, |
| 171 bool success, | 174 bool success, |
| 172 std::unique_ptr<std::vector<SnippetProto>> entries) { | 175 std::unique_ptr<std::vector<SnippetProto>> entries) { |
| 173 if (!success) { | 176 if (!success) { |
| 174 DVLOG(1) << "RemoteSuggestionsDatabase load failed."; | 177 DVLOG(1) << "RemoteSuggestionsDatabase load failed."; |
| 175 OnDatabaseError(); | 178 OnDatabaseError(); |
| 176 return; | 179 return; |
| (...skipping 15 matching lines...) Expand all Loading... |
| 192 LOG(WARNING) | 195 LOG(WARNING) |
| 193 << "Loaded proto without ID from the DB. Cannot clean this up."; | 196 << "Loaded proto without ID from the DB. Cannot clean this up."; |
| 194 } | 197 } |
| 195 } | 198 } |
| 196 } | 199 } |
| 197 | 200 |
| 198 callback.Run(std::move(snippets)); | 201 callback.Run(std::move(snippets)); |
| 199 | 202 |
| 200 // If any of the snippet protos couldn't be converted to actual snippets, | 203 // If any of the snippet protos couldn't be converted to actual snippets, |
| 201 // clean them up now. | 204 // clean them up now. |
| 202 if (!keys_to_remove->empty()) | 205 if (!keys_to_remove->empty()) { |
| 203 DeleteSnippets(std::move(keys_to_remove)); | 206 DeleteSnippets(std::move(keys_to_remove)); |
| 207 } |
| 204 } | 208 } |
| 205 | 209 |
| 206 void RemoteSuggestionsDatabase::OnDatabaseSaved(bool success) { | 210 void RemoteSuggestionsDatabase::OnDatabaseSaved(bool success) { |
| 207 if (!success) { | 211 if (!success) { |
| 208 DVLOG(1) << "RemoteSuggestionsDatabase save failed."; | 212 DVLOG(1) << "RemoteSuggestionsDatabase save failed."; |
| 209 OnDatabaseError(); | 213 OnDatabaseError(); |
| 210 } | 214 } |
| 211 } | 215 } |
| 212 | 216 |
| 213 void RemoteSuggestionsDatabase::OnImageDatabaseInited(bool success) { | 217 void RemoteSuggestionsDatabase::OnImageDatabaseInited(bool success) { |
| 214 DCHECK(!image_database_initialized_); | 218 DCHECK(!image_database_initialized_); |
| 215 if (!success) { | 219 if (!success) { |
| 216 DVLOG(1) << "RemoteSuggestionsDatabase init failed."; | 220 DVLOG(1) << "RemoteSuggestionsDatabase init failed."; |
| 217 OnDatabaseError(); | 221 OnDatabaseError(); |
| 218 return; | 222 return; |
| 219 } | 223 } |
| 220 image_database_initialized_ = true; | 224 image_database_initialized_ = true; |
| 221 if (IsInitialized()) | 225 if (IsInitialized()) { |
| 222 ProcessPendingLoads(); | 226 ProcessPendingLoads(); |
| 227 } |
| 223 } | 228 } |
| 224 | 229 |
| 225 void RemoteSuggestionsDatabase::OnImageDatabaseLoaded( | 230 void RemoteSuggestionsDatabase::OnImageDatabaseLoaded( |
| 226 const SnippetImageCallback& callback, | 231 const SnippetImageCallback& callback, |
| 227 bool success, | 232 bool success, |
| 228 std::unique_ptr<SnippetImageProto> entry) { | 233 std::unique_ptr<SnippetImageProto> entry) { |
| 229 if (!success) { | 234 if (!success) { |
| 230 DVLOG(1) << "RemoteSuggestionsDatabase load failed."; | 235 DVLOG(1) << "RemoteSuggestionsDatabase load failed."; |
| 231 OnDatabaseError(); | 236 OnDatabaseError(); |
| 232 return; | 237 return; |
| (...skipping 11 matching lines...) Expand all Loading... |
| 244 void RemoteSuggestionsDatabase::OnImageDatabaseSaved(bool success) { | 249 void RemoteSuggestionsDatabase::OnImageDatabaseSaved(bool success) { |
| 245 if (!success) { | 250 if (!success) { |
| 246 DVLOG(1) << "RemoteSuggestionsDatabase save failed."; | 251 DVLOG(1) << "RemoteSuggestionsDatabase save failed."; |
| 247 OnDatabaseError(); | 252 OnDatabaseError(); |
| 248 } | 253 } |
| 249 } | 254 } |
| 250 | 255 |
| 251 void RemoteSuggestionsDatabase::OnDatabaseError() { | 256 void RemoteSuggestionsDatabase::OnDatabaseError() { |
| 252 database_.reset(); | 257 database_.reset(); |
| 253 image_database_.reset(); | 258 image_database_.reset(); |
| 254 if (!error_callback_.is_null()) | 259 if (!error_callback_.is_null()) { |
| 255 error_callback_.Run(); | 260 error_callback_.Run(); |
| 261 } |
| 256 } | 262 } |
| 257 | 263 |
| 258 void RemoteSuggestionsDatabase::ProcessPendingLoads() { | 264 void RemoteSuggestionsDatabase::ProcessPendingLoads() { |
| 259 DCHECK(IsInitialized()); | 265 DCHECK(IsInitialized()); |
| 260 | 266 |
| 261 for (const auto& callback : pending_snippets_callbacks_) | 267 for (const auto& callback : pending_snippets_callbacks_) { |
| 262 LoadSnippetsImpl(callback); | 268 LoadSnippetsImpl(callback); |
| 269 } |
| 263 pending_snippets_callbacks_.clear(); | 270 pending_snippets_callbacks_.clear(); |
| 264 | 271 |
| 265 for (const auto& id_callback : pending_image_callbacks_) | 272 for (const auto& id_callback : pending_image_callbacks_) { |
| 266 LoadImageImpl(id_callback.first, id_callback.second); | 273 LoadImageImpl(id_callback.first, id_callback.second); |
| 274 } |
| 267 pending_image_callbacks_.clear(); | 275 pending_image_callbacks_.clear(); |
| 268 } | 276 } |
| 269 | 277 |
| 270 void RemoteSuggestionsDatabase::LoadSnippetsImpl( | 278 void RemoteSuggestionsDatabase::LoadSnippetsImpl( |
| 271 const SnippetsCallback& callback) { | 279 const SnippetsCallback& callback) { |
| 272 DCHECK(IsInitialized()); | 280 DCHECK(IsInitialized()); |
| 273 database_->LoadEntries( | 281 database_->LoadEntries( |
| 274 base::Bind(&RemoteSuggestionsDatabase::OnDatabaseLoaded, | 282 base::Bind(&RemoteSuggestionsDatabase::OnDatabaseLoaded, |
| 275 weak_ptr_factory_.GetWeakPtr(), callback)); | 283 weak_ptr_factory_.GetWeakPtr(), callback)); |
| 276 } | 284 } |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 308 auto keys_to_remove = base::MakeUnique<std::vector<std::string>>(); | 316 auto keys_to_remove = base::MakeUnique<std::vector<std::string>>(); |
| 309 for (const std::string& key : *image_keys) { | 317 for (const std::string& key : *image_keys) { |
| 310 if (references->count(key) == 0) { | 318 if (references->count(key) == 0) { |
| 311 keys_to_remove->emplace_back(key); | 319 keys_to_remove->emplace_back(key); |
| 312 } | 320 } |
| 313 } | 321 } |
| 314 DeleteImages(std::move(keys_to_remove)); | 322 DeleteImages(std::move(keys_to_remove)); |
| 315 } | 323 } |
| 316 | 324 |
| 317 } // namespace ntp_snippets | 325 } // namespace ntp_snippets |
| OLD | NEW |