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 #include <map> | |
| 9 #include <set> | |
| 10 #include <utility> | |
| 8 | 11 |
| 9 #include "base/basictypes.h" | 12 #include "base/basictypes.h" |
| 10 #include "base/bind.h" | 13 #include "base/bind.h" |
| 11 #include "base/file_path.h" | 14 #include "base/file_path.h" |
| 12 #include "base/file_util.h" | 15 #include "base/file_util.h" |
| 13 #include "base/logging.h" | 16 #include "base/logging.h" |
| 14 #include "base/memory/ref_counted.h" | 17 #include "base/memory/ref_counted.h" |
| 15 #include "base/memory/scoped_ptr.h" | 18 #include "base/memory/scoped_ptr.h" |
| 16 #include "base/metrics/histogram.h" | 19 #include "base/metrics/histogram.h" |
| 17 #include "base/string_util.h" | 20 #include "base/string_util.h" |
| 21 #include "base/synchronization/lock.h" | |
| 18 #include "base/threading/thread.h" | 22 #include "base/threading/thread.h" |
| 19 #include "base/threading/thread_restrictions.h" | 23 #include "base/threading/thread_restrictions.h" |
| 20 #include "chrome/browser/diagnostics/sqlite_diagnostics.h" | 24 #include "chrome/browser/diagnostics/sqlite_diagnostics.h" |
| 21 #include "content/browser/browser_thread.h" | 25 #include "content/browser/browser_thread.h" |
| 22 #include "googleurl/src/gurl.h" | 26 #include "googleurl/src/gurl.h" |
| 27 #include "net/base/registry_controlled_domain.h" | |
| 28 | |
|
erikwright (departed)
2011/09/20 19:00:44
nit: Remove this blank line.
guohui
2011/10/06 15:40:00
Done.
| |
| 23 #include "sql/meta_table.h" | 29 #include "sql/meta_table.h" |
| 24 #include "sql/statement.h" | 30 #include "sql/statement.h" |
| 25 #include "sql/transaction.h" | 31 #include "sql/transaction.h" |
| 26 | 32 |
| 27 using base::Time; | 33 using base::Time; |
| 28 | 34 |
| 29 // This class is designed to be shared between any calling threads and the | 35 // This class is designed to be shared between any calling threads and the |
| 30 // database thread. It batches operations and commits them on a timer. | 36 // database thread. It batches operations and commits them on a timer. |
| 31 // This class expects to be Load()'ed once on any thread. Loading occurs | 37 |
| 32 // asynchronously on the DB thread and the caller will be notified on the IO | 38 // This class loads cookies by domain keys (always eTLD+1). Loading is triggered |
|
Randy Smith (Not in Mondays)
2011/09/21 20:52:39
I thought it was the CookieMonster that was based
guohui
2011/10/06 15:40:00
Done.
| |
| 33 // thread. Subsequent to loading, mutations may be queued by any thread using | 39 // either by Load() which chain-loads cookies for all domain keys, or by |
| 40 // LoadCookiesForKey() which loads cookies for a specific domain key and thus | |
| 41 // allows individual URL requests to be processed before the whole cookie store | |
| 42 // is loaded. In both cases, loading occurs asynchronously on the DB thread and | |
| 43 // the caller will be notified on the IO thread upon the loading completes, with | |
| 44 // all cookies that have been accumulated in cookies_ since last notification. | |
| 45 | |
| 46 // Subsequent to loading, mutations may be queued by any thread using | |
| 34 // AddCookie, UpdateCookieAccessTime, and DeleteCookie. These are flushed to | 47 // AddCookie, UpdateCookieAccessTime, and DeleteCookie. These are flushed to |
| 35 // disk on the DB thread every 30 seconds, 512 operations, or call to Flush(), | 48 // disk on the DB thread every 30 seconds, 512 operations, or call to Flush(), |
| 36 // whichever occurs first. | 49 // whichever occurs first. |
| 37 class SQLitePersistentCookieStore::Backend | 50 class SQLitePersistentCookieStore::Backend |
| 38 : public base::RefCountedThreadSafe<SQLitePersistentCookieStore::Backend> { | 51 : public base::RefCountedThreadSafe<SQLitePersistentCookieStore::Backend> { |
| 39 public: | 52 public: |
| 40 explicit Backend(const FilePath& path) | 53 explicit Backend(const FilePath& path) |
| 41 : path_(path), | 54 : path_(path), |
| 42 db_(NULL), | 55 db_(NULL), |
| 43 num_pending_(0), | 56 num_pending_(0), |
| 44 clear_local_state_on_exit_(false) { | 57 clear_local_state_on_exit_(false) { |
| 45 } | 58 } |
| 46 | 59 |
| 47 // Creates or load the SQLite database. | 60 // Creates or loads the SQLite database. |
| 48 bool Load(const LoadedCallback& loaded_callback); | 61 void Load(const LoadedCallback& loaded_callback); |
| 62 | |
| 63 // Loads cookies for the domain key (eTLD+1). | |
| 64 void LoadCookiesForKey(const std::string& domain, | |
| 65 const LoadedCallback& loaded_callback); | |
| 49 | 66 |
| 50 // Batch a cookie addition. | 67 // Batch a cookie addition. |
| 51 void AddCookie(const net::CookieMonster::CanonicalCookie& cc); | 68 void AddCookie(const net::CookieMonster::CanonicalCookie& cc); |
| 52 | 69 |
| 53 // Batch a cookie access time update. | 70 // Batch a cookie access time update. |
| 54 void UpdateCookieAccessTime(const net::CookieMonster::CanonicalCookie& cc); | 71 void UpdateCookieAccessTime(const net::CookieMonster::CanonicalCookie& cc); |
| 55 | 72 |
| 56 // Batch a cookie deletion. | 73 // Batch a cookie deletion. |
| 57 void DeleteCookie(const net::CookieMonster::CanonicalCookie& cc); | 74 void DeleteCookie(const net::CookieMonster::CanonicalCookie& cc); |
| 58 | 75 |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 91 | 108 |
| 92 OperationType op() const { return op_; } | 109 OperationType op() const { return op_; } |
| 93 const net::CookieMonster::CanonicalCookie& cc() const { return cc_; } | 110 const net::CookieMonster::CanonicalCookie& cc() const { return cc_; } |
| 94 | 111 |
| 95 private: | 112 private: |
| 96 OperationType op_; | 113 OperationType op_; |
| 97 net::CookieMonster::CanonicalCookie cc_; | 114 net::CookieMonster::CanonicalCookie cc_; |
| 98 }; | 115 }; |
| 99 | 116 |
| 100 private: | 117 private: |
| 101 // Creates or load the SQLite database on DB thread. | 118 // Creates or loads the SQLite database on DB thread. |
| 102 void LoadAndNotifyOnDBThread(const LoadedCallback& loaded_callback); | 119 void LoadAndNotifyOnDBThread(const LoadedCallback& loaded_callback); |
| 103 // Notify the CookieMonster when loading complete. | 120 |
| 121 // Loads cookies for the domain key (eTLD+1) on DB thread. | |
| 122 void LoadKeyAndNotifyOnDBThread(const std::string& domains, | |
| 123 const LoadedCallback& loaded_callback); | |
| 124 | |
| 125 // Notifies the CookieMonster when loading completes for a specific domain key | |
| 126 // or for all domain keys. Triggers the callback and passes it all cookies | |
| 127 // that have been loaded from DB since last IO notification. | |
| 104 void NotifyOnIOThread( | 128 void NotifyOnIOThread( |
| 105 const LoadedCallback& loaded_callback, | 129 const LoadedCallback& loaded_callback, |
| 106 bool load_success, | 130 bool load_success); |
| 107 const std::vector<net::CookieMonster::CanonicalCookie*>& cookies); | |
| 108 // Initialize the data base. | 131 // Initialize the data base. |
| 132 | |
| 109 bool InitializeDatabase(); | 133 bool InitializeDatabase(); |
| 110 // Load cookies to the data base, and read cookies. | 134 |
| 111 bool LoadInternal(std::vector<net::CookieMonster::CanonicalCookie*>* cookies); | 135 // Chain load cookies from DB by domain key. |
| 136 void ChainLoadCookies(const LoadedCallback& loaded_callback); | |
| 137 | |
| 138 // Load all cookies for a set of domains/hosts | |
| 139 bool LoadCookiesForDomains(const std::set<std::string>& key); | |
| 112 | 140 |
| 113 // Batch a cookie operation (add or delete) | 141 // Batch a cookie operation (add or delete) |
| 114 void BatchOperation(PendingOperation::OperationType op, | 142 void BatchOperation(PendingOperation::OperationType op, |
| 115 const net::CookieMonster::CanonicalCookie& cc); | 143 const net::CookieMonster::CanonicalCookie& cc); |
| 116 // Commit our pending operations to the database. | 144 // Commit our pending operations to the database. |
| 117 void Commit(); | 145 void Commit(); |
| 118 // Close() executed on the background thread. | 146 // Close() executed on the background thread. |
| 119 void InternalBackgroundClose(); | 147 void InternalBackgroundClose(); |
| 120 | 148 |
| 121 FilePath path_; | 149 FilePath path_; |
| 122 scoped_ptr<sql::Connection> db_; | 150 scoped_ptr<sql::Connection> db_; |
| 123 sql::MetaTable meta_table_; | 151 sql::MetaTable meta_table_; |
| 124 | 152 |
| 125 typedef std::list<PendingOperation*> PendingOperationsList; | 153 typedef std::list<PendingOperation*> PendingOperationsList; |
| 126 PendingOperationsList pending_; | 154 PendingOperationsList pending_; |
| 127 PendingOperationsList::size_type num_pending_; | 155 PendingOperationsList::size_type num_pending_; |
| 128 // True if the persistent store should be deleted upon destruction. | 156 // True if the persistent store should be deleted upon destruction. |
| 129 bool clear_local_state_on_exit_; | 157 bool clear_local_state_on_exit_; |
| 130 // Guard |pending_|, |num_pending_| and |clear_local_state_on_exit_|. | 158 // Guard |cookies_|, |pending_|, |num_pending_| and |
| 159 // |clear_local_state_on_exit_|. | |
| 131 base::Lock lock_; | 160 base::Lock lock_; |
| 132 | 161 |
| 162 // Temporary buffer for cookies loaded from DB. Accumulates cookies to reduce | |
| 163 // the number of messages sent to the IO thread. Sent back in response to | |
| 164 // individual load requests for domain keys or when all loading completes. | |
| 165 std::vector<net::CookieMonster::CanonicalCookie*> cookies_; | |
| 166 | |
| 167 // Map of domain keys(eTLD+1) to domains/hosts that are to be loaded from DB. | |
| 168 std::map<std::string, std::set<std::string>> keys_to_load_; | |
| 169 | |
| 170 // Indicates if DB has been initialized. | |
| 171 bool initialized_; | |
| 172 | |
| 133 DISALLOW_COPY_AND_ASSIGN(Backend); | 173 DISALLOW_COPY_AND_ASSIGN(Backend); |
| 134 }; | 174 }; |
| 135 | 175 |
| 136 // Version number of the database. In version 4, we migrated the time epoch. | 176 // Version number of the database. In version 4, we migrated the time epoch. |
| 137 // If you open the DB with an older version on Mac or Linux, the times will | 177 // If you open the DB with an older version on Mac or Linux, the times will |
| 138 // look wonky, but the file will likely be usable. On Windows version 3 and 4 | 178 // look wonky, but the file will likely be usable. On Windows version 3 and 4 |
| 139 // are the same. | 179 // are the same. |
| 140 // | 180 // |
| 141 // Version 3 updated the database to include the last access time, so we can | 181 // Version 3 updated the database to include the last access time, so we can |
| 142 // expire them in decreasing order of use when we've reached the maximum | 182 // expire them in decreasing order of use when we've reached the maximum |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 160 "secure INTEGER NOT NULL," | 200 "secure INTEGER NOT NULL," |
| 161 "httponly INTEGER NOT NULL," | 201 "httponly INTEGER NOT NULL," |
| 162 "last_access_utc INTEGER NOT NULL)")) | 202 "last_access_utc INTEGER NOT NULL)")) |
| 163 return false; | 203 return false; |
| 164 } | 204 } |
| 165 | 205 |
| 166 // Try to create the index every time. Older versions did not have this index, | 206 // Try to create the index every time. Older versions did not have this index, |
| 167 // so we want those people to get it. Ignore errors, since it may exist. | 207 // so we want those people to get it. Ignore errors, since it may exist. |
| 168 db->Execute("CREATE INDEX IF NOT EXISTS cookie_times ON cookies" | 208 db->Execute("CREATE INDEX IF NOT EXISTS cookie_times ON cookies" |
| 169 " (creation_utc)"); | 209 " (creation_utc)"); |
| 210 | |
| 211 db->Execute("CREATE INDEX IF NOT EXISTS domain ON cookies(host_key)"); | |
| 212 | |
| 170 return true; | 213 return true; |
| 171 } | 214 } |
| 172 | 215 |
| 173 } // namespace | 216 } // namespace |
| 174 | 217 |
| 175 bool SQLitePersistentCookieStore::Backend::Load( | 218 void SQLitePersistentCookieStore::Backend::Load( |
| 176 const LoadedCallback& loaded_callback) { | 219 const LoadedCallback& loaded_callback) { |
| 177 // This function should be called only once per instance. | 220 // This function should be called only once per instance. |
| 178 DCHECK(!db_.get()); | 221 DCHECK(!db_.get()); |
| 179 BrowserThread::PostTask( | 222 BrowserThread::PostTask( |
| 180 BrowserThread::DB, FROM_HERE, | 223 BrowserThread::DB, FROM_HERE, |
| 181 base::Bind(&Backend::LoadAndNotifyOnDBThread, base::Unretained(this), | 224 base::Bind(&Backend::LoadAndNotifyOnDBThread, this, loaded_callback)); |
| 182 loaded_callback)); | 225 } |
| 183 return true; | 226 |
| 227 void SQLitePersistentCookieStore::Backend::LoadCookiesForKey( | |
| 228 const std::string& key, | |
| 229 const LoadedCallback& loaded_callback) { | |
| 230 BrowserThread::PostTask( | |
| 231 BrowserThread::DB, FROM_HERE, | |
| 232 base::Bind(&Backend::LoadKeyAndNotifyOnDBThread, this, | |
| 233 key, | |
| 234 loaded_callback)); | |
| 184 } | 235 } |
| 185 | 236 |
| 186 void SQLitePersistentCookieStore::Backend::LoadAndNotifyOnDBThread( | 237 void SQLitePersistentCookieStore::Backend::LoadAndNotifyOnDBThread( |
| 187 const LoadedCallback& loaded_callback) { | 238 const LoadedCallback& loaded_callback) { |
| 188 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); | 239 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); |
| 189 std::vector<net::CookieMonster::CanonicalCookie*> cookies; | |
| 190 | 240 |
| 191 bool load_success = LoadInternal(&cookies); | 241 if (!InitializeDatabase()) { |
| 242 BrowserThread::PostTask( | |
| 243 BrowserThread::IO, FROM_HERE, | |
| 244 base::Bind(&SQLitePersistentCookieStore::Backend::NotifyOnIOThread, | |
| 245 this, loaded_callback, false)); | |
| 246 } else { | |
| 247 ChainLoadCookies(loaded_callback); | |
| 248 } | |
| 249 } | |
| 192 | 250 |
| 193 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, base::Bind( | 251 void SQLitePersistentCookieStore::Backend::LoadKeyAndNotifyOnDBThread( |
| 194 &SQLitePersistentCookieStore::Backend::NotifyOnIOThread, | 252 const std::string& key, |
| 195 base::Unretained(this), loaded_callback, load_success, cookies)); | 253 const LoadedCallback& loaded_callback) { |
| 254 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); | |
| 255 | |
| 256 bool success = false; | |
| 257 if (InitializeDatabase()) { | |
| 258 std::map<std::string, std::set<std::string> >::iterator | |
| 259 it = keys_to_load_.find(key); | |
| 260 if (it != keys_to_load_.end()) { | |
| 261 success = LoadCookiesForDomains(it->second); | |
| 262 keys_to_load_.erase(it); | |
| 263 } else { | |
| 264 success = true; | |
| 265 } | |
| 266 } | |
| 267 | |
| 268 BrowserThread::PostTask( | |
| 269 BrowserThread::IO, FROM_HERE, | |
| 270 base::Bind(&SQLitePersistentCookieStore::Backend::NotifyOnIOThread, | |
| 271 this, loaded_callback, success)); | |
| 196 } | 272 } |
| 197 | 273 |
| 198 void SQLitePersistentCookieStore::Backend::NotifyOnIOThread( | 274 void SQLitePersistentCookieStore::Backend::NotifyOnIOThread( |
| 199 const LoadedCallback& loaded_callback, | 275 const LoadedCallback& loaded_callback, |
| 200 bool load_success, | 276 bool load_success) { |
| 201 const std::vector<net::CookieMonster::CanonicalCookie*>& cookies) { | |
| 202 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 277 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 278 | |
| 279 std::vector<net::CookieMonster::CanonicalCookie*> cookies; | |
| 280 { | |
| 281 base::AutoLock locked(lock_); | |
| 282 cookies = cookies_; | |
| 283 cookies_.clear(); | |
| 284 } | |
| 285 | |
| 203 loaded_callback.Run(cookies); | 286 loaded_callback.Run(cookies); |
| 204 } | 287 } |
| 205 | 288 |
| 206 bool SQLitePersistentCookieStore::Backend::InitializeDatabase() { | 289 bool SQLitePersistentCookieStore::Backend::InitializeDatabase() { |
| 290 if (initialized_) { | |
| 291 return true; | |
| 292 } | |
| 293 | |
| 207 const FilePath dir = path_.DirName(); | 294 const FilePath dir = path_.DirName(); |
| 208 if (!file_util::PathExists(dir) && !file_util::CreateDirectory(dir)) { | 295 if (!file_util::PathExists(dir) && !file_util::CreateDirectory(dir)) { |
| 209 return false; | 296 return false; |
| 210 } | 297 } |
| 211 | 298 |
| 212 db_.reset(new sql::Connection); | 299 db_.reset(new sql::Connection); |
| 213 if (!db_->Open(path_)) { | 300 if (!db_->Open(path_)) { |
| 214 NOTREACHED() << "Unable to open cookie DB."; | 301 NOTREACHED() << "Unable to open cookie DB."; |
| 215 db_.reset(); | 302 db_.reset(); |
| 216 return false; | 303 return false; |
| 217 } | 304 } |
| 218 | 305 |
| 219 db_->set_error_delegate(GetErrorHandlerForCookieDb()); | 306 db_->set_error_delegate(GetErrorHandlerForCookieDb()); |
| 220 | 307 |
| 221 if (!EnsureDatabaseVersion() || !InitTable(db_.get())) { | 308 if (!EnsureDatabaseVersion() || !InitTable(db_.get())) { |
| 222 NOTREACHED() << "Unable to open cookie DB."; | 309 NOTREACHED() << "Unable to open cookie DB."; |
| 223 db_.reset(); | 310 db_.reset(); |
| 224 return false; | 311 return false; |
| 225 } | 312 } |
| 226 | 313 |
| 227 db_->Preload(); | 314 db_->Preload(); |
| 228 return true; | |
| 229 } | |
| 230 | 315 |
| 231 bool SQLitePersistentCookieStore::Backend::LoadInternal( | 316 // Retrieve all the domains |
| 232 std::vector<net::CookieMonster::CanonicalCookie*>* cookies) { | 317 sql::Statement smt(db_->GetUniqueStatement( |
| 233 if (!InitializeDatabase()) { | 318 "SELECT DISTINCT host_key FROM cookies")); |
| 234 return false; | |
| 235 } | |
| 236 | 319 |
| 237 // Slurp all the cookies into the out-vector. | |
| 238 sql::Statement smt(db_->GetUniqueStatement( | |
| 239 "SELECT creation_utc, host_key, name, value, path, expires_utc, secure, " | |
| 240 "httponly, last_access_utc FROM cookies")); | |
| 241 if (!smt) { | 320 if (!smt) { |
| 242 NOTREACHED() << "select statement prep failed"; | 321 NOTREACHED() << "select statement prep failed"; |
| 243 db_.reset(); | 322 db_.reset(); |
| 244 return false; | 323 return false; |
| 245 } | 324 } |
| 246 | 325 |
| 326 // Build a map of domain keys (always eTLD+1) to domains. | |
| 247 while (smt.Step()) { | 327 while (smt.Step()) { |
| 248 scoped_ptr<net::CookieMonster::CanonicalCookie> cc( | 328 std::string domain = smt.ColumnString(0); |
| 249 new net::CookieMonster::CanonicalCookie( | 329 std::string key = |
| 250 // The "source" URL is not used with persisted cookies. | 330 net::RegistryControlledDomainService::GetDomainAndRegistry(domain); |
| 251 GURL(), // Source | 331 |
| 252 smt.ColumnString(2), // name | 332 std::map<std::string, std::set<std::string> >::iterator it = |
| 253 smt.ColumnString(3), // value | 333 keys_to_load_.find(key); |
| 254 smt.ColumnString(1), // domain | 334 if (it == keys_to_load_.end()) |
| 255 smt.ColumnString(4), // path | 335 it = keys_to_load_.insert(std::make_pair |
| 256 std::string(), // TODO(abarth): Persist mac_key | 336 (key, std::set<std::string>())).first; |
| 257 std::string(), // TODO(abarth): Persist mac_algorithm | 337 it->second.insert(domain); |
| 258 Time::FromInternalValue(smt.ColumnInt64(0)), // creation_utc | 338 } |
| 259 Time::FromInternalValue(smt.ColumnInt64(5)), // expires_utc | 339 |
| 260 Time::FromInternalValue(smt.ColumnInt64(8)), // last_access_utc | 340 initialized_ = true; |
| 261 smt.ColumnInt(6) != 0, // secure | 341 return true; |
| 262 smt.ColumnInt(7) != 0, // httponly | 342 } |
| 263 true)); // has_expires | 343 |
| 264 DLOG_IF(WARNING, | 344 void SQLitePersistentCookieStore::Backend::ChainLoadCookies( |
| 265 cc->CreationDate() > Time::Now()) << L"CreationDate too recent"; | 345 const LoadedCallback& loaded_callback) { |
|
Randy Smith (Not in Mondays)
2011/09/21 20:52:39
It's fairly important (for both reader and compute
guohui
2011/10/06 15:40:00
Done.
| |
| 266 cookies->push_back(cc.release()); | 346 bool load_success = true; |
| 347 | |
| 348 if (keys_to_load_.size() > 0) { | |
| 349 // Load cookies for the first domain key. | |
| 350 std::map<std::string, std::set<std::string> >::iterator | |
| 351 it = keys_to_load_.begin(); | |
| 352 load_success = LoadCookiesForDomains(it->second); | |
| 353 keys_to_load_.erase(it); | |
| 354 } | |
| 355 | |
| 356 // If load is successful and there are more domain keys to be loaded, | |
| 357 // then post a DB task to continue chain-load; | |
| 358 // Otherwise notify on IO thread. | |
| 359 if (load_success && keys_to_load_.size() > 0) { | |
| 360 BrowserThread::PostTask( | |
| 361 BrowserThread::DB, FROM_HERE, | |
| 362 base::Bind(&Backend::ChainLoadCookies, this, loaded_callback)); | |
| 363 } else { | |
| 364 BrowserThread::PostTask( | |
| 365 BrowserThread::IO, FROM_HERE, | |
| 366 base::Bind(&SQLitePersistentCookieStore::Backend::NotifyOnIOThread, | |
| 367 this, loaded_callback, load_success)); | |
| 368 } | |
| 369 } | |
| 370 | |
| 371 bool SQLitePersistentCookieStore::Backend::LoadCookiesForDomains( | |
| 372 const std::set<std::string>& domains) { | |
| 373 sql::Statement smt(db_->GetCachedStatement(SQL_FROM_HERE, | |
| 374 "SELECT creation_utc, host_key, name, value, path, expires_utc, secure, " | |
| 375 "httponly, last_access_utc FROM cookies WHERE host_key = ?")); | |
| 376 if (!smt) { | |
| 377 NOTREACHED() << "select statement prep failed"; | |
| 378 db_.reset(); | |
| 379 return false; | |
| 380 } | |
| 381 | |
| 382 std::set<std::string>::const_iterator it = domains.begin(); | |
| 383 for (; it != domains.end(); ++it) { | |
| 384 base::AutoLock locked(lock_); | |
|
erikwright (departed)
2011/09/20 19:00:44
I think what Randy had in mind was to only lock ar
Randy Smith (Not in Mondays)
2011/09/21 20:52:39
Yes, and I think of file system operations as gene
guohui
2011/10/06 15:40:00
Fixed by accumulating all cookies in a local colle
| |
| 385 smt.BindString(0, *it); | |
| 386 while (smt.Step()) { | |
| 387 scoped_ptr<net::CookieMonster::CanonicalCookie> cc( | |
| 388 new net::CookieMonster::CanonicalCookie( | |
| 389 // The "source" URL is not used with persisted cookies. | |
| 390 GURL(), // Source | |
| 391 smt.ColumnString(2), // name | |
| 392 smt.ColumnString(3), // value | |
| 393 smt.ColumnString(1), // domain | |
| 394 smt.ColumnString(4), // path | |
| 395 std::string(), // TODO(abarth): Persist mac_key | |
| 396 std::string(), // TODO(abarth): Persist mac_algorithm | |
| 397 Time::FromInternalValue(smt.ColumnInt64(0)), // creation_utc | |
| 398 Time::FromInternalValue(smt.ColumnInt64(5)), // expires_utc | |
| 399 Time::FromInternalValue(smt.ColumnInt64(8)), // last_access_utc | |
| 400 smt.ColumnInt(6) != 0, // secure | |
| 401 smt.ColumnInt(7) != 0, // httponly | |
| 402 true)); // has_expires | |
| 403 DLOG_IF(WARNING, | |
| 404 cc->CreationDate() > Time::Now()) << L"CreationDate too recent"; | |
| 405 cookies_.push_back(cc.release()); | |
| 406 } | |
| 407 smt.Reset(); | |
| 267 } | 408 } |
| 268 | 409 |
| 269 return true; | 410 return true; |
| 270 } | 411 } |
| 271 | 412 |
| 272 bool SQLitePersistentCookieStore::Backend::EnsureDatabaseVersion() { | 413 bool SQLitePersistentCookieStore::Backend::EnsureDatabaseVersion() { |
| 273 // Version check. | 414 // Version check. |
| 274 if (!meta_table_.Init( | 415 if (!meta_table_.Init( |
| 275 db_.get(), kCurrentVersionNumber, kCompatibleVersionNumber)) { | 416 db_.get(), kCurrentVersionNumber, kCompatibleVersionNumber)) { |
| 276 return false; | 417 return false; |
| (...skipping 250 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 527 | 668 |
| 528 SQLitePersistentCookieStore::~SQLitePersistentCookieStore() { | 669 SQLitePersistentCookieStore::~SQLitePersistentCookieStore() { |
| 529 if (backend_.get()) { | 670 if (backend_.get()) { |
| 530 backend_->Close(); | 671 backend_->Close(); |
| 531 // Release our reference, it will probably still have a reference if the | 672 // Release our reference, it will probably still have a reference if the |
| 532 // background thread has not run Close() yet. | 673 // background thread has not run Close() yet. |
| 533 backend_ = NULL; | 674 backend_ = NULL; |
| 534 } | 675 } |
| 535 } | 676 } |
| 536 | 677 |
| 537 bool SQLitePersistentCookieStore::Load(const LoadedCallback& loaded_callback) { | 678 void SQLitePersistentCookieStore::Load(const LoadedCallback& loaded_callback) { |
| 538 return backend_->Load(loaded_callback); | 679 backend_->Load(loaded_callback); |
| 680 } | |
| 681 | |
| 682 void SQLitePersistentCookieStore::LoadCookiesForKey( | |
| 683 const std::string& key, | |
| 684 const LoadedCallback& loaded_callback) { | |
| 685 backend_->LoadCookiesForKey(key, loaded_callback); | |
| 539 } | 686 } |
| 540 | 687 |
| 541 void SQLitePersistentCookieStore::AddCookie( | 688 void SQLitePersistentCookieStore::AddCookie( |
| 542 const net::CookieMonster::CanonicalCookie& cc) { | 689 const net::CookieMonster::CanonicalCookie& cc) { |
| 543 if (backend_.get()) | 690 if (backend_.get()) |
| 544 backend_->AddCookie(cc); | 691 backend_->AddCookie(cc); |
| 545 } | 692 } |
| 546 | 693 |
| 547 void SQLitePersistentCookieStore::UpdateCookieAccessTime( | 694 void SQLitePersistentCookieStore::UpdateCookieAccessTime( |
| 548 const net::CookieMonster::CanonicalCookie& cc) { | 695 const net::CookieMonster::CanonicalCookie& cc) { |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 561 if (backend_.get()) | 708 if (backend_.get()) |
| 562 backend_->SetClearLocalStateOnExit(clear_local_state); | 709 backend_->SetClearLocalStateOnExit(clear_local_state); |
| 563 } | 710 } |
| 564 | 711 |
| 565 void SQLitePersistentCookieStore::Flush(Task* completion_task) { | 712 void SQLitePersistentCookieStore::Flush(Task* completion_task) { |
| 566 if (backend_.get()) | 713 if (backend_.get()) |
| 567 backend_->Flush(completion_task); | 714 backend_->Flush(completion_task); |
| 568 else if (completion_task) | 715 else if (completion_task) |
| 569 MessageLoop::current()->PostTask(FROM_HERE, completion_task); | 716 MessageLoop::current()->PostTask(FROM_HERE, completion_task); |
| 570 } | 717 } |
| OLD | NEW |