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 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
385 } | 390 } |
386 | 391 |
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 // Creates the indices on the cookie table, if they don't already exist. |
396 bool InitTable(sql::Connection* db) { | 401 bool UpdateIndicesOnTable(sql::Connection* db) { |
397 if (!db->DoesTableExist("cookies")) { | |
398 std::string stmt(base::StringPrintf( | |
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 | |
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)")) |
425 return false; | 408 return false; |
426 | 409 |
410 if (!db->Execute( | |
411 "CREATE INDEX IF NOT EXISTS is_transient ON cookies(persistent) " | |
412 "where persistent != 1")) { | |
413 return false; | |
414 } | |
415 | |
427 return true; | 416 return true; |
428 } | 417 } |
429 | 418 |
419 // Initializes the cookies table, returning true on success. | |
420 bool InitTable(sql::Connection* db) { | |
421 if (db->DoesTableExist("cookies")) | |
422 return true; | |
423 | |
424 std::string stmt(base::StringPrintf( | |
425 "CREATE TABLE cookies (" | |
426 "creation_utc INTEGER NOT NULL UNIQUE PRIMARY KEY," | |
427 "host_key TEXT NOT NULL," | |
428 "name TEXT NOT NULL," | |
429 "value TEXT NOT NULL," | |
430 "path TEXT NOT NULL," | |
431 "expires_utc INTEGER NOT NULL," | |
432 "secure INTEGER NOT NULL," | |
433 "httponly INTEGER NOT NULL," | |
434 "last_access_utc INTEGER NOT NULL, " | |
435 "has_expires INTEGER NOT NULL DEFAULT 1, " | |
436 "persistent INTEGER NOT NULL DEFAULT 1," | |
437 "priority INTEGER NOT NULL DEFAULT %d," | |
438 "encrypted_value BLOB DEFAULT ''," | |
439 "firstpartyonly INTEGER NOT NULL DEFAULT 0)", | |
440 CookiePriorityToDBCookiePriority(net::COOKIE_PRIORITY_DEFAULT))); | |
441 if (!db->Execute(stmt.c_str())) | |
442 return false; | |
443 | |
444 return UpdateIndicesOnTable(db); | |
445 } | |
446 | |
430 } // namespace | 447 } // namespace |
431 | 448 |
432 void SQLitePersistentCookieStore::Backend::Load( | 449 void SQLitePersistentCookieStore::Backend::Load( |
433 const LoadedCallback& loaded_callback) { | 450 const LoadedCallback& loaded_callback) { |
434 // This function should be called only once per instance. | 451 // This function should be called only once per instance. |
435 DCHECK(!db_.get()); | 452 DCHECK(!db_.get()); |
436 PostBackgroundTask(FROM_HERE, base::Bind( | 453 PostBackgroundTask(FROM_HERE, base::Bind( |
437 &Backend::LoadAndNotifyInBackground, this, | 454 &Backend::LoadAndNotifyInBackground, this, |
438 loaded_callback, base::Time::Now())); | 455 loaded_callback, base::Time::Now())); |
439 } | 456 } |
(...skipping 497 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
937 } | 954 } |
938 ++cur_version; | 955 ++cur_version; |
939 meta_table_.SetVersionNumber(cur_version); | 956 meta_table_.SetVersionNumber(cur_version); |
940 meta_table_.SetCompatibleVersionNumber( | 957 meta_table_.SetCompatibleVersionNumber( |
941 std::min(cur_version, kCompatibleVersionNumber)); | 958 std::min(cur_version, kCompatibleVersionNumber)); |
942 transaction.Commit(); | 959 transaction.Commit(); |
943 UMA_HISTOGRAM_TIMES("Cookie.TimeDatabaseMigrationToV8", | 960 UMA_HISTOGRAM_TIMES("Cookie.TimeDatabaseMigrationToV8", |
944 base::TimeTicks::Now() - start_time); | 961 base::TimeTicks::Now() - start_time); |
945 } | 962 } |
946 | 963 |
964 if (cur_version == 8) { | |
965 const base::TimeTicks start_time = base::TimeTicks::Now(); | |
966 sql::Transaction transaction(db_.get()); | |
967 if (!transaction.Begin()) | |
968 return false; | |
969 | |
970 if (!UpdateIndicesOnTable(db_.get())) { | |
erikwright (departed)
2015/04/17 01:20:47
I don't know that this pattern makes sense.
Imagi
erikchen
2015/04/17 01:34:47
Excellent point. I failed to notice that the patte
| |
971 LOG(WARNING) << "Unable to update cookie database to version 9."; | |
972 return false; | |
973 } | |
974 ++cur_version; | |
975 meta_table_.SetVersionNumber(cur_version); | |
976 meta_table_.SetCompatibleVersionNumber( | |
977 std::min(cur_version, kCompatibleVersionNumber)); | |
978 transaction.Commit(); | |
979 UMA_HISTOGRAM_TIMES("Cookie.TimeDatabaseMigrationToV9", | |
980 base::TimeTicks::Now() - start_time); | |
981 } | |
982 | |
947 // Put future migration cases here. | 983 // Put future migration cases here. |
948 | 984 |
949 if (cur_version < kCurrentVersionNumber) { | 985 if (cur_version < kCurrentVersionNumber) { |
950 UMA_HISTOGRAM_COUNTS_100("Cookie.CorruptMetaTable", 1); | 986 UMA_HISTOGRAM_COUNTS_100("Cookie.CorruptMetaTable", 1); |
951 | 987 |
952 meta_table_.Reset(); | 988 meta_table_.Reset(); |
953 db_.reset(new sql::Connection); | 989 db_.reset(new sql::Connection); |
954 if (!sql::Connection::Delete(path_) || | 990 if (!sql::Connection::Delete(path_) || |
955 !db_->Open(path_) || | 991 !db_->Open(path_) || |
956 !meta_table_.Init( | 992 !meta_table_.Init( |
(...skipping 429 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1386 (config.session_cookie_mode == | 1422 (config.session_cookie_mode == |
1387 CookieStoreConfig::RESTORED_SESSION_COOKIES)) { | 1423 CookieStoreConfig::RESTORED_SESSION_COOKIES)) { |
1388 cookie_monster->SetPersistSessionCookies(true); | 1424 cookie_monster->SetPersistSessionCookies(true); |
1389 } | 1425 } |
1390 } | 1426 } |
1391 | 1427 |
1392 return cookie_monster; | 1428 return cookie_monster; |
1393 } | 1429 } |
1394 | 1430 |
1395 } // namespace content | 1431 } // namespace content |
OLD | NEW |