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_NATIVE_BACKEND_KWALLET_X_H_
6 #define CHROME_BROWSER_PASSWORD_MANAGER_NATIVE_BACKEND_KWALLET_X_H_
10 #include "base/basictypes.h"
11 #include "base/memory/ref_counted.h"
12 #include "base/memory/scoped_vector.h"
13 #include "base/time/time.h"
14 #include "chrome/browser/password_manager/password_store_factory.h"
15 #include "chrome/browser/password_manager/password_store_x.h"
16 #include "chrome/browser/profiles/profile.h"
34 // NativeBackend implementation using KWallet.
35 class NativeBackendKWallet
: public PasswordStoreX::NativeBackend
{
37 explicit NativeBackendKWallet(LocalProfileId id
);
39 ~NativeBackendKWallet() override
;
43 // Implements NativeBackend interface.
44 password_manager::PasswordStoreChangeList
AddLogin(
45 const autofill::PasswordForm
& form
) override
;
46 bool UpdateLogin(const autofill::PasswordForm
& form
,
47 password_manager::PasswordStoreChangeList
* changes
) override
;
48 bool RemoveLogin(const autofill::PasswordForm
& form
) override
;
49 bool RemoveLoginsCreatedBetween(
50 base::Time delete_begin
,
51 base::Time delete_end
,
52 password_manager::PasswordStoreChangeList
* changes
) override
;
53 bool RemoveLoginsSyncedBetween(
54 base::Time delete_begin
,
55 base::Time delete_end
,
56 password_manager::PasswordStoreChangeList
* changes
) override
;
57 bool GetLogins(const autofill::PasswordForm
& form
,
58 ScopedVector
<autofill::PasswordForm
>* forms
) override
;
59 bool GetAutofillableLogins(
60 ScopedVector
<autofill::PasswordForm
>* forms
) override
;
61 bool GetBlacklistLogins(ScopedVector
<autofill::PasswordForm
>* forms
) override
;
64 // Invalid handle returned by WalletHandle().
65 static const int kInvalidKWalletHandle
= -1;
67 // Internally used by Init(), but also for testing to provide a mock bus.
68 bool InitWithBus(scoped_refptr
<dbus::Bus
> optional_bus
);
70 // Deserializes a list of PasswordForms from the wallet.
71 static void DeserializeValue(const std::string
& signon_realm
,
73 ScopedVector
<autofill::PasswordForm
>* forms
);
77 INIT_SUCCESS
, // Init succeeded.
78 TEMPORARY_FAIL
, // Init failed, but might succeed after StartKWalletd().
79 PERMANENT_FAIL
// Init failed, and is not likely to work later either.
82 enum TimestampToCompare
{
89 InitResult
InitWallet();
90 void InitOnDBThread(scoped_refptr
<dbus::Bus
> optional_bus
,
91 base::WaitableEvent
* event
,
94 // Reads PasswordForms from the wallet that match the given signon_realm.
95 bool GetLoginsList(const std::string
& signon_realm
,
97 ScopedVector
<autofill::PasswordForm
>* forms
);
99 // Reads PasswordForms from the wallet with the given autofillability state.
100 bool GetLoginsList(bool autofillable
,
102 ScopedVector
<autofill::PasswordForm
>* forms
);
104 // Helper for some of the above GetLoginsList() methods.
105 bool GetAllLogins(int wallet_handle
,
106 ScopedVector
<autofill::PasswordForm
>* forms
);
108 // Writes a list of PasswordForms to the wallet with the given signon_realm.
109 // Overwrites any existing list for this signon_realm. Removes the entry if
110 // |forms| is empty. Returns true on success.
111 bool SetLoginsList(const std::vector
<autofill::PasswordForm
*>& forms
,
112 const std::string
& signon_realm
,
115 // Removes password created/synced in the time interval. Returns |true| if the
116 // operation succeeded. |changes| will contain the changes applied.
117 bool RemoveLoginsBetween(base::Time delete_begin
,
118 base::Time delete_end
,
119 TimestampToCompare date_to_compare
,
120 password_manager::PasswordStoreChangeList
* changes
);
122 // Opens the wallet and ensures that the "Chrome Form Data" folder exists.
123 // Returns kInvalidWalletHandle on error.
126 // Serializes a list of PasswordForms to be stored in the wallet.
127 static void SerializeValue(const std::vector
<autofill::PasswordForm
*>& forms
,
130 // Deserializes a list of PasswordForms from the wallet.
131 // |size_32| controls reading the size field within the pickle as 32 bits.
132 // We used to use Pickle::WriteSize() to write the number of password forms,
133 // but that has a different size on 32- and 64-bit systems. So, now we always
134 // write a 64-bit quantity, but we support trying to read it as either size
135 // when reading old pickles that fail to deserialize using the native size.
136 static bool DeserializeValueSize(const std::string
& signon_realm
,
137 const PickleIterator
& iter
,
141 ScopedVector
<autofill::PasswordForm
>* forms
);
143 // In case the fields in the pickle ever change, version them so we can try to
144 // read old pickles. (Note: do not eat old pickles past the expiration date.)
145 static const int kPickleVersion
= 6;
147 // Generates a profile-specific folder name based on profile_id_.
148 std::string
GetProfileSpecificFolderName() const;
150 // The local profile id, used to generate the folder name.
151 const LocalProfileId profile_id_
;
153 // The KWallet folder name, possibly based on the local profile id.
154 std::string folder_name_
;
156 // DBus handle for communication with klauncher and kwalletd.
157 scoped_refptr
<dbus::Bus
> session_bus_
;
158 // Object proxy for kwalletd. We do not own this.
159 dbus::ObjectProxy
* kwallet_proxy_
;
161 // The name of the wallet we've opened. Set during Init().
162 std::string wallet_name_
;
163 // The application name (e.g. "Chromium"), shown in KWallet auth dialogs.
164 const std::string app_name_
;
166 DISALLOW_COPY_AND_ASSIGN(NativeBackendKWallet
);
169 #endif // CHROME_BROWSER_PASSWORD_MANAGER_NATIVE_BACKEND_KWALLET_X_H_