| 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" |
| 11 #include "base/logging.h" | 11 #include "base/logging.h" |
| 12 #include "base/metrics/histogram_macros.h" | 12 #include "base/metrics/histogram_macros.h" |
| 13 #include "base/sequenced_task_runner.h" | 13 #include "base/sequenced_task_runner.h" |
| 14 #include "base/strings/utf_string_conversions.h" | 14 #include "base/strings/utf_string_conversions.h" |
| 15 #include "base/threading/thread_task_runner_handle.h" | 15 #include "base/threading/thread_task_runner_handle.h" |
| 16 #include "components/offline_pages/offline_page_item.h" | 16 #include "components/offline_pages/offline_page_item.h" |
| 17 #include "sql/connection.h" | 17 #include "sql/connection.h" |
| 18 #include "sql/statement.h" | 18 #include "sql/statement.h" |
| 19 #include "sql/transaction.h" | 19 #include "sql/transaction.h" |
| 20 | 20 |
| 21 namespace offline_pages { | 21 namespace offline_pages { |
| 22 | 22 |
| 23 using StoreState = OfflinePageMetadataStore::StoreState; | |
| 24 using ItemActionStatus = OfflinePageMetadataStore::ItemActionStatus; | |
| 25 | |
| 26 namespace { | 23 namespace { |
| 27 | 24 |
| 28 // This is a macro instead of a const so that | 25 // This is a macro instead of a const so that |
| 29 // it can be used inline in other SQL statements below. | 26 // it can be used inline in other SQL statements below. |
| 30 #define OFFLINE_PAGES_TABLE_NAME "offlinepages_v1" | 27 #define OFFLINE_PAGES_TABLE_NAME "offlinepages_v1" |
| 31 | 28 |
| 32 bool CreateOfflinePagesTable(sql::Connection* db) { | 29 bool CreateOfflinePagesTable(sql::Connection* db) { |
| 33 const char kSql[] = "CREATE TABLE IF NOT EXISTS " OFFLINE_PAGES_TABLE_NAME | 30 const char kSql[] = "CREATE TABLE IF NOT EXISTS " OFFLINE_PAGES_TABLE_NAME |
| 34 "(offline_id INTEGER PRIMARY KEY NOT NULL," | 31 "(offline_id INTEGER PRIMARY KEY NOT NULL," |
| 35 " creation_time INTEGER NOT NULL," | 32 " creation_time INTEGER NOT NULL," |
| (...skipping 170 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 206 statement.BindInt(8, item.access_count); | 203 statement.BindInt(8, item.access_count); |
| 207 statement.BindInt64(9, item.expiration_time.ToInternalValue()); | 204 statement.BindInt64(9, item.expiration_time.ToInternalValue()); |
| 208 statement.BindString16(10, item.title); | 205 statement.BindString16(10, item.title); |
| 209 if (!statement.Run()) | 206 if (!statement.Run()) |
| 210 return ItemActionStatus::STORE_ERROR; | 207 return ItemActionStatus::STORE_ERROR; |
| 211 if (db->GetLastChangeCount() == 0) | 208 if (db->GetLastChangeCount() == 0) |
| 212 return ItemActionStatus::ALREADY_EXISTS; | 209 return ItemActionStatus::ALREADY_EXISTS; |
| 213 return ItemActionStatus::SUCCESS; | 210 return ItemActionStatus::SUCCESS; |
| 214 } | 211 } |
| 215 | 212 |
| 216 bool InsertOrReplace(sql::Connection* db, const OfflinePageItem& item) { | 213 bool Update(sql::Connection* db, const OfflinePageItem& item) { |
| 217 const char kSql[] = | 214 const char kSql[] = |
| 218 "INSERT OR REPLACE INTO " OFFLINE_PAGES_TABLE_NAME | 215 "UPDATE OR IGNORE " OFFLINE_PAGES_TABLE_NAME |
| 219 " (offline_id, online_url, client_namespace, client_id, file_path, " | 216 " SET online_url = ?, client_namespace = ?, client_id = ?, file_path = ?," |
| 220 "file_size, creation_time, last_access_time, access_count, " | 217 " file_size = ?, creation_time = ?, last_access_time = ?," |
| 221 "expiration_time, title)" | 218 " access_count = ?, expiration_time = ?, title = ?" |
| 222 " VALUES " | 219 " WHERE offline_id = ?"; |
| 223 " (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"; | |
| 224 | 220 |
| 225 sql::Statement statement(db->GetCachedStatement(SQL_FROM_HERE, kSql)); | 221 sql::Statement statement(db->GetCachedStatement(SQL_FROM_HERE, kSql)); |
| 226 statement.BindInt64(0, item.offline_id); | 222 statement.BindString(0, item.url.spec()); |
| 227 statement.BindString(1, item.url.spec()); | 223 statement.BindString(1, item.client_id.name_space); |
| 228 statement.BindString(2, item.client_id.name_space); | 224 statement.BindString(2, item.client_id.id); |
| 229 statement.BindString(3, item.client_id.id); | 225 statement.BindString(3, GetUTF8StringFromPath(item.file_path)); |
| 230 statement.BindString(4, GetUTF8StringFromPath(item.file_path)); | 226 statement.BindInt64(4, item.file_size); |
| 231 statement.BindInt64(5, item.file_size); | 227 statement.BindInt64(5, item.creation_time.ToInternalValue()); |
| 232 statement.BindInt64(6, item.creation_time.ToInternalValue()); | 228 statement.BindInt64(6, item.last_access_time.ToInternalValue()); |
| 233 statement.BindInt64(7, item.last_access_time.ToInternalValue()); | 229 statement.BindInt(7, item.access_count); |
| 234 statement.BindInt(8, item.access_count); | 230 statement.BindInt64(8, item.expiration_time.ToInternalValue()); |
| 235 statement.BindInt64(9, item.expiration_time.ToInternalValue()); | 231 statement.BindString16(9, item.title); |
| 236 statement.BindString16(10, item.title); | 232 statement.BindInt64(10, item.offline_id); |
| 237 return statement.Run(); | 233 return statement.Run() && db->GetLastChangeCount() > 0; |
| 238 } | 234 } |
| 239 | 235 |
| 240 bool InitDatabase(sql::Connection* db, base::FilePath path) { | 236 bool InitDatabase(sql::Connection* db, base::FilePath path) { |
| 241 db->set_page_size(4096); | 237 db->set_page_size(4096); |
| 242 db->set_cache_size(500); | 238 db->set_cache_size(500); |
| 243 db->set_histogram_tag("OfflinePageMetadata"); | 239 db->set_histogram_tag("OfflinePageMetadata"); |
| 244 db->set_exclusive_locking(); | 240 db->set_exclusive_locking(); |
| 245 | 241 |
| 246 base::File::Error err; | 242 base::File::Error err; |
| 247 if (!base::CreateDirectoryAndGetError(path.DirName(), &err)) { | 243 if (!base::CreateDirectoryAndGetError(path.DirName(), &err)) { |
| (...skipping 23 matching lines...) Expand all Loading... |
| 271 } else { | 267 } else { |
| 272 DVLOG(1) << "Offline pages database loading failed: " << status; | 268 DVLOG(1) << "Offline pages database loading failed: " << status; |
| 273 } | 269 } |
| 274 runner->PostTask(FROM_HERE, base::Bind(callback, status, result)); | 270 runner->PostTask(FROM_HERE, base::Bind(callback, status, result)); |
| 275 } | 271 } |
| 276 | 272 |
| 277 void OpenConnectionSync(sql::Connection* db, | 273 void OpenConnectionSync(sql::Connection* db, |
| 278 scoped_refptr<base::SingleThreadTaskRunner> runner, | 274 scoped_refptr<base::SingleThreadTaskRunner> runner, |
| 279 const base::FilePath& path, | 275 const base::FilePath& path, |
| 280 const base::Callback<void(StoreState)>& callback) { | 276 const base::Callback<void(StoreState)>& callback) { |
| 281 StoreState state = InitDatabase(db, path) | 277 StoreState state = InitDatabase(db, path) ? StoreState::LOADED |
| 282 ? OfflinePageMetadataStore::LOADED | 278 : StoreState::FAILED_INITIALIZATION; |
| 283 : OfflinePageMetadataStore::FAILED_INITIALIZATION; | |
| 284 runner->PostTask(FROM_HERE, base::Bind(callback, state)); | 279 runner->PostTask(FROM_HERE, base::Bind(callback, state)); |
| 285 } | 280 } |
| 286 | 281 |
| 282 bool GetPageByOfflineIdSync(sql::Connection* db, |
| 283 int64_t offline_id, |
| 284 OfflinePageItem* item) { |
| 285 const char kSql[] = |
| 286 "SELECT * FROM " OFFLINE_PAGES_TABLE_NAME " WHERE offline_id = ?"; |
| 287 sql::Statement statement(db->GetCachedStatement(SQL_FROM_HERE, kSql)); |
| 288 statement.BindInt64(0, offline_id); |
| 289 |
| 290 bool found = false; |
| 291 while (statement.Step()) { |
| 292 *item = MakeOfflinePageItem(&statement); |
| 293 found = true; |
| 294 } |
| 295 |
| 296 return found; |
| 297 } |
| 298 |
| 287 void GetOfflinePagesSync( | 299 void GetOfflinePagesSync( |
| 288 sql::Connection* db, | 300 sql::Connection* db, |
| 289 scoped_refptr<base::SingleThreadTaskRunner> runner, | 301 scoped_refptr<base::SingleThreadTaskRunner> runner, |
| 290 const OfflinePageMetadataStore::LoadCallback& callback) { | 302 const OfflinePageMetadataStore::LoadCallback& callback) { |
| 291 const char kSql[] = "SELECT * FROM " OFFLINE_PAGES_TABLE_NAME; | 303 const char kSql[] = "SELECT * FROM " OFFLINE_PAGES_TABLE_NAME; |
| 292 | 304 |
| 293 sql::Statement statement(db->GetCachedStatement(SQL_FROM_HERE, kSql)); | 305 sql::Statement statement(db->GetCachedStatement(SQL_FROM_HERE, kSql)); |
| 294 | 306 |
| 295 std::vector<OfflinePageItem> result; | 307 std::vector<OfflinePageItem> result; |
| 296 while (statement.Step()) | 308 while (statement.Step()) |
| (...skipping 10 matching lines...) Expand all Loading... |
| 307 } | 319 } |
| 308 | 320 |
| 309 void AddOfflinePageSync(sql::Connection* db, | 321 void AddOfflinePageSync(sql::Connection* db, |
| 310 scoped_refptr<base::SingleThreadTaskRunner> runner, | 322 scoped_refptr<base::SingleThreadTaskRunner> runner, |
| 311 const OfflinePageItem& offline_page, | 323 const OfflinePageItem& offline_page, |
| 312 const OfflinePageMetadataStore::AddCallback& callback) { | 324 const OfflinePageMetadataStore::AddCallback& callback) { |
| 313 ItemActionStatus status = Insert(db, offline_page); | 325 ItemActionStatus status = Insert(db, offline_page); |
| 314 runner->PostTask(FROM_HERE, base::Bind(callback, status)); | 326 runner->PostTask(FROM_HERE, base::Bind(callback, status)); |
| 315 } | 327 } |
| 316 | 328 |
| 329 void PostStoreErrorForAllPages( |
| 330 scoped_refptr<base::SingleThreadTaskRunner> runner, |
| 331 const std::vector<OfflinePageItem>& pages, |
| 332 const OfflinePageMetadataStore::UpdateCallback& callback) { |
| 333 std::unique_ptr<StoreUpdateResult> result( |
| 334 new StoreUpdateResult(StoreState::LOADED)); |
| 335 for (const auto& page : pages) { |
| 336 // TODO(fgorski): Maybe we should be more explicit about transaction |
| 337 // failures. |
| 338 result->item_statuses.push_back( |
| 339 std::make_pair(page.offline_id, ItemActionStatus::STORE_ERROR)); |
| 340 } |
| 341 runner->PostTask(FROM_HERE, base::Bind(callback, base::Passed(&result))); |
| 342 } |
| 343 |
| 344 void PostStoreErrorForAllIds( |
| 345 scoped_refptr<base::SingleThreadTaskRunner> runner, |
| 346 const std::vector<int64_t>& offline_ids, |
| 347 const OfflinePageMetadataStore::UpdateCallback& callback) { |
| 348 std::unique_ptr<StoreUpdateResult> result( |
| 349 new StoreUpdateResult(StoreState::LOADED)); |
| 350 for (const auto& offline_id : offline_ids) { |
| 351 // TODO(fgorski): Maybe we should be more explicit about transaction |
| 352 // failures. |
| 353 result->item_statuses.push_back( |
| 354 std::make_pair(offline_id, ItemActionStatus::STORE_ERROR)); |
| 355 } |
| 356 runner->PostTask(FROM_HERE, base::Bind(callback, base::Passed(&result))); |
| 357 } |
| 358 |
| 317 void UpdateOfflinePagesSync( | 359 void UpdateOfflinePagesSync( |
| 318 sql::Connection* db, | 360 sql::Connection* db, |
| 319 scoped_refptr<base::SingleThreadTaskRunner> runner, | 361 scoped_refptr<base::SingleThreadTaskRunner> runner, |
| 320 const std::vector<OfflinePageItem>& pages, | 362 const std::vector<OfflinePageItem>& pages, |
| 321 const OfflinePageMetadataStore::UpdateCallback& callback) { | 363 const OfflinePageMetadataStore::UpdateCallback& callback) { |
| 322 // TODO(fgorski): Update the callback to provide information about all items | 364 std::unique_ptr<StoreUpdateResult> result( |
| 323 // and not just a high level boolean. | 365 new StoreUpdateResult(StoreState::LOADED)); |
| 366 |
| 324 sql::Transaction transaction(db); | 367 sql::Transaction transaction(db); |
| 325 if (!transaction.Begin()) { | 368 if (!transaction.Begin()) { |
| 326 runner->PostTask(FROM_HERE, base::Bind(callback, false)); | 369 // TODO(fgorski): Pass the result to reuse the one created above. |
| 370 // TODO(fgorski): When reusing the result below, make sure the clear it. |
| 371 PostStoreErrorForAllPages(runner, pages, callback); |
| 327 return; | 372 return; |
| 328 } | 373 } |
| 329 | 374 |
| 330 // TODO(fgorski): Switch to only accept updates and not create new items. | 375 for (auto& page : pages) { |
| 331 for (auto& page : pages) | 376 // TODO(fgorski): Update Update method to return ItemActionStatus. |
| 332 InsertOrReplace(db, page); | 377 if (Update(db, page)) { |
| 378 result->updated_items.push_back(page); |
| 379 result->item_statuses.push_back( |
| 380 std::make_pair(page.offline_id, ItemActionStatus::SUCCESS)); |
| 381 } else { |
| 382 result->item_statuses.push_back( |
| 383 std::make_pair(page.offline_id, ItemActionStatus::DOESNT_EXIST)); |
| 384 } |
| 385 } |
| 333 | 386 |
| 334 bool result = transaction.Commit(); | 387 if (!transaction.Commit()) { |
| 335 runner->PostTask(FROM_HERE, base::Bind(callback, result)); | 388 PostStoreErrorForAllPages(runner, pages, callback); |
| 389 return; |
| 390 } |
| 391 runner->PostTask(FROM_HERE, base::Bind(callback, base::Passed(&result))); |
| 336 } | 392 } |
| 337 | 393 |
| 338 void RemoveOfflinePagesSync( | 394 void RemoveOfflinePagesSync( |
| 339 const std::vector<int64_t>& offline_ids, | 395 const std::vector<int64_t>& offline_ids, |
| 340 sql::Connection* db, | 396 sql::Connection* db, |
| 341 scoped_refptr<base::SingleThreadTaskRunner> runner, | 397 scoped_refptr<base::SingleThreadTaskRunner> runner, |
| 342 const OfflinePageMetadataStore::UpdateCallback& callback) { | 398 const OfflinePageMetadataStore::UpdateCallback& callback) { |
| 343 // TODO(bburns): add UMA metrics here (and for levelDB). | 399 // TODO(fgorski): Perhaps add metrics here. |
| 400 std::unique_ptr<StoreUpdateResult> result( |
| 401 new StoreUpdateResult(StoreState::LOADED)); |
| 344 | 402 |
| 345 // If you create a transaction but don't Commit() it is automatically | 403 // If you create a transaction but don't Commit() it is automatically |
| 346 // rolled back by its destructor when it falls out of scope. | 404 // rolled back by its destructor when it falls out of scope. |
| 347 sql::Transaction transaction(db); | 405 sql::Transaction transaction(db); |
| 348 if (!transaction.Begin()) { | 406 if (!transaction.Begin()) { |
| 349 runner->PostTask(FROM_HERE, base::Bind(callback, false)); | 407 PostStoreErrorForAllIds(runner, offline_ids, callback); |
| 350 return; | 408 return; |
| 351 } | 409 } |
| 352 | 410 |
| 353 for (auto offline_id : offline_ids) { | 411 for (auto offline_id : offline_ids) { |
| 354 if (!DeleteByOfflineId(db, offline_id)) { | 412 OfflinePageItem page; |
| 355 runner->PostTask(FROM_HERE, base::Bind(callback, false)); | 413 ItemActionStatus status; |
| 356 return; | 414 if (!GetPageByOfflineIdSync(db, offline_id, &page)) { |
| 415 status = ItemActionStatus::DOESNT_EXIST; |
| 416 } else if (!DeleteByOfflineId(db, offline_id)) { |
| 417 status = ItemActionStatus::STORE_ERROR; |
| 418 } else { |
| 419 status = ItemActionStatus::SUCCESS; |
| 420 result->updated_items.push_back(page); |
| 357 } | 421 } |
| 422 |
| 423 result->item_statuses.push_back(std::make_pair(offline_id, status)); |
| 358 } | 424 } |
| 359 | 425 |
| 360 bool success = transaction.Commit(); | 426 if (!transaction.Commit()) { |
| 361 runner->PostTask(FROM_HERE, base::Bind(callback, success)); | 427 PostStoreErrorForAllIds(runner, offline_ids, callback); |
| 428 return; |
| 429 } |
| 430 |
| 431 runner->PostTask(FROM_HERE, base::Bind(callback, base::Passed(&result))); |
| 362 } | 432 } |
| 363 | 433 |
| 364 void ResetSync(sql::Connection* db, | 434 void ResetSync(sql::Connection* db, |
| 365 const base::FilePath& db_file_path, | 435 const base::FilePath& db_file_path, |
| 366 scoped_refptr<base::SingleThreadTaskRunner> runner, | 436 scoped_refptr<base::SingleThreadTaskRunner> runner, |
| 367 const base::Callback<void(StoreState)>& callback) { | 437 const base::Callback<void(StoreState)>& callback) { |
| 368 // This method deletes the content of the whole store and reinitializes it. | 438 // This method deletes the content of the whole store and reinitializes it. |
| 369 bool success = db->Raze(); | 439 bool success = db->Raze(); |
| 370 db->Close(); | 440 db->Close(); |
| 371 StoreState state; | 441 StoreState state; |
| 372 if (success) { | 442 if (success) { |
| 373 state = InitDatabase(db, db_file_path) | 443 state = InitDatabase(db, db_file_path) ? StoreState::LOADED |
| 374 ? OfflinePageMetadataStore::LOADED | 444 : StoreState::FAILED_INITIALIZATION; |
| 375 : OfflinePageMetadataStore::FAILED_INITIALIZATION; | |
| 376 } else { | 445 } else { |
| 377 state = OfflinePageMetadataStore::FAILED_RESET; | 446 state = StoreState::FAILED_RESET; |
| 378 } | 447 } |
| 379 runner->PostTask(FROM_HERE, base::Bind(callback, state)); | 448 runner->PostTask(FROM_HERE, base::Bind(callback, state)); |
| 380 } | 449 } |
| 381 | 450 |
| 382 } // anonymous namespace | 451 } // anonymous namespace |
| 383 | 452 |
| 384 OfflinePageMetadataStoreSQL::OfflinePageMetadataStoreSQL( | 453 OfflinePageMetadataStoreSQL::OfflinePageMetadataStoreSQL( |
| 385 scoped_refptr<base::SequencedTaskRunner> background_task_runner, | 454 scoped_refptr<base::SequencedTaskRunner> background_task_runner, |
| 386 const base::FilePath& path) | 455 const base::FilePath& path) |
| 387 : background_task_runner_(std::move(background_task_runner)), | 456 : background_task_runner_(std::move(background_task_runner)), |
| 388 db_file_path_(path.AppendASCII("OfflinePages.db")), | 457 db_file_path_(path.AppendASCII("OfflinePages.db")), |
| 389 state_(NOT_LOADED), | 458 state_(StoreState::NOT_LOADED), |
| 390 weak_ptr_factory_(this) { | 459 weak_ptr_factory_(this) { |
| 391 OpenConnection(); | 460 OpenConnection(); |
| 392 } | 461 } |
| 393 | 462 |
| 394 OfflinePageMetadataStoreSQL::~OfflinePageMetadataStoreSQL() { | 463 OfflinePageMetadataStoreSQL::~OfflinePageMetadataStoreSQL() { |
| 395 if (db_.get() && | 464 if (db_.get() && |
| 396 !background_task_runner_->DeleteSoon(FROM_HERE, db_.release())) { | 465 !background_task_runner_->DeleteSoon(FROM_HERE, db_.release())) { |
| 397 DLOG(WARNING) << "SQL database will not be deleted."; | 466 DLOG(WARNING) << "SQL database will not be deleted."; |
| 398 } | 467 } |
| 399 } | 468 } |
| 400 | 469 |
| 401 void OfflinePageMetadataStoreSQL::GetOfflinePages( | 470 void OfflinePageMetadataStoreSQL::GetOfflinePages( |
| 402 const LoadCallback& callback) { | 471 const LoadCallback& callback) { |
| 403 background_task_runner_->PostTask( | 472 background_task_runner_->PostTask( |
| 404 FROM_HERE, base::Bind(&GetOfflinePagesSync, db_.get(), | 473 FROM_HERE, base::Bind(&GetOfflinePagesSync, db_.get(), |
| 405 base::ThreadTaskRunnerHandle::Get(), callback)); | 474 base::ThreadTaskRunnerHandle::Get(), callback)); |
| 406 } | 475 } |
| 407 | 476 |
| 408 void OfflinePageMetadataStoreSQL::AddOfflinePage( | 477 void OfflinePageMetadataStoreSQL::AddOfflinePage( |
| 409 const OfflinePageItem& offline_page, | 478 const OfflinePageItem& offline_page, |
| 410 const AddCallback& callback) { | 479 const AddCallback& callback) { |
| 411 if (!CheckDb(base::Bind(callback, STORE_ERROR))) | 480 if (!CheckDb(base::Bind(callback, ItemActionStatus::STORE_ERROR))) |
| 412 return; | 481 return; |
| 413 | 482 |
| 414 background_task_runner_->PostTask( | 483 background_task_runner_->PostTask( |
| 415 FROM_HERE, | 484 FROM_HERE, |
| 416 base::Bind(&AddOfflinePageSync, db_.get(), | 485 base::Bind(&AddOfflinePageSync, db_.get(), |
| 417 base::ThreadTaskRunnerHandle::Get(), offline_page, callback)); | 486 base::ThreadTaskRunnerHandle::Get(), offline_page, callback)); |
| 418 } | 487 } |
| 419 | 488 |
| 420 void OfflinePageMetadataStoreSQL::UpdateOfflinePages( | 489 void OfflinePageMetadataStoreSQL::UpdateOfflinePages( |
| 421 const std::vector<OfflinePageItem>& pages, | 490 const std::vector<OfflinePageItem>& pages, |
| 422 const UpdateCallback& callback) { | 491 const UpdateCallback& callback) { |
| 423 if (!CheckDb(base::Bind(callback, false))) | 492 if (!db_.get()) { |
| 493 PostStoreErrorForAllPages(base::ThreadTaskRunnerHandle::Get(), pages, |
| 494 callback); |
| 424 return; | 495 return; |
| 496 } |
| 425 | 497 |
| 426 background_task_runner_->PostTask( | 498 background_task_runner_->PostTask( |
| 427 FROM_HERE, | 499 FROM_HERE, |
| 428 base::Bind(&UpdateOfflinePagesSync, db_.get(), | 500 base::Bind(&UpdateOfflinePagesSync, db_.get(), |
| 429 base::ThreadTaskRunnerHandle::Get(), pages, callback)); | 501 base::ThreadTaskRunnerHandle::Get(), pages, callback)); |
| 430 } | 502 } |
| 431 | 503 |
| 432 void OfflinePageMetadataStoreSQL::RemoveOfflinePages( | 504 void OfflinePageMetadataStoreSQL::RemoveOfflinePages( |
| 433 const std::vector<int64_t>& offline_ids, | 505 const std::vector<int64_t>& offline_ids, |
| 434 const UpdateCallback& callback) { | 506 const UpdateCallback& callback) { |
| 435 DCHECK(db_.get()); | 507 DCHECK(db_.get()); |
| 436 | 508 |
| 437 if (offline_ids.empty()) { | 509 if (offline_ids.empty()) { |
| 438 // Nothing to do, but post a callback instead of calling directly | 510 // Nothing to do, but post a callback instead of calling directly |
| 439 // to preserve the async style behavior to prevent bugs. | 511 // to preserve the async style behavior to prevent bugs. |
| 440 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, | 512 // TODO(fgorski): Refactor to accept item action status. |
| 441 base::Bind(callback, true)); | 513 // It works not because the list of offline IDs is empty. |
| 514 PostStoreErrorForAllIds(base::ThreadTaskRunnerHandle::Get(), offline_ids, |
| 515 callback); |
| 442 return; | 516 return; |
| 443 } | 517 } |
| 444 | 518 |
| 445 background_task_runner_->PostTask( | 519 background_task_runner_->PostTask( |
| 446 FROM_HERE, base::Bind(&RemoveOfflinePagesSync, offline_ids, db_.get(), | 520 FROM_HERE, base::Bind(&RemoveOfflinePagesSync, offline_ids, db_.get(), |
| 447 base::ThreadTaskRunnerHandle::Get(), callback)); | 521 base::ThreadTaskRunnerHandle::Get(), callback)); |
| 448 } | 522 } |
| 449 | 523 |
| 450 void OfflinePageMetadataStoreSQL::Reset(const ResetCallback& callback) { | 524 void OfflinePageMetadataStoreSQL::Reset(const ResetCallback& callback) { |
| 451 if (!CheckDb(base::Bind(callback, false))) | 525 if (!CheckDb(base::Bind(callback, false))) |
| (...skipping 21 matching lines...) Expand all Loading... |
| 473 base::Bind(&OfflinePageMetadataStoreSQL::OnOpenConnectionDone, | 547 base::Bind(&OfflinePageMetadataStoreSQL::OnOpenConnectionDone, |
| 474 weak_ptr_factory_.GetWeakPtr()))); | 548 weak_ptr_factory_.GetWeakPtr()))); |
| 475 } | 549 } |
| 476 | 550 |
| 477 void OfflinePageMetadataStoreSQL::OnOpenConnectionDone(StoreState state) { | 551 void OfflinePageMetadataStoreSQL::OnOpenConnectionDone(StoreState state) { |
| 478 DCHECK(db_.get()); | 552 DCHECK(db_.get()); |
| 479 | 553 |
| 480 state_ = state; | 554 state_ = state; |
| 481 | 555 |
| 482 // Unfortunately we were not able to open DB connection. | 556 // Unfortunately we were not able to open DB connection. |
| 483 if (state != OfflinePageMetadataStore::LOADED) | 557 if (state != StoreState::LOADED) |
| 484 db_.reset(); | 558 db_.reset(); |
| 485 | 559 |
| 486 // TODO(fgorski): This might be a place to start store recovery. Alternatively | 560 // TODO(fgorski): This might be a place to start store recovery. Alternatively |
| 487 // that can be attempted in the OfflinePageModel. | 561 // that can be attempted in the OfflinePageModel. |
| 488 } | 562 } |
| 489 | 563 |
| 490 void OfflinePageMetadataStoreSQL::OnResetDone(const ResetCallback& callback, | 564 void OfflinePageMetadataStoreSQL::OnResetDone(const ResetCallback& callback, |
| 491 StoreState state) { | 565 StoreState state) { |
| 492 OnOpenConnectionDone(state); | 566 OnOpenConnectionDone(state); |
| 493 callback.Run(state == LOADED); | 567 callback.Run(state == StoreState::LOADED); |
| 494 } | 568 } |
| 495 | 569 |
| 496 bool OfflinePageMetadataStoreSQL::CheckDb(const base::Closure& callback) { | 570 bool OfflinePageMetadataStoreSQL::CheckDb(const base::Closure& callback) { |
| 497 DCHECK(db_.get()); | 571 DCHECK(db_.get()); |
| 498 if (!db_.get()) { | 572 if (!db_.get()) { |
| 499 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, | 573 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, |
| 500 base::Bind(callback)); | 574 base::Bind(callback)); |
| 501 return false; | 575 return false; |
| 502 } | 576 } |
| 503 return true; | 577 return true; |
| 504 } | 578 } |
| 505 | 579 |
| 506 } // namespace offline_pages | 580 } // namespace offline_pages |
| OLD | NEW |