OLD | NEW |
---|---|
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 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/options/password_manager_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/files/file_path.h" | |
10 #include "base/metrics/histogram.h" | |
9 #include "base/prefs/pref_service.h" | 11 #include "base/prefs/pref_service.h" |
10 #include "base/strings/string_number_conversions.h" | 12 #include "base/strings/string_number_conversions.h" |
11 #include "base/strings/string_split.h" | 13 #include "base/strings/string_split.h" |
12 #include "base/strings/utf_string_conversions.h" | 14 #include "base/strings/utf_string_conversions.h" |
13 #include "base/values.h" | 15 #include "base/values.h" |
14 #include "chrome/browser/chrome_notification_types.h" | 16 #include "chrome/browser/chrome_notification_types.h" |
17 #include "chrome/browser/password_manager/sync_metrics.h" | |
15 #include "chrome/browser/profiles/profile.h" | 18 #include "chrome/browser/profiles/profile.h" |
16 #include "chrome/browser/sync/profile_sync_service.h" | 19 #include "chrome/browser/sync/profile_sync_service.h" |
17 #include "chrome/browser/sync/profile_sync_service_factory.h" | 20 #include "chrome/browser/sync/profile_sync_service_factory.h" |
18 #if defined(OS_WIN) && defined(USE_ASH) | 21 #if defined(OS_WIN) && defined(USE_ASH) |
19 #include "chrome/browser/ui/ash/ash_util.h" | 22 #include "chrome/browser/ui/ash/ash_util.h" |
20 #endif | 23 #endif |
24 #include "chrome/browser/ui/chrome_select_file_policy.h" | |
21 #include "chrome/common/pref_names.h" | 25 #include "chrome/common/pref_names.h" |
22 #include "chrome/common/url_constants.h" | 26 #include "chrome/common/url_constants.h" |
23 #include "chrome/grit/generated_resources.h" | 27 #include "chrome/grit/generated_resources.h" |
24 #include "components/autofill/core/common/password_form.h" | 28 #include "components/autofill/core/common/password_form.h" |
25 #include "components/password_manager/core/browser/affiliation_utils.h" | 29 #include "components/password_manager/core/browser/affiliation_utils.h" |
30 #include "components/password_manager/core/browser/export/password_exporter.h" | |
26 #include "components/password_manager/core/browser/password_bubble_experiment.h" | 31 #include "components/password_manager/core/browser/password_bubble_experiment.h" |
32 #include "components/password_manager/core/browser/password_store.h" | |
27 #include "components/password_manager/core/common/experiments.h" | 33 #include "components/password_manager/core/common/experiments.h" |
34 #include "content/public/browser/browser_thread.h" | |
28 #include "content/public/browser/notification_details.h" | 35 #include "content/public/browser/notification_details.h" |
29 #include "content/public/browser/notification_source.h" | 36 #include "content/public/browser/notification_source.h" |
30 #include "content/public/browser/user_metrics.h" | 37 #include "content/public/browser/user_metrics.h" |
31 #include "content/public/browser/web_contents.h" | 38 #include "content/public/browser/web_contents.h" |
32 #include "content/public/browser/web_ui.h" | 39 #include "content/public/browser/web_ui.h" |
33 #include "content/public/common/content_switches.h" | 40 #include "content/public/common/content_switches.h" |
34 #include "net/base/net_util.h" | 41 #include "net/base/net_util.h" |
35 #include "ui/base/l10n/l10n_util.h" | 42 #include "ui/base/l10n/l10n_util.h" |
36 | 43 |
37 namespace options { | 44 namespace options { |
38 | 45 |
39 PasswordManagerHandler::PasswordManagerHandler() | 46 namespace { |
40 : password_manager_presenter_(this) {} | 47 |
48 // Enumeration of different callers of SelectFile. Starting count at 1 so | |
49 // accidental call of SelectFile with params=NULL will error out. | |
50 enum { | |
51 IMPORT_FILE_SELECTED = 1, | |
52 EXPORT_FILE_SELECTED, | |
53 }; | |
54 | |
55 } // namespace | |
56 | |
57 PasswordManagerHandler::PasswordManagerHandler() { | |
58 password_manager_presenter_.reset(new PasswordManagerPresenter(this)); | |
59 } | |
60 | |
61 PasswordManagerHandler::PasswordManagerHandler( | |
62 PasswordManagerPresenter* presenter) { | |
63 password_manager_presenter_.reset(presenter); | |
64 } | |
41 | 65 |
42 PasswordManagerHandler::~PasswordManagerHandler() {} | 66 PasswordManagerHandler::~PasswordManagerHandler() {} |
43 | 67 |
44 Profile* PasswordManagerHandler::GetProfile() { | 68 Profile* PasswordManagerHandler::GetProfile() { |
45 return Profile::FromWebUI(web_ui()); | 69 return Profile::FromWebUI(web_ui()); |
46 } | 70 } |
47 | 71 |
48 #if !defined(OS_ANDROID) | 72 #if !defined(OS_ANDROID) |
49 gfx::NativeWindow PasswordManagerHandler::GetNativeWindow() const { | 73 gfx::NativeWindow PasswordManagerHandler::GetNativeWindow() const { |
50 return web_ui()->GetWebContents()->GetTopLevelNativeWindow(); | 74 return web_ui()->GetWebContents()->GetTopLevelNativeWindow(); |
51 } | 75 } |
52 #endif | 76 #endif |
53 | 77 |
54 void PasswordManagerHandler::GetLocalizedValues( | 78 void PasswordManagerHandler::GetLocalizedValues( |
55 base::DictionaryValue* localized_strings) { | 79 base::DictionaryValue* localized_strings) { |
56 DCHECK(localized_strings); | 80 DCHECK(localized_strings); |
57 | 81 |
58 static const OptionsStringResource resources[] = { | 82 static const OptionsStringResource resources[] = { |
59 { "autoSigninTitle", | 83 {"autoSigninTitle", IDS_PASSWORDS_AUTO_SIGNIN_TITLE}, |
60 IDS_PASSWORDS_AUTO_SIGNIN_TITLE }, | 84 {"autoSigninDescription", IDS_PASSWORDS_AUTO_SIGNIN_DESCRIPTION}, |
61 { "autoSigninDescription", | 85 {"savedPasswordsTitle", IDS_PASSWORDS_SHOW_PASSWORDS_TAB_TITLE}, |
62 IDS_PASSWORDS_AUTO_SIGNIN_DESCRIPTION }, | 86 {"passwordExceptionsTitle", IDS_PASSWORDS_EXCEPTIONS_TAB_TITLE}, |
63 { "savedPasswordsTitle", | 87 {"passwordSearchPlaceholder", IDS_PASSWORDS_PAGE_SEARCH_PASSWORDS}, |
64 IDS_PASSWORDS_SHOW_PASSWORDS_TAB_TITLE }, | 88 {"passwordShowButton", IDS_PASSWORDS_PAGE_VIEW_SHOW_BUTTON}, |
65 { "passwordExceptionsTitle", | 89 {"passwordHideButton", IDS_PASSWORDS_PAGE_VIEW_HIDE_BUTTON}, |
66 IDS_PASSWORDS_EXCEPTIONS_TAB_TITLE }, | 90 {"passwordsNoPasswordsDescription", |
67 { "passwordSearchPlaceholder", | 91 IDS_PASSWORDS_PAGE_VIEW_NO_PASSWORDS_DESCRIPTION}, |
68 IDS_PASSWORDS_PAGE_SEARCH_PASSWORDS }, | 92 {"passwordsNoExceptionsDescription", |
69 { "passwordShowButton", | 93 IDS_PASSWORDS_PAGE_VIEW_NO_EXCEPTIONS_DESCRIPTION}, |
70 IDS_PASSWORDS_PAGE_VIEW_SHOW_BUTTON }, | 94 {"passwordManagerImportPasswordButtonText", |
71 { "passwordHideButton", | 95 IDS_PASSWORD_MANAGER_IMPORT_BUTTON}, |
72 IDS_PASSWORDS_PAGE_VIEW_HIDE_BUTTON }, | 96 {"passwordManagerExportPasswordButtonText", |
73 { "passwordsNoPasswordsDescription", | 97 IDS_PASSWORD_MANAGER_EXPORT_BUTTON}, |
74 IDS_PASSWORDS_PAGE_VIEW_NO_PASSWORDS_DESCRIPTION }, | 98 {"importPasswordCompletedMessage", |
75 { "passwordsNoExceptionsDescription", | 99 IDS_PASSWORD_MANAGER_IMPORT_COMPLETED_MESSAGE}, |
76 IDS_PASSWORDS_PAGE_VIEW_NO_EXCEPTIONS_DESCRIPTION }, | |
77 }; | 100 }; |
78 | 101 |
79 RegisterStrings(localized_strings, resources, arraysize(resources)); | 102 RegisterStrings(localized_strings, resources, arraysize(resources)); |
80 | 103 |
81 const ProfileSyncService* sync_service = | 104 const ProfileSyncService* sync_service = |
82 ProfileSyncServiceFactory::GetForProfile(GetProfile()); | 105 ProfileSyncServiceFactory::GetForProfile(GetProfile()); |
83 int title_id = | 106 int title_id = |
84 password_bubble_experiment::IsSmartLockBrandingEnabled(sync_service) ? | 107 password_bubble_experiment::IsSmartLockBrandingEnabled(sync_service) ? |
85 IDS_PASSWORDS_EXCEPTIONS_SMART_LOCK_WINDOW_TITLE : | 108 IDS_PASSWORDS_EXCEPTIONS_SMART_LOCK_WINDOW_TITLE : |
86 IDS_PASSWORDS_EXCEPTIONS_WINDOW_TITLE; | 109 IDS_PASSWORDS_EXCEPTIONS_WINDOW_TITLE; |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
134 base::Bind(&PasswordManagerHandler::HandleRemoveSavedPassword, | 157 base::Bind(&PasswordManagerHandler::HandleRemoveSavedPassword, |
135 base::Unretained(this))); | 158 base::Unretained(this))); |
136 web_ui()->RegisterMessageCallback( | 159 web_ui()->RegisterMessageCallback( |
137 "removePasswordException", | 160 "removePasswordException", |
138 base::Bind(&PasswordManagerHandler::HandleRemovePasswordException, | 161 base::Bind(&PasswordManagerHandler::HandleRemovePasswordException, |
139 base::Unretained(this))); | 162 base::Unretained(this))); |
140 web_ui()->RegisterMessageCallback( | 163 web_ui()->RegisterMessageCallback( |
141 "requestShowPassword", | 164 "requestShowPassword", |
142 base::Bind(&PasswordManagerHandler::HandleRequestShowPassword, | 165 base::Bind(&PasswordManagerHandler::HandleRequestShowPassword, |
143 base::Unretained(this))); | 166 base::Unretained(this))); |
167 web_ui()->RegisterMessageCallback( | |
168 "importPassword", | |
169 base::Bind(&PasswordManagerHandler::HandlePasswordImport, | |
170 base::Unretained(this))); | |
171 web_ui()->RegisterMessageCallback( | |
172 "exportPassword", | |
173 base::Bind(&PasswordManagerHandler::HandlePasswordExport, | |
174 base::Unretained(this))); | |
144 } | 175 } |
145 | 176 |
146 void PasswordManagerHandler::InitializeHandler() { | 177 void PasswordManagerHandler::InitializeHandler() { |
147 password_manager_presenter_.Initialize(); | 178 password_manager_presenter_->Initialize(); |
148 } | 179 } |
149 | 180 |
150 void PasswordManagerHandler::InitializePage() { | 181 void PasswordManagerHandler::InitializePage() { |
151 base::FundamentalValue visible( | 182 base::FundamentalValue visible( |
152 password_manager::ManageAccountLinkExperimentEnabled()); | 183 password_manager::ManageAccountLinkExperimentEnabled()); |
153 web_ui()->CallJavascriptFunction( | 184 web_ui()->CallJavascriptFunction( |
154 "PasswordManager.setManageAccountLinkVisibility", visible); | 185 "PasswordManager.setManageAccountLinkVisibility", visible); |
186 if (password_manager::ImportExportExperimentEnabled()) { | |
187 web_ui()->CallJavascriptFunction("PasswordManager.showImportExportButton"); | |
188 } | |
155 } | 189 } |
156 | 190 |
157 void PasswordManagerHandler::HandleRemoveSavedPassword( | 191 void PasswordManagerHandler::HandleRemoveSavedPassword( |
158 const base::ListValue* args) { | 192 const base::ListValue* args) { |
159 std::string string_value = base::UTF16ToUTF8(ExtractStringValue(args)); | 193 std::string string_value = base::UTF16ToUTF8(ExtractStringValue(args)); |
160 int index; | 194 int index; |
161 if (base::StringToInt(string_value, &index) && index >= 0) { | 195 if (base::StringToInt(string_value, &index) && index >= 0) { |
162 password_manager_presenter_.RemoveSavedPassword(static_cast<size_t>(index)); | 196 password_manager_presenter_->RemoveSavedPassword( |
197 static_cast<size_t>(index)); | |
163 } | 198 } |
164 } | 199 } |
165 | 200 |
166 void PasswordManagerHandler::HandleRemovePasswordException( | 201 void PasswordManagerHandler::HandleRemovePasswordException( |
167 const base::ListValue* args) { | 202 const base::ListValue* args) { |
168 std::string string_value = base::UTF16ToUTF8(ExtractStringValue(args)); | 203 std::string string_value = base::UTF16ToUTF8(ExtractStringValue(args)); |
169 int index; | 204 int index; |
170 if (base::StringToInt(string_value, &index) && index >= 0) { | 205 if (base::StringToInt(string_value, &index) && index >= 0) { |
171 password_manager_presenter_.RemovePasswordException( | 206 password_manager_presenter_->RemovePasswordException( |
172 static_cast<size_t>(index)); | 207 static_cast<size_t>(index)); |
173 } | 208 } |
174 } | 209 } |
175 | 210 |
176 void PasswordManagerHandler::HandleRequestShowPassword( | 211 void PasswordManagerHandler::HandleRequestShowPassword( |
177 const base::ListValue* args) { | 212 const base::ListValue* args) { |
178 int index; | 213 int index; |
179 if (!ExtractIntegerValue(args, &index)) | 214 if (!ExtractIntegerValue(args, &index)) |
180 NOTREACHED(); | 215 NOTREACHED(); |
181 | 216 |
182 password_manager_presenter_.RequestShowPassword(static_cast<size_t>(index)); | 217 password_manager_presenter_->RequestShowPassword(static_cast<size_t>(index)); |
183 } | 218 } |
184 | 219 |
185 void PasswordManagerHandler::ShowPassword( | 220 void PasswordManagerHandler::ShowPassword( |
186 size_t index, | 221 size_t index, |
187 const std::string& origin_url, | 222 const std::string& origin_url, |
188 const std::string& username, | 223 const std::string& username, |
189 const base::string16& password_value) { | 224 const base::string16& password_value) { |
190 // Call back the front end to reveal the password. | 225 // Call back the front end to reveal the password. |
191 web_ui()->CallJavascriptFunction( | 226 web_ui()->CallJavascriptFunction( |
192 "PasswordManager.showPassword", | 227 "PasswordManager.showPassword", |
193 base::FundamentalValue(static_cast<int>(index)), | 228 base::FundamentalValue(static_cast<int>(index)), |
194 base::StringValue(password_value)); | 229 base::StringValue(password_value)); |
195 } | 230 } |
196 | 231 |
197 void PasswordManagerHandler::HandleUpdatePasswordLists( | 232 void PasswordManagerHandler::HandleUpdatePasswordLists( |
198 const base::ListValue* args) { | 233 const base::ListValue* args) { |
199 password_manager_presenter_.UpdatePasswordLists(); | 234 password_manager_presenter_->UpdatePasswordLists(); |
200 } | 235 } |
201 | 236 |
202 void PasswordManagerHandler::SetPasswordList( | 237 void PasswordManagerHandler::SetPasswordList( |
203 const ScopedVector<autofill::PasswordForm>& password_list, | 238 const ScopedVector<autofill::PasswordForm>& password_list, |
204 bool show_passwords) { | 239 bool show_passwords) { |
205 base::ListValue entries; | 240 base::ListValue entries; |
206 languages_ = GetProfile()->GetPrefs()->GetString(prefs::kAcceptLanguages); | 241 languages_ = GetProfile()->GetPrefs()->GetString(prefs::kAcceptLanguages); |
207 base::string16 placeholder(base::ASCIIToUTF16(" ")); | 242 base::string16 placeholder(base::ASCIIToUTF16(" ")); |
208 for (size_t i = 0; i < password_list.size(); ++i) { | 243 for (size_t i = 0; i < password_list.size(); ++i) { |
209 base::ListValue* entry = new base::ListValue(); | 244 base::ListValue* entry = new base::ListValue(); |
(...skipping 25 matching lines...) Expand all Loading... | |
235 base::ListValue entries; | 270 base::ListValue entries; |
236 for (size_t i = 0; i < password_exception_list.size(); ++i) { | 271 for (size_t i = 0; i < password_exception_list.size(); ++i) { |
237 entries.AppendString(password_manager::GetHumanReadableOrigin( | 272 entries.AppendString(password_manager::GetHumanReadableOrigin( |
238 *password_exception_list[i], languages_)); | 273 *password_exception_list[i], languages_)); |
239 } | 274 } |
240 | 275 |
241 web_ui()->CallJavascriptFunction("PasswordManager.setPasswordExceptionsList", | 276 web_ui()->CallJavascriptFunction("PasswordManager.setPasswordExceptionsList", |
242 entries); | 277 entries); |
243 } | 278 } |
244 | 279 |
280 void PasswordManagerHandler::FileSelected(const base::FilePath& path, | |
281 int index, | |
282 void* params) { | |
283 switch (reinterpret_cast<intptr_t>(params)) { | |
284 case IMPORT_FILE_SELECTED: | |
285 ImportPasswordFileSelected(path); | |
286 break; | |
287 case EXPORT_FILE_SELECTED: | |
288 ExportPasswordFileSelected(path); | |
289 break; | |
290 default: | |
291 NOTREACHED(); | |
292 } | |
293 } | |
294 | |
295 void PasswordManagerHandler::HandlePasswordImport(const base::ListValue* args) { | |
296 #if !defined(OS_ANDROID) // This is never called on Android. | |
297 ui::SelectFileDialog::FileTypeInfo file_type_info; | |
298 file_type_info.extensions = | |
299 password_manager::PasswordImporter::GetSupportedFileExtensions(); | |
300 DCHECK(!file_type_info.extensions.empty() && | |
301 !file_type_info.extensions[0].empty()); | |
302 file_type_info.include_all_files = true; | |
303 selected_file_dialog_ = ui::SelectFileDialog::Create( | |
304 this, new ChromeSelectFilePolicy(web_ui()->GetWebContents())); | |
305 selected_file_dialog_->SelectFile( | |
306 ui::SelectFileDialog::SELECT_OPEN_FILE, | |
307 l10n_util::GetStringUTF16(IDS_PASSWORD_MANAGER_IMPORT_DIALOG_TITLE), | |
308 base::FilePath(), &file_type_info, 1, file_type_info.extensions[0][0], | |
309 web_ui()->GetWebContents()->GetTopLevelNativeWindow(), | |
310 reinterpret_cast<void*>(IMPORT_FILE_SELECTED)); | |
311 #endif | |
312 } | |
313 | |
314 void PasswordManagerHandler::ImportPasswordFileSelected( | |
315 const base::FilePath& path) { | |
316 password_manager::PasswordImporter::Import( | |
317 path, content::BrowserThread::GetMessageLoopProxyForThread( | |
318 content::BrowserThread::FILE).get(), | |
319 base::Bind(&PasswordManagerHandler::ImportPasswordFileRead, | |
320 base::Unretained(this))); | |
321 } | |
322 | |
323 void PasswordManagerHandler::ImportPasswordFileRead( | |
324 password_manager::PasswordImporter::Result result, | |
325 const std::vector<autofill::PasswordForm>& forms) { | |
326 UMA_HISTOGRAM_ENUMERATION("PasswordManager.ImportPasswordFromCSVResult", | |
327 result, | |
328 password_manager::PasswordImporter::SEMANTIC_ERROR); | |
329 if (result != password_manager::PasswordImporter::SUCCESS) { | |
330 return; | |
331 } | |
332 | |
333 UMA_HISTOGRAM_COUNTS("PasswordManager.ImportedPasswordsPerUserInCSV", | |
334 forms.size()); | |
335 | |
336 password_manager::PasswordStore* store = | |
337 password_manager_presenter_->GetPasswordStore(); | |
338 if (store) { | |
339 for (const autofill::PasswordForm& form : forms) { | |
340 store->AddLogin(form); | |
341 } | |
342 | |
343 // Display Feedback UI | |
344 base::FundamentalValue visible(true); | |
345 web_ui()->CallJavascriptFunction( | |
346 "PasswordManager.setImportCompleteUIVisibility", visible); | |
347 } else { | |
348 UMA_HISTOGRAM_COUNTS("PasswordManager.FailToStorePasswordImportedFromCSV", | |
349 1); | |
350 } | |
351 } | |
352 | |
353 void PasswordManagerHandler::HandlePasswordExport(const base::ListValue* args) { | |
354 #if !defined(OS_ANDROID) // This is never called on Android. | |
355 if (password_manager_presenter_->IsUserAuthenticated()) { | |
356 ui::SelectFileDialog::FileTypeInfo file_type_info; | |
357 file_type_info.extensions = | |
358 password_manager::PasswordExporter::GetSupportedFileExtensions(); | |
359 DCHECK(!file_type_info.extensions.empty() && | |
360 !file_type_info.extensions[0].empty()); | |
361 file_type_info.include_all_files = true; | |
362 selected_file_dialog_ = ui::SelectFileDialog::Create( | |
363 this, new ChromeSelectFilePolicy(web_ui()->GetWebContents())); | |
364 selected_file_dialog_->SelectFile( | |
365 ui::SelectFileDialog::SELECT_SAVEAS_FILE, | |
366 l10n_util::GetStringUTF16(IDS_PASSWORD_MANAGER_EXPORT_DIALOG_TITLE), | |
367 base::FilePath(), &file_type_info, 1, file_type_info.extensions[0][0], | |
368 GetNativeWindow(), reinterpret_cast<void*>(EXPORT_FILE_SELECTED)); | |
369 } | |
370 #endif | |
371 } | |
372 | |
373 void PasswordManagerHandler::ExportPasswordFileSelected( | |
374 const base::FilePath& path) { | |
375 ScopedVector<autofill::PasswordForm> password_list = | |
376 password_manager_presenter_->GetAllPasswords().Pass(); | |
377 UMA_HISTOGRAM_COUNTS("PasswordManager.ExportedPasswordsPerUserInCSV", | |
378 password_list.size()); | |
379 password_manager::PasswordExporter::Export( | |
380 path, password_list.Pass(), | |
381 content::BrowserThread::GetMessageLoopProxyForThread( | |
382 content::BrowserThread::FILE).get(), | |
383 base::Bind(&PasswordManagerHandler::ExportPasswordFileWritten, | |
384 base::Unretained(this))); | |
385 } | |
386 | |
387 void PasswordManagerHandler::ExportPasswordFileWritten() { | |
388 // We do not plan to give any UI feedback for export | |
Garrett Casto
2015/07/07 22:11:09
Nit: This should wrap closer to 80 characters.
| |
389 // at this moment as it will almost always succeed. If the plan changes, this | |
390 // is the place to put the feedback logic. | |
391 } | |
392 | |
245 } // namespace options | 393 } // namespace options |
OLD | NEW |