OLD | NEW |
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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 // This class isn't pretty. It's just a step better than globals, which is what | 5 // This class isn't pretty. It's just a step better than globals, which is what |
6 // these were previously. | 6 // these were previously. |
7 | 7 |
8 #include "chrome/browser/sync/util/user_settings.h" | 8 #include "chrome/browser/sync/util/user_settings.h" |
9 | 9 |
10 #include "build/build_config.h" | 10 #include "build/build_config.h" |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
62 c = c | (static_cast<unsigned char>(*i - 'A') << 4); | 62 c = c | (static_cast<unsigned char>(*i - 'A') << 4); |
63 result.push_back(c); | 63 result.push_back(c); |
64 } | 64 } |
65 return result; | 65 return result; |
66 } | 66 } |
67 | 67 |
68 static const char PASSWORD_HASH[] = "password_hash2"; | 68 static const char PASSWORD_HASH[] = "password_hash2"; |
69 static const char SALT[] = "salt2"; | 69 static const char SALT[] = "salt2"; |
70 | 70 |
71 static const int kSaltSize = 20; | 71 static const int kSaltSize = 20; |
72 static const int kCurrentDBVersion = 11; | 72 static const int kCurrentDBVersion = 12; |
73 | 73 |
74 UserSettings::ScopedDBHandle::ScopedDBHandle(UserSettings* settings) | 74 UserSettings::ScopedDBHandle::ScopedDBHandle(UserSettings* settings) |
75 : mutex_lock_(settings->dbhandle_mutex_), handle_(&settings->dbhandle_) { | 75 : mutex_lock_(settings->dbhandle_mutex_), handle_(&settings->dbhandle_) { |
76 } | 76 } |
77 | 77 |
78 UserSettings::UserSettings() : dbhandle_(NULL) { | 78 UserSettings::UserSettings() : dbhandle_(NULL) { |
79 } | 79 } |
80 | 80 |
81 string UserSettings::email() const { | 81 string UserSettings::email() const { |
82 AutoLock lock(mutex_); | 82 AutoLock lock(mutex_); |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
131 dst_syncdata_path = dst_syncdata_path.Append( | 131 dst_syncdata_path = dst_syncdata_path.Append( |
132 DirectoryManager::GetSyncDataDatabaseFilename()); | 132 DirectoryManager::GetSyncDataDatabaseFilename()); |
133 if (!file_util::Move(src_syncdata_path, dst_syncdata_path)) { | 133 if (!file_util::Move(src_syncdata_path, dst_syncdata_path)) { |
134 LOG(WARNING) << "Unable to upgrade UserSettings from v10"; | 134 LOG(WARNING) << "Unable to upgrade UserSettings from v10"; |
135 return; | 135 return; |
136 } | 136 } |
137 } | 137 } |
138 ExecOrDie(handle, "DROP TABLE shares"); | 138 ExecOrDie(handle, "DROP TABLE shares"); |
139 ExecOrDie(handle, "UPDATE db_version SET version = 11"); | 139 ExecOrDie(handle, "UPDATE db_version SET version = 11"); |
140 // FALL THROUGH | 140 // FALL THROUGH |
| 141 case 11: |
| 142 ExecOrDie(handle, "DROP TABLE signin_types"); |
| 143 ExecOrDie(handle, "UPDATE db_version SET version = 12"); |
| 144 // FALL THROUGH |
141 case kCurrentDBVersion: | 145 case kCurrentDBVersion: |
142 // Nothing to migrate. | 146 // Nothing to migrate. |
143 return; | 147 return; |
144 } | 148 } |
145 } | 149 } |
146 | 150 |
147 static void MakeCookiesTable(sqlite3* const dbhandle) { | 151 static void MakeCookiesTable(sqlite3* const dbhandle) { |
148 // This table keeps a list of auth tokens for each signed in account. There | 152 // This table keeps a list of auth tokens for each signed in account. There |
149 // will be as many rows as there are auth tokens per sign in. | 153 // will be as many rows as there are auth tokens per sign in. |
150 // The service_token column will store encrypted values. | 154 // The service_token column will store encrypted values. |
151 ExecOrDie(dbhandle, | 155 ExecOrDie(dbhandle, |
152 "CREATE TABLE cookies" | 156 "CREATE TABLE cookies" |
153 " (email, service_name, service_token, " | 157 " (email, service_name, service_token, " |
154 " PRIMARY KEY(email, service_name) ON CONFLICT REPLACE)"); | 158 " PRIMARY KEY(email, service_name) ON CONFLICT REPLACE)"); |
155 } | 159 } |
156 | 160 |
157 static void MakeSigninTypesTable(sqlite3* const dbhandle) { | |
158 // With every successful gaia authentication, remember if it was | |
159 // a hosted domain or not. | |
160 ExecOrDie(dbhandle, | |
161 "CREATE TABLE signin_types" | |
162 " (signin, signin_type, " | |
163 " PRIMARY KEY(signin, signin_type) ON CONFLICT REPLACE)"); | |
164 } | |
165 | |
166 static void MakeClientIDTable(sqlite3* const dbhandle) { | 161 static void MakeClientIDTable(sqlite3* const dbhandle) { |
167 // Stores a single client ID value that can be used as the client id, if | 162 // Stores a single client ID value that can be used as the client id, if |
168 // there's not another such ID provided on the install. | 163 // there's not another such ID provided on the install. |
169 ExecOrDie(dbhandle, "CREATE TABLE client_id (id) "); | 164 ExecOrDie(dbhandle, "CREATE TABLE client_id (id) "); |
170 { | 165 { |
171 SQLStatement statement; | 166 SQLStatement statement; |
172 statement.prepare(dbhandle, | 167 statement.prepare(dbhandle, |
173 "INSERT INTO client_id values ( ? )"); | 168 "INSERT INTO client_id values ( ? )"); |
174 statement.bind_string(0, Generate128BitRandomHexString()); | 169 statement.bind_string(0, Generate128BitRandomHexString()); |
175 if (SQLITE_DONE != statement.step()) { | 170 if (SQLITE_DONE != statement.step()) { |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
240 statement.prepare(dbhandle.get(), | 235 statement.prepare(dbhandle.get(), |
241 "INSERT INTO db_version values ( ? )"); | 236 "INSERT INTO db_version values ( ? )"); |
242 statement.bind_int(0, kCurrentDBVersion); | 237 statement.bind_int(0, kCurrentDBVersion); |
243 if (SQLITE_DONE != statement.step()) { | 238 if (SQLITE_DONE != statement.step()) { |
244 return false; | 239 return false; |
245 } | 240 } |
246 } | 241 } |
247 | 242 |
248 MakeSigninsTable(dbhandle.get()); | 243 MakeSigninsTable(dbhandle.get()); |
249 MakeCookiesTable(dbhandle.get()); | 244 MakeCookiesTable(dbhandle.get()); |
250 MakeSigninTypesTable(dbhandle.get()); | |
251 MakeClientIDTable(dbhandle.get()); | 245 MakeClientIDTable(dbhandle.get()); |
252 } | 246 } |
253 transaction.Commit(); | 247 transaction.Commit(); |
254 } | 248 } |
255 #if defined(OS_WIN) | 249 #if defined(OS_WIN) |
256 // Do not index this file. Scanning can occur every time we close the file, | 250 // Do not index this file. Scanning can occur every time we close the file, |
257 // which causes long delays in SQLite's file locking. | 251 // which causes long delays in SQLite's file locking. |
258 const DWORD attrs = GetFileAttributes(settings_path.value().c_str()); | 252 const DWORD attrs = GetFileAttributes(settings_path.value().c_str()); |
259 const BOOL attrs_set = | 253 const BOOL attrs_set = |
260 SetFileAttributes(settings_path.value().c_str(), | 254 SetFileAttributes(settings_path.value().c_str(), |
(...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
422 return hash == GetHashFromDigest(md5.GetDigest()); | 416 return hash == GetHashFromDigest(md5.GetDigest()); |
423 } | 417 } |
424 | 418 |
425 void UserSettings::SwitchUser(const string& username) { | 419 void UserSettings::SwitchUser(const string& username) { |
426 { | 420 { |
427 AutoLock lock(mutex_); | 421 AutoLock lock(mutex_); |
428 email_ = username; | 422 email_ = username; |
429 } | 423 } |
430 } | 424 } |
431 | 425 |
432 void UserSettings::RememberSigninType(const string& signin, | |
433 gaia::SignIn signin_type) { | |
434 ScopedDBHandle dbhandle(this); | |
435 SQLStatement statement; | |
436 statement.prepare(dbhandle.get(), | |
437 "INSERT INTO signin_types(signin, signin_type)" | |
438 " values ( ?, ? )"); | |
439 statement.bind_string(0, signin); | |
440 statement.bind_int(1, static_cast<int>(signin_type)); | |
441 if (SQLITE_DONE != statement.step()) { | |
442 LOG(FATAL) << sqlite3_errmsg(dbhandle.get()); | |
443 } | |
444 } | |
445 | |
446 gaia::SignIn UserSettings::RecallSigninType(const string& signin, | |
447 gaia::SignIn default_type) { | |
448 ScopedDBHandle dbhandle(this); | |
449 SQLStatement statement; | |
450 statement.prepare(dbhandle.get(), | |
451 "SELECT signin_type from signin_types WHERE signin = ?"); | |
452 statement.bind_string(0, signin); | |
453 int query_result = statement.step(); | |
454 if (SQLITE_ROW == query_result) { | |
455 int signin_type = statement.column_int(0); | |
456 return static_cast<gaia::SignIn>(signin_type); | |
457 } | |
458 return default_type; | |
459 } | |
460 | |
461 string UserSettings::GetClientId() { | 426 string UserSettings::GetClientId() { |
462 ScopedDBHandle dbhandle(this); | 427 ScopedDBHandle dbhandle(this); |
463 SQLStatement statement; | 428 SQLStatement statement; |
464 statement.prepare(dbhandle.get(), "SELECT id FROM client_id"); | 429 statement.prepare(dbhandle.get(), "SELECT id FROM client_id"); |
465 int query_result = statement.step(); | 430 int query_result = statement.step(); |
466 string client_id; | 431 string client_id; |
467 if (query_result == SQLITE_ROW) | 432 if (query_result == SQLITE_ROW) |
468 client_id = statement.column_string(0); | 433 client_id = statement.column_string(0); |
469 return client_id; | 434 return client_id; |
470 } | 435 } |
471 | 436 |
472 void UserSettings::ClearAllServiceTokens() { | 437 void UserSettings::ClearAllServiceTokens() { |
473 ScopedDBHandle dbhandle(this); | 438 ScopedDBHandle dbhandle(this); |
474 ExecOrDie(dbhandle.get(), "DELETE FROM cookies"); | 439 ExecOrDie(dbhandle.get(), "DELETE FROM cookies"); |
475 } | 440 } |
476 | 441 |
477 bool UserSettings::GetLastUser(string* username) { | 442 bool UserSettings::GetLastUser(string* username) { |
478 ScopedDBHandle dbhandle(this); | 443 ScopedDBHandle dbhandle(this); |
479 SQLStatement query; | 444 SQLStatement query; |
480 query.prepare(dbhandle.get(), "SELECT email FROM cookies"); | 445 query.prepare(dbhandle.get(), "SELECT email FROM cookies"); |
481 if (SQLITE_ROW == query.step()) { | 446 if (SQLITE_ROW == query.step()) { |
482 *username = query.column_string(0); | 447 *username = query.column_string(0); |
483 return true; | 448 return true; |
484 } else { | 449 } else { |
485 return false; | 450 return false; |
486 } | 451 } |
487 } | 452 } |
488 | 453 |
489 } // namespace browser_sync | 454 } // namespace browser_sync |
OLD | NEW |