1// Copyright 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/kiosk_autolaunch_screen_handler.h"
6
7#include <string>
8
9#include "base/command_line.h"
10#include "base/prefs/pref_service.h"
11#include "base/strings/utf_string_conversions.h"
12#include "chrome/browser/browser_process.h"
13#include "chrome/browser/chrome_notification_types.h"
14#include "chrome/browser/chromeos/app_mode/kiosk_app_manager.h"
15#include "chrome/common/chrome_switches.h"
16#include "chrome/common/pref_names.h"
17#include "chromeos/chromeos_switches.h"
18#include "chromeos/dbus/dbus_thread_manager.h"
19#include "chromeos/dbus/session_manager_client.h"
20#include "content/public/browser/notification_details.h"
21#include "content/public/browser/notification_service.h"
22#include "grit/browser_resources.h"
23#include "grit/chromium_strings.h"
24#include "grit/generated_resources.h"
25#include "ui/webui/web_ui_util.h"
26
27namespace {
28
29const char kJsScreenPath[] = "login.AutolaunchScreen";
30
31// Autolaunch screen id.
32const char kAutolaunchScreen[] = "autolaunch";
33
34}  // namespace
35
36namespace chromeos {
37
38KioskAutolaunchScreenHandler::KioskAutolaunchScreenHandler()
39    : BaseScreenHandler(kJsScreenPath),
40      delegate_(NULL),
41      show_on_init_(false),
42      is_visible_(false) {
43  KioskAppManager::Get()->AddObserver(this);
44}
45
46KioskAutolaunchScreenHandler::~KioskAutolaunchScreenHandler() {
47  if (delegate_)
48    delegate_->OnActorDestroyed(this);
49
50  KioskAppManager::Get()->RemoveObserver(this);
51}
52
53void KioskAutolaunchScreenHandler::Show() {
54  if (!page_is_ready()) {
55    show_on_init_ = true;
56    return;
57  }
58  UpdateKioskApp();
59  ShowScreen(kAutolaunchScreen, NULL);
60}
61
62void KioskAutolaunchScreenHandler::SetDelegate(Delegate* delegate) {
63  delegate_ = delegate;
64  if (page_is_ready())
65    Initialize();
66}
67
68void KioskAutolaunchScreenHandler::UpdateKioskApp() {
69  if (!is_visible_)
70    return;
71
72  KioskAppManager* manager = KioskAppManager::Get();
73  KioskAppManager::App app;
74  std::string app_id = manager->GetAutoLaunchApp();
75  if (app_id.empty() ||
76      manager->IsAutoLaunchEnabled() ||
77      !manager->GetApp(app_id, &app)) {
78    return;
79  }
80
81  base::DictionaryValue app_info;
82  app_info.SetString("appName", app.name);
83
84  std::string icon_url("chrome://theme/IDR_APP_DEFAULT_ICON");
85  if (!app.icon.isNull())
86    icon_url = webui::GetBitmapDataUrl(*app.icon.bitmap());
87
88  app_info.SetString("appIconUrl", icon_url);
89  CallJS("updateApp", app_info);
90}
91
92void KioskAutolaunchScreenHandler::DeclareLocalizedValues(
93    LocalizedValuesBuilder* builder) {
94  builder->Add("autolaunchTitle", IDS_AUTOSTART_WARNING_TITLE);
95  builder->Add("autolaunchWarningTitle", IDS_AUTOSTART_WARNING_TITLE);
96  builder->Add("autolaunchWarning", IDS_KIOSK_AUTOSTART_SCREEN_WARNING_MSG);
97  builder->Add("autolaunchConfirmButton", IDS_KIOSK_AUTOSTART_CONFIRM);
98  builder->Add("autolaunchCancelButton", IDS_CANCEL);
99}
100
101void KioskAutolaunchScreenHandler::Initialize() {
102  if (!page_is_ready() || !delegate_)
103    return;
104
105  if (show_on_init_) {
106    Show();
107    show_on_init_ = false;
108  }
109}
110
111void KioskAutolaunchScreenHandler::RegisterMessages() {
112  AddCallback("autolaunchVisible",
113              &KioskAutolaunchScreenHandler::HandleOnVisible);
114  AddCallback("autolaunchOnCancel",
115              &KioskAutolaunchScreenHandler::HandleOnCancel);
116  AddCallback("autolaunchOnConfirm",
117              &KioskAutolaunchScreenHandler::HandleOnConfirm);
118}
119
120void KioskAutolaunchScreenHandler::HandleOnCancel() {
121  KioskAppManager::Get()->RemoveObserver(this);
122  KioskAppManager::Get()->SetEnableAutoLaunch(false);
123  if (delegate_)
124    delegate_->OnExit(false);
125
126  content::NotificationService::current()->Notify(
127      chrome::NOTIFICATION_KIOSK_AUTOLAUNCH_WARNING_COMPLETED,
128      content::NotificationService::AllSources(),
129      content::NotificationService::NoDetails());
130}
131
132void KioskAutolaunchScreenHandler::HandleOnConfirm() {
133  KioskAppManager::Get()->RemoveObserver(this);
134  KioskAppManager::Get()->SetEnableAutoLaunch(true);
135  if (delegate_)
136    delegate_->OnExit(true);
137
138  content::NotificationService::current()->Notify(
139      chrome::NOTIFICATION_KIOSK_AUTOLAUNCH_WARNING_COMPLETED,
140      content::NotificationService::AllSources(),
141      content::NotificationService::NoDetails());
142}
143
144void KioskAutolaunchScreenHandler::HandleOnVisible() {
145  if (is_visible_)
146    return;
147
148  is_visible_ = true;
149  UpdateKioskApp();
150  content::NotificationService::current()->Notify(
151      chrome::NOTIFICATION_KIOSK_AUTOLAUNCH_WARNING_VISIBLE,
152      content::NotificationService::AllSources(),
153      content::NotificationService::NoDetails());
154}
155
156void KioskAutolaunchScreenHandler::OnKioskAppsSettingsChanged() {
157  UpdateKioskApp();
158}
159
160void KioskAutolaunchScreenHandler::OnKioskAppDataChanged(
161    const std::string& app_id) {
162  UpdateKioskApp();
163}
164
165}  // namespace chromeos
166