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/ntp_snippets_database.h" | 5 #include "components/ntp_snippets/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 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
43 | 43 |
44 base::FilePath image_dir = database_dir.AppendASCII(kImageDatabaseFolder); | 44 base::FilePath image_dir = database_dir.AppendASCII(kImageDatabaseFolder); |
45 image_database_->Init(kImageDatabaseUMAClientName, image_dir, | 45 image_database_->Init(kImageDatabaseUMAClientName, image_dir, |
46 base::Bind(&NTPSnippetsDatabase::OnImageDatabaseInited, | 46 base::Bind(&NTPSnippetsDatabase::OnImageDatabaseInited, |
47 weak_ptr_factory_.GetWeakPtr())); | 47 weak_ptr_factory_.GetWeakPtr())); |
48 } | 48 } |
49 | 49 |
50 NTPSnippetsDatabase::~NTPSnippetsDatabase() {} | 50 NTPSnippetsDatabase::~NTPSnippetsDatabase() {} |
51 | 51 |
52 bool NTPSnippetsDatabase::IsInitialized() const { | 52 bool NTPSnippetsDatabase::IsInitialized() const { |
53 return database_ && database_initialized_ && image_database_ && | 53 return !IsErrorState() && database_initialized_ && |
54 image_database_initialized_; | 54 image_database_initialized_; |
55 } | 55 } |
56 | 56 |
| 57 bool NTPSnippetsDatabase::IsErrorState() const { |
| 58 return !database_ || !image_database_; |
| 59 } |
| 60 |
| 61 void NTPSnippetsDatabase::SetErrorCallback( |
| 62 const base::Closure& error_callback) { |
| 63 error_callback_ = error_callback; |
| 64 } |
| 65 |
57 void NTPSnippetsDatabase::LoadSnippets(const SnippetsCallback& callback) { | 66 void NTPSnippetsDatabase::LoadSnippets(const SnippetsCallback& callback) { |
58 if (IsInitialized()) | 67 if (IsInitialized()) |
59 LoadSnippetsImpl(callback); | 68 LoadSnippetsImpl(callback); |
60 else | 69 else |
61 pending_snippets_callbacks_.emplace_back(callback); | 70 pending_snippets_callbacks_.emplace_back(callback); |
62 } | 71 } |
63 | 72 |
64 void NTPSnippetsDatabase::SaveSnippet(const NTPSnippet& snippet) { | 73 void NTPSnippetsDatabase::SaveSnippet(const NTPSnippet& snippet) { |
65 std::unique_ptr<KeyEntryVector> entries_to_save(new KeyEntryVector()); | 74 std::unique_ptr<KeyEntryVector> entries_to_save(new KeyEntryVector()); |
66 entries_to_save->emplace_back(snippet.id(), snippet.ToProto()); | 75 entries_to_save->emplace_back(snippet.id(), snippet.ToProto()); |
(...skipping 24 matching lines...) Expand all Loading... |
91 void NTPSnippetsDatabase::LoadImage(const std::string& snippet_id, | 100 void NTPSnippetsDatabase::LoadImage(const std::string& snippet_id, |
92 const SnippetImageCallback& callback) { | 101 const SnippetImageCallback& callback) { |
93 if (IsInitialized()) | 102 if (IsInitialized()) |
94 LoadImageImpl(snippet_id, callback); | 103 LoadImageImpl(snippet_id, callback); |
95 else | 104 else |
96 pending_image_callbacks_.emplace_back(snippet_id, callback); | 105 pending_image_callbacks_.emplace_back(snippet_id, callback); |
97 } | 106 } |
98 | 107 |
99 void NTPSnippetsDatabase::SaveImage(const std::string& snippet_id, | 108 void NTPSnippetsDatabase::SaveImage(const std::string& snippet_id, |
100 const std::string& image_data) { | 109 const std::string& image_data) { |
101 // TODO(treib): After we pass errors to the client, DCHECK(IsInitialized()). | 110 DCHECK(IsInitialized()); |
102 if (!IsInitialized()) | |
103 return; | |
104 | 111 |
105 SnippetImageProto image_proto; | 112 SnippetImageProto image_proto; |
106 image_proto.set_data(image_data); | 113 image_proto.set_data(image_data); |
107 | 114 |
108 std::unique_ptr<ImageKeyEntryVector> entries_to_save( | 115 std::unique_ptr<ImageKeyEntryVector> entries_to_save( |
109 new ImageKeyEntryVector()); | 116 new ImageKeyEntryVector()); |
110 entries_to_save->emplace_back(snippet_id, std::move(image_proto)); | 117 entries_to_save->emplace_back(snippet_id, std::move(image_proto)); |
111 | 118 |
112 image_database_->UpdateEntries( | 119 image_database_->UpdateEntries( |
113 std::move(entries_to_save), | 120 std::move(entries_to_save), |
114 base::WrapUnique(new std::vector<std::string>()), | 121 base::WrapUnique(new std::vector<std::string>()), |
115 base::Bind(&NTPSnippetsDatabase::OnImageDatabaseSaved, | 122 base::Bind(&NTPSnippetsDatabase::OnImageDatabaseSaved, |
116 weak_ptr_factory_.GetWeakPtr())); | 123 weak_ptr_factory_.GetWeakPtr())); |
117 } | 124 } |
118 | 125 |
119 void NTPSnippetsDatabase::DeleteImage(const std::string& snippet_id) { | 126 void NTPSnippetsDatabase::DeleteImage(const std::string& snippet_id) { |
120 DeleteImagesImpl( | 127 DeleteImagesImpl( |
121 base::WrapUnique(new std::vector<std::string>(1, snippet_id))); | 128 base::WrapUnique(new std::vector<std::string>(1, snippet_id))); |
122 } | 129 } |
123 | 130 |
124 void NTPSnippetsDatabase::OnDatabaseInited(bool success) { | 131 void NTPSnippetsDatabase::OnDatabaseInited(bool success) { |
125 DCHECK(!database_initialized_); | 132 DCHECK(!database_initialized_); |
126 if (!success) { | 133 if (!success) { |
127 DVLOG(1) << "NTPSnippetsDatabase init failed."; | 134 DVLOG(1) << "NTPSnippetsDatabase init failed."; |
128 ResetDatabases(); | 135 OnDatabaseError(); |
129 return; | 136 return; |
130 } | 137 } |
131 database_initialized_ = true; | 138 database_initialized_ = true; |
132 if (IsInitialized()) | 139 if (IsInitialized()) |
133 ProcessPendingLoads(); | 140 ProcessPendingLoads(); |
134 } | 141 } |
135 | 142 |
136 void NTPSnippetsDatabase::OnDatabaseLoaded( | 143 void NTPSnippetsDatabase::OnDatabaseLoaded( |
137 const SnippetsCallback& callback, | 144 const SnippetsCallback& callback, |
138 bool success, | 145 bool success, |
139 std::unique_ptr<std::vector<SnippetProto>> entries) { | 146 std::unique_ptr<std::vector<SnippetProto>> entries) { |
140 if (!success) { | 147 if (!success) { |
141 DVLOG(1) << "NTPSnippetsDatabase load failed."; | 148 DVLOG(1) << "NTPSnippetsDatabase load failed."; |
142 ResetDatabases(); | 149 OnDatabaseError(); |
143 return; | 150 return; |
144 } | 151 } |
145 | 152 |
146 std::unique_ptr<std::vector<std::string>> keys_to_remove( | 153 std::unique_ptr<std::vector<std::string>> keys_to_remove( |
147 new std::vector<std::string>()); | 154 new std::vector<std::string>()); |
148 | 155 |
149 NTPSnippet::PtrVector snippets; | 156 NTPSnippet::PtrVector snippets; |
150 for (const SnippetProto& proto : *entries) { | 157 for (const SnippetProto& proto : *entries) { |
151 std::unique_ptr<NTPSnippet> snippet = NTPSnippet::CreateFromProto(proto); | 158 std::unique_ptr<NTPSnippet> snippet = NTPSnippet::CreateFromProto(proto); |
152 if (snippet) { | 159 if (snippet) { |
153 snippets.emplace_back(std::move(snippet)); | 160 snippets.emplace_back(std::move(snippet)); |
154 } else { | 161 } else { |
155 LOG(WARNING) << "Invalid proto from DB " << proto.id(); | 162 LOG(WARNING) << "Invalid proto from DB " << proto.id(); |
156 keys_to_remove->emplace_back(proto.id()); | 163 keys_to_remove->emplace_back(proto.id()); |
157 } | 164 } |
158 } | 165 } |
159 | 166 |
160 callback.Run(std::move(snippets)); | 167 callback.Run(std::move(snippets)); |
161 | 168 |
162 // If any of the snippet protos couldn't be converted to actual snippets, | 169 // If any of the snippet protos couldn't be converted to actual snippets, |
163 // clean them up now. | 170 // clean them up now. |
164 if (!keys_to_remove->empty()) | 171 if (!keys_to_remove->empty()) |
165 DeleteSnippetsImpl(std::move(keys_to_remove)); | 172 DeleteSnippetsImpl(std::move(keys_to_remove)); |
166 } | 173 } |
167 | 174 |
168 void NTPSnippetsDatabase::OnDatabaseSaved(bool success) { | 175 void NTPSnippetsDatabase::OnDatabaseSaved(bool success) { |
169 if (!success) { | 176 if (!success) { |
170 DVLOG(1) << "NTPSnippetsDatabase save failed."; | 177 DVLOG(1) << "NTPSnippetsDatabase save failed."; |
171 ResetDatabases(); | 178 OnDatabaseError(); |
172 } | 179 } |
173 } | 180 } |
174 | 181 |
175 void NTPSnippetsDatabase::OnImageDatabaseInited(bool success) { | 182 void NTPSnippetsDatabase::OnImageDatabaseInited(bool success) { |
176 DCHECK(!image_database_initialized_); | 183 DCHECK(!image_database_initialized_); |
177 if (!success) { | 184 if (!success) { |
178 DVLOG(1) << "NTPSnippetsDatabase init failed."; | 185 DVLOG(1) << "NTPSnippetsDatabase init failed."; |
179 ResetDatabases(); | 186 OnDatabaseError(); |
180 return; | 187 return; |
181 } | 188 } |
182 image_database_initialized_ = true; | 189 image_database_initialized_ = true; |
183 if (IsInitialized()) | 190 if (IsInitialized()) |
184 ProcessPendingLoads(); | 191 ProcessPendingLoads(); |
185 } | 192 } |
186 | 193 |
187 void NTPSnippetsDatabase::OnImageDatabaseLoaded( | 194 void NTPSnippetsDatabase::OnImageDatabaseLoaded( |
188 const SnippetImageCallback& callback, | 195 const SnippetImageCallback& callback, |
189 bool success, | 196 bool success, |
190 std::unique_ptr<SnippetImageProto> entry) { | 197 std::unique_ptr<SnippetImageProto> entry) { |
191 if (!success) { | 198 if (!success) { |
192 DVLOG(1) << "NTPSnippetsDatabase load failed."; | 199 DVLOG(1) << "NTPSnippetsDatabase load failed."; |
193 ResetDatabases(); | 200 OnDatabaseError(); |
194 return; | 201 return; |
195 } | 202 } |
196 | 203 |
197 if (!entry) { | 204 if (!entry) { |
198 callback.Run(std::string()); | 205 callback.Run(std::string()); |
199 return; | 206 return; |
200 } | 207 } |
201 | 208 |
202 std::unique_ptr<std::string> data(entry->release_data()); | 209 std::unique_ptr<std::string> data(entry->release_data()); |
203 callback.Run(std::move(*data)); | 210 callback.Run(std::move(*data)); |
204 } | 211 } |
205 | 212 |
206 void NTPSnippetsDatabase::OnImageDatabaseSaved(bool success) { | 213 void NTPSnippetsDatabase::OnImageDatabaseSaved(bool success) { |
207 if (!success) { | 214 if (!success) { |
208 DVLOG(1) << "NTPSnippetsDatabase save failed."; | 215 DVLOG(1) << "NTPSnippetsDatabase save failed."; |
209 ResetDatabases(); | 216 OnDatabaseError(); |
210 } | 217 } |
211 } | 218 } |
212 | 219 |
| 220 void NTPSnippetsDatabase::OnDatabaseError() { |
| 221 database_.reset(); |
| 222 image_database_.reset(); |
| 223 if (!error_callback_.is_null()) |
| 224 error_callback_.Run(); |
| 225 } |
| 226 |
213 void NTPSnippetsDatabase::ProcessPendingLoads() { | 227 void NTPSnippetsDatabase::ProcessPendingLoads() { |
214 DCHECK(IsInitialized()); | 228 DCHECK(IsInitialized()); |
215 | 229 |
216 for (const auto& callback : pending_snippets_callbacks_) | 230 for (const auto& callback : pending_snippets_callbacks_) |
217 LoadSnippetsImpl(callback); | 231 LoadSnippetsImpl(callback); |
218 pending_snippets_callbacks_.clear(); | 232 pending_snippets_callbacks_.clear(); |
219 | 233 |
220 for (const auto& id_callback : pending_image_callbacks_) | 234 for (const auto& id_callback : pending_image_callbacks_) |
221 LoadImageImpl(id_callback.first, id_callback.second); | 235 LoadImageImpl(id_callback.first, id_callback.second); |
222 pending_image_callbacks_.clear(); | 236 pending_image_callbacks_.clear(); |
223 } | 237 } |
224 | 238 |
225 void NTPSnippetsDatabase::LoadSnippetsImpl(const SnippetsCallback& callback) { | 239 void NTPSnippetsDatabase::LoadSnippetsImpl(const SnippetsCallback& callback) { |
226 DCHECK(IsInitialized()); | 240 DCHECK(IsInitialized()); |
227 database_->LoadEntries(base::Bind(&NTPSnippetsDatabase::OnDatabaseLoaded, | 241 database_->LoadEntries(base::Bind(&NTPSnippetsDatabase::OnDatabaseLoaded, |
228 weak_ptr_factory_.GetWeakPtr(), | 242 weak_ptr_factory_.GetWeakPtr(), |
229 callback)); | 243 callback)); |
230 } | 244 } |
231 | 245 |
232 void NTPSnippetsDatabase::SaveSnippetsImpl( | 246 void NTPSnippetsDatabase::SaveSnippetsImpl( |
233 std::unique_ptr<KeyEntryVector> entries_to_save) { | 247 std::unique_ptr<KeyEntryVector> entries_to_save) { |
234 // TODO(treib): After we pass errors to the client, DCHECK(IsInitialized()). | 248 DCHECK(IsInitialized()); |
235 if (!IsInitialized()) | |
236 return; | |
237 | 249 |
238 std::unique_ptr<std::vector<std::string>> keys_to_remove( | 250 std::unique_ptr<std::vector<std::string>> keys_to_remove( |
239 new std::vector<std::string>()); | 251 new std::vector<std::string>()); |
240 database_->UpdateEntries(std::move(entries_to_save), | 252 database_->UpdateEntries(std::move(entries_to_save), |
241 std::move(keys_to_remove), | 253 std::move(keys_to_remove), |
242 base::Bind(&NTPSnippetsDatabase::OnDatabaseSaved, | 254 base::Bind(&NTPSnippetsDatabase::OnDatabaseSaved, |
243 weak_ptr_factory_.GetWeakPtr())); | 255 weak_ptr_factory_.GetWeakPtr())); |
244 } | 256 } |
245 | 257 |
246 void NTPSnippetsDatabase::DeleteSnippetsImpl( | 258 void NTPSnippetsDatabase::DeleteSnippetsImpl( |
247 std::unique_ptr<std::vector<std::string>> keys_to_remove) { | 259 std::unique_ptr<std::vector<std::string>> keys_to_remove) { |
248 // TODO(treib): After we pass errors to the client, DCHECK(IsInitialized()). | 260 DCHECK(IsInitialized()); |
249 if (!IsInitialized()) | |
250 return; | |
251 | 261 |
252 DeleteImagesImpl( | 262 DeleteImagesImpl( |
253 base::WrapUnique(new std::vector<std::string>(*keys_to_remove))); | 263 base::WrapUnique(new std::vector<std::string>(*keys_to_remove))); |
254 | 264 |
255 std::unique_ptr<KeyEntryVector> entries_to_save(new KeyEntryVector()); | 265 std::unique_ptr<KeyEntryVector> entries_to_save(new KeyEntryVector()); |
256 database_->UpdateEntries(std::move(entries_to_save), | 266 database_->UpdateEntries(std::move(entries_to_save), |
257 std::move(keys_to_remove), | 267 std::move(keys_to_remove), |
258 base::Bind(&NTPSnippetsDatabase::OnDatabaseSaved, | 268 base::Bind(&NTPSnippetsDatabase::OnDatabaseSaved, |
259 weak_ptr_factory_.GetWeakPtr())); | 269 weak_ptr_factory_.GetWeakPtr())); |
260 } | 270 } |
261 | 271 |
262 void NTPSnippetsDatabase::LoadImageImpl(const std::string& snippet_id, | 272 void NTPSnippetsDatabase::LoadImageImpl(const std::string& snippet_id, |
263 const SnippetImageCallback& callback) { | 273 const SnippetImageCallback& callback) { |
264 DCHECK(IsInitialized()); | 274 DCHECK(IsInitialized()); |
265 image_database_->GetEntry( | 275 image_database_->GetEntry( |
266 snippet_id, | 276 snippet_id, |
267 base::Bind(&NTPSnippetsDatabase::OnImageDatabaseLoaded, | 277 base::Bind(&NTPSnippetsDatabase::OnImageDatabaseLoaded, |
268 weak_ptr_factory_.GetWeakPtr(), callback)); | 278 weak_ptr_factory_.GetWeakPtr(), callback)); |
269 } | 279 } |
270 | 280 |
271 void NTPSnippetsDatabase::DeleteImagesImpl( | 281 void NTPSnippetsDatabase::DeleteImagesImpl( |
272 std::unique_ptr<std::vector<std::string>> keys_to_remove) { | 282 std::unique_ptr<std::vector<std::string>> keys_to_remove) { |
273 // TODO(treib): After we pass errors to the client, DCHECK(IsInitialized()). | 283 DCHECK(IsInitialized()); |
274 if (!IsInitialized()) | |
275 return; | |
276 | 284 |
277 image_database_->UpdateEntries( | 285 image_database_->UpdateEntries( |
278 base::WrapUnique(new ImageKeyEntryVector()), | 286 base::WrapUnique(new ImageKeyEntryVector()), |
279 std::move(keys_to_remove), | 287 std::move(keys_to_remove), |
280 base::Bind(&NTPSnippetsDatabase::OnImageDatabaseSaved, | 288 base::Bind(&NTPSnippetsDatabase::OnImageDatabaseSaved, |
281 weak_ptr_factory_.GetWeakPtr())); | 289 weak_ptr_factory_.GetWeakPtr())); |
282 } | 290 } |
283 | 291 |
284 void NTPSnippetsDatabase::ResetDatabases() { | |
285 database_.reset(); | |
286 image_database_.reset(); | |
287 } | |
288 | |
289 } // namespace ntp_snippets | 292 } // namespace ntp_snippets |
OLD | NEW |