Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / resources / chromeos / login / login_common.js
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 /**
6  * @fileoverview Common OOBE controller methods.
7  */
8
9 <include src="../../login/screen.js"></include>
10 <include src="../user_images_grid.js"></include>
11 <include src="apps_menu.js"></include>
12 <include src="../../login/bubble.js"></include>
13 <include src="../../login/display_manager.js"></include>
14 <include src="header_bar.js"></include>
15 <include src="network_dropdown.js"></include>
16 <include src="oobe_screen_reset.js"></include>
17 <include src="oobe_screen_autolaunch.js"></include>
18 <include src="oobe_screen_enable_kiosk.js"></include>
19 <include src="oobe_screen_terms_of_service.js"></include>
20 <include src="oobe_screen_user_image.js"></include>
21 <include src="../../login/screen_account_picker.js"></include>
22 <include src="screen_app_launch_splash.js"></include>
23 <include src="screen_error_message.js"></include>
24 <include src="screen_gaia_signin.js"></include>
25 <include src="screen_locally_managed_user_creation.js"></include>
26 <include src="screen_password_changed.js"></include>
27 <include src="screen_tpm_error.js"></include>
28 <include src="screen_wrong_hwid.js"></include>
29 <include src="screen_confirm_password.js"></include>
30 <include src="screen_fatal_error.js"></include>
31 <include src="../../login/user_pod_row.js"></include>
32 <include src="../../login/resource_loader.js"></include>
33
34 cr.define('cr.ui', function() {
35   var DisplayManager = cr.ui.login.DisplayManager;
36
37   /**
38   * Constructs an Out of box controller. It manages initialization of screens,
39   * transitions, error messages display.
40   * @extends {DisplayManager}
41   * @constructor
42   */
43   function Oobe() {
44   }
45
46   /**
47    * Delay in milliseconds between start of OOBE animation and start of
48    * header bar animation.
49    */
50   var HEADER_BAR_DELAY_MS = 300;
51
52   cr.addSingletonGetter(Oobe);
53
54   Oobe.prototype = {
55     __proto__: DisplayManager.prototype,
56   };
57
58   /**
59    * Handle accelerators. These are passed from native code instead of a JS
60    * event handler in order to make sure that embedded iframes cannot swallow
61    * them.
62    * @param {string} name Accelerator name.
63    */
64   Oobe.handleAccelerator = function(name) {
65     Oobe.getInstance().handleAccelerator(name);
66   };
67
68   /**
69    * Shows the given screen.
70    * @param {Object} screen Screen params dict, e.g. {id: screenId, data: data}
71    */
72   Oobe.showScreen = function(screen) {
73     Oobe.getInstance().showScreen(screen);
74   };
75
76   /**
77    * Updates version label visibilty.
78    * @param {boolean} show True if version label should be visible.
79    */
80   Oobe.showVersion = function(show) {
81     Oobe.getInstance().showVersion(show);
82   };
83
84   /**
85    * Update body class to switch between OOBE UI and Login UI.
86    */
87   Oobe.showOobeUI = function(showOobe) {
88     if (showOobe) {
89       document.body.classList.add('oobe-display');
90
91       // Callback to animate the header bar in.
92       var showHeaderBar = function() {
93         login.HeaderBar.animateIn(function() {
94           chrome.send('headerBarVisible');
95         });
96       };
97       // Start asynchronously so the OOBE network screen comes in first.
98       window.setTimeout(showHeaderBar, HEADER_BAR_DELAY_MS);
99     } else {
100       document.body.classList.remove('oobe-display');
101       Oobe.getInstance().prepareForLoginDisplay_();
102     }
103
104     Oobe.getInstance().headerHidden = false;
105   };
106
107   /**
108    * Enables keyboard driven flow.
109    */
110   Oobe.enableKeyboardFlow = function(value) {
111     // Don't show header bar for OOBE.
112     Oobe.getInstance().forceKeyboardFlow = value;
113   };
114
115   /**
116    * Disables signin UI.
117    */
118   Oobe.disableSigninUI = function() {
119     DisplayManager.disableSigninUI();
120   };
121
122   /**
123    * Shows signin UI.
124    * @param {string} opt_email An optional email for signin UI.
125    */
126   Oobe.showSigninUI = function(opt_email) {
127     DisplayManager.showSigninUI(opt_email);
128   };
129
130   /**
131    * Resets sign-in input fields.
132    * @param {boolean} forceOnline Whether online sign-in should be forced.
133    * If |forceOnline| is false previously used sign-in type will be used.
134    */
135   Oobe.resetSigninUI = function(forceOnline) {
136     DisplayManager.resetSigninUI(forceOnline);
137   };
138
139   /**
140    * Shows sign-in error bubble.
141    * @param {number} loginAttempts Number of login attemps tried.
142    * @param {string} message Error message to show.
143    * @param {string} link Text to use for help link.
144    * @param {number} helpId Help topic Id associated with help link.
145    */
146   Oobe.showSignInError = function(loginAttempts, message, link, helpId) {
147     DisplayManager.showSignInError(loginAttempts, message, link, helpId);
148   };
149
150   /**
151    * Shows password changed screen that offers migration.
152    * @param {boolean} showError Whether to show the incorrect password error.
153    */
154   Oobe.showPasswordChangedScreen = function(showError) {
155     DisplayManager.showPasswordChangedScreen(showError);
156   };
157
158   /**
159    * Shows dialog to create managed user.
160    */
161   Oobe.showManagedUserCreationScreen = function() {
162     DisplayManager.showManagedUserCreationScreen();
163   };
164
165   /**
166    * Shows TPM error screen.
167    */
168   Oobe.showTpmError = function() {
169     DisplayManager.showTpmError();
170   };
171
172   /**
173    * Clears error bubble as well as optional menus that could be open.
174    */
175   Oobe.clearErrors = function() {
176     var accessibilityMenu = $('accessibility-menu');
177     if (accessibilityMenu)
178       accessibilityMenu.hide();
179     DisplayManager.clearErrors();
180   };
181
182   /**
183    * Displays animations on successful authentication, that have to happen
184    * before login UI is dismissed.
185    */
186   Oobe.animateAuthenticationSuccess = function() {
187     login.HeaderBar.animateOut(function() {
188       chrome.send('unlockOnLoginSuccess');
189     });
190   };
191
192   /**
193    * Displays animations that have to happen once login UI is fully displayed.
194    */
195   Oobe.animateOnceFullyDisplayed = function() {
196     login.HeaderBar.animateIn();
197   };
198
199   /**
200    * Handles login success notification.
201    */
202   Oobe.onLoginSuccess = function(username) {
203     if (Oobe.getInstance().currentScreen.id == SCREEN_ACCOUNT_PICKER) {
204       // TODO(nkostylev): Enable animation back when session start jank
205       // is reduced. See http://crosbug.com/11116 http://crosbug.com/18307
206       // $('pod-row').startAuthenticatedAnimation();
207     }
208   };
209
210   /**
211    * Sets text content for a div with |labelId|.
212    * @param {string} labelId Id of the label div.
213    * @param {string} labelText Text for the label.
214    */
215   Oobe.setLabelText = function(labelId, labelText) {
216     DisplayManager.setLabelText(labelId, labelText);
217   };
218
219   /**
220    * Sets the text content of the enterprise info message.
221    * If the text is empty, the entire notification will be hidden.
222    * @param {string} messageText The message text.
223    */
224   Oobe.setEnterpriseInfo = function(messageText) {
225     DisplayManager.setEnterpriseInfo(messageText);
226   };
227
228   /**
229    * Updates the device requisition string shown in the requisition prompt.
230    * @param {string} requisition The device requisition.
231    */
232   Oobe.updateDeviceRequisition = function(requisition) {
233     Oobe.getInstance().updateDeviceRequisition(requisition);
234   };
235
236   /**
237    * Enforces focus on user pod of locked user.
238    */
239   Oobe.forceLockedUserPodFocus = function() {
240     login.AccountPickerScreen.forceLockedUserPodFocus();
241   };
242
243   /**
244    * Clears password field in user-pod.
245    */
246   Oobe.clearUserPodPassword = function() {
247     DisplayManager.clearUserPodPassword();
248   };
249
250   /**
251    * Restores input focus to currently selected pod.
252    */
253   Oobe.refocusCurrentPod = function() {
254     DisplayManager.refocusCurrentPod();
255   };
256
257   /**
258    * Skip to login screen for telemetry.
259    */
260   Oobe.skipToLoginForTesting = function() {
261     Oobe.disableSigninUI();
262     chrome.send('skipToLoginForTesting');
263   };
264
265   /**
266    * Login for telemetry.
267    * @param {string} username Login username.
268    * @param {string} password Login password.
269    */
270   Oobe.loginForTesting = function(username, password) {
271     Oobe.disableSigninUI();
272     chrome.send('skipToLoginForTesting', [username]);
273     chrome.send('completeLogin', [username, password, false]);
274   };
275
276   /**
277    * Guest login for telemetry.
278    */
279   Oobe.guestLoginForTesting = function() {
280     Oobe.skipToLoginForTesting();
281     chrome.send('launchIncognito');
282   };
283
284   /**
285    * Authenticate for telemetry - used for screenlocker.
286    * @param {string} username Login username.
287    * @param {string} password Login password.
288    */
289   Oobe.authenticateForTesting = function(username, password) {
290     Oobe.disableSigninUI();
291     chrome.send('authenticateUser', [username, password]);
292   };
293
294   /**
295    * Gaia login screen for telemetry.
296    */
297   Oobe.addUserForTesting = function() {
298     Oobe.skipToLoginForTesting();
299     chrome.send('addUser');
300   };
301
302   /**
303    * Hotrod requisition for telemetry.
304    */
305   Oobe.remoraRequisitionForTesting = function() {
306     chrome.send('setDeviceRequisition', ['remora']);
307   };
308
309   /**
310    * Finish enterprise enrollment for telemetry.
311    */
312   Oobe.enterpriseEnrollmentDone = function() {
313     chrome.send('oauthEnrollClose', ['done']);
314   };
315
316   /**
317    * Shows/hides login UI control bar with buttons like [Shut down].
318    */
319   Oobe.showControlBar = function(show) {
320     Oobe.getInstance().headerHidden = !show;
321   };
322
323   // Export
324   return {
325     Oobe: Oobe
326   };
327 });
328
329 var Oobe = cr.ui.Oobe;
330
331 // Allow selection events on components with editable text (password field)
332 // bug (http://code.google.com/p/chromium/issues/detail?id=125863)
333 disableTextSelectAndDrag(function(e) {
334   var src = e.target;
335   return src instanceof HTMLTextAreaElement ||
336          src instanceof HTMLInputElement &&
337          /text|password|search/.test(src.type);
338 });
339
340 // Register assets for async loading.
341 [{
342   id: SCREEN_OOBE_ENROLLMENT,
343   html: [{ url: 'chrome://oobe/enrollment.html', targetID: 'inner-container' }],
344   css: ['chrome://oobe/enrollment.css'],
345   js: ['chrome://oobe/enrollment.js']
346 }].forEach(cr.ui.login.ResourceLoader.registerAssets);
347
348 document.addEventListener('DOMContentLoaded', function() {
349   'use strict';
350
351   // Immediately load async assets.
352   // TODO(dconnelly): remove this at some point and only load as needed.
353   // See crbug.com/236426
354   cr.ui.login.ResourceLoader.loadAssets(SCREEN_OOBE_ENROLLMENT, function() {
355     // This screen is async-loaded so we manually trigger i18n processing.
356     i18nTemplate.process($('oauth-enrollment'), loadTimeData);
357     // Delayed binding since this isn't defined yet.
358     login.OAuthEnrollmentScreen.register();
359   });
360
361   // Delayed binding since this isn't defined yet.
362   cr.ui.Oobe.initialize();
363 });