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