- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / webui / chromeos / login / app_launch_splash_screen_handler.cc
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/chromeos/login/app_launch_splash_screen_handler.h"
6
7 #include "chrome/browser/chromeos/app_mode/kiosk_app_manager.h"
8 #include "chrome/browser/ui/webui/chromeos/login/oobe_ui.h"
9 #include "grit/browser_resources.h"
10 #include "grit/chrome_unscaled_resources.h"
11 #include "grit/chromium_strings.h"
12 #include "grit/generated_resources.h"
13 #include "ui/base/l10n/l10n_util.h"
14 #include "ui/base/resource/resource_bundle.h"
15 #include "ui/base/webui/web_ui_util.h"
16
17 namespace {
18
19 const char kJsScreenPath[] = "login.AppLaunchSplashScreen";
20
21 }  // namespace
22
23 namespace chromeos {
24
25 AppLaunchSplashScreenHandler::AppLaunchSplashScreenHandler()
26     : BaseScreenHandler(kJsScreenPath),
27       delegate_(NULL),
28       show_on_init_(false),
29       state_(APP_LAUNCH_STATE_LOADING_AUTH_FILE) {
30 }
31
32 AppLaunchSplashScreenHandler::~AppLaunchSplashScreenHandler() {
33 }
34
35 void AppLaunchSplashScreenHandler::DeclareLocalizedValues(
36     LocalizedValuesBuilder* builder) {
37
38   builder->Add("appStartMessage", IDS_APP_START_NETWORK_WAIT_MESSAGE);
39   builder->Add("configureNetwork", IDS_APP_START_CONFIGURE_NETWORK);
40
41   const string16 product_os_name =
42       l10n_util::GetStringUTF16(IDS_SHORT_PRODUCT_OS_NAME);
43   builder->Add(
44       "shortcutInfo",
45       l10n_util::GetStringFUTF16(IDS_APP_START_BAILOUT_SHORTCUT_FORMAT,
46                                  product_os_name));
47
48   builder->Add(
49       "productName",
50       l10n_util::GetStringUTF16(IDS_SHORT_PRODUCT_OS_NAME));
51 }
52
53 void AppLaunchSplashScreenHandler::Initialize() {
54   if (show_on_init_) {
55     show_on_init_ = false;
56     Show(app_id_);
57   }
58 }
59
60 void AppLaunchSplashScreenHandler::Show(const std::string& app_id) {
61   app_id_ = app_id;
62   if (!page_is_ready()) {
63     show_on_init_ = true;
64     return;
65   }
66
67   base::DictionaryValue data;
68   data.SetBoolean("shortcutEnabled",
69                   !KioskAppManager::Get()->GetDisableBailoutShortcut());
70
71   // |data| will take ownership of |app_info|.
72   base::DictionaryValue *app_info = new base::DictionaryValue();
73   PopulateAppInfo(app_info);
74   data.Set("appInfo", app_info);
75
76   SetLaunchText(l10n_util::GetStringUTF8(GetProgressMessageFromState(state_)));
77   ShowScreen(OobeUI::kScreenAppLaunchSplash, &data);
78 }
79
80 void AppLaunchSplashScreenHandler::RegisterMessages() {
81   AddCallback("configureNetwork",
82               &AppLaunchSplashScreenHandler::HandleConfigureNetwork);
83   AddCallback("cancelAppLaunch",
84               &AppLaunchSplashScreenHandler::HandleCancelAppLaunch);
85 }
86
87 void AppLaunchSplashScreenHandler::PrepareToShow() {
88 }
89
90 void AppLaunchSplashScreenHandler::Hide() {
91 }
92
93 void AppLaunchSplashScreenHandler::ToggleNetworkConfig(bool visible) {
94   CallJS("toggleNetworkConfig", visible);
95 }
96
97 void AppLaunchSplashScreenHandler::UpdateAppLaunchState(AppLaunchState state) {
98   if (state == state_)
99     return;
100
101   state_ = state;
102   if (page_is_ready()) {
103     SetLaunchText(
104         l10n_util::GetStringUTF8(GetProgressMessageFromState(state_)));
105   }
106 }
107
108 void AppLaunchSplashScreenHandler::SetDelegate(
109     AppLaunchSplashScreenHandler::Delegate* delegate) {
110   delegate_ = delegate;
111 }
112
113 void AppLaunchSplashScreenHandler::PopulateAppInfo(
114     base::DictionaryValue* out_info) {
115   KioskAppManager::App app;
116   KioskAppManager::Get()->GetApp(app_id_, &app);
117
118   if (app.name.empty())
119     app.name = l10n_util::GetStringUTF8(IDS_SHORT_PRODUCT_NAME);
120
121   if (app.icon.isNull()) {
122     app.icon = *ResourceBundle::GetSharedInstance().GetImageSkiaNamed(
123         IDR_PRODUCT_LOGO_128);
124   }
125
126   out_info->SetString("name", app.name);
127   out_info->SetString("iconURL", webui::GetBitmapDataUrl(*app.icon.bitmap()));
128 }
129
130 void AppLaunchSplashScreenHandler::SetLaunchText(const std::string& text) {
131   CallJS("updateMessage", text);
132 }
133
134 int AppLaunchSplashScreenHandler::GetProgressMessageFromState(
135     AppLaunchState state) {
136   switch (state) {
137     case APP_LAUNCH_STATE_LOADING_AUTH_FILE:
138     case APP_LAUNCH_STATE_LOADING_TOKEN_SERVICE:
139       // TODO(zelidrag): Add better string for this one than "Please wait..."
140       return IDS_SYNC_SETUP_SPINNER_TITLE;
141     case APP_LAUNCH_STATE_PREPARING_NETWORK:
142       return IDS_APP_START_NETWORK_WAIT_MESSAGE;
143     case APP_LAUNCH_STATE_INSTALLING_APPLICATION:
144       return IDS_APP_START_APP_WAIT_MESSAGE;
145     case APP_LAUNCH_STATE_WAITING_APP_WINDOW:
146       return IDS_APP_START_WAIT_FOR_APP_WINDOW_MESSAGE;
147     case APP_LAUNCH_STATE_NETWORK_WAIT_TIMEOUT:
148       return IDS_APP_START_NETWORK_WAIT_TIMEOUT_MESSAGE;
149   }
150   return IDS_APP_START_NETWORK_WAIT_MESSAGE;
151 }
152
153 void AppLaunchSplashScreenHandler::HandleConfigureNetwork() {
154   if (delegate_)
155     delegate_->OnConfigureNetwork();
156   else
157     LOG(WARNING) << "No delegate set to handle network configuration.";
158 }
159
160 void AppLaunchSplashScreenHandler::HandleCancelAppLaunch() {
161   if (delegate_)
162     delegate_->OnCancelAppLaunch();
163   else
164     LOG(WARNING) << "No delegate set to handle cancel app launch";
165 }
166
167 }  // namespace chromeos