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