Index: chrome/browser/net/sqlite_persistent_cookie_store.cc |
=================================================================== |
--- chrome/browser/net/sqlite_persistent_cookie_store.cc (revision 99705) |
+++ chrome/browser/net/sqlite_persistent_cookie_store.cc (working copy) |
@@ -7,6 +7,7 @@ |
#include <list> |
#include "base/basictypes.h" |
+#include "base/bind.h" |
#include "base/file_path.h" |
#include "base/file_util.h" |
#include "base/logging.h" |
@@ -38,7 +39,7 @@ |
} |
// Creates or load the SQLite database. |
- bool Load(std::vector<net::CookieMonster::CanonicalCookie*>* cookies); |
+ bool Load(const LoadedCallback& loaded_callback); |
// Batch a cookie addition. |
void AddCookie(const net::CookieMonster::CanonicalCookie& cc); |
@@ -91,6 +92,18 @@ |
}; |
private: |
+ // Creates or load the SQLite database on DB thread. |
+ void LoadAndNotifyOnDBThread(const LoadedCallback& loaded_callback); |
+ // Notify the CookieMonster when loading complete. |
+ void NotifyOnIOThread( |
+ const LoadedCallback& loaded_callback, |
+ bool load_success, |
+ const std::vector<net::CookieMonster::CanonicalCookie*>& cookies); |
+ // Initialize the data base. |
+ bool InitializeDatabase(); |
+ // Load cookies to the data base, and read cookies. |
+ bool LoadInternal(std::vector<net::CookieMonster::CanonicalCookie*>* cookies); |
+ |
// Batch a cookie operation (add or delete) |
void BatchOperation(PendingOperation::OperationType op, |
const net::CookieMonster::CanonicalCookie& cc); |
@@ -154,19 +167,40 @@ |
} // namespace |
bool SQLitePersistentCookieStore::Backend::Load( |
- std::vector<net::CookieMonster::CanonicalCookie*>* cookies) { |
+ const LoadedCallback& loaded_callback) { |
// This function should be called only once per instance. |
DCHECK(!db_.get()); |
+ BrowserThread::PostTask( |
+ BrowserThread::DB, FROM_HERE, |
+ base::Bind(&Backend::LoadAndNotifyOnDBThread, base::Unretained(this), |
+ loaded_callback)); |
+ return true; |
+} |
- // Ensure the parent directory for storing cookies is created before reading |
- // from it. We make an exception to allow IO on the UI thread here because |
- // we are going to disk anyway in db_->Open. (This code will be moved to the |
- // DB thread as part of http://crbug.com/52909.) |
- { |
- base::ThreadRestrictions::ScopedAllowIO allow_io; |
- const FilePath dir = path_.DirName(); |
- if (!file_util::PathExists(dir) && !file_util::CreateDirectory(dir)) |
- return false; |
+void SQLitePersistentCookieStore::Backend::LoadAndNotifyOnDBThread( |
+ const LoadedCallback& loaded_callback) { |
+ DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); |
+ std::vector<net::CookieMonster::CanonicalCookie*> cookies; |
+ |
+ bool load_success = LoadInternal(&cookies); |
+ |
+ BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, base::Bind( |
+ &SQLitePersistentCookieStore::Backend::NotifyOnIOThread, |
+ base::Unretained(this), loaded_callback, load_success, cookies)); |
+} |
+ |
+void SQLitePersistentCookieStore::Backend::NotifyOnIOThread( |
+ const LoadedCallback& loaded_callback, |
+ bool load_success, |
+ const std::vector<net::CookieMonster::CanonicalCookie*>& cookies) { |
+ DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
+ loaded_callback.Run(cookies); |
+} |
+ |
+bool SQLitePersistentCookieStore::Backend::InitializeDatabase() { |
+ const FilePath dir = path_.DirName(); |
+ if (!file_util::PathExists(dir) && !file_util::CreateDirectory(dir)) { |
+ return false; |
} |
db_.reset(new sql::Connection); |
@@ -185,7 +219,15 @@ |
} |
db_->Preload(); |
+ return true; |
+} |
+bool SQLitePersistentCookieStore::Backend::LoadInternal( |
+ std::vector<net::CookieMonster::CanonicalCookie*>* cookies) { |
+ if (!InitializeDatabase()) { |
+ return false; |
+ } |
+ |
// Slurp all the cookies into the out-vector. |
sql::Statement smt(db_->GetUniqueStatement( |
"SELECT creation_utc, host_key, name, value, path, expires_utc, secure, " |
@@ -472,6 +514,7 @@ |
} |
SQLitePersistentCookieStore::SQLitePersistentCookieStore(const FilePath& path) |
: backend_(new Backend(path)) { |
+ DCHECK(!BrowserThread::CurrentlyOn(BrowserThread::DB)); |
} |
SQLitePersistentCookieStore::~SQLitePersistentCookieStore() { |
@@ -483,9 +526,8 @@ |
} |
} |
-bool SQLitePersistentCookieStore::Load( |
- std::vector<net::CookieMonster::CanonicalCookie*>* cookies) { |
- return backend_->Load(cookies); |
+bool SQLitePersistentCookieStore::Load(const LoadedCallback& loaded_callback) { |
+ return backend_->Load(loaded_callback); |
} |
void SQLitePersistentCookieStore::AddCookie( |