Popular sites on the NTP: check that experiment group StartsWith (rather than IS...
[chromium-blink-merge.git] / chrome / browser / ui / webui / options / password_manager_handler.cc
blob43198c1ba3b0cc34ff268e69cf3e08e678ef6a57
1 // Copyright 2013 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/ui/webui/options/password_manager_handler.h"
7 #include "base/bind.h"
8 #include "base/command_line.h"
9 #include "base/prefs/pref_service.h"
10 #include "base/strings/string_number_conversions.h"
11 #include "base/strings/string_split.h"
12 #include "base/strings/utf_string_conversions.h"
13 #include "base/values.h"
14 #include "chrome/browser/chrome_notification_types.h"
15 #include "chrome/browser/profiles/profile.h"
16 #include "chrome/browser/sync/profile_sync_service.h"
17 #include "chrome/browser/sync/profile_sync_service_factory.h"
18 #if defined(OS_WIN) && defined(USE_ASH)
19 #include "chrome/browser/ui/ash/ash_util.h"
20 #endif
21 #include "chrome/common/pref_names.h"
22 #include "chrome/common/url_constants.h"
23 #include "chrome/grit/generated_resources.h"
24 #include "components/autofill/core/common/password_form.h"
25 #include "components/password_manager/core/browser/affiliation_utils.h"
26 #include "components/password_manager/core/browser/password_bubble_experiment.h"
27 #include "components/password_manager/core/common/experiments.h"
28 #include "content/public/browser/notification_details.h"
29 #include "content/public/browser/notification_source.h"
30 #include "content/public/browser/user_metrics.h"
31 #include "content/public/browser/web_contents.h"
32 #include "content/public/browser/web_ui.h"
33 #include "content/public/common/content_switches.h"
34 #include "net/base/net_util.h"
35 #include "ui/base/l10n/l10n_util.h"
37 namespace options {
39 PasswordManagerHandler::PasswordManagerHandler()
40 : password_manager_presenter_(this) {}
42 PasswordManagerHandler::~PasswordManagerHandler() {}
44 Profile* PasswordManagerHandler::GetProfile() {
45 return Profile::FromWebUI(web_ui());
48 #if !defined(OS_ANDROID)
49 gfx::NativeWindow PasswordManagerHandler::GetNativeWindow() const {
50 return web_ui()->GetWebContents()->GetTopLevelNativeWindow();
52 #endif
54 void PasswordManagerHandler::GetLocalizedValues(
55 base::DictionaryValue* localized_strings) {
56 DCHECK(localized_strings);
58 static const OptionsStringResource resources[] = {
59 { "autoSigninTitle",
60 IDS_PASSWORDS_AUTO_SIGNIN_TITLE },
61 { "autoSigninDescription",
62 IDS_PASSWORDS_AUTO_SIGNIN_DESCRIPTION },
63 { "savedPasswordsTitle",
64 IDS_PASSWORDS_SHOW_PASSWORDS_TAB_TITLE },
65 { "passwordExceptionsTitle",
66 IDS_PASSWORDS_EXCEPTIONS_TAB_TITLE },
67 { "passwordSearchPlaceholder",
68 IDS_PASSWORDS_PAGE_SEARCH_PASSWORDS },
69 { "passwordShowButton",
70 IDS_PASSWORDS_PAGE_VIEW_SHOW_BUTTON },
71 { "passwordHideButton",
72 IDS_PASSWORDS_PAGE_VIEW_HIDE_BUTTON },
73 { "passwordsNoPasswordsDescription",
74 IDS_PASSWORDS_PAGE_VIEW_NO_PASSWORDS_DESCRIPTION },
75 { "passwordsNoExceptionsDescription",
76 IDS_PASSWORDS_PAGE_VIEW_NO_EXCEPTIONS_DESCRIPTION },
79 RegisterStrings(localized_strings, resources, arraysize(resources));
81 const ProfileSyncService* sync_service =
82 ProfileSyncServiceFactory::GetForProfile(GetProfile());
83 int title_id =
84 password_bubble_experiment::IsSmartLockBrandingEnabled(sync_service) ?
85 IDS_PASSWORD_MANAGER_SMART_LOCK_FOR_PASSWORDS :
86 IDS_PASSWORDS_EXCEPTIONS_WINDOW_TITLE;
87 RegisterTitle(localized_strings, "passwordsPage", title_id);
89 localized_strings->SetString("passwordManagerLearnMoreURL",
90 chrome::kPasswordManagerLearnMoreURL);
91 localized_strings->SetString("passwordsManagePasswordsLink",
92 chrome::kPasswordManagerAccountDashboardURL);
94 std::string management_hostname =
95 GURL(chrome::kPasswordManagerAccountDashboardURL).host();
96 base::string16 link_text = base::UTF8ToUTF16(management_hostname);
97 size_t offset;
98 base::string16 full_text = l10n_util::GetStringFUTF16(
99 IDS_MANAGE_PASSWORDS_REMOTE_TEXT, link_text, &offset);
101 localized_strings->SetString("passwordsManagePasswordsBeforeLinkText",
102 full_text.substr(0, offset));
103 localized_strings->SetString("passwordsManagePasswordsLinkText",
104 full_text.substr(offset,
105 link_text.size()));
106 localized_strings->SetString("passwordsManagePasswordsAfterLinkText",
107 full_text.substr(offset + link_text.size()));
109 bool disable_show_passwords = false;
111 #if defined(OS_WIN) && defined(USE_ASH)
112 // We disable the ability to show passwords when running in Windows Metro
113 // interface. This is because we cannot pop native Win32 dialogs from the
114 // Metro process.
115 // TODO(wfh): Revisit this if Metro usage grows.
116 if (chrome::IsNativeWindowInAsh(GetNativeWindow()))
117 disable_show_passwords = true;
118 #endif
120 localized_strings->SetBoolean("disableShowPasswords", disable_show_passwords);
121 localized_strings->SetBoolean(
122 "enableCredentialManagerAPI",
123 base::CommandLine::ForCurrentProcess()->HasSwitch(
124 switches::kEnableCredentialManagerAPI));
127 void PasswordManagerHandler::RegisterMessages() {
128 web_ui()->RegisterMessageCallback(
129 "updatePasswordLists",
130 base::Bind(&PasswordManagerHandler::HandleUpdatePasswordLists,
131 base::Unretained(this)));
132 web_ui()->RegisterMessageCallback(
133 "removeSavedPassword",
134 base::Bind(&PasswordManagerHandler::HandleRemoveSavedPassword,
135 base::Unretained(this)));
136 web_ui()->RegisterMessageCallback(
137 "removePasswordException",
138 base::Bind(&PasswordManagerHandler::HandleRemovePasswordException,
139 base::Unretained(this)));
140 web_ui()->RegisterMessageCallback(
141 "requestShowPassword",
142 base::Bind(&PasswordManagerHandler::HandleRequestShowPassword,
143 base::Unretained(this)));
146 void PasswordManagerHandler::InitializeHandler() {
147 password_manager_presenter_.Initialize();
150 void PasswordManagerHandler::InitializePage() {
151 base::FundamentalValue visible(
152 password_manager::ManageAccountLinkExperimentEnabled());
153 web_ui()->CallJavascriptFunction(
154 "PasswordManager.setManageAccountLinkVisibility", visible);
157 void PasswordManagerHandler::HandleRemoveSavedPassword(
158 const base::ListValue* args) {
159 std::string string_value = base::UTF16ToUTF8(ExtractStringValue(args));
160 int index;
161 if (base::StringToInt(string_value, &index) && index >= 0) {
162 password_manager_presenter_.RemoveSavedPassword(static_cast<size_t>(index));
166 void PasswordManagerHandler::HandleRemovePasswordException(
167 const base::ListValue* args) {
168 std::string string_value = base::UTF16ToUTF8(ExtractStringValue(args));
169 int index;
170 if (base::StringToInt(string_value, &index) && index >= 0) {
171 password_manager_presenter_.RemovePasswordException(
172 static_cast<size_t>(index));
176 void PasswordManagerHandler::HandleRequestShowPassword(
177 const base::ListValue* args) {
178 int index;
179 if (!ExtractIntegerValue(args, &index))
180 NOTREACHED();
182 password_manager_presenter_.RequestShowPassword(static_cast<size_t>(index));
185 void PasswordManagerHandler::ShowPassword(
186 size_t index,
187 const std::string& origin_url,
188 const std::string& username,
189 const base::string16& password_value) {
190 // Call back the front end to reveal the password.
191 web_ui()->CallJavascriptFunction(
192 "PasswordManager.showPassword",
193 base::FundamentalValue(static_cast<int>(index)),
194 base::StringValue(password_value));
197 void PasswordManagerHandler::HandleUpdatePasswordLists(
198 const base::ListValue* args) {
199 password_manager_presenter_.UpdatePasswordLists();
202 void PasswordManagerHandler::SetPasswordList(
203 const ScopedVector<autofill::PasswordForm>& password_list,
204 bool show_passwords) {
205 base::ListValue entries;
206 languages_ = GetProfile()->GetPrefs()->GetString(prefs::kAcceptLanguages);
207 base::string16 placeholder(base::ASCIIToUTF16(" "));
208 for (size_t i = 0; i < password_list.size(); ++i) {
209 base::ListValue* entry = new base::ListValue();
210 entry->AppendString(password_manager::GetHumanReadableOrigin(
211 *password_list[i], languages_));
212 entry->AppendString(password_list[i]->username_value);
213 if (show_passwords) {
214 entry->AppendString(password_list[i]->password_value);
215 } else {
216 // Use a placeholder value with the same length as the password.
217 entry->AppendString(
218 base::string16(password_list[i]->password_value.length(), ' '));
220 const GURL& federation_url = password_list[i]->federation_url;
221 if (!federation_url.is_empty()) {
222 entry->AppendString(l10n_util::GetStringFUTF16(
223 IDS_PASSWORDS_VIA_FEDERATION,
224 base::UTF8ToUTF16(federation_url.host())));
226 entries.Append(entry);
229 web_ui()->CallJavascriptFunction("PasswordManager.setSavedPasswordsList",
230 entries);
233 void PasswordManagerHandler::SetPasswordExceptionList(
234 const ScopedVector<autofill::PasswordForm>& password_exception_list) {
235 base::ListValue entries;
236 for (size_t i = 0; i < password_exception_list.size(); ++i) {
237 entries.AppendString(password_manager::GetHumanReadableOrigin(
238 *password_exception_list[i], languages_));
241 web_ui()->CallJavascriptFunction("PasswordManager.setPasswordExceptionsList",
242 entries);
245 } // namespace options