Chromium Code Reviews| Index: chrome/browser/ui/webui/settings/passwords_handler.cc |
| diff --git a/chrome/browser/ui/webui/settings/passwords_handler.cc b/chrome/browser/ui/webui/settings/passwords_handler.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..d9948d837ced6a0797cd10c3b1d20918a6304991 |
| --- /dev/null |
| +++ b/chrome/browser/ui/webui/settings/passwords_handler.cc |
| @@ -0,0 +1,179 @@ |
| +// Copyright 2016 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "chrome/browser/ui/webui/settings/passwords_handler.h" |
| + |
| +#include "base/bind.h" |
| +#include "base/prefs/pref_service.h" |
| +#include "base/strings/string_number_conversions.h" |
| +#include "base/strings/utf_string_conversions.h" |
| +#include "build/build_config.h" |
| +#include "chrome/browser/profiles/profile.h" |
| +#include "chrome/common/pref_names.h" |
| +#include "chrome/grit/generated_resources.h" |
| +#include "components/autofill/core/common/password_form.h" |
| +#include "components/password_manager/core/browser/password_ui_utils.h" |
| +#include "components/url_formatter/url_formatter.h" |
| +#include "content/public/browser/web_contents.h" |
| +#include "content/public/browser/web_ui.h" |
| +#include "content/public/common/origin_util.h" |
| +#include "net/base/net_util.h" |
| +#include "ui/base/l10n/l10n_util.h" |
| + |
| +namespace settings { |
| + |
| +namespace { |
| +// The following constants should be synchronized with the constants in |
| +// chrome/browser/resources/options/password_manager_list.js. |
| +const char kOriginField[] = "origin"; |
| +const char kShownUrlField[] = "shownUrl"; |
| +const char kIsAndroidUriField[] = "isAndroidUri"; |
| +const char kIsSecureField[] = "isSecure"; |
| +const char kUsernameField[] = "username"; |
| +const char kPasswordField[] = "password"; |
| +const char kFederationField[] = "federation"; |
| + |
| +// Copies from |form| to |entry| the origin, shown origin, whether the origin is |
| +// Android URI, and whether the origin is secure. |
| +void CopyOriginInfoOfPasswordForm(const autofill::PasswordForm& form, |
| + const std::string& languages, |
| + base::DictionaryValue* entry) { |
| + entry->SetString( |
| + kOriginField, |
| + url_formatter::FormatUrl( |
| + form.origin, languages, url_formatter::kFormatUrlOmitNothing, |
| + net::UnescapeRule::SPACES, nullptr, nullptr, nullptr)); |
| + bool is_android_uri = false; |
| + entry->SetString(kShownUrlField, password_manager::GetShownOrigin( |
| + form, languages, &is_android_uri)); |
| + entry->SetBoolean(kIsAndroidUriField, is_android_uri); |
| + entry->SetBoolean(kIsSecureField, content::IsOriginSecure(form.origin)); |
| +} |
| + |
| +} // namespace |
| + |
| +PasswordsHandler::PasswordsHandler() : password_manager_presenter_(this) {} |
| + |
| +PasswordsHandler::~PasswordsHandler() {} |
| + |
| +Profile* PasswordsHandler::GetProfile() { |
| + return Profile::FromWebUI(web_ui()); |
| +} |
| + |
| +#if !defined(OS_ANDROID) |
|
michaelpg
2016/01/22 20:54:17
GYP includes this in "chrome_browser_ui_non_mobile
hcarmona
2016/01/26 23:21:43
Good to know, but this file is gone.
|
| +gfx::NativeWindow PasswordsHandler::GetNativeWindow() const { |
| + return web_ui()->GetWebContents()->GetTopLevelNativeWindow(); |
| +} |
| +#endif |
| + |
| +void PasswordsHandler::RegisterMessages() { |
| + password_manager_presenter_.Initialize(); |
| + web_ui()->RegisterMessageCallback( |
| + "updatePasswordLists", |
| + base::Bind(&PasswordsHandler::HandleUpdatePasswordLists, |
| + base::Unretained(this))); |
| + web_ui()->RegisterMessageCallback( |
| + "removeSavedPassword", |
|
michaelpg
2016/01/22 20:54:17
why reimplement these instead of using chrome.pass
hcarmona
2016/01/26 23:21:43
Done. Implemented using chrome.passwordsPrivate.
|
| + base::Bind(&PasswordsHandler::HandleRemoveSavedPassword, |
| + base::Unretained(this))); |
| + web_ui()->RegisterMessageCallback( |
| + "removePasswordException", |
| + base::Bind(&PasswordsHandler::HandleRemovePasswordException, |
| + base::Unretained(this))); |
| + web_ui()->RegisterMessageCallback( |
| + "requestShowPassword", |
| + base::Bind(&PasswordsHandler::HandleRequestShowPassword, |
| + base::Unretained(this))); |
| +} |
| + |
| +void PasswordsHandler::HandleRemoveSavedPassword(const base::ListValue* args) { |
|
michaelpg
2016/01/22 20:54:17
nit: order according to header file
hcarmona
2016/01/26 23:21:43
Acknowledged.
|
| + std::string string_value = base::UTF16ToUTF8(ExtractStringValue(args)); |
|
michaelpg
2016/01/22 20:54:17
here and below: why not pass as an integer?
hcarmona
2016/01/26 23:21:43
Acknowledged.
|
| + int index; |
| + if (base::StringToInt(string_value, &index) && index >= 0) { |
|
michaelpg
2016/01/22 20:54:17
here and below: StringToUint?
DCHECK if false?
hcarmona
2016/01/26 23:21:43
Acknowledged.
|
| + password_manager_presenter_.RemoveSavedPassword(static_cast<size_t>(index)); |
| + } |
| +} |
| + |
| +void PasswordsHandler::HandleRemovePasswordException( |
| + const base::ListValue* args) { |
| + std::string string_value = base::UTF16ToUTF8(ExtractStringValue(args)); |
| + int index; |
| + if (base::StringToInt(string_value, &index) && index >= 0) { |
| + password_manager_presenter_.RemovePasswordException( |
| + static_cast<size_t>(index)); |
| + } |
| +} |
| + |
| +void PasswordsHandler::HandleRequestShowPassword(const base::ListValue* args) { |
| + int index; |
| + if (!ExtractIntegerValue(args, &index)) |
| + NOTREACHED(); |
| + |
| + password_manager_presenter_.RequestShowPassword(static_cast<size_t>(index)); |
| +} |
| + |
| +void PasswordsHandler::ShowPassword(size_t index, |
| + const std::string& origin_url, |
| + const std::string& username, |
| + const base::string16& password_value) { |
| + // Call back the front end to reveal the password. |
| + web_ui()->CallJavascriptFunction( |
| + "settings.PasswordsPrivateApi.showPassword", |
| + base::FundamentalValue(static_cast<int>(index)), |
| + base::StringValue(password_value)); |
| +} |
| + |
| +void PasswordsHandler::HandleUpdatePasswordLists(const base::ListValue* args) { |
| + password_manager_presenter_.UpdatePasswordLists(); |
| +} |
| + |
| +void PasswordsHandler::SetPasswordList( |
| + const std::vector<scoped_ptr<autofill::PasswordForm>>& password_list, |
| + bool show_passwords) { |
| + base::ListValue entries; |
| + languages_ = GetProfile()->GetPrefs()->GetString(prefs::kAcceptLanguages); |
| + base::string16 placeholder(base::ASCIIToUTF16(" ")); |
| + for (const auto& saved_password : password_list) { |
| + scoped_ptr<base::DictionaryValue> entry(new base::DictionaryValue); |
| + CopyOriginInfoOfPasswordForm(*saved_password, languages_, entry.get()); |
| + |
| + entry->SetString(kUsernameField, saved_password->username_value); |
| + if (show_passwords) { |
| + entry->SetString(kPasswordField, saved_password->password_value); |
| + } else { |
| + // Use a placeholder value with the same length as the password. |
| + entry->SetString( |
| + kPasswordField, |
| + base::string16(saved_password->password_value.length(), ' ')); |
| + } |
| + const GURL& federation_url = saved_password->federation_url; |
| + if (!federation_url.is_empty()) { |
| + entry->SetString( |
| + kFederationField, |
| + l10n_util::GetStringFUTF16(IDS_PASSWORDS_VIA_FEDERATION, |
| + base::UTF8ToUTF16(federation_url.host()))); |
| + } |
| + |
| + entries.Append(entry.release()); |
| + } |
| + |
| + web_ui()->CallJavascriptFunction( |
| + "settings.PasswordsPrivateApi.setSavedPasswordsList", entries); |
| +} |
| + |
| +void PasswordsHandler::SetPasswordExceptionList( |
| + const std::vector<scoped_ptr<autofill::PasswordForm>>& |
| + password_exception_list) { |
| + base::ListValue entries; |
| + for (const auto& exception : password_exception_list) { |
| + scoped_ptr<base::DictionaryValue> entry(new base::DictionaryValue); |
| + CopyOriginInfoOfPasswordForm(*exception, languages_, entry.get()); |
| + entries.Append(entry.release()); |
| + } |
| + |
| + web_ui()->CallJavascriptFunction( |
| + "settings.PasswordsPrivateApi.setPasswordExceptionsList", entries); |
| +} |
| + |
| +} // namespace settings |