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 "chrome/browser/password_manager/password_store_default.h"
18 namespace user_prefs
{
19 class PrefRegistrySyncable
;
22 // PasswordStoreX is used on Linux and other non-Windows, non-Mac OS X
23 // operating systems. It uses a "native backend" to actually store the password
24 // data when such a backend is available, and otherwise falls back to using the
25 // login database like PasswordStoreDefault. It also handles automatically
26 // migrating password data to a native backend from the login database.
28 // There are currently native backends for GNOME Keyring and KWallet.
29 class PasswordStoreX
: public PasswordStoreDefault
{
31 // NativeBackends more or less implement the PaswordStore interface, but
32 // with return values rather than implicit consumer notification.
35 typedef std::vector
<autofill::PasswordForm
*> PasswordFormList
;
37 virtual ~NativeBackend() {}
39 virtual bool Init() = 0;
41 virtual bool AddLogin(const autofill::PasswordForm
& form
) = 0;
42 virtual bool UpdateLogin(const autofill::PasswordForm
& form
) = 0;
43 virtual bool RemoveLogin(const autofill::PasswordForm
& form
) = 0;
44 virtual bool RemoveLoginsCreatedBetween(const base::Time
& delete_begin
,
45 const base::Time
& delete_end
) = 0;
46 virtual bool GetLogins(const autofill::PasswordForm
& form
,
47 PasswordFormList
* forms
) = 0;
48 virtual bool GetLoginsCreatedBetween(const base::Time
& get_begin
,
49 const base::Time
& get_end
,
50 PasswordFormList
* forms
) = 0;
51 virtual bool GetAutofillableLogins(PasswordFormList
* forms
) = 0;
52 virtual bool GetBlacklistLogins(PasswordFormList
* forms
) = 0;
55 // Takes ownership of |login_db| and |backend|. |backend| may be NULL in which
56 // case this PasswordStoreX will act the same as PasswordStoreDefault.
57 PasswordStoreX(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 void AddLoginImpl(const autofill::PasswordForm
& form
) OVERRIDE
;
81 virtual void UpdateLoginImpl(
82 const autofill::PasswordForm
& form
) OVERRIDE
;
83 virtual void RemoveLoginImpl(
84 const autofill::PasswordForm
& form
) OVERRIDE
;
85 virtual void RemoveLoginsCreatedBetweenImpl(
86 const base::Time
& delete_begin
, const base::Time
& delete_end
) OVERRIDE
;
87 virtual void GetLoginsImpl(
88 const autofill::PasswordForm
& form
,
89 AuthorizationPromptPolicy prompt_policy
,
90 const ConsumerCallbackRunner
& callback_runner
) OVERRIDE
;
91 virtual void GetAutofillableLoginsImpl(GetLoginsRequest
* request
) OVERRIDE
;
92 virtual void GetBlacklistLoginsImpl(GetLoginsRequest
* request
) OVERRIDE
;
93 virtual bool FillAutofillableLogins(
94 std::vector
<autofill::PasswordForm
*>* forms
) OVERRIDE
;
95 virtual bool FillBlacklistLogins(
96 std::vector
<autofill::PasswordForm
*>* forms
) OVERRIDE
;
98 // Sort logins by origin, like the ORDER BY clause in login_database.cc.
99 void SortLoginsByOrigin(NativeBackend::PasswordFormList
* list
);
101 // Check to see whether migration is necessary, and perform it if so.
102 void CheckMigration();
104 // Return true if we should try using the native backend.
105 bool use_native_backend() { return !!backend_
.get(); }
107 // Return true if we can fall back on the default store, warning the first
108 // time we call it when falling back is necessary. See |allow_fallback_|.
109 bool allow_default_store();
111 // Synchronously migrates all the passwords stored in the login database to
112 // the native backend. If successful, the login database will be left with no
113 // stored passwords, and the number of passwords migrated will be returned.
114 // (This might be 0 if migration was not necessary.) Returns < 0 on failure.
115 ssize_t
MigrateLogins();
117 // The native backend in use, or NULL if none.
118 scoped_ptr
<NativeBackend
> backend_
;
119 // Whether we have already attempted migration to the native store.
120 bool migration_checked_
;
121 // Whether we should allow falling back to the default store. If there is
122 // nothing to migrate, then the first attempt to use the native store will
123 // be the first time we try to use it and we should allow falling back. If
124 // we have migrated successfully, then we do not allow falling back.
125 bool allow_fallback_
;
127 DISALLOW_COPY_AND_ASSIGN(PasswordStoreX
);
130 #endif // CHROME_BROWSER_PASSWORD_MANAGER_PASSWORD_STORE_X_H_