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