| 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/offline_pages/offline_page_metadata_store_sql.h" | 5 #include "components/offline_pages/offline_page_metadata_store_sql.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/files/file_path.h" | 8 #include "base/files/file_path.h" |
| 9 #include "base/files/file_util.h" | 9 #include "base/files/file_util.h" |
| 10 #include "base/location.h" | 10 #include "base/location.h" |
| (...skipping 278 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 289 if (statement.Succeeded()) { | 289 if (statement.Succeeded()) { |
| 290 NotifyLoadResult(runner, callback, OfflinePageMetadataStore::LOAD_SUCCEEDED, | 290 NotifyLoadResult(runner, callback, OfflinePageMetadataStore::LOAD_SUCCEEDED, |
| 291 result); | 291 result); |
| 292 } else { | 292 } else { |
| 293 result.clear(); | 293 result.clear(); |
| 294 NotifyLoadResult(runner, callback, | 294 NotifyLoadResult(runner, callback, |
| 295 OfflinePageMetadataStore::STORE_LOAD_FAILED, result); | 295 OfflinePageMetadataStore::STORE_LOAD_FAILED, result); |
| 296 } | 296 } |
| 297 } | 297 } |
| 298 | 298 |
| 299 void AddOrUpdateOfflinePageSync( | 299 void AddOfflinePageSync(sql::Connection* db, |
| 300 const OfflinePageItem& offline_page, | 300 scoped_refptr<base::SingleThreadTaskRunner> runner, |
| 301 const OfflinePageItem& offline_page, |
| 302 const OfflinePageMetadataStore::AddCallback& callback) { |
| 303 // TODO(fgorski): Only insert should happen here. |
| 304 InsertOrReplace(db, offline_page); |
| 305 runner->PostTask(FROM_HERE, |
| 306 base::Bind(callback, OfflinePageMetadataStore::SUCCESS)); |
| 307 } |
| 308 |
| 309 void UpdateOfflinePagesSync( |
| 301 sql::Connection* db, | 310 sql::Connection* db, |
| 302 scoped_refptr<base::SingleThreadTaskRunner> runner, | 311 scoped_refptr<base::SingleThreadTaskRunner> runner, |
| 312 const std::vector<OfflinePageItem>& pages, |
| 303 const OfflinePageMetadataStore::UpdateCallback& callback) { | 313 const OfflinePageMetadataStore::UpdateCallback& callback) { |
| 304 // TODO(bburns): add UMA metrics here (and for levelDB). | 314 // TODO(fgorski): Update the callback to provide information about all items |
| 305 bool ok = InsertOrReplace(db, offline_page); | 315 // and not just a high level boolean. |
| 306 runner->PostTask(FROM_HERE, base::Bind(callback, ok)); | 316 sql::Transaction transaction(db); |
| 317 if (!transaction.Begin()) { |
| 318 runner->PostTask(FROM_HERE, base::Bind(callback, false)); |
| 319 return; |
| 320 } |
| 321 |
| 322 // TODO(fgorski): Switch to only accept updates and not create new items. |
| 323 for (auto& page : pages) |
| 324 InsertOrReplace(db, page); |
| 325 |
| 326 bool result = transaction.Commit(); |
| 327 runner->PostTask(FROM_HERE, base::Bind(callback, result)); |
| 307 } | 328 } |
| 308 | 329 |
| 309 void RemoveOfflinePagesSync( | 330 void RemoveOfflinePagesSync( |
| 310 const std::vector<int64_t>& offline_ids, | 331 const std::vector<int64_t>& offline_ids, |
| 311 sql::Connection* db, | 332 sql::Connection* db, |
| 312 scoped_refptr<base::SingleThreadTaskRunner> runner, | 333 scoped_refptr<base::SingleThreadTaskRunner> runner, |
| 313 const OfflinePageMetadataStore::UpdateCallback& callback) { | 334 const OfflinePageMetadataStore::UpdateCallback& callback) { |
| 314 // TODO(bburns): add UMA metrics here (and for levelDB). | 335 // TODO(bburns): add UMA metrics here (and for levelDB). |
| 315 | 336 |
| 316 // If you create a transaction but don't Commit() it is automatically | 337 // If you create a transaction but don't Commit() it is automatically |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 369 } | 390 } |
| 370 } | 391 } |
| 371 | 392 |
| 372 void OfflinePageMetadataStoreSQL::GetOfflinePages( | 393 void OfflinePageMetadataStoreSQL::GetOfflinePages( |
| 373 const LoadCallback& callback) { | 394 const LoadCallback& callback) { |
| 374 background_task_runner_->PostTask( | 395 background_task_runner_->PostTask( |
| 375 FROM_HERE, base::Bind(&GetOfflinePagesSync, db_.get(), | 396 FROM_HERE, base::Bind(&GetOfflinePagesSync, db_.get(), |
| 376 base::ThreadTaskRunnerHandle::Get(), callback)); | 397 base::ThreadTaskRunnerHandle::Get(), callback)); |
| 377 } | 398 } |
| 378 | 399 |
| 379 void OfflinePageMetadataStoreSQL::AddOrUpdateOfflinePage( | 400 void OfflinePageMetadataStoreSQL::AddOfflinePage( |
| 380 const OfflinePageItem& offline_page, | 401 const OfflinePageItem& offline_page, |
| 381 const UpdateCallback& callback) { | 402 const AddCallback& callback) { |
| 382 DCHECK(db_.get()); | 403 if (!CheckDb(base::Bind(callback, STORE_ERROR))) |
| 404 return; |
| 405 |
| 383 background_task_runner_->PostTask( | 406 background_task_runner_->PostTask( |
| 384 FROM_HERE, | 407 FROM_HERE, |
| 385 base::Bind(&AddOrUpdateOfflinePageSync, offline_page, db_.get(), | 408 base::Bind(&AddOfflinePageSync, db_.get(), |
| 386 base::ThreadTaskRunnerHandle::Get(), callback)); | 409 base::ThreadTaskRunnerHandle::Get(), offline_page, callback)); |
| 410 } |
| 411 |
| 412 void OfflinePageMetadataStoreSQL::UpdateOfflinePages( |
| 413 const std::vector<OfflinePageItem>& pages, |
| 414 const UpdateCallback& callback) { |
| 415 if (!CheckDb(base::Bind(callback, false))) |
| 416 return; |
| 417 |
| 418 background_task_runner_->PostTask( |
| 419 FROM_HERE, |
| 420 base::Bind(&UpdateOfflinePagesSync, db_.get(), |
| 421 base::ThreadTaskRunnerHandle::Get(), pages, callback)); |
| 387 } | 422 } |
| 388 | 423 |
| 389 void OfflinePageMetadataStoreSQL::RemoveOfflinePages( | 424 void OfflinePageMetadataStoreSQL::RemoveOfflinePages( |
| 390 const std::vector<int64_t>& offline_ids, | 425 const std::vector<int64_t>& offline_ids, |
| 391 const UpdateCallback& callback) { | 426 const UpdateCallback& callback) { |
| 392 DCHECK(db_.get()); | 427 DCHECK(db_.get()); |
| 393 | 428 |
| 394 if (offline_ids.empty()) { | 429 if (offline_ids.empty()) { |
| 395 // Nothing to do, but post a callback instead of calling directly | 430 // Nothing to do, but post a callback instead of calling directly |
| 396 // to preserve the async style behavior to prevent bugs. | 431 // to preserve the async style behavior to prevent bugs. |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 454 DCHECK(db_.get()); | 489 DCHECK(db_.get()); |
| 455 if (!db_.get()) { | 490 if (!db_.get()) { |
| 456 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, | 491 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, |
| 457 base::Bind(callback)); | 492 base::Bind(callback)); |
| 458 return false; | 493 return false; |
| 459 } | 494 } |
| 460 return true; | 495 return true; |
| 461 } | 496 } |
| 462 | 497 |
| 463 } // namespace offline_pages | 498 } // namespace offline_pages |
| OLD | NEW |