Chromium Code Reviews| Index: chrome/browser/net/sqlite_persistent_cookie_store.cc |
| =================================================================== |
| --- chrome/browser/net/sqlite_persistent_cookie_store.cc (revision 96178) |
| +++ 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,15 @@ |
| }; |
| private: |
| + // Creates or load the SQLite database on DB thread. |
| + void LoadAndNotifyOnDBThread(); |
| + // Notify the CookieMonster when loading complete. |
| + void NotifyOnIOThread(bool load_success); |
| + // Initialize the data base. |
| + bool InitializeDatabase(); |
| + // Load cookies to the data base, and read cookies. |
| + bool LoadInternal(); |
| + |
| // Batch a cookie operation (add or delete) |
| void BatchOperation(PendingOperation::OperationType op, |
| const net::CookieMonster::CanonicalCookie& cc); |
| @@ -103,6 +113,9 @@ |
| scoped_ptr<sql::Connection> db_; |
| sql::MetaTable meta_table_; |
| + std::vector<net::CookieMonster::CanonicalCookie*> cookies_; |
| + LoadedCallback loaded_callback_; |
|
Randy Smith (Not in Mondays)
2011/08/19 21:40:05
I'm not familiar with the new callback interface,
erikwright (departed)
2011/09/06 17:34:45
Done.
|
| + |
| typedef std::list<PendingOperation*> PendingOperationsList; |
| PendingOperationsList pending_; |
| PendingOperationsList::size_type num_pending_; |
| @@ -154,19 +167,35 @@ |
| } // 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()); |
| + DCHECK(loaded_callback_.is_null()); |
| + loaded_callback_ = loaded_callback; |
| + BrowserThread::PostTask( |
| + BrowserThread::DB, FROM_HERE, |
| + base::Bind(&Backend::LoadAndNotifyOnDBThread, base::Unretained(this))); |
| + 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() { |
| + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); |
| + bool load_success = LoadInternal(); |
| + BrowserThread::PostTask( |
| + BrowserThread::IO, FROM_HERE, |
| + base::Bind(&SQLitePersistentCookieStore::Backend::NotifyOnIOThread, |
| + base::Unretained(this), load_success)); |
| +} |
| + |
| +void SQLitePersistentCookieStore::Backend::NotifyOnIOThread(bool load_success) { |
| + 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 +214,14 @@ |
| } |
| db_->Preload(); |
| + return true; |
| +} |
| +bool SQLitePersistentCookieStore::Backend::LoadInternal() { |
| + 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, " |
| @@ -196,6 +232,11 @@ |
| return false; |
| } |
| + // Reserve space for the maximum amount of cookies a database should have. |
| + // This prevents multiple vector growth / copies as we append cookies. |
| + const size_t kMaxCookies = 3300; |
| + cookies_.reserve(kMaxCookies); |
| + |
| while (smt.Step()) { |
| scoped_ptr<net::CookieMonster::CanonicalCookie> cc( |
| new net::CookieMonster::CanonicalCookie( |
| @@ -215,7 +256,7 @@ |
| true)); // has_expires |
| DLOG_IF(WARNING, |
| cc->CreationDate() > Time::Now()) << L"CreationDate too recent"; |
| - cookies->push_back(cc.release()); |
| + cookies_.push_back(cc.release()); |
| } |
| return true; |
| @@ -483,9 +524,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( |