login_database.h revision c407dc5cd9bdc5668497f21b26b09d988ab439de
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_PASSWORD_MANAGER_LOGIN_DATABASE_H_
6#define CHROME_BROWSER_PASSWORD_MANAGER_LOGIN_DATABASE_H_
7
8#include <string>
9#include <vector>
10
11#include "app/sql/connection.h"
12#include "app/sql/meta_table.h"
13#include "base/file_path.h"
14#include "base/string16.h"
15#include "webkit/glue/password_form.h"
16
17struct sqlite3;
18
19// Interface to the database storage of login information, intended as a helper
20// for PasswordStore on platforms that need internal storage of some or all of
21// the login information.
22class LoginDatabase {
23 public:
24  LoginDatabase();
25  virtual ~LoginDatabase();
26
27  // Initialize the database with an sqlite file at the given path.
28  // If false is returned, no other method should be called.
29  bool Init(const FilePath& db_path);
30
31  // Reports usage metrics to UMA.
32  void ReportMetrics();
33
34  // Adds |form| to the list of remembered password forms.
35  bool AddLogin(const webkit_glue::PasswordForm& form);
36
37  // Updates remembered password form. Returns true on success and sets
38  // items_changed (if non-NULL) to the number of logins updated.
39  bool UpdateLogin(const webkit_glue::PasswordForm& form, int* items_changed);
40
41  // Removes |form| from the list of remembered password forms.
42  bool RemoveLogin(const webkit_glue::PasswordForm& form);
43
44  // Removes all logins created from |delete_begin| onwards (inclusive) and
45  // before |delete_end|. You may use a null Time value to do an unbounded
46  // delete in either direction.
47  bool RemoveLoginsCreatedBetween(const base::Time delete_begin,
48                                  const base::Time delete_end);
49
50  // Loads a list of matching password forms into the specified vector |forms|.
51  // The list will contain all possibly relevant entries to the observed |form|,
52  // including blacklisted matches.
53  bool GetLogins(const webkit_glue::PasswordForm& form,
54                 std::vector<webkit_glue::PasswordForm*>* forms) const;
55
56  // Loads all logins created from |begin| onwards (inclusive) and before |end|.
57  // You may use a null Time value to do an unbounded search in either
58  // direction.
59  bool GetLoginsCreatedBetween(
60      const base::Time begin,
61      const base::Time end,
62      std::vector<webkit_glue::PasswordForm*>* forms) const;
63
64  // Loads the complete list of autofillable password forms (i.e., not blacklist
65  // entries) into |forms|.
66  bool GetAutofillableLogins(
67      std::vector<webkit_glue::PasswordForm*>* forms) const;
68
69  // Loads the complete list of blacklist forms into |forms|.
70  bool GetBlacklistLogins(std::vector<webkit_glue::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  // Returns an encrypted version of plain_text.
81  std::string EncryptedString(const string16& plain_text) const;
82
83  // Returns a decrypted version of cipher_text.
84  string16 DecryptedString(const std::string& cipher_text) const;
85
86  bool InitLoginsTable();
87  void MigrateOldVersionsAsNeeded();
88
89  // Fills |form| from the values in the given statement (which is assumed to
90  // be of the form used by the Get*Logins methods).
91  void InitPasswordFormFromStatement(webkit_glue::PasswordForm* form,
92                                     sql::Statement& s) const;
93
94  // Loads all logins whose blacklist setting matches |blacklisted| into
95  // |forms|.
96  bool GetAllLoginsWithBlacklistSetting(
97      bool blacklisted, std::vector<webkit_glue::PasswordForm*>* forms) const;
98
99  FilePath db_path_;
100  mutable sql::Connection db_;
101  sql::MetaTable meta_table_;
102
103  DISALLOW_COPY_AND_ASSIGN(LoginDatabase);
104};
105
106#endif  // CHROME_BROWSER_PASSWORD_MANAGER_LOGIN_DATABASE_H_
107