Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(122)

Side by Side Diff: chrome/browser/ui/webui/settings/passwords_handler.cc

Issue 1591053002: Add a password handler to get the list of passwords in md-settings. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: feedback + closure compile Created 4 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/ui/webui/options/password_manager_handler.h" 5 #include "chrome/browser/ui/webui/settings/passwords_handler.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/command_line.h" 8 #include "base/command_line.h"
9 #include "base/macros.h" 9 #include "base/macros.h"
10 #include "base/prefs/pref_service.h" 10 #include "base/prefs/pref_service.h"
11 #include "base/strings/string_number_conversions.h" 11 #include "base/strings/string_number_conversions.h"
12 #include "base/strings/string_split.h" 12 #include "base/strings/string_split.h"
13 #include "base/strings/utf_string_conversions.h" 13 #include "base/strings/utf_string_conversions.h"
14 #include "base/values.h" 14 #include "base/values.h"
15 #include "build/build_config.h" 15 #include "build/build_config.h"
16 #include "chrome/browser/chrome_notification_types.h" 16 #include "chrome/browser/chrome_notification_types.h"
17 #include "chrome/browser/profiles/profile.h" 17 #include "chrome/browser/profiles/profile.h"
18 #include "chrome/browser/sync/profile_sync_service_factory.h" 18 #include "chrome/browser/sync/profile_sync_service_factory.h"
michaelpg 2016/01/16 07:12:48 get rid of the includes you don't use
hcarmona 2016/01/20 02:32:50 I've removed a bunch. Compiles on my machine, tryb
19 #include "chrome/common/pref_names.h" 19 #include "chrome/common/pref_names.h"
20 #include "chrome/common/url_constants.h" 20 #include "chrome/common/url_constants.h"
21 #include "chrome/grit/generated_resources.h" 21 #include "chrome/grit/generated_resources.h"
22 #include "components/autofill/core/common/password_form.h" 22 #include "components/autofill/core/common/password_form.h"
23 #include "components/browser_sync/browser/profile_sync_service.h" 23 #include "components/browser_sync/browser/profile_sync_service.h"
24 #include "components/password_manager/core/browser/password_bubble_experiment.h" 24 #include "components/password_manager/core/browser/password_bubble_experiment.h"
25 #include "components/password_manager/core/browser/password_manager_constants.h" 25 #include "components/password_manager/core/browser/password_manager_constants.h"
26 #include "components/password_manager/core/browser/password_ui_utils.h" 26 #include "components/password_manager/core/browser/password_ui_utils.h"
27 #include "components/password_manager/core/common/experiments.h" 27 #include "components/password_manager/core/common/experiments.h"
28 #include "components/url_formatter/url_formatter.h" 28 #include "components/url_formatter/url_formatter.h"
29 #include "content/public/browser/notification_details.h" 29 #include "content/public/browser/notification_details.h"
30 #include "content/public/browser/notification_source.h" 30 #include "content/public/browser/notification_source.h"
31 #include "content/public/browser/user_metrics.h" 31 #include "content/public/browser/user_metrics.h"
32 #include "content/public/browser/web_contents.h" 32 #include "content/public/browser/web_contents.h"
33 #include "content/public/browser/web_ui.h" 33 #include "content/public/browser/web_ui.h"
34 #include "content/public/common/content_switches.h" 34 #include "content/public/common/content_switches.h"
35 #include "content/public/common/origin_util.h" 35 #include "content/public/common/origin_util.h"
36 #include "net/base/net_util.h" 36 #include "net/base/net_util.h"
37 #include "ui/base/l10n/l10n_util.h" 37 #include "ui/base/l10n/l10n_util.h"
38 38
39 #if defined(OS_WIN) && defined(USE_ASH) 39 #if defined(OS_WIN) && defined(USE_ASH)
40 #include "chrome/browser/ui/ash/ash_util.h" 40 #include "chrome/browser/ui/ash/ash_util.h"
41 #endif 41 #endif
42 42
43 namespace options { 43 namespace settings {
44 44
45 namespace { 45 namespace {
46 // The following constants should be synchronized with the constants in 46 // The following constants should be synchronized with the constants in
47 // chrome/browser/resources/options/password_manager_list.js. 47 // chrome/browser/resources/options/password_manager_list.js.
48 const char kOriginField[] = "origin"; 48 const char kOriginField[] = "origin";
49 const char kShownUrlField[] = "shownUrl"; 49 const char kShownUrlField[] = "shownUrl";
50 const char kIsAndroidUriField[] = "isAndroidUri"; 50 const char kIsAndroidUriField[] = "isAndroidUri";
51 const char kIsSecureField[] = "isSecure"; 51 const char kIsSecureField[] = "isSecure";
52 const char kUsernameField[] = "username"; 52 const char kUsernameField[] = "username";
53 const char kPasswordField[] = "password"; 53 const char kPasswordField[] = "password";
(...skipping 11 matching lines...) Expand all
65 net::UnescapeRule::SPACES, nullptr, nullptr, nullptr)); 65 net::UnescapeRule::SPACES, nullptr, nullptr, nullptr));
66 bool is_android_uri = false; 66 bool is_android_uri = false;
67 entry->SetString(kShownUrlField, password_manager::GetShownOrigin( 67 entry->SetString(kShownUrlField, password_manager::GetShownOrigin(
68 form, languages, &is_android_uri)); 68 form, languages, &is_android_uri));
69 entry->SetBoolean(kIsAndroidUriField, is_android_uri); 69 entry->SetBoolean(kIsAndroidUriField, is_android_uri);
70 entry->SetBoolean(kIsSecureField, content::IsOriginSecure(form.origin)); 70 entry->SetBoolean(kIsSecureField, content::IsOriginSecure(form.origin));
71 } 71 }
72 72
73 } // namespace 73 } // namespace
74 74
75 PasswordManagerHandler::PasswordManagerHandler() 75 PasswordsHandler::PasswordsHandler() : password_manager_presenter_(this) {}
76 : password_manager_presenter_(this) {}
77 76
78 PasswordManagerHandler::~PasswordManagerHandler() {} 77 PasswordsHandler::~PasswordsHandler() {}
79 78
80 Profile* PasswordManagerHandler::GetProfile() { 79 Profile* PasswordsHandler::GetProfile() {
81 return Profile::FromWebUI(web_ui()); 80 return Profile::FromWebUI(web_ui());
82 } 81 }
83 82
84 #if !defined(OS_ANDROID) 83 #if !defined(OS_ANDROID)
85 gfx::NativeWindow PasswordManagerHandler::GetNativeWindow() const { 84 gfx::NativeWindow PasswordsHandler::GetNativeWindow() const {
86 return web_ui()->GetWebContents()->GetTopLevelNativeWindow(); 85 return web_ui()->GetWebContents()->GetTopLevelNativeWindow();
87 } 86 }
88 #endif 87 #endif
89 88
90 void PasswordManagerHandler::GetLocalizedValues( 89 void PasswordsHandler::RegisterMessages() {
91 base::DictionaryValue* localized_strings) { 90 password_manager_presenter_.Initialize();
92 DCHECK(localized_strings);
93
94 static const OptionsStringResource resources[] = {
95 {"autoSigninTitle", IDS_PASSWORDS_AUTO_SIGNIN_TITLE},
96 {"autoSigninDescription", IDS_PASSWORDS_AUTO_SIGNIN_DESCRIPTION},
97 {"savedPasswordsTitle", IDS_PASSWORDS_SHOW_PASSWORDS_TAB_TITLE},
98 {"passwordExceptionsTitle", IDS_PASSWORDS_EXCEPTIONS_TAB_TITLE},
99 {"passwordSearchPlaceholder", IDS_PASSWORDS_PAGE_SEARCH_PASSWORDS},
100 {"passwordShowButton", IDS_PASSWORDS_PAGE_VIEW_SHOW_BUTTON},
101 {"passwordHideButton", IDS_PASSWORDS_PAGE_VIEW_HIDE_BUTTON},
102 {"passwordsNoPasswordsDescription",
103 IDS_PASSWORDS_PAGE_VIEW_NO_PASSWORDS_DESCRIPTION},
104 {"passwordsNoExceptionsDescription",
105 IDS_PASSWORDS_PAGE_VIEW_NO_EXCEPTIONS_DESCRIPTION},
106 };
107
108 RegisterStrings(localized_strings, resources, arraysize(resources));
109
110 const ProfileSyncService* sync_service =
111 ProfileSyncServiceFactory::GetForProfile(GetProfile());
112 int title_id =
113 password_bubble_experiment::IsSmartLockBrandingEnabled(sync_service)
114 ? IDS_PASSWORD_MANAGER_SMART_LOCK_FOR_PASSWORDS
115 : IDS_PASSWORDS_EXCEPTIONS_WINDOW_TITLE;
116 RegisterTitle(localized_strings, "passwordsPage", title_id);
117
118 localized_strings->SetString("passwordManagerLearnMoreURL",
119 chrome::kPasswordManagerLearnMoreURL);
120 localized_strings->SetString(
121 "passwordsManagePasswordsLink",
122 password_manager::kPasswordManagerAccountDashboardURL);
123
124 std::string management_hostname =
125 GURL(password_manager::kPasswordManagerAccountDashboardURL).host();
126 base::string16 link_text = base::UTF8ToUTF16(management_hostname);
127 size_t offset;
128 base::string16 full_text = l10n_util::GetStringFUTF16(
129 IDS_MANAGE_PASSWORDS_REMOTE_TEXT, link_text, &offset);
130
131 localized_strings->SetString("passwordsManagePasswordsBeforeLinkText",
132 full_text.substr(0, offset));
133 localized_strings->SetString("passwordsManagePasswordsLinkText",
134 full_text.substr(offset, link_text.size()));
135 localized_strings->SetString("passwordsManagePasswordsAfterLinkText",
136 full_text.substr(offset + link_text.size()));
137
138 bool disable_show_passwords = false;
139
140 #if defined(OS_WIN) && defined(USE_ASH)
141 // We disable the ability to show passwords when running in Windows Metro
142 // interface. This is because we cannot pop native Win32 dialogs from the
143 // Metro process.
144 // TODO(wfh): Revisit this if Metro usage grows.
145 if (chrome::IsNativeWindowInAsh(GetNativeWindow()))
146 disable_show_passwords = true;
147 #endif
148
149 localized_strings->SetBoolean("disableShowPasswords", disable_show_passwords);
150 localized_strings->SetBoolean(
151 "enableCredentialManagerAPI",
152 base::CommandLine::ForCurrentProcess()->HasSwitch(
153 switches::kEnableCredentialManagerAPI));
154 }
155
156 void PasswordManagerHandler::RegisterMessages() {
157 web_ui()->RegisterMessageCallback( 91 web_ui()->RegisterMessageCallback(
158 "updatePasswordLists", 92 "updatePasswordLists",
159 base::Bind(&PasswordManagerHandler::HandleUpdatePasswordLists, 93 base::Bind(&PasswordsHandler::HandleUpdatePasswordLists,
160 base::Unretained(this))); 94 base::Unretained(this)));
161 web_ui()->RegisterMessageCallback( 95 web_ui()->RegisterMessageCallback(
162 "removeSavedPassword", 96 "removeSavedPassword",
163 base::Bind(&PasswordManagerHandler::HandleRemoveSavedPassword, 97 base::Bind(&PasswordsHandler::HandleRemoveSavedPassword,
164 base::Unretained(this))); 98 base::Unretained(this)));
165 web_ui()->RegisterMessageCallback( 99 web_ui()->RegisterMessageCallback(
166 "removePasswordException", 100 "removePasswordException",
167 base::Bind(&PasswordManagerHandler::HandleRemovePasswordException, 101 base::Bind(&PasswordsHandler::HandleRemovePasswordException,
168 base::Unretained(this))); 102 base::Unretained(this)));
169 web_ui()->RegisterMessageCallback( 103 web_ui()->RegisterMessageCallback(
170 "requestShowPassword", 104 "requestShowPassword",
171 base::Bind(&PasswordManagerHandler::HandleRequestShowPassword, 105 base::Bind(&PasswordsHandler::HandleRequestShowPassword,
172 base::Unretained(this))); 106 base::Unretained(this)));
173 } 107 }
174 108
175 void PasswordManagerHandler::InitializeHandler() { 109 void PasswordsHandler::HandleRemoveSavedPassword(const base::ListValue* args) {
176 password_manager_presenter_.Initialize();
177 }
178
179 void PasswordManagerHandler::HandleRemoveSavedPassword(
180 const base::ListValue* args) {
181 std::string string_value = base::UTF16ToUTF8(ExtractStringValue(args)); 110 std::string string_value = base::UTF16ToUTF8(ExtractStringValue(args));
182 int index; 111 int index;
183 if (base::StringToInt(string_value, &index) && index >= 0) { 112 if (base::StringToInt(string_value, &index) && index >= 0) {
184 password_manager_presenter_.RemoveSavedPassword(static_cast<size_t>(index)); 113 password_manager_presenter_.RemoveSavedPassword(static_cast<size_t>(index));
185 } 114 }
186 } 115 }
187 116
188 void PasswordManagerHandler::HandleRemovePasswordException( 117 void PasswordsHandler::HandleRemovePasswordException(
189 const base::ListValue* args) { 118 const base::ListValue* args) {
190 std::string string_value = base::UTF16ToUTF8(ExtractStringValue(args)); 119 std::string string_value = base::UTF16ToUTF8(ExtractStringValue(args));
191 int index; 120 int index;
192 if (base::StringToInt(string_value, &index) && index >= 0) { 121 if (base::StringToInt(string_value, &index) && index >= 0) {
193 password_manager_presenter_.RemovePasswordException( 122 password_manager_presenter_.RemovePasswordException(
194 static_cast<size_t>(index)); 123 static_cast<size_t>(index));
195 } 124 }
196 } 125 }
197 126
198 void PasswordManagerHandler::HandleRequestShowPassword( 127 void PasswordsHandler::HandleRequestShowPassword(const base::ListValue* args) {
199 const base::ListValue* args) {
200 int index; 128 int index;
201 if (!ExtractIntegerValue(args, &index)) 129 if (!ExtractIntegerValue(args, &index))
202 NOTREACHED(); 130 NOTREACHED();
203 131
204 password_manager_presenter_.RequestShowPassword(static_cast<size_t>(index)); 132 password_manager_presenter_.RequestShowPassword(static_cast<size_t>(index));
205 } 133 }
206 134
207 void PasswordManagerHandler::ShowPassword( 135 void PasswordsHandler::ShowPassword(size_t index,
208 size_t index, 136 const std::string& origin_url,
209 const std::string& origin_url, 137 const std::string& username,
210 const std::string& username, 138 const base::string16& password_value) {
211 const base::string16& password_value) {
212 // Call back the front end to reveal the password. 139 // Call back the front end to reveal the password.
213 web_ui()->CallJavascriptFunction( 140 web_ui()->CallJavascriptFunction(
214 "PasswordManager.showPassword", 141 "settings.PasswordsPrivateApi.showPassword",
215 base::FundamentalValue(static_cast<int>(index)), 142 base::FundamentalValue(static_cast<int>(index)),
216 base::StringValue(password_value)); 143 base::StringValue(password_value));
217 } 144 }
218 145
219 void PasswordManagerHandler::HandleUpdatePasswordLists( 146 void PasswordsHandler::HandleUpdatePasswordLists(const base::ListValue* args) {
220 const base::ListValue* args) {
221 password_manager_presenter_.UpdatePasswordLists(); 147 password_manager_presenter_.UpdatePasswordLists();
222 } 148 }
223 149
224 void PasswordManagerHandler::SetPasswordList( 150 void PasswordsHandler::SetPasswordList(
225 const std::vector<scoped_ptr<autofill::PasswordForm>>& password_list, 151 const std::vector<scoped_ptr<autofill::PasswordForm>>& password_list,
226 bool show_passwords) { 152 bool show_passwords) {
227 base::ListValue entries; 153 base::ListValue entries;
228 languages_ = GetProfile()->GetPrefs()->GetString(prefs::kAcceptLanguages); 154 languages_ = GetProfile()->GetPrefs()->GetString(prefs::kAcceptLanguages);
229 base::string16 placeholder(base::ASCIIToUTF16(" ")); 155 base::string16 placeholder(base::ASCIIToUTF16(" "));
230 for (const auto& saved_password : password_list) { 156 for (const auto& saved_password : password_list) {
231 scoped_ptr<base::DictionaryValue> entry(new base::DictionaryValue); 157 scoped_ptr<base::DictionaryValue> entry(new base::DictionaryValue);
232 CopyOriginInfoOfPasswordForm(*saved_password, languages_, entry.get()); 158 CopyOriginInfoOfPasswordForm(*saved_password, languages_, entry.get());
233 159
234 entry->SetString(kUsernameField, saved_password->username_value); 160 entry->SetString(kUsernameField, saved_password->username_value);
235 if (show_passwords) { 161 if (show_passwords) {
236 entry->SetString(kPasswordField, saved_password->password_value); 162 entry->SetString(kPasswordField, saved_password->password_value);
237 } else { 163 } else {
238 // Use a placeholder value with the same length as the password. 164 // Use a placeholder value with the same length as the password.
239 entry->SetString( 165 entry->SetString(
240 kPasswordField, 166 kPasswordField,
241 base::string16(saved_password->password_value.length(), ' ')); 167 base::string16(saved_password->password_value.length(), ' '));
242 } 168 }
243 const GURL& federation_url = saved_password->federation_url; 169 const GURL& federation_url = saved_password->federation_url;
244 if (!federation_url.is_empty()) { 170 if (!federation_url.is_empty()) {
245 entry->SetString( 171 entry->SetString(
246 kFederationField, 172 kFederationField,
247 l10n_util::GetStringFUTF16(IDS_PASSWORDS_VIA_FEDERATION, 173 l10n_util::GetStringFUTF16(IDS_PASSWORDS_VIA_FEDERATION,
248 base::UTF8ToUTF16(federation_url.host()))); 174 base::UTF8ToUTF16(federation_url.host())));
249 } 175 }
250 176
251 entries.Append(entry.release()); 177 entries.Append(entry.release());
252 } 178 }
253 179
254 web_ui()->CallJavascriptFunction("PasswordManager.setSavedPasswordsList", 180 web_ui()->CallJavascriptFunction(
255 entries); 181 "settings.PasswordsPrivateApi.setSavedPasswordsList", entries);
256 } 182 }
257 183
258 void PasswordManagerHandler::SetPasswordExceptionList( 184 void PasswordsHandler::SetPasswordExceptionList(
259 const std::vector<scoped_ptr<autofill::PasswordForm>>& 185 const std::vector<scoped_ptr<autofill::PasswordForm>>&
260 password_exception_list) { 186 password_exception_list) {
261 base::ListValue entries; 187 base::ListValue entries;
262 for (const auto& exception : password_exception_list) { 188 for (const auto& exception : password_exception_list) {
263 scoped_ptr<base::DictionaryValue> entry(new base::DictionaryValue); 189 scoped_ptr<base::DictionaryValue> entry(new base::DictionaryValue);
264 CopyOriginInfoOfPasswordForm(*exception, languages_, entry.get()); 190 CopyOriginInfoOfPasswordForm(*exception, languages_, entry.get());
265 entries.Append(entry.release()); 191 entries.Append(entry.release());
266 } 192 }
267 193
268 web_ui()->CallJavascriptFunction("PasswordManager.setPasswordExceptionsList", 194 web_ui()->CallJavascriptFunction(
269 entries); 195 "settings.PasswordsPrivateApi.setPasswordExceptionsList", entries);
270 } 196 }
271 197
272 } // namespace options 198 } // namespace settings
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698