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