OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "chrome/browser/net/sqlite_persistent_cookie_store.h" | 5 #include "chrome/browser/net/sqlite_persistent_cookie_store.h" |
6 | 6 |
7 #include <list> | 7 #include <list> |
8 #include <map> | 8 #include <map> |
9 #include <set> | 9 #include <set> |
10 #include <utility> | 10 #include <utility> |
(...skipping 414 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
425 cookies.swap(cookies_); | 425 cookies.swap(cookies_); |
426 } | 426 } |
427 | 427 |
428 loaded_callback.Run(cookies); | 428 loaded_callback.Run(cookies); |
429 } | 429 } |
430 | 430 |
431 bool SQLitePersistentCookieStore::Backend::InitializeDatabase() { | 431 bool SQLitePersistentCookieStore::Backend::InitializeDatabase() { |
432 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); | 432 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); |
433 | 433 |
434 if (initialized_) { | 434 if (initialized_) { |
435 return true; | 435 // Return false if we were previously initialized but the DB has since been |
| 436 // closed. |
| 437 return db_.get() ? true : false; |
436 } | 438 } |
437 | 439 |
438 const FilePath dir = path_.DirName(); | 440 const FilePath dir = path_.DirName(); |
439 if (!file_util::PathExists(dir) && !file_util::CreateDirectory(dir)) { | 441 if (!file_util::PathExists(dir) && !file_util::CreateDirectory(dir)) { |
440 return false; | 442 return false; |
441 } | 443 } |
442 | 444 |
443 db_.reset(new sql::Connection); | 445 db_.reset(new sql::Connection); |
444 if (!db_->Open(path_)) { | 446 if (!db_->Open(path_)) { |
445 NOTREACHED() << "Unable to open cookie DB."; | 447 NOTREACHED() << "Unable to open cookie DB."; |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
485 return true; | 487 return true; |
486 } | 488 } |
487 | 489 |
488 void SQLitePersistentCookieStore::Backend::ChainLoadCookies( | 490 void SQLitePersistentCookieStore::Backend::ChainLoadCookies( |
489 const LoadedCallback& loaded_callback) { | 491 const LoadedCallback& loaded_callback) { |
490 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); | 492 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); |
491 IncrementTimeDelta increment(&cookie_load_duration_); | 493 IncrementTimeDelta increment(&cookie_load_duration_); |
492 | 494 |
493 bool load_success = true; | 495 bool load_success = true; |
494 | 496 |
495 if (keys_to_load_.size() > 0) { | 497 if (!db_.get()) { |
| 498 // Close() has been called on this store. |
| 499 load_success = false; |
| 500 } else if (keys_to_load_.size() > 0) { |
496 // Load cookies for the first domain key. | 501 // Load cookies for the first domain key. |
497 std::map<std::string, std::set<std::string> >::iterator | 502 std::map<std::string, std::set<std::string> >::iterator |
498 it = keys_to_load_.begin(); | 503 it = keys_to_load_.begin(); |
499 load_success = LoadCookiesForDomains(it->second); | 504 load_success = LoadCookiesForDomains(it->second); |
500 keys_to_load_.erase(it); | 505 keys_to_load_.erase(it); |
501 } | 506 } |
502 | 507 |
503 // If load is successful and there are more domain keys to be loaded, | 508 // If load is successful and there are more domain keys to be loaded, |
504 // then post a DB task to continue chain-load; | 509 // then post a DB task to continue chain-load; |
505 // Otherwise notify on IO thread. | 510 // Otherwise notify on IO thread. |
(...skipping 309 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
815 BrowserThread::DB, FROM_HERE, base::Bind(&Backend::Commit, this)); | 820 BrowserThread::DB, FROM_HERE, base::Bind(&Backend::Commit, this)); |
816 if (completion_task) { | 821 if (completion_task) { |
817 // We want the completion task to run immediately after Commit() returns. | 822 // We want the completion task to run immediately after Commit() returns. |
818 // Posting it from here means there is less chance of another task getting | 823 // Posting it from here means there is less chance of another task getting |
819 // onto the message queue first, than if we posted it from Commit() itself. | 824 // onto the message queue first, than if we posted it from Commit() itself. |
820 BrowserThread::PostTask(BrowserThread::DB, FROM_HERE, completion_task); | 825 BrowserThread::PostTask(BrowserThread::DB, FROM_HERE, completion_task); |
821 } | 826 } |
822 } | 827 } |
823 | 828 |
824 // Fire off a close message to the background thread. We could still have a | 829 // Fire off a close message to the background thread. We could still have a |
825 // pending commit timer that will be holding a reference on us, but if/when | 830 // pending commit timer or Load operations holding references on us, but if/when |
826 // this fires we will already have been cleaned up and it will be ignored. | 831 // this fires we will already have been cleaned up and it will be ignored. |
827 void SQLitePersistentCookieStore::Backend::Close() { | 832 void SQLitePersistentCookieStore::Backend::Close() { |
828 if (BrowserThread::CurrentlyOn(BrowserThread::DB)) { | 833 if (BrowserThread::CurrentlyOn(BrowserThread::DB)) { |
829 InternalBackgroundClose(); | 834 InternalBackgroundClose(); |
830 } else { | 835 } else { |
831 // Must close the backend on the background thread. | 836 // Must close the backend on the background thread. |
832 BrowserThread::PostTask( | 837 BrowserThread::PostTask( |
833 BrowserThread::DB, FROM_HERE, | 838 BrowserThread::DB, FROM_HERE, |
834 base::Bind(&Backend::InternalBackgroundClose, this)); | 839 base::Bind(&Backend::InternalBackgroundClose, this)); |
835 } | 840 } |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
906 if (backend_.get()) | 911 if (backend_.get()) |
907 backend_->SetClearLocalStateOnExit(clear_local_state); | 912 backend_->SetClearLocalStateOnExit(clear_local_state); |
908 } | 913 } |
909 | 914 |
910 void SQLitePersistentCookieStore::Flush(Task* completion_task) { | 915 void SQLitePersistentCookieStore::Flush(Task* completion_task) { |
911 if (backend_.get()) | 916 if (backend_.get()) |
912 backend_->Flush(completion_task); | 917 backend_->Flush(completion_task); |
913 else if (completion_task) | 918 else if (completion_task) |
914 MessageLoop::current()->PostTask(FROM_HERE, completion_task); | 919 MessageLoop::current()->PostTask(FROM_HERE, completion_task); |
915 } | 920 } |
OLD | NEW |