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"
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 "base/values.h"
12 #include "chrome/browser/chrome_notification_types.h"
13 #include "chrome/browser/profiles/profile.h"
14 #if defined(OS_WIN) && defined(USE_ASH)
15 #include "chrome/browser/ui/ash/ash_util.h"
17 #include "chrome/common/pref_names.h"
18 #include "chrome/common/url_constants.h"
19 #include "components/autofill/core/common/password_form.h"
20 #include "content/public/browser/notification_details.h"
21 #include "content/public/browser/notification_source.h"
22 #include "content/public/browser/user_metrics.h"
23 #include "content/public/browser/web_contents.h"
24 #include "content/public/browser/web_contents_view.h"
25 #include "content/public/browser/web_ui.h"
26 #include "grit/chromium_strings.h"
27 #include "grit/generated_resources.h"
28 #include "net/base/net_util.h"
29 #include "ui/base/l10n/l10n_util.h"
33 PasswordManagerHandler::PasswordManagerHandler()
34 : password_manager_presenter_(this) {}
36 PasswordManagerHandler::~PasswordManagerHandler() {}
38 Profile
* PasswordManagerHandler::GetProfile() {
39 return Profile::FromWebUI(web_ui());
42 #if !defined(OS_ANDROID)
43 gfx::NativeWindow
PasswordManagerHandler::GetNativeWindow() {
44 return web_ui()->GetWebContents()->GetView()->GetTopLevelNativeWindow();
48 void PasswordManagerHandler::GetLocalizedValues(
49 base::DictionaryValue
* localized_strings
) {
50 DCHECK(localized_strings
);
52 static const OptionsStringResource resources
[] = {
53 { "savedPasswordsTitle",
54 IDS_PASSWORDS_SHOW_PASSWORDS_TAB_TITLE
},
55 { "passwordExceptionsTitle",
56 IDS_PASSWORDS_EXCEPTIONS_TAB_TITLE
},
57 { "passwordSearchPlaceholder",
58 IDS_PASSWORDS_PAGE_SEARCH_PASSWORDS
},
59 { "passwordShowButton",
60 IDS_PASSWORDS_PAGE_VIEW_SHOW_BUTTON
},
61 { "passwordHideButton",
62 IDS_PASSWORDS_PAGE_VIEW_HIDE_BUTTON
},
63 { "passwordsNoPasswordsDescription",
64 IDS_PASSWORDS_PAGE_VIEW_NO_PASSWORDS_DESCRIPTION
},
65 { "passwordsNoExceptionsDescription",
66 IDS_PASSWORDS_PAGE_VIEW_NO_EXCEPTIONS_DESCRIPTION
},
69 RegisterStrings(localized_strings
, resources
, arraysize(resources
));
70 RegisterTitle(localized_strings
, "passwordsPage",
71 IDS_PASSWORDS_EXCEPTIONS_WINDOW_TITLE
);
73 localized_strings
->SetString("passwordManagerLearnMoreURL",
74 chrome::kPasswordManagerLearnMoreURL
);
75 bool disable_show_passwords
= false;
77 #if defined(OS_WIN) && defined(USE_ASH)
78 // We disable the ability to show passwords when running in Windows Metro
79 // interface. This is because we cannot pop native Win32 dialogs from the
81 // TODO(wfh): Revisit this if Metro usage grows.
82 if (chrome::IsNativeWindowInAsh(GetNativeWindow()))
83 disable_show_passwords
= true;
86 localized_strings
->SetBoolean("disableShowPasswords", disable_show_passwords
);
89 void PasswordManagerHandler::RegisterMessages() {
90 web_ui()->RegisterMessageCallback(
91 "updatePasswordLists",
92 base::Bind(&PasswordManagerHandler::HandleUpdatePasswordLists
,
93 base::Unretained(this)));
94 web_ui()->RegisterMessageCallback(
95 "removeSavedPassword",
96 base::Bind(&PasswordManagerHandler::HandleRemoveSavedPassword
,
97 base::Unretained(this)));
98 web_ui()->RegisterMessageCallback(
99 "removePasswordException",
100 base::Bind(&PasswordManagerHandler::HandleRemovePasswordException
,
101 base::Unretained(this)));
102 web_ui()->RegisterMessageCallback(
103 "requestShowPassword",
104 base::Bind(&PasswordManagerHandler::HandleRequestShowPassword
,
105 base::Unretained(this)));
108 void PasswordManagerHandler::InitializeHandler() {
109 password_manager_presenter_
.Initialize();
112 void PasswordManagerHandler::HandleRemoveSavedPassword(
113 const base::ListValue
* args
) {
114 std::string string_value
= base::UTF16ToUTF8(ExtractStringValue(args
));
116 if (base::StringToInt(string_value
, &index
) && index
>= 0) {
117 password_manager_presenter_
.RemoveSavedPassword(static_cast<size_t>(index
));
121 void PasswordManagerHandler::HandleRemovePasswordException(
122 const base::ListValue
* args
) {
123 std::string string_value
= base::UTF16ToUTF8(ExtractStringValue(args
));
125 if (base::StringToInt(string_value
, &index
) && index
>= 0) {
126 password_manager_presenter_
.RemovePasswordException(
127 static_cast<size_t>(index
));
131 void PasswordManagerHandler::HandleRequestShowPassword(
132 const base::ListValue
* args
) {
134 if (!ExtractIntegerValue(args
, &index
))
137 password_manager_presenter_
.RequestShowPassword(static_cast<size_t>(index
));
140 void PasswordManagerHandler::ShowPassword(
142 const base::string16
& password_value
) {
143 // Call back the front end to reveal the password.
144 web_ui()->CallJavascriptFunction(
145 "PasswordManager.showPassword",
146 base::FundamentalValue(static_cast<int>(index
)),
147 base::StringValue(password_value
));
150 void PasswordManagerHandler::HandleUpdatePasswordLists(
151 const base::ListValue
* args
) {
152 password_manager_presenter_
.UpdatePasswordLists();
155 void PasswordManagerHandler::SetPasswordList(
156 const ScopedVector
<autofill::PasswordForm
>& password_list
,
157 bool show_passwords
) {
158 base::ListValue entries
;
159 languages_
= GetProfile()->GetPrefs()->GetString(prefs::kAcceptLanguages
);
160 base::string16
placeholder(base::ASCIIToUTF16(" "));
161 for (size_t i
= 0; i
< password_list
.size(); ++i
) {
162 base::ListValue
* entry
= new base::ListValue();
163 entry
->Append(new base::StringValue(net::FormatUrl(password_list
[i
]->origin
,
165 entry
->Append(new base::StringValue(password_list
[i
]->username_value
));
166 if (show_passwords
) {
167 entry
->Append(new base::StringValue(password_list
[i
]->password_value
));
169 // Use a placeholder value with the same length as the password.
170 entry
->Append(new base::StringValue(
171 base::string16(password_list
[i
]->password_value
.length(), ' ')));
173 entries
.Append(entry
);
176 web_ui()->CallJavascriptFunction("PasswordManager.setSavedPasswordsList",
180 void PasswordManagerHandler::SetPasswordExceptionList(
181 const ScopedVector
<autofill::PasswordForm
>& password_exception_list
) {
182 base::ListValue entries
;
183 for (size_t i
= 0; i
< password_exception_list
.size(); ++i
) {
184 entries
.Append(new base::StringValue(
185 net::FormatUrl(password_exception_list
[i
]->origin
, languages_
)));
188 web_ui()->CallJavascriptFunction("PasswordManager.setPasswordExceptionsList",
192 } // namespace options