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
;
33 using password_manager::PSLMatchingHelper
;
35 #define GNOME_KEYRING_DEFINE_POINTER(name) \
36 typeof(&::gnome_keyring_##name) GnomeKeyringLoader::gnome_keyring_##name;
37 GNOME_KEYRING_FOR_EACH_FUNC(GNOME_KEYRING_DEFINE_POINTER
)
38 #undef GNOME_KEYRING_DEFINE_POINTER
40 bool GnomeKeyringLoader::keyring_loaded
= false;
42 #if defined(DLOPEN_GNOME_KEYRING)
44 #define GNOME_KEYRING_FUNCTION_INFO(name) \
45 {"gnome_keyring_"#name, reinterpret_cast<void**>(&gnome_keyring_##name)},
46 const GnomeKeyringLoader::FunctionInfo
GnomeKeyringLoader::functions
[] = {
47 GNOME_KEYRING_FOR_EACH_FUNC(GNOME_KEYRING_FUNCTION_INFO
)
50 #undef GNOME_KEYRING_FUNCTION_INFO
52 /* Load the library and initialize the function pointers. */
53 bool GnomeKeyringLoader::LoadGnomeKeyring() {
57 void* handle
= dlopen("libgnome-keyring.so.0", RTLD_NOW
| RTLD_GLOBAL
);
59 // We wanted to use GNOME Keyring, but we couldn't load it. Warn, because
60 // either the user asked for this, or we autodetected it incorrectly. (Or
61 // the system has broken libraries, which is also good to warn about.)
62 LOG(WARNING
) << "Could not load libgnome-keyring.so.0: " << dlerror();
66 for (size_t i
= 0; functions
[i
].name
; ++i
) {
68 *functions
[i
].pointer
= dlsym(handle
, functions
[i
].name
);
69 const char* error
= dlerror();
71 LOG(ERROR
) << "Unable to load symbol "
72 << functions
[i
].name
<< ": " << error
;
78 keyring_loaded
= true;
79 // We leak the library handle. That's OK: this function is called only once.
83 #else // defined(DLOPEN_GNOME_KEYRING)
85 bool GnomeKeyringLoader::LoadGnomeKeyring() {
88 #define GNOME_KEYRING_ASSIGN_POINTER(name) \
89 gnome_keyring_##name = &::gnome_keyring_##name;
90 GNOME_KEYRING_FOR_EACH_FUNC(GNOME_KEYRING_ASSIGN_POINTER
)
91 #undef GNOME_KEYRING_ASSIGN_POINTER
92 keyring_loaded
= true;
96 #endif // defined(DLOPEN_GNOME_KEYRING)
100 const char kGnomeKeyringAppString
[] = "chrome";
102 // Convert the attributes of a given keyring entry into a new PasswordForm.
103 // Note: does *not* get the actual password, as that is not a key attribute!
104 // Returns NULL if the attributes are for the wrong application.
105 scoped_ptr
<PasswordForm
> FormFromAttributes(GnomeKeyringAttributeList
* attrs
) {
106 // Read the string and int attributes into the appropriate map.
107 std::map
<std::string
, std::string
> string_attr_map
;
108 std::map
<std::string
, uint32_t> uint_attr_map
;
109 for (guint i
= 0; i
< attrs
->len
; ++i
) {
110 GnomeKeyringAttribute attr
= gnome_keyring_attribute_list_index(attrs
, i
);
111 if (attr
.type
== GNOME_KEYRING_ATTRIBUTE_TYPE_STRING
)
112 string_attr_map
[attr
.name
] = attr
.value
.string
;
113 else if (attr
.type
== GNOME_KEYRING_ATTRIBUTE_TYPE_UINT32
)
114 uint_attr_map
[attr
.name
] = attr
.value
.integer
;
116 // Check to make sure this is a password we care about.
117 const std::string
& app_value
= string_attr_map
["application"];
118 if (!base::StringPiece(app_value
).starts_with(kGnomeKeyringAppString
))
119 return scoped_ptr
<PasswordForm
>();
121 scoped_ptr
<PasswordForm
> form(new PasswordForm());
122 form
->origin
= GURL(string_attr_map
["origin_url"]);
123 form
->action
= GURL(string_attr_map
["action_url"]);
124 form
->username_element
= UTF8ToUTF16(string_attr_map
["username_element"]);
125 form
->username_value
= UTF8ToUTF16(string_attr_map
["username_value"]);
126 form
->password_element
= UTF8ToUTF16(string_attr_map
["password_element"]);
127 form
->submit_element
= UTF8ToUTF16(string_attr_map
["submit_element"]);
128 form
->signon_realm
= string_attr_map
["signon_realm"];
129 form
->ssl_valid
= uint_attr_map
["ssl_valid"];
130 form
->preferred
= uint_attr_map
["preferred"];
131 int64 date_created
= 0;
132 bool date_ok
= base::StringToInt64(string_attr_map
["date_created"],
135 form
->date_created
= base::Time::FromTimeT(date_created
);
136 form
->blacklisted_by_user
= uint_attr_map
["blacklisted_by_user"];
137 form
->type
= static_cast<PasswordForm::Type
>(uint_attr_map
["type"]);
138 form
->times_used
= uint_attr_map
["times_used"];
139 form
->scheme
= static_cast<PasswordForm::Scheme
>(uint_attr_map
["scheme"]);
140 int64 date_synced
= 0;
141 base::StringToInt64(string_attr_map
["date_synced"], &date_synced
);
142 form
->date_synced
= base::Time::FromInternalValue(date_synced
);
143 form
->display_name
= UTF8ToUTF16(string_attr_map
["display_name"]);
144 form
->avatar_url
= GURL(string_attr_map
["avatar_url"]);
145 form
->federation_url
= GURL(string_attr_map
["federation_url"]);
146 form
->is_zero_click
= uint_attr_map
["is_zero_click"];
151 // Parse all the results from the given GList into a PasswordFormList, and free
152 // the GList. PasswordForms are allocated on the heap, and should be deleted by
153 // the consumer. If not NULL, |lookup_form| is used to filter out results --
154 // only credentials with signon realms passing the PSL matching (done by
155 // |helper|) against |lookup_form->signon_realm| will be kept. PSL matched
156 // results get their signon_realm, origin, and action rewritten to those of
157 // |lookup_form_|, with the original signon_realm saved into the result's
158 // original_signon_realm data member.
159 void ConvertFormList(GList
* found
,
160 const PasswordForm
* lookup_form
,
161 const PSLMatchingHelper
& helper
,
162 NativeBackendGnome::PasswordFormList
* forms
) {
163 PSLMatchingHelper::PSLDomainMatchMetric psl_domain_match_metric
=
164 PSLMatchingHelper::PSL_DOMAIN_MATCH_NONE
;
165 for (GList
* element
= g_list_first(found
); element
!= NULL
;
166 element
= g_list_next(element
)) {
167 GnomeKeyringFound
* data
= static_cast<GnomeKeyringFound
*>(element
->data
);
168 GnomeKeyringAttributeList
* attrs
= data
->attributes
;
170 scoped_ptr
<PasswordForm
> form(FormFromAttributes(attrs
));
172 if (lookup_form
&& form
->signon_realm
!= lookup_form
->signon_realm
) {
173 // This is not an exact match, we try PSL matching.
174 if (lookup_form
->scheme
!= PasswordForm::SCHEME_HTML
||
175 form
->scheme
!= PasswordForm::SCHEME_HTML
||
176 !(PSLMatchingHelper::IsPublicSuffixDomainMatch(
177 lookup_form
->signon_realm
, form
->signon_realm
))) {
180 psl_domain_match_metric
= PSLMatchingHelper::PSL_DOMAIN_MATCH_FOUND
;
181 form
->original_signon_realm
= form
->signon_realm
;
182 form
->signon_realm
= lookup_form
->signon_realm
;
183 form
->origin
= lookup_form
->origin
;
184 form
->action
= lookup_form
->action
;
187 form
->password_value
= UTF8ToUTF16(data
->secret
);
189 LOG(WARNING
) << "Unable to access password from list element!";
191 forms
->push_back(form
.release());
193 LOG(WARNING
) << "Could not initialize PasswordForm from attributes!";
197 UMA_HISTOGRAM_ENUMERATION(
198 "PasswordManager.PslDomainMatchTriggering",
199 helper
.IsMatchingEnabled()
200 ? psl_domain_match_metric
201 : PSLMatchingHelper::PSL_DOMAIN_MATCH_DISABLED
,
202 PSLMatchingHelper::PSL_DOMAIN_MATCH_COUNT
);
206 // Schema is analagous to the fields in PasswordForm.
207 // TODO(gcasto): Adding 'form_data' would be nice, but we would need to
208 // serialize in a way that is guaranteed to not have any embedded NULLs. Pickle
209 // doesn't make this guarantee, so we just don't serialize this field. Since
210 // it's only used to crowd source data collection it doesn't matter that much
211 // if it's not available on this platform.
212 const GnomeKeyringPasswordSchema kGnomeSchema
= {
213 GNOME_KEYRING_ITEM_GENERIC_SECRET
, {
214 { "origin_url", GNOME_KEYRING_ATTRIBUTE_TYPE_STRING
},
215 { "action_url", GNOME_KEYRING_ATTRIBUTE_TYPE_STRING
},
216 { "username_element", GNOME_KEYRING_ATTRIBUTE_TYPE_STRING
},
217 { "username_value", GNOME_KEYRING_ATTRIBUTE_TYPE_STRING
},
218 { "password_element", GNOME_KEYRING_ATTRIBUTE_TYPE_STRING
},
219 { "submit_element", GNOME_KEYRING_ATTRIBUTE_TYPE_STRING
},
220 { "signon_realm", GNOME_KEYRING_ATTRIBUTE_TYPE_STRING
},
221 { "ssl_valid", GNOME_KEYRING_ATTRIBUTE_TYPE_UINT32
},
222 { "preferred", GNOME_KEYRING_ATTRIBUTE_TYPE_UINT32
},
223 { "date_created", GNOME_KEYRING_ATTRIBUTE_TYPE_STRING
},
224 { "blacklisted_by_user", GNOME_KEYRING_ATTRIBUTE_TYPE_UINT32
},
225 { "scheme", GNOME_KEYRING_ATTRIBUTE_TYPE_UINT32
},
226 { "type", GNOME_KEYRING_ATTRIBUTE_TYPE_UINT32
},
227 { "times_used", GNOME_KEYRING_ATTRIBUTE_TYPE_UINT32
},
228 { "date_synced", GNOME_KEYRING_ATTRIBUTE_TYPE_STRING
},
229 { "display_name", GNOME_KEYRING_ATTRIBUTE_TYPE_STRING
},
230 { "avatar_url", GNOME_KEYRING_ATTRIBUTE_TYPE_STRING
},
231 { "federation_url", GNOME_KEYRING_ATTRIBUTE_TYPE_STRING
},
232 { "is_zero_click", GNOME_KEYRING_ATTRIBUTE_TYPE_UINT32
},
233 // This field is always "chrome" so that we can search for it.
234 { "application", GNOME_KEYRING_ATTRIBUTE_TYPE_STRING
},
239 // Sadly, PasswordStore goes to great lengths to switch from the originally
240 // calling thread to the DB thread, and to provide an asynchronous API to
241 // callers while using a synchronous (virtual) API provided by subclasses like
242 // PasswordStoreX -- but GNOME Keyring really wants to be on the GLib main
243 // thread, which is the UI thread to us. So we end up having to switch threads
244 // again, possibly back to the very same thread (in case the UI thread is the
245 // caller, e.g. in the password management UI), and *block* the DB thread
246 // waiting for a response from the UI thread to provide the synchronous API
247 // PasswordStore expects of us. (It will then in turn switch back to the
248 // original caller to send the asynchronous reply to the original request.)
250 // This class represents a call to a GNOME Keyring method. A RunnableMethod
251 // should be posted to the UI thread to call one of its action methods, and then
252 // a WaitResult() method should be called to wait for the result. Each instance
253 // supports only one outstanding method at a time, though multiple instances may
254 // be used in parallel.
255 class GKRMethod
: public GnomeKeyringLoader
{
257 typedef NativeBackendGnome::PasswordFormList PasswordFormList
;
259 GKRMethod() : event_(false, false), result_(GNOME_KEYRING_RESULT_CANCELLED
) {}
261 // Action methods. These call gnome_keyring_* functions. Call from UI thread.
262 // See GetProfileSpecificAppString() for more information on the app string.
263 void AddLogin(const PasswordForm
& form
, const char* app_string
);
264 void AddLoginSearch(const PasswordForm
& form
, const char* app_string
);
265 void UpdateLoginSearch(const PasswordForm
& form
, const char* app_string
);
266 void RemoveLogin(const PasswordForm
& form
, const char* app_string
);
267 void GetLogins(const PasswordForm
& form
, const char* app_string
);
268 void GetLoginsList(uint32_t blacklisted_by_user
, const char* app_string
);
269 void GetAllLogins(const char* app_string
);
271 // Use after AddLogin, RemoveLogin.
272 GnomeKeyringResult
WaitResult();
274 // Use after AddLoginSearch, UpdateLoginSearch, GetLogins, GetLoginsList,
276 GnomeKeyringResult
WaitResult(PasswordFormList
* forms
);
279 struct GnomeKeyringAttributeListFreeDeleter
{
280 inline void operator()(void* list
) const {
281 gnome_keyring_attribute_list_free(
282 static_cast<GnomeKeyringAttributeList
*>(list
));
286 typedef scoped_ptr
<GnomeKeyringAttributeList
,
287 GnomeKeyringAttributeListFreeDeleter
> ScopedAttributeList
;
289 // Helper methods to abbreviate Gnome Keyring long API names.
290 static void AppendString(ScopedAttributeList
* list
,
293 static void AppendString(ScopedAttributeList
* list
,
295 const std::string
& value
);
296 static void AppendUint32(ScopedAttributeList
* list
,
300 // All these callbacks are called on UI thread.
301 static void OnOperationDone(GnomeKeyringResult result
, gpointer data
);
303 static void OnOperationGetList(GnomeKeyringResult result
, GList
* list
,
306 base::WaitableEvent event_
;
307 GnomeKeyringResult result_
;
308 NativeBackendGnome::PasswordFormList forms_
;
309 // If the credential search is specified by a single form and needs to use PSL
310 // matching, then the specifying form is stored in |lookup_form_|. If PSL
311 // matching is used to find a result, then the results signon realm, origin
312 // and action are stored are replaced by those of |lookup_form_|.
313 // Additionally, |lookup_form_->signon_realm| is also used to narrow down the
314 // found logins to those which indeed PSL-match the look-up. And finally,
315 // |lookup_form_| set to NULL means that PSL matching is not required.
316 scoped_ptr
<PasswordForm
> lookup_form_
;
317 const PSLMatchingHelper helper_
;
320 void GKRMethod::AddLogin(const PasswordForm
& form
, const char* app_string
) {
321 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
322 time_t date_created
= form
.date_created
.ToTimeT();
323 // If we are asked to save a password with 0 date, use the current time.
324 // We don't want to actually save passwords as though on January 1, 1970.
326 date_created
= time(NULL
);
327 int64 date_synced
= form
.date_synced
.ToInternalValue();
328 gnome_keyring_store_password(
330 NULL
, // Default keyring.
331 form
.origin
.spec().c_str(), // Display name.
332 UTF16ToUTF8(form
.password_value
).c_str(),
335 NULL
, // destroy_data
336 "origin_url", form
.origin
.spec().c_str(),
337 "action_url", form
.action
.spec().c_str(),
338 "username_element", UTF16ToUTF8(form
.username_element
).c_str(),
339 "username_value", UTF16ToUTF8(form
.username_value
).c_str(),
340 "password_element", UTF16ToUTF8(form
.password_element
).c_str(),
341 "submit_element", UTF16ToUTF8(form
.submit_element
).c_str(),
342 "signon_realm", form
.signon_realm
.c_str(),
343 "ssl_valid", form
.ssl_valid
,
344 "preferred", form
.preferred
,
345 "date_created", base::Int64ToString(date_created
).c_str(),
346 "blacklisted_by_user", form
.blacklisted_by_user
,
348 "times_used", form
.times_used
,
349 "scheme", form
.scheme
,
350 "date_synced", base::Int64ToString(date_synced
).c_str(),
351 "display_name", UTF16ToUTF8(form
.display_name
).c_str(),
352 "avatar_url", form
.avatar_url
.spec().c_str(),
353 "federation_url", form
.federation_url
.spec().c_str(),
354 "is_zero_click", form
.is_zero_click
,
355 "application", app_string
,
359 void GKRMethod::AddLoginSearch(const PasswordForm
& form
,
360 const char* app_string
) {
361 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
362 lookup_form_
.reset(NULL
);
363 // Search GNOME Keyring for matching passwords to update.
364 ScopedAttributeList
attrs(gnome_keyring_attribute_list_new());
365 AppendString(&attrs
, "origin_url", form
.origin
.spec());
366 AppendString(&attrs
, "username_element", UTF16ToUTF8(form
.username_element
));
367 AppendString(&attrs
, "username_value", UTF16ToUTF8(form
.username_value
));
368 AppendString(&attrs
, "password_element", UTF16ToUTF8(form
.password_element
));
369 AppendString(&attrs
, "submit_element", UTF16ToUTF8(form
.submit_element
));
370 AppendString(&attrs
, "signon_realm", form
.signon_realm
);
371 AppendString(&attrs
, "application", app_string
);
372 gnome_keyring_find_items(GNOME_KEYRING_ITEM_GENERIC_SECRET
,
376 /*destroy_data=*/NULL
);
379 void GKRMethod::UpdateLoginSearch(const PasswordForm
& form
,
380 const char* app_string
) {
381 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
382 lookup_form_
.reset(NULL
);
383 // Search GNOME Keyring for matching passwords to update.
384 ScopedAttributeList
attrs(gnome_keyring_attribute_list_new());
385 AppendString(&attrs
, "origin_url", form
.origin
.spec());
386 AppendString(&attrs
, "username_element", UTF16ToUTF8(form
.username_element
));
387 AppendString(&attrs
, "username_value", UTF16ToUTF8(form
.username_value
));
388 AppendString(&attrs
, "password_element", UTF16ToUTF8(form
.password_element
));
389 AppendString(&attrs
, "signon_realm", form
.signon_realm
);
390 AppendString(&attrs
, "application", app_string
);
391 gnome_keyring_find_items(GNOME_KEYRING_ITEM_GENERIC_SECRET
,
395 /*destroy_data=*/NULL
);
398 void GKRMethod::RemoveLogin(const PasswordForm
& form
, const char* app_string
) {
399 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
400 // We find forms using the same fields as LoginDatabase::RemoveLogin().
401 gnome_keyring_delete_password(
405 NULL
, // destroy_data
406 "origin_url", form
.origin
.spec().c_str(),
407 "action_url", form
.action
.spec().c_str(),
408 "username_element", UTF16ToUTF8(form
.username_element
).c_str(),
409 "username_value", UTF16ToUTF8(form
.username_value
).c_str(),
410 "password_element", UTF16ToUTF8(form
.password_element
).c_str(),
411 "submit_element", UTF16ToUTF8(form
.submit_element
).c_str(),
412 "signon_realm", form
.signon_realm
.c_str(),
413 "application", app_string
,
417 void GKRMethod::GetLogins(const PasswordForm
& form
, const char* app_string
) {
418 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
419 lookup_form_
.reset(new PasswordForm(form
));
420 // Search GNOME Keyring for matching passwords.
421 ScopedAttributeList
attrs(gnome_keyring_attribute_list_new());
422 if (!helper_
.ShouldPSLDomainMatchingApply(
423 PSLMatchingHelper::GetRegistryControlledDomain(
424 GURL(form
.signon_realm
)))) {
425 AppendString(&attrs
, "signon_realm", form
.signon_realm
);
427 AppendString(&attrs
, "application", app_string
);
428 gnome_keyring_find_items(GNOME_KEYRING_ITEM_GENERIC_SECRET
,
432 /*destroy_data=*/NULL
);
435 void GKRMethod::GetLoginsList(uint32_t blacklisted_by_user
,
436 const char* app_string
) {
437 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
438 lookup_form_
.reset(NULL
);
439 // Search GNOME Keyring for matching passwords.
440 ScopedAttributeList
attrs(gnome_keyring_attribute_list_new());
441 AppendUint32(&attrs
, "blacklisted_by_user", blacklisted_by_user
);
442 AppendString(&attrs
, "application", app_string
);
443 gnome_keyring_find_items(GNOME_KEYRING_ITEM_GENERIC_SECRET
,
447 /*destroy_data=*/NULL
);
450 void GKRMethod::GetAllLogins(const char* app_string
) {
451 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
452 lookup_form_
.reset(NULL
);
453 // We need to search for something, otherwise we get no results - so
454 // we search for the fixed application string.
455 ScopedAttributeList
attrs(gnome_keyring_attribute_list_new());
456 AppendString(&attrs
, "application", app_string
);
457 gnome_keyring_find_items(GNOME_KEYRING_ITEM_GENERIC_SECRET
,
461 /*destroy_data=*/NULL
);
464 GnomeKeyringResult
GKRMethod::WaitResult() {
465 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB
));
470 GnomeKeyringResult
GKRMethod::WaitResult(PasswordFormList
* forms
) {
471 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB
));
473 if (forms
->empty()) {
474 // Normal case. Avoid extra allocation by swapping.
477 // Rare case. Append forms_ to *forms.
478 forms
->insert(forms
->end(), forms_
.begin(), forms_
.end());
485 void GKRMethod::AppendString(GKRMethod::ScopedAttributeList
* list
,
488 gnome_keyring_attribute_list_append_string(list
->get(), name
, value
);
492 void GKRMethod::AppendString(GKRMethod::ScopedAttributeList
* list
,
494 const std::string
& value
) {
495 AppendString(list
, name
, value
.c_str());
499 void GKRMethod::AppendUint32(GKRMethod::ScopedAttributeList
* list
,
502 gnome_keyring_attribute_list_append_uint32(list
->get(), name
, value
);
506 void GKRMethod::OnOperationDone(GnomeKeyringResult result
, gpointer data
) {
507 GKRMethod
* method
= static_cast<GKRMethod
*>(data
);
508 method
->result_
= result
;
509 method
->event_
.Signal();
513 void GKRMethod::OnOperationGetList(GnomeKeyringResult result
, GList
* list
,
515 GKRMethod
* method
= static_cast<GKRMethod
*>(data
);
516 method
->result_
= result
;
517 method
->forms_
.clear();
518 // |list| will be freed after this callback returns, so convert it now.
520 list
, method
->lookup_form_
.get(), method
->helper_
, &method
->forms_
);
521 method
->lookup_form_
.reset(NULL
);
522 method
->event_
.Signal();
527 NativeBackendGnome::NativeBackendGnome(LocalProfileId id
)
529 app_string_
= GetProfileSpecificAppString();
532 NativeBackendGnome::~NativeBackendGnome() {
535 bool NativeBackendGnome::Init() {
536 return LoadGnomeKeyring() && gnome_keyring_is_available();
539 bool NativeBackendGnome::RawAddLogin(const PasswordForm
& form
) {
540 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB
));
542 BrowserThread::PostTask(BrowserThread::UI
, FROM_HERE
,
543 base::Bind(&GKRMethod::AddLogin
,
544 base::Unretained(&method
),
545 form
, app_string_
.c_str()));
546 GnomeKeyringResult result
= method
.WaitResult();
547 if (result
!= GNOME_KEYRING_RESULT_OK
) {
548 LOG(ERROR
) << "Keyring save failed: "
549 << gnome_keyring_result_to_message(result
);
555 password_manager::PasswordStoreChangeList
NativeBackendGnome::AddLogin(
556 const PasswordForm
& form
) {
557 // Based on LoginDatabase::AddLogin(), we search for an existing match based
558 // on origin_url, username_element, username_value, password_element, submit
559 // element, and signon_realm first, remove that, and then add the new entry.
560 // We'd add the new one first, and then delete the original, but then the
561 // delete might actually delete the newly-added entry!
562 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB
));
564 BrowserThread::PostTask(BrowserThread::UI
, FROM_HERE
,
565 base::Bind(&GKRMethod::AddLoginSearch
,
566 base::Unretained(&method
),
567 form
, app_string_
.c_str()));
568 ScopedVector
<autofill::PasswordForm
> forms
;
569 GnomeKeyringResult result
= method
.WaitResult(&forms
.get());
570 if (result
!= GNOME_KEYRING_RESULT_OK
&&
571 result
!= GNOME_KEYRING_RESULT_NO_MATCH
) {
572 LOG(ERROR
) << "Keyring find failed: "
573 << gnome_keyring_result_to_message(result
);
574 return password_manager::PasswordStoreChangeList();
576 password_manager::PasswordStoreChangeList changes
;
577 if (forms
.size() > 0) {
578 if (forms
.size() > 1) {
579 LOG(WARNING
) << "Adding login when there are " << forms
.size()
580 << " matching logins already! Will replace only the first.";
583 if (RemoveLogin(*forms
[0])) {
584 changes
.push_back(password_manager::PasswordStoreChange(
585 password_manager::PasswordStoreChange::REMOVE
, *forms
[0]));
588 if (RawAddLogin(form
)) {
589 changes
.push_back(password_manager::PasswordStoreChange(
590 password_manager::PasswordStoreChange::ADD
, form
));
595 bool NativeBackendGnome::UpdateLogin(
596 const PasswordForm
& form
,
597 password_manager::PasswordStoreChangeList
* changes
) {
598 // Based on LoginDatabase::UpdateLogin(), we search for forms to update by
599 // origin_url, username_element, username_value, password_element, and
600 // signon_realm. We then compare the result to the updated form. If they
601 // differ in any of the mutable fields, then we remove the original, and
602 // then add the new entry. We'd add the new one first, and then delete the
603 // original, but then the delete might actually delete the newly-added entry!
604 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB
));
608 BrowserThread::PostTask(BrowserThread::UI
, FROM_HERE
,
609 base::Bind(&GKRMethod::UpdateLoginSearch
,
610 base::Unretained(&method
),
611 form
, app_string_
.c_str()));
612 ScopedVector
<autofill::PasswordForm
> forms
;
613 GnomeKeyringResult result
= method
.WaitResult(&forms
.get());
614 if (result
!= GNOME_KEYRING_RESULT_OK
) {
615 LOG(ERROR
) << "Keyring find failed: "
616 << gnome_keyring_result_to_message(result
);
620 bool removed
= false;
621 for (size_t i
= 0; i
< forms
.size(); ++i
) {
622 if (*forms
[i
] != form
) {
623 RemoveLogin(*forms
[i
]);
630 if (RawAddLogin(form
)) {
631 password_manager::PasswordStoreChange
change(
632 password_manager::PasswordStoreChange::UPDATE
, form
);
633 changes
->push_back(change
);
639 bool NativeBackendGnome::RemoveLogin(const PasswordForm
& form
) {
640 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB
));
642 BrowserThread::PostTask(BrowserThread::UI
, FROM_HERE
,
643 base::Bind(&GKRMethod::RemoveLogin
,
644 base::Unretained(&method
),
645 form
, app_string_
.c_str()));
646 GnomeKeyringResult result
= method
.WaitResult();
647 if (result
!= GNOME_KEYRING_RESULT_OK
) {
648 // Warning, not error, because this can sometimes happen due to the user
649 // racing with the daemon to delete the password a second time.
650 LOG(WARNING
) << "Keyring delete failed: "
651 << gnome_keyring_result_to_message(result
);
657 bool NativeBackendGnome::RemoveLoginsCreatedBetween(
658 base::Time delete_begin
,
659 base::Time delete_end
,
660 password_manager::PasswordStoreChangeList
* changes
) {
661 return RemoveLoginsBetween(
662 delete_begin
, delete_end
, CREATION_TIMESTAMP
, changes
);
665 bool NativeBackendGnome::RemoveLoginsSyncedBetween(
666 base::Time delete_begin
,
667 base::Time delete_end
,
668 password_manager::PasswordStoreChangeList
* changes
) {
669 return RemoveLoginsBetween(delete_begin
, delete_end
, SYNC_TIMESTAMP
, changes
);
672 bool NativeBackendGnome::GetLogins(const PasswordForm
& form
,
673 PasswordFormList
* forms
) {
674 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB
));
676 BrowserThread::PostTask(BrowserThread::UI
, FROM_HERE
,
677 base::Bind(&GKRMethod::GetLogins
,
678 base::Unretained(&method
),
679 form
, app_string_
.c_str()));
680 GnomeKeyringResult result
= method
.WaitResult(forms
);
681 if (result
== GNOME_KEYRING_RESULT_NO_MATCH
)
683 if (result
!= GNOME_KEYRING_RESULT_OK
) {
684 LOG(ERROR
) << "Keyring find failed: "
685 << gnome_keyring_result_to_message(result
);
691 bool NativeBackendGnome::GetAutofillableLogins(PasswordFormList
* forms
) {
692 return GetLoginsList(forms
, true);
695 bool NativeBackendGnome::GetBlacklistLogins(PasswordFormList
* forms
) {
696 return GetLoginsList(forms
, false);
699 bool NativeBackendGnome::GetLoginsList(PasswordFormList
* forms
,
701 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB
));
703 uint32_t blacklisted_by_user
= !autofillable
;
706 BrowserThread::PostTask(BrowserThread::UI
, FROM_HERE
,
707 base::Bind(&GKRMethod::GetLoginsList
,
708 base::Unretained(&method
),
709 blacklisted_by_user
, app_string_
.c_str()));
710 GnomeKeyringResult result
= method
.WaitResult(forms
);
711 if (result
== GNOME_KEYRING_RESULT_NO_MATCH
)
713 if (result
!= GNOME_KEYRING_RESULT_OK
) {
714 LOG(ERROR
) << "Keyring find failed: "
715 << gnome_keyring_result_to_message(result
);
721 bool NativeBackendGnome::GetAllLogins(PasswordFormList
* forms
) {
723 BrowserThread::PostTask(BrowserThread::UI
, FROM_HERE
,
724 base::Bind(&GKRMethod::GetAllLogins
,
725 base::Unretained(&method
),
726 app_string_
.c_str()));
727 GnomeKeyringResult result
= method
.WaitResult(forms
);
728 if (result
== GNOME_KEYRING_RESULT_NO_MATCH
)
730 if (result
!= GNOME_KEYRING_RESULT_OK
) {
731 LOG(ERROR
) << "Keyring find failed: "
732 << gnome_keyring_result_to_message(result
);
738 bool NativeBackendGnome::GetLoginsBetween(base::Time get_begin
,
740 TimestampToCompare date_to_compare
,
741 PasswordFormList
* forms
) {
742 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB
));
743 // We could walk the list and add items as we find them, but it is much
744 // easier to build the list and then filter the results.
745 PasswordFormList all_forms
;
746 if (!GetAllLogins(&all_forms
))
749 base::Time
autofill::PasswordForm::*date_member
=
750 date_to_compare
== CREATION_TIMESTAMP
751 ? &autofill::PasswordForm::date_created
752 : &autofill::PasswordForm::date_synced
;
753 for (size_t i
= 0; i
< all_forms
.size(); ++i
) {
754 if (get_begin
<= all_forms
[i
]->*date_member
&&
755 (get_end
.is_null() || all_forms
[i
]->*date_member
< get_end
)) {
756 forms
->push_back(all_forms
[i
]);
765 bool NativeBackendGnome::RemoveLoginsBetween(
766 base::Time get_begin
,
768 TimestampToCompare date_to_compare
,
769 password_manager::PasswordStoreChangeList
* changes
) {
770 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB
));
773 // We could walk the list and delete items as we find them, but it is much
774 // easier to build the list and use RemoveLogin() to delete them.
775 ScopedVector
<autofill::PasswordForm
> forms
;
776 if (!GetLoginsBetween(get_begin
, get_end
, date_to_compare
, &forms
.get()))
780 for (size_t i
= 0; i
< forms
.size(); ++i
) {
781 if (RemoveLogin(*forms
[i
])) {
782 changes
->push_back(password_manager::PasswordStoreChange(
783 password_manager::PasswordStoreChange::REMOVE
, *forms
[i
]));
791 std::string
NativeBackendGnome::GetProfileSpecificAppString() const {
792 // Originally, the application string was always just "chrome" and used only
793 // so that we had *something* to search for since GNOME Keyring won't search
794 // for nothing. Now we use it to distinguish passwords for different profiles.
795 return base::StringPrintf("%s-%d", kGnomeKeyringAppString
, profile_id_
);