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.
5 #ifndef CHROME_BROWSER_PASSWORD_MANAGER_PASSWORD_STORE_X_H_
6 #define CHROME_BROWSER_PASSWORD_MANAGER_PASSWORD_STORE_X_H_
10 #include "base/memory/scoped_ptr.h"
11 #include "base/time/time.h"
12 #include "components/password_manager/core/browser/password_store_default.h"
16 namespace user_prefs {
17 class PrefRegistrySyncable;
20 namespace password_manager {
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.
30 // There are currently native backends for GNOME Keyring and KWallet.
31 class PasswordStoreX : public password_manager::PasswordStoreDefault {
33 // NativeBackends more or less implement the PaswordStore interface, but
34 // with return values rather than implicit consumer notification.
37 typedef std::vector<autofill::PasswordForm*> PasswordFormList;
39 virtual ~NativeBackend() {}
41 virtual bool Init() = 0;
43 virtual bool AddLogin(const autofill::PasswordForm& form) = 0;
44 virtual bool UpdateLogin(const autofill::PasswordForm& form) = 0;
45 virtual bool RemoveLogin(const autofill::PasswordForm& form) = 0;
46 virtual bool RemoveLoginsCreatedBetween(const base::Time& delete_begin,
47 const base::Time& delete_end) = 0;
48 virtual bool GetLogins(const autofill::PasswordForm& form,
49 PasswordFormList* forms) = 0;
50 virtual bool GetLoginsCreatedBetween(const base::Time& get_begin,
51 const base::Time& get_end,
52 PasswordFormList* forms) = 0;
53 virtual bool GetAutofillableLogins(PasswordFormList* forms) = 0;
54 virtual bool GetBlacklistLogins(PasswordFormList* forms) = 0;
57 // Takes ownership of |login_db| and |backend|. |backend| may be NULL in which
58 // case this PasswordStoreX will act the same as PasswordStoreDefault.
59 PasswordStoreX(scoped_refptr<base::SingleThreadTaskRunner> main_thread_runner,
60 scoped_refptr<base::SingleThreadTaskRunner> db_thread_runner,
61 password_manager::LoginDatabase* login_db,
62 NativeBackend* backend);
65 friend class PasswordStoreXTest;
67 virtual ~PasswordStoreX();
69 // Implements PasswordStore interface.
70 virtual password_manager::PasswordStoreChangeList AddLoginImpl(
71 const autofill::PasswordForm& form) OVERRIDE;
72 virtual password_manager::PasswordStoreChangeList UpdateLoginImpl(
73 const autofill::PasswordForm& form) OVERRIDE;
74 virtual password_manager::PasswordStoreChangeList RemoveLoginImpl(
75 const autofill::PasswordForm& form) OVERRIDE;
76 virtual password_manager::PasswordStoreChangeList
77 RemoveLoginsCreatedBetweenImpl(const base::Time& delete_begin,
78 const base::Time& delete_end) OVERRIDE;
79 virtual void GetLoginsImpl(
80 const autofill::PasswordForm& form,
81 AuthorizationPromptPolicy prompt_policy,
82 const ConsumerCallbackRunner& callback_runner) OVERRIDE;
83 virtual void GetAutofillableLoginsImpl(GetLoginsRequest* request) OVERRIDE;
84 virtual void GetBlacklistLoginsImpl(GetLoginsRequest* request) OVERRIDE;
85 virtual bool FillAutofillableLogins(
86 std::vector<autofill::PasswordForm*>* forms) OVERRIDE;
87 virtual bool FillBlacklistLogins(
88 std::vector<autofill::PasswordForm*>* forms) OVERRIDE;
90 // Sort logins by origin, like the ORDER BY clause in login_database.cc.
91 void SortLoginsByOrigin(NativeBackend::PasswordFormList* list);
93 // Check to see whether migration is necessary, and perform it if so.
94 void CheckMigration();
96 // Return true if we should try using the native backend.
97 bool use_native_backend() { return !!backend_.get(); }
99 // Return true if we can fall back on the default store, warning the first
100 // time we call it when falling back is necessary. See |allow_fallback_|.
101 bool allow_default_store();
103 // Synchronously migrates all the passwords stored in the login database to
104 // the native backend. If successful, the login database will be left with no
105 // stored passwords, and the number of passwords migrated will be returned.
106 // (This might be 0 if migration was not necessary.) Returns < 0 on failure.
107 ssize_t MigrateLogins();
109 // The native backend in use, or NULL if none.
110 scoped_ptr<NativeBackend> backend_;
111 // Whether we have already attempted migration to the native store.
112 bool migration_checked_;
113 // Whether we should allow falling back to the default store. If there is
114 // nothing to migrate, then the first attempt to use the native store will
115 // be the first time we try to use it and we should allow falling back. If
116 // we have migrated successfully, then we do not allow falling back.
117 bool allow_fallback_;
119 DISALLOW_COPY_AND_ASSIGN(PasswordStoreX);
122 #endif // CHROME_BROWSER_PASSWORD_MANAGER_PASSWORD_STORE_X_H_