NaCl: Update revision in DEPS, r12770 -> r12773
[chromium-blink-merge.git] / chrome / browser / ui / webui / chromeos / login / kiosk_app_menu_handler.cc
blob2b5f0087f910e1388fbb1217bc61c804d0aff181
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.
5 #include "chrome/browser/ui/webui/chromeos/login/kiosk_app_menu_handler.h"
7 #include "base/bind.h"
8 #include "base/command_line.h"
9 #include "base/sys_info.h"
10 #include "base/values.h"
11 #include "chrome/browser/browser_process.h"
12 #include "chrome/browser/chrome_notification_types.h"
13 #include "chrome/browser/chromeos/app_mode/kiosk_app_launch_error.h"
14 #include "chrome/browser/chromeos/login/existing_user_controller.h"
15 #include "chromeos/chromeos_switches.h"
16 #include "content/public/browser/notification_details.h"
17 #include "content/public/browser/notification_service.h"
18 #include "content/public/browser/web_ui.h"
19 #include "grit/generated_resources.h"
20 #include "ui/base/l10n/l10n_util.h"
21 #include "ui/base/webui/web_ui_util.h"
23 namespace chromeos {
25 namespace {
27 // JS functions that define new and old kiosk UI API.
28 const char kKioskSetAppsNewAPI[] = "login.AccountPickerScreen.setApps";
29 const char kKioskSetAppsOldAPI[] = "login.AppsMenuButton.setApps";
30 const char kKioskShowErrorNewAPI[] = "login.AccountPickerScreen.showAppError";
31 const char kKioskShowErrorOldAPI[] = "login.AppsMenuButton.showError";
33 } // namespace
35 KioskAppMenuHandler::KioskAppMenuHandler()
36 : weak_ptr_factory_(this),
37 is_webui_initialized_(false) {
38 KioskAppManager::Get()->AddObserver(this);
41 KioskAppMenuHandler::~KioskAppMenuHandler() {
42 KioskAppManager::Get()->RemoveObserver(this);
45 void KioskAppMenuHandler::GetLocalizedStrings(
46 base::DictionaryValue* localized_strings) {
47 localized_strings->SetString(
48 "showApps",
49 l10n_util::GetStringUTF16(IDS_KIOSK_APPS_BUTTON));
50 localized_strings->SetString(
51 "confirmKioskAppDiagnosticModeFormat",
52 l10n_util::GetStringUTF16(IDS_LOGIN_CONFIRM_KIOSK_DIAGNOSTIC_FORMAT));
53 localized_strings->SetString(
54 "confirmKioskAppDiagnosticModeYes",
55 l10n_util::GetStringUTF16(IDS_CONFIRM_MESSAGEBOX_YES_BUTTON_LABEL));
56 localized_strings->SetString(
57 "confirmKioskAppDiagnosticModeNo",
58 l10n_util::GetStringUTF16(IDS_CONFIRM_MESSAGEBOX_NO_BUTTON_LABEL));
61 void KioskAppMenuHandler::RegisterMessages() {
62 web_ui()->RegisterMessageCallback("initializeKioskApps",
63 base::Bind(&KioskAppMenuHandler::HandleInitializeKioskApps,
64 base::Unretained(this)));
65 web_ui()->RegisterMessageCallback("kioskAppsLoaded",
66 base::Bind(&KioskAppMenuHandler::HandleKioskAppsLoaded,
67 base::Unretained(this)));
68 web_ui()->RegisterMessageCallback("checkKioskAppLaunchError",
69 base::Bind(&KioskAppMenuHandler::HandleCheckKioskAppLaunchError,
70 base::Unretained(this)));
73 void KioskAppMenuHandler::SendKioskApps() {
74 if (!is_webui_initialized_)
75 return;
77 KioskAppManager::Apps apps;
78 KioskAppManager::Get()->GetApps(&apps);
80 base::ListValue apps_list;
81 for (size_t i = 0; i < apps.size(); ++i) {
82 const KioskAppManager::App& app_data = apps[i];
84 scoped_ptr<base::DictionaryValue> app_info(new base::DictionaryValue);
85 app_info->SetBoolean("isApp", true);
86 app_info->SetString("id", app_data.app_id);
87 app_info->SetString("label", app_data.name);
89 // TODO(xiyuan): Replace data url with a URLDataSource.
90 std::string icon_url("chrome://theme/IDR_APP_DEFAULT_ICON");
91 if (!app_data.icon.isNull())
92 icon_url = webui::GetBitmapDataUrl(*app_data.icon.bitmap());
93 app_info->SetString("iconUrl", icon_url);
95 apps_list.Append(app_info.release());
98 bool new_kiosk_ui = !CommandLine::ForCurrentProcess()->
99 HasSwitch(switches::kDisableNewKioskUI);
100 web_ui()->CallJavascriptFunction(new_kiosk_ui ?
101 kKioskSetAppsNewAPI : kKioskSetAppsOldAPI,
102 apps_list);
105 void KioskAppMenuHandler::HandleInitializeKioskApps(
106 const base::ListValue* args) {
107 is_webui_initialized_ = true;
108 SendKioskApps();
111 void KioskAppMenuHandler::HandleKioskAppsLoaded(
112 const base::ListValue* args) {
113 content::NotificationService::current()->Notify(
114 chrome::NOTIFICATION_KIOSK_APPS_LOADED,
115 content::NotificationService::AllSources(),
116 content::NotificationService::NoDetails());
119 void KioskAppMenuHandler::HandleCheckKioskAppLaunchError(
120 const base::ListValue* args) {
121 KioskAppLaunchError::Error error = KioskAppLaunchError::Get();
122 if (error == KioskAppLaunchError::NONE)
123 return;
124 KioskAppLaunchError::Clear();
126 const std::string error_message = KioskAppLaunchError::GetErrorMessage(error);
127 bool new_kiosk_ui = !CommandLine::ForCurrentProcess()->
128 HasSwitch(switches::kDisableNewKioskUI);
129 web_ui()->CallJavascriptFunction(new_kiosk_ui ?
130 kKioskShowErrorNewAPI : kKioskShowErrorOldAPI,
131 base::StringValue(error_message));
134 void KioskAppMenuHandler::OnKioskAppsSettingsChanged() {
135 SendKioskApps();
138 void KioskAppMenuHandler::OnKioskAppDataChanged(const std::string& app_id) {
139 SendKioskApps();
142 } // namespace chromeos