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 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;
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;
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;
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);
76 friend class PasswordStoreXTest;
78 ~PasswordStoreX() override;
80 // Implements PasswordStore interface.
81 password_manager::PasswordStoreChangeList AddLoginImpl(
82 const autofill::PasswordForm& form) override;
83 password_manager::PasswordStoreChangeList UpdateLoginImpl(
84 const autofill::PasswordForm& form) override;
85 password_manager::PasswordStoreChangeList RemoveLoginImpl(
86 const autofill::PasswordForm& form) override;
87 password_manager::PasswordStoreChangeList RemoveLoginsCreatedBetweenImpl(
88 base::Time delete_begin,
89 base::Time delete_end) override;
90 password_manager::PasswordStoreChangeList RemoveLoginsSyncedBetweenImpl(
91 base::Time delete_begin,
92 base::Time delete_end) override;
93 void GetLoginsImpl(const autofill::PasswordForm& form,
94 AuthorizationPromptPolicy prompt_policy,
95 const ConsumerCallbackRunner& callback_runner) override;
96 void GetAutofillableLoginsImpl(GetLoginsRequest* request) override;
97 void GetBlacklistLoginsImpl(GetLoginsRequest* request) override;
98 bool FillAutofillableLogins(
99 std::vector<autofill::PasswordForm*>* forms) override;
100 bool FillBlacklistLogins(
101 std::vector<autofill::PasswordForm*>* forms) override;
103 // Sort logins by origin, like the ORDER BY clause in login_database.cc.
104 void SortLoginsByOrigin(NativeBackend::PasswordFormList* list);
106 // Check to see whether migration is necessary, and perform it if so.
107 void CheckMigration();
109 // Return true if we should try using the native backend.
110 bool use_native_backend() { return !!backend_.get(); }
112 // Return true if we can fall back on the default store, warning the first
113 // time we call it when falling back is necessary. See |allow_fallback_|.
114 bool allow_default_store();
116 // Synchronously migrates all the passwords stored in the login database to
117 // the native backend. If successful, the login database will be left with no
118 // stored passwords, and the number of passwords migrated will be returned.
119 // (This might be 0 if migration was not necessary.) Returns < 0 on failure.
120 ssize_t MigrateLogins();
122 // The native backend in use, or NULL if none.
123 scoped_ptr<NativeBackend> backend_;
124 // Whether we have already attempted migration to the native store.
125 bool migration_checked_;
126 // Whether we should allow falling back to the default store. If there is
127 // nothing to migrate, then the first attempt to use the native store will
128 // be the first time we try to use it and we should allow falling back. If
129 // we have migrated successfully, then we do not allow falling back.
130 bool allow_fallback_;
132 DISALLOW_COPY_AND_ASSIGN(PasswordStoreX);
135 #endif // CHROME_BROWSER_PASSWORD_MANAGER_PASSWORD_STORE_X_H_