Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / components / password_manager / core / browser / login_database.h
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.
4
5 #ifndef COMPONENTS_PASSWORD_MANAGER_CORE_BROWSER_LOGIN_DATABASE_H_
6 #define COMPONENTS_PASSWORD_MANAGER_CORE_BROWSER_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 "components/password_manager/core/browser/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 namespace password_manager {
23
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.
27 class LoginDatabase {
28  public:
29   LoginDatabase();
30   virtual ~LoginDatabase();
31
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);
35
36   // Reports usage metrics to UMA.
37   void ReportMetrics();
38
39   // Adds |form| to the list of remembered password forms.
40   bool AddLogin(const autofill::PasswordForm& form);
41
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);
45
46   // Removes |form| from the list of remembered password forms.
47   bool RemoveLogin(const autofill::PasswordForm& form);
48
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);
54
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;
60
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
63   // direction.
64   bool GetLoginsCreatedBetween(
65       const base::Time begin,
66       const base::Time end,
67       std::vector<autofill::PasswordForm*>* forms) const;
68
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;
73
74   // Loads the complete list of blacklist forms into |forms|.
75   bool GetBlacklistLogins(
76       std::vector<autofill::PasswordForm*>* forms) const;
77
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();
84
85  private:
86   friend class LoginDatabaseTest;
87
88   // Result values for encryption/decryption actions.
89   enum EncryptionResult {
90     // Success.
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,
100   };
101
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;
108
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;
115
116   bool InitLoginsTable();
117   bool MigrateOldVersionsAsNeeded();
118
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;
125
126   // Loads all logins whose blacklist setting matches |blacklisted| into
127   // |forms|.
128   bool GetAllLoginsWithBlacklistSetting(
129       bool blacklisted, std::vector<autofill::PasswordForm*>* forms) const;
130
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;
134
135   base::FilePath db_path_;
136   mutable sql::Connection db_;
137   sql::MetaTable meta_table_;
138
139   PSLMatchingHelper psl_helper_;
140
141   DISALLOW_COPY_AND_ASSIGN(LoginDatabase);
142 };
143
144 }  // namespace password_manager
145
146 #endif  // COMPONENTS_PASSWORD_MANAGER_CORE_BROWSER_LOGIN_DATABASE_H_