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.
5 #include "chrome/browser/ui/webui/chromeos/login/terms_of_service_screen_handler.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"
32 const char kJsScreenPath[] = "login.TermsOfServiceScreen";
38 TermsOfServiceScreenHandler::TermsOfServiceScreenHandler(
39 CoreOobeActor* core_oobe_actor)
40 : BaseScreenHandler(kJsScreenPath),
42 core_oobe_actor_(core_oobe_actor),
47 TermsOfServiceScreenHandler::~TermsOfServiceScreenHandler() {
49 screen_->OnActorDestroyed(this);
52 void TermsOfServiceScreenHandler::RegisterMessages() {
53 AddCallback("termsOfServiceBack",
54 &TermsOfServiceScreenHandler::HandleBack);
55 AddCallback("termsOfServiceAccept",
56 &TermsOfServiceScreenHandler::HandleAccept);
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);
76 void TermsOfServiceScreenHandler::SetDelegate(Delegate* screen) {
80 void TermsOfServiceScreenHandler::Show() {
81 if (!page_is_ready()) {
86 const std::string locale =
88 ->GetProfileByUserUnsafe(
89 user_manager::UserManager::Get()->GetActiveUser())
91 ->GetString(prefs::kApplicationLocale);
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.
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
111 void TermsOfServiceScreenHandler::Hide() {
114 void TermsOfServiceScreenHandler::SetDomain(const std::string& domain) {
119 void TermsOfServiceScreenHandler::OnLoadError() {
121 terms_of_service_ = "";
122 UpdateTermsOfServiceInUI();
125 void TermsOfServiceScreenHandler::OnLoadSuccess(
126 const std::string& terms_of_service) {
128 terms_of_service_ = terms_of_service;
129 UpdateTermsOfServiceInUI();
132 void TermsOfServiceScreenHandler::Initialize() {
135 show_on_init_ = false;
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);
152 void TermsOfServiceScreenHandler::DoShow() {
153 // Determine the user's most preferred input method.
154 std::vector<std::string> input_methods;
157 ->GetProfileByUserUnsafe(
158 user_manager::UserManager::Get()->GetActiveUser())
160 ->GetString(prefs::kLanguagePreloadEngines),
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 */);
174 // Updates the domain name shown in the UI.
177 // Update the UI to show an error message or the Terms of Service.
178 UpdateTermsOfServiceInUI();
180 ShowScreen(OobeUI::kScreenTermsOfService, NULL);
183 void TermsOfServiceScreenHandler::UpdateDomainInUI() {
185 CallJS("setDomain", domain_);
188 void TermsOfServiceScreenHandler::UpdateTermsOfServiceInUI() {
189 if (!page_is_ready())
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.
197 CallJS("setTermsOfServiceLoadError");
198 else if (!terms_of_service_.empty())
199 CallJS("setTermsOfService", terms_of_service_);
202 void TermsOfServiceScreenHandler::HandleBack() {
204 screen_->OnDecline();
207 void TermsOfServiceScreenHandler::HandleAccept() {
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();
221 } // namespace chromeos