1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef CHROME_BROWSER_SYNC_UTIL_USER_SETTINGS_H_ 6 #define CHROME_BROWSER_SYNC_UTIL_USER_SETTINGS_H_ 7 #pragma once 8 9 #include <map> 10 #include <string> 11 12 #include "base/synchronization/lock.h" 13 #include "build/build_config.h" 14 15 extern "C" struct sqlite3; 16 17 class FilePath; 18 19 namespace browser_sync { 20 21 void ExecOrDie(sqlite3* dbhandle, const char *query); 22 std::string APEncode(const std::string& in); 23 std::string APDecode(const std::string& in); 24 25 class URLFactory; 26 27 class UserSettings { 28 public: 29 // db_path is used for the main user settings. 30 // passwords_file contains hashes of passwords. 31 UserSettings(); 32 ~UserSettings(); 33 // Returns false (failure) if the db is a newer version. 34 bool Init(const FilePath& settings_path); 35 void StoreHashedPassword(const std::string& email, 36 const std::string& password); 37 bool VerifyAgainstStoredHash(const std::string& email, 38 const std::string& password); 39 40 // Set the username. 41 void SwitchUser(const std::string& email); 42 43 // Saves the email address and the named service token for the given user. 44 // Call this multiple times with the same email parameter to save multiple 45 // service tokens. 46 void SetAuthTokenForService(const std::string& email, 47 const std::string& service_name, 48 const std::string& long_lived_service_token); 49 // Erases all saved service tokens. 50 void ClearAllServiceTokens(); 51 52 // Returns the user name whose credentials have been persisted. 53 bool GetLastUser(std::string* username); 54 55 // Returns the user name whose credentials have been persisted as well as a 56 // service token for the named service 57 bool GetLastUserAndServiceToken(const std::string& service_name, 58 std::string* username, 59 std::string* service_token); 60 61 void RemoveAllGuestSettings(); 62 63 void StoreEmailForSignin(const std::string& signin, 64 const std::string& primary_email); 65 66 // Multiple email addresses can map to the same Google Account. This method 67 // returns the primary Google Account email associated with |signin|, which 68 // is used as both input and output. 69 bool GetEmailForSignin(std::string* signin); 70 71 std::string email() const; 72 73 // Get a unique ID suitable for use as the client ID. This ID has the 74 // lifetime of the user settings database. You may use this ID if your 75 // operating environment does not provide its own unique client ID. 76 std::string GetClientId(); 77 78 protected: 79 struct ScopedDBHandle { 80 explicit ScopedDBHandle(UserSettings* settings); 81 inline sqlite3* get() const { return *handle_; } 82 base::AutoLock mutex_lock_; 83 sqlite3** const handle_; 84 }; 85 86 friend struct ScopedDBHandle; 87 friend class URLFactory; 88 89 void MigrateOldVersionsAsNeeded(sqlite3* const handle, int current_version); 90 91 private: 92 std::string email_; 93 mutable base::Lock mutex_; // protects email_. 94 95 // We keep a single dbhandle. 96 sqlite3* dbhandle_; 97 base::Lock dbhandle_mutex_; 98 99 // TODO(sync): Use in-memory cache for service auth tokens on posix. 100 // Have someone competent in Windows switch it over to not use Sqlite in the 101 // future. 102 #ifndef OS_WIN 103 typedef std::map<std::string, std::string> ServiceTokenMap; 104 ServiceTokenMap service_tokens_; 105 #endif // OS_WIN 106 107 DISALLOW_COPY_AND_ASSIGN(UserSettings); 108 }; 109 110 } // namespace browser_sync 111 112 #endif // CHROME_BROWSER_SYNC_UTIL_USER_SETTINGS_H_ 113