| 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(); |
| 97 // Notify the CookieMonster when loading complete. |
| 98 void NotifyOnIOThread(bool load_success); |
| 99 // Initialize the data base. |
| 100 bool InitializeDatabase(); |
| 101 // Load cookies to the data base, and read cookies. |
| 102 bool LoadInternal(); |
| 103 |
| 94 // Batch a cookie operation (add or delete) | 104 // Batch a cookie operation (add or delete) |
| 95 void BatchOperation(PendingOperation::OperationType op, | 105 void BatchOperation(PendingOperation::OperationType op, |
| 96 const net::CookieMonster::CanonicalCookie& cc); | 106 const net::CookieMonster::CanonicalCookie& cc); |
| 97 // Commit our pending operations to the database. | 107 // Commit our pending operations to the database. |
| 98 void Commit(); | 108 void Commit(); |
| 99 // Close() executed on the background thread. | 109 // Close() executed on the background thread. |
| 100 void InternalBackgroundClose(); | 110 void InternalBackgroundClose(); |
| 101 | 111 |
| 102 FilePath path_; | 112 FilePath path_; |
| 103 scoped_ptr<sql::Connection> db_; | 113 scoped_ptr<sql::Connection> db_; |
| 104 sql::MetaTable meta_table_; | 114 sql::MetaTable meta_table_; |
| 105 | 115 |
| 116 std::vector<net::CookieMonster::CanonicalCookie*> cookies_; |
| 117 LoadedCallback loaded_callback_; |
| 118 |
| 106 typedef std::list<PendingOperation*> PendingOperationsList; | 119 typedef std::list<PendingOperation*> PendingOperationsList; |
| 107 PendingOperationsList pending_; | 120 PendingOperationsList pending_; |
| 108 PendingOperationsList::size_type num_pending_; | 121 PendingOperationsList::size_type num_pending_; |
| 109 // True if the persistent store should be deleted upon destruction. | 122 // True if the persistent store should be deleted upon destruction. |
| 110 bool clear_local_state_on_exit_; | 123 bool clear_local_state_on_exit_; |
| 111 // Guard |pending_|, |num_pending_| and |clear_local_state_on_exit_|. | 124 // Guard |pending_|, |num_pending_| and |clear_local_state_on_exit_|. |
| 112 base::Lock lock_; | 125 base::Lock lock_; |
| 113 | 126 |
| 114 DISALLOW_COPY_AND_ASSIGN(Backend); | 127 DISALLOW_COPY_AND_ASSIGN(Backend); |
| 115 }; | 128 }; |
| (...skipping 31 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 DCHECK(loaded_callback_.is_null()); |
| 174 loaded_callback_ = loaded_callback; |
| 175 BrowserThread::PostTask( |
| 176 BrowserThread::DB, FROM_HERE, |
| 177 base::Bind(&Backend::LoadAndNotifyOnDBThread, base::Unretained(this))); |
| 178 return true; |
| 179 } |
| 160 | 180 |
| 161 // Ensure the parent directory for storing cookies is created before reading | 181 void SQLitePersistentCookieStore::Backend::LoadAndNotifyOnDBThread() { |
| 162 // from it. We make an exception to allow IO on the UI thread here because | 182 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); |
| 163 // we are going to disk anyway in db_->Open. (This code will be moved to the | 183 bool load_success = LoadInternal(); |
| 164 // DB thread as part of http://crbug.com/52909.) | 184 BrowserThread::PostTask( |
| 165 { | 185 BrowserThread::IO, FROM_HERE, |
| 166 base::ThreadRestrictions::ScopedAllowIO allow_io; | 186 base::Bind(&SQLitePersistentCookieStore::Backend::NotifyOnIOThread, |
| 167 const FilePath dir = path_.DirName(); | 187 base::Unretained(this), load_success)); |
| 168 if (!file_util::PathExists(dir) && !file_util::CreateDirectory(dir)) | 188 } |
| 169 return false; | 189 |
| 190 void SQLitePersistentCookieStore::Backend::NotifyOnIOThread(bool load_success) { |
| 191 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 192 loaded_callback_.Run(cookies_); |
| 193 } |
| 194 |
| 195 bool SQLitePersistentCookieStore::Backend::InitializeDatabase() { |
| 196 const FilePath dir = path_.DirName(); |
| 197 if (!file_util::PathExists(dir) && !file_util::CreateDirectory(dir)) { |
| 198 return false; |
| 170 } | 199 } |
| 171 | 200 |
| 172 db_.reset(new sql::Connection); | 201 db_.reset(new sql::Connection); |
| 173 if (!db_->Open(path_)) { | 202 if (!db_->Open(path_)) { |
| 174 NOTREACHED() << "Unable to open cookie DB."; | 203 NOTREACHED() << "Unable to open cookie DB."; |
| 175 db_.reset(); | 204 db_.reset(); |
| 176 return false; | 205 return false; |
| 177 } | 206 } |
| 178 | 207 |
| 179 db_->set_error_delegate(GetErrorHandlerForCookieDb()); | 208 db_->set_error_delegate(GetErrorHandlerForCookieDb()); |
| 180 | 209 |
| 181 if (!EnsureDatabaseVersion() || !InitTable(db_.get())) { | 210 if (!EnsureDatabaseVersion() || !InitTable(db_.get())) { |
| 182 NOTREACHED() << "Unable to open cookie DB."; | 211 NOTREACHED() << "Unable to open cookie DB."; |
| 183 db_.reset(); | 212 db_.reset(); |
| 184 return false; | 213 return false; |
| 185 } | 214 } |
| 186 | 215 |
| 187 db_->Preload(); | 216 db_->Preload(); |
| 217 return true; |
| 218 } |
| 219 |
| 220 bool SQLitePersistentCookieStore::Backend::LoadInternal() { |
| 221 if (!InitializeDatabase()) { |
| 222 return false; |
| 223 } |
| 188 | 224 |
| 189 // Slurp all the cookies into the out-vector. | 225 // Slurp all the cookies into the out-vector. |
| 190 sql::Statement smt(db_->GetUniqueStatement( | 226 sql::Statement smt(db_->GetUniqueStatement( |
| 191 "SELECT creation_utc, host_key, name, value, path, expires_utc, secure, " | 227 "SELECT creation_utc, host_key, name, value, path, expires_utc, secure, " |
| 192 "httponly, last_access_utc FROM cookies")); | 228 "httponly, last_access_utc FROM cookies")); |
| 193 if (!smt) { | 229 if (!smt) { |
| 194 NOTREACHED() << "select statement prep failed"; | 230 NOTREACHED() << "select statement prep failed"; |
| 195 db_.reset(); | 231 db_.reset(); |
| 196 return false; | 232 return false; |
| 197 } | 233 } |
| 198 | 234 |
| 235 // Reserve space for the maximum amount of cookies a database should have. |
| 236 // This prevents multiple vector growth / copies as we append cookies. |
| 237 const size_t kMaxCookies = 3300; |
| 238 cookies_.reserve(kMaxCookies); |
| 239 |
| 199 while (smt.Step()) { | 240 while (smt.Step()) { |
| 200 scoped_ptr<net::CookieMonster::CanonicalCookie> cc( | 241 scoped_ptr<net::CookieMonster::CanonicalCookie> cc( |
| 201 new net::CookieMonster::CanonicalCookie( | 242 new net::CookieMonster::CanonicalCookie( |
| 202 // The "source" URL is not used with persisted cookies. | 243 // The "source" URL is not used with persisted cookies. |
| 203 GURL(), // Source | 244 GURL(), // Source |
| 204 smt.ColumnString(2), // name | 245 smt.ColumnString(2), // name |
| 205 smt.ColumnString(3), // value | 246 smt.ColumnString(3), // value |
| 206 smt.ColumnString(1), // domain | 247 smt.ColumnString(1), // domain |
| 207 smt.ColumnString(4), // path | 248 smt.ColumnString(4), // path |
| 208 std::string(), // TODO(abarth): Persist mac_key | 249 std::string(), // TODO(abarth): Persist mac_key |
| 209 std::string(), // TODO(abarth): Persist mac_algorithm | 250 std::string(), // TODO(abarth): Persist mac_algorithm |
| 210 Time::FromInternalValue(smt.ColumnInt64(0)), // creation_utc | 251 Time::FromInternalValue(smt.ColumnInt64(0)), // creation_utc |
| 211 Time::FromInternalValue(smt.ColumnInt64(5)), // expires_utc | 252 Time::FromInternalValue(smt.ColumnInt64(5)), // expires_utc |
| 212 Time::FromInternalValue(smt.ColumnInt64(8)), // last_access_utc | 253 Time::FromInternalValue(smt.ColumnInt64(8)), // last_access_utc |
| 213 smt.ColumnInt(6) != 0, // secure | 254 smt.ColumnInt(6) != 0, // secure |
| 214 smt.ColumnInt(7) != 0, // httponly | 255 smt.ColumnInt(7) != 0, // httponly |
| 215 true)); // has_expires | 256 true)); // has_expires |
| 216 DLOG_IF(WARNING, | 257 DLOG_IF(WARNING, |
| 217 cc->CreationDate() > Time::Now()) << L"CreationDate too recent"; | 258 cc->CreationDate() > Time::Now()) << L"CreationDate too recent"; |
| 218 cookies->push_back(cc.release()); | 259 cookies_.push_back(cc.release()); |
| 219 } | 260 } |
| 220 | 261 |
| 221 return true; | 262 return true; |
| 222 } | 263 } |
| 223 | 264 |
| 224 bool SQLitePersistentCookieStore::Backend::EnsureDatabaseVersion() { | 265 bool SQLitePersistentCookieStore::Backend::EnsureDatabaseVersion() { |
| 225 // Version check. | 266 // Version check. |
| 226 if (!meta_table_.Init( | 267 if (!meta_table_.Init( |
| 227 db_.get(), kCurrentVersionNumber, kCompatibleVersionNumber)) { | 268 db_.get(), kCurrentVersionNumber, kCompatibleVersionNumber)) { |
| 228 return false; | 269 return false; |
| (...skipping 247 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 476 | 517 |
| 477 SQLitePersistentCookieStore::~SQLitePersistentCookieStore() { | 518 SQLitePersistentCookieStore::~SQLitePersistentCookieStore() { |
| 478 if (backend_.get()) { | 519 if (backend_.get()) { |
| 479 backend_->Close(); | 520 backend_->Close(); |
| 480 // Release our reference, it will probably still have a reference if the | 521 // Release our reference, it will probably still have a reference if the |
| 481 // background thread has not run Close() yet. | 522 // background thread has not run Close() yet. |
| 482 backend_ = NULL; | 523 backend_ = NULL; |
| 483 } | 524 } |
| 484 } | 525 } |
| 485 | 526 |
| 486 bool SQLitePersistentCookieStore::Load( | 527 bool SQLitePersistentCookieStore::Load(const LoadedCallback& loaded_callback) { |
| 487 std::vector<net::CookieMonster::CanonicalCookie*>* cookies) { | 528 return backend_->Load(loaded_callback); |
| 488 return backend_->Load(cookies); | |
| 489 } | 529 } |
| 490 | 530 |
| 491 void SQLitePersistentCookieStore::AddCookie( | 531 void SQLitePersistentCookieStore::AddCookie( |
| 492 const net::CookieMonster::CanonicalCookie& cc) { | 532 const net::CookieMonster::CanonicalCookie& cc) { |
| 493 if (backend_.get()) | 533 if (backend_.get()) |
| 494 backend_->AddCookie(cc); | 534 backend_->AddCookie(cc); |
| 495 } | 535 } |
| 496 | 536 |
| 497 void SQLitePersistentCookieStore::UpdateCookieAccessTime( | 537 void SQLitePersistentCookieStore::UpdateCookieAccessTime( |
| 498 const net::CookieMonster::CanonicalCookie& cc) { | 538 const net::CookieMonster::CanonicalCookie& cc) { |
| (...skipping 12 matching lines...) Expand all Loading... |
| 511 if (backend_.get()) | 551 if (backend_.get()) |
| 512 backend_->SetClearLocalStateOnExit(clear_local_state); | 552 backend_->SetClearLocalStateOnExit(clear_local_state); |
| 513 } | 553 } |
| 514 | 554 |
| 515 void SQLitePersistentCookieStore::Flush(Task* completion_task) { | 555 void SQLitePersistentCookieStore::Flush(Task* completion_task) { |
| 516 if (backend_.get()) | 556 if (backend_.get()) |
| 517 backend_->Flush(completion_task); | 557 backend_->Flush(completion_task); |
| 518 else if (completion_task) | 558 else if (completion_task) |
| 519 MessageLoop::current()->PostTask(FROM_HERE, completion_task); | 559 MessageLoop::current()->PostTask(FROM_HERE, completion_task); |
| 520 } | 560 } |
| OLD | NEW |