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