1// Copyright (c) 2012 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/eula_screen_handler.h"
6
7#include <string>
8
9#include "base/strings/utf_string_conversions.h"
10#include "chrome/browser/chromeos/login/help_app_launcher.h"
11#include "chrome/browser/chromeos/login/helper.h"
12#include "chrome/browser/chromeos/login/screens/core_oobe_actor.h"
13#include "chrome/browser/chromeos/login/ui/login_web_dialog.h"
14#include "chrome/browser/chromeos/login/ui/webui_login_display.h"
15#include "chrome/browser/profiles/profile.h"
16#include "chrome/browser/ui/webui/chromeos/login/oobe_ui.h"
17#include "chrome/common/url_constants.h"
18#include "chrome/grit/chromium_strings.h"
19#include "chrome/grit/generated_resources.h"
20#include "chrome/grit/locale_settings.h"
21#include "content/public/browser/web_contents.h"
22#include "ui/base/l10n/l10n_util.h"
23#include "ui/views/widget/widget.h"
24#include "url/gurl.h"
25
26namespace {
27
28const char kJsScreenPath[] = "login.EulaScreen";
29
30// Helper class to tweak display details of credits pages in the context
31// of OOBE/EULA step.
32class CreditsWebDialog : public chromeos::LoginWebDialog {
33 public:
34  CreditsWebDialog(Profile* profile,
35                   gfx::NativeWindow parent_window,
36                   int title_id,
37                   const GURL& url)
38      : chromeos::LoginWebDialog(profile, NULL, parent_window,
39                                 l10n_util::GetStringUTF16(title_id),
40                                 url,
41                                 chromeos::LoginWebDialog::STYLE_BUBBLE) {
42  }
43
44  virtual void OnLoadingStateChanged(content::WebContents* source) OVERRIDE {
45    chromeos::LoginWebDialog::OnLoadingStateChanged(source);
46    // Remove visual elements that we can handle in EULA page.
47    bool is_loading = source->IsLoading();
48    if (!is_loading && source->GetWebUI()) {
49      source->GetWebUI()->CallJavascriptFunction(
50          "(function () {"
51          "  document.body.classList.toggle('dialog', true);"
52          "  keyboard.initializeKeyboardFlow();"
53          "})");
54    }
55  }
56
57 private:
58  DISALLOW_COPY_AND_ASSIGN(CreditsWebDialog);
59};
60
61void ShowCreditsDialog(Profile* profile,
62                       gfx::NativeWindow parent_window,
63                       int title_id,
64                       const GURL& credits_url) {
65  CreditsWebDialog* dialog = new CreditsWebDialog(profile,
66                                                  parent_window,
67                                                  title_id,
68                                                  credits_url);
69  gfx::Rect screen_bounds(chromeos::CalculateScreenBounds(gfx::Size()));
70  dialog->SetDialogSize(l10n_util::GetLocalizedContentsWidthInPixels(
71                            IDS_CREDITS_APP_DIALOG_WIDTH_PIXELS),
72                        l10n_util::GetLocalizedContentsWidthInPixels(
73                            IDS_CREDITS_APP_DIALOG_HEIGHT_PIXELS));
74  dialog->Show();
75  // The dialog object will be deleted on dialog close.
76}
77
78}  // namespace
79
80namespace chromeos {
81
82EulaScreenHandler::EulaScreenHandler(CoreOobeActor* core_oobe_actor)
83    : BaseScreenHandler(kJsScreenPath),
84      delegate_(NULL),
85      core_oobe_actor_(core_oobe_actor),
86      show_on_init_(false) {
87}
88
89EulaScreenHandler::~EulaScreenHandler() {
90  if (delegate_)
91    delegate_->OnActorDestroyed(this);
92}
93
94void EulaScreenHandler::PrepareToShow() {
95}
96
97void EulaScreenHandler::Show() {
98  if (!page_is_ready()) {
99    show_on_init_ = true;
100    return;
101  }
102  ShowScreen(OobeUI::kScreenOobeEula, NULL);
103}
104
105void EulaScreenHandler::Hide() {
106}
107
108void EulaScreenHandler::SetDelegate(Delegate* delegate) {
109  delegate_ = delegate;
110  if (page_is_ready())
111    Initialize();
112}
113
114void EulaScreenHandler::DeclareLocalizedValues(
115    LocalizedValuesBuilder* builder) {
116  builder->Add("eulaScreenTitle", IDS_EULA_SCREEN_TITLE);
117  builder->Add("eulaScreenAccessibleTitle", IDS_EULA_SCREEN_ACCESSIBLE_TITLE);
118  builder->Add("checkboxLogging", IDS_EULA_CHECKBOX_ENABLE_LOGGING);
119  builder->Add("back", IDS_EULA_BACK_BUTTON);
120  builder->Add("acceptAgreement", IDS_EULA_ACCEPT_AND_CONTINUE_BUTTON);
121  builder->Add("eulaSystemInstallationSettings",
122               IDS_EULA_SYSTEM_SECURITY_SETTING);
123  builder->Add("eulaTpmDesc", IDS_EULA_TPM_DESCRIPTION);
124  builder->Add("eulaTpmKeyDesc", IDS_EULA_TPM_KEY_DESCRIPTION);
125  builder->Add("eulaTpmDescPowerwash", IDS_EULA_TPM_KEY_DESCRIPTION_POWERWASH);
126  builder->Add("eulaTpmBusy", IDS_EULA_TPM_BUSY);
127  builder->Add("eulaSystemInstallationSettingsOkButton", IDS_OK);
128  builder->Add("termsOfServiceLoading", IDS_TERMS_OF_SERVICE_SCREEN_LOADING);
129#if defined(ENABLE_RLZ)
130  builder->AddF("eulaRlzDesc",
131                IDS_EULA_RLZ_DESCRIPTION,
132                IDS_SHORT_PRODUCT_NAME,
133                IDS_PRODUCT_NAME);
134  builder->AddF("eulaRlzEnable",
135                IDS_EULA_RLZ_ENABLE,
136                IDS_SHORT_PRODUCT_OS_NAME);
137#endif
138
139  builder->Add("chromeCreditsLink", IDS_ABOUT_VERSION_LICENSE_EULA);
140  builder->Add("chromeosCreditsLink", IDS_ABOUT_CROS_VERSION_LICENSE_EULA);
141}
142
143void EulaScreenHandler::GetAdditionalParameters(base::DictionaryValue* dict) {
144#if defined(ENABLE_RLZ)
145  dict->SetString("rlzEnabled", "enabled");
146#else
147  dict->SetString("rlzEnabled", "disabled");
148#endif
149}
150
151void EulaScreenHandler::Initialize() {
152  if (!page_is_ready() || !delegate_)
153    return;
154
155  core_oobe_actor_->SetUsageStats(delegate_->IsUsageStatsEnabled());
156
157  // This OEM EULA is a file:// URL which we're unable to load in iframe.
158  // Instead if it's defined we use chrome://terms/oem that will load same file.
159  if (!delegate_->GetOemEulaUrl().is_empty())
160    core_oobe_actor_->SetOemEulaUrl(chrome::kChromeUITermsOemURL);
161
162  if (show_on_init_) {
163    Show();
164    show_on_init_ = false;
165  }
166}
167
168void EulaScreenHandler::RegisterMessages() {
169  AddCallback("eulaOnExit", &EulaScreenHandler::HandleOnExit);
170  AddCallback("eulaOnLearnMore", &EulaScreenHandler::HandleOnLearnMore);
171  AddCallback("eulaOnChromeOSCredits",
172              &EulaScreenHandler::HandleOnChromeOSCredits);
173  AddCallback("eulaOnChromeCredits",
174              &EulaScreenHandler::HandleOnChromeCredits);
175  AddCallback("eulaOnLearnMore", &EulaScreenHandler::HandleOnLearnMore);
176  AddCallback("eulaOnInstallationSettingsPopupOpened",
177              &EulaScreenHandler::HandleOnInstallationSettingsPopupOpened);
178}
179
180void EulaScreenHandler::OnPasswordFetched(const std::string& tpm_password) {
181  core_oobe_actor_->SetTpmPassword(tpm_password);
182}
183
184void EulaScreenHandler::HandleOnExit(bool accepted, bool usage_stats_enabled) {
185  if (delegate_)
186    delegate_->OnExit(accepted, usage_stats_enabled);
187}
188
189void EulaScreenHandler::HandleOnLearnMore() {
190  if (!help_app_.get())
191    help_app_ = new HelpAppLauncher(GetNativeWindow());
192  help_app_->ShowHelpTopic(HelpAppLauncher::HELP_STATS_USAGE);
193}
194
195void EulaScreenHandler::HandleOnChromeOSCredits() {
196  ShowCreditsDialog(
197      Profile::FromBrowserContext(
198          web_ui()->GetWebContents()->GetBrowserContext()),
199      GetNativeWindow(),
200      IDS_ABOUT_CROS_VERSION_LICENSE_EULA,
201      GURL(chrome::kChromeUIOSCreditsURL));
202}
203
204void EulaScreenHandler::HandleOnChromeCredits() {
205  ShowCreditsDialog(
206      Profile::FromBrowserContext(
207          web_ui()->GetWebContents()->GetBrowserContext()),
208      GetNativeWindow(),
209      IDS_ABOUT_VERSION_LICENSE_EULA,
210      GURL(chrome::kChromeUICreditsURL));
211}
212
213void EulaScreenHandler::HandleOnInstallationSettingsPopupOpened() {
214  if (delegate_)
215    delegate_->InitiatePasswordFetch();
216}
217
218}  // namespace chromeos
219