1868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// Copyright 2013 The Chromium Authors. All rights reserved.
2868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
3868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// found in the LICENSE file.
4868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
5868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "chrome/browser/ui/webui/chromeos/login/kiosk_autolaunch_screen_handler.h"
6868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
7868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include <string>
8868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
9868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "base/command_line.h"
10868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "base/prefs/pref_service.h"
11868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "base/strings/utf_string_conversions.h"
12868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "chrome/browser/browser_process.h"
137dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch#include "chrome/browser/chrome_notification_types.h"
14868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "chrome/browser/chromeos/app_mode/kiosk_app_manager.h"
15868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "chrome/common/chrome_switches.h"
16868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "chrome/common/pref_names.h"
171320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "chrome/grit/generated_resources.h"
18868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "chromeos/chromeos_switches.h"
19868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "chromeos/dbus/dbus_thread_manager.h"
20868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "chromeos/dbus/session_manager_client.h"
21868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "content/public/browser/notification_details.h"
22868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "content/public/browser/notification_service.h"
234e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)#include "ui/base/webui/web_ui_util.h"
24868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
25868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)namespace {
26868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
27a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)const char kJsScreenPath[] = "login.AutolaunchScreen";
28a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)
29868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// Autolaunch screen id.
30868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)const char kAutolaunchScreen[] = "autolaunch";
31868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
32868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}  // namespace
33868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
34868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)namespace chromeos {
35868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
36868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)KioskAutolaunchScreenHandler::KioskAutolaunchScreenHandler()
37a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)    : BaseScreenHandler(kJsScreenPath),
38a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)      delegate_(NULL),
39a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)      show_on_init_(false),
40a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)      is_visible_(false) {
41868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  KioskAppManager::Get()->AddObserver(this);
42868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}
43868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
44868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)KioskAutolaunchScreenHandler::~KioskAutolaunchScreenHandler() {
45868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  if (delegate_)
46868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    delegate_->OnActorDestroyed(this);
47868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
48868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  KioskAppManager::Get()->RemoveObserver(this);
49868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}
50868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
51868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)void KioskAutolaunchScreenHandler::Show() {
52868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  if (!page_is_ready()) {
53868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    show_on_init_ = true;
54868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    return;
55868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  }
56868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  UpdateKioskApp();
57868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  ShowScreen(kAutolaunchScreen, NULL);
58868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}
59868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
60868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)void KioskAutolaunchScreenHandler::SetDelegate(Delegate* delegate) {
61868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  delegate_ = delegate;
62868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  if (page_is_ready())
63868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    Initialize();
64868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}
65868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
66868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)void KioskAutolaunchScreenHandler::UpdateKioskApp() {
67868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  if (!is_visible_)
68868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    return;
69868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
70868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  KioskAppManager* manager = KioskAppManager::Get();
71868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  KioskAppManager::App app;
72868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  std::string app_id = manager->GetAutoLaunchApp();
73868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  if (app_id.empty() ||
74868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      manager->IsAutoLaunchEnabled() ||
75868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      !manager->GetApp(app_id, &app)) {
76868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    return;
77868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  }
78868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
79868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  base::DictionaryValue app_info;
80868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  app_info.SetString("appName", app.name);
81868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
82868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  std::string icon_url("chrome://theme/IDR_APP_DEFAULT_ICON");
83868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  if (!app.icon.isNull())
84868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    icon_url = webui::GetBitmapDataUrl(*app.icon.bitmap());
85868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
86868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  app_info.SetString("appIconUrl", icon_url);
87a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  CallJS("updateApp", app_info);
88868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}
89868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
90868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)void KioskAutolaunchScreenHandler::DeclareLocalizedValues(
91868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    LocalizedValuesBuilder* builder) {
92868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  builder->Add("autolaunchTitle", IDS_AUTOSTART_WARNING_TITLE);
93868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  builder->Add("autolaunchWarningTitle", IDS_AUTOSTART_WARNING_TITLE);
94868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  builder->Add("autolaunchWarning", IDS_KIOSK_AUTOSTART_SCREEN_WARNING_MSG);
95868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  builder->Add("autolaunchConfirmButton", IDS_KIOSK_AUTOSTART_CONFIRM);
96868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  builder->Add("autolaunchCancelButton", IDS_CANCEL);
97868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}
98868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
99868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)void KioskAutolaunchScreenHandler::Initialize() {
100868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  if (!page_is_ready() || !delegate_)
101868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    return;
102868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
103868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  if (show_on_init_) {
104868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    Show();
105868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    show_on_init_ = false;
106868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  }
107868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}
108868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
109868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)void KioskAutolaunchScreenHandler::RegisterMessages() {
110868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  AddCallback("autolaunchVisible",
111868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)              &KioskAutolaunchScreenHandler::HandleOnVisible);
112868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  AddCallback("autolaunchOnCancel",
113868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)              &KioskAutolaunchScreenHandler::HandleOnCancel);
114868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  AddCallback("autolaunchOnConfirm",
115868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)              &KioskAutolaunchScreenHandler::HandleOnConfirm);
116868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}
117868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
118868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)void KioskAutolaunchScreenHandler::HandleOnCancel() {
119868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  KioskAppManager::Get()->RemoveObserver(this);
120868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  KioskAppManager::Get()->SetEnableAutoLaunch(false);
121868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  if (delegate_)
122868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    delegate_->OnExit(false);
123868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
124868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  content::NotificationService::current()->Notify(
125868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      chrome::NOTIFICATION_KIOSK_AUTOLAUNCH_WARNING_COMPLETED,
126868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      content::NotificationService::AllSources(),
127868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      content::NotificationService::NoDetails());
128868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}
129868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
130868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)void KioskAutolaunchScreenHandler::HandleOnConfirm() {
131868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  KioskAppManager::Get()->RemoveObserver(this);
132868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  KioskAppManager::Get()->SetEnableAutoLaunch(true);
133868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  if (delegate_)
134868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    delegate_->OnExit(true);
135868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
136868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  content::NotificationService::current()->Notify(
137868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      chrome::NOTIFICATION_KIOSK_AUTOLAUNCH_WARNING_COMPLETED,
138868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      content::NotificationService::AllSources(),
139868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      content::NotificationService::NoDetails());
140868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}
141868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
142868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)void KioskAutolaunchScreenHandler::HandleOnVisible() {
143868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  if (is_visible_)
144868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    return;
145868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
146868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  is_visible_ = true;
147868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  UpdateKioskApp();
148868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  content::NotificationService::current()->Notify(
149868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      chrome::NOTIFICATION_KIOSK_AUTOLAUNCH_WARNING_VISIBLE,
150868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      content::NotificationService::AllSources(),
151868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      content::NotificationService::NoDetails());
152868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}
153868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
154868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)void KioskAutolaunchScreenHandler::OnKioskAppsSettingsChanged() {
155868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  UpdateKioskApp();
156868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}
157868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
158868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)void KioskAutolaunchScreenHandler::OnKioskAppDataChanged(
159868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    const std::string& app_id) {
160868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  UpdateKioskApp();
161868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}
162868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
163868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}  // namespace chromeos
164