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 // Updates |form| and appends the changes to |changes|. |changes| shouldn't
45 // be null. Returns false iff the operation failed due to a system backend
47 virtual bool UpdateLogin(
48 const autofill::PasswordForm
& form
,
49 password_manager::PasswordStoreChangeList
* changes
) = 0;
50 // Removes |form| and appends the changes to |changes|. |changes| shouldn't
51 // be null. Returns false iff the operation failed due to a system backend
53 virtual bool RemoveLogin(
54 const autofill::PasswordForm
& form
,
55 password_manager::PasswordStoreChangeList
* changes
) = 0;
57 // Removes all logins created/synced from |delete_begin| onwards (inclusive)
58 // and before |delete_end|. You may use a null Time value to do an unbounded
59 // delete in either direction.
60 virtual bool RemoveLoginsCreatedBetween(
61 base::Time delete_begin
,
62 base::Time delete_end
,
63 password_manager::PasswordStoreChangeList
* changes
) = 0;
64 virtual bool RemoveLoginsSyncedBetween(
65 base::Time delete_begin
,
66 base::Time delete_end
,
67 password_manager::PasswordStoreChangeList
* changes
) = 0;
69 // The three methods below overwrite |forms| with all stored credentials
70 // matching |form|, all stored non-blacklisted credentials, and all stored
71 // blacklisted credentials, respectively. On success, they return true.
72 virtual bool GetLogins(const autofill::PasswordForm
& form
,
73 ScopedVector
<autofill::PasswordForm
>* forms
)
74 WARN_UNUSED_RESULT
= 0;
75 virtual bool GetAutofillableLogins(
76 ScopedVector
<autofill::PasswordForm
>* forms
) WARN_UNUSED_RESULT
= 0;
77 virtual bool GetBlacklistLogins(ScopedVector
<autofill::PasswordForm
>* forms
)
78 WARN_UNUSED_RESULT
= 0;
81 // Takes ownership of |login_db| and |backend|. |backend| may be NULL in which
82 // case this PasswordStoreX will act the same as PasswordStoreDefault.
83 PasswordStoreX(scoped_refptr
<base::SingleThreadTaskRunner
> main_thread_runner
,
84 scoped_refptr
<base::SingleThreadTaskRunner
> db_thread_runner
,
85 scoped_ptr
<password_manager::LoginDatabase
> login_db
,
86 NativeBackend
* backend
);
89 friend class PasswordStoreXTest
;
91 ~PasswordStoreX() override
;
93 // Implements PasswordStore interface.
94 password_manager::PasswordStoreChangeList
AddLoginImpl(
95 const autofill::PasswordForm
& form
) override
;
96 password_manager::PasswordStoreChangeList
UpdateLoginImpl(
97 const autofill::PasswordForm
& form
) override
;
98 password_manager::PasswordStoreChangeList
RemoveLoginImpl(
99 const autofill::PasswordForm
& form
) override
;
100 password_manager::PasswordStoreChangeList
RemoveLoginsCreatedBetweenImpl(
101 base::Time delete_begin
,
102 base::Time delete_end
) override
;
103 password_manager::PasswordStoreChangeList
RemoveLoginsSyncedBetweenImpl(
104 base::Time delete_begin
,
105 base::Time delete_end
) override
;
106 ScopedVector
<autofill::PasswordForm
> FillMatchingLogins(
107 const autofill::PasswordForm
& form
,
108 AuthorizationPromptPolicy prompt_policy
) override
;
109 bool FillAutofillableLogins(
110 ScopedVector
<autofill::PasswordForm
>* forms
) override
;
111 bool FillBlacklistLogins(
112 ScopedVector
<autofill::PasswordForm
>* forms
) override
;
114 // Check to see whether migration is necessary, and perform it if so.
115 void CheckMigration();
117 // Return true if we should try using the native backend.
118 bool use_native_backend() { return !!backend_
.get(); }
120 // Return true if we can fall back on the default store, warning the first
121 // time we call it when falling back is necessary. See |allow_fallback_|.
122 bool allow_default_store();
124 // Synchronously migrates all the passwords stored in the login database to
125 // the native backend. If successful, the login database will be left with no
126 // stored passwords, and the number of passwords migrated will be returned.
127 // (This might be 0 if migration was not necessary.) Returns < 0 on failure.
128 ssize_t
MigrateLogins();
130 // The native backend in use, or NULL if none.
131 scoped_ptr
<NativeBackend
> backend_
;
132 // Whether we have already attempted migration to the native store.
133 bool migration_checked_
;
134 // Whether we should allow falling back to the default store. If there is
135 // nothing to migrate, then the first attempt to use the native store will
136 // be the first time we try to use it and we should allow falling back. If
137 // we have migrated successfully, then we do not allow falling back.
138 bool allow_fallback_
;
140 DISALLOW_COPY_AND_ASSIGN(PasswordStoreX
);
143 #endif // CHROME_BROWSER_PASSWORD_MANAGER_PASSWORD_STORE_X_H_