1 // Copyright (c) 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. 4 5 #include "chrome/browser/ui/webui/chromeos/login/terms_of_service_screen_handler.h" 6 7 #include <vector> 8 9 #include "base/bind.h" 10 #include "base/bind_helpers.h" 11 #include "base/memory/scoped_ptr.h" 12 #include "base/prefs/pref_service.h" 13 #include "base/strings/string_split.h" 14 #include "base/values.h" 15 #include "chrome/browser/browser_process.h" 16 #include "chrome/browser/chromeos/base/locale_util.h" 17 #include "chrome/browser/chromeos/login/screens/core_oobe_actor.h" 18 #include "chrome/browser/chromeos/profiles/profile_helper.h" 19 #include "chrome/browser/profiles/profile.h" 20 #include "chrome/browser/profiles/profile_manager.h" 21 #include "chrome/browser/ui/webui/chromeos/login/oobe_ui.h" 22 #include "chrome/common/pref_names.h" 23 #include "chrome/grit/chromium_strings.h" 24 #include "chrome/grit/generated_resources.h" 25 #include "chromeos/ime/input_method_manager.h" 26 #include "components/user_manager/user.h" 27 #include "components/user_manager/user_manager.h" 28 #include "content/public/browser/web_ui.h" 29 30 namespace { 31 32 const char kJsScreenPath[] = "login.TermsOfServiceScreen"; 33 34 } // namespace 35 36 namespace chromeos { 37 38 TermsOfServiceScreenHandler::TermsOfServiceScreenHandler( 39 CoreOobeActor* core_oobe_actor) 40 : BaseScreenHandler(kJsScreenPath), 41 screen_(NULL), 42 core_oobe_actor_(core_oobe_actor), 43 show_on_init_(false), 44 load_error_(false) { 45 } 46 47 TermsOfServiceScreenHandler::~TermsOfServiceScreenHandler() { 48 if (screen_) 49 screen_->OnActorDestroyed(this); 50 } 51 52 void TermsOfServiceScreenHandler::RegisterMessages() { 53 AddCallback("termsOfServiceBack", 54 &TermsOfServiceScreenHandler::HandleBack); 55 AddCallback("termsOfServiceAccept", 56 &TermsOfServiceScreenHandler::HandleAccept); 57 } 58 59 void TermsOfServiceScreenHandler::DeclareLocalizedValues( 60 LocalizedValuesBuilder* builder) { 61 builder->Add("termsOfServiceScreenHeading", 62 IDS_TERMS_OF_SERVICE_SCREEN_HEADING); 63 builder->Add("termsOfServiceScreenSubheading", 64 IDS_TERMS_OF_SERVICE_SCREEN_SUBHEADING); 65 builder->Add("termsOfServiceContentHeading", 66 IDS_TERMS_OF_SERVICE_SCREEN_CONTENT_HEADING); 67 builder->Add("termsOfServiceLoading", IDS_TERMS_OF_SERVICE_SCREEN_LOADING); 68 builder->Add("termsOfServiceError", IDS_TERMS_OF_SERVICE_SCREEN_ERROR); 69 builder->Add("termsOfServiceTryAgain", IDS_TERMS_OF_SERVICE_SCREEN_TRY_AGAIN); 70 builder->Add("termsOfServiceBackButton", 71 IDS_TERMS_OF_SERVICE_SCREEN_BACK_BUTTON); 72 builder->Add("termsOfServiceAcceptButton", 73 IDS_TERMS_OF_SERVICE_SCREEN_ACCEPT_BUTTON); 74 } 75 76 void TermsOfServiceScreenHandler::SetDelegate(Delegate* screen) { 77 screen_ = screen; 78 } 79 80 void TermsOfServiceScreenHandler::Show() { 81 if (!page_is_ready()) { 82 show_on_init_ = true; 83 return; 84 } 85 86 const std::string locale = 87 ProfileHelper::Get() 88 ->GetProfileByUserUnsafe( 89 user_manager::UserManager::Get()->GetActiveUser()) 90 ->GetPrefs() 91 ->GetString(prefs::kApplicationLocale); 92 93 if (locale.empty() || locale == g_browser_process->GetApplicationLocale()) { 94 // If the user has not chosen a UI locale yet or the chosen locale matches 95 // the current UI locale, show the screen immediately. 96 DoShow(); 97 return; 98 } 99 100 // Switch to the user's UI locale before showing the screen. 101 scoped_ptr<locale_util::SwitchLanguageCallback> callback( 102 new locale_util::SwitchLanguageCallback( 103 base::Bind(&TermsOfServiceScreenHandler::OnLanguageChangedCallback, 104 base::Unretained(this)))); 105 locale_util::SwitchLanguage(locale, 106 true, // enable_locale_keyboard_layouts 107 false, // login_layouts_only 108 callback.Pass()); 109 } 110 111 void TermsOfServiceScreenHandler::Hide() { 112 } 113 114 void TermsOfServiceScreenHandler::SetDomain(const std::string& domain) { 115 domain_ = domain; 116 UpdateDomainInUI(); 117 } 118 119 void TermsOfServiceScreenHandler::OnLoadError() { 120 load_error_ = true; 121 terms_of_service_ = ""; 122 UpdateTermsOfServiceInUI(); 123 } 124 125 void TermsOfServiceScreenHandler::OnLoadSuccess( 126 const std::string& terms_of_service) { 127 load_error_ = false; 128 terms_of_service_ = terms_of_service; 129 UpdateTermsOfServiceInUI(); 130 } 131 132 void TermsOfServiceScreenHandler::Initialize() { 133 if (show_on_init_) { 134 Show(); 135 show_on_init_ = false; 136 } 137 } 138 139 void TermsOfServiceScreenHandler::OnLanguageChangedCallback( 140 const std::string& requested_locale, 141 const std::string& loaded_locale, 142 const bool success) { 143 // Update the screen contents to the new locale. 144 base::DictionaryValue localized_strings; 145 static_cast<OobeUI*>(web_ui()->GetController()) 146 ->GetLocalizedStrings(&localized_strings); 147 core_oobe_actor_->ReloadContent(localized_strings); 148 149 DoShow(); 150 } 151 152 void TermsOfServiceScreenHandler::DoShow() { 153 // Determine the user's most preferred input method. 154 std::vector<std::string> input_methods; 155 base::SplitString( 156 ProfileHelper::Get() 157 ->GetProfileByUserUnsafe( 158 user_manager::UserManager::Get()->GetActiveUser()) 159 ->GetPrefs() 160 ->GetString(prefs::kLanguagePreloadEngines), 161 ',', 162 &input_methods); 163 164 if (!input_methods.empty()) { 165 // If the user has a preferred input method, enable it and switch to it. 166 chromeos::input_method::InputMethodManager* input_method_manager = 167 chromeos::input_method::InputMethodManager::Get(); 168 input_method_manager->GetActiveIMEState()->EnableInputMethod( 169 input_methods.front()); 170 input_method_manager->GetActiveIMEState()->ChangeInputMethod( 171 input_methods.front(), false /* show_message */); 172 } 173 174 // Updates the domain name shown in the UI. 175 UpdateDomainInUI(); 176 177 // Update the UI to show an error message or the Terms of Service. 178 UpdateTermsOfServiceInUI(); 179 180 ShowScreen(OobeUI::kScreenTermsOfService, NULL); 181 } 182 183 void TermsOfServiceScreenHandler::UpdateDomainInUI() { 184 if (page_is_ready()) 185 CallJS("setDomain", domain_); 186 } 187 188 void TermsOfServiceScreenHandler::UpdateTermsOfServiceInUI() { 189 if (!page_is_ready()) 190 return; 191 192 // If either |load_error_| or |terms_of_service_| is set, the download of the 193 // Terms of Service has completed and the UI should be updated. Otherwise, the 194 // download is still in progress and the UI will be updated when the 195 // OnLoadError() or the OnLoadSuccess() callback is called. 196 if (load_error_) 197 CallJS("setTermsOfServiceLoadError"); 198 else if (!terms_of_service_.empty()) 199 CallJS("setTermsOfService", terms_of_service_); 200 } 201 202 void TermsOfServiceScreenHandler::HandleBack() { 203 if (screen_) 204 screen_->OnDecline(); 205 } 206 207 void TermsOfServiceScreenHandler::HandleAccept() { 208 if (!screen_) 209 return; 210 211 // If the Terms of Service have not been successfully downloaded, the "accept 212 // and continue" button should not be accessible. If the user managed to 213 // activate it somehow anway, do not treat this as acceptance of the Terms 214 // and Conditions and end the session instead, as if the user had declined. 215 if (terms_of_service_.empty()) 216 screen_->OnDecline(); 217 else 218 screen_->OnAccept(); 219 } 220 221 } // namespace chromeos 222