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