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 #include "chrome/browser/password_manager/native_backend_gnome_x.h"
8 #include <gnome-keyring.h>
14 #include "base/basictypes.h"
15 #include "base/logging.h"
16 #include "base/memory/scoped_ptr.h"
17 #include "base/metrics/histogram.h"
18 #include "base/strings/string_number_conversions.h"
19 #include "base/strings/string_piece.h"
20 #include "base/strings/string_util.h"
21 #include "base/strings/stringprintf.h"
22 #include "base/strings/utf_string_conversions.h"
23 #include "base/synchronization/waitable_event.h"
24 #include "base/time/time.h"
25 #include "components/autofill/core/common/password_form.h"
26 #include "components/password_manager/core/browser/psl_matching_helper.h"
27 #include "content/public/browser/browser_thread.h"
29 using autofill::PasswordForm
;
30 using base::UTF8ToUTF16
;
31 using base::UTF16ToUTF8
;
32 using content::BrowserThread
;
35 const int kMaxPossibleTimeTValue
= std::numeric_limits
<int>::max();
38 #define GNOME_KEYRING_DEFINE_POINTER(name) \
39 typeof(&::gnome_keyring_##name) GnomeKeyringLoader::gnome_keyring_##name;
40 GNOME_KEYRING_FOR_EACH_FUNC(GNOME_KEYRING_DEFINE_POINTER
)
41 #undef GNOME_KEYRING_DEFINE_POINTER
43 bool GnomeKeyringLoader::keyring_loaded
= false;
45 #if defined(DLOPEN_GNOME_KEYRING)
47 #define GNOME_KEYRING_FUNCTION_INFO(name) \
48 {"gnome_keyring_"#name, reinterpret_cast<void**>(&gnome_keyring_##name)},
49 const GnomeKeyringLoader::FunctionInfo
GnomeKeyringLoader::functions
[] = {
50 GNOME_KEYRING_FOR_EACH_FUNC(GNOME_KEYRING_FUNCTION_INFO
)
53 #undef GNOME_KEYRING_FUNCTION_INFO
55 /* Load the library and initialize the function pointers. */
56 bool GnomeKeyringLoader::LoadGnomeKeyring() {
60 void* handle
= dlopen("libgnome-keyring.so.0", RTLD_NOW
| RTLD_GLOBAL
);
62 // We wanted to use GNOME Keyring, but we couldn't load it. Warn, because
63 // either the user asked for this, or we autodetected it incorrectly. (Or
64 // the system has broken libraries, which is also good to warn about.)
65 LOG(WARNING
) << "Could not load libgnome-keyring.so.0: " << dlerror();
69 for (size_t i
= 0; functions
[i
].name
; ++i
) {
71 *functions
[i
].pointer
= dlsym(handle
, functions
[i
].name
);
72 const char* error
= dlerror();
74 LOG(ERROR
) << "Unable to load symbol "
75 << functions
[i
].name
<< ": " << error
;
81 keyring_loaded
= true;
82 // We leak the library handle. That's OK: this function is called only once.
86 #else // defined(DLOPEN_GNOME_KEYRING)
88 bool GnomeKeyringLoader::LoadGnomeKeyring() {
91 #define GNOME_KEYRING_ASSIGN_POINTER(name) \
92 gnome_keyring_##name = &::gnome_keyring_##name;
93 GNOME_KEYRING_FOR_EACH_FUNC(GNOME_KEYRING_ASSIGN_POINTER
)
94 #undef GNOME_KEYRING_ASSIGN_POINTER
95 keyring_loaded
= true;
99 #endif // defined(DLOPEN_GNOME_KEYRING)
103 const char kGnomeKeyringAppString
[] = "chrome";
105 // Convert the attributes of a given keyring entry into a new PasswordForm.
106 // Note: does *not* get the actual password, as that is not a key attribute!
107 // Returns NULL if the attributes are for the wrong application.
108 scoped_ptr
<PasswordForm
> FormFromAttributes(GnomeKeyringAttributeList
* attrs
) {
109 // Read the string and int attributes into the appropriate map.
110 std::map
<std::string
, std::string
> string_attr_map
;
111 std::map
<std::string
, uint32_t> uint_attr_map
;
112 for (guint i
= 0; i
< attrs
->len
; ++i
) {
113 GnomeKeyringAttribute attr
= gnome_keyring_attribute_list_index(attrs
, i
);
114 if (attr
.type
== GNOME_KEYRING_ATTRIBUTE_TYPE_STRING
)
115 string_attr_map
[attr
.name
] = attr
.value
.string
;
116 else if (attr
.type
== GNOME_KEYRING_ATTRIBUTE_TYPE_UINT32
)
117 uint_attr_map
[attr
.name
] = attr
.value
.integer
;
119 // Check to make sure this is a password we care about.
120 const std::string
& app_value
= string_attr_map
["application"];
121 if (!base::StringPiece(app_value
).starts_with(kGnomeKeyringAppString
))
122 return scoped_ptr
<PasswordForm
>();
124 scoped_ptr
<PasswordForm
> form(new PasswordForm());
125 form
->origin
= GURL(string_attr_map
["origin_url"]);
126 form
->action
= GURL(string_attr_map
["action_url"]);
127 form
->username_element
= UTF8ToUTF16(string_attr_map
["username_element"]);
128 form
->username_value
= UTF8ToUTF16(string_attr_map
["username_value"]);
129 form
->password_element
= UTF8ToUTF16(string_attr_map
["password_element"]);
130 form
->submit_element
= UTF8ToUTF16(string_attr_map
["submit_element"]);
131 form
->signon_realm
= string_attr_map
["signon_realm"];
132 form
->ssl_valid
= uint_attr_map
["ssl_valid"];
133 form
->preferred
= uint_attr_map
["preferred"];
134 int64 date_created
= 0;
135 bool date_ok
= base::StringToInt64(string_attr_map
["date_created"],
138 // In the past |date_created| was stored as time_t. Currently is stored as
139 // base::Time's internal value. We need to distinguish, which format the
140 // number in |date_created| was stored in. We use the fact that
141 // kMaxPossibleTimeTValue interpreted as the internal value corresponds to an
142 // unlikely date back in 17th century, and anything above
143 // kMaxPossibleTimeTValue clearly must be in the internal value format.
144 form
->date_created
= date_created
< kMaxPossibleTimeTValue
145 ? base::Time::FromTimeT(date_created
)
146 : base::Time::FromInternalValue(date_created
);
147 form
->blacklisted_by_user
= uint_attr_map
["blacklisted_by_user"];
148 form
->type
= static_cast<PasswordForm::Type
>(uint_attr_map
["type"]);
149 form
->times_used
= uint_attr_map
["times_used"];
150 form
->scheme
= static_cast<PasswordForm::Scheme
>(uint_attr_map
["scheme"]);
151 int64 date_synced
= 0;
152 base::StringToInt64(string_attr_map
["date_synced"], &date_synced
);
153 form
->date_synced
= base::Time::FromInternalValue(date_synced
);
154 form
->display_name
= UTF8ToUTF16(string_attr_map
["display_name"]);
155 form
->avatar_url
= GURL(string_attr_map
["avatar_url"]);
156 form
->federation_url
= GURL(string_attr_map
["federation_url"]);
157 form
->skip_zero_click
= uint_attr_map
["skip_zero_click"];
158 form
->generation_upload_status
=
159 static_cast<PasswordForm::GenerationUploadStatus
>(
160 uint_attr_map
["generation_upload_status"]);
161 DeserializeFormDataFromBase64String(string_attr_map
["form_data"],
166 // Converts native credentials in |found| to PasswordForms. If not NULL,
167 // |lookup_form| is used to filter out results -- only credentials with signon
168 // realms passing the PSL matching against |lookup_form->signon_realm| will be
169 // kept. PSL matched results get their signon_realm, origin, and action
170 // rewritten to those of |lookup_form_|, with the original signon_realm saved
171 // into the result's original_signon_realm data member.
172 ScopedVector
<PasswordForm
> ConvertFormList(GList
* found
,
173 const PasswordForm
* lookup_form
) {
174 ScopedVector
<PasswordForm
> forms
;
175 password_manager::PSLDomainMatchMetric psl_domain_match_metric
=
176 password_manager::PSL_DOMAIN_MATCH_NONE
;
177 for (GList
* element
= g_list_first(found
); element
;
178 element
= g_list_next(element
)) {
179 GnomeKeyringFound
* data
= static_cast<GnomeKeyringFound
*>(element
->data
);
180 GnomeKeyringAttributeList
* attrs
= data
->attributes
;
182 scoped_ptr
<PasswordForm
> form(FormFromAttributes(attrs
));
184 if (lookup_form
&& form
->signon_realm
!= lookup_form
->signon_realm
) {
185 // This is not an exact match, we try PSL matching.
186 if (lookup_form
->scheme
!= PasswordForm::SCHEME_HTML
||
187 form
->scheme
!= PasswordForm::SCHEME_HTML
||
188 !(password_manager::IsPublicSuffixDomainMatch(
189 lookup_form
->signon_realm
, form
->signon_realm
))) {
192 psl_domain_match_metric
= password_manager::PSL_DOMAIN_MATCH_FOUND
;
193 form
->original_signon_realm
= form
->signon_realm
;
194 form
->signon_realm
= lookup_form
->signon_realm
;
195 form
->origin
= lookup_form
->origin
;
196 form
->action
= lookup_form
->action
;
199 form
->password_value
= UTF8ToUTF16(data
->secret
);
201 LOG(WARNING
) << "Unable to access password from list element!";
203 forms
.push_back(form
.release());
205 LOG(WARNING
) << "Could not initialize PasswordForm from attributes!";
209 const GURL
signon_realm(lookup_form
->signon_realm
);
210 std::string registered_domain
=
211 password_manager::GetRegistryControlledDomain(signon_realm
);
212 UMA_HISTOGRAM_ENUMERATION(
213 "PasswordManager.PslDomainMatchTriggering",
214 password_manager::ShouldPSLDomainMatchingApply(registered_domain
)
215 ? psl_domain_match_metric
216 : password_manager::PSL_DOMAIN_MATCH_NOT_USED
,
217 password_manager::PSL_DOMAIN_MATCH_COUNT
);
222 // Schema is analagous to the fields in PasswordForm.
223 const GnomeKeyringPasswordSchema kGnomeSchema
= {
224 GNOME_KEYRING_ITEM_GENERIC_SECRET
, {
225 { "origin_url", GNOME_KEYRING_ATTRIBUTE_TYPE_STRING
},
226 { "action_url", GNOME_KEYRING_ATTRIBUTE_TYPE_STRING
},
227 { "username_element", GNOME_KEYRING_ATTRIBUTE_TYPE_STRING
},
228 { "username_value", GNOME_KEYRING_ATTRIBUTE_TYPE_STRING
},
229 { "password_element", GNOME_KEYRING_ATTRIBUTE_TYPE_STRING
},
230 { "submit_element", GNOME_KEYRING_ATTRIBUTE_TYPE_STRING
},
231 { "signon_realm", GNOME_KEYRING_ATTRIBUTE_TYPE_STRING
},
232 { "ssl_valid", GNOME_KEYRING_ATTRIBUTE_TYPE_UINT32
},
233 { "preferred", GNOME_KEYRING_ATTRIBUTE_TYPE_UINT32
},
234 { "date_created", GNOME_KEYRING_ATTRIBUTE_TYPE_STRING
},
235 { "blacklisted_by_user", GNOME_KEYRING_ATTRIBUTE_TYPE_UINT32
},
236 { "scheme", GNOME_KEYRING_ATTRIBUTE_TYPE_UINT32
},
237 { "type", GNOME_KEYRING_ATTRIBUTE_TYPE_UINT32
},
238 { "times_used", GNOME_KEYRING_ATTRIBUTE_TYPE_UINT32
},
239 { "date_synced", GNOME_KEYRING_ATTRIBUTE_TYPE_STRING
},
240 { "display_name", GNOME_KEYRING_ATTRIBUTE_TYPE_STRING
},
241 { "avatar_url", GNOME_KEYRING_ATTRIBUTE_TYPE_STRING
},
242 { "federation_url", GNOME_KEYRING_ATTRIBUTE_TYPE_STRING
},
243 { "skip_zero_click", GNOME_KEYRING_ATTRIBUTE_TYPE_UINT32
},
244 { "generation_upload_status", GNOME_KEYRING_ATTRIBUTE_TYPE_UINT32
},
245 { "form_data", GNOME_KEYRING_ATTRIBUTE_TYPE_STRING
},
246 // This field is always "chrome" so that we can search for it.
247 { "application", GNOME_KEYRING_ATTRIBUTE_TYPE_STRING
},
252 // Sadly, PasswordStore goes to great lengths to switch from the originally
253 // calling thread to the DB thread, and to provide an asynchronous API to
254 // callers while using a synchronous (virtual) API provided by subclasses like
255 // PasswordStoreX -- but GNOME Keyring really wants to be on the GLib main
256 // thread, which is the UI thread to us. So we end up having to switch threads
257 // again, possibly back to the very same thread (in case the UI thread is the
258 // caller, e.g. in the password management UI), and *block* the DB thread
259 // waiting for a response from the UI thread to provide the synchronous API
260 // PasswordStore expects of us. (It will then in turn switch back to the
261 // original caller to send the asynchronous reply to the original request.)
263 // This class represents a call to a GNOME Keyring method. A RunnableMethod
264 // should be posted to the UI thread to call one of its action methods, and then
265 // a WaitResult() method should be called to wait for the result. Each instance
266 // supports only one outstanding method at a time, though multiple instances may
267 // be used in parallel.
268 class GKRMethod
: public GnomeKeyringLoader
{
270 GKRMethod() : event_(false, false), result_(GNOME_KEYRING_RESULT_CANCELLED
) {}
272 // Action methods. These call gnome_keyring_* functions. Call from UI thread.
273 // See GetProfileSpecificAppString() for more information on the app string.
274 void AddLogin(const PasswordForm
& form
, const char* app_string
);
275 void AddLoginSearch(const PasswordForm
& form
, const char* app_string
);
276 void UpdateLoginSearch(const PasswordForm
& form
, const char* app_string
);
277 void RemoveLogin(const PasswordForm
& form
, const char* app_string
);
278 void GetLogins(const PasswordForm
& form
, const char* app_string
);
279 void GetLoginsList(uint32_t blacklisted_by_user
, const char* app_string
);
280 void GetAllLogins(const char* app_string
);
282 // Use after AddLogin, RemoveLogin.
283 GnomeKeyringResult
WaitResult();
285 // Use after AddLoginSearch, UpdateLoginSearch, GetLogins, GetLoginsList,
286 // GetAllLogins. Replaces the content of |forms| with found logins.
287 GnomeKeyringResult
WaitResult(ScopedVector
<PasswordForm
>* forms
);
290 struct GnomeKeyringAttributeListFreeDeleter
{
291 inline void operator()(void* list
) const {
292 gnome_keyring_attribute_list_free(
293 static_cast<GnomeKeyringAttributeList
*>(list
));
297 typedef scoped_ptr
<GnomeKeyringAttributeList
,
298 GnomeKeyringAttributeListFreeDeleter
> ScopedAttributeList
;
300 // Helper methods to abbreviate Gnome Keyring long API names.
301 static void AppendString(ScopedAttributeList
* list
,
304 static void AppendString(ScopedAttributeList
* list
,
306 const std::string
& value
);
307 static void AppendUint32(ScopedAttributeList
* list
,
311 // All these callbacks are called on UI thread.
312 static void OnOperationDone(GnomeKeyringResult result
, gpointer data
);
314 // This is marked as static, but acts on the GKRMethod instance that |data|
315 // points to. Saves |result| to |result_|. If the result is OK, overwrites
316 // |forms_| with the found credentials. Clears |forms_| otherwise.
317 static void OnOperationGetList(GnomeKeyringResult result
, GList
* list
,
320 base::WaitableEvent event_
;
321 GnomeKeyringResult result_
;
322 ScopedVector
<PasswordForm
> forms_
;
323 // If the credential search is specified by a single form and needs to use PSL
324 // matching, then the specifying form is stored in |lookup_form_|. If PSL
325 // matching is used to find a result, then the results signon realm, origin
326 // and action are stored are replaced by those of |lookup_form_|.
327 // Additionally, |lookup_form_->signon_realm| is also used to narrow down the
328 // found logins to those which indeed PSL-match the look-up. And finally,
329 // |lookup_form_| set to NULL means that PSL matching is not required.
330 scoped_ptr
<PasswordForm
> lookup_form_
;
333 void GKRMethod::AddLogin(const PasswordForm
& form
, const char* app_string
) {
334 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
335 int64 date_created
= form
.date_created
.ToInternalValue();
336 // If we are asked to save a password with 0 date, use the current time.
337 // We don't want to actually save passwords as though on January 1, 1601.
339 date_created
= base::Time::Now().ToInternalValue();
340 int64 date_synced
= form
.date_synced
.ToInternalValue();
341 std::string form_data
;
342 SerializeFormDataToBase64String(form
.form_data
, &form_data
);
343 gnome_keyring_store_password(
345 nullptr, // Default keyring.
346 form
.origin
.spec().c_str(), // Display name.
347 UTF16ToUTF8(form
.password_value
).c_str(),
350 nullptr, // destroy_data
351 "origin_url", form
.origin
.spec().c_str(),
352 "action_url", form
.action
.spec().c_str(),
353 "username_element", UTF16ToUTF8(form
.username_element
).c_str(),
354 "username_value", UTF16ToUTF8(form
.username_value
).c_str(),
355 "password_element", UTF16ToUTF8(form
.password_element
).c_str(),
356 "submit_element", UTF16ToUTF8(form
.submit_element
).c_str(),
357 "signon_realm", form
.signon_realm
.c_str(),
358 "ssl_valid", form
.ssl_valid
,
359 "preferred", form
.preferred
,
360 "date_created", base::Int64ToString(date_created
).c_str(),
361 "blacklisted_by_user", form
.blacklisted_by_user
,
363 "times_used", form
.times_used
,
364 "scheme", form
.scheme
,
365 "date_synced", base::Int64ToString(date_synced
).c_str(),
366 "display_name", UTF16ToUTF8(form
.display_name
).c_str(),
367 "avatar_url", form
.avatar_url
.spec().c_str(),
368 "federation_url", form
.federation_url
.spec().c_str(),
369 "skip_zero_click", form
.skip_zero_click
,
370 "generation_upload_status", form
.generation_upload_status
,
371 "form_data", form_data
.c_str(),
372 "application", app_string
,
376 void GKRMethod::AddLoginSearch(const PasswordForm
& form
,
377 const char* app_string
) {
378 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
379 lookup_form_
.reset(nullptr);
380 // Search GNOME Keyring for matching passwords to update.
381 ScopedAttributeList
attrs(gnome_keyring_attribute_list_new());
382 AppendString(&attrs
, "origin_url", form
.origin
.spec());
383 AppendString(&attrs
, "username_element", UTF16ToUTF8(form
.username_element
));
384 AppendString(&attrs
, "username_value", UTF16ToUTF8(form
.username_value
));
385 AppendString(&attrs
, "password_element", UTF16ToUTF8(form
.password_element
));
386 AppendString(&attrs
, "submit_element", UTF16ToUTF8(form
.submit_element
));
387 AppendString(&attrs
, "signon_realm", form
.signon_realm
);
388 AppendString(&attrs
, "application", app_string
);
389 gnome_keyring_find_items(GNOME_KEYRING_ITEM_GENERIC_SECRET
,
393 /*destroy_data=*/nullptr);
396 void GKRMethod::UpdateLoginSearch(const PasswordForm
& form
,
397 const char* app_string
) {
398 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
399 lookup_form_
.reset(nullptr);
400 // Search GNOME Keyring for matching passwords to update.
401 ScopedAttributeList
attrs(gnome_keyring_attribute_list_new());
402 AppendString(&attrs
, "origin_url", form
.origin
.spec());
403 AppendString(&attrs
, "username_element", UTF16ToUTF8(form
.username_element
));
404 AppendString(&attrs
, "username_value", UTF16ToUTF8(form
.username_value
));
405 AppendString(&attrs
, "password_element", UTF16ToUTF8(form
.password_element
));
406 AppendString(&attrs
, "signon_realm", form
.signon_realm
);
407 AppendString(&attrs
, "application", app_string
);
408 gnome_keyring_find_items(GNOME_KEYRING_ITEM_GENERIC_SECRET
,
412 /*destroy_data=*/nullptr);
415 void GKRMethod::RemoveLogin(const PasswordForm
& form
, const char* app_string
) {
416 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
417 // We find forms using the same fields as LoginDatabase::RemoveLogin().
418 gnome_keyring_delete_password(
422 nullptr, // destroy_data
423 "origin_url", form
.origin
.spec().c_str(),
424 "username_element", UTF16ToUTF8(form
.username_element
).c_str(),
425 "username_value", UTF16ToUTF8(form
.username_value
).c_str(),
426 "password_element", UTF16ToUTF8(form
.password_element
).c_str(),
427 "submit_element", UTF16ToUTF8(form
.submit_element
).c_str(),
428 "signon_realm", form
.signon_realm
.c_str(),
429 "application", app_string
,
433 void GKRMethod::GetLogins(const PasswordForm
& form
, const char* app_string
) {
434 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
435 lookup_form_
.reset(new PasswordForm(form
));
436 // Search GNOME Keyring for matching passwords.
437 ScopedAttributeList
attrs(gnome_keyring_attribute_list_new());
438 if (!password_manager::ShouldPSLDomainMatchingApply(
439 password_manager::GetRegistryControlledDomain(
440 GURL(form
.signon_realm
)))) {
441 AppendString(&attrs
, "signon_realm", form
.signon_realm
);
443 AppendString(&attrs
, "application", app_string
);
444 gnome_keyring_find_items(GNOME_KEYRING_ITEM_GENERIC_SECRET
,
448 /*destroy_data=*/nullptr);
451 void GKRMethod::GetLoginsList(uint32_t blacklisted_by_user
,
452 const char* app_string
) {
453 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
454 lookup_form_
.reset(nullptr);
455 // Search GNOME Keyring for matching passwords.
456 ScopedAttributeList
attrs(gnome_keyring_attribute_list_new());
457 AppendUint32(&attrs
, "blacklisted_by_user", blacklisted_by_user
);
458 AppendString(&attrs
, "application", app_string
);
459 gnome_keyring_find_items(GNOME_KEYRING_ITEM_GENERIC_SECRET
,
463 /*destroy_data=*/nullptr);
466 void GKRMethod::GetAllLogins(const char* app_string
) {
467 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
468 lookup_form_
.reset(nullptr);
469 // We need to search for something, otherwise we get no results - so
470 // we search for the fixed application string.
471 ScopedAttributeList
attrs(gnome_keyring_attribute_list_new());
472 AppendString(&attrs
, "application", app_string
);
473 gnome_keyring_find_items(GNOME_KEYRING_ITEM_GENERIC_SECRET
,
477 /*destroy_data=*/nullptr);
480 GnomeKeyringResult
GKRMethod::WaitResult() {
481 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB
));
486 GnomeKeyringResult
GKRMethod::WaitResult(ScopedVector
<PasswordForm
>* forms
) {
487 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB
));
489 *forms
= forms_
.Pass();
494 void GKRMethod::AppendString(GKRMethod::ScopedAttributeList
* list
,
497 gnome_keyring_attribute_list_append_string(list
->get(), name
, value
);
501 void GKRMethod::AppendString(GKRMethod::ScopedAttributeList
* list
,
503 const std::string
& value
) {
504 AppendString(list
, name
, value
.c_str());
508 void GKRMethod::AppendUint32(GKRMethod::ScopedAttributeList
* list
,
511 gnome_keyring_attribute_list_append_uint32(list
->get(), name
, value
);
515 void GKRMethod::OnOperationDone(GnomeKeyringResult result
, gpointer data
) {
516 GKRMethod
* method
= static_cast<GKRMethod
*>(data
);
517 method
->result_
= result
;
518 method
->event_
.Signal();
522 void GKRMethod::OnOperationGetList(GnomeKeyringResult result
, GList
* list
,
524 GKRMethod
* method
= static_cast<GKRMethod
*>(data
);
525 method
->result_
= result
;
526 // |list| will be freed after this callback returns, so convert it now.
527 if (result
== GNOME_KEYRING_RESULT_OK
)
528 method
->forms_
= ConvertFormList(list
, method
->lookup_form_
.get());
530 method
->forms_
.clear();
531 method
->lookup_form_
.reset();
532 method
->event_
.Signal();
535 // Generates a profile-specific app string based on profile_id.
536 std::string
GetProfileSpecificAppString(LocalProfileId profile_id
) {
537 // Originally, the application string was always just "chrome" and used only
538 // so that we had *something* to search for since GNOME Keyring won't search
539 // for nothing. Now we use it to distinguish passwords for different profiles.
540 return base::StringPrintf("%s-%d", kGnomeKeyringAppString
, profile_id
);
545 NativeBackendGnome::NativeBackendGnome(LocalProfileId id
)
546 : profile_id_(id
), app_string_(GetProfileSpecificAppString(id
)) {
549 NativeBackendGnome::~NativeBackendGnome() {
552 bool NativeBackendGnome::Init() {
553 return LoadGnomeKeyring() && gnome_keyring_is_available();
556 bool NativeBackendGnome::RawAddLogin(const PasswordForm
& form
) {
557 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB
));
559 BrowserThread::PostTask(BrowserThread::UI
, FROM_HERE
,
560 base::Bind(&GKRMethod::AddLogin
,
561 base::Unretained(&method
),
562 form
, app_string_
.c_str()));
563 GnomeKeyringResult result
= method
.WaitResult();
564 if (result
!= GNOME_KEYRING_RESULT_OK
) {
565 LOG(ERROR
) << "Keyring save failed: "
566 << gnome_keyring_result_to_message(result
);
572 password_manager::PasswordStoreChangeList
NativeBackendGnome::AddLogin(
573 const PasswordForm
& form
) {
574 // Based on LoginDatabase::AddLogin(), we search for an existing match based
575 // on origin_url, username_element, username_value, password_element, submit
576 // element, and signon_realm first, remove that, and then add the new entry.
577 // We'd add the new one first, and then delete the original, but then the
578 // delete might actually delete the newly-added entry!
579 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB
));
581 BrowserThread::PostTask(BrowserThread::UI
, FROM_HERE
,
582 base::Bind(&GKRMethod::AddLoginSearch
,
583 base::Unretained(&method
),
584 form
, app_string_
.c_str()));
585 ScopedVector
<PasswordForm
> forms
;
586 GnomeKeyringResult result
= method
.WaitResult(&forms
);
587 if (result
!= GNOME_KEYRING_RESULT_OK
&&
588 result
!= GNOME_KEYRING_RESULT_NO_MATCH
) {
589 LOG(ERROR
) << "Keyring find failed: "
590 << gnome_keyring_result_to_message(result
);
591 return password_manager::PasswordStoreChangeList();
593 password_manager::PasswordStoreChangeList changes
;
594 if (forms
.size() > 0) {
595 if (forms
.size() > 1) {
596 LOG(WARNING
) << "Adding login when there are " << forms
.size()
597 << " matching logins already! Will replace only the first.";
600 if (RemoveLogin(*forms
[0])) {
601 changes
.push_back(password_manager::PasswordStoreChange(
602 password_manager::PasswordStoreChange::REMOVE
, *forms
[0]));
605 if (RawAddLogin(form
)) {
606 changes
.push_back(password_manager::PasswordStoreChange(
607 password_manager::PasswordStoreChange::ADD
, form
));
612 bool NativeBackendGnome::UpdateLogin(
613 const PasswordForm
& form
,
614 password_manager::PasswordStoreChangeList
* changes
) {
615 // Based on LoginDatabase::UpdateLogin(), we search for forms to update by
616 // origin_url, username_element, username_value, password_element, and
617 // signon_realm. We then compare the result to the updated form. If they
618 // differ in any of the mutable fields, then we remove the original, and
619 // then add the new entry. We'd add the new one first, and then delete the
620 // original, but then the delete might actually delete the newly-added entry!
621 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB
));
625 BrowserThread::PostTask(BrowserThread::UI
, FROM_HERE
,
626 base::Bind(&GKRMethod::UpdateLoginSearch
,
627 base::Unretained(&method
),
628 form
, app_string_
.c_str()));
629 ScopedVector
<PasswordForm
> forms
;
630 GnomeKeyringResult result
= method
.WaitResult(&forms
);
631 if (result
!= GNOME_KEYRING_RESULT_OK
) {
632 LOG(ERROR
) << "Keyring find failed: "
633 << gnome_keyring_result_to_message(result
);
637 bool removed
= false;
638 for (size_t i
= 0; i
< forms
.size(); ++i
) {
639 if (*forms
[i
] != form
) {
640 RemoveLogin(*forms
[i
]);
647 if (RawAddLogin(form
)) {
648 password_manager::PasswordStoreChange
change(
649 password_manager::PasswordStoreChange::UPDATE
, form
);
650 changes
->push_back(change
);
656 bool NativeBackendGnome::RemoveLogin(const PasswordForm
& form
) {
657 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB
));
659 BrowserThread::PostTask(BrowserThread::UI
, FROM_HERE
,
660 base::Bind(&GKRMethod::RemoveLogin
,
661 base::Unretained(&method
),
662 form
, app_string_
.c_str()));
663 GnomeKeyringResult result
= method
.WaitResult();
664 if (result
!= GNOME_KEYRING_RESULT_OK
) {
665 // Warning, not error, because this can sometimes happen due to the user
666 // racing with the daemon to delete the password a second time.
667 LOG(WARNING
) << "Keyring delete failed: "
668 << gnome_keyring_result_to_message(result
);
674 bool NativeBackendGnome::RemoveLoginsCreatedBetween(
675 base::Time delete_begin
,
676 base::Time delete_end
,
677 password_manager::PasswordStoreChangeList
* changes
) {
678 return RemoveLoginsBetween(
679 delete_begin
, delete_end
, CREATION_TIMESTAMP
, changes
);
682 bool NativeBackendGnome::RemoveLoginsSyncedBetween(
683 base::Time delete_begin
,
684 base::Time delete_end
,
685 password_manager::PasswordStoreChangeList
* changes
) {
686 return RemoveLoginsBetween(delete_begin
, delete_end
, SYNC_TIMESTAMP
, changes
);
689 bool NativeBackendGnome::GetLogins(const PasswordForm
& form
,
690 ScopedVector
<PasswordForm
>* forms
) {
691 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB
));
693 BrowserThread::PostTask(BrowserThread::UI
, FROM_HERE
,
694 base::Bind(&GKRMethod::GetLogins
,
695 base::Unretained(&method
),
696 form
, app_string_
.c_str()));
697 GnomeKeyringResult result
= method
.WaitResult(forms
);
698 if (result
== GNOME_KEYRING_RESULT_NO_MATCH
)
700 if (result
!= GNOME_KEYRING_RESULT_OK
) {
701 LOG(ERROR
) << "Keyring find failed: "
702 << gnome_keyring_result_to_message(result
);
708 bool NativeBackendGnome::GetAutofillableLogins(
709 ScopedVector
<PasswordForm
>* forms
) {
710 return GetLoginsList(true, forms
);
713 bool NativeBackendGnome::GetBlacklistLogins(ScopedVector
<PasswordForm
>* forms
) {
714 return GetLoginsList(false, forms
);
717 bool NativeBackendGnome::GetLoginsList(bool autofillable
,
718 ScopedVector
<PasswordForm
>* forms
) {
719 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB
));
721 uint32_t blacklisted_by_user
= !autofillable
;
724 BrowserThread::PostTask(BrowserThread::UI
, FROM_HERE
,
725 base::Bind(&GKRMethod::GetLoginsList
,
726 base::Unretained(&method
),
727 blacklisted_by_user
, app_string_
.c_str()));
728 GnomeKeyringResult result
= method
.WaitResult(forms
);
729 if (result
== GNOME_KEYRING_RESULT_NO_MATCH
)
731 if (result
!= GNOME_KEYRING_RESULT_OK
) {
732 LOG(ERROR
) << "Keyring find failed: "
733 << gnome_keyring_result_to_message(result
);
739 bool NativeBackendGnome::GetAllLogins(ScopedVector
<PasswordForm
>* forms
) {
741 BrowserThread::PostTask(BrowserThread::UI
, FROM_HERE
,
742 base::Bind(&GKRMethod::GetAllLogins
,
743 base::Unretained(&method
),
744 app_string_
.c_str()));
745 GnomeKeyringResult result
= method
.WaitResult(forms
);
746 if (result
== GNOME_KEYRING_RESULT_NO_MATCH
)
748 if (result
!= GNOME_KEYRING_RESULT_OK
) {
749 LOG(ERROR
) << "Keyring find failed: "
750 << gnome_keyring_result_to_message(result
);
756 bool NativeBackendGnome::GetLoginsBetween(base::Time get_begin
,
758 TimestampToCompare date_to_compare
,
759 ScopedVector
<PasswordForm
>* forms
) {
760 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB
));
762 // We could walk the list and add items as we find them, but it is much
763 // easier to build the list and then filter the results.
764 ScopedVector
<PasswordForm
> all_forms
;
765 if (!GetAllLogins(&all_forms
))
768 base::Time
PasswordForm::*date_member
= date_to_compare
== CREATION_TIMESTAMP
769 ? &PasswordForm::date_created
770 : &PasswordForm::date_synced
;
771 for (auto& saved_form
: all_forms
) {
772 if (get_begin
<= saved_form
->*date_member
&&
773 (get_end
.is_null() || saved_form
->*date_member
< get_end
)) {
774 forms
->push_back(saved_form
);
775 saved_form
= nullptr;
782 bool NativeBackendGnome::RemoveLoginsBetween(
783 base::Time get_begin
,
785 TimestampToCompare date_to_compare
,
786 password_manager::PasswordStoreChangeList
* changes
) {
787 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB
));
790 // We could walk the list and delete items as we find them, but it is much
791 // easier to build the list and use RemoveLogin() to delete them.
792 ScopedVector
<PasswordForm
> forms
;
793 if (!GetLoginsBetween(get_begin
, get_end
, date_to_compare
, &forms
))
797 for (size_t i
= 0; i
< forms
.size(); ++i
) {
798 if (RemoveLogin(*forms
[i
])) {
799 changes
->push_back(password_manager::PasswordStoreChange(
800 password_manager::PasswordStoreChange::REMOVE
, *forms
[i
]));