| 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 | 8 |
| 9 #include "base/basictypes.h" | 9 #include "base/basictypes.h" |
| 10 #include "base/bind.h" |
| 10 #include "base/file_path.h" | 11 #include "base/file_path.h" |
| 11 #include "base/file_util.h" | 12 #include "base/file_util.h" |
| 12 #include "base/logging.h" | 13 #include "base/logging.h" |
| 13 #include "base/memory/ref_counted.h" | 14 #include "base/memory/ref_counted.h" |
| 14 #include "base/memory/scoped_ptr.h" | 15 #include "base/memory/scoped_ptr.h" |
| 15 #include "base/metrics/histogram.h" | 16 #include "base/metrics/histogram.h" |
| 16 #include "base/string_util.h" | 17 #include "base/string_util.h" |
| 17 #include "base/threading/thread.h" | 18 #include "base/threading/thread.h" |
| 18 #include "base/threading/thread_restrictions.h" | 19 #include "base/threading/thread_restrictions.h" |
| 19 #include "chrome/browser/diagnostics/sqlite_diagnostics.h" | 20 #include "chrome/browser/diagnostics/sqlite_diagnostics.h" |
| (...skipping 11 matching lines...) Expand all Loading... |
| 31 : public base::RefCountedThreadSafe<SQLitePersistentCookieStore::Backend> { | 32 : public base::RefCountedThreadSafe<SQLitePersistentCookieStore::Backend> { |
| 32 public: | 33 public: |
| 33 explicit Backend(const FilePath& path) | 34 explicit Backend(const FilePath& path) |
| 34 : path_(path), | 35 : path_(path), |
| 35 db_(NULL), | 36 db_(NULL), |
| 36 num_pending_(0), | 37 num_pending_(0), |
| 37 clear_local_state_on_exit_(false) { | 38 clear_local_state_on_exit_(false) { |
| 38 } | 39 } |
| 39 | 40 |
| 40 // Creates or load the SQLite database. | 41 // Creates or load the SQLite database. |
| 41 bool Load(std::vector<net::CookieMonster::CanonicalCookie*>* cookies); | 42 bool Load(const LoadedCallback& loaded_callback); |
| 42 | 43 |
| 43 // Batch a cookie addition. | 44 // Batch a cookie addition. |
| 44 void AddCookie(const net::CookieMonster::CanonicalCookie& cc); | 45 void AddCookie(const net::CookieMonster::CanonicalCookie& cc); |
| 45 | 46 |
| 46 // Batch a cookie access time update. | 47 // Batch a cookie access time update. |
| 47 void UpdateCookieAccessTime(const net::CookieMonster::CanonicalCookie& cc); | 48 void UpdateCookieAccessTime(const net::CookieMonster::CanonicalCookie& cc); |
| 48 | 49 |
| 49 // Batch a cookie deletion. | 50 // Batch a cookie deletion. |
| 50 void DeleteCookie(const net::CookieMonster::CanonicalCookie& cc); | 51 void DeleteCookie(const net::CookieMonster::CanonicalCookie& cc); |
| 51 | 52 |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 84 | 85 |
| 85 OperationType op() const { return op_; } | 86 OperationType op() const { return op_; } |
| 86 const net::CookieMonster::CanonicalCookie& cc() const { return cc_; } | 87 const net::CookieMonster::CanonicalCookie& cc() const { return cc_; } |
| 87 | 88 |
| 88 private: | 89 private: |
| 89 OperationType op_; | 90 OperationType op_; |
| 90 net::CookieMonster::CanonicalCookie cc_; | 91 net::CookieMonster::CanonicalCookie cc_; |
| 91 }; | 92 }; |
| 92 | 93 |
| 93 private: | 94 private: |
| 95 // Creates or load the SQLite database on DB thread. |
| 96 void LoadAndNotifyOnDBThread(const LoadedCallback& loaded_callback); |
| 97 // Notify the CookieMonster when loading complete. |
| 98 void NotifyOnIOThread( |
| 99 const LoadedCallback& loaded_callback, |
| 100 bool load_success, |
| 101 const std::vector<net::CookieMonster::CanonicalCookie*>& cookies); |
| 102 // Initialize the data base. |
| 103 bool InitializeDatabase(); |
| 104 // Load cookies to the data base, and read cookies. |
| 105 bool LoadInternal(std::vector<net::CookieMonster::CanonicalCookie*>* cookies); |
| 106 |
| 94 // Batch a cookie operation (add or delete) | 107 // Batch a cookie operation (add or delete) |
| 95 void BatchOperation(PendingOperation::OperationType op, | 108 void BatchOperation(PendingOperation::OperationType op, |
| 96 const net::CookieMonster::CanonicalCookie& cc); | 109 const net::CookieMonster::CanonicalCookie& cc); |
| 97 // Commit our pending operations to the database. | 110 // Commit our pending operations to the database. |
| 98 void Commit(); | 111 void Commit(); |
| 99 // Close() executed on the background thread. | 112 // Close() executed on the background thread. |
| 100 void InternalBackgroundClose(); | 113 void InternalBackgroundClose(); |
| 101 | 114 |
| 102 FilePath path_; | 115 FilePath path_; |
| 103 scoped_ptr<sql::Connection> db_; | 116 scoped_ptr<sql::Connection> db_; |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 147 // Try to create the index every time. Older versions did not have this index, | 160 // Try to create the index every time. Older versions did not have this index, |
| 148 // so we want those people to get it. Ignore errors, since it may exist. | 161 // so we want those people to get it. Ignore errors, since it may exist. |
| 149 db->Execute("CREATE INDEX IF NOT EXISTS cookie_times ON cookies" | 162 db->Execute("CREATE INDEX IF NOT EXISTS cookie_times ON cookies" |
| 150 " (creation_utc)"); | 163 " (creation_utc)"); |
| 151 return true; | 164 return true; |
| 152 } | 165 } |
| 153 | 166 |
| 154 } // namespace | 167 } // namespace |
| 155 | 168 |
| 156 bool SQLitePersistentCookieStore::Backend::Load( | 169 bool SQLitePersistentCookieStore::Backend::Load( |
| 157 std::vector<net::CookieMonster::CanonicalCookie*>* cookies) { | 170 const LoadedCallback& loaded_callback) { |
| 158 // This function should be called only once per instance. | 171 // This function should be called only once per instance. |
| 159 DCHECK(!db_.get()); | 172 DCHECK(!db_.get()); |
| 173 BrowserThread::PostTask( |
| 174 BrowserThread::DB, FROM_HERE, |
| 175 base::Bind(&Backend::LoadAndNotifyOnDBThread, base::Unretained(this), |
| 176 loaded_callback)); |
| 177 return true; |
| 178 } |
| 160 | 179 |
| 161 // Ensure the parent directory for storing cookies is created before reading | 180 void SQLitePersistentCookieStore::Backend::LoadAndNotifyOnDBThread( |
| 162 // from it. We make an exception to allow IO on the UI thread here because | 181 const LoadedCallback& loaded_callback) { |
| 163 // we are going to disk anyway in db_->Open. (This code will be moved to the | 182 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); |
| 164 // DB thread as part of http://crbug.com/52909.) | 183 std::vector<net::CookieMonster::CanonicalCookie*> cookies; |
| 165 { | 184 |
| 166 base::ThreadRestrictions::ScopedAllowIO allow_io; | 185 bool load_success = LoadInternal(&cookies); |
| 167 const FilePath dir = path_.DirName(); | 186 |
| 168 if (!file_util::PathExists(dir) && !file_util::CreateDirectory(dir)) | 187 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, base::Bind( |
| 169 return false; | 188 &SQLitePersistentCookieStore::Backend::NotifyOnIOThread, |
| 189 base::Unretained(this), loaded_callback, load_success, cookies)); |
| 190 } |
| 191 |
| 192 void SQLitePersistentCookieStore::Backend::NotifyOnIOThread( |
| 193 const LoadedCallback& loaded_callback, |
| 194 bool load_success, |
| 195 const std::vector<net::CookieMonster::CanonicalCookie*>& cookies) { |
| 196 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 197 loaded_callback.Run(cookies); |
| 198 } |
| 199 |
| 200 bool SQLitePersistentCookieStore::Backend::InitializeDatabase() { |
| 201 const FilePath dir = path_.DirName(); |
| 202 if (!file_util::PathExists(dir) && !file_util::CreateDirectory(dir)) { |
| 203 return false; |
| 170 } | 204 } |
| 171 | 205 |
| 172 db_.reset(new sql::Connection); | 206 db_.reset(new sql::Connection); |
| 173 if (!db_->Open(path_)) { | 207 if (!db_->Open(path_)) { |
| 174 NOTREACHED() << "Unable to open cookie DB."; | 208 NOTREACHED() << "Unable to open cookie DB."; |
| 175 db_.reset(); | 209 db_.reset(); |
| 176 return false; | 210 return false; |
| 177 } | 211 } |
| 178 | 212 |
| 179 db_->set_error_delegate(GetErrorHandlerForCookieDb()); | 213 db_->set_error_delegate(GetErrorHandlerForCookieDb()); |
| 180 | 214 |
| 181 if (!EnsureDatabaseVersion() || !InitTable(db_.get())) { | 215 if (!EnsureDatabaseVersion() || !InitTable(db_.get())) { |
| 182 NOTREACHED() << "Unable to open cookie DB."; | 216 NOTREACHED() << "Unable to open cookie DB."; |
| 183 db_.reset(); | 217 db_.reset(); |
| 184 return false; | 218 return false; |
| 185 } | 219 } |
| 186 | 220 |
| 187 db_->Preload(); | 221 db_->Preload(); |
| 222 return true; |
| 223 } |
| 224 |
| 225 bool SQLitePersistentCookieStore::Backend::LoadInternal( |
| 226 std::vector<net::CookieMonster::CanonicalCookie*>* cookies) { |
| 227 if (!InitializeDatabase()) { |
| 228 return false; |
| 229 } |
| 188 | 230 |
| 189 // Slurp all the cookies into the out-vector. | 231 // Slurp all the cookies into the out-vector. |
| 190 sql::Statement smt(db_->GetUniqueStatement( | 232 sql::Statement smt(db_->GetUniqueStatement( |
| 191 "SELECT creation_utc, host_key, name, value, path, expires_utc, secure, " | 233 "SELECT creation_utc, host_key, name, value, path, expires_utc, secure, " |
| 192 "httponly, last_access_utc FROM cookies")); | 234 "httponly, last_access_utc FROM cookies")); |
| 193 if (!smt) { | 235 if (!smt) { |
| 194 NOTREACHED() << "select statement prep failed"; | 236 NOTREACHED() << "select statement prep failed"; |
| 195 db_.reset(); | 237 db_.reset(); |
| 196 return false; | 238 return false; |
| 197 } | 239 } |
| 198 | 240 |
| 241 // Reserve space for the maximum amount of cookies a database should have. |
| 242 // This prevents multiple vector growth / copies as we append cookies. |
| 243 const size_t kMaxCookies = 3300; |
| 244 cookies->reserve(kMaxCookies); |
| 245 |
| 199 while (smt.Step()) { | 246 while (smt.Step()) { |
| 200 scoped_ptr<net::CookieMonster::CanonicalCookie> cc( | 247 scoped_ptr<net::CookieMonster::CanonicalCookie> cc( |
| 201 new net::CookieMonster::CanonicalCookie( | 248 new net::CookieMonster::CanonicalCookie( |
| 202 // The "source" URL is not used with persisted cookies. | 249 // The "source" URL is not used with persisted cookies. |
| 203 GURL(), // Source | 250 GURL(), // Source |
| 204 smt.ColumnString(2), // name | 251 smt.ColumnString(2), // name |
| 205 smt.ColumnString(3), // value | 252 smt.ColumnString(3), // value |
| 206 smt.ColumnString(1), // domain | 253 smt.ColumnString(1), // domain |
| 207 smt.ColumnString(4), // path | 254 smt.ColumnString(4), // path |
| 208 std::string(), // TODO(abarth): Persist mac_key | 255 std::string(), // TODO(abarth): Persist mac_key |
| (...skipping 267 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 476 | 523 |
| 477 SQLitePersistentCookieStore::~SQLitePersistentCookieStore() { | 524 SQLitePersistentCookieStore::~SQLitePersistentCookieStore() { |
| 478 if (backend_.get()) { | 525 if (backend_.get()) { |
| 479 backend_->Close(); | 526 backend_->Close(); |
| 480 // Release our reference, it will probably still have a reference if the | 527 // Release our reference, it will probably still have a reference if the |
| 481 // background thread has not run Close() yet. | 528 // background thread has not run Close() yet. |
| 482 backend_ = NULL; | 529 backend_ = NULL; |
| 483 } | 530 } |
| 484 } | 531 } |
| 485 | 532 |
| 486 bool SQLitePersistentCookieStore::Load( | 533 bool SQLitePersistentCookieStore::Load(const LoadedCallback& loaded_callback) { |
| 487 std::vector<net::CookieMonster::CanonicalCookie*>* cookies) { | 534 return backend_->Load(loaded_callback); |
| 488 return backend_->Load(cookies); | |
| 489 } | 535 } |
| 490 | 536 |
| 491 void SQLitePersistentCookieStore::AddCookie( | 537 void SQLitePersistentCookieStore::AddCookie( |
| 492 const net::CookieMonster::CanonicalCookie& cc) { | 538 const net::CookieMonster::CanonicalCookie& cc) { |
| 493 if (backend_.get()) | 539 if (backend_.get()) |
| 494 backend_->AddCookie(cc); | 540 backend_->AddCookie(cc); |
| 495 } | 541 } |
| 496 | 542 |
| 497 void SQLitePersistentCookieStore::UpdateCookieAccessTime( | 543 void SQLitePersistentCookieStore::UpdateCookieAccessTime( |
| 498 const net::CookieMonster::CanonicalCookie& cc) { | 544 const net::CookieMonster::CanonicalCookie& cc) { |
| (...skipping 12 matching lines...) Expand all Loading... |
| 511 if (backend_.get()) | 557 if (backend_.get()) |
| 512 backend_->SetClearLocalStateOnExit(clear_local_state); | 558 backend_->SetClearLocalStateOnExit(clear_local_state); |
| 513 } | 559 } |
| 514 | 560 |
| 515 void SQLitePersistentCookieStore::Flush(Task* completion_task) { | 561 void SQLitePersistentCookieStore::Flush(Task* completion_task) { |
| 516 if (backend_.get()) | 562 if (backend_.get()) |
| 517 backend_->Flush(completion_task); | 563 backend_->Flush(completion_task); |
| 518 else if (completion_task) | 564 else if (completion_task) |
| 519 MessageLoop::current()->PostTask(FROM_HERE, completion_task); | 565 MessageLoop::current()->PostTask(FROM_HERE, completion_task); |
| 520 } | 566 } |
| OLD | NEW |