| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2016 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. |
| 4 |
| 5 #include "chrome/browser/ui/webui/settings/passwords_handler.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/prefs/pref_service.h" |
| 9 #include "base/strings/string_number_conversions.h" |
| 10 #include "base/strings/utf_string_conversions.h" |
| 11 #include "build/build_config.h" |
| 12 #include "chrome/browser/profiles/profile.h" |
| 13 #include "chrome/common/pref_names.h" |
| 14 #include "chrome/grit/generated_resources.h" |
| 15 #include "components/autofill/core/common/password_form.h" |
| 16 #include "components/password_manager/core/browser/password_ui_utils.h" |
| 17 #include "components/url_formatter/url_formatter.h" |
| 18 #include "content/public/browser/web_contents.h" |
| 19 #include "content/public/browser/web_ui.h" |
| 20 #include "content/public/common/origin_util.h" |
| 21 #include "net/base/escape.h" |
| 22 #include "ui/base/l10n/l10n_util.h" |
| 23 |
| 24 namespace settings { |
| 25 |
| 26 namespace { |
| 27 // The following constants should be synchronized with the constants in |
| 28 // chrome/browser/resources/options/password_manager_list.js. |
| 29 const char kOriginField[] = "origin"; |
| 30 const char kShownUrlField[] = "shownUrl"; |
| 31 const char kIsAndroidUriField[] = "isAndroidUri"; |
| 32 const char kIsSecureField[] = "isSecure"; |
| 33 const char kUsernameField[] = "username"; |
| 34 const char kPasswordField[] = "password"; |
| 35 const char kFederationField[] = "federation"; |
| 36 |
| 37 // Copies from |form| to |entry| the origin, shown origin, whether the origin is |
| 38 // Android URI, and whether the origin is secure. |
| 39 void CopyOriginInfoOfPasswordForm(const autofill::PasswordForm& form, |
| 40 const std::string& languages, |
| 41 base::DictionaryValue* entry) { |
| 42 entry->SetString( |
| 43 kOriginField, |
| 44 url_formatter::FormatUrl( |
| 45 form.origin, languages, url_formatter::kFormatUrlOmitNothing, |
| 46 net::UnescapeRule::SPACES, nullptr, nullptr, nullptr)); |
| 47 bool is_android_uri = false; |
| 48 entry->SetString(kShownUrlField, password_manager::GetShownOrigin( |
| 49 form, languages, &is_android_uri)); |
| 50 entry->SetBoolean(kIsAndroidUriField, is_android_uri); |
| 51 entry->SetBoolean(kIsSecureField, content::IsOriginSecure(form.origin)); |
| 52 } |
| 53 |
| 54 } // namespace |
| 55 |
| 56 PasswordsHandler::PasswordsHandler() : password_manager_presenter_(this) {} |
| 57 |
| 58 PasswordsHandler::~PasswordsHandler() {} |
| 59 |
| 60 Profile* PasswordsHandler::GetProfile() { |
| 61 return Profile::FromWebUI(web_ui()); |
| 62 } |
| 63 |
| 64 #if !defined(OS_ANDROID) |
| 65 gfx::NativeWindow PasswordsHandler::GetNativeWindow() const { |
| 66 return web_ui()->GetWebContents()->GetTopLevelNativeWindow(); |
| 67 } |
| 68 #endif |
| 69 |
| 70 void PasswordsHandler::RegisterMessages() { |
| 71 password_manager_presenter_.Initialize(); |
| 72 web_ui()->RegisterMessageCallback( |
| 73 "updatePasswordLists", |
| 74 base::Bind(&PasswordsHandler::HandleUpdatePasswordLists, |
| 75 base::Unretained(this))); |
| 76 web_ui()->RegisterMessageCallback( |
| 77 "removeSavedPassword", |
| 78 base::Bind(&PasswordsHandler::HandleRemoveSavedPassword, |
| 79 base::Unretained(this))); |
| 80 web_ui()->RegisterMessageCallback( |
| 81 "removePasswordException", |
| 82 base::Bind(&PasswordsHandler::HandleRemovePasswordException, |
| 83 base::Unretained(this))); |
| 84 web_ui()->RegisterMessageCallback( |
| 85 "requestShowPassword", |
| 86 base::Bind(&PasswordsHandler::HandleRequestShowPassword, |
| 87 base::Unretained(this))); |
| 88 } |
| 89 |
| 90 void PasswordsHandler::HandleRemoveSavedPassword(const base::ListValue* args) { |
| 91 std::string string_value = base::UTF16ToUTF8(ExtractStringValue(args)); |
| 92 int index; |
| 93 if (base::StringToInt(string_value, &index) && index >= 0) { |
| 94 password_manager_presenter_.RemoveSavedPassword(static_cast<size_t>(index)); |
| 95 } |
| 96 } |
| 97 |
| 98 void PasswordsHandler::HandleRemovePasswordException( |
| 99 const base::ListValue* args) { |
| 100 std::string string_value = base::UTF16ToUTF8(ExtractStringValue(args)); |
| 101 int index; |
| 102 if (base::StringToInt(string_value, &index) && index >= 0) { |
| 103 password_manager_presenter_.RemovePasswordException( |
| 104 static_cast<size_t>(index)); |
| 105 } |
| 106 } |
| 107 |
| 108 void PasswordsHandler::HandleRequestShowPassword(const base::ListValue* args) { |
| 109 int index; |
| 110 if (!ExtractIntegerValue(args, &index)) |
| 111 NOTREACHED(); |
| 112 |
| 113 password_manager_presenter_.RequestShowPassword(static_cast<size_t>(index)); |
| 114 } |
| 115 |
| 116 void PasswordsHandler::ShowPassword(size_t index, |
| 117 const std::string& origin_url, |
| 118 const std::string& username, |
| 119 const base::string16& password_value) { |
| 120 // Call back the front end to reveal the password. |
| 121 web_ui()->CallJavascriptFunction( |
| 122 "settings.PasswordsPrivateApi.showPassword", |
| 123 base::FundamentalValue(static_cast<int>(index)), |
| 124 base::StringValue(password_value)); |
| 125 } |
| 126 |
| 127 void PasswordsHandler::HandleUpdatePasswordLists(const base::ListValue* args) { |
| 128 password_manager_presenter_.UpdatePasswordLists(); |
| 129 } |
| 130 |
| 131 void PasswordsHandler::SetPasswordList( |
| 132 const std::vector<scoped_ptr<autofill::PasswordForm>>& password_list, |
| 133 bool show_passwords) { |
| 134 base::ListValue entries; |
| 135 languages_ = GetProfile()->GetPrefs()->GetString(prefs::kAcceptLanguages); |
| 136 base::string16 placeholder(base::ASCIIToUTF16(" ")); |
| 137 for (const auto& saved_password : password_list) { |
| 138 scoped_ptr<base::DictionaryValue> entry(new base::DictionaryValue); |
| 139 CopyOriginInfoOfPasswordForm(*saved_password, languages_, entry.get()); |
| 140 |
| 141 entry->SetString(kUsernameField, saved_password->username_value); |
| 142 if (show_passwords) { |
| 143 entry->SetString(kPasswordField, saved_password->password_value); |
| 144 } else { |
| 145 // Use a placeholder value with the same length as the password. |
| 146 entry->SetString( |
| 147 kPasswordField, |
| 148 base::string16(saved_password->password_value.length(), ' ')); |
| 149 } |
| 150 const GURL& federation_url = saved_password->federation_url; |
| 151 if (!federation_url.is_empty()) { |
| 152 entry->SetString( |
| 153 kFederationField, |
| 154 l10n_util::GetStringFUTF16(IDS_PASSWORDS_VIA_FEDERATION, |
| 155 base::UTF8ToUTF16(federation_url.host()))); |
| 156 } |
| 157 |
| 158 entries.Append(entry.release()); |
| 159 } |
| 160 |
| 161 web_ui()->CallJavascriptFunction( |
| 162 "settings.PasswordsPrivateApi.setSavedPasswordsList", entries); |
| 163 } |
| 164 |
| 165 void PasswordsHandler::SetPasswordExceptionList( |
| 166 const std::vector<scoped_ptr<autofill::PasswordForm>>& |
| 167 password_exception_list) { |
| 168 base::ListValue entries; |
| 169 for (const auto& exception : password_exception_list) { |
| 170 scoped_ptr<base::DictionaryValue> entry(new base::DictionaryValue); |
| 171 CopyOriginInfoOfPasswordForm(*exception, languages_, entry.get()); |
| 172 entries.Append(entry.release()); |
| 173 } |
| 174 |
| 175 web_ui()->CallJavascriptFunction( |
| 176 "settings.PasswordsPrivateApi.setPasswordExceptionsList", entries); |
| 177 } |
| 178 |
| 179 } // namespace settings |
| OLD | NEW |