Home | History | Annotate | Download | only in password_manager
      1 // Copyright (c) 2012 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_PASSWORD_STORE_X_H_
      6 #define CHROME_BROWSER_PASSWORD_MANAGER_PASSWORD_STORE_X_H_
      7 
      8 #include <vector>
      9 
     10 #include "base/memory/scoped_ptr.h"
     11 #include "base/time/time.h"
     12 #include "components/password_manager/core/browser/password_store_default.h"
     13 
     14 class PrefService;
     15 
     16 namespace user_prefs {
     17 class PrefRegistrySyncable;
     18 }
     19 
     20 namespace password_manager {
     21 class LoginDatabase;
     22 }
     23 
     24 // PasswordStoreX is used on Linux and other non-Windows, non-Mac OS X
     25 // operating systems. It uses a "native backend" to actually store the password
     26 // data when such a backend is available, and otherwise falls back to using the
     27 // login database like PasswordStoreDefault. It also handles automatically
     28 // migrating password data to a native backend from the login database.
     29 //
     30 // There are currently native backends for GNOME Keyring and KWallet.
     31 class PasswordStoreX : public password_manager::PasswordStoreDefault {
     32  public:
     33   // NativeBackends more or less implement the PaswordStore interface, but
     34   // with return values rather than implicit consumer notification.
     35   class NativeBackend {
     36    public:
     37     typedef std::vector<autofill::PasswordForm*> PasswordFormList;
     38 
     39     virtual ~NativeBackend() {}
     40 
     41     virtual bool Init() = 0;
     42 
     43     virtual password_manager::PasswordStoreChangeList AddLogin(
     44         const autofill::PasswordForm& form) = 0;
     45     virtual bool UpdateLogin(
     46         const autofill::PasswordForm& form,
     47         password_manager::PasswordStoreChangeList* changes) = 0;
     48     virtual bool RemoveLogin(const autofill::PasswordForm& form) = 0;
     49 
     50     // Removes all logins created/synced from |delete_begin| onwards (inclusive)
     51     // and before |delete_end|. You may use a null Time value to do an unbounded
     52     // delete in either direction.
     53     virtual bool RemoveLoginsCreatedBetween(
     54         base::Time delete_begin,
     55         base::Time delete_end,
     56         password_manager::PasswordStoreChangeList* changes) = 0;
     57     virtual bool RemoveLoginsSyncedBetween(
     58         base::Time delete_begin,
     59         base::Time delete_end,
     60         password_manager::PasswordStoreChangeList* changes) = 0;
     61 
     62     virtual bool GetLogins(const autofill::PasswordForm& form,
     63                            PasswordFormList* forms) = 0;
     64     virtual bool GetAutofillableLogins(PasswordFormList* forms) = 0;
     65     virtual bool GetBlacklistLogins(PasswordFormList* forms) = 0;
     66   };
     67 
     68   // Takes ownership of |login_db| and |backend|. |backend| may be NULL in which
     69   // case this PasswordStoreX will act the same as PasswordStoreDefault.
     70   PasswordStoreX(scoped_refptr<base::SingleThreadTaskRunner> main_thread_runner,
     71                  scoped_refptr<base::SingleThreadTaskRunner> db_thread_runner,
     72                  password_manager::LoginDatabase* login_db,
     73                  NativeBackend* backend);
     74 
     75  private:
     76   friend class PasswordStoreXTest;
     77 
     78   virtual ~PasswordStoreX();
     79 
     80   // Implements PasswordStore interface.
     81   virtual password_manager::PasswordStoreChangeList AddLoginImpl(
     82       const autofill::PasswordForm& form) OVERRIDE;
     83   virtual password_manager::PasswordStoreChangeList UpdateLoginImpl(
     84       const autofill::PasswordForm& form) OVERRIDE;
     85   virtual password_manager::PasswordStoreChangeList RemoveLoginImpl(
     86       const autofill::PasswordForm& form) OVERRIDE;
     87   virtual password_manager::PasswordStoreChangeList
     88       RemoveLoginsCreatedBetweenImpl(base::Time delete_begin,
     89                                      base::Time delete_end) OVERRIDE;
     90   virtual password_manager::PasswordStoreChangeList
     91       RemoveLoginsSyncedBetweenImpl(base::Time delete_begin,
     92                                     base::Time delete_end) OVERRIDE;
     93   virtual void GetLoginsImpl(
     94       const autofill::PasswordForm& form,
     95       AuthorizationPromptPolicy prompt_policy,
     96       const ConsumerCallbackRunner& callback_runner) OVERRIDE;
     97   virtual void GetAutofillableLoginsImpl(GetLoginsRequest* request) OVERRIDE;
     98   virtual void GetBlacklistLoginsImpl(GetLoginsRequest* request) OVERRIDE;
     99   virtual bool FillAutofillableLogins(
    100       std::vector<autofill::PasswordForm*>* forms) OVERRIDE;
    101   virtual bool FillBlacklistLogins(
    102       std::vector<autofill::PasswordForm*>* forms) OVERRIDE;
    103 
    104   // Sort logins by origin, like the ORDER BY clause in login_database.cc.
    105   void SortLoginsByOrigin(NativeBackend::PasswordFormList* list);
    106 
    107   // Check to see whether migration is necessary, and perform it if so.
    108   void CheckMigration();
    109 
    110   // Return true if we should try using the native backend.
    111   bool use_native_backend() { return !!backend_.get(); }
    112 
    113   // Return true if we can fall back on the default store, warning the first
    114   // time we call it when falling back is necessary. See |allow_fallback_|.
    115   bool allow_default_store();
    116 
    117   // Synchronously migrates all the passwords stored in the login database to
    118   // the native backend. If successful, the login database will be left with no
    119   // stored passwords, and the number of passwords migrated will be returned.
    120   // (This might be 0 if migration was not necessary.) Returns < 0 on failure.
    121   ssize_t MigrateLogins();
    122 
    123   // The native backend in use, or NULL if none.
    124   scoped_ptr<NativeBackend> backend_;
    125   // Whether we have already attempted migration to the native store.
    126   bool migration_checked_;
    127   // Whether we should allow falling back to the default store. If there is
    128   // nothing to migrate, then the first attempt to use the native store will
    129   // be the first time we try to use it and we should allow falling back. If
    130   // we have migrated successfully, then we do not allow falling back.
    131   bool allow_fallback_;
    132 
    133   DISALLOW_COPY_AND_ASSIGN(PasswordStoreX);
    134 };
    135 
    136 #endif  // CHROME_BROWSER_PASSWORD_MANAGER_PASSWORD_STORE_X_H_
    137