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