OLD | NEW |
---|---|
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/common/net/cookie_monster_sqlite.h" | 5 #include "chrome/common/net/cookie_monster_sqlite.h" |
6 | 6 |
7 #include <list> | 7 #include <list> |
8 | 8 |
9 #include "base/basictypes.h" | 9 #include "base/basictypes.h" |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
(...skipping 253 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
264 | 264 |
265 SQLitePersistentCookieStore::~SQLitePersistentCookieStore() { | 265 SQLitePersistentCookieStore::~SQLitePersistentCookieStore() { |
266 if (backend_.get()) { | 266 if (backend_.get()) { |
267 backend_->Close(); | 267 backend_->Close(); |
268 // Release our reference, it will probably still have a reference if the | 268 // Release our reference, it will probably still have a reference if the |
269 // background thread has not run Close() yet. | 269 // background thread has not run Close() yet. |
270 backend_ = NULL; | 270 backend_ = NULL; |
271 } | 271 } |
272 } | 272 } |
273 | 273 |
274 // Version number of the database. | 274 // Version number of the database. In version 4, we migrated the time epoch. |
275 static const int kCurrentVersionNumber = 3; | 275 // If you open the DB with an older version on Mac or Linux, the times will |
276 // look wonky, but the file will likely be usable. On Windows version 3 and 4 | |
277 // are the same. | |
278 static const int kCurrentVersionNumber = 4; | |
276 static const int kCompatibleVersionNumber = 3; | 279 static const int kCompatibleVersionNumber = 3; |
Evan Martin
2009/09/01 00:04:14
Can you doc this too? (I checked the code and it
| |
277 | 280 |
278 namespace { | 281 namespace { |
279 | 282 |
280 // Initializes the cookies table, returning true on success. | 283 // Initializes the cookies table, returning true on success. |
281 bool InitTable(sqlite3* db) { | 284 bool InitTable(sqlite3* db) { |
282 if (!DoesSqliteTableExist(db, "cookies")) { | 285 if (!DoesSqliteTableExist(db, "cookies")) { |
283 if (sqlite3_exec(db, "CREATE TABLE cookies (" | 286 if (sqlite3_exec(db, "CREATE TABLE cookies (" |
284 "creation_utc INTEGER NOT NULL UNIQUE PRIMARY KEY," | 287 "creation_utc INTEGER NOT NULL UNIQUE PRIMARY KEY," |
285 "host_key TEXT NOT NULL," | 288 "host_key TEXT NOT NULL," |
286 "name TEXT NOT NULL," | 289 "name TEXT NOT NULL," |
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
386 LOG(WARNING) << "Unable to update cookie database to version 3."; | 389 LOG(WARNING) << "Unable to update cookie database to version 3."; |
387 return false; | 390 return false; |
388 } | 391 } |
389 ++cur_version; | 392 ++cur_version; |
390 meta_table_.SetVersionNumber(cur_version); | 393 meta_table_.SetVersionNumber(cur_version); |
391 meta_table_.SetCompatibleVersionNumber( | 394 meta_table_.SetCompatibleVersionNumber( |
392 std::min(cur_version, kCompatibleVersionNumber)); | 395 std::min(cur_version, kCompatibleVersionNumber)); |
393 transaction.Commit(); | 396 transaction.Commit(); |
394 } | 397 } |
395 | 398 |
399 if (cur_version == 3) { | |
400 // The time epoch changed for Mac & Linux in this version to match Windows. | |
401 // This patch came after the main epoch change happened, so some | |
402 // developers have "good" times for cookies added by the more recent | |
403 // versions. So we have to be careful to only update times that are under | |
404 // the old system (which will appear to be from before 1970 in the new | |
405 // system). The magic number used below is 1970 in our time units. | |
406 SQLTransaction transaction(db); | |
407 transaction.Begin(); | |
408 #if !defined(OS_WIN) | |
409 sqlite3_exec(db, | |
410 "UPDATE cookies " | |
411 "SET creation_utc = creation_utc + 11644473600000000 " | |
412 "WHERE rowid IN " | |
413 "(SELECT rowid FROM cookies WHERE " | |
414 "creation_utc > 0 AND creation_utc < 11644473600000000)", | |
415 NULL, NULL, NULL); | |
416 sqlite3_exec(db, | |
417 "UPDATE cookies " | |
418 "SET expires_utc = expires_utc + 11644473600000000 " | |
419 "WHERE rowid IN " | |
420 "(SELECT rowid FROM cookies WHERE " | |
421 "expires_utc > 0 AND expires_utc < 11644473600000000)", | |
422 NULL, NULL, NULL); | |
423 sqlite3_exec(db, | |
424 "UPDATE cookies " | |
425 "SET last_access_utc = last_access_utc + 11644473600000000 " | |
426 "WHERE rowid IN " | |
427 "(SELECT rowid FROM cookies WHERE " | |
428 "last_access_utc > 0 AND last_access_utc < 11644473600000000)", | |
429 NULL, NULL, NULL); | |
430 #endif | |
431 ++cur_version; | |
432 meta_table_.SetVersionNumber(cur_version); | |
433 transaction.Commit(); | |
434 } | |
435 | |
396 // Put future migration cases here. | 436 // Put future migration cases here. |
397 | 437 |
398 // When the version is too old, we just try to continue anyway, there should | 438 // When the version is too old, we just try to continue anyway, there should |
399 // not be a released product that makes a database too old for us to handle. | 439 // not be a released product that makes a database too old for us to handle. |
400 LOG_IF(WARNING, cur_version < kCurrentVersionNumber) << | 440 LOG_IF(WARNING, cur_version < kCurrentVersionNumber) << |
401 "Cookie database version " << cur_version << " is too old to handle."; | 441 "Cookie database version " << cur_version << " is too old to handle."; |
402 | 442 |
403 return true; | 443 return true; |
404 } | 444 } |
405 | 445 |
406 void SQLitePersistentCookieStore::AddCookie( | 446 void SQLitePersistentCookieStore::AddCookie( |
407 const std::string& key, | 447 const std::string& key, |
408 const net::CookieMonster::CanonicalCookie& cc) { | 448 const net::CookieMonster::CanonicalCookie& cc) { |
409 if (backend_.get()) | 449 if (backend_.get()) |
410 backend_->AddCookie(key, cc); | 450 backend_->AddCookie(key, cc); |
411 } | 451 } |
412 | 452 |
413 void SQLitePersistentCookieStore::UpdateCookieAccessTime( | 453 void SQLitePersistentCookieStore::UpdateCookieAccessTime( |
414 const net::CookieMonster::CanonicalCookie& cc) { | 454 const net::CookieMonster::CanonicalCookie& cc) { |
415 if (backend_.get()) | 455 if (backend_.get()) |
416 backend_->UpdateCookieAccessTime(cc); | 456 backend_->UpdateCookieAccessTime(cc); |
417 } | 457 } |
418 | 458 |
419 void SQLitePersistentCookieStore::DeleteCookie( | 459 void SQLitePersistentCookieStore::DeleteCookie( |
420 const net::CookieMonster::CanonicalCookie& cc) { | 460 const net::CookieMonster::CanonicalCookie& cc) { |
421 if (backend_.get()) | 461 if (backend_.get()) |
422 backend_->DeleteCookie(cc); | 462 backend_->DeleteCookie(cc); |
423 } | 463 } |
OLD | NEW |