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 bool FillAutofillableLogins(
98 ScopedVector
<autofill::PasswordForm
>* forms
) override
;
99 bool FillBlacklistLogins(
100 ScopedVector
<autofill::PasswordForm
>* forms
) override
;
102 // Check to see whether migration is necessary, and perform it if so.
103 void CheckMigration();
105 // Return true if we should try using the native backend.
106 bool use_native_backend() { return !!backend_
.get(); }
108 // Return true if we can fall back on the default store, warning the first
109 // time we call it when falling back is necessary. See |allow_fallback_|.
110 bool allow_default_store();
112 // Synchronously migrates all the passwords stored in the login database to
113 // the native backend. If successful, the login database will be left with no
114 // stored passwords, and the number of passwords migrated will be returned.
115 // (This might be 0 if migration was not necessary.) Returns < 0 on failure.
116 ssize_t
MigrateLogins();
118 // The native backend in use, or NULL if none.
119 scoped_ptr
<NativeBackend
> backend_
;
120 // Whether we have already attempted migration to the native store.
121 bool migration_checked_
;
122 // Whether we should allow falling back to the default store. If there is
123 // nothing to migrate, then the first attempt to use the native store will
124 // be the first time we try to use it and we should allow falling back. If
125 // we have migrated successfully, then we do not allow falling back.
126 bool allow_fallback_
;
128 DISALLOW_COPY_AND_ASSIGN(PasswordStoreX
);
131 #endif // CHROME_BROWSER_PASSWORD_MANAGER_PASSWORD_STORE_X_H_