kiosk: Skip network check for offline enabled app.
[chromium-blink-merge.git] / chrome / browser / ui / webui / chromeos / login / app_launch_splash_screen_handler.cc
blob1068d19f9cf271d3999093da0fd071e903b0a344
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/app_launch_splash_screen_handler.h"
7 #include "chrome/browser/chromeos/app_mode/kiosk_app_manager.h"
8 #include "chrome/browser/chromeos/login/screens/error_screen_actor.h"
9 #include "chrome/browser/ui/webui/chromeos/login/oobe_ui.h"
10 #include "chromeos/network/network_state.h"
11 #include "chromeos/network/network_state_handler.h"
12 #include "grit/browser_resources.h"
13 #include "grit/chrome_unscaled_resources.h"
14 #include "grit/chromium_strings.h"
15 #include "grit/generated_resources.h"
16 #include "ui/base/l10n/l10n_util.h"
17 #include "ui/base/resource/resource_bundle.h"
18 #include "ui/base/webui/web_ui_util.h"
20 namespace {
22 const char kJsScreenPath[] = "login.AppLaunchSplashScreen";
24 // Returns network name by service path.
25 std::string GetNetworkName(const std::string& service_path) {
26 const chromeos::NetworkState* network =
27 chromeos::NetworkHandler::Get()->network_state_handler()->GetNetworkState(
28 service_path);
29 if (!network)
30 return std::string();
31 return network->name();
34 } // namespace
36 namespace chromeos {
38 AppLaunchSplashScreenHandler::AppLaunchSplashScreenHandler(
39 const scoped_refptr<NetworkStateInformer>& network_state_informer,
40 ErrorScreenActor* error_screen_actor)
41 : BaseScreenHandler(kJsScreenPath),
42 delegate_(NULL),
43 show_on_init_(false),
44 state_(APP_LAUNCH_STATE_LOADING_AUTH_FILE),
45 network_state_informer_(network_state_informer),
46 error_screen_actor_(error_screen_actor) {
47 network_state_informer_->AddObserver(this);
50 AppLaunchSplashScreenHandler::~AppLaunchSplashScreenHandler() {
51 network_state_informer_->RemoveObserver(this);
54 void AppLaunchSplashScreenHandler::DeclareLocalizedValues(
55 LocalizedValuesBuilder* builder) {
57 builder->Add("appStartMessage", IDS_APP_START_NETWORK_WAIT_MESSAGE);
58 builder->Add("configureNetwork", IDS_APP_START_CONFIGURE_NETWORK);
60 const base::string16 product_os_name =
61 l10n_util::GetStringUTF16(IDS_SHORT_PRODUCT_OS_NAME);
62 builder->Add(
63 "shortcutInfo",
64 l10n_util::GetStringFUTF16(IDS_APP_START_BAILOUT_SHORTCUT_FORMAT,
65 product_os_name));
67 builder->Add(
68 "productName",
69 l10n_util::GetStringUTF16(IDS_SHORT_PRODUCT_OS_NAME));
72 void AppLaunchSplashScreenHandler::Initialize() {
73 if (show_on_init_) {
74 show_on_init_ = false;
75 Show(app_id_);
79 void AppLaunchSplashScreenHandler::Show(const std::string& app_id) {
80 app_id_ = app_id;
81 if (!page_is_ready()) {
82 show_on_init_ = true;
83 return;
86 base::DictionaryValue data;
87 data.SetBoolean("shortcutEnabled",
88 !KioskAppManager::Get()->GetDisableBailoutShortcut());
90 // |data| will take ownership of |app_info|.
91 base::DictionaryValue *app_info = new base::DictionaryValue();
92 PopulateAppInfo(app_info);
93 data.Set("appInfo", app_info);
95 SetLaunchText(l10n_util::GetStringUTF8(GetProgressMessageFromState(state_)));
96 ShowScreen(OobeUI::kScreenAppLaunchSplash, &data);
99 void AppLaunchSplashScreenHandler::RegisterMessages() {
100 AddCallback("configureNetwork",
101 &AppLaunchSplashScreenHandler::HandleConfigureNetwork);
102 AddCallback("cancelAppLaunch",
103 &AppLaunchSplashScreenHandler::HandleCancelAppLaunch);
106 void AppLaunchSplashScreenHandler::PrepareToShow() {
109 void AppLaunchSplashScreenHandler::Hide() {
112 void AppLaunchSplashScreenHandler::ToggleNetworkConfig(bool visible) {
113 CallJS("toggleNetworkConfig", visible);
116 void AppLaunchSplashScreenHandler::UpdateAppLaunchState(AppLaunchState state) {
117 if (state == state_)
118 return;
120 state_ = state;
121 if (page_is_ready()) {
122 SetLaunchText(
123 l10n_util::GetStringUTF8(GetProgressMessageFromState(state_)));
125 UpdateState(ErrorScreenActor::ERROR_REASON_UPDATE);
128 void AppLaunchSplashScreenHandler::SetDelegate(
129 AppLaunchSplashScreenHandler::Delegate* delegate) {
130 delegate_ = delegate;
133 void AppLaunchSplashScreenHandler::ShowNetworkConfigureUI() {
134 NetworkStateInformer::State state = network_state_informer_->state();
135 if (state == NetworkStateInformer::ONLINE) {
136 delegate_->OnNetworkStateChanged(true);
137 return;
140 const std::string network_path = network_state_informer_->network_path();
141 const std::string network_name = GetNetworkName(network_path);
143 error_screen_actor_->SetUIState(ErrorScreen::UI_STATE_KIOSK_MODE);
144 error_screen_actor_->AllowGuestSignin(false);
145 error_screen_actor_->AllowOfflineLogin(false);
147 switch (state) {
148 case NetworkStateInformer::CAPTIVE_PORTAL: {
149 error_screen_actor_->SetErrorState(
150 ErrorScreen::ERROR_STATE_PORTAL, network_name);
151 error_screen_actor_->FixCaptivePortal();
153 break;
155 case NetworkStateInformer::PROXY_AUTH_REQUIRED: {
156 error_screen_actor_->SetErrorState(
157 ErrorScreen::ERROR_STATE_PROXY, network_name);
158 break;
160 case NetworkStateInformer::OFFLINE: {
161 error_screen_actor_->SetErrorState(
162 ErrorScreen::ERROR_STATE_OFFLINE, network_name);
163 break;
165 default:
166 error_screen_actor_->SetErrorState(
167 ErrorScreen::ERROR_STATE_OFFLINE, network_name);
168 NOTREACHED();
169 break;
172 OobeUI::Screen screen = OobeUI::SCREEN_UNKNOWN;
173 OobeUI* oobe_ui = static_cast<OobeUI*>(web_ui()->GetController());
174 if (oobe_ui)
175 screen = oobe_ui->current_screen();
177 if (screen != OobeUI::SCREEN_ERROR_MESSAGE)
178 error_screen_actor_->Show(OobeDisplay::SCREEN_APP_LAUNCH_SPLASH, NULL);
181 bool AppLaunchSplashScreenHandler::IsNetworkReady() {
182 return network_state_informer_->state() == NetworkStateInformer::ONLINE;
185 void AppLaunchSplashScreenHandler::OnNetworkReady() {
186 // Purposely leave blank because the online case is handled in UpdateState
187 // call below.
190 void AppLaunchSplashScreenHandler::UpdateState(
191 ErrorScreenActor::ErrorReason reason) {
192 if (!delegate_ ||
193 (state_ != APP_LAUNCH_STATE_PREPARING_NETWORK &&
194 state_ != APP_LAUNCH_STATE_NETWORK_WAIT_TIMEOUT)) {
195 return;
198 NetworkStateInformer::State state = network_state_informer_->state();
199 delegate_->OnNetworkStateChanged(state == NetworkStateInformer::ONLINE);
202 void AppLaunchSplashScreenHandler::PopulateAppInfo(
203 base::DictionaryValue* out_info) {
204 KioskAppManager::App app;
205 KioskAppManager::Get()->GetApp(app_id_, &app);
207 if (app.name.empty())
208 app.name = l10n_util::GetStringUTF8(IDS_SHORT_PRODUCT_NAME);
210 if (app.icon.isNull()) {
211 app.icon = *ResourceBundle::GetSharedInstance().GetImageSkiaNamed(
212 IDR_PRODUCT_LOGO_128);
215 out_info->SetString("name", app.name);
216 out_info->SetString("iconURL", webui::GetBitmapDataUrl(*app.icon.bitmap()));
219 void AppLaunchSplashScreenHandler::SetLaunchText(const std::string& text) {
220 CallJS("updateMessage", text);
223 int AppLaunchSplashScreenHandler::GetProgressMessageFromState(
224 AppLaunchState state) {
225 switch (state) {
226 case APP_LAUNCH_STATE_LOADING_AUTH_FILE:
227 case APP_LAUNCH_STATE_LOADING_TOKEN_SERVICE:
228 // TODO(zelidrag): Add better string for this one than "Please wait..."
229 return IDS_SYNC_SETUP_SPINNER_TITLE;
230 case APP_LAUNCH_STATE_PREPARING_NETWORK:
231 return IDS_APP_START_NETWORK_WAIT_MESSAGE;
232 case APP_LAUNCH_STATE_INSTALLING_APPLICATION:
233 return IDS_APP_START_APP_WAIT_MESSAGE;
234 case APP_LAUNCH_STATE_WAITING_APP_WINDOW:
235 return IDS_APP_START_WAIT_FOR_APP_WINDOW_MESSAGE;
236 case APP_LAUNCH_STATE_NETWORK_WAIT_TIMEOUT:
237 return IDS_APP_START_NETWORK_WAIT_TIMEOUT_MESSAGE;
239 return IDS_APP_START_NETWORK_WAIT_MESSAGE;
242 void AppLaunchSplashScreenHandler::HandleConfigureNetwork() {
243 if (delegate_)
244 delegate_->OnConfigureNetwork();
245 else
246 LOG(WARNING) << "No delegate set to handle network configuration.";
249 void AppLaunchSplashScreenHandler::HandleCancelAppLaunch() {
250 if (delegate_)
251 delegate_->OnCancelAppLaunch();
252 else
253 LOG(WARNING) << "No delegate set to handle cancel app launch";
256 } // namespace chromeos