summaryrefslogtreecommitdiffstats
path: root/chrome/browser/webdata/token_service_table.cc
diff options
context:
space:
mode:
authorandybons@chromium.org <andybons@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2011-04-01 13:34:53 +0000
committerandybons@chromium.org <andybons@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2011-04-01 13:34:53 +0000
commit9268173603c6ee2b9292fe53017fd27a444307b6 (patch)
tree400fbb906d2a2f7f264261c2ad2873a61ffda1db /chrome/browser/webdata/token_service_table.cc
parent0414411f67979897882bbad4f7b5f218c30c1f76 (diff)
downloadchromium_src-9268173603c6ee2b9292fe53017fd27a444307b6.zip
chromium_src-9268173603c6ee2b9292fe53017fd27a444307b6.tar.gz
chromium_src-9268173603c6ee2b9292fe53017fd27a444307b6.tar.bz2
Split out the rest of the tables within WebDatabase.
Logins, WebApps and TokenService BUG=none TEST=WebDatabaseMigrationTest,LoginsTableTest,WebAppsTableTest,TokenServiceTableTest Review URL: http://codereview.chromium.org/6670122 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@80150 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/webdata/token_service_table.cc')
-rw-r--r--chrome/browser/webdata/token_service_table.cc92
1 files changed, 92 insertions, 0 deletions
diff --git a/chrome/browser/webdata/token_service_table.cc b/chrome/browser/webdata/token_service_table.cc
new file mode 100644
index 0000000..8567112
--- /dev/null
+++ b/chrome/browser/webdata/token_service_table.cc
@@ -0,0 +1,92 @@
+// Copyright (c) 2011 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/browser/webdata/token_service_table.h"
+
+#include <map>
+#include <string>
+
+#include "app/sql/statement.h"
+#include "base/logging.h"
+#include "chrome/browser/password_manager/encryptor.h"
+
+bool TokenServiceTable::Init() {
+ if (!db_->DoesTableExist("token_service")) {
+ if (!db_->Execute("CREATE TABLE token_service ("
+ "service VARCHAR PRIMARY KEY NOT NULL,"
+ "encrypted_token BLOB)")) {
+ NOTREACHED();
+ return false;
+ }
+ }
+ return true;
+}
+
+bool TokenServiceTable::IsSyncable() {
+ return true;
+}
+
+bool TokenServiceTable::RemoveAllTokens() {
+ sql::Statement s(db_->GetUniqueStatement(
+ "DELETE FROM token_service"));
+ if (!s) {
+ NOTREACHED() << "Statement prepare failed";
+ return false;
+ }
+
+ return s.Run();
+}
+
+bool TokenServiceTable::SetTokenForService(const std::string& service,
+ const std::string& token) {
+ // Don't bother with a cached statement since this will be a relatively
+ // infrequent operation.
+ sql::Statement s(db_->GetUniqueStatement(
+ "INSERT OR REPLACE INTO token_service "
+ "(service, encrypted_token) VALUES (?, ?)"));
+ if (!s) {
+ NOTREACHED() << "Statement prepare failed";
+ return false;
+ }
+
+ std::string encrypted_token;
+
+ bool encrypted = Encryptor::EncryptString(token, &encrypted_token);
+ if (!encrypted) {
+ return false;
+ }
+
+ s.BindString(0, service);
+ s.BindBlob(1, encrypted_token.data(),
+ static_cast<int>(encrypted_token.length()));
+ return s.Run();
+}
+
+bool TokenServiceTable::GetAllTokens(
+ std::map<std::string, std::string>* tokens) {
+ sql::Statement s(db_->GetUniqueStatement(
+ "SELECT service, encrypted_token FROM token_service"));
+ if (!s) {
+ NOTREACHED() << "Statement prepare failed";
+ return false;
+ }
+
+ while (s.Step()) {
+ std::string encrypted_token;
+ std::string decrypted_token;
+ std::string service;
+ service = s.ColumnString(0);
+ bool entry_ok = !service.empty() &&
+ s.ColumnBlobAsString(1, &encrypted_token);
+ if (entry_ok) {
+ Encryptor::DecryptString(encrypted_token, &decrypted_token);
+ (*tokens)[service] = decrypted_token;
+ } else {
+ NOTREACHED();
+ return false;
+ }
+ }
+ return true;
+}
+