| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "content/browser/net/sqlite_persistent_cookie_store.h" | 5 #include "net/extras/sqlite/sqlite_persistent_cookie_store.h" |
| 6 | 6 |
| 7 #include <list> | 7 #include <list> |
| 8 #include <map> | 8 #include <map> |
| 9 #include <set> | 9 #include <set> |
| 10 #include <utility> | 10 #include <utility> |
| 11 | 11 |
| 12 #include "base/basictypes.h" | 12 #include "base/basictypes.h" |
| 13 #include "base/bind.h" | 13 #include "base/bind.h" |
| 14 #include "base/callback.h" | 14 #include "base/callback.h" |
| 15 #include "base/files/file_path.h" | 15 #include "base/files/file_path.h" |
| 16 #include "base/files/file_util.h" | 16 #include "base/files/file_util.h" |
| 17 #include "base/location.h" | 17 #include "base/location.h" |
| 18 #include "base/logging.h" | 18 #include "base/logging.h" |
| 19 #include "base/memory/ref_counted.h" | 19 #include "base/memory/ref_counted.h" |
| 20 #include "base/memory/scoped_ptr.h" | 20 #include "base/memory/scoped_ptr.h" |
| 21 #include "base/metrics/field_trial.h" | 21 #include "base/metrics/field_trial.h" |
| 22 #include "base/metrics/histogram.h" | 22 #include "base/metrics/histogram.h" |
| 23 #include "base/sequenced_task_runner.h" | 23 #include "base/sequenced_task_runner.h" |
| 24 #include "base/strings/string_util.h" | 24 #include "base/strings/string_util.h" |
| 25 #include "base/strings/stringprintf.h" | 25 #include "base/strings/stringprintf.h" |
| 26 #include "base/synchronization/lock.h" | 26 #include "base/synchronization/lock.h" |
| 27 #include "base/threading/sequenced_worker_pool.h" | 27 #include "base/threading/sequenced_worker_pool.h" |
| 28 #include "base/time/time.h" | 28 #include "base/time/time.h" |
| 29 #include "content/public/browser/browser_thread.h" | |
| 30 #include "content/public/browser/cookie_store_factory.h" | |
| 31 #include "net/base/registry_controlled_domains/registry_controlled_domain.h" | 29 #include "net/base/registry_controlled_domains/registry_controlled_domain.h" |
| 32 #include "net/cookies/canonical_cookie.h" | 30 #include "net/cookies/canonical_cookie.h" |
| 33 #include "net/cookies/cookie_constants.h" | 31 #include "net/cookies/cookie_constants.h" |
| 34 #include "net/cookies/cookie_util.h" | 32 #include "net/cookies/cookie_util.h" |
| 35 #include "net/extras/sqlite/cookie_crypto_delegate.h" | 33 #include "net/extras/sqlite/cookie_crypto_delegate.h" |
| 36 #include "sql/error_delegate_util.h" | 34 #include "sql/error_delegate_util.h" |
| 37 #include "sql/meta_table.h" | 35 #include "sql/meta_table.h" |
| 38 #include "sql/statement.h" | 36 #include "sql/statement.h" |
| 39 #include "sql/transaction.h" | 37 #include "sql/transaction.h" |
| 40 #include "storage/browser/quota/special_storage_policy.h" | |
| 41 #include "third_party/sqlite/sqlite3.h" | |
| 42 #include "url/gurl.h" | 38 #include "url/gurl.h" |
| 43 | 39 |
| 44 using base::Time; | 40 using base::Time; |
| 45 | 41 |
| 46 namespace { | 42 namespace { |
| 47 | 43 |
| 48 // The persistent cookie store is loaded into memory on eTLD at a time. This | 44 // The persistent cookie store is loaded into memory on eTLD at a time. This |
| 49 // variable controls the delay between loading eTLDs, so as to not overload the | 45 // variable controls the delay between loading eTLDs, so as to not overload the |
| 50 // CPU or I/O with these low priority requests immediately after start up. | 46 // CPU or I/O with these low priority requests immediately after start up. |
| 51 const int kLoadDelayMilliseconds = 200; | 47 const int kLoadDelayMilliseconds = 200; |
| 52 | 48 |
| 53 } // namespace | 49 } // namespace |
| 54 | 50 |
| 55 namespace content { | 51 namespace net { |
| 56 | 52 |
| 57 // This class is designed to be shared between any client thread and the | 53 // This class is designed to be shared between any client thread and the |
| 58 // background task runner. It batches operations and commits them on a timer. | 54 // background task runner. It batches operations and commits them on a timer. |
| 59 // | 55 // |
| 60 // SQLitePersistentCookieStore::Load is called to load all cookies. It | 56 // SQLitePersistentCookieStore::Load is called to load all cookies. It |
| 61 // delegates to Backend::Load, which posts a Backend::LoadAndNotifyOnDBThread | 57 // delegates to Backend::Load, which posts a Backend::LoadAndNotifyOnDBThread |
| 62 // task to the background runner. This task calls Backend::ChainLoadCookies(), | 58 // task to the background runner. This task calls Backend::ChainLoadCookies(), |
| 63 // which repeatedly posts itself to the BG runner to load each eTLD+1's cookies | 59 // which repeatedly posts itself to the BG runner to load each eTLD+1's cookies |
| 64 // in separate tasks. When this is complete, Backend::CompleteLoadOnIOThread is | 60 // in separate tasks. When this is complete, Backend::CompleteLoadOnIOThread is |
| 65 // posted to the client runner, which notifies the caller of | 61 // posted to the client runner, which notifies the caller of |
| (...skipping 11 matching lines...) Expand all Loading... |
| 77 // disk on the BG runner every 30 seconds, 512 operations, or call to Flush(), | 73 // disk on the BG runner every 30 seconds, 512 operations, or call to Flush(), |
| 78 // whichever occurs first. | 74 // whichever occurs first. |
| 79 class SQLitePersistentCookieStore::Backend | 75 class SQLitePersistentCookieStore::Backend |
| 80 : public base::RefCountedThreadSafe<SQLitePersistentCookieStore::Backend> { | 76 : public base::RefCountedThreadSafe<SQLitePersistentCookieStore::Backend> { |
| 81 public: | 77 public: |
| 82 Backend( | 78 Backend( |
| 83 const base::FilePath& path, | 79 const base::FilePath& path, |
| 84 const scoped_refptr<base::SequencedTaskRunner>& client_task_runner, | 80 const scoped_refptr<base::SequencedTaskRunner>& client_task_runner, |
| 85 const scoped_refptr<base::SequencedTaskRunner>& background_task_runner, | 81 const scoped_refptr<base::SequencedTaskRunner>& background_task_runner, |
| 86 bool restore_old_session_cookies, | 82 bool restore_old_session_cookies, |
| 87 storage::SpecialStoragePolicy* special_storage_policy, | 83 CookieCryptoDelegate* crypto_delegate) |
| 88 net::CookieCryptoDelegate* crypto_delegate) | |
| 89 : path_(path), | 84 : path_(path), |
| 90 num_pending_(0), | 85 num_pending_(0), |
| 91 force_keep_session_state_(false), | |
| 92 initialized_(false), | 86 initialized_(false), |
| 93 corruption_detected_(false), | 87 corruption_detected_(false), |
| 94 restore_old_session_cookies_(restore_old_session_cookies), | 88 restore_old_session_cookies_(restore_old_session_cookies), |
| 95 special_storage_policy_(special_storage_policy), | |
| 96 num_cookies_read_(0), | 89 num_cookies_read_(0), |
| 97 client_task_runner_(client_task_runner), | 90 client_task_runner_(client_task_runner), |
| 98 background_task_runner_(background_task_runner), | 91 background_task_runner_(background_task_runner), |
| 99 num_priority_waiting_(0), | 92 num_priority_waiting_(0), |
| 100 total_priority_requests_(0), | 93 total_priority_requests_(0), |
| 101 crypto_(crypto_delegate) {} | 94 crypto_(crypto_delegate) {} |
| 102 | 95 |
| 103 // Creates or loads the SQLite database. | 96 // Creates or loads the SQLite database. |
| 104 void Load(const LoadedCallback& loaded_callback); | 97 void Load(const LoadedCallback& loaded_callback); |
| 105 | 98 |
| 106 // Loads cookies for the domain key (eTLD+1). | 99 // Loads cookies for the domain key (eTLD+1). |
| 107 void LoadCookiesForKey(const std::string& domain, | 100 void LoadCookiesForKey(const std::string& domain, |
| 108 const LoadedCallback& loaded_callback); | 101 const LoadedCallback& loaded_callback); |
| 109 | 102 |
| 110 // Steps through all results of |smt|, makes a cookie from each, and adds the | 103 // Steps through all results of |smt|, makes a cookie from each, and adds the |
| 111 // cookie to |cookies|. This method also updates |cookies_per_origin_| and | 104 // cookie to |cookies|. This method also updates |num_cookies_read_|. |
| 112 // |num_cookies_read_|. | 105 void MakeCookiesFromSQLStatement(std::vector<CanonicalCookie*>* cookies, |
| 113 void MakeCookiesFromSQLStatement(std::vector<net::CanonicalCookie*>* cookies, | |
| 114 sql::Statement* statement); | 106 sql::Statement* statement); |
| 115 | 107 |
| 116 // Batch a cookie addition. | 108 // Batch a cookie addition. |
| 117 void AddCookie(const net::CanonicalCookie& cc); | 109 void AddCookie(const CanonicalCookie& cc); |
| 118 | 110 |
| 119 // Batch a cookie access time update. | 111 // Batch a cookie access time update. |
| 120 void UpdateCookieAccessTime(const net::CanonicalCookie& cc); | 112 void UpdateCookieAccessTime(const CanonicalCookie& cc); |
| 121 | 113 |
| 122 // Batch a cookie deletion. | 114 // Batch a cookie deletion. |
| 123 void DeleteCookie(const net::CanonicalCookie& cc); | 115 void DeleteCookie(const CanonicalCookie& cc); |
| 124 | 116 |
| 125 // Commit pending operations as soon as possible. | 117 // Commit pending operations as soon as possible. |
| 126 void Flush(const base::Closure& callback); | 118 void Flush(const base::Closure& callback); |
| 127 | 119 |
| 128 // Commit any pending operations and close the database. This must be called | 120 // Commit any pending operations and close the database. This must be called |
| 129 // before the object is destructed. | 121 // before the object is destructed. |
| 130 void Close(); | 122 void Close(); |
| 131 | 123 |
| 132 void SetForceKeepSessionState(); | 124 // Post background delete of all cookies that match |cookies|. |
| 125 void DeleteAllInList(const std::list<CookieOrigin>& cookies); |
| 133 | 126 |
| 134 private: | 127 private: |
| 135 friend class base::RefCountedThreadSafe<SQLitePersistentCookieStore::Backend>; | 128 friend class base::RefCountedThreadSafe<SQLitePersistentCookieStore::Backend>; |
| 136 | 129 |
| 137 // You should call Close() before destructing this object. | 130 // You should call Close() before destructing this object. |
| 138 ~Backend() { | 131 ~Backend() { |
| 139 DCHECK(!db_.get()) << "Close should have already been called."; | 132 DCHECK(!db_.get()) << "Close should have already been called."; |
| 140 DCHECK(num_pending_ == 0 && pending_.empty()); | 133 DCHECK_EQ(0u, num_pending_); |
| 134 DCHECK(pending_.empty()); |
| 141 } | 135 } |
| 142 | 136 |
| 143 // Database upgrade statements. | 137 // Database upgrade statements. |
| 144 bool EnsureDatabaseVersion(); | 138 bool EnsureDatabaseVersion(); |
| 145 | 139 |
| 146 class PendingOperation { | 140 class PendingOperation { |
| 147 public: | 141 public: |
| 148 typedef enum { | 142 enum OperationType { |
| 149 COOKIE_ADD, | 143 COOKIE_ADD, |
| 150 COOKIE_UPDATEACCESS, | 144 COOKIE_UPDATEACCESS, |
| 151 COOKIE_DELETE, | 145 COOKIE_DELETE, |
| 152 } OperationType; | 146 }; |
| 153 | 147 |
| 154 PendingOperation(OperationType op, const net::CanonicalCookie& cc) | 148 PendingOperation(OperationType op, const CanonicalCookie& cc) |
| 155 : op_(op), cc_(cc) { } | 149 : op_(op), cc_(cc) { } |
| 156 | 150 |
| 157 OperationType op() const { return op_; } | 151 OperationType op() const { return op_; } |
| 158 const net::CanonicalCookie& cc() const { return cc_; } | 152 const CanonicalCookie& cc() const { return cc_; } |
| 159 | 153 |
| 160 private: | 154 private: |
| 161 OperationType op_; | 155 OperationType op_; |
| 162 net::CanonicalCookie cc_; | 156 CanonicalCookie cc_; |
| 163 }; | 157 }; |
| 164 | 158 |
| 165 private: | 159 private: |
| 166 // Creates or loads the SQLite database on background runner. | 160 // Creates or loads the SQLite database on background runner. |
| 167 void LoadAndNotifyInBackground(const LoadedCallback& loaded_callback, | 161 void LoadAndNotifyInBackground(const LoadedCallback& loaded_callback, |
| 168 const base::Time& posted_at); | 162 const base::Time& posted_at); |
| 169 | 163 |
| 170 // Loads cookies for the domain key (eTLD+1) on background runner. | 164 // Loads cookies for the domain key (eTLD+1) on background runner. |
| 171 void LoadKeyAndNotifyInBackground(const std::string& domains, | 165 void LoadKeyAndNotifyInBackground(const std::string& domains, |
| 172 const LoadedCallback& loaded_callback, | 166 const LoadedCallback& loaded_callback, |
| (...skipping 30 matching lines...) Expand all Loading... |
| 203 // Loads cookies for the next domain key from the DB, then either reschedules | 197 // Loads cookies for the next domain key from the DB, then either reschedules |
| 204 // itself or schedules the provided callback to run on the client runner (if | 198 // itself or schedules the provided callback to run on the client runner (if |
| 205 // all domains are loaded). | 199 // all domains are loaded). |
| 206 void ChainLoadCookies(const LoadedCallback& loaded_callback); | 200 void ChainLoadCookies(const LoadedCallback& loaded_callback); |
| 207 | 201 |
| 208 // Load all cookies for a set of domains/hosts | 202 // Load all cookies for a set of domains/hosts |
| 209 bool LoadCookiesForDomains(const std::set<std::string>& key); | 203 bool LoadCookiesForDomains(const std::set<std::string>& key); |
| 210 | 204 |
| 211 // Batch a cookie operation (add or delete) | 205 // Batch a cookie operation (add or delete) |
| 212 void BatchOperation(PendingOperation::OperationType op, | 206 void BatchOperation(PendingOperation::OperationType op, |
| 213 const net::CanonicalCookie& cc); | 207 const CanonicalCookie& cc); |
| 214 // Commit our pending operations to the database. | 208 // Commit our pending operations to the database. |
| 215 void Commit(); | 209 void Commit(); |
| 216 // Close() executed on the background runner. | 210 // Close() executed on the background runner. |
| 217 void InternalBackgroundClose(); | 211 void InternalBackgroundClose(); |
| 218 | 212 |
| 219 void DeleteSessionCookiesOnStartup(); | 213 void DeleteSessionCookiesOnStartup(); |
| 220 | 214 |
| 221 void DeleteSessionCookiesOnShutdown(); | 215 void BackgroundDeleteAllInList(const std::list<CookieOrigin>& cookies); |
| 222 | 216 |
| 223 void DatabaseErrorCallback(int error, sql::Statement* stmt); | 217 void DatabaseErrorCallback(int error, sql::Statement* stmt); |
| 224 void KillDatabase(); | 218 void KillDatabase(); |
| 225 | 219 |
| 226 void PostBackgroundTask(const tracked_objects::Location& origin, | 220 void PostBackgroundTask(const tracked_objects::Location& origin, |
| 227 const base::Closure& task); | 221 const base::Closure& task); |
| 228 void PostClientTask(const tracked_objects::Location& origin, | 222 void PostClientTask(const tracked_objects::Location& origin, |
| 229 const base::Closure& task); | 223 const base::Closure& task); |
| 230 | 224 |
| 231 // Shared code between the different load strategies to be used after all | 225 // Shared code between the different load strategies to be used after all |
| 232 // cookies have been loaded. | 226 // cookies have been loaded. |
| 233 void FinishedLoadingCookies(const LoadedCallback& loaded_callback, | 227 void FinishedLoadingCookies(const LoadedCallback& loaded_callback, |
| 234 bool success); | 228 bool success); |
| 235 | 229 |
| 236 base::FilePath path_; | 230 const base::FilePath path_; |
| 237 scoped_ptr<sql::Connection> db_; | 231 scoped_ptr<sql::Connection> db_; |
| 238 sql::MetaTable meta_table_; | 232 sql::MetaTable meta_table_; |
| 239 | 233 |
| 240 typedef std::list<PendingOperation*> PendingOperationsList; | 234 typedef std::list<PendingOperation*> PendingOperationsList; |
| 241 PendingOperationsList pending_; | 235 PendingOperationsList pending_; |
| 242 PendingOperationsList::size_type num_pending_; | 236 PendingOperationsList::size_type num_pending_; |
| 243 // True if the persistent store should skip delete on exit rules. | 237 // Guard |cookies_|, |pending_|, |num_pending_|. |
| 244 bool force_keep_session_state_; | |
| 245 // Guard |cookies_|, |pending_|, |num_pending_|, |force_keep_session_state_| | |
| 246 base::Lock lock_; | 238 base::Lock lock_; |
| 247 | 239 |
| 248 // Temporary buffer for cookies loaded from DB. Accumulates cookies to reduce | 240 // Temporary buffer for cookies loaded from DB. Accumulates cookies to reduce |
| 249 // the number of messages sent to the client runner. Sent back in response to | 241 // the number of messages sent to the client runner. Sent back in response to |
| 250 // individual load requests for domain keys or when all loading completes. | 242 // individual load requests for domain keys or when all loading completes. |
| 251 std::vector<net::CanonicalCookie*> cookies_; | 243 std::vector<CanonicalCookie*> cookies_; |
| 252 | 244 |
| 253 // Map of domain keys(eTLD+1) to domains/hosts that are to be loaded from DB. | 245 // Map of domain keys(eTLD+1) to domains/hosts that are to be loaded from DB. |
| 254 std::map<std::string, std::set<std::string> > keys_to_load_; | 246 std::map<std::string, std::set<std::string> > keys_to_load_; |
| 255 | 247 |
| 256 // Map of (domain keys(eTLD+1), is secure cookie) to number of cookies in the | |
| 257 // database. | |
| 258 typedef std::pair<std::string, bool> CookieOrigin; | |
| 259 typedef std::map<CookieOrigin, int> CookiesPerOriginMap; | |
| 260 CookiesPerOriginMap cookies_per_origin_; | |
| 261 | |
| 262 // Indicates if DB has been initialized. | 248 // Indicates if DB has been initialized. |
| 263 bool initialized_; | 249 bool initialized_; |
| 264 | 250 |
| 265 // Indicates if the kill-database callback has been scheduled. | 251 // Indicates if the kill-database callback has been scheduled. |
| 266 bool corruption_detected_; | 252 bool corruption_detected_; |
| 267 | 253 |
| 268 // If false, we should filter out session cookies when reading the DB. | 254 // If false, we should filter out session cookies when reading the DB. |
| 269 bool restore_old_session_cookies_; | 255 bool restore_old_session_cookies_; |
| 270 | 256 |
| 271 // Policy defining what data is deleted on shutdown. | |
| 272 scoped_refptr<storage::SpecialStoragePolicy> special_storage_policy_; | |
| 273 | |
| 274 // The cumulative time spent loading the cookies on the background runner. | 257 // The cumulative time spent loading the cookies on the background runner. |
| 275 // Incremented and reported from the background runner. | 258 // Incremented and reported from the background runner. |
| 276 base::TimeDelta cookie_load_duration_; | 259 base::TimeDelta cookie_load_duration_; |
| 277 | 260 |
| 278 // The total number of cookies read. Incremented and reported on the | 261 // The total number of cookies read. Incremented and reported on the |
| 279 // background runner. | 262 // background runner. |
| 280 int num_cookies_read_; | 263 int num_cookies_read_; |
| 281 | 264 |
| 282 scoped_refptr<base::SequencedTaskRunner> client_task_runner_; | 265 scoped_refptr<base::SequencedTaskRunner> client_task_runner_; |
| 283 scoped_refptr<base::SequencedTaskRunner> background_task_runner_; | 266 scoped_refptr<base::SequencedTaskRunner> background_task_runner_; |
| 284 | 267 |
| 285 // Guards the following metrics-related properties (only accessed when | 268 // Guards the following metrics-related properties (only accessed when |
| 286 // starting/completing priority loads or completing the total load). | 269 // starting/completing priority loads or completing the total load). |
| 287 base::Lock metrics_lock_; | 270 base::Lock metrics_lock_; |
| 288 int num_priority_waiting_; | 271 int num_priority_waiting_; |
| 289 // The total number of priority requests. | 272 // The total number of priority requests. |
| 290 int total_priority_requests_; | 273 int total_priority_requests_; |
| 291 // The time when |num_priority_waiting_| incremented to 1. | 274 // The time when |num_priority_waiting_| incremented to 1. |
| 292 base::Time current_priority_wait_start_; | 275 base::Time current_priority_wait_start_; |
| 293 // The cumulative duration of time when |num_priority_waiting_| was greater | 276 // The cumulative duration of time when |num_priority_waiting_| was greater |
| 294 // than 1. | 277 // than 1. |
| 295 base::TimeDelta priority_wait_duration_; | 278 base::TimeDelta priority_wait_duration_; |
| 296 // Class with functions that do cryptographic operations (for protecting | 279 // Class with functions that do cryptographic operations (for protecting |
| 297 // cookies stored persistently). | 280 // cookies stored persistently). |
| 298 // | 281 // |
| 299 // Not owned. | 282 // Not owned. |
| 300 net::CookieCryptoDelegate* crypto_; | 283 CookieCryptoDelegate* crypto_; |
| 301 | 284 |
| 302 DISALLOW_COPY_AND_ASSIGN(Backend); | 285 DISALLOW_COPY_AND_ASSIGN(Backend); |
| 303 }; | 286 }; |
| 304 | 287 |
| 305 namespace { | 288 namespace { |
| 306 | 289 |
| 307 // Version number of the database. | 290 // Version number of the database. |
| 308 // | 291 // |
| 309 // Version 8 adds "first-party only" cookies. | 292 // Version 8 adds "first-party only" cookies. |
| 310 // | 293 // |
| (...skipping 23 matching lines...) Expand all Loading... |
| 334 const int kCurrentVersionNumber = 8; | 317 const int kCurrentVersionNumber = 8; |
| 335 const int kCompatibleVersionNumber = 5; | 318 const int kCompatibleVersionNumber = 5; |
| 336 | 319 |
| 337 // Possible values for the 'priority' column. | 320 // Possible values for the 'priority' column. |
| 338 enum DBCookiePriority { | 321 enum DBCookiePriority { |
| 339 kCookiePriorityLow = 0, | 322 kCookiePriorityLow = 0, |
| 340 kCookiePriorityMedium = 1, | 323 kCookiePriorityMedium = 1, |
| 341 kCookiePriorityHigh = 2, | 324 kCookiePriorityHigh = 2, |
| 342 }; | 325 }; |
| 343 | 326 |
| 344 DBCookiePriority CookiePriorityToDBCookiePriority(net::CookiePriority value) { | 327 DBCookiePriority CookiePriorityToDBCookiePriority(CookiePriority value) { |
| 345 switch (value) { | 328 switch (value) { |
| 346 case net::COOKIE_PRIORITY_LOW: | 329 case COOKIE_PRIORITY_LOW: |
| 347 return kCookiePriorityLow; | 330 return kCookiePriorityLow; |
| 348 case net::COOKIE_PRIORITY_MEDIUM: | 331 case COOKIE_PRIORITY_MEDIUM: |
| 349 return kCookiePriorityMedium; | 332 return kCookiePriorityMedium; |
| 350 case net::COOKIE_PRIORITY_HIGH: | 333 case COOKIE_PRIORITY_HIGH: |
| 351 return kCookiePriorityHigh; | 334 return kCookiePriorityHigh; |
| 352 } | 335 } |
| 353 | 336 |
| 354 NOTREACHED(); | 337 NOTREACHED(); |
| 355 return kCookiePriorityMedium; | 338 return kCookiePriorityMedium; |
| 356 } | 339 } |
| 357 | 340 |
| 358 net::CookiePriority DBCookiePriorityToCookiePriority(DBCookiePriority value) { | 341 CookiePriority DBCookiePriorityToCookiePriority(DBCookiePriority value) { |
| 359 switch (value) { | 342 switch (value) { |
| 360 case kCookiePriorityLow: | 343 case kCookiePriorityLow: |
| 361 return net::COOKIE_PRIORITY_LOW; | 344 return COOKIE_PRIORITY_LOW; |
| 362 case kCookiePriorityMedium: | 345 case kCookiePriorityMedium: |
| 363 return net::COOKIE_PRIORITY_MEDIUM; | 346 return COOKIE_PRIORITY_MEDIUM; |
| 364 case kCookiePriorityHigh: | 347 case kCookiePriorityHigh: |
| 365 return net::COOKIE_PRIORITY_HIGH; | 348 return COOKIE_PRIORITY_HIGH; |
| 366 } | 349 } |
| 367 | 350 |
| 368 NOTREACHED(); | 351 NOTREACHED(); |
| 369 return net::COOKIE_PRIORITY_DEFAULT; | 352 return COOKIE_PRIORITY_DEFAULT; |
| 370 } | 353 } |
| 371 | 354 |
| 372 // Increments a specified TimeDelta by the duration between this object's | 355 // Increments a specified TimeDelta by the duration between this object's |
| 373 // constructor and destructor. Not thread safe. Multiple instances may be | 356 // constructor and destructor. Not thread safe. Multiple instances may be |
| 374 // created with the same delta instance as long as their lifetimes are nested. | 357 // created with the same delta instance as long as their lifetimes are nested. |
| 375 // The shortest lived instances have no impact. | 358 // The shortest lived instances have no impact. |
| 376 class IncrementTimeDelta { | 359 class IncrementTimeDelta { |
| 377 public: | 360 public: |
| 378 explicit IncrementTimeDelta(base::TimeDelta* delta) : | 361 explicit IncrementTimeDelta(base::TimeDelta* delta) : |
| 379 delta_(delta), | 362 delta_(delta), |
| (...skipping 24 matching lines...) Expand all Loading... |
| 404 "path TEXT NOT NULL," | 387 "path TEXT NOT NULL," |
| 405 "expires_utc INTEGER NOT NULL," | 388 "expires_utc INTEGER NOT NULL," |
| 406 "secure INTEGER NOT NULL," | 389 "secure INTEGER NOT NULL," |
| 407 "httponly INTEGER NOT NULL," | 390 "httponly INTEGER NOT NULL," |
| 408 "last_access_utc INTEGER NOT NULL, " | 391 "last_access_utc INTEGER NOT NULL, " |
| 409 "has_expires INTEGER NOT NULL DEFAULT 1, " | 392 "has_expires INTEGER NOT NULL DEFAULT 1, " |
| 410 "persistent INTEGER NOT NULL DEFAULT 1," | 393 "persistent INTEGER NOT NULL DEFAULT 1," |
| 411 "priority INTEGER NOT NULL DEFAULT %d," | 394 "priority INTEGER NOT NULL DEFAULT %d," |
| 412 "encrypted_value BLOB DEFAULT ''," | 395 "encrypted_value BLOB DEFAULT ''," |
| 413 "firstpartyonly INTEGER NOT NULL DEFAULT 0)", | 396 "firstpartyonly INTEGER NOT NULL DEFAULT 0)", |
| 414 CookiePriorityToDBCookiePriority(net::COOKIE_PRIORITY_DEFAULT))); | 397 CookiePriorityToDBCookiePriority(COOKIE_PRIORITY_DEFAULT))); |
| 415 if (!db->Execute(stmt.c_str())) | 398 if (!db->Execute(stmt.c_str())) |
| 416 return false; | 399 return false; |
| 417 } | 400 } |
| 418 | 401 |
| 419 // Older code created an index on creation_utc, which is already | 402 // Older code created an index on creation_utc, which is already |
| 420 // primary key for the table. | 403 // primary key for the table. |
| 421 if (!db->Execute("DROP INDEX IF EXISTS cookie_times")) | 404 if (!db->Execute("DROP INDEX IF EXISTS cookie_times")) |
| 422 return false; | 405 return false; |
| 423 | 406 |
| 424 if (!db->Execute("CREATE INDEX IF NOT EXISTS domain ON cookies(host_key)")) | 407 if (!db->Execute("CREATE INDEX IF NOT EXISTS domain ON cookies(host_key)")) |
| (...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 564 | 547 |
| 565 if (load_success) | 548 if (load_success) |
| 566 ReportMetrics(); | 549 ReportMetrics(); |
| 567 } | 550 } |
| 568 | 551 |
| 569 void SQLitePersistentCookieStore::Backend::Notify( | 552 void SQLitePersistentCookieStore::Backend::Notify( |
| 570 const LoadedCallback& loaded_callback, | 553 const LoadedCallback& loaded_callback, |
| 571 bool load_success) { | 554 bool load_success) { |
| 572 DCHECK(client_task_runner_->RunsTasksOnCurrentThread()); | 555 DCHECK(client_task_runner_->RunsTasksOnCurrentThread()); |
| 573 | 556 |
| 574 std::vector<net::CanonicalCookie*> cookies; | 557 std::vector<CanonicalCookie*> cookies; |
| 575 { | 558 { |
| 576 base::AutoLock locked(lock_); | 559 base::AutoLock locked(lock_); |
| 577 cookies.swap(cookies_); | 560 cookies.swap(cookies_); |
| 578 } | 561 } |
| 579 | 562 |
| 580 loaded_callback.Run(cookies); | 563 loaded_callback.Run(cookies); |
| 581 } | 564 } |
| 582 | 565 |
| 583 bool SQLitePersistentCookieStore::Backend::InitializeDatabase() { | 566 bool SQLitePersistentCookieStore::Backend::InitializeDatabase() { |
| 584 DCHECK(background_task_runner_->RunsTasksOnCurrentThread()); | 567 DCHECK(background_task_runner_->RunsTasksOnCurrentThread()); |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 655 base::Time::Now() - start, | 638 base::Time::Now() - start, |
| 656 base::TimeDelta::FromMilliseconds(1), base::TimeDelta::FromMinutes(1), | 639 base::TimeDelta::FromMilliseconds(1), base::TimeDelta::FromMinutes(1), |
| 657 50); | 640 50); |
| 658 | 641 |
| 659 base::Time start_parse = base::Time::Now(); | 642 base::Time start_parse = base::Time::Now(); |
| 660 | 643 |
| 661 // Build a map of domain keys (always eTLD+1) to domains. | 644 // Build a map of domain keys (always eTLD+1) to domains. |
| 662 for (size_t idx = 0; idx < host_keys.size(); ++idx) { | 645 for (size_t idx = 0; idx < host_keys.size(); ++idx) { |
| 663 const std::string& domain = host_keys[idx]; | 646 const std::string& domain = host_keys[idx]; |
| 664 std::string key = | 647 std::string key = |
| 665 net::registry_controlled_domains::GetDomainAndRegistry( | 648 registry_controlled_domains::GetDomainAndRegistry( |
| 666 domain, | 649 domain, |
| 667 net::registry_controlled_domains::INCLUDE_PRIVATE_REGISTRIES); | 650 registry_controlled_domains::INCLUDE_PRIVATE_REGISTRIES); |
| 668 | 651 |
| 669 keys_to_load_[key].insert(domain); | 652 keys_to_load_[key].insert(domain); |
| 670 } | 653 } |
| 671 | 654 |
| 672 UMA_HISTOGRAM_CUSTOM_TIMES( | 655 UMA_HISTOGRAM_CUSTOM_TIMES( |
| 673 "Cookie.TimeParseDomains", | 656 "Cookie.TimeParseDomains", |
| 674 base::Time::Now() - start_parse, | 657 base::Time::Now() - start_parse, |
| 675 base::TimeDelta::FromMilliseconds(1), base::TimeDelta::FromMinutes(1), | 658 base::TimeDelta::FromMilliseconds(1), base::TimeDelta::FromMinutes(1), |
| 676 50); | 659 50); |
| 677 | 660 |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 739 "has_expires, persistent, priority FROM cookies WHERE host_key = ? " | 722 "has_expires, persistent, priority FROM cookies WHERE host_key = ? " |
| 740 "AND persistent = 1")); | 723 "AND persistent = 1")); |
| 741 } | 724 } |
| 742 if (!smt.is_valid()) { | 725 if (!smt.is_valid()) { |
| 743 smt.Clear(); // Disconnect smt_ref from db_. | 726 smt.Clear(); // Disconnect smt_ref from db_. |
| 744 meta_table_.Reset(); | 727 meta_table_.Reset(); |
| 745 db_.reset(); | 728 db_.reset(); |
| 746 return false; | 729 return false; |
| 747 } | 730 } |
| 748 | 731 |
| 749 std::vector<net::CanonicalCookie*> cookies; | 732 std::vector<CanonicalCookie*> cookies; |
| 750 std::set<std::string>::const_iterator it = domains.begin(); | 733 std::set<std::string>::const_iterator it = domains.begin(); |
| 751 for (; it != domains.end(); ++it) { | 734 for (; it != domains.end(); ++it) { |
| 752 smt.BindString(0, *it); | 735 smt.BindString(0, *it); |
| 753 MakeCookiesFromSQLStatement(&cookies, &smt); | 736 MakeCookiesFromSQLStatement(&cookies, &smt); |
| 754 smt.Reset(true); | 737 smt.Reset(true); |
| 755 } | 738 } |
| 756 { | 739 { |
| 757 base::AutoLock locked(lock_); | 740 base::AutoLock locked(lock_); |
| 758 cookies_.insert(cookies_.end(), cookies.begin(), cookies.end()); | 741 cookies_.insert(cookies_.end(), cookies.begin(), cookies.end()); |
| 759 } | 742 } |
| 760 return true; | 743 return true; |
| 761 } | 744 } |
| 762 | 745 |
| 763 void SQLitePersistentCookieStore::Backend::MakeCookiesFromSQLStatement( | 746 void SQLitePersistentCookieStore::Backend::MakeCookiesFromSQLStatement( |
| 764 std::vector<net::CanonicalCookie*>* cookies, | 747 std::vector<CanonicalCookie*>* cookies, |
| 765 sql::Statement* statement) { | 748 sql::Statement* statement) { |
| 766 sql::Statement& smt = *statement; | 749 sql::Statement& smt = *statement; |
| 767 while (smt.Step()) { | 750 while (smt.Step()) { |
| 768 std::string value; | 751 std::string value; |
| 769 std::string encrypted_value = smt.ColumnString(4); | 752 std::string encrypted_value = smt.ColumnString(4); |
| 770 if (!encrypted_value.empty() && crypto_) { | 753 if (!encrypted_value.empty() && crypto_) { |
| 771 crypto_->DecryptString(encrypted_value, &value); | 754 crypto_->DecryptString(encrypted_value, &value); |
| 772 } else { | 755 } else { |
| 773 DCHECK(encrypted_value.empty()); | 756 DCHECK(encrypted_value.empty()); |
| 774 value = smt.ColumnString(3); | 757 value = smt.ColumnString(3); |
| 775 } | 758 } |
| 776 scoped_ptr<net::CanonicalCookie> cc(new net::CanonicalCookie( | 759 scoped_ptr<CanonicalCookie> cc(new CanonicalCookie( |
| 777 // The "source" URL is not used with persisted cookies. | 760 // The "source" URL is not used with persisted cookies. |
| 778 GURL(), // Source | 761 GURL(), // Source |
| 779 smt.ColumnString(2), // name | 762 smt.ColumnString(2), // name |
| 780 value, // value | 763 value, // value |
| 781 smt.ColumnString(1), // domain | 764 smt.ColumnString(1), // domain |
| 782 smt.ColumnString(5), // path | 765 smt.ColumnString(5), // path |
| 783 Time::FromInternalValue(smt.ColumnInt64(0)), // creation_utc | 766 Time::FromInternalValue(smt.ColumnInt64(0)), // creation_utc |
| 784 Time::FromInternalValue(smt.ColumnInt64(6)), // expires_utc | 767 Time::FromInternalValue(smt.ColumnInt64(6)), // expires_utc |
| 785 Time::FromInternalValue(smt.ColumnInt64(10)), // last_access_utc | 768 Time::FromInternalValue(smt.ColumnInt64(10)), // last_access_utc |
| 786 smt.ColumnInt(7) != 0, // secure | 769 smt.ColumnInt(7) != 0, // secure |
| 787 smt.ColumnInt(8) != 0, // httponly | 770 smt.ColumnInt(8) != 0, // httponly |
| 788 smt.ColumnInt(9) != 0, // firstpartyonly | 771 smt.ColumnInt(9) != 0, // firstpartyonly |
| 789 DBCookiePriorityToCookiePriority( | 772 DBCookiePriorityToCookiePriority( |
| 790 static_cast<DBCookiePriority>(smt.ColumnInt(13))))); // priority | 773 static_cast<DBCookiePriority>(smt.ColumnInt(13))))); // priority |
| 791 DLOG_IF(WARNING, cc->CreationDate() > Time::Now()) | 774 DLOG_IF(WARNING, cc->CreationDate() > Time::Now()) |
| 792 << L"CreationDate too recent"; | 775 << L"CreationDate too recent"; |
| 793 cookies_per_origin_[CookieOrigin(cc->Domain(), cc->IsSecure())]++; | |
| 794 cookies->push_back(cc.release()); | 776 cookies->push_back(cc.release()); |
| 795 ++num_cookies_read_; | 777 ++num_cookies_read_; |
| 796 } | 778 } |
| 797 } | 779 } |
| 798 | 780 |
| 799 bool SQLitePersistentCookieStore::Backend::EnsureDatabaseVersion() { | 781 bool SQLitePersistentCookieStore::Backend::EnsureDatabaseVersion() { |
| 800 // Version check. | 782 // Version check. |
| 801 if (!meta_table_.Init( | 783 if (!meta_table_.Init( |
| 802 db_.get(), kCurrentVersionNumber, kCompatibleVersionNumber)) { | 784 db_.get(), kCurrentVersionNumber, kCompatibleVersionNumber)) { |
| 803 return false; | 785 return false; |
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 882 } | 864 } |
| 883 | 865 |
| 884 if (cur_version == 5) { | 866 if (cur_version == 5) { |
| 885 const base::TimeTicks start_time = base::TimeTicks::Now(); | 867 const base::TimeTicks start_time = base::TimeTicks::Now(); |
| 886 sql::Transaction transaction(db_.get()); | 868 sql::Transaction transaction(db_.get()); |
| 887 if (!transaction.Begin()) | 869 if (!transaction.Begin()) |
| 888 return false; | 870 return false; |
| 889 // Alter the table to add the priority column with a default value. | 871 // Alter the table to add the priority column with a default value. |
| 890 std::string stmt(base::StringPrintf( | 872 std::string stmt(base::StringPrintf( |
| 891 "ALTER TABLE cookies ADD COLUMN priority INTEGER DEFAULT %d", | 873 "ALTER TABLE cookies ADD COLUMN priority INTEGER DEFAULT %d", |
| 892 CookiePriorityToDBCookiePriority(net::COOKIE_PRIORITY_DEFAULT))); | 874 CookiePriorityToDBCookiePriority(COOKIE_PRIORITY_DEFAULT))); |
| 893 if (!db_->Execute(stmt.c_str())) { | 875 if (!db_->Execute(stmt.c_str())) { |
| 894 LOG(WARNING) << "Unable to update cookie database to version 6."; | 876 LOG(WARNING) << "Unable to update cookie database to version 6."; |
| 895 return false; | 877 return false; |
| 896 } | 878 } |
| 897 ++cur_version; | 879 ++cur_version; |
| 898 meta_table_.SetVersionNumber(cur_version); | 880 meta_table_.SetVersionNumber(cur_version); |
| 899 meta_table_.SetCompatibleVersionNumber( | 881 meta_table_.SetCompatibleVersionNumber( |
| 900 std::min(cur_version, kCompatibleVersionNumber)); | 882 std::min(cur_version, kCompatibleVersionNumber)); |
| 901 transaction.Commit(); | 883 transaction.Commit(); |
| 902 UMA_HISTOGRAM_TIMES("Cookie.TimeDatabaseMigrationToV6", | 884 UMA_HISTOGRAM_TIMES("Cookie.TimeDatabaseMigrationToV6", |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 960 meta_table_.Reset(); | 942 meta_table_.Reset(); |
| 961 db_.reset(); | 943 db_.reset(); |
| 962 return false; | 944 return false; |
| 963 } | 945 } |
| 964 } | 946 } |
| 965 | 947 |
| 966 return true; | 948 return true; |
| 967 } | 949 } |
| 968 | 950 |
| 969 void SQLitePersistentCookieStore::Backend::AddCookie( | 951 void SQLitePersistentCookieStore::Backend::AddCookie( |
| 970 const net::CanonicalCookie& cc) { | 952 const CanonicalCookie& cc) { |
| 971 BatchOperation(PendingOperation::COOKIE_ADD, cc); | 953 BatchOperation(PendingOperation::COOKIE_ADD, cc); |
| 972 } | 954 } |
| 973 | 955 |
| 974 void SQLitePersistentCookieStore::Backend::UpdateCookieAccessTime( | 956 void SQLitePersistentCookieStore::Backend::UpdateCookieAccessTime( |
| 975 const net::CanonicalCookie& cc) { | 957 const CanonicalCookie& cc) { |
| 976 BatchOperation(PendingOperation::COOKIE_UPDATEACCESS, cc); | 958 BatchOperation(PendingOperation::COOKIE_UPDATEACCESS, cc); |
| 977 } | 959 } |
| 978 | 960 |
| 979 void SQLitePersistentCookieStore::Backend::DeleteCookie( | 961 void SQLitePersistentCookieStore::Backend::DeleteCookie( |
| 980 const net::CanonicalCookie& cc) { | 962 const CanonicalCookie& cc) { |
| 981 BatchOperation(PendingOperation::COOKIE_DELETE, cc); | 963 BatchOperation(PendingOperation::COOKIE_DELETE, cc); |
| 982 } | 964 } |
| 983 | 965 |
| 984 void SQLitePersistentCookieStore::Backend::BatchOperation( | 966 void SQLitePersistentCookieStore::Backend::BatchOperation( |
| 985 PendingOperation::OperationType op, | 967 PendingOperation::OperationType op, |
| 986 const net::CanonicalCookie& cc) { | 968 const CanonicalCookie& cc) { |
| 987 // Commit every 30 seconds. | 969 // Commit every 30 seconds. |
| 988 static const int kCommitIntervalMs = 30 * 1000; | 970 static const int kCommitIntervalMs = 30 * 1000; |
| 989 // Commit right away if we have more than 512 outstanding operations. | 971 // Commit right away if we have more than 512 outstanding operations. |
| 990 static const size_t kCommitAfterBatchSize = 512; | 972 static const size_t kCommitAfterBatchSize = 512; |
| 991 DCHECK(!background_task_runner_->RunsTasksOnCurrentThread()); | 973 DCHECK(!background_task_runner_->RunsTasksOnCurrentThread()); |
| 992 | 974 |
| 993 // We do a full copy of the cookie here, and hopefully just here. | 975 // We do a full copy of the cookie here, and hopefully just here. |
| 994 scoped_ptr<PendingOperation> po(new PendingOperation(op, cc)); | 976 scoped_ptr<PendingOperation> po(new PendingOperation(op, cc)); |
| 995 | 977 |
| 996 PendingOperationsList::size_type num_pending; | 978 PendingOperationsList::size_type num_pending; |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1049 sql::Transaction transaction(db_.get()); | 1031 sql::Transaction transaction(db_.get()); |
| 1050 if (!transaction.Begin()) | 1032 if (!transaction.Begin()) |
| 1051 return; | 1033 return; |
| 1052 | 1034 |
| 1053 for (PendingOperationsList::iterator it = ops.begin(); | 1035 for (PendingOperationsList::iterator it = ops.begin(); |
| 1054 it != ops.end(); ++it) { | 1036 it != ops.end(); ++it) { |
| 1055 // Free the cookies as we commit them to the database. | 1037 // Free the cookies as we commit them to the database. |
| 1056 scoped_ptr<PendingOperation> po(*it); | 1038 scoped_ptr<PendingOperation> po(*it); |
| 1057 switch (po->op()) { | 1039 switch (po->op()) { |
| 1058 case PendingOperation::COOKIE_ADD: | 1040 case PendingOperation::COOKIE_ADD: |
| 1059 cookies_per_origin_[ | |
| 1060 CookieOrigin(po->cc().Domain(), po->cc().IsSecure())]++; | |
| 1061 add_smt.Reset(true); | 1041 add_smt.Reset(true); |
| 1062 add_smt.BindInt64(0, po->cc().CreationDate().ToInternalValue()); | 1042 add_smt.BindInt64(0, po->cc().CreationDate().ToInternalValue()); |
| 1063 add_smt.BindString(1, po->cc().Domain()); | 1043 add_smt.BindString(1, po->cc().Domain()); |
| 1064 add_smt.BindString(2, po->cc().Name()); | 1044 add_smt.BindString(2, po->cc().Name()); |
| 1065 if (crypto_) { | 1045 if (crypto_) { |
| 1066 std::string encrypted_value; | 1046 std::string encrypted_value; |
| 1067 add_smt.BindCString(3, ""); // value | 1047 add_smt.BindCString(3, ""); // value |
| 1068 crypto_->EncryptString(po->cc().Value(), &encrypted_value); | 1048 crypto_->EncryptString(po->cc().Value(), &encrypted_value); |
| 1069 // BindBlob() immediately makes an internal copy of the data. | 1049 // BindBlob() immediately makes an internal copy of the data. |
| 1070 add_smt.BindBlob(4, encrypted_value.data(), | 1050 add_smt.BindBlob(4, encrypted_value.data(), |
| (...skipping 20 matching lines...) Expand all Loading... |
| 1091 update_access_smt.Reset(true); | 1071 update_access_smt.Reset(true); |
| 1092 update_access_smt.BindInt64(0, | 1072 update_access_smt.BindInt64(0, |
| 1093 po->cc().LastAccessDate().ToInternalValue()); | 1073 po->cc().LastAccessDate().ToInternalValue()); |
| 1094 update_access_smt.BindInt64(1, | 1074 update_access_smt.BindInt64(1, |
| 1095 po->cc().CreationDate().ToInternalValue()); | 1075 po->cc().CreationDate().ToInternalValue()); |
| 1096 if (!update_access_smt.Run()) | 1076 if (!update_access_smt.Run()) |
| 1097 NOTREACHED() << "Could not update cookie last access time in the DB."; | 1077 NOTREACHED() << "Could not update cookie last access time in the DB."; |
| 1098 break; | 1078 break; |
| 1099 | 1079 |
| 1100 case PendingOperation::COOKIE_DELETE: | 1080 case PendingOperation::COOKIE_DELETE: |
| 1101 cookies_per_origin_[ | |
| 1102 CookieOrigin(po->cc().Domain(), po->cc().IsSecure())]--; | |
| 1103 del_smt.Reset(true); | 1081 del_smt.Reset(true); |
| 1104 del_smt.BindInt64(0, po->cc().CreationDate().ToInternalValue()); | 1082 del_smt.BindInt64(0, po->cc().CreationDate().ToInternalValue()); |
| 1105 if (!del_smt.Run()) | 1083 if (!del_smt.Run()) |
| 1106 NOTREACHED() << "Could not delete a cookie from the DB."; | 1084 NOTREACHED() << "Could not delete a cookie from the DB."; |
| 1107 break; | 1085 break; |
| 1108 | 1086 |
| 1109 default: | 1087 default: |
| 1110 NOTREACHED(); | 1088 NOTREACHED(); |
| 1111 break; | 1089 break; |
| 1112 } | 1090 } |
| (...skipping 27 matching lines...) Expand all Loading... |
| 1140 PostBackgroundTask(FROM_HERE, | 1118 PostBackgroundTask(FROM_HERE, |
| 1141 base::Bind(&Backend::InternalBackgroundClose, this)); | 1119 base::Bind(&Backend::InternalBackgroundClose, this)); |
| 1142 } | 1120 } |
| 1143 } | 1121 } |
| 1144 | 1122 |
| 1145 void SQLitePersistentCookieStore::Backend::InternalBackgroundClose() { | 1123 void SQLitePersistentCookieStore::Backend::InternalBackgroundClose() { |
| 1146 DCHECK(background_task_runner_->RunsTasksOnCurrentThread()); | 1124 DCHECK(background_task_runner_->RunsTasksOnCurrentThread()); |
| 1147 // Commit any pending operations | 1125 // Commit any pending operations |
| 1148 Commit(); | 1126 Commit(); |
| 1149 | 1127 |
| 1150 if (!force_keep_session_state_ && special_storage_policy_.get() && | |
| 1151 special_storage_policy_->HasSessionOnlyOrigins()) { | |
| 1152 DeleteSessionCookiesOnShutdown(); | |
| 1153 } | |
| 1154 | |
| 1155 meta_table_.Reset(); | 1128 meta_table_.Reset(); |
| 1156 db_.reset(); | 1129 db_.reset(); |
| 1157 } | 1130 } |
| 1158 | 1131 |
| 1159 void SQLitePersistentCookieStore::Backend::DeleteSessionCookiesOnShutdown() { | |
| 1160 DCHECK(background_task_runner_->RunsTasksOnCurrentThread()); | |
| 1161 | |
| 1162 if (!db_) | |
| 1163 return; | |
| 1164 | |
| 1165 if (!special_storage_policy_.get()) | |
| 1166 return; | |
| 1167 | |
| 1168 sql::Statement del_smt(db_->GetCachedStatement( | |
| 1169 SQL_FROM_HERE, "DELETE FROM cookies WHERE host_key=? AND secure=?")); | |
| 1170 if (!del_smt.is_valid()) { | |
| 1171 LOG(WARNING) << "Unable to delete cookies on shutdown."; | |
| 1172 return; | |
| 1173 } | |
| 1174 | |
| 1175 sql::Transaction transaction(db_.get()); | |
| 1176 if (!transaction.Begin()) { | |
| 1177 LOG(WARNING) << "Unable to delete cookies on shutdown."; | |
| 1178 return; | |
| 1179 } | |
| 1180 | |
| 1181 for (CookiesPerOriginMap::iterator it = cookies_per_origin_.begin(); | |
| 1182 it != cookies_per_origin_.end(); ++it) { | |
| 1183 if (it->second <= 0) { | |
| 1184 DCHECK_EQ(0, it->second); | |
| 1185 continue; | |
| 1186 } | |
| 1187 const GURL url(net::cookie_util::CookieOriginToURL(it->first.first, | |
| 1188 it->first.second)); | |
| 1189 if (!url.is_valid() || !special_storage_policy_->IsStorageSessionOnly(url)) | |
| 1190 continue; | |
| 1191 | |
| 1192 del_smt.Reset(true); | |
| 1193 del_smt.BindString(0, it->first.first); | |
| 1194 del_smt.BindInt(1, it->first.second); | |
| 1195 if (!del_smt.Run()) | |
| 1196 NOTREACHED() << "Could not delete a cookie from the DB."; | |
| 1197 } | |
| 1198 | |
| 1199 if (!transaction.Commit()) | |
| 1200 LOG(WARNING) << "Unable to delete cookies on shutdown."; | |
| 1201 } | |
| 1202 | |
| 1203 void SQLitePersistentCookieStore::Backend::DatabaseErrorCallback( | 1132 void SQLitePersistentCookieStore::Backend::DatabaseErrorCallback( |
| 1204 int error, | 1133 int error, |
| 1205 sql::Statement* stmt) { | 1134 sql::Statement* stmt) { |
| 1206 DCHECK(background_task_runner_->RunsTasksOnCurrentThread()); | 1135 DCHECK(background_task_runner_->RunsTasksOnCurrentThread()); |
| 1207 | 1136 |
| 1208 if (!sql::IsErrorCatastrophic(error)) | 1137 if (!sql::IsErrorCatastrophic(error)) |
| 1209 return; | 1138 return; |
| 1210 | 1139 |
| 1211 // TODO(shess): Running KillDatabase() multiple times should be | 1140 // TODO(shess): Running KillDatabase() multiple times should be |
| 1212 // safe. | 1141 // safe. |
| (...skipping 16 matching lines...) Expand all Loading... |
| 1229 if (db_) { | 1158 if (db_) { |
| 1230 // This Backend will now be in-memory only. In a future run we will recreate | 1159 // This Backend will now be in-memory only. In a future run we will recreate |
| 1231 // the database. Hopefully things go better then! | 1160 // the database. Hopefully things go better then! |
| 1232 bool success = db_->RazeAndClose(); | 1161 bool success = db_->RazeAndClose(); |
| 1233 UMA_HISTOGRAM_BOOLEAN("Cookie.KillDatabaseResult", success); | 1162 UMA_HISTOGRAM_BOOLEAN("Cookie.KillDatabaseResult", success); |
| 1234 meta_table_.Reset(); | 1163 meta_table_.Reset(); |
| 1235 db_.reset(); | 1164 db_.reset(); |
| 1236 } | 1165 } |
| 1237 } | 1166 } |
| 1238 | 1167 |
| 1239 void SQLitePersistentCookieStore::Backend::SetForceKeepSessionState() { | 1168 void SQLitePersistentCookieStore::Backend::DeleteAllInList( |
| 1240 base::AutoLock locked(lock_); | 1169 const std::list<CookieOrigin>& cookies) { |
| 1241 force_keep_session_state_ = true; | 1170 if (cookies.empty()) |
| 1171 return; |
| 1172 |
| 1173 // Perform deletion on background task runner. |
| 1174 background_task_runner_->PostTask( |
| 1175 FROM_HERE, |
| 1176 base::Bind( |
| 1177 &Backend::BackgroundDeleteAllInList, this, cookies)); |
| 1242 } | 1178 } |
| 1243 | 1179 |
| 1244 void SQLitePersistentCookieStore::Backend::DeleteSessionCookiesOnStartup() { | 1180 void SQLitePersistentCookieStore::Backend::DeleteSessionCookiesOnStartup() { |
| 1245 DCHECK(background_task_runner_->RunsTasksOnCurrentThread()); | 1181 DCHECK(background_task_runner_->RunsTasksOnCurrentThread()); |
| 1246 if (!db_->Execute("DELETE FROM cookies WHERE persistent == 0")) | 1182 if (!db_->Execute("DELETE FROM cookies WHERE persistent == 0")) |
| 1247 LOG(WARNING) << "Unable to delete session cookies."; | 1183 LOG(WARNING) << "Unable to delete session cookies."; |
| 1248 } | 1184 } |
| 1249 | 1185 |
| 1186 void SQLitePersistentCookieStore::Backend::BackgroundDeleteAllInList( |
| 1187 const std::list<CookieOrigin>& cookies) { |
| 1188 DCHECK(background_task_runner_->RunsTasksOnCurrentThread()); |
| 1189 |
| 1190 if (!db_) |
| 1191 return; |
| 1192 |
| 1193 // Force a commit of any pending writes before issuing deletes. |
| 1194 // TODO(rohitrao): Remove the need for this Commit() by instead pruning the |
| 1195 // list of pending operations. |
| 1196 Commit(); |
| 1197 |
| 1198 sql::Statement del_smt(db_->GetCachedStatement( |
| 1199 SQL_FROM_HERE, "DELETE FROM cookies WHERE host_key=? AND secure=?")); |
| 1200 if (!del_smt.is_valid()) { |
| 1201 LOG(WARNING) << "Unable to delete cookies on shutdown."; |
| 1202 return; |
| 1203 } |
| 1204 |
| 1205 sql::Transaction transaction(db_.get()); |
| 1206 if (!transaction.Begin()) { |
| 1207 LOG(WARNING) << "Unable to delete cookies on shutdown."; |
| 1208 return; |
| 1209 } |
| 1210 |
| 1211 for (auto it = cookies.begin(); it != cookies.end(); ++it) { |
| 1212 const GURL url(cookie_util::CookieOriginToURL(it->first, it->second)); |
| 1213 del_smt.Reset(true); |
| 1214 del_smt.BindString(0, it->first); |
| 1215 del_smt.BindInt(1, it->second); |
| 1216 if (!del_smt.Run()) |
| 1217 NOTREACHED() << "Could not delete a cookie from the DB."; |
| 1218 } |
| 1219 |
| 1220 if (!transaction.Commit()) |
| 1221 LOG(WARNING) << "Unable to delete cookies on shutdown."; |
| 1222 } |
| 1223 |
| 1250 void SQLitePersistentCookieStore::Backend::PostBackgroundTask( | 1224 void SQLitePersistentCookieStore::Backend::PostBackgroundTask( |
| 1251 const tracked_objects::Location& origin, const base::Closure& task) { | 1225 const tracked_objects::Location& origin, const base::Closure& task) { |
| 1252 if (!background_task_runner_->PostTask(origin, task)) { | 1226 if (!background_task_runner_->PostTask(origin, task)) { |
| 1253 LOG(WARNING) << "Failed to post task from " << origin.ToString() | 1227 LOG(WARNING) << "Failed to post task from " << origin.ToString() |
| 1254 << " to background_task_runner_."; | 1228 << " to background_task_runner_."; |
| 1255 } | 1229 } |
| 1256 } | 1230 } |
| 1257 | 1231 |
| 1258 void SQLitePersistentCookieStore::Backend::PostClientTask( | 1232 void SQLitePersistentCookieStore::Backend::PostClientTask( |
| 1259 const tracked_objects::Location& origin, const base::Closure& task) { | 1233 const tracked_objects::Location& origin, const base::Closure& task) { |
| (...skipping 10 matching lines...) Expand all Loading... |
| 1270 loaded_callback, success)); | 1244 loaded_callback, success)); |
| 1271 if (success && !restore_old_session_cookies_) | 1245 if (success && !restore_old_session_cookies_) |
| 1272 DeleteSessionCookiesOnStartup(); | 1246 DeleteSessionCookiesOnStartup(); |
| 1273 } | 1247 } |
| 1274 | 1248 |
| 1275 SQLitePersistentCookieStore::SQLitePersistentCookieStore( | 1249 SQLitePersistentCookieStore::SQLitePersistentCookieStore( |
| 1276 const base::FilePath& path, | 1250 const base::FilePath& path, |
| 1277 const scoped_refptr<base::SequencedTaskRunner>& client_task_runner, | 1251 const scoped_refptr<base::SequencedTaskRunner>& client_task_runner, |
| 1278 const scoped_refptr<base::SequencedTaskRunner>& background_task_runner, | 1252 const scoped_refptr<base::SequencedTaskRunner>& background_task_runner, |
| 1279 bool restore_old_session_cookies, | 1253 bool restore_old_session_cookies, |
| 1280 storage::SpecialStoragePolicy* special_storage_policy, | 1254 CookieCryptoDelegate* crypto_delegate) |
| 1281 net::CookieCryptoDelegate* crypto_delegate) | |
| 1282 : backend_(new Backend(path, | 1255 : backend_(new Backend(path, |
| 1283 client_task_runner, | 1256 client_task_runner, |
| 1284 background_task_runner, | 1257 background_task_runner, |
| 1285 restore_old_session_cookies, | 1258 restore_old_session_cookies, |
| 1286 special_storage_policy, | |
| 1287 crypto_delegate)) { | 1259 crypto_delegate)) { |
| 1288 } | 1260 } |
| 1289 | 1261 |
| 1262 void SQLitePersistentCookieStore::DeleteAllInList( |
| 1263 const std::list<CookieOrigin>& cookies) { |
| 1264 backend_->DeleteAllInList(cookies); |
| 1265 } |
| 1266 |
| 1290 void SQLitePersistentCookieStore::Load(const LoadedCallback& loaded_callback) { | 1267 void SQLitePersistentCookieStore::Load(const LoadedCallback& loaded_callback) { |
| 1291 backend_->Load(loaded_callback); | 1268 backend_->Load(loaded_callback); |
| 1292 } | 1269 } |
| 1293 | 1270 |
| 1294 void SQLitePersistentCookieStore::LoadCookiesForKey( | 1271 void SQLitePersistentCookieStore::LoadCookiesForKey( |
| 1295 const std::string& key, | 1272 const std::string& key, |
| 1296 const LoadedCallback& loaded_callback) { | 1273 const LoadedCallback& loaded_callback) { |
| 1297 backend_->LoadCookiesForKey(key, loaded_callback); | 1274 backend_->LoadCookiesForKey(key, loaded_callback); |
| 1298 } | 1275 } |
| 1299 | 1276 |
| 1300 void SQLitePersistentCookieStore::AddCookie(const net::CanonicalCookie& cc) { | 1277 void SQLitePersistentCookieStore::AddCookie(const CanonicalCookie& cc) { |
| 1301 backend_->AddCookie(cc); | 1278 backend_->AddCookie(cc); |
| 1302 } | 1279 } |
| 1303 | 1280 |
| 1304 void SQLitePersistentCookieStore::UpdateCookieAccessTime( | 1281 void SQLitePersistentCookieStore::UpdateCookieAccessTime( |
| 1305 const net::CanonicalCookie& cc) { | 1282 const CanonicalCookie& cc) { |
| 1306 backend_->UpdateCookieAccessTime(cc); | 1283 backend_->UpdateCookieAccessTime(cc); |
| 1307 } | 1284 } |
| 1308 | 1285 |
| 1309 void SQLitePersistentCookieStore::DeleteCookie(const net::CanonicalCookie& cc) { | 1286 void SQLitePersistentCookieStore::DeleteCookie(const CanonicalCookie& cc) { |
| 1310 backend_->DeleteCookie(cc); | 1287 backend_->DeleteCookie(cc); |
| 1311 } | 1288 } |
| 1312 | 1289 |
| 1313 void SQLitePersistentCookieStore::SetForceKeepSessionState() { | 1290 void SQLitePersistentCookieStore::SetForceKeepSessionState() { |
| 1314 backend_->SetForceKeepSessionState(); | 1291 // This store never discards session-only cookies, so this call has no effect. |
| 1315 } | 1292 } |
| 1316 | 1293 |
| 1317 void SQLitePersistentCookieStore::Flush(const base::Closure& callback) { | 1294 void SQLitePersistentCookieStore::Flush(const base::Closure& callback) { |
| 1318 backend_->Flush(callback); | 1295 backend_->Flush(callback); |
| 1319 } | 1296 } |
| 1320 | 1297 |
| 1321 SQLitePersistentCookieStore::~SQLitePersistentCookieStore() { | 1298 SQLitePersistentCookieStore::~SQLitePersistentCookieStore() { |
| 1322 backend_->Close(); | 1299 backend_->Close(); |
| 1323 // We release our reference to the Backend, though it will probably still have | 1300 // We release our reference to the Backend, though it will probably still have |
| 1324 // a reference if the background runner has not run Close() yet. | 1301 // a reference if the background runner has not run Close() yet. |
| 1325 } | 1302 } |
| 1326 | 1303 |
| 1327 CookieStoreConfig::CookieStoreConfig() | 1304 } // namespace net |
| 1328 : session_cookie_mode(EPHEMERAL_SESSION_COOKIES), | |
| 1329 crypto_delegate(NULL) { | |
| 1330 // Default to an in-memory cookie store. | |
| 1331 } | |
| 1332 | |
| 1333 CookieStoreConfig::CookieStoreConfig( | |
| 1334 const base::FilePath& path, | |
| 1335 SessionCookieMode session_cookie_mode, | |
| 1336 storage::SpecialStoragePolicy* storage_policy, | |
| 1337 net::CookieMonsterDelegate* cookie_delegate) | |
| 1338 : path(path), | |
| 1339 session_cookie_mode(session_cookie_mode), | |
| 1340 storage_policy(storage_policy), | |
| 1341 cookie_delegate(cookie_delegate), | |
| 1342 crypto_delegate(NULL) { | |
| 1343 CHECK(!path.empty() || session_cookie_mode == EPHEMERAL_SESSION_COOKIES); | |
| 1344 } | |
| 1345 | |
| 1346 CookieStoreConfig::~CookieStoreConfig() { | |
| 1347 } | |
| 1348 | |
| 1349 net::CookieStore* CreateCookieStore(const CookieStoreConfig& config) { | |
| 1350 net::CookieMonster* cookie_monster = NULL; | |
| 1351 | |
| 1352 if (config.path.empty()) { | |
| 1353 // Empty path means in-memory store. | |
| 1354 cookie_monster = new net::CookieMonster(NULL, config.cookie_delegate.get()); | |
| 1355 } else { | |
| 1356 scoped_refptr<base::SequencedTaskRunner> client_task_runner = | |
| 1357 config.client_task_runner; | |
| 1358 scoped_refptr<base::SequencedTaskRunner> background_task_runner = | |
| 1359 config.background_task_runner; | |
| 1360 | |
| 1361 if (!client_task_runner.get()) { | |
| 1362 client_task_runner = | |
| 1363 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO); | |
| 1364 } | |
| 1365 | |
| 1366 if (!background_task_runner.get()) { | |
| 1367 background_task_runner = | |
| 1368 BrowserThread::GetBlockingPool()->GetSequencedTaskRunner( | |
| 1369 BrowserThread::GetBlockingPool()->GetSequenceToken()); | |
| 1370 } | |
| 1371 | |
| 1372 SQLitePersistentCookieStore* persistent_store = | |
| 1373 new SQLitePersistentCookieStore( | |
| 1374 config.path, | |
| 1375 client_task_runner, | |
| 1376 background_task_runner, | |
| 1377 (config.session_cookie_mode == | |
| 1378 CookieStoreConfig::RESTORED_SESSION_COOKIES), | |
| 1379 config.storage_policy.get(), | |
| 1380 config.crypto_delegate); | |
| 1381 | |
| 1382 cookie_monster = | |
| 1383 new net::CookieMonster(persistent_store, config.cookie_delegate.get()); | |
| 1384 if ((config.session_cookie_mode == | |
| 1385 CookieStoreConfig::PERSISTANT_SESSION_COOKIES) || | |
| 1386 (config.session_cookie_mode == | |
| 1387 CookieStoreConfig::RESTORED_SESSION_COOKIES)) { | |
| 1388 cookie_monster->SetPersistSessionCookies(true); | |
| 1389 } | |
| 1390 } | |
| 1391 | |
| 1392 return cookie_monster; | |
| 1393 } | |
| 1394 | |
| 1395 } // namespace content | |
| OLD | NEW |