| OLD | NEW |
| (Empty) |
| 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/options/chromeos/kiosk_apps_handler.h" | |
| 6 | |
| 7 #include <algorithm> | |
| 8 #include <set> | |
| 9 #include <string> | |
| 10 #include <vector> | |
| 11 | |
| 12 #include "base/bind.h" | |
| 13 #include "base/lazy_instance.h" | |
| 14 #include "base/memory/scoped_ptr.h" | |
| 15 #include "base/observer_list.h" | |
| 16 #include "base/string_util.h" | |
| 17 #include "base/values.h" | |
| 18 #include "chrome/browser/chromeos/app_mode/kiosk_app_manager.h" | |
| 19 #include "chrome/common/extensions/extension.h" | |
| 20 #include "content/public/browser/browser_thread.h" | |
| 21 #include "content/public/browser/web_ui.h" | |
| 22 #include "googleurl/src/gurl.h" | |
| 23 #include "grit/chromium_strings.h" | |
| 24 #include "grit/generated_resources.h" | |
| 25 #include "ui/base/l10n/l10n_util.h" | |
| 26 #include "ui/webui/web_ui_util.h" | |
| 27 | |
| 28 namespace chromeos { | |
| 29 namespace options { | |
| 30 | |
| 31 namespace { | |
| 32 | |
| 33 // Populates app info dictionary with |app_data|. | |
| 34 void PopulateAppDict(const KioskAppManager::App& app_data, | |
| 35 base::DictionaryValue* app_dict) { | |
| 36 std::string icon_url("chrome://theme/IDR_APP_DEFAULT_ICON"); | |
| 37 | |
| 38 // TODO(xiyuan): Replace data url with a URLDataSource. | |
| 39 if (!app_data.icon.isNull()) | |
| 40 icon_url = webui::GetBitmapDataUrl(*app_data.icon.bitmap()); | |
| 41 | |
| 42 app_dict->SetString("id", app_data.app_id); | |
| 43 app_dict->SetString("name", app_data.name); | |
| 44 app_dict->SetString("iconURL", icon_url); | |
| 45 app_dict->SetBoolean( | |
| 46 "autoLaunch", | |
| 47 KioskAppManager::Get()->GetAutoLaunchApp() == app_data.app_id); | |
| 48 app_dict->SetBoolean("isLoading", app_data.is_loading); | |
| 49 } | |
| 50 | |
| 51 // Sanitize app id input value and extracts app id out of it. | |
| 52 // Returns false if an app id could not be derived out of the input. | |
| 53 bool ExtractsAppIdFromInput(const std::string& input, | |
| 54 std::string* app_id) { | |
| 55 if (extensions::Extension::IdIsValid(input)) { | |
| 56 *app_id = input; | |
| 57 return true; | |
| 58 } | |
| 59 | |
| 60 GURL webstore_url = GURL(input); | |
| 61 if (!webstore_url.is_valid()) | |
| 62 return false; | |
| 63 | |
| 64 GURL webstore_base_url = | |
| 65 GURL(extension_urls::GetWebstoreItemDetailURLPrefix()); | |
| 66 | |
| 67 if (webstore_url.scheme() != webstore_base_url.scheme() || | |
| 68 webstore_url.host() != webstore_base_url.host() || | |
| 69 !StartsWithASCII( | |
| 70 webstore_url.path(), webstore_base_url.path(), true)) { | |
| 71 return false; | |
| 72 } | |
| 73 | |
| 74 const std::string path = webstore_url.path(); | |
| 75 const size_t last_slash = path.rfind('/'); | |
| 76 if (last_slash == std::string::npos) | |
| 77 return false; | |
| 78 | |
| 79 const std::string candidate_id = path.substr(last_slash + 1); | |
| 80 if (!extensions::Extension::IdIsValid(candidate_id)) | |
| 81 return false; | |
| 82 | |
| 83 *app_id = candidate_id; | |
| 84 return true; | |
| 85 } | |
| 86 | |
| 87 } // namespace | |
| 88 | |
| 89 KioskAppsHandler::KioskAppsHandler() | |
| 90 : kiosk_app_manager_(KioskAppManager::Get()), | |
| 91 initialized_(false) { | |
| 92 kiosk_app_manager_->AddObserver(this); | |
| 93 } | |
| 94 | |
| 95 KioskAppsHandler::~KioskAppsHandler() { | |
| 96 kiosk_app_manager_->RemoveObserver(this); | |
| 97 } | |
| 98 | |
| 99 void KioskAppsHandler::RegisterMessages() { | |
| 100 web_ui()->RegisterMessageCallback("getKioskApps", | |
| 101 base::Bind(&KioskAppsHandler::HandleGetKioskApps, | |
| 102 base::Unretained(this))); | |
| 103 web_ui()->RegisterMessageCallback("addKioskApp", | |
| 104 base::Bind(&KioskAppsHandler::HandleAddKioskApp, | |
| 105 base::Unretained(this))); | |
| 106 web_ui()->RegisterMessageCallback("removeKioskApp", | |
| 107 base::Bind(&KioskAppsHandler::HandleRemoveKioskApp, | |
| 108 base::Unretained(this))); | |
| 109 web_ui()->RegisterMessageCallback("enableKioskAutoLaunch", | |
| 110 base::Bind(&KioskAppsHandler::HandleEnableKioskAutoLaunch, | |
| 111 base::Unretained(this))); | |
| 112 web_ui()->RegisterMessageCallback("disableKioskAutoLaunch", | |
| 113 base::Bind(&KioskAppsHandler::HandleDisableKioskAutoLaunch, | |
| 114 base::Unretained(this))); | |
| 115 } | |
| 116 | |
| 117 void KioskAppsHandler::GetLocalizedValues( | |
| 118 base::DictionaryValue* localized_strings) { | |
| 119 DCHECK(localized_strings); | |
| 120 | |
| 121 RegisterTitle(localized_strings, | |
| 122 "kioskOverlayTitle", | |
| 123 IDS_OPTIONS_KIOSK_OVERLAY_TITLE); | |
| 124 | |
| 125 localized_strings->SetString( | |
| 126 "addKioskApp", | |
| 127 l10n_util::GetStringUTF16(IDS_OPTIONS_KIOSK_ADD_APP)); | |
| 128 localized_strings->SetString( | |
| 129 "kioskAppIdEditHint", | |
| 130 l10n_util::GetStringUTF16(IDS_OPTIONS_KIOSK_ADD_APP_HINT)); | |
| 131 localized_strings->SetString( | |
| 132 "enableAutoLaunchButton", | |
| 133 l10n_util::GetStringUTF16(IDS_OPTIONS_KIOSK_ENABLE_AUTO_LAUNCH)); | |
| 134 localized_strings->SetString( | |
| 135 "disableAutoLaunchButton", | |
| 136 l10n_util::GetStringUTF16(IDS_OPTIONS_KIOSK_DISABLE_AUTO_LAUNCH)); | |
| 137 localized_strings->SetString( | |
| 138 "autoLaunch", | |
| 139 l10n_util::GetStringUTF16(IDS_OPTIONS_KIOSK_AUTO_LAUNCH)); | |
| 140 localized_strings->SetString( | |
| 141 "invalidApp", | |
| 142 l10n_util::GetStringUTF16(IDS_OPTIONS_KIOSK_INVALID_APP)); | |
| 143 localized_strings->SetString( | |
| 144 "kioskDiableBailoutShortcutLabel", | |
| 145 l10n_util::GetStringUTF16( | |
| 146 IDS_OPTIONS_KIOSK_DISABLE_BAILOUT_SHORTCUT_LABEL)); | |
| 147 localized_strings->SetString( | |
| 148 "kioskDisableBailoutShortcutWarningBold", | |
| 149 l10n_util::GetStringUTF16( | |
| 150 IDS_OPTIONS_KIOSK_DISABLE_BAILOUT_SHORTCUT_WARNING_BOLD)); | |
| 151 const string16 product_os_name = | |
| 152 l10n_util::GetStringUTF16(IDS_SHORT_PRODUCT_OS_NAME); | |
| 153 localized_strings->SetString( | |
| 154 "kioskDisableBailoutShortcutWarning", | |
| 155 l10n_util::GetStringFUTF16( | |
| 156 IDS_OPTIONS_KIOSK_DISABLE_BAILOUT_SHORTCUT_WARNING_FORMAT, | |
| 157 product_os_name)); | |
| 158 localized_strings->SetString( | |
| 159 "kioskDisableBailoutShortcutConfirm", | |
| 160 l10n_util::GetStringUTF16(IDS_CONFIRM_MESSAGEBOX_YES_BUTTON_LABEL)); | |
| 161 localized_strings->SetString( | |
| 162 "kioskDisableBailoutShortcutCancel", | |
| 163 l10n_util::GetStringUTF16(IDS_CONFIRM_MESSAGEBOX_NO_BUTTON_LABEL)); | |
| 164 } | |
| 165 | |
| 166 void KioskAppsHandler::OnKioskAppDataChanged(const std::string& app_id) { | |
| 167 KioskAppManager::App app_data; | |
| 168 if (!kiosk_app_manager_->GetApp(app_id, &app_data)) | |
| 169 return; | |
| 170 | |
| 171 base::DictionaryValue app_dict; | |
| 172 PopulateAppDict(app_data, &app_dict); | |
| 173 | |
| 174 web_ui()->CallJavascriptFunction("options.KioskAppsOverlay.updateApp", | |
| 175 app_dict); | |
| 176 } | |
| 177 | |
| 178 void KioskAppsHandler::OnKioskAppDataLoadFailure(const std::string& app_id) { | |
| 179 base::StringValue app_id_value(app_id); | |
| 180 web_ui()->CallJavascriptFunction("options.KioskAppsOverlay.showError", | |
| 181 app_id_value); | |
| 182 } | |
| 183 | |
| 184 void KioskAppsHandler::SendKioskApps() { | |
| 185 if (!initialized_) | |
| 186 return; | |
| 187 | |
| 188 KioskAppManager::Apps apps; | |
| 189 kiosk_app_manager_->GetApps(&apps); | |
| 190 | |
| 191 base::ListValue apps_list; | |
| 192 for (size_t i = 0; i < apps.size(); ++i) { | |
| 193 const KioskAppManager::App& app_data = apps[i]; | |
| 194 | |
| 195 scoped_ptr<base::DictionaryValue> app_info(new base::DictionaryValue); | |
| 196 PopulateAppDict(app_data, app_info.get()); | |
| 197 apps_list.Append(app_info.release()); | |
| 198 } | |
| 199 | |
| 200 web_ui()->CallJavascriptFunction("options.KioskAppsOverlay.setApps", | |
| 201 apps_list); | |
| 202 } | |
| 203 | |
| 204 void KioskAppsHandler::HandleGetKioskApps(const base::ListValue* args) { | |
| 205 initialized_ = true; | |
| 206 SendKioskApps(); | |
| 207 } | |
| 208 | |
| 209 void KioskAppsHandler::HandleAddKioskApp(const base::ListValue* args) { | |
| 210 std::string input; | |
| 211 CHECK(args->GetString(0, &input)); | |
| 212 | |
| 213 std::string app_id; | |
| 214 if (!ExtractsAppIdFromInput(input, &app_id)) { | |
| 215 OnKioskAppDataLoadFailure(input); | |
| 216 return; | |
| 217 } | |
| 218 | |
| 219 kiosk_app_manager_->AddApp(app_id); | |
| 220 } | |
| 221 | |
| 222 void KioskAppsHandler::HandleRemoveKioskApp(const base::ListValue* args) { | |
| 223 std::string app_id; | |
| 224 CHECK(args->GetString(0, &app_id)); | |
| 225 | |
| 226 kiosk_app_manager_->RemoveApp(app_id); | |
| 227 } | |
| 228 | |
| 229 void KioskAppsHandler::HandleEnableKioskAutoLaunch( | |
| 230 const base::ListValue* args) { | |
| 231 std::string app_id; | |
| 232 CHECK(args->GetString(0, &app_id)); | |
| 233 | |
| 234 kiosk_app_manager_->SetAutoLaunchApp(app_id); | |
| 235 } | |
| 236 | |
| 237 void KioskAppsHandler::HandleDisableKioskAutoLaunch( | |
| 238 const base::ListValue* args) { | |
| 239 std::string app_id; | |
| 240 CHECK(args->GetString(0, &app_id)); | |
| 241 | |
| 242 std::string startup_app_id = kiosk_app_manager_->GetAutoLaunchApp(); | |
| 243 if (startup_app_id != app_id) | |
| 244 return; | |
| 245 | |
| 246 kiosk_app_manager_->SetAutoLaunchApp(""); | |
| 247 } | |
| 248 | |
| 249 } // namespace options | |
| 250 } // namespace chromeos | |
| OLD | NEW |