OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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/offline_pages/offline_page_metadata_store_impl.h" | 5 #include "components/offline_pages/offline_page_metadata_store_impl.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
| 8 #include <utility> |
8 #include <vector> | 9 #include <vector> |
9 | 10 |
10 #include "base/bind.h" | 11 #include "base/bind.h" |
11 #include "base/files/file_path.h" | 12 #include "base/files/file_path.h" |
12 #include "base/location.h" | 13 #include "base/location.h" |
13 #include "base/metrics/histogram_macros.h" | 14 #include "base/metrics/histogram_macros.h" |
14 #include "base/sequenced_task_runner.h" | 15 #include "base/sequenced_task_runner.h" |
15 #include "base/strings/string_number_conversions.h" | 16 #include "base/strings/string_number_conversions.h" |
16 #include "base/strings/utf_string_conversions.h" | 17 #include "base/strings/utf_string_conversions.h" |
17 #include "base/thread_task_runner_handle.h" | 18 #include "base/thread_task_runner_handle.h" |
(...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
182 new ProtoDatabase<OfflinePageEntry>::KeyEntryVector()); | 183 new ProtoDatabase<OfflinePageEntry>::KeyEntryVector()); |
183 scoped_ptr<std::vector<std::string>> keys_to_remove( | 184 scoped_ptr<std::vector<std::string>> keys_to_remove( |
184 new std::vector<std::string>()); | 185 new std::vector<std::string>()); |
185 | 186 |
186 OfflinePageEntry offline_page_proto; | 187 OfflinePageEntry offline_page_proto; |
187 OfflinePageItemToEntry(offline_page_item, &offline_page_proto); | 188 OfflinePageItemToEntry(offline_page_item, &offline_page_proto); |
188 entries_to_save->push_back( | 189 entries_to_save->push_back( |
189 std::make_pair(base::Int64ToString(offline_page_item.bookmark_id), | 190 std::make_pair(base::Int64ToString(offline_page_item.bookmark_id), |
190 offline_page_proto)); | 191 offline_page_proto)); |
191 | 192 |
192 UpdateEntries(entries_to_save.Pass(), keys_to_remove.Pass(), callback); | 193 UpdateEntries(std::move(entries_to_save), std::move(keys_to_remove), |
| 194 callback); |
193 } | 195 } |
194 | 196 |
195 void OfflinePageMetadataStoreImpl::RemoveOfflinePages( | 197 void OfflinePageMetadataStoreImpl::RemoveOfflinePages( |
196 const std::vector<int64_t>& bookmark_ids, | 198 const std::vector<int64_t>& bookmark_ids, |
197 const UpdateCallback& callback) { | 199 const UpdateCallback& callback) { |
198 scoped_ptr<ProtoDatabase<OfflinePageEntry>::KeyEntryVector> entries_to_save( | 200 scoped_ptr<ProtoDatabase<OfflinePageEntry>::KeyEntryVector> entries_to_save( |
199 new ProtoDatabase<OfflinePageEntry>::KeyEntryVector()); | 201 new ProtoDatabase<OfflinePageEntry>::KeyEntryVector()); |
200 scoped_ptr<std::vector<std::string>> keys_to_remove( | 202 scoped_ptr<std::vector<std::string>> keys_to_remove( |
201 new std::vector<std::string>()); | 203 new std::vector<std::string>()); |
202 | 204 |
203 for (int64_t id : bookmark_ids) | 205 for (int64_t id : bookmark_ids) |
204 keys_to_remove->push_back(base::Int64ToString(id)); | 206 keys_to_remove->push_back(base::Int64ToString(id)); |
205 | 207 |
206 UpdateEntries(entries_to_save.Pass(), keys_to_remove.Pass(), callback); | 208 UpdateEntries(std::move(entries_to_save), std::move(keys_to_remove), |
| 209 callback); |
207 } | 210 } |
208 | 211 |
209 void OfflinePageMetadataStoreImpl::UpdateEntries( | 212 void OfflinePageMetadataStoreImpl::UpdateEntries( |
210 scoped_ptr<ProtoDatabase<OfflinePageEntry>::KeyEntryVector> entries_to_save, | 213 scoped_ptr<ProtoDatabase<OfflinePageEntry>::KeyEntryVector> entries_to_save, |
211 scoped_ptr<std::vector<std::string>> keys_to_remove, | 214 scoped_ptr<std::vector<std::string>> keys_to_remove, |
212 const UpdateCallback& callback) { | 215 const UpdateCallback& callback) { |
213 if (!database_.get()) { | 216 if (!database_.get()) { |
214 // Failing fast here, because DB is not initialized, and there is nothing | 217 // Failing fast here, because DB is not initialized, and there is nothing |
215 // that can be done about it. | 218 // that can be done about it. |
216 // Callback is invoked through message loop to avoid improper retry and | 219 // Callback is invoked through message loop to avoid improper retry and |
217 // simplify testing. | 220 // simplify testing. |
218 DVLOG(1) << "Offline pages database not available in UpdateEntries."; | 221 DVLOG(1) << "Offline pages database not available in UpdateEntries."; |
219 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, | 222 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, |
220 base::Bind(callback, false)); | 223 base::Bind(callback, false)); |
221 return; | 224 return; |
222 } | 225 } |
223 | 226 |
224 database_->UpdateEntries( | 227 database_->UpdateEntries( |
225 entries_to_save.Pass(), keys_to_remove.Pass(), | 228 std::move(entries_to_save), std::move(keys_to_remove), |
226 base::Bind(&OfflinePageMetadataStoreImpl::UpdateDone, | 229 base::Bind(&OfflinePageMetadataStoreImpl::UpdateDone, |
227 weak_ptr_factory_.GetWeakPtr(), | 230 weak_ptr_factory_.GetWeakPtr(), callback)); |
228 callback)); | |
229 } | 231 } |
230 | 232 |
231 void OfflinePageMetadataStoreImpl::UpdateDone( | 233 void OfflinePageMetadataStoreImpl::UpdateDone( |
232 const OfflinePageMetadataStore::UpdateCallback& callback, | 234 const OfflinePageMetadataStore::UpdateCallback& callback, |
233 bool success) { | 235 bool success) { |
234 if (!success) { | 236 if (!success) { |
235 // TODO(fgorski): Add UMA for this case. Consider rebuilding the store. | 237 // TODO(fgorski): Add UMA for this case. Consider rebuilding the store. |
236 DVLOG(1) << "Offline pages database update failed."; | 238 DVLOG(1) << "Offline pages database update failed."; |
237 } | 239 } |
238 | 240 |
239 callback.Run(success); | 241 callback.Run(success); |
240 } | 242 } |
241 | 243 |
242 void OfflinePageMetadataStoreImpl::Reset(const ResetCallback& callback) { | 244 void OfflinePageMetadataStoreImpl::Reset(const ResetCallback& callback) { |
243 database_->Destroy( | 245 database_->Destroy( |
244 base::Bind(&OfflinePageMetadataStoreImpl::ResetDone, | 246 base::Bind(&OfflinePageMetadataStoreImpl::ResetDone, |
245 weak_ptr_factory_.GetWeakPtr(), | 247 weak_ptr_factory_.GetWeakPtr(), |
246 callback)); | 248 callback)); |
247 } | 249 } |
248 | 250 |
249 void OfflinePageMetadataStoreImpl::ResetDone( | 251 void OfflinePageMetadataStoreImpl::ResetDone( |
250 const ResetCallback& callback, | 252 const ResetCallback& callback, |
251 bool success) { | 253 bool success) { |
252 database_.reset(); | 254 database_.reset(); |
253 weak_ptr_factory_.InvalidateWeakPtrs(); | 255 weak_ptr_factory_.InvalidateWeakPtrs(); |
254 callback.Run(success); | 256 callback.Run(success); |
255 } | 257 } |
256 | 258 |
257 } // namespace offline_pages | 259 } // namespace offline_pages |
OLD | NEW |