1 // Copyright (c) 2011 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_PASSWORD_MANAGER_LOGIN_DATABASE_H_ 6 #define CHROME_BROWSER_PASSWORD_MANAGER_LOGIN_DATABASE_H_ 7 8 #include <string> 9 #include <vector> 10 11 #include "base/files/file_path.h" 12 #include "base/pickle.h" 13 #include "base/strings/string16.h" 14 #include "chrome/browser/password_manager/psl_matching_helper.h" 15 #include "sql/connection.h" 16 #include "sql/meta_table.h" 17 18 namespace autofill { 19 struct PasswordForm; 20 } // namespace autofill 21 22 // Interface to the database storage of login information, intended as a helper 23 // for PasswordStore on platforms that need internal storage of some or all of 24 // the login information. 25 class LoginDatabase { 26 public: 27 LoginDatabase(); 28 virtual ~LoginDatabase(); 29 30 // Initialize the database with an sqlite file at the given path. 31 // If false is returned, no other method should be called. 32 bool Init(const base::FilePath& db_path); 33 34 // Reports usage metrics to UMA. 35 void ReportMetrics(); 36 37 // Adds |form| to the list of remembered password forms. 38 bool AddLogin(const autofill::PasswordForm& form); 39 40 // Updates remembered password form. Returns true on success and sets 41 // items_changed (if non-NULL) to the number of logins updated. 42 bool UpdateLogin(const autofill::PasswordForm& form, int* items_changed); 43 44 // Removes |form| from the list of remembered password forms. 45 bool RemoveLogin(const autofill::PasswordForm& form); 46 47 // Removes all logins created from |delete_begin| onwards (inclusive) and 48 // before |delete_end|. You may use a null Time value to do an unbounded 49 // delete in either direction. 50 bool RemoveLoginsCreatedBetween(const base::Time delete_begin, 51 const base::Time delete_end); 52 53 // Loads a list of matching password forms into the specified vector |forms|. 54 // The list will contain all possibly relevant entries to the observed |form|, 55 // including blacklisted matches. 56 bool GetLogins(const autofill::PasswordForm& form, 57 std::vector<autofill::PasswordForm*>* forms) const; 58 59 // Loads all logins created from |begin| onwards (inclusive) and before |end|. 60 // You may use a null Time value to do an unbounded search in either 61 // direction. 62 bool GetLoginsCreatedBetween( 63 const base::Time begin, 64 const base::Time end, 65 std::vector<autofill::PasswordForm*>* forms) const; 66 67 // Loads the complete list of autofillable password forms (i.e., not blacklist 68 // entries) into |forms|. 69 bool GetAutofillableLogins( 70 std::vector<autofill::PasswordForm*>* forms) const; 71 72 // Loads the complete list of blacklist forms into |forms|. 73 bool GetBlacklistLogins( 74 std::vector<autofill::PasswordForm*>* forms) const; 75 76 // Deletes the login database file on disk, and creates a new, empty database. 77 // This can be used after migrating passwords to some other store, to ensure 78 // that SQLite doesn't leave fragments of passwords in the database file. 79 // Returns true on success; otherwise, whether the file was deleted and 80 // whether further use of this login database will succeed is unspecified. 81 bool DeleteAndRecreateDatabaseFile(); 82 83 private: 84 friend class LoginDatabaseTest; 85 86 // Result values for encryption/decryption actions. 87 enum EncryptionResult { 88 // Success. 89 ENCRYPTION_RESULT_SUCCESS, 90 // Failure for a specific item (e.g., the encrypted value was manually 91 // moved from another machine, and can't be decrypted on this machine). 92 // This is presumed to be a permanent failure. 93 ENCRYPTION_RESULT_ITEM_FAILURE, 94 // A service-level failure (e.g., on a platform using a keyring, the keyring 95 // is temporarily unavailable). 96 // This is presumed to be a temporary failure. 97 ENCRYPTION_RESULT_SERVICE_FAILURE, 98 }; 99 100 // Encrypts plain_text, setting the value of cipher_text and returning true if 101 // successful, or returning false and leaving cipher_text unchanged if 102 // encryption fails (e.g., if the underlying OS encryption system is 103 // temporarily unavailable). 104 EncryptionResult EncryptedString(const base::string16& plain_text, 105 std::string* cipher_text) const; 106 107 // Decrypts cipher_text, setting the value of plain_text and returning true if 108 // successful, or returning false and leaving plain_text unchanged if 109 // decryption fails (e.g., if the underlying OS encryption system is 110 // temporarily unavailable). 111 EncryptionResult DecryptedString(const std::string& cipher_text, 112 base::string16* plain_text) const; 113 114 bool InitLoginsTable(); 115 bool MigrateOldVersionsAsNeeded(); 116 117 // Fills |form| from the values in the given statement (which is assumed to 118 // be of the form used by the Get*Logins methods). 119 // Returns the EncryptionResult from decrypting the password in |s|; if not 120 // ENCRYPTION_RESULT_SUCCESS, |form| is not filled. 121 EncryptionResult InitPasswordFormFromStatement(autofill::PasswordForm* form, 122 sql::Statement& s) const; 123 124 // Loads all logins whose blacklist setting matches |blacklisted| into 125 // |forms|. 126 bool GetAllLoginsWithBlacklistSetting( 127 bool blacklisted, std::vector<autofill::PasswordForm*>* forms) const; 128 129 // Serialization routines for vectors. 130 Pickle SerializeVector(const std::vector<base::string16>& vec) const; 131 std::vector<base::string16> DeserializeVector(const Pickle& pickle) const; 132 133 base::FilePath db_path_; 134 mutable sql::Connection db_; 135 sql::MetaTable meta_table_; 136 137 PSLMatchingHelper psl_helper_; 138 139 DISALLOW_COPY_AND_ASSIGN(LoginDatabase); 140 }; 141 142 #endif // CHROME_BROWSER_PASSWORD_MANAGER_LOGIN_DATABASE_H_ 143