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