Update broken references to image assets
[chromium-blink-merge.git] / chrome / browser / password_manager / native_backend_gnome_x.cc
blobd221a234a4dbe5b8521ba7ee2fbc56a6a00f582c
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"
7 #include <dlfcn.h>
8 #include <gnome-keyring.h>
10 #include <map>
11 #include <string>
12 #include <vector>
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/password_manager_metrics_util.h"
27 #include "components/password_manager/core/browser/password_manager_util.h"
28 #include "components/password_manager/core/browser/psl_matching_helper.h"
29 #include "content/public/browser/browser_thread.h"
31 using autofill::PasswordForm;
32 using base::UTF8ToUTF16;
33 using base::UTF16ToUTF8;
34 using content::BrowserThread;
35 using namespace password_manager::metrics_util;
37 namespace {
38 const int kMaxPossibleTimeTValue = std::numeric_limits<int>::max();
41 #define GNOME_KEYRING_DEFINE_POINTER(name) \
42 typeof(&::gnome_keyring_##name) GnomeKeyringLoader::gnome_keyring_##name;
43 GNOME_KEYRING_FOR_EACH_FUNC(GNOME_KEYRING_DEFINE_POINTER)
44 #undef GNOME_KEYRING_DEFINE_POINTER
46 bool GnomeKeyringLoader::keyring_loaded = false;
48 #if defined(DLOPEN_GNOME_KEYRING)
50 #define GNOME_KEYRING_FUNCTION_INFO(name) \
51 {"gnome_keyring_"#name, reinterpret_cast<void**>(&gnome_keyring_##name)},
52 const GnomeKeyringLoader::FunctionInfo GnomeKeyringLoader::functions[] = {
53 GNOME_KEYRING_FOR_EACH_FUNC(GNOME_KEYRING_FUNCTION_INFO)
54 {nullptr, nullptr}
56 #undef GNOME_KEYRING_FUNCTION_INFO
58 /* Load the library and initialize the function pointers. */
59 bool GnomeKeyringLoader::LoadGnomeKeyring() {
60 if (keyring_loaded)
61 return true;
63 void* handle = dlopen("libgnome-keyring.so.0", RTLD_NOW | RTLD_GLOBAL);
64 if (!handle) {
65 // We wanted to use GNOME Keyring, but we couldn't load it. Warn, because
66 // either the user asked for this, or we autodetected it incorrectly. (Or
67 // the system has broken libraries, which is also good to warn about.)
68 LOG(WARNING) << "Could not load libgnome-keyring.so.0: " << dlerror();
69 return false;
72 for (size_t i = 0; functions[i].name; ++i) {
73 dlerror();
74 *functions[i].pointer = dlsym(handle, functions[i].name);
75 const char* error = dlerror();
76 if (error) {
77 LOG(ERROR) << "Unable to load symbol "
78 << functions[i].name << ": " << error;
79 dlclose(handle);
80 return false;
84 keyring_loaded = true;
85 // We leak the library handle. That's OK: this function is called only once.
86 return true;
89 #else // defined(DLOPEN_GNOME_KEYRING)
91 bool GnomeKeyringLoader::LoadGnomeKeyring() {
92 if (keyring_loaded)
93 return true;
94 #define GNOME_KEYRING_ASSIGN_POINTER(name) \
95 gnome_keyring_##name = &::gnome_keyring_##name;
96 GNOME_KEYRING_FOR_EACH_FUNC(GNOME_KEYRING_ASSIGN_POINTER)
97 #undef GNOME_KEYRING_ASSIGN_POINTER
98 keyring_loaded = true;
99 return true;
102 #endif // defined(DLOPEN_GNOME_KEYRING)
104 namespace {
106 const char kGnomeKeyringAppString[] = "chrome";
108 // Convert the attributes of a given keyring entry into a new PasswordForm.
109 // Note: does *not* get the actual password, as that is not a key attribute!
110 // Returns NULL if the attributes are for the wrong application.
111 scoped_ptr<PasswordForm> FormFromAttributes(GnomeKeyringAttributeList* attrs) {
112 // Read the string and int attributes into the appropriate map.
113 std::map<std::string, std::string> string_attr_map;
114 std::map<std::string, uint32_t> uint_attr_map;
115 for (guint i = 0; i < attrs->len; ++i) {
116 GnomeKeyringAttribute attr = gnome_keyring_attribute_list_index(attrs, i);
117 if (attr.type == GNOME_KEYRING_ATTRIBUTE_TYPE_STRING)
118 string_attr_map[attr.name] = attr.value.string;
119 else if (attr.type == GNOME_KEYRING_ATTRIBUTE_TYPE_UINT32)
120 uint_attr_map[attr.name] = attr.value.integer;
122 // Check to make sure this is a password we care about.
123 const std::string& app_value = string_attr_map["application"];
124 if (!base::StringPiece(app_value).starts_with(kGnomeKeyringAppString))
125 return scoped_ptr<PasswordForm>();
127 scoped_ptr<PasswordForm> form(new PasswordForm());
128 form->origin = GURL(string_attr_map["origin_url"]);
129 form->action = GURL(string_attr_map["action_url"]);
130 form->username_element = UTF8ToUTF16(string_attr_map["username_element"]);
131 form->username_value = UTF8ToUTF16(string_attr_map["username_value"]);
132 form->password_element = UTF8ToUTF16(string_attr_map["password_element"]);
133 form->submit_element = UTF8ToUTF16(string_attr_map["submit_element"]);
134 form->signon_realm = string_attr_map["signon_realm"];
135 form->ssl_valid = uint_attr_map["ssl_valid"];
136 form->preferred = uint_attr_map["preferred"];
137 int64 date_created = 0;
138 bool date_ok = base::StringToInt64(string_attr_map["date_created"],
139 &date_created);
140 DCHECK(date_ok);
141 // In the past |date_created| was stored as time_t. Currently is stored as
142 // base::Time's internal value. We need to distinguish, which format the
143 // number in |date_created| was stored in. We use the fact that
144 // kMaxPossibleTimeTValue interpreted as the internal value corresponds to an
145 // unlikely date back in 17th century, and anything above
146 // kMaxPossibleTimeTValue clearly must be in the internal value format.
147 form->date_created = date_created < kMaxPossibleTimeTValue
148 ? base::Time::FromTimeT(date_created)
149 : base::Time::FromInternalValue(date_created);
150 form->blacklisted_by_user = uint_attr_map["blacklisted_by_user"];
151 form->type = static_cast<PasswordForm::Type>(uint_attr_map["type"]);
152 form->times_used = uint_attr_map["times_used"];
153 form->scheme = static_cast<PasswordForm::Scheme>(uint_attr_map["scheme"]);
154 int64 date_synced = 0;
155 base::StringToInt64(string_attr_map["date_synced"], &date_synced);
156 form->date_synced = base::Time::FromInternalValue(date_synced);
157 form->display_name = UTF8ToUTF16(string_attr_map["display_name"]);
158 form->icon_url = GURL(string_attr_map["avatar_url"]);
159 form->federation_url = GURL(string_attr_map["federation_url"]);
160 form->skip_zero_click = uint_attr_map["skip_zero_click"];
161 form->generation_upload_status =
162 static_cast<PasswordForm::GenerationUploadStatus>(
163 uint_attr_map["generation_upload_status"]);
164 if (!string_attr_map["form_data"].empty()) {
165 bool success = DeserializeFormDataFromBase64String(
166 string_attr_map["form_data"], &form->form_data);
167 FormDeserializationStatus status = success ? GNOME_SUCCESS : GNOME_FAILURE;
168 LogFormDataDeserializationStatus(status);
170 return form.Pass();
173 // Converts native credentials in |found| to PasswordForms. If not NULL,
174 // |lookup_form| is used to filter out results -- only credentials with signon
175 // realms passing the PSL matching against |lookup_form->signon_realm| will be
176 // kept. PSL matched results get their signon_realm, origin, and action
177 // rewritten to those of |lookup_form_|, with the original signon_realm saved
178 // into the result's original_signon_realm data member.
179 ScopedVector<PasswordForm> ConvertFormList(GList* found,
180 const PasswordForm* lookup_form) {
181 ScopedVector<PasswordForm> forms;
182 password_manager::PSLDomainMatchMetric psl_domain_match_metric =
183 password_manager::PSL_DOMAIN_MATCH_NONE;
184 for (GList* element = g_list_first(found); element;
185 element = g_list_next(element)) {
186 GnomeKeyringFound* data = static_cast<GnomeKeyringFound*>(element->data);
187 GnomeKeyringAttributeList* attrs = data->attributes;
189 scoped_ptr<PasswordForm> form(FormFromAttributes(attrs));
190 if (form) {
191 if (lookup_form && form->signon_realm != lookup_form->signon_realm) {
192 // This is not an exact match, we try PSL matching.
193 if (lookup_form->scheme != PasswordForm::SCHEME_HTML ||
194 form->scheme != PasswordForm::SCHEME_HTML ||
195 !(password_manager::IsPublicSuffixDomainMatch(
196 lookup_form->signon_realm, form->signon_realm))) {
197 continue;
199 psl_domain_match_metric = password_manager::PSL_DOMAIN_MATCH_FOUND;
200 form->original_signon_realm = form->signon_realm;
201 form->signon_realm = lookup_form->signon_realm;
202 form->origin = lookup_form->origin;
203 form->action = lookup_form->action;
205 if (data->secret) {
206 form->password_value = UTF8ToUTF16(data->secret);
207 } else {
208 LOG(WARNING) << "Unable to access password from list element!";
210 forms.push_back(form.Pass());
211 } else {
212 LOG(WARNING) << "Could not initialize PasswordForm from attributes!";
215 if (lookup_form) {
216 const GURL signon_realm(lookup_form->signon_realm);
217 std::string registered_domain =
218 password_manager::GetRegistryControlledDomain(signon_realm);
219 UMA_HISTOGRAM_ENUMERATION(
220 "PasswordManager.PslDomainMatchTriggering",
221 password_manager::ShouldPSLDomainMatchingApply(registered_domain)
222 ? psl_domain_match_metric
223 : password_manager::PSL_DOMAIN_MATCH_NOT_USED,
224 password_manager::PSL_DOMAIN_MATCH_COUNT);
226 return forms.Pass();
229 // Schema is analagous to the fields in PasswordForm.
230 const GnomeKeyringPasswordSchema kGnomeSchema = {
231 GNOME_KEYRING_ITEM_GENERIC_SECRET, {
232 { "origin_url", GNOME_KEYRING_ATTRIBUTE_TYPE_STRING },
233 { "action_url", GNOME_KEYRING_ATTRIBUTE_TYPE_STRING },
234 { "username_element", GNOME_KEYRING_ATTRIBUTE_TYPE_STRING },
235 { "username_value", GNOME_KEYRING_ATTRIBUTE_TYPE_STRING },
236 { "password_element", GNOME_KEYRING_ATTRIBUTE_TYPE_STRING },
237 { "submit_element", GNOME_KEYRING_ATTRIBUTE_TYPE_STRING },
238 { "signon_realm", GNOME_KEYRING_ATTRIBUTE_TYPE_STRING },
239 { "ssl_valid", GNOME_KEYRING_ATTRIBUTE_TYPE_UINT32 },
240 { "preferred", GNOME_KEYRING_ATTRIBUTE_TYPE_UINT32 },
241 { "date_created", GNOME_KEYRING_ATTRIBUTE_TYPE_STRING },
242 { "blacklisted_by_user", GNOME_KEYRING_ATTRIBUTE_TYPE_UINT32 },
243 { "scheme", GNOME_KEYRING_ATTRIBUTE_TYPE_UINT32 },
244 { "type", GNOME_KEYRING_ATTRIBUTE_TYPE_UINT32 },
245 { "times_used", GNOME_KEYRING_ATTRIBUTE_TYPE_UINT32 },
246 { "date_synced", GNOME_KEYRING_ATTRIBUTE_TYPE_STRING },
247 { "display_name", GNOME_KEYRING_ATTRIBUTE_TYPE_STRING },
248 { "avatar_url", GNOME_KEYRING_ATTRIBUTE_TYPE_STRING },
249 { "federation_url", GNOME_KEYRING_ATTRIBUTE_TYPE_STRING },
250 { "skip_zero_click", GNOME_KEYRING_ATTRIBUTE_TYPE_UINT32 },
251 { "generation_upload_status", GNOME_KEYRING_ATTRIBUTE_TYPE_UINT32 },
252 { "form_data", GNOME_KEYRING_ATTRIBUTE_TYPE_STRING },
253 // This field is always "chrome" so that we can search for it.
254 { "application", GNOME_KEYRING_ATTRIBUTE_TYPE_STRING },
255 { nullptr }
259 // Sadly, PasswordStore goes to great lengths to switch from the originally
260 // calling thread to the DB thread, and to provide an asynchronous API to
261 // callers while using a synchronous (virtual) API provided by subclasses like
262 // PasswordStoreX -- but GNOME Keyring really wants to be on the GLib main
263 // thread, which is the UI thread to us. So we end up having to switch threads
264 // again, possibly back to the very same thread (in case the UI thread is the
265 // caller, e.g. in the password management UI), and *block* the DB thread
266 // waiting for a response from the UI thread to provide the synchronous API
267 // PasswordStore expects of us. (It will then in turn switch back to the
268 // original caller to send the asynchronous reply to the original request.)
270 // This class represents a call to a GNOME Keyring method. A RunnableMethod
271 // should be posted to the UI thread to call one of its action methods, and then
272 // a WaitResult() method should be called to wait for the result. Each instance
273 // supports only one outstanding method at a time, though multiple instances may
274 // be used in parallel.
275 class GKRMethod : public GnomeKeyringLoader {
276 public:
277 GKRMethod() : event_(false, false), result_(GNOME_KEYRING_RESULT_CANCELLED) {}
279 // Action methods. These call gnome_keyring_* functions. Call from UI thread.
280 // See GetProfileSpecificAppString() for more information on the app string.
281 void AddLogin(const PasswordForm& form, const char* app_string);
282 void LoginSearch(const PasswordForm& form, const char* app_string);
283 void RemoveLogin(const PasswordForm& form, const char* app_string);
284 void GetLogins(const PasswordForm& form, const char* app_string);
285 void GetLoginsList(uint32_t blacklisted_by_user, const char* app_string);
286 void GetAllLogins(const char* app_string);
288 // Use after AddLogin, RemoveLogin.
289 GnomeKeyringResult WaitResult();
291 // Use after LoginSearch, GetLogins, GetLoginsList, GetAllLogins. Replaces the
292 // content of |forms| with found logins.
293 GnomeKeyringResult WaitResult(ScopedVector<PasswordForm>* forms);
295 private:
296 struct GnomeKeyringAttributeListFreeDeleter {
297 inline void operator()(void* list) const {
298 gnome_keyring_attribute_list_free(
299 static_cast<GnomeKeyringAttributeList*>(list));
303 typedef scoped_ptr<GnomeKeyringAttributeList,
304 GnomeKeyringAttributeListFreeDeleter> ScopedAttributeList;
306 // Helper methods to abbreviate Gnome Keyring long API names.
307 static void AppendString(ScopedAttributeList* list,
308 const char* name,
309 const char* value);
310 static void AppendString(ScopedAttributeList* list,
311 const char* name,
312 const std::string& value);
313 static void AppendUint32(ScopedAttributeList* list,
314 const char* name,
315 guint32 value);
317 // All these callbacks are called on UI thread.
318 static void OnOperationDone(GnomeKeyringResult result, gpointer data);
320 // This is marked as static, but acts on the GKRMethod instance that |data|
321 // points to. Saves |result| to |result_|. If the result is OK, overwrites
322 // |forms_| with the found credentials. Clears |forms_| otherwise.
323 static void OnOperationGetList(GnomeKeyringResult result, GList* list,
324 gpointer data);
326 base::WaitableEvent event_;
327 GnomeKeyringResult result_;
328 ScopedVector<PasswordForm> forms_;
329 // If the credential search is specified by a single form and needs to use PSL
330 // matching, then the specifying form is stored in |lookup_form_|. If PSL
331 // matching is used to find a result, then the results signon realm, origin
332 // and action are stored are replaced by those of |lookup_form_|.
333 // Additionally, |lookup_form_->signon_realm| is also used to narrow down the
334 // found logins to those which indeed PSL-match the look-up. And finally,
335 // |lookup_form_| set to NULL means that PSL matching is not required.
336 scoped_ptr<PasswordForm> lookup_form_;
339 void GKRMethod::AddLogin(const PasswordForm& form, const char* app_string) {
340 DCHECK_CURRENTLY_ON(BrowserThread::UI);
341 int64 date_created = form.date_created.ToInternalValue();
342 // If we are asked to save a password with 0 date, use the current time.
343 // We don't want to actually save passwords as though on January 1, 1601.
344 if (!date_created)
345 date_created = base::Time::Now().ToInternalValue();
346 int64 date_synced = form.date_synced.ToInternalValue();
347 std::string form_data;
348 SerializeFormDataToBase64String(form.form_data, &form_data);
349 gnome_keyring_store_password(
350 &kGnomeSchema,
351 nullptr, // Default keyring.
352 form.origin.spec().c_str(), // Display name.
353 UTF16ToUTF8(form.password_value).c_str(),
354 OnOperationDone,
355 this, // data
356 nullptr, // destroy_data
357 "origin_url", form.origin.spec().c_str(),
358 "action_url", form.action.spec().c_str(),
359 "username_element", UTF16ToUTF8(form.username_element).c_str(),
360 "username_value", UTF16ToUTF8(form.username_value).c_str(),
361 "password_element", UTF16ToUTF8(form.password_element).c_str(),
362 "submit_element", UTF16ToUTF8(form.submit_element).c_str(),
363 "signon_realm", form.signon_realm.c_str(),
364 "ssl_valid", form.ssl_valid,
365 "preferred", form.preferred,
366 "date_created", base::Int64ToString(date_created).c_str(),
367 "blacklisted_by_user", form.blacklisted_by_user,
368 "type", form.type,
369 "times_used", form.times_used,
370 "scheme", form.scheme,
371 "date_synced", base::Int64ToString(date_synced).c_str(),
372 "display_name", UTF16ToUTF8(form.display_name).c_str(),
373 "avatar_url", form.icon_url.spec().c_str(),
374 "federation_url", form.federation_url.spec().c_str(),
375 "skip_zero_click", form.skip_zero_click,
376 "generation_upload_status", form.generation_upload_status,
377 "form_data", form_data.c_str(),
378 "application", app_string,
379 nullptr);
382 void GKRMethod::LoginSearch(const PasswordForm& form,
383 const char* app_string) {
384 DCHECK_CURRENTLY_ON(BrowserThread::UI);
385 lookup_form_.reset(nullptr);
386 // Search GNOME Keyring for matching passwords to update.
387 ScopedAttributeList attrs(gnome_keyring_attribute_list_new());
388 AppendString(&attrs, "origin_url", form.origin.spec());
389 AppendString(&attrs, "username_element", UTF16ToUTF8(form.username_element));
390 AppendString(&attrs, "username_value", UTF16ToUTF8(form.username_value));
391 AppendString(&attrs, "password_element", UTF16ToUTF8(form.password_element));
392 AppendString(&attrs, "signon_realm", form.signon_realm);
393 AppendString(&attrs, "application", app_string);
394 gnome_keyring_find_items(GNOME_KEYRING_ITEM_GENERIC_SECRET,
395 attrs.get(),
396 OnOperationGetList,
397 /*data=*/this,
398 /*destroy_data=*/nullptr);
401 void GKRMethod::RemoveLogin(const PasswordForm& form, const char* app_string) {
402 DCHECK_CURRENTLY_ON(BrowserThread::UI);
403 // We find forms using the same fields as LoginDatabase::RemoveLogin().
404 gnome_keyring_delete_password(
405 &kGnomeSchema,
406 OnOperationDone,
407 this, // data
408 nullptr, // destroy_data
409 "origin_url", form.origin.spec().c_str(),
410 "username_element", UTF16ToUTF8(form.username_element).c_str(),
411 "username_value", UTF16ToUTF8(form.username_value).c_str(),
412 "password_element", UTF16ToUTF8(form.password_element).c_str(),
413 "signon_realm", form.signon_realm.c_str(),
414 "application", app_string,
415 nullptr);
418 void GKRMethod::GetLogins(const PasswordForm& form, const char* app_string) {
419 DCHECK_CURRENTLY_ON(BrowserThread::UI);
420 lookup_form_.reset(new PasswordForm(form));
421 // Search GNOME Keyring for matching passwords.
422 ScopedAttributeList attrs(gnome_keyring_attribute_list_new());
423 if (!password_manager::ShouldPSLDomainMatchingApply(
424 password_manager::GetRegistryControlledDomain(
425 GURL(form.signon_realm)))) {
426 AppendString(&attrs, "signon_realm", form.signon_realm);
428 AppendString(&attrs, "application", app_string);
429 gnome_keyring_find_items(GNOME_KEYRING_ITEM_GENERIC_SECRET,
430 attrs.get(),
431 OnOperationGetList,
432 /*data=*/this,
433 /*destroy_data=*/nullptr);
436 void GKRMethod::GetLoginsList(uint32_t blacklisted_by_user,
437 const char* app_string) {
438 DCHECK_CURRENTLY_ON(BrowserThread::UI);
439 lookup_form_.reset(nullptr);
440 // Search GNOME Keyring for matching passwords.
441 ScopedAttributeList attrs(gnome_keyring_attribute_list_new());
442 AppendUint32(&attrs, "blacklisted_by_user", blacklisted_by_user);
443 AppendString(&attrs, "application", app_string);
444 gnome_keyring_find_items(GNOME_KEYRING_ITEM_GENERIC_SECRET,
445 attrs.get(),
446 OnOperationGetList,
447 /*data=*/this,
448 /*destroy_data=*/nullptr);
451 void GKRMethod::GetAllLogins(const char* app_string) {
452 DCHECK_CURRENTLY_ON(BrowserThread::UI);
453 lookup_form_.reset(nullptr);
454 // We need to search for something, otherwise we get no results - so
455 // we search for the fixed application string.
456 ScopedAttributeList attrs(gnome_keyring_attribute_list_new());
457 AppendString(&attrs, "application", app_string);
458 gnome_keyring_find_items(GNOME_KEYRING_ITEM_GENERIC_SECRET,
459 attrs.get(),
460 OnOperationGetList,
461 /*data=*/this,
462 /*destroy_data=*/nullptr);
465 GnomeKeyringResult GKRMethod::WaitResult() {
466 DCHECK_CURRENTLY_ON(BrowserThread::DB);
467 event_.Wait();
468 return result_;
471 GnomeKeyringResult GKRMethod::WaitResult(ScopedVector<PasswordForm>* forms) {
472 DCHECK_CURRENTLY_ON(BrowserThread::DB);
473 event_.Wait();
474 *forms = forms_.Pass();
475 return result_;
478 // static
479 void GKRMethod::AppendString(GKRMethod::ScopedAttributeList* list,
480 const char* name,
481 const char* value) {
482 gnome_keyring_attribute_list_append_string(list->get(), name, value);
485 // static
486 void GKRMethod::AppendString(GKRMethod::ScopedAttributeList* list,
487 const char* name,
488 const std::string& value) {
489 AppendString(list, name, value.c_str());
492 // static
493 void GKRMethod::AppendUint32(GKRMethod::ScopedAttributeList* list,
494 const char* name,
495 guint32 value) {
496 gnome_keyring_attribute_list_append_uint32(list->get(), name, value);
499 // static
500 void GKRMethod::OnOperationDone(GnomeKeyringResult result, gpointer data) {
501 GKRMethod* method = static_cast<GKRMethod*>(data);
502 method->result_ = result;
503 method->event_.Signal();
506 // static
507 void GKRMethod::OnOperationGetList(GnomeKeyringResult result, GList* list,
508 gpointer data) {
509 GKRMethod* method = static_cast<GKRMethod*>(data);
510 method->result_ = result;
511 // |list| will be freed after this callback returns, so convert it now.
512 if (result == GNOME_KEYRING_RESULT_OK)
513 method->forms_ = ConvertFormList(list, method->lookup_form_.get());
514 else
515 method->forms_.clear();
516 method->lookup_form_.reset();
517 method->event_.Signal();
520 // Generates a profile-specific app string based on profile_id.
521 std::string GetProfileSpecificAppString(LocalProfileId profile_id) {
522 // Originally, the application string was always just "chrome" and used only
523 // so that we had *something* to search for since GNOME Keyring won't search
524 // for nothing. Now we use it to distinguish passwords for different profiles.
525 return base::StringPrintf("%s-%d", kGnomeKeyringAppString, profile_id);
528 } // namespace
530 NativeBackendGnome::NativeBackendGnome(LocalProfileId id)
531 : profile_id_(id), app_string_(GetProfileSpecificAppString(id)) {
534 NativeBackendGnome::~NativeBackendGnome() {
537 bool NativeBackendGnome::Init() {
538 return LoadGnomeKeyring() && gnome_keyring_is_available();
541 bool NativeBackendGnome::RawAddLogin(const PasswordForm& form) {
542 DCHECK_CURRENTLY_ON(BrowserThread::DB);
543 GKRMethod method;
544 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
545 base::Bind(&GKRMethod::AddLogin,
546 base::Unretained(&method),
547 form, app_string_.c_str()));
548 GnomeKeyringResult result = method.WaitResult();
549 if (result != GNOME_KEYRING_RESULT_OK) {
550 LOG(ERROR) << "Keyring save failed: "
551 << gnome_keyring_result_to_message(result);
552 return false;
554 return true;
557 password_manager::PasswordStoreChangeList NativeBackendGnome::AddLogin(
558 const PasswordForm& form) {
559 // Based on LoginDatabase::AddLogin(), we search for an existing match based
560 // on origin_url, username_element, username_value, password_element, submit
561 // element, and signon_realm first, remove that, and then add the new entry.
562 // We'd add the new one first, and then delete the original, but then the
563 // delete might actually delete the newly-added entry!
564 DCHECK_CURRENTLY_ON(BrowserThread::DB);
565 GKRMethod method;
566 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
567 base::Bind(&GKRMethod::LoginSearch,
568 base::Unretained(&method),
569 form, app_string_.c_str()));
570 ScopedVector<PasswordForm> forms;
571 GnomeKeyringResult result = method.WaitResult(&forms);
572 if (result != GNOME_KEYRING_RESULT_OK &&
573 result != GNOME_KEYRING_RESULT_NO_MATCH) {
574 LOG(ERROR) << "Keyring find failed: "
575 << gnome_keyring_result_to_message(result);
576 return password_manager::PasswordStoreChangeList();
578 password_manager::PasswordStoreChangeList changes;
579 if (forms.size() > 0) {
580 password_manager::PasswordStoreChangeList temp_changes;
581 if (forms.size() > 1) {
582 LOG(WARNING) << "Adding login when there are " << forms.size()
583 << " matching logins already!";
585 for (const PasswordForm* old_form : forms) {
586 if (!RemoveLogin(*old_form, &temp_changes))
587 return changes;
589 changes.push_back(password_manager::PasswordStoreChange(
590 password_manager::PasswordStoreChange::REMOVE, *forms[0]));
592 if (RawAddLogin(form)) {
593 changes.push_back(password_manager::PasswordStoreChange(
594 password_manager::PasswordStoreChange::ADD, form));
596 return changes;
599 bool NativeBackendGnome::UpdateLogin(
600 const PasswordForm& form,
601 password_manager::PasswordStoreChangeList* changes) {
602 // Based on LoginDatabase::UpdateLogin(), we search for forms to update by
603 // origin_url, username_element, username_value, password_element, and
604 // signon_realm. We then compare the result to the updated form. If they
605 // differ in any of the mutable fields, then we remove the original, and
606 // then add the new entry. We'd add the new one first, and then delete the
607 // original, but then the delete might actually delete the newly-added entry!
608 DCHECK_CURRENTLY_ON(BrowserThread::DB);
609 DCHECK(changes);
610 GKRMethod method;
611 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
612 base::Bind(&GKRMethod::LoginSearch,
613 base::Unretained(&method),
614 form, app_string_.c_str()));
615 ScopedVector<PasswordForm> forms;
616 GnomeKeyringResult result = method.WaitResult(&forms);
617 if (result == GNOME_KEYRING_RESULT_NO_MATCH)
618 return true;
619 if (result != GNOME_KEYRING_RESULT_OK) {
620 LOG(ERROR) << "Keyring find failed: "
621 << gnome_keyring_result_to_message(result);
622 return false;
624 if (forms.size() == 1 && *forms.front() == form)
625 return true;
627 password_manager::PasswordStoreChangeList temp_changes;
628 for (const PasswordForm* keychain_form : forms) {
629 // Remove all the obsolete forms. Note that RemoveLogin can remove any form
630 // matching the unique key. Thus, it's important to call it the right number
631 // of times.
632 if (!RemoveLogin(*keychain_form, &temp_changes))
633 return false;
636 if (RawAddLogin(form)) {
637 password_manager::PasswordStoreChange change(
638 password_manager::PasswordStoreChange::UPDATE, form);
639 changes->push_back(change);
640 return true;
642 return false;
645 bool NativeBackendGnome::RemoveLogin(
646 const PasswordForm& form,
647 password_manager::PasswordStoreChangeList* changes) {
648 DCHECK_CURRENTLY_ON(BrowserThread::DB);
649 DCHECK(changes);
650 GKRMethod method;
651 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
652 base::Bind(&GKRMethod::RemoveLogin,
653 base::Unretained(&method),
654 form, app_string_.c_str()));
655 GnomeKeyringResult result = method.WaitResult();
656 if (result == GNOME_KEYRING_RESULT_NO_MATCH)
657 return true;
659 if (result != GNOME_KEYRING_RESULT_OK) {
660 LOG(ERROR) << "Keyring delete failed: "
661 << gnome_keyring_result_to_message(result);
662 return false;
664 changes->push_back(password_manager::PasswordStoreChange(
665 password_manager::PasswordStoreChange::REMOVE, form));
666 return true;
669 bool NativeBackendGnome::RemoveLoginsCreatedBetween(
670 base::Time delete_begin,
671 base::Time delete_end,
672 password_manager::PasswordStoreChangeList* changes) {
673 return RemoveLoginsBetween(
674 delete_begin, delete_end, CREATION_TIMESTAMP, changes);
677 bool NativeBackendGnome::RemoveLoginsSyncedBetween(
678 base::Time delete_begin,
679 base::Time delete_end,
680 password_manager::PasswordStoreChangeList* changes) {
681 return RemoveLoginsBetween(delete_begin, delete_end, SYNC_TIMESTAMP, changes);
684 bool NativeBackendGnome::GetLogins(const PasswordForm& form,
685 ScopedVector<PasswordForm>* forms) {
686 DCHECK_CURRENTLY_ON(BrowserThread::DB);
687 GKRMethod method;
688 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
689 base::Bind(&GKRMethod::GetLogins,
690 base::Unretained(&method),
691 form, app_string_.c_str()));
692 GnomeKeyringResult result = method.WaitResult(forms);
693 if (result == GNOME_KEYRING_RESULT_NO_MATCH)
694 return true;
695 if (result != GNOME_KEYRING_RESULT_OK) {
696 LOG(ERROR) << "Keyring find failed: "
697 << gnome_keyring_result_to_message(result);
698 return false;
700 return true;
703 bool NativeBackendGnome::GetAutofillableLogins(
704 ScopedVector<PasswordForm>* forms) {
705 return GetLoginsList(true, forms);
708 bool NativeBackendGnome::GetBlacklistLogins(ScopedVector<PasswordForm>* forms) {
709 return GetLoginsList(false, forms);
712 bool NativeBackendGnome::GetLoginsList(bool autofillable,
713 ScopedVector<PasswordForm>* forms) {
714 DCHECK_CURRENTLY_ON(BrowserThread::DB);
716 uint32_t blacklisted_by_user = !autofillable;
718 GKRMethod method;
719 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
720 base::Bind(&GKRMethod::GetLoginsList,
721 base::Unretained(&method),
722 blacklisted_by_user, app_string_.c_str()));
723 GnomeKeyringResult result = method.WaitResult(forms);
724 if (result == GNOME_KEYRING_RESULT_NO_MATCH)
725 return true;
726 if (result != GNOME_KEYRING_RESULT_OK) {
727 LOG(ERROR) << "Keyring find failed: "
728 << gnome_keyring_result_to_message(result);
729 return false;
732 // Get rid of the forms with the same sync tags.
733 ScopedVector<autofill::PasswordForm> duplicates;
734 std::vector<std::vector<autofill::PasswordForm*>> tag_groups;
735 password_manager_util::FindDuplicates(forms, &duplicates, &tag_groups);
736 if (duplicates.empty())
737 return true;
738 for (const auto& group : tag_groups) {
739 if (group.size() > 1) {
740 // There are duplicates. Readd the first form. AddLogin() is smart enough
741 // to clean the previous ones.
742 password_manager::PasswordStoreChangeList changes = AddLogin(*group[0]);
743 if (changes.empty() ||
744 changes.back().type() != password_manager::PasswordStoreChange::ADD)
745 return false;
748 return true;
751 bool NativeBackendGnome::GetAllLogins(ScopedVector<PasswordForm>* forms) {
752 GKRMethod method;
753 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
754 base::Bind(&GKRMethod::GetAllLogins,
755 base::Unretained(&method),
756 app_string_.c_str()));
757 GnomeKeyringResult result = method.WaitResult(forms);
758 if (result == GNOME_KEYRING_RESULT_NO_MATCH)
759 return true;
760 if (result != GNOME_KEYRING_RESULT_OK) {
761 LOG(ERROR) << "Keyring find failed: "
762 << gnome_keyring_result_to_message(result);
763 return false;
765 return true;
768 bool NativeBackendGnome::GetLoginsBetween(base::Time get_begin,
769 base::Time get_end,
770 TimestampToCompare date_to_compare,
771 ScopedVector<PasswordForm>* forms) {
772 DCHECK_CURRENTLY_ON(BrowserThread::DB);
773 forms->clear();
774 // We could walk the list and add items as we find them, but it is much
775 // easier to build the list and then filter the results.
776 ScopedVector<PasswordForm> all_forms;
777 if (!GetAllLogins(&all_forms))
778 return false;
780 base::Time PasswordForm::*date_member = date_to_compare == CREATION_TIMESTAMP
781 ? &PasswordForm::date_created
782 : &PasswordForm::date_synced;
783 for (auto& saved_form : all_forms) {
784 if (get_begin <= saved_form->*date_member &&
785 (get_end.is_null() || saved_form->*date_member < get_end)) {
786 forms->push_back(saved_form);
787 saved_form = nullptr;
791 return true;
794 bool NativeBackendGnome::RemoveLoginsBetween(
795 base::Time get_begin,
796 base::Time get_end,
797 TimestampToCompare date_to_compare,
798 password_manager::PasswordStoreChangeList* changes) {
799 DCHECK_CURRENTLY_ON(BrowserThread::DB);
800 DCHECK(changes);
801 changes->clear();
802 // We could walk the list and delete items as we find them, but it is much
803 // easier to build the list and use RemoveLogin() to delete them.
804 ScopedVector<PasswordForm> forms;
805 if (!GetLoginsBetween(get_begin, get_end, date_to_compare, &forms))
806 return false;
808 for (size_t i = 0; i < forms.size(); ++i) {
809 if (!RemoveLogin(*forms[i], changes))
810 return false;
812 return true;