Add new certificateProvider extension API.
[chromium-blink-merge.git] / chrome / browser / ui / webui / chromeos / login / kiosk_app_menu_handler.cc
blob9934b0587ae27eb28c602e2b26bd6d59a6cae7cb
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/strings/string_number_conversions.h"
10 #include "base/sys_info.h"
11 #include "base/values.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_launch_error.h"
15 #include "chrome/browser/chromeos/login/existing_user_controller.h"
16 #include "chrome/browser/chromeos/login/screens/network_error.h"
17 #include "chrome/grit/generated_resources.h"
18 #include "chromeos/chromeos_switches.h"
19 #include "content/public/browser/notification_details.h"
20 #include "content/public/browser/notification_service.h"
21 #include "content/public/browser/web_ui.h"
22 #include "ui/base/l10n/l10n_util.h"
23 #include "ui/base/webui/web_ui_util.h"
25 namespace chromeos {
27 namespace {
29 // JS functions that define new and old kiosk UI API.
30 const char kKioskSetAppsNewAPI[] = "login.AccountPickerScreen.setApps";
31 const char kKioskSetAppsOldAPI[] = "login.AppsMenuButton.setApps";
32 const char kKioskShowErrorNewAPI[] = "login.AccountPickerScreen.showAppError";
33 const char kKioskShowErrorOldAPI[] = "login.AppsMenuButton.showError";
35 } // namespace
37 KioskAppMenuHandler::KioskAppMenuHandler(
38 const scoped_refptr<NetworkStateInformer>& network_state_informer)
39 : is_webui_initialized_(false),
40 network_state_informer_(network_state_informer),
41 weak_ptr_factory_(this) {
42 KioskAppManager::Get()->AddObserver(this);
43 network_state_informer_->AddObserver(this);
46 KioskAppMenuHandler::~KioskAppMenuHandler() {
47 KioskAppManager::Get()->RemoveObserver(this);
48 network_state_informer_->RemoveObserver(this);
51 void KioskAppMenuHandler::GetLocalizedStrings(
52 base::DictionaryValue* localized_strings) {
53 localized_strings->SetString(
54 "showApps",
55 l10n_util::GetStringUTF16(IDS_KIOSK_APPS_BUTTON));
56 localized_strings->SetString(
57 "confirmKioskAppDiagnosticModeFormat",
58 l10n_util::GetStringUTF16(IDS_LOGIN_CONFIRM_KIOSK_DIAGNOSTIC_FORMAT));
59 localized_strings->SetString(
60 "confirmKioskAppDiagnosticModeYes",
61 l10n_util::GetStringUTF16(IDS_CONFIRM_MESSAGEBOX_YES_BUTTON_LABEL));
62 localized_strings->SetString(
63 "confirmKioskAppDiagnosticModeNo",
64 l10n_util::GetStringUTF16(IDS_CONFIRM_MESSAGEBOX_NO_BUTTON_LABEL));
65 localized_strings->SetBoolean(
66 "kioskAppHasLaunchError",
67 KioskAppLaunchError::Get() != KioskAppLaunchError::NONE);
70 void KioskAppMenuHandler::RegisterMessages() {
71 web_ui()->RegisterMessageCallback("initializeKioskApps",
72 base::Bind(&KioskAppMenuHandler::HandleInitializeKioskApps,
73 base::Unretained(this)));
74 web_ui()->RegisterMessageCallback("kioskAppsLoaded",
75 base::Bind(&KioskAppMenuHandler::HandleKioskAppsLoaded,
76 base::Unretained(this)));
77 web_ui()->RegisterMessageCallback("checkKioskAppLaunchError",
78 base::Bind(&KioskAppMenuHandler::HandleCheckKioskAppLaunchError,
79 base::Unretained(this)));
82 // static
83 bool KioskAppMenuHandler::EnableNewKioskUI() {
84 // Turn off new kiosk UI for M34/35.
85 // TODO(xiyuan, nkostylev): Revist for http://crbug.com/362062.
86 return false;
89 void KioskAppMenuHandler::SendKioskApps() {
90 if (!is_webui_initialized_)
91 return;
93 KioskAppManager::Apps apps;
94 KioskAppManager::Get()->GetApps(&apps);
96 base::ListValue apps_list;
97 for (size_t i = 0; i < apps.size(); ++i) {
98 const KioskAppManager::App& app_data = apps[i];
100 scoped_ptr<base::DictionaryValue> app_info(new base::DictionaryValue);
101 app_info->SetBoolean("isApp", true);
102 app_info->SetString("id", app_data.app_id);
103 app_info->SetString("label", app_data.name);
105 // TODO(xiyuan): Replace data url with a URLDataSource.
106 std::string icon_url("chrome://theme/IDR_APP_DEFAULT_ICON");
107 if (!app_data.icon.isNull())
108 icon_url = webui::GetBitmapDataUrl(*app_data.icon.bitmap());
109 app_info->SetString("iconUrl", icon_url);
111 apps_list.Append(app_info.release());
114 web_ui()->CallJavascriptFunction(EnableNewKioskUI() ?
115 kKioskSetAppsNewAPI : kKioskSetAppsOldAPI,
116 apps_list);
119 void KioskAppMenuHandler::HandleInitializeKioskApps(
120 const base::ListValue* args) {
121 is_webui_initialized_ = true;
122 SendKioskApps();
123 UpdateState(NetworkError::ERROR_REASON_UPDATE);
126 void KioskAppMenuHandler::HandleKioskAppsLoaded(
127 const base::ListValue* args) {
128 content::NotificationService::current()->Notify(
129 chrome::NOTIFICATION_KIOSK_APPS_LOADED,
130 content::NotificationService::AllSources(),
131 content::NotificationService::NoDetails());
134 void KioskAppMenuHandler::HandleCheckKioskAppLaunchError(
135 const base::ListValue* args) {
136 KioskAppLaunchError::Error error = KioskAppLaunchError::Get();
137 if (error == KioskAppLaunchError::NONE)
138 return;
139 KioskAppLaunchError::Clear();
141 const std::string error_message = KioskAppLaunchError::GetErrorMessage(error);
142 bool new_kiosk_ui = EnableNewKioskUI();
143 web_ui()->CallJavascriptFunction(new_kiosk_ui ?
144 kKioskShowErrorNewAPI : kKioskShowErrorOldAPI,
145 base::StringValue(error_message));
148 void KioskAppMenuHandler::OnKioskAppsSettingsChanged() {
149 SendKioskApps();
152 void KioskAppMenuHandler::OnKioskAppDataChanged(const std::string& app_id) {
153 SendKioskApps();
156 void KioskAppMenuHandler::UpdateState(NetworkError::ErrorReason reason) {
157 if (network_state_informer_->state() == NetworkStateInformer::ONLINE)
158 KioskAppManager::Get()->RetryFailedAppDataFetch();
161 } // namespace chromeos