Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / ui / webui / chromeos / login / kiosk_app_menu_handler.cc
blob0ab33ed3b4da3710ef4b4e4b617e6488858f69ef
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/values.h"
10 #include "chrome/browser/browser_process.h"
11 #include "chrome/browser/chrome_notification_types.h"
12 #include "chrome/browser/chromeos/app_mode/kiosk_app_launch_error.h"
13 #include "chrome/browser/chromeos/login/existing_user_controller.h"
14 #include "chrome/browser/policy/browser_policy_connector.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 KioskAppMenuHandler::KioskAppMenuHandler()
26 : initialized_(false),
27 weak_ptr_factory_(this) {
28 KioskAppManager::Get()->AddObserver(this);
31 KioskAppMenuHandler::~KioskAppMenuHandler() {
32 KioskAppManager::Get()->RemoveObserver(this);
35 void KioskAppMenuHandler::GetLocalizedStrings(
36 base::DictionaryValue* localized_strings) {
37 localized_strings->SetString(
38 "showApps",
39 l10n_util::GetStringUTF16(IDS_KIOSK_APPS_BUTTON));
42 void KioskAppMenuHandler::RegisterMessages() {
43 web_ui()->RegisterMessageCallback("initializeKioskApps",
44 base::Bind(&KioskAppMenuHandler::HandleInitializeKioskApps,
45 base::Unretained(this)));
46 web_ui()->RegisterMessageCallback("kioskAppsLoaded",
47 base::Bind(&KioskAppMenuHandler::HandleKioskAppsLoaded,
48 base::Unretained(this)));
49 web_ui()->RegisterMessageCallback("checkKioskAppLaunchError",
50 base::Bind(&KioskAppMenuHandler::HandleCheckKioskAppLaunchError,
51 base::Unretained(this)));
54 void KioskAppMenuHandler::SendKioskApps() {
55 if (!initialized_)
56 return;
58 KioskAppManager::Apps apps;
59 KioskAppManager::Get()->GetApps(&apps);
61 base::ListValue apps_list;
62 for (size_t i = 0; i < apps.size(); ++i) {
63 const KioskAppManager::App& app_data = apps[i];
65 scoped_ptr<base::DictionaryValue> app_info(new base::DictionaryValue);
66 app_info->SetString("id", app_data.app_id);
67 app_info->SetString("label", app_data.name);
69 // TODO(xiyuan): Replace data url with a URLDataSource.
70 std::string icon_url("chrome://theme/IDR_APP_DEFAULT_ICON");
71 if (!app_data.icon.isNull())
72 icon_url = webui::GetBitmapDataUrl(*app_data.icon.bitmap());
73 app_info->SetString("iconUrl", icon_url);
75 apps_list.Append(app_info.release());
78 web_ui()->CallJavascriptFunction("login.AppsMenuButton.setApps",
79 apps_list);
82 void KioskAppMenuHandler::HandleInitializeKioskApps(
83 const base::ListValue* args) {
84 if (g_browser_process->browser_policy_connector()->IsEnterpriseManaged()) {
85 initialized_ = true;
86 SendKioskApps();
87 return;
90 KioskAppManager::Get()->GetConsumerKioskModeStatus(
91 base::Bind(&KioskAppMenuHandler::OnGetConsumerKioskModeStatus,
92 weak_ptr_factory_.GetWeakPtr()));
95 void KioskAppMenuHandler::OnGetConsumerKioskModeStatus(
96 KioskAppManager::ConsumerKioskModeStatus status) {
97 initialized_ =
98 status == KioskAppManager::CONSUMER_KIOSK_MODE_ENABLED;
99 SendKioskApps();
102 void KioskAppMenuHandler::HandleKioskAppsLoaded(
103 const base::ListValue* args) {
104 content::NotificationService::current()->Notify(
105 chrome::NOTIFICATION_KIOSK_APPS_LOADED,
106 content::NotificationService::AllSources(),
107 content::NotificationService::NoDetails());
110 void KioskAppMenuHandler::HandleCheckKioskAppLaunchError(
111 const base::ListValue* args) {
112 KioskAppLaunchError::Error error = KioskAppLaunchError::Get();
113 if (error == KioskAppLaunchError::NONE)
114 return;
115 KioskAppLaunchError::Clear();
117 const std::string error_message = KioskAppLaunchError::GetErrorMessage(error);
118 web_ui()->CallJavascriptFunction("login.AppsMenuButton.showError",
119 base::StringValue(error_message));
122 void KioskAppMenuHandler::OnKioskAppsSettingsChanged() {
123 SendKioskApps();
126 void KioskAppMenuHandler::OnKioskAppDataChanged(const std::string& app_id) {
127 SendKioskApps();
130 } // namespace chromeos