Index: chrome/browser/net/sqlite_persistent_cookie_store.cc |
=================================================================== |
--- chrome/browser/net/sqlite_persistent_cookie_store.cc (revision 26016) |
+++ chrome/browser/net/sqlite_persistent_cookie_store.cc (working copy) |
@@ -1,18 +1,19 @@ |
-// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. |
+// Copyright (c) 2009 The Chromium Authors. All rights reserved. |
// Use of this source code is governed by a BSD-style license that can be |
// found in the LICENSE file. |
-#include "chrome/common/net/cookie_monster_sqlite.h" |
+#include "chrome/browser/net/sqlite_persistent_cookie_store.h" |
#include <list> |
+#include "app/sql/statement.h" |
+#include "app/sql/transaction.h" |
#include "base/basictypes.h" |
#include "base/logging.h" |
#include "base/ref_counted.h" |
+#include "base/scoped_ptr.h" |
#include "base/string_util.h" |
#include "base/thread.h" |
-#include "chrome/common/sqlite_compiled_statement.h" |
-#include "chrome/common/sqlite_utils.h" |
using base::Time; |
@@ -23,10 +24,9 @@ |
public: |
// The passed database pointer must be already-initialized. This object will |
// take ownership. |
- explicit Backend(sqlite3* db, MessageLoop* loop) |
+ explicit Backend(sql::Connection* db, MessageLoop* loop) |
: db_(db), |
background_loop_(loop), |
- cache_(new SqliteStatementCache(db)), |
num_pending_(0) { |
DCHECK(db_) << "Database must exist."; |
} |
@@ -85,9 +85,8 @@ |
// Close() executed on the background thread. |
void InternalBackgroundClose(); |
- sqlite3* db_; |
+ sql::Connection* db_; |
MessageLoop* background_loop_; |
- SqliteStatementCache* cache_; |
typedef std::list<PendingOperation*> PendingOperationsList; |
PendingOperationsList pending_; |
@@ -159,70 +158,69 @@ |
if (!db_ || ops.empty()) |
return; |
- SQLITE_UNIQUE_STATEMENT(add_smt, *cache_, |
+ sql::Statement add_smt(db_->GetCachedStatement(SQL_FROM_HERE, |
"INSERT INTO cookies (creation_utc, host_key, name, value, path, " |
"expires_utc, secure, httponly, last_access_utc) " |
- "VALUES (?,?,?,?,?,?,?,?,?)"); |
- if (!add_smt.is_valid()) { |
+ "VALUES (?,?,?,?,?,?,?,?,?)")); |
+ if (!add_smt) { |
NOTREACHED(); |
return; |
} |
- SQLITE_UNIQUE_STATEMENT(update_access_smt, *cache_, |
- "UPDATE cookies SET last_access_utc=? " |
- "WHERE creation_utc=?"); |
- if (!update_access_smt.is_valid()) { |
+ sql::Statement update_access_smt(db_->GetCachedStatement(SQL_FROM_HERE, |
+ "UPDATE cookies SET last_access_utc=? WHERE creation_utc=?")); |
+ if (!update_access_smt) { |
NOTREACHED(); |
return; |
} |
- SQLITE_UNIQUE_STATEMENT(del_smt, *cache_, |
- "DELETE FROM cookies WHERE creation_utc=?"); |
- if (!del_smt.is_valid()) { |
+ sql::Statement del_smt(db_->GetCachedStatement(SQL_FROM_HERE, |
+ "DELETE FROM cookies WHERE creation_utc=?")); |
+ if (!del_smt) { |
NOTREACHED(); |
return; |
} |
- SQLTransaction transaction(db_); |
- transaction.Begin(); |
+ sql::Transaction transaction(db_); |
+ if (!transaction.Begin()) { |
+ NOTREACHED(); |
+ return; |
+ } |
for (PendingOperationsList::iterator it = ops.begin(); |
it != ops.end(); ++it) { |
// Free the cookies as we commit them to the database. |
scoped_ptr<PendingOperation> po(*it); |
switch (po->op()) { |
case PendingOperation::COOKIE_ADD: |
- add_smt->reset(); |
- add_smt->bind_int64(0, po->cc().CreationDate().ToInternalValue()); |
- add_smt->bind_string(1, po->key()); |
- add_smt->bind_string(2, po->cc().Name()); |
- add_smt->bind_string(3, po->cc().Value()); |
- add_smt->bind_string(4, po->cc().Path()); |
- add_smt->bind_int64(5, po->cc().ExpiryDate().ToInternalValue()); |
- add_smt->bind_int(6, po->cc().IsSecure()); |
- add_smt->bind_int(7, po->cc().IsHttpOnly()); |
- add_smt->bind_int64(8, po->cc().LastAccessDate().ToInternalValue()); |
- if (add_smt->step() != SQLITE_DONE) { |
+ add_smt.Reset(); |
+ add_smt.BindInt64(0, po->cc().CreationDate().ToInternalValue()); |
+ add_smt.BindString(1, po->key()); |
+ add_smt.BindString(2, po->cc().Name()); |
+ add_smt.BindString(3, po->cc().Value()); |
+ add_smt.BindString(4, po->cc().Path()); |
+ add_smt.BindInt64(5, po->cc().ExpiryDate().ToInternalValue()); |
+ add_smt.BindInt(6, po->cc().IsSecure()); |
+ add_smt.BindInt(7, po->cc().IsHttpOnly()); |
+ add_smt.BindInt64(8, po->cc().LastAccessDate().ToInternalValue()); |
+ if (!add_smt.Run()) |
NOTREACHED() << "Could not add a cookie to the DB."; |
- } |
break; |
case PendingOperation::COOKIE_UPDATEACCESS: |
- update_access_smt->reset(); |
- update_access_smt->bind_int64(0, |
+ update_access_smt.Reset(); |
+ update_access_smt.BindInt64(0, |
po->cc().LastAccessDate().ToInternalValue()); |
- update_access_smt->bind_int64(1, |
+ update_access_smt.BindInt64(1, |
po->cc().CreationDate().ToInternalValue()); |
- if (update_access_smt->step() != SQLITE_DONE) { |
+ if (!update_access_smt.Run()) |
NOTREACHED() << "Could not update cookie last access time in the DB."; |
- } |
break; |
case PendingOperation::COOKIE_DELETE: |
- del_smt->reset(); |
- del_smt->bind_int64(0, po->cc().CreationDate().ToInternalValue()); |
- if (del_smt->step() != SQLITE_DONE) { |
+ del_smt.Reset(); |
+ del_smt.BindInt64(0, po->cc().CreationDate().ToInternalValue()); |
+ if (!del_smt.Run()) |
NOTREACHED() << "Could not delete a cookie from the DB."; |
- } |
break; |
default: |
@@ -248,15 +246,14 @@ |
DCHECK(MessageLoop::current() == background_loop_); |
// Commit any pending operations |
Commit(); |
- // We must destroy the cache before closing the database. |
- delete cache_; |
- cache_ = NULL; |
- sqlite3_close(db_); |
+ |
+ delete db_; |
db_ = NULL; |
} |
SQLitePersistentCookieStore::SQLitePersistentCookieStore( |
- const std::wstring& path, MessageLoop* background_loop) |
+ const FilePath& path, |
+ MessageLoop* background_loop) |
: path_(path), |
background_loop_(background_loop) { |
DCHECK(background_loop) << "SQLitePersistentCookieStore needs a MessageLoop"; |
@@ -285,28 +282,25 @@ |
namespace { |
// Initializes the cookies table, returning true on success. |
-bool InitTable(sqlite3* db) { |
- if (!DoesSqliteTableExist(db, "cookies")) { |
- if (sqlite3_exec(db, "CREATE TABLE cookies (" |
- "creation_utc INTEGER NOT NULL UNIQUE PRIMARY KEY," |
- "host_key TEXT NOT NULL," |
- "name TEXT NOT NULL," |
- "value TEXT NOT NULL," |
- "path TEXT NOT NULL," |
- // We only store persistent, so we know it expires |
- "expires_utc INTEGER NOT NULL," |
- "secure INTEGER NOT NULL," |
- "httponly INTEGER NOT NULL," |
- "last_access_utc INTEGER NOT NULL)", |
- NULL, NULL, NULL) != SQLITE_OK) |
+bool InitTable(sql::Connection* db) { |
+ if (!db->DoesTableExist("cookies")) { |
+ if (!db->Execute("CREATE TABLE cookies (" |
+ "creation_utc INTEGER NOT NULL UNIQUE PRIMARY KEY," |
+ "host_key TEXT NOT NULL," |
+ "name TEXT NOT NULL," |
+ "value TEXT NOT NULL," |
+ "path TEXT NOT NULL," |
+ // We only store persistent, so we know it expires |
+ "expires_utc INTEGER NOT NULL," |
+ "secure INTEGER NOT NULL," |
+ "httponly INTEGER NOT NULL," |
+ "last_access_utc INTEGER NOT NULL)")) |
return false; |
} |
// Try to create the index every time. Older versions did not have this index, |
// so we want those people to get it. Ignore errors, since it may exist. |
- sqlite3_exec(db, "CREATE INDEX cookie_times ON cookies (creation_utc)", |
- NULL, NULL, NULL); |
- |
+ db->Execute("CREATE INDEX cookie_times ON cookies (creation_utc)"); |
return true; |
} |
@@ -314,65 +308,55 @@ |
bool SQLitePersistentCookieStore::Load( |
std::vector<net::CookieMonster::KeyedCanonicalCookie>* cookies) { |
- DCHECK(!path_.empty()); |
- sqlite3* db; |
- if (sqlite3_open(WideToUTF8(path_).c_str(), &db) != SQLITE_OK) { |
+ scoped_ptr<sql::Connection> db(new sql::Connection); |
+ if (!db->Init(path_)) { |
NOTREACHED() << "Unable to open cookie DB."; |
return false; |
} |
- if (!EnsureDatabaseVersion(db) || !InitTable(db)) { |
+ if (!EnsureDatabaseVersion(db.get()) || !InitTable(db.get())) { |
NOTREACHED() << "Unable to initialize cookie DB."; |
- sqlite3_close(db); |
return false; |
} |
- MetaTableHelper::PrimeCache(std::string(), db); |
+ db->Preload(); |
// Slurp all the cookies into the out-vector. |
- SQLStatement smt; |
- if (smt.prepare(db, |
+ sql::Statement smt(db->GetUniqueStatement( |
"SELECT creation_utc, host_key, name, value, path, expires_utc, secure, " |
- "httponly, last_access_utc FROM cookies") != SQLITE_OK) { |
+ "httponly, last_access_utc FROM cookies")); |
+ if (!smt) { |
NOTREACHED() << "select statement prep failed"; |
- sqlite3_close(db); |
return false; |
} |
- while (smt.step() == SQLITE_ROW) { |
- std::string key = smt.column_string(1); |
+ while (smt.Step()) { |
scoped_ptr<net::CookieMonster::CanonicalCookie> cc( |
new net::CookieMonster::CanonicalCookie( |
- smt.column_string(2), // name |
- smt.column_string(3), // value |
- smt.column_string(4), // path |
- smt.column_int(6) != 0, // secure |
- smt.column_int(7) != 0, // httponly |
- Time::FromInternalValue(smt.column_int64(0)), // creation_utc |
- Time::FromInternalValue(smt.column_int64(8)), // last_access_utc |
- true, // has_expires |
- Time::FromInternalValue(smt.column_int64(5)))); // expires_utc |
- // Memory allocation failed. |
- if (!cc.get()) |
- break; |
- |
+ smt.ColumnString(2), // name |
+ smt.ColumnString(3), // value |
+ smt.ColumnString(4), // path |
+ smt.ColumnInt(6) != 0, // secure |
+ smt.ColumnInt(7) != 0, // httponly |
+ Time::FromInternalValue(smt.ColumnInt64(0)), // creation_utc |
+ Time::FromInternalValue(smt.ColumnInt64(8)), // last_access_utc |
+ true, // has_expires |
+ Time::FromInternalValue(smt.ColumnInt64(5)))); // expires_utc |
DLOG_IF(WARNING, |
cc->CreationDate() > Time::Now()) << L"CreationDate too recent"; |
cookies->push_back( |
- net::CookieMonster::KeyedCanonicalCookie(smt.column_string(1), |
+ net::CookieMonster::KeyedCanonicalCookie(smt.ColumnString(1), |
cc.release())); |
} |
// Create the backend, this will take ownership of the db pointer. |
- backend_ = new Backend(db, background_loop_); |
- |
+ backend_ = new Backend(db.release(), background_loop_); |
return true; |
} |
-bool SQLitePersistentCookieStore::EnsureDatabaseVersion(sqlite3* db) { |
+bool SQLitePersistentCookieStore::EnsureDatabaseVersion(sql::Connection* db) { |
// Version check. |
- if (!meta_table_.Init(std::string(), kCurrentVersionNumber, |
- kCompatibleVersionNumber, db)) |
+ if (!meta_table_.Init(db, kCurrentVersionNumber, kCompatibleVersionNumber)) |
return false; |
if (meta_table_.GetCompatibleVersionNumber() > kCurrentVersionNumber) { |
@@ -382,14 +366,12 @@ |
int cur_version = meta_table_.GetVersionNumber(); |
if (cur_version == 2) { |
- SQLTransaction transaction(db); |
- transaction.Begin(); |
- if ((sqlite3_exec(db, |
- "ALTER TABLE cookies ADD COLUMN last_access_utc " |
- "INTEGER DEFAULT 0", NULL, NULL, NULL) != SQLITE_OK) || |
- (sqlite3_exec(db, |
- "UPDATE cookies SET last_access_utc = creation_utc", |
- NULL, NULL, NULL) != SQLITE_OK)) { |
+ sql::Transaction transaction(db); |
+ if (!transaction.Begin()) |
+ return false; |
+ if (!db->Execute("ALTER TABLE cookies ADD COLUMN last_access_utc " |
+ "INTEGER DEFAULT 0") || |
+ !db->Execute("UPDATE cookies SET last_access_utc = creation_utc")) { |
LOG(WARNING) << "Unable to update cookie database to version 3."; |
return false; |
} |
@@ -407,30 +389,27 @@ |
// versions. So we have to be careful to only update times that are under |
// the old system (which will appear to be from before 1970 in the new |
// system). The magic number used below is 1970 in our time units. |
- SQLTransaction transaction(db); |
+ sql::Transaction transaction(db); |
transaction.Begin(); |
#if !defined(OS_WIN) |
- sqlite3_exec(db, |
+ db->Execute( |
"UPDATE cookies " |
"SET creation_utc = creation_utc + 11644473600000000 " |
"WHERE rowid IN " |
"(SELECT rowid FROM cookies WHERE " |
- "creation_utc > 0 AND creation_utc < 11644473600000000)", |
- NULL, NULL, NULL); |
- sqlite3_exec(db, |
+ "creation_utc > 0 AND creation_utc < 11644473600000000)"); |
+ db->Execute( |
"UPDATE cookies " |
"SET expires_utc = expires_utc + 11644473600000000 " |
"WHERE rowid IN " |
"(SELECT rowid FROM cookies WHERE " |
- "expires_utc > 0 AND expires_utc < 11644473600000000)", |
- NULL, NULL, NULL); |
- sqlite3_exec(db, |
+ "expires_utc > 0 AND expires_utc < 11644473600000000)"); |
+ db->Execute( |
"UPDATE cookies " |
"SET last_access_utc = last_access_utc + 11644473600000000 " |
"WHERE rowid IN " |
"(SELECT rowid FROM cookies WHERE " |
- "last_access_utc > 0 AND last_access_utc < 11644473600000000)", |
- NULL, NULL, NULL); |
+ "last_access_utc > 0 AND last_access_utc < 11644473600000000)"); |
#endif |
++cur_version; |
meta_table_.SetVersionNumber(cur_version); |
Property changes on: chrome/browser/net/sqlite_persistent_cookie_store.cc |
___________________________________________________________________ |
Added: svn:mergeinfo |
Merged /branches/chrome_webkit_merge_branch/chrome/common/net/cookie_monster_sqlite.cc:r69-2775 |