OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/ui/webui/signin/signin_error_ui.h" |
| 6 |
| 7 #include "base/strings/utf_string_conversions.h" |
| 8 #include "chrome/browser/browser_process.h" |
| 9 #include "chrome/browser/profiles/profile_attributes_entry.h" |
| 10 #include "chrome/browser/profiles/profile_attributes_storage.h" |
| 11 #include "chrome/browser/profiles/profile_manager.h" |
| 12 #include "chrome/browser/ui/browser_window.h" |
| 13 #include "chrome/browser/ui/webui/signin/login_ui_service.h" |
| 14 #include "chrome/browser/ui/webui/signin/login_ui_service_factory.h" |
| 15 #include "chrome/browser/ui/webui/signin/signin_error_handler.h" |
| 16 #include "chrome/common/url_constants.h" |
| 17 #include "chrome/grit/generated_resources.h" |
| 18 #include "content/public/browser/web_ui.h" |
| 19 #include "content/public/browser/web_ui_data_source.h" |
| 20 #include "grit/browser_resources.h" |
| 21 #include "ui/base/l10n/l10n_util.h" |
| 22 #include "ui/base/webui/web_ui_util.h" |
| 23 #include "ui/gfx/text_elider.h" |
| 24 |
| 25 SigninErrorUI::SigninErrorUI(content::WebUI* web_ui) |
| 26 : SigninErrorUI(web_ui, new SigninErrorHandler) {} |
| 27 |
| 28 SigninErrorUI::SigninErrorUI(content::WebUI* web_ui, |
| 29 SigninErrorHandler* handler) |
| 30 : WebDialogUI(web_ui) { |
| 31 Profile* profile = Profile::FromWebUI(web_ui); |
| 32 content::WebUIDataSource* source = |
| 33 content::WebUIDataSource::Create(chrome::kChromeUISigninErrorHost); |
| 34 source->SetJsonPath("strings.js"); |
| 35 source->SetDefaultResource(IDR_SIGNIN_ERROR_HTML); |
| 36 source->AddResourcePath("signin_error.js", IDR_SIGNIN_ERROR_JS); |
| 37 |
| 38 // Retrieve the last signin error message and email used. |
| 39 LoginUIService* login_ui_service = |
| 40 LoginUIServiceFactory::GetForProfile(profile); |
| 41 base::string16 last_login_result(login_ui_service->GetLastLoginResult()); |
| 42 base::string16 email = login_ui_service->GetLastLoginErrorEmail(); |
| 43 if (email.empty()) { |
| 44 source->AddLocalizedString("signinErrorTitle", IDS_SIGNIN_ERROR_TITLE); |
| 45 } else { |
| 46 source->AddString( |
| 47 "signinErrorTitle", |
| 48 l10n_util::GetStringFUTF16(IDS_SIGNIN_ERROR_EMAIL_TITLE, email)); |
| 49 } |
| 50 |
| 51 // Tweak the dialog UI depending on whether the signin error is |
| 52 // username-in-use error. |
| 53 base::string16 existing_name; |
| 54 if (last_login_result.compare( |
| 55 l10n_util::GetStringUTF16(IDS_SYNC_USER_NAME_IN_USE_ERROR)) == 0) { |
| 56 ProfileManager* profile_manager = g_browser_process->profile_manager(); |
| 57 if (profile_manager) { |
| 58 std::vector<ProfileAttributesEntry*> entries = |
| 59 profile_manager->GetProfileAttributesStorage() |
| 60 .GetAllProfilesAttributes(); |
| 61 DCHECK(!email.empty()); |
| 62 for (const ProfileAttributesEntry* entry : entries) { |
| 63 if (gaia::AreEmailsSame(base::UTF16ToUTF8(email), |
| 64 base::UTF16ToUTF8(entry->GetUserName()))) { |
| 65 handler->set_duplicate_profile_entry(entry); |
| 66 existing_name = entry->GetName(); |
| 67 } |
| 68 } |
| 69 } |
| 70 DCHECK(!existing_name.empty()); |
| 71 source->AddString( |
| 72 "signinErrorMessage", |
| 73 l10n_util::GetStringFUTF16( |
| 74 IDS_SIGNIN_ERROR_LEARN_MORE_LINK, |
| 75 l10n_util::GetStringFUTF16(IDS_SYNC_USER_NAME_IN_USE_BY_ERROR, |
| 76 existing_name))); |
| 77 // Elide the existing name for the switch uer button label. |
| 78 existing_name = |
| 79 gfx::TruncateString(existing_name, 10, gfx::CHARACTER_BREAK); |
| 80 } else { |
| 81 source->AddString("signinErrorMessage", |
| 82 l10n_util::GetStringFUTF16( |
| 83 IDS_SIGNIN_ERROR_LEARN_MORE_LINK, last_login_result)); |
| 84 } |
| 85 |
| 86 // Add button label strings. |
| 87 source->AddString("signinErrorSwitchLabel", |
| 88 l10n_util::GetStringFUTF16( |
| 89 IDS_SIGNIN_ERROR_SWITCH_BUTTON_LABEL, existing_name)); |
| 90 source->AddLocalizedString("signinErrorOkLabel", |
| 91 IDS_SIGNIN_ERROR_OK_BUTTON_LABEL); |
| 92 |
| 93 base::DictionaryValue strings; |
| 94 webui::SetLoadTimeDataDefaults(g_browser_process->GetApplicationLocale(), |
| 95 &strings); |
| 96 source->AddLocalizedStrings(strings); |
| 97 |
| 98 content::WebUIDataSource::Add(profile, source); |
| 99 web_ui->AddMessageHandler(handler); |
| 100 } |
OLD | NEW |