summaryrefslogtreecommitdiffstats
path: root/chrome/browser/password_manager/login_database.h
blob: 3db5ce3ac45dd9c7caca176fd3cbf0fbcc9b8dc0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
// Copyright (c) 2009 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROME_BROWSER_PASSWORD_MANAGER_LOGIN_DATABASE_H_
#define CHROME_BROWSER_PASSWORD_MANAGER_LOGIN_DATABASE_H_

#include <string>
#include <vector>

#include "base/string16.h"
#include "chrome/browser/meta_table_helper.h"
#include "webkit/glue/password_form.h"

class FilePath;
struct sqlite3;

// Base class for database storage of login information, intended as a helper
// for PasswordStore on platforms that need internal storage of some or all of
// the login information.
// Subclasses need to provide EncryptedString and DecryptedString
// implementations, which will be used to encrypt the password in the database.
class LoginDatabase {
 public:
  LoginDatabase();
  virtual ~LoginDatabase();

  // Initialize the database with an sqlite file at the given path.
  // If false is returned, no other method should be called.
  bool Init(const FilePath& db_path);

  // Adds |form| to the list of remembered password forms.
  bool AddLogin(const webkit_glue::PasswordForm& form);

  // Updates remembered password form. Returns true on success and sets
  // items_changed (if non-NULL) to the number of logins updated.
  bool UpdateLogin(const webkit_glue::PasswordForm& form, int* items_changed);

  // Removes |form| from the list of remembered password forms.
  bool RemoveLogin(const webkit_glue::PasswordForm& form);

  // Removes all logins created from |delete_begin| onwards (inclusive) and
  // before |delete_end|. You may use a null Time value to do an unbounded
  // delete in either direction.
  bool RemoveLoginsCreatedBetween(const base::Time delete_begin,
                                  const base::Time delete_end);

  // Loads a list of matching password forms into the specified vector |forms|.
  // The list will contain all possibly relevant entries to the observed |form|,
  // including blacklisted matches.
  bool GetLogins(const webkit_glue::PasswordForm& form,
                 std::vector<webkit_glue::PasswordForm*>* forms) const;

  // Loads the complete list of autofillable password forms (i.e., not blacklist
  // entries) into |forms|.
  bool GetAutofillableLogins(
      std::vector<webkit_glue::PasswordForm*>* forms) const;

  // Loads the complete list of blacklist forms into |forms|.
  bool GetBlacklistLogins(
      std::vector<webkit_glue::PasswordForm*>* forms) const;

 protected:
  // Returns an encrypted version of plain_text.
  virtual std::string EncryptedString(const string16& plain_text) const = 0;

  // Returns a decrypted version of cipher_text.
  virtual string16 DecryptedString(const std::string& cipher_text)
      const = 0;

  bool InitLoginsTable();
  void MigrateOldVersionsAsNeeded();

 private:
  // Fills |form| from the values in the given statement (which is assumed to
  // be of the form used by the Get*Logins methods).
  void InitPasswordFormFromStatement(webkit_glue::PasswordForm* form,
                                     SQLStatement* s) const;

  // Loads all logins whose blacklist setting matches |blacklisted| into
  // |forms|.
  bool GetAllLoginsWithBlacklistSetting(
      bool blacklisted, std::vector<webkit_glue::PasswordForm*>* forms) const;

  sqlite3* db_;
  MetaTableHelper meta_table_;

  DISALLOW_COPY_AND_ASSIGN(LoginDatabase);
};


#endif  // CHROME_BROWSER_PASSWORD_MANAGER_LOGIN_DATABASE_H_