Popular sites on the NTP: check that experiment group StartsWith (rather than IS...
[chromium-blink-merge.git] / chrome / browser / ui / webui / chromeos / login / terms_of_service_screen_handler.cc
blob83d866369071985fe4545ce2c489f3a7fa26b802
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"
7 #include <vector>
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 "components/login/localized_values_builder.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 #include "ui/base/ime/chromeos/input_method_manager.h"
31 namespace {
33 const char kJsScreenPath[] = "login.TermsOfServiceScreen";
35 } // namespace
37 namespace chromeos {
39 TermsOfServiceScreenHandler::TermsOfServiceScreenHandler(
40 CoreOobeActor* core_oobe_actor)
41 : BaseScreenHandler(kJsScreenPath),
42 screen_(NULL),
43 core_oobe_actor_(core_oobe_actor),
44 show_on_init_(false),
45 load_error_(false) {
48 TermsOfServiceScreenHandler::~TermsOfServiceScreenHandler() {
49 if (screen_)
50 screen_->OnActorDestroyed(this);
53 void TermsOfServiceScreenHandler::RegisterMessages() {
54 AddCallback("termsOfServiceBack",
55 &TermsOfServiceScreenHandler::HandleBack);
56 AddCallback("termsOfServiceAccept",
57 &TermsOfServiceScreenHandler::HandleAccept);
60 void TermsOfServiceScreenHandler::DeclareLocalizedValues(
61 ::login::LocalizedValuesBuilder* builder) {
62 builder->Add("termsOfServiceScreenHeading",
63 IDS_TERMS_OF_SERVICE_SCREEN_HEADING);
64 builder->Add("termsOfServiceScreenSubheading",
65 IDS_TERMS_OF_SERVICE_SCREEN_SUBHEADING);
66 builder->Add("termsOfServiceContentHeading",
67 IDS_TERMS_OF_SERVICE_SCREEN_CONTENT_HEADING);
68 builder->Add("termsOfServiceLoading", IDS_TERMS_OF_SERVICE_SCREEN_LOADING);
69 builder->Add("termsOfServiceError", IDS_TERMS_OF_SERVICE_SCREEN_ERROR);
70 builder->Add("termsOfServiceTryAgain", IDS_TERMS_OF_SERVICE_SCREEN_TRY_AGAIN);
71 builder->Add("termsOfServiceBackButton",
72 IDS_TERMS_OF_SERVICE_SCREEN_BACK_BUTTON);
73 builder->Add("termsOfServiceAcceptButton",
74 IDS_TERMS_OF_SERVICE_SCREEN_ACCEPT_BUTTON);
77 void TermsOfServiceScreenHandler::SetDelegate(Delegate* screen) {
78 screen_ = screen;
81 void TermsOfServiceScreenHandler::Show() {
82 if (!page_is_ready()) {
83 show_on_init_ = true;
84 return;
87 const std::string locale =
88 ProfileHelper::Get()
89 ->GetProfileByUserUnsafe(
90 user_manager::UserManager::Get()->GetActiveUser())
91 ->GetPrefs()
92 ->GetString(prefs::kApplicationLocale);
94 if (locale.empty() || locale == g_browser_process->GetApplicationLocale()) {
95 // If the user has not chosen a UI locale yet or the chosen locale matches
96 // the current UI locale, show the screen immediately.
97 DoShow();
98 return;
101 // Switch to the user's UI locale before showing the screen.
102 locale_util::SwitchLanguageCallback callback(
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, ProfileManager::GetActiveUserProfile());
111 void TermsOfServiceScreenHandler::Hide() {
114 void TermsOfServiceScreenHandler::SetDomain(const std::string& domain) {
115 domain_ = domain;
116 UpdateDomainInUI();
119 void TermsOfServiceScreenHandler::OnLoadError() {
120 load_error_ = true;
121 terms_of_service_ = "";
122 UpdateTermsOfServiceInUI();
125 void TermsOfServiceScreenHandler::OnLoadSuccess(
126 const std::string& terms_of_service) {
127 load_error_ = false;
128 terms_of_service_ = terms_of_service;
129 UpdateTermsOfServiceInUI();
132 void TermsOfServiceScreenHandler::Initialize() {
133 if (show_on_init_) {
134 Show();
135 show_on_init_ = false;
139 void TermsOfServiceScreenHandler::OnLanguageChangedCallback(
140 const locale_util::LanguageSwitchResult& result) {
141 // Update the screen contents to the new locale.
142 base::DictionaryValue localized_strings;
143 static_cast<OobeUI*>(web_ui()->GetController())
144 ->GetLocalizedStrings(&localized_strings);
145 core_oobe_actor_->ReloadContent(localized_strings);
147 DoShow();
150 void TermsOfServiceScreenHandler::DoShow() {
151 // Determine the user's most preferred input method.
152 std::vector<std::string> input_methods = base::SplitString(
153 ProfileHelper::Get()
154 ->GetProfileByUserUnsafe(
155 user_manager::UserManager::Get()->GetActiveUser())
156 ->GetPrefs()
157 ->GetString(prefs::kLanguagePreloadEngines),
158 ",", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL);
160 if (!input_methods.empty()) {
161 // If the user has a preferred input method, enable it and switch to it.
162 chromeos::input_method::InputMethodManager* input_method_manager =
163 chromeos::input_method::InputMethodManager::Get();
164 input_method_manager->GetActiveIMEState()->EnableInputMethod(
165 input_methods.front());
166 input_method_manager->GetActiveIMEState()->ChangeInputMethod(
167 input_methods.front(), false /* show_message */);
170 // Updates the domain name shown in the UI.
171 UpdateDomainInUI();
173 // Update the UI to show an error message or the Terms of Service.
174 UpdateTermsOfServiceInUI();
176 ShowScreen(OobeUI::kScreenTermsOfService, NULL);
179 void TermsOfServiceScreenHandler::UpdateDomainInUI() {
180 if (page_is_ready())
181 CallJS("setDomain", domain_);
184 void TermsOfServiceScreenHandler::UpdateTermsOfServiceInUI() {
185 if (!page_is_ready())
186 return;
188 // If either |load_error_| or |terms_of_service_| is set, the download of the
189 // Terms of Service has completed and the UI should be updated. Otherwise, the
190 // download is still in progress and the UI will be updated when the
191 // OnLoadError() or the OnLoadSuccess() callback is called.
192 if (load_error_)
193 CallJS("setTermsOfServiceLoadError");
194 else if (!terms_of_service_.empty())
195 CallJS("setTermsOfService", terms_of_service_);
198 void TermsOfServiceScreenHandler::HandleBack() {
199 if (screen_)
200 screen_->OnDecline();
203 void TermsOfServiceScreenHandler::HandleAccept() {
204 if (!screen_)
205 return;
207 // If the Terms of Service have not been successfully downloaded, the "accept
208 // and continue" button should not be accessible. If the user managed to
209 // activate it somehow anway, do not treat this as acceptance of the Terms
210 // and Conditions and end the session instead, as if the user had declined.
211 if (terms_of_service_.empty())
212 screen_->OnDecline();
213 else
214 screen_->OnAccept();
217 } // namespace chromeos