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

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: 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"
(...skipping 384 matching lines...) Expand 10 before | Expand all | Expand 10 after
395 // TODO(fgorski): add UMA metrics here. 395 // TODO(fgorski): add UMA metrics here.
396 offline_pages::ChangeRequestsState(db, request_ids, new_state, results, 396 offline_pages::ChangeRequestsState(db, request_ids, new_state, results,
397 requests); 397 requests);
398 runner->PostTask(FROM_HERE, 398 runner->PostTask(FROM_HERE,
399 base::Bind(callback, results, base::Passed(&requests))); 399 base::Bind(callback, results, base::Passed(&requests)));
400 } 400 }
401 401
402 void OpenConnectionSync(sql::Connection* db, 402 void OpenConnectionSync(sql::Connection* db,
403 scoped_refptr<base::SingleThreadTaskRunner> runner, 403 scoped_refptr<base::SingleThreadTaskRunner> runner,
404 const base::FilePath& path, 404 const base::FilePath& path,
405 const base::Callback<void(bool)>& callback) { 405 const base::Callback<void(StoreState)>& callback) {
406 bool success = InitDatabase(db, path); 406 StoreState state =
407 runner->PostTask(FROM_HERE, base::Bind(callback, success)); 407 InitDatabase(db, path) ? StoreState::LOADED : StoreState::FAILED_LOADING;
408 runner->PostTask(FROM_HERE, base::Bind(callback, state));
408 } 409 }
409 410
410 void ResetSync(sql::Connection* db, 411 void ResetSync(sql::Connection* db,
411 const base::FilePath& db_file_path, 412 const base::FilePath& db_file_path,
412 scoped_refptr<base::SingleThreadTaskRunner> runner, 413 scoped_refptr<base::SingleThreadTaskRunner> runner,
413 const RequestQueueStore::ResetCallback& callback) { 414 const base::Callback<void(StoreState)>& callback) {
Pete Williamson 2016/09/30 18:34:14 Should we have a more descriptive name for this ty
fgorski 2016/09/30 22:51:23 Done.
414 // This method deletes the content of the whole store and reinitializes it. 415 // This method deletes the content of the whole store and reinitializes it.
415 bool success = db->Raze(); 416 bool success = db->Raze();
416 db->Close(); 417 db->Close();
417 if (success) 418 StoreState state;
418 success = InitDatabase(db, db_file_path); 419 if (!success)
419 runner->PostTask(FROM_HERE, base::Bind(callback, success)); 420 state = StoreState::FAILED_RESET;
421 if (InitDatabase(db, db_file_path))
422 state = StoreState::LOADED;
423 else
424 state = StoreState::FAILED_LOADING;
425
426 runner->PostTask(FROM_HERE, base::Bind(callback, state));
420 } 427 }
421 428
422 } // anonymous namespace 429 } // anonymous namespace
423 430
424 RequestQueueStoreSQL::RequestQueueStoreSQL( 431 RequestQueueStoreSQL::RequestQueueStoreSQL(
425 scoped_refptr<base::SequencedTaskRunner> background_task_runner, 432 scoped_refptr<base::SequencedTaskRunner> background_task_runner,
426 const base::FilePath& path) 433 const base::FilePath& path)
427 : background_task_runner_(std::move(background_task_runner)), 434 : background_task_runner_(std::move(background_task_runner)),
428 db_file_path_(path.AppendASCII("RequestQueue.db")), 435 db_file_path_(path.AppendASCII("RequestQueue.db")),
429 weak_ptr_factory_(this) { 436 weak_ptr_factory_(this) {
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after
538 DCHECK(!db_); 545 DCHECK(!db_);
539 db_.reset(new sql::Connection()); 546 db_.reset(new sql::Connection());
540 background_task_runner_->PostTask( 547 background_task_runner_->PostTask(
541 FROM_HERE, 548 FROM_HERE,
542 base::Bind(&OpenConnectionSync, db_.get(), 549 base::Bind(&OpenConnectionSync, db_.get(),
543 base::ThreadTaskRunnerHandle::Get(), db_file_path_, 550 base::ThreadTaskRunnerHandle::Get(), db_file_path_,
544 base::Bind(&RequestQueueStoreSQL::OnOpenConnectionDone, 551 base::Bind(&RequestQueueStoreSQL::OnOpenConnectionDone,
545 weak_ptr_factory_.GetWeakPtr()))); 552 weak_ptr_factory_.GetWeakPtr())));
546 } 553 }
547 554
548 void RequestQueueStoreSQL::OnOpenConnectionDone(bool success) { 555 void RequestQueueStoreSQL::OnOpenConnectionDone(StoreState state) {
549 DCHECK(db_.get()); 556 DCHECK(db_.get());
550 557
558 state_ = state;
559
551 // Unfortunately we were not able to open DB connection. 560 // Unfortunately we were not able to open DB connection.
552 if (!success) 561 if (state_ != StoreState::LOADED)
553 db_.reset(); 562 db_.reset();
554 } 563 }
555 564
556 void RequestQueueStoreSQL::OnResetDone(const ResetCallback& callback, 565 void RequestQueueStoreSQL::OnResetDone(const ResetCallback& callback,
557 bool success) { 566 StoreState state) {
558 // Complete connection initialization post reset. 567 // Complete connection initialization post reset.
559 OnOpenConnectionDone(success); 568 OnOpenConnectionDone(state);
560 callback.Run(success); 569 callback.Run(state == StoreState::LOADED);
570 }
571
572 StoreState RequestQueueStoreSQL::state() const {
573 return state_;
561 } 574 }
562 575
563 } // namespace offline_pages 576 } // 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