Prepare password_bubble_experiment and password_manager_util for componentisation
[chromium-blink-merge.git] / chrome / browser / ui / webui / options / password_manager_handler.cc
blob24d2e4479e82ace03a53df968bc1c897260f41d5
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/browser/ui/passwords/password_bubble_experiment.h"
22 #include "chrome/common/pref_names.h"
23 #include "chrome/common/url_constants.h"
24 #include "chrome/grit/generated_resources.h"
25 #include "components/autofill/core/common/password_form.h"
26 #include "components/password_manager/core/browser/affiliation_utils.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_PASSWORDS_EXCEPTIONS_SMART_LOCK_WINDOW_TITLE :
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 offset + 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 base::string16& password_value) {
188 // Call back the front end to reveal the password.
189 web_ui()->CallJavascriptFunction(
190 "PasswordManager.showPassword",
191 base::FundamentalValue(static_cast<int>(index)),
192 base::StringValue(password_value));
195 void PasswordManagerHandler::HandleUpdatePasswordLists(
196 const base::ListValue* args) {
197 password_manager_presenter_.UpdatePasswordLists();
200 void PasswordManagerHandler::SetPasswordList(
201 const ScopedVector<autofill::PasswordForm>& password_list,
202 bool show_passwords) {
203 base::ListValue entries;
204 languages_ = GetProfile()->GetPrefs()->GetString(prefs::kAcceptLanguages);
205 base::string16 placeholder(base::ASCIIToUTF16(" "));
206 for (size_t i = 0; i < password_list.size(); ++i) {
207 base::ListValue* entry = new base::ListValue();
208 entry->AppendString(password_manager::GetHumanReadableOrigin(
209 *password_list[i], languages_));
210 entry->AppendString(password_list[i]->username_value);
211 if (show_passwords) {
212 entry->AppendString(password_list[i]->password_value);
213 } else {
214 // Use a placeholder value with the same length as the password.
215 entry->AppendString(
216 base::string16(password_list[i]->password_value.length(), ' '));
218 const GURL& federation_url = password_list[i]->federation_url;
219 if (!federation_url.is_empty()) {
220 entry->AppendString(l10n_util::GetStringFUTF16(
221 IDS_PASSWORDS_VIA_FEDERATION,
222 base::UTF8ToUTF16(federation_url.host())));
224 entries.Append(entry);
227 web_ui()->CallJavascriptFunction("PasswordManager.setSavedPasswordsList",
228 entries);
231 void PasswordManagerHandler::SetPasswordExceptionList(
232 const ScopedVector<autofill::PasswordForm>& password_exception_list) {
233 base::ListValue entries;
234 for (size_t i = 0; i < password_exception_list.size(); ++i) {
235 entries.AppendString(password_manager::GetHumanReadableOrigin(
236 *password_exception_list[i], languages_));
239 web_ui()->CallJavascriptFunction("PasswordManager.setPasswordExceptionsList",
240 entries);
243 } // namespace options