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"
17 namespace user_prefs
{
18 class PrefRegistrySyncable
;
21 // PasswordStoreX is used on Linux and other non-Windows, non-Mac OS X
22 // operating systems. It uses a "native backend" to actually store the password
23 // data when such a backend is available, and otherwise falls back to using the
24 // login database like PasswordStoreDefault. It also handles automatically
25 // migrating password data to a native backend from the login database.
27 // There are currently native backends for GNOME Keyring and KWallet.
28 class PasswordStoreX
: public PasswordStoreDefault
{
30 // NativeBackends more or less implement the PaswordStore interface, but
31 // with return values rather than implicit consumer notification.
34 typedef std::vector
<autofill::PasswordForm
*> PasswordFormList
;
36 virtual ~NativeBackend() {}
38 virtual bool Init() = 0;
40 virtual bool AddLogin(const autofill::PasswordForm
& form
) = 0;
41 virtual bool UpdateLogin(const autofill::PasswordForm
& form
) = 0;
42 virtual bool RemoveLogin(const autofill::PasswordForm
& form
) = 0;
43 virtual bool RemoveLoginsCreatedBetween(const base::Time
& delete_begin
,
44 const base::Time
& delete_end
) = 0;
45 virtual bool GetLogins(const autofill::PasswordForm
& form
,
46 PasswordFormList
* forms
) = 0;
47 virtual bool GetLoginsCreatedBetween(const base::Time
& get_begin
,
48 const base::Time
& get_end
,
49 PasswordFormList
* forms
) = 0;
50 virtual bool GetAutofillableLogins(PasswordFormList
* forms
) = 0;
51 virtual bool GetBlacklistLogins(PasswordFormList
* forms
) = 0;
54 // Takes ownership of |login_db| and |backend|. |backend| may be NULL in which
55 // case this PasswordStoreX will act the same as PasswordStoreDefault.
56 PasswordStoreX(scoped_refptr
<base::SingleThreadTaskRunner
> main_thread_runner
,
57 scoped_refptr
<base::SingleThreadTaskRunner
> db_thread_runner
,
58 LoginDatabase
* login_db
,
59 NativeBackend
* backend
);
61 #if !defined(OS_MACOSX) && !defined(OS_CHROMEOS) && defined(OS_POSIX)
62 // Registers the pref setting used for the methods below.
63 static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable
* registry
);
65 // Returns true if passwords have been tagged with the local profile id.
66 static bool PasswordsUseLocalProfileId(PrefService
* prefs
);
68 // Sets the persistent bit indicating that passwords have been tagged with the
69 // local profile id. This cannot be unset; passwords get migrated only once.
70 // The caller promises that |prefs| will not be deleted any time soon.
71 static void SetPasswordsUseLocalProfileId(PrefService
* prefs
);
72 #endif // !defined(OS_MACOSX) && !defined(OS_CHROMEOS) && defined(OS_POSIX)
75 friend class PasswordStoreXTest
;
77 virtual ~PasswordStoreX();
79 // Implements PasswordStore interface.
80 virtual PasswordStoreChangeList
AddLoginImpl(
81 const autofill::PasswordForm
& form
) OVERRIDE
;
82 virtual PasswordStoreChangeList
UpdateLoginImpl(
83 const autofill::PasswordForm
& form
) OVERRIDE
;
84 virtual PasswordStoreChangeList
RemoveLoginImpl(
85 const autofill::PasswordForm
& form
) OVERRIDE
;
86 virtual PasswordStoreChangeList
RemoveLoginsCreatedBetweenImpl(
87 const base::Time
& delete_begin
, const base::Time
& delete_end
) OVERRIDE
;
88 virtual void GetLoginsImpl(
89 const autofill::PasswordForm
& form
,
90 AuthorizationPromptPolicy prompt_policy
,
91 const ConsumerCallbackRunner
& callback_runner
) OVERRIDE
;
92 virtual void GetAutofillableLoginsImpl(GetLoginsRequest
* request
) OVERRIDE
;
93 virtual void GetBlacklistLoginsImpl(GetLoginsRequest
* request
) OVERRIDE
;
94 virtual bool FillAutofillableLogins(
95 std::vector
<autofill::PasswordForm
*>* forms
) OVERRIDE
;
96 virtual bool FillBlacklistLogins(
97 std::vector
<autofill::PasswordForm
*>* forms
) OVERRIDE
;
99 // Sort logins by origin, like the ORDER BY clause in login_database.cc.
100 void SortLoginsByOrigin(NativeBackend::PasswordFormList
* list
);
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_