Home | History | Annotate | Download | only in password_manager
      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 #pragma once
      8 
      9 #include <string>
     10 #include <vector>
     11 
     12 #include "app/sql/connection.h"
     13 #include "app/sql/meta_table.h"
     14 #include "base/file_path.h"
     15 #include "base/string16.h"
     16 #include "webkit/glue/password_form.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 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 webkit_glue::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 webkit_glue::PasswordForm& form, int* items_changed);
     39 
     40   // Removes |form| from the list of remembered password forms.
     41   bool RemoveLogin(const webkit_glue::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 webkit_glue::PasswordForm& form,
     53                  std::vector<webkit_glue::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<webkit_glue::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<webkit_glue::PasswordForm*>* forms) const;
     67 
     68   // Loads the complete list of blacklist forms into |forms|.
     69   bool GetBlacklistLogins(std::vector<webkit_glue::PasswordForm*>* forms) const;
     70 
     71   // Deletes the login database file on disk, and creates a new, empty database.
     72   // This can be used after migrating passwords to some other store, to ensure
     73   // that SQLite doesn't leave fragments of passwords in the database file.
     74   // Returns true on success; otherwise, whether the file was deleted and
     75   // whether further use of this login database will succeed is unspecified.
     76   bool DeleteAndRecreateDatabaseFile();
     77 
     78  private:
     79   // Returns an encrypted version of plain_text.
     80   std::string EncryptedString(const string16& plain_text) const;
     81 
     82   // Returns a decrypted version of cipher_text.
     83   string16 DecryptedString(const std::string& cipher_text) const;
     84 
     85   bool InitLoginsTable();
     86   void MigrateOldVersionsAsNeeded();
     87 
     88   // Fills |form| from the values in the given statement (which is assumed to
     89   // be of the form used by the Get*Logins methods).
     90   void InitPasswordFormFromStatement(webkit_glue::PasswordForm* form,
     91                                      sql::Statement& s) const;
     92 
     93   // Loads all logins whose blacklist setting matches |blacklisted| into
     94   // |forms|.
     95   bool GetAllLoginsWithBlacklistSetting(
     96       bool blacklisted, std::vector<webkit_glue::PasswordForm*>* forms) const;
     97 
     98   FilePath db_path_;
     99   mutable sql::Connection db_;
    100   sql::MetaTable meta_table_;
    101 
    102   DISALLOW_COPY_AND_ASSIGN(LoginDatabase);
    103 };
    104 
    105 #endif  // CHROME_BROWSER_PASSWORD_MANAGER_LOGIN_DATABASE_H_
    106