| 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 "content/browser/net/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> |
| (...skipping 288 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 299 // Not owned. | 299 // Not owned. |
| 300 net::CookieCryptoDelegate* crypto_; | 300 net::CookieCryptoDelegate* crypto_; |
| 301 | 301 |
| 302 DISALLOW_COPY_AND_ASSIGN(Backend); | 302 DISALLOW_COPY_AND_ASSIGN(Backend); |
| 303 }; | 303 }; |
| 304 | 304 |
| 305 namespace { | 305 namespace { |
| 306 | 306 |
| 307 // Version number of the database. | 307 // Version number of the database. |
| 308 // | 308 // |
| 309 // Version 9 adds a partial index to track non-persistent cookies. |
| 310 // Non-persistent cookies sometimes need to be deleted on startup. There are |
| 311 // frequently few or no non-persistent cookies, so the partial index allows the |
| 312 // deletion to be sped up or skipped, without having to page in the DB. |
| 313 // |
| 309 // Version 8 adds "first-party only" cookies. | 314 // Version 8 adds "first-party only" cookies. |
| 310 // | 315 // |
| 311 // Version 7 adds encrypted values. Old values will continue to be used but | 316 // Version 7 adds encrypted values. Old values will continue to be used but |
| 312 // all new values written will be encrypted on selected operating systems. New | 317 // all new values written will be encrypted on selected operating systems. New |
| 313 // records read by old clients will simply get an empty cookie value while old | 318 // records read by old clients will simply get an empty cookie value while old |
| 314 // records read by new clients will continue to operate with the unencrypted | 319 // records read by new clients will continue to operate with the unencrypted |
| 315 // version. New and old clients alike will always write/update records with | 320 // version. New and old clients alike will always write/update records with |
| 316 // what they support. | 321 // what they support. |
| 317 // | 322 // |
| 318 // Version 6 adds cookie priorities. This allows developers to influence the | 323 // Version 6 adds cookie priorities. This allows developers to influence the |
| 319 // order in which cookies are evicted in order to meet domain cookie limits. | 324 // order in which cookies are evicted in order to meet domain cookie limits. |
| 320 // | 325 // |
| 321 // Version 5 adds the columns has_expires and is_persistent, so that the | 326 // Version 5 adds the columns has_expires and is_persistent, so that the |
| 322 // database can store session cookies as well as persistent cookies. Databases | 327 // database can store session cookies as well as persistent cookies. Databases |
| 323 // of version 5 are incompatible with older versions of code. If a database of | 328 // of version 5 are incompatible with older versions of code. If a database of |
| 324 // version 5 is read by older code, session cookies will be treated as normal | 329 // version 5 is read by older code, session cookies will be treated as normal |
| 325 // cookies. Currently, these fields are written, but not read anymore. | 330 // cookies. Currently, these fields are written, but not read anymore. |
| 326 // | 331 // |
| 327 // In version 4, we migrated the time epoch. If you open the DB with an older | 332 // In version 4, we migrated the time epoch. If you open the DB with an older |
| 328 // version on Mac or Linux, the times will look wonky, but the file will likely | 333 // version on Mac or Linux, the times will look wonky, but the file will likely |
| 329 // be usable. On Windows version 3 and 4 are the same. | 334 // be usable. On Windows version 3 and 4 are the same. |
| 330 // | 335 // |
| 331 // Version 3 updated the database to include the last access time, so we can | 336 // Version 3 updated the database to include the last access time, so we can |
| 332 // expire them in decreasing order of use when we've reached the maximum | 337 // expire them in decreasing order of use when we've reached the maximum |
| 333 // number of cookies. | 338 // number of cookies. |
| 334 const int kCurrentVersionNumber = 8; | 339 const int kCurrentVersionNumber = 9; |
| 335 const int kCompatibleVersionNumber = 5; | 340 const int kCompatibleVersionNumber = 5; |
| 336 | 341 |
| 337 // Possible values for the 'priority' column. | 342 // Possible values for the 'priority' column. |
| 338 enum DBCookiePriority { | 343 enum DBCookiePriority { |
| 339 kCookiePriorityLow = 0, | 344 kCookiePriorityLow = 0, |
| 340 kCookiePriorityMedium = 1, | 345 kCookiePriorityMedium = 1, |
| 341 kCookiePriorityHigh = 2, | 346 kCookiePriorityHigh = 2, |
| 342 }; | 347 }; |
| 343 | 348 |
| 344 DBCookiePriority CookiePriorityToDBCookiePriority(net::CookiePriority value) { | 349 DBCookiePriority CookiePriorityToDBCookiePriority(net::CookiePriority value) { |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 387 private: | 392 private: |
| 388 base::TimeDelta* delta_; | 393 base::TimeDelta* delta_; |
| 389 base::TimeDelta original_value_; | 394 base::TimeDelta original_value_; |
| 390 base::Time start_; | 395 base::Time start_; |
| 391 | 396 |
| 392 DISALLOW_COPY_AND_ASSIGN(IncrementTimeDelta); | 397 DISALLOW_COPY_AND_ASSIGN(IncrementTimeDelta); |
| 393 }; | 398 }; |
| 394 | 399 |
| 395 // Initializes the cookies table, returning true on success. | 400 // Initializes the cookies table, returning true on success. |
| 396 bool InitTable(sql::Connection* db) { | 401 bool InitTable(sql::Connection* db) { |
| 397 if (!db->DoesTableExist("cookies")) { | 402 if (db->DoesTableExist("cookies")) |
| 398 std::string stmt(base::StringPrintf( | 403 return true; |
| 399 "CREATE TABLE cookies (" | |
| 400 "creation_utc INTEGER NOT NULL UNIQUE PRIMARY KEY," | |
| 401 "host_key TEXT NOT NULL," | |
| 402 "name TEXT NOT NULL," | |
| 403 "value TEXT NOT NULL," | |
| 404 "path TEXT NOT NULL," | |
| 405 "expires_utc INTEGER NOT NULL," | |
| 406 "secure INTEGER NOT NULL," | |
| 407 "httponly INTEGER NOT NULL," | |
| 408 "last_access_utc INTEGER NOT NULL, " | |
| 409 "has_expires INTEGER NOT NULL DEFAULT 1, " | |
| 410 "persistent INTEGER NOT NULL DEFAULT 1," | |
| 411 "priority INTEGER NOT NULL DEFAULT %d," | |
| 412 "encrypted_value BLOB DEFAULT ''," | |
| 413 "firstpartyonly INTEGER NOT NULL DEFAULT 0)", | |
| 414 CookiePriorityToDBCookiePriority(net::COOKIE_PRIORITY_DEFAULT))); | |
| 415 if (!db->Execute(stmt.c_str())) | |
| 416 return false; | |
| 417 } | |
| 418 | 404 |
| 419 // Older code created an index on creation_utc, which is already | 405 std::string stmt(base::StringPrintf( |
| 420 // primary key for the table. | 406 "CREATE TABLE cookies (" |
| 421 if (!db->Execute("DROP INDEX IF EXISTS cookie_times")) | 407 "creation_utc INTEGER NOT NULL UNIQUE PRIMARY KEY," |
| 408 "host_key TEXT NOT NULL," |
| 409 "name TEXT NOT NULL," |
| 410 "value TEXT NOT NULL," |
| 411 "path TEXT NOT NULL," |
| 412 "expires_utc INTEGER NOT NULL," |
| 413 "secure INTEGER NOT NULL," |
| 414 "httponly INTEGER NOT NULL," |
| 415 "last_access_utc INTEGER NOT NULL, " |
| 416 "has_expires INTEGER NOT NULL DEFAULT 1, " |
| 417 "persistent INTEGER NOT NULL DEFAULT 1," |
| 418 "priority INTEGER NOT NULL DEFAULT %d," |
| 419 "encrypted_value BLOB DEFAULT ''," |
| 420 "firstpartyonly INTEGER NOT NULL DEFAULT 0)", |
| 421 CookiePriorityToDBCookiePriority(net::COOKIE_PRIORITY_DEFAULT))); |
| 422 if (!db->Execute(stmt.c_str())) |
| 422 return false; | 423 return false; |
| 423 | 424 |
| 424 if (!db->Execute("CREATE INDEX IF NOT EXISTS domain ON cookies(host_key)")) | 425 if (!db->Execute("CREATE INDEX domain ON cookies(host_key)")) |
| 425 return false; | 426 return false; |
| 426 | 427 |
| 428 if (!db->Execute( |
| 429 "CREATE INDEX is_transient ON cookies(persistent) " |
| 430 "where persistent != 1")) { |
| 431 return false; |
| 432 } |
| 433 |
| 427 return true; | 434 return true; |
| 428 } | 435 } |
| 429 | 436 |
| 430 } // namespace | 437 } // namespace |
| 431 | 438 |
| 432 void SQLitePersistentCookieStore::Backend::Load( | 439 void SQLitePersistentCookieStore::Backend::Load( |
| 433 const LoadedCallback& loaded_callback) { | 440 const LoadedCallback& loaded_callback) { |
| 434 // This function should be called only once per instance. | 441 // This function should be called only once per instance. |
| 435 DCHECK(!db_.get()); | 442 DCHECK(!db_.get()); |
| 436 PostBackgroundTask(FROM_HERE, base::Bind( | 443 PostBackgroundTask(FROM_HERE, base::Bind( |
| (...skipping 500 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 937 } | 944 } |
| 938 ++cur_version; | 945 ++cur_version; |
| 939 meta_table_.SetVersionNumber(cur_version); | 946 meta_table_.SetVersionNumber(cur_version); |
| 940 meta_table_.SetCompatibleVersionNumber( | 947 meta_table_.SetCompatibleVersionNumber( |
| 941 std::min(cur_version, kCompatibleVersionNumber)); | 948 std::min(cur_version, kCompatibleVersionNumber)); |
| 942 transaction.Commit(); | 949 transaction.Commit(); |
| 943 UMA_HISTOGRAM_TIMES("Cookie.TimeDatabaseMigrationToV8", | 950 UMA_HISTOGRAM_TIMES("Cookie.TimeDatabaseMigrationToV8", |
| 944 base::TimeTicks::Now() - start_time); | 951 base::TimeTicks::Now() - start_time); |
| 945 } | 952 } |
| 946 | 953 |
| 954 if (cur_version == 8) { |
| 955 const base::TimeTicks start_time = base::TimeTicks::Now(); |
| 956 sql::Transaction transaction(db_.get()); |
| 957 if (!transaction.Begin()) |
| 958 return false; |
| 959 |
| 960 if (!db_->Execute("DROP INDEX IF EXISTS cookie_times")) { |
| 961 LOG(WARNING) |
| 962 << "Unable to drop table cookie_times in update to version 9."; |
| 963 return false; |
| 964 } |
| 965 |
| 966 if (!db_->Execute( |
| 967 "CREATE INDEX IF NOT EXISTS domain ON cookies(host_key)")) { |
| 968 LOG(WARNING) << "Unable to create index domain in update to version 9."; |
| 969 return false; |
| 970 } |
| 971 |
| 972 if (!db_->Execute( |
| 973 "CREATE INDEX IF NOT EXISTS is_transient ON cookies(persistent) " |
| 974 "where persistent != 1")) { |
| 975 LOG(WARNING) |
| 976 << "Unable to create index is_transient in update to version 9."; |
| 977 return false; |
| 978 } |
| 979 ++cur_version; |
| 980 meta_table_.SetVersionNumber(cur_version); |
| 981 meta_table_.SetCompatibleVersionNumber( |
| 982 std::min(cur_version, kCompatibleVersionNumber)); |
| 983 transaction.Commit(); |
| 984 UMA_HISTOGRAM_TIMES("Cookie.TimeDatabaseMigrationToV9", |
| 985 base::TimeTicks::Now() - start_time); |
| 986 } |
| 987 |
| 947 // Put future migration cases here. | 988 // Put future migration cases here. |
| 948 | 989 |
| 949 if (cur_version < kCurrentVersionNumber) { | 990 if (cur_version < kCurrentVersionNumber) { |
| 950 UMA_HISTOGRAM_COUNTS_100("Cookie.CorruptMetaTable", 1); | 991 UMA_HISTOGRAM_COUNTS_100("Cookie.CorruptMetaTable", 1); |
| 951 | 992 |
| 952 meta_table_.Reset(); | 993 meta_table_.Reset(); |
| 953 db_.reset(new sql::Connection); | 994 db_.reset(new sql::Connection); |
| 954 if (!sql::Connection::Delete(path_) || | 995 if (!sql::Connection::Delete(path_) || |
| 955 !db_->Open(path_) || | 996 !db_->Open(path_) || |
| 956 !meta_table_.Init( | 997 !meta_table_.Init( |
| (...skipping 429 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1386 (config.session_cookie_mode == | 1427 (config.session_cookie_mode == |
| 1387 CookieStoreConfig::RESTORED_SESSION_COOKIES)) { | 1428 CookieStoreConfig::RESTORED_SESSION_COOKIES)) { |
| 1388 cookie_monster->SetPersistSessionCookies(true); | 1429 cookie_monster->SetPersistSessionCookies(true); |
| 1389 } | 1430 } |
| 1390 } | 1431 } |
| 1391 | 1432 |
| 1392 return cookie_monster; | 1433 return cookie_monster; |
| 1393 } | 1434 } |
| 1394 | 1435 |
| 1395 } // namespace content | 1436 } // namespace content |
| OLD | NEW |