Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(239)

Side by Side Diff: components/offline_pages/background/request_queue_store_sql.cc

Issue 2379353003: [Offline pages] Applying StoreState to track RequestQueueStore state (Closed)
Patch Set: Addressing feedback Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « components/offline_pages/background/request_queue_store_sql.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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/background/request_queue_store_sql.h" 5 #include "components/offline_pages/background/request_queue_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/sequenced_task_runner.h" 12 #include "base/sequenced_task_runner.h"
13 #include "base/threading/thread_task_runner_handle.h" 13 #include "base/threading/thread_task_runner_handle.h"
14 #include "components/offline_pages/background/save_page_request.h" 14 #include "components/offline_pages/background/save_page_request.h"
15 #include "sql/connection.h" 15 #include "sql/connection.h"
16 #include "sql/statement.h" 16 #include "sql/statement.h"
17 #include "sql/transaction.h" 17 #include "sql/transaction.h"
18 18
19 namespace offline_pages { 19 namespace offline_pages {
20 20
21 template class StoreUpdateResult<SavePageRequest>; 21 template class StoreUpdateResult<SavePageRequest>;
22 22
23 namespace { 23 namespace {
24 24
25 using UpdateStatus = RequestQueueStore::UpdateStatus; 25 using UpdateStatus = RequestQueueStore::UpdateStatus;
26 using StoreStateCallback = base::Callback<void(StoreState)>;
26 27
27 // This is a macro instead of a const so that 28 // This is a macro instead of a const so that
28 // it can be used inline in other SQL statements below. 29 // it can be used inline in other SQL statements below.
29 #define REQUEST_QUEUE_TABLE_NAME "request_queue_v1" 30 #define REQUEST_QUEUE_TABLE_NAME "request_queue_v1"
30 const bool kUserRequested = true; 31 const bool kUserRequested = true;
31 32
32 bool CreateRequestQueueTable(sql::Connection* db) { 33 bool CreateRequestQueueTable(sql::Connection* db) {
33 const char kSql[] = "CREATE TABLE IF NOT EXISTS " REQUEST_QUEUE_TABLE_NAME 34 const char kSql[] = "CREATE TABLE IF NOT EXISTS " REQUEST_QUEUE_TABLE_NAME
34 " (request_id INTEGER PRIMARY KEY NOT NULL," 35 " (request_id INTEGER PRIMARY KEY NOT NULL,"
35 " creation_time INTEGER NOT NULL," 36 " creation_time INTEGER NOT NULL,"
(...skipping 359 matching lines...) Expand 10 before | Expand all | Expand 10 after
395 // TODO(fgorski): add UMA metrics here. 396 // TODO(fgorski): add UMA metrics here.
396 offline_pages::ChangeRequestsState(db, request_ids, new_state, results, 397 offline_pages::ChangeRequestsState(db, request_ids, new_state, results,
397 requests); 398 requests);
398 runner->PostTask(FROM_HERE, 399 runner->PostTask(FROM_HERE,
399 base::Bind(callback, results, base::Passed(&requests))); 400 base::Bind(callback, results, base::Passed(&requests)));
400 } 401 }
401 402
402 void OpenConnectionSync(sql::Connection* db, 403 void OpenConnectionSync(sql::Connection* db,
403 scoped_refptr<base::SingleThreadTaskRunner> runner, 404 scoped_refptr<base::SingleThreadTaskRunner> runner,
404 const base::FilePath& path, 405 const base::FilePath& path,
405 const base::Callback<void(bool)>& callback) { 406 const StoreStateCallback& callback) {
406 bool success = InitDatabase(db, path); 407 StoreState state =
407 runner->PostTask(FROM_HERE, base::Bind(callback, success)); 408 InitDatabase(db, path) ? StoreState::LOADED : StoreState::FAILED_LOADING;
409 runner->PostTask(FROM_HERE, base::Bind(callback, state));
408 } 410 }
409 411
410 void ResetSync(sql::Connection* db, 412 void ResetSync(sql::Connection* db,
411 const base::FilePath& db_file_path, 413 const base::FilePath& db_file_path,
412 scoped_refptr<base::SingleThreadTaskRunner> runner, 414 scoped_refptr<base::SingleThreadTaskRunner> runner,
413 const RequestQueueStore::ResetCallback& callback) { 415 const StoreStateCallback& callback) {
414 // This method deletes the content of the whole store and reinitializes it. 416 // This method deletes the content of the whole store and reinitializes it.
415 bool success = db->Raze(); 417 bool success = db->Raze();
416 db->Close(); 418 db->Close();
417 if (success) 419 StoreState state;
418 success = InitDatabase(db, db_file_path); 420 if (!success)
419 runner->PostTask(FROM_HERE, base::Bind(callback, success)); 421 state = StoreState::FAILED_RESET;
422 if (InitDatabase(db, db_file_path))
423 state = StoreState::LOADED;
424 else
425 state = StoreState::FAILED_LOADING;
426
427 runner->PostTask(FROM_HERE, base::Bind(callback, state));
420 } 428 }
421 429
422 } // anonymous namespace 430 } // anonymous namespace
423 431
424 RequestQueueStoreSQL::RequestQueueStoreSQL( 432 RequestQueueStoreSQL::RequestQueueStoreSQL(
425 scoped_refptr<base::SequencedTaskRunner> background_task_runner, 433 scoped_refptr<base::SequencedTaskRunner> background_task_runner,
426 const base::FilePath& path) 434 const base::FilePath& path)
427 : background_task_runner_(std::move(background_task_runner)), 435 : background_task_runner_(std::move(background_task_runner)),
428 db_file_path_(path.AppendASCII("RequestQueue.db")), 436 db_file_path_(path.AppendASCII("RequestQueue.db")),
429 weak_ptr_factory_(this) { 437 weak_ptr_factory_(this) {
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after
538 DCHECK(!db_); 546 DCHECK(!db_);
539 db_.reset(new sql::Connection()); 547 db_.reset(new sql::Connection());
540 background_task_runner_->PostTask( 548 background_task_runner_->PostTask(
541 FROM_HERE, 549 FROM_HERE,
542 base::Bind(&OpenConnectionSync, db_.get(), 550 base::Bind(&OpenConnectionSync, db_.get(),
543 base::ThreadTaskRunnerHandle::Get(), db_file_path_, 551 base::ThreadTaskRunnerHandle::Get(), db_file_path_,
544 base::Bind(&RequestQueueStoreSQL::OnOpenConnectionDone, 552 base::Bind(&RequestQueueStoreSQL::OnOpenConnectionDone,
545 weak_ptr_factory_.GetWeakPtr()))); 553 weak_ptr_factory_.GetWeakPtr())));
546 } 554 }
547 555
548 void RequestQueueStoreSQL::OnOpenConnectionDone(bool success) { 556 void RequestQueueStoreSQL::OnOpenConnectionDone(StoreState state) {
549 DCHECK(db_.get()); 557 DCHECK(db_.get());
550 558
559 state_ = state;
560
551 // Unfortunately we were not able to open DB connection. 561 // Unfortunately we were not able to open DB connection.
552 if (!success) 562 if (state_ != StoreState::LOADED)
553 db_.reset(); 563 db_.reset();
554 } 564 }
555 565
556 void RequestQueueStoreSQL::OnResetDone(const ResetCallback& callback, 566 void RequestQueueStoreSQL::OnResetDone(const ResetCallback& callback,
557 bool success) { 567 StoreState state) {
558 // Complete connection initialization post reset. 568 // Complete connection initialization post reset.
559 OnOpenConnectionDone(success); 569 OnOpenConnectionDone(state);
560 callback.Run(success); 570 callback.Run(state == StoreState::LOADED);
571 }
572
573 StoreState RequestQueueStoreSQL::state() const {
574 return state_;
561 } 575 }
562 576
563 } // namespace offline_pages 577 } // namespace offline_pages
OLDNEW
« no previous file with comments | « components/offline_pages/background/request_queue_store_sql.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698