Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / resources / chromeos / login / login_common.js
blobe9cca699535237ab29f3e56573149e2ab5b17299
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="test_util.js">
10 <include src="../../../../../ui/login/screen.js">
11 <include src="screen_context.js">
12 <include src="../user_images_grid.js">
13 <include src="apps_menu.js">
14 <include src="../../../../../ui/login/bubble.js">
15 <include src="../../../../../ui/login/display_manager.js">
16 <include src="header_bar.js">
17 <include src="network_dropdown.js">
18 <include src="oobe_screen_reset_confirmation_overlay.js">
19 <include src="oobe_screen_reset.js">
20 <include src="oobe_screen_autolaunch.js">
21 <include src="oobe_screen_enable_kiosk.js">
22 <include src="oobe_screen_terms_of_service.js">
23 <include src="oobe_screen_user_image.js">
24 <include src="../../../../../ui/login/account_picker/screen_account_picker.js">
25 <include src="screen_app_launch_splash.js">
26 <include src="screen_error_message.js">
27 <include src="screen_gaia_signin.js">
28 <include src="screen_password_changed.js">
29 <include src="screen_supervised_user_creation.js">
30 <include src="screen_tpm_error.js">
31 <include src="screen_wrong_hwid.js">
32 <include src="screen_confirm_password.js">
33 <include src="screen_fatal_error.js">
34 <include src="screen_device_disabled.js">
35 <include src="../../../../../ui/login/login_ui_tools.js">
36 <include src="../../../../../ui/login/account_picker/user_pod_row.js">
37 <include src="../../../../../ui/login/resource_loader.js">
39 cr.define('cr.ui', function() {
40   var DisplayManager = cr.ui.login.DisplayManager;
42   /**
43   * Constructs an Out of box controller. It manages initialization of screens,
44   * transitions, error messages display.
45   * @extends {DisplayManager}
46   * @constructor
47   */
48   function Oobe() {
49   }
51   /**
52    * Delay in milliseconds between start of OOBE animation and start of
53    * header bar animation.
54    */
55   var HEADER_BAR_DELAY_MS = 300;
57   cr.addSingletonGetter(Oobe);
59   Oobe.prototype = {
60     __proto__: DisplayManager.prototype,
61   };
63   /**
64    * Handle accelerators. These are passed from native code instead of a JS
65    * event handler in order to make sure that embedded iframes cannot swallow
66    * them.
67    * @param {string} name Accelerator name.
68    */
69   Oobe.handleAccelerator = function(name) {
70     Oobe.getInstance().handleAccelerator(name);
71   };
73   /**
74    * Shows the given screen.
75    * @param {Object} screen Screen params dict, e.g. {id: screenId, data: data}
76    */
77   Oobe.showScreen = function(screen) {
78     Oobe.getInstance().showScreen(screen);
79   };
81   /**
82    * Updates version label visibility.
83    * @param {boolean} show True if version label should be visible.
84    */
85   Oobe.showVersion = function(show) {
86     Oobe.getInstance().showVersion(show);
87   };
89   /**
90    * Update body class to switch between OOBE UI and Login UI.
91    */
92   Oobe.showOobeUI = function(showOobe) {
93     if (showOobe) {
94       document.body.classList.add('oobe-display');
96       // Callback to animate the header bar in.
97       var showHeaderBar = function() {
98         login.HeaderBar.animateIn(false, function() {
99           chrome.send('headerBarVisible');
100         });
101       };
102       // Start asynchronously so the OOBE network screen comes in first.
103       window.setTimeout(showHeaderBar, HEADER_BAR_DELAY_MS);
104     } else {
105       document.body.classList.remove('oobe-display');
106       Oobe.getInstance().prepareForLoginDisplay_();
107       // Ensure header bar is visible when switching to Login UI from oobe.
108       if (Oobe.getInstance().displayType == DISPLAY_TYPE.OOBE)
109         login.HeaderBar.animateIn(true);
110     }
112     Oobe.getInstance().headerHidden = false;
113   };
115   /**
116    * When |showShutdown| is set to "true", the shutdown button is shown and the
117    * reboot button hidden. If set to "false", the reboot button is visible and
118    * the shutdown button hidden.
119    */
120   Oobe.showShutdown = function(showShutdown) {
121     $('login-header-bar').showShutdownButton = showShutdown;
122     $('login-header-bar').showRebootButton = !showShutdown;
123   };
125   /**
126    * Enables keyboard driven flow.
127    */
128   Oobe.enableKeyboardFlow = function(value) {
129     // Don't show header bar for OOBE.
130     Oobe.getInstance().forceKeyboardFlow = value;
131   };
133   /**
134    * Disables signin UI.
135    */
136   Oobe.disableSigninUI = function() {
137     DisplayManager.disableSigninUI();
138   };
140   /**
141    * Shows signin UI.
142    * @param {string} opt_email An optional email for signin UI.
143    */
144   Oobe.showSigninUI = function(opt_email) {
145     DisplayManager.showSigninUI(opt_email);
146   };
148   /**
149    * Resets sign-in input fields.
150    * @param {boolean} forceOnline Whether online sign-in should be forced.
151    * If |forceOnline| is false previously used sign-in type will be used.
152    */
153   Oobe.resetSigninUI = function(forceOnline) {
154     DisplayManager.resetSigninUI(forceOnline);
155   };
157   /**
158    * Shows sign-in error bubble.
159    * @param {number} loginAttempts Number of login attemps tried.
160    * @param {string} message Error message to show.
161    * @param {string} link Text to use for help link.
162    * @param {number} helpId Help topic Id associated with help link.
163    */
164   Oobe.showSignInError = function(loginAttempts, message, link, helpId) {
165     DisplayManager.showSignInError(loginAttempts, message, link, helpId);
166   };
168   /**
169    * Shows password changed screen that offers migration.
170    * @param {boolean} showError Whether to show the incorrect password error.
171    */
172   Oobe.showPasswordChangedScreen = function(showError, email) {
173     DisplayManager.showPasswordChangedScreen(showError, email);
174   };
176   /**
177    * Shows dialog to create a supervised user.
178    */
179   Oobe.showSupervisedUserCreationScreen = function() {
180     DisplayManager.showSupervisedUserCreationScreen();
181   };
183   /**
184    * Shows TPM error screen.
185    */
186   Oobe.showTpmError = function() {
187     DisplayManager.showTpmError();
188   };
190   /**
191    * Show user-pods.
192    */
193   Oobe.showUserPods = function() {
194     $('pod-row').loadLastWallpaper();
195     Oobe.showScreen({id: SCREEN_ACCOUNT_PICKER});
196     Oobe.resetSigninUI(true);
197   };
199   /**
200    * Clears error bubble as well as optional menus that could be open.
201    */
202   Oobe.clearErrors = function() {
203     var accessibilityMenu = $('accessibility-menu');
204     if (accessibilityMenu)
205       accessibilityMenu.hide();
206     DisplayManager.clearErrors();
207   };
209   /**
210    * Displays animations on successful authentication, that have to happen
211    * before login UI is dismissed.
212    */
213   Oobe.animateAuthenticationSuccess = function() {
214     login.HeaderBar.animateOut(function() {
215       chrome.send('unlockOnLoginSuccess');
216     });
217   };
219   /**
220    * Displays animations that have to happen once login UI is fully displayed.
221    */
222   Oobe.animateOnceFullyDisplayed = function() {
223     login.HeaderBar.animateIn(true, function() {
224       chrome.send('headerBarVisible');
225     });
226   };
228   /**
229    * Sets text content for a div with |labelId|.
230    * @param {string} labelId Id of the label div.
231    * @param {string} labelText Text for the label.
232    */
233   Oobe.setLabelText = function(labelId, labelText) {
234     DisplayManager.setLabelText(labelId, labelText);
235   };
237   /**
238    * Sets the text content of the enterprise info message.
239    * If the text is empty, the entire notification will be hidden.
240    * @param {string} messageText The message text.
241    */
242   Oobe.setEnterpriseInfo = function(messageText, assetId) {
243     DisplayManager.setEnterpriseInfo(messageText, assetId);
244   };
246   /**
247    * Updates the device requisition string shown in the requisition prompt.
248    * @param {string} requisition The device requisition.
249    */
250   Oobe.updateDeviceRequisition = function(requisition) {
251     Oobe.getInstance().updateDeviceRequisition(requisition);
252   };
254   /**
255    * Enforces focus on user pod of locked user.
256    */
257   Oobe.forceLockedUserPodFocus = function() {
258     login.AccountPickerScreen.forceLockedUserPodFocus();
259   };
261   /**
262    * Clears password field in user-pod.
263    */
264   Oobe.clearUserPodPassword = function() {
265     DisplayManager.clearUserPodPassword();
266   };
268   /**
269    * Restores input focus to currently selected pod.
270    */
271   Oobe.refocusCurrentPod = function() {
272     DisplayManager.refocusCurrentPod();
273   };
275   /**
276    * Skip to login screen for telemetry.
277    */
278   Oobe.skipToLoginForTesting = function() {
279     Oobe.disableSigninUI();
280     chrome.send('skipToLoginForTesting');
281   };
283   /**
284    * Login for telemetry.
285    * @param {string} username Login username.
286    * @param {string} password Login password.
287    */
288   Oobe.loginForTesting = function(username, password) {
289     Oobe.disableSigninUI();
290     chrome.send('skipToLoginForTesting', [username]);
291     chrome.send('completeLogin', ['12345', username, password, false]);
292   };
294   /**
295    * Guest login for telemetry.
296    */
297   Oobe.guestLoginForTesting = function() {
298     Oobe.skipToLoginForTesting();
299     chrome.send('launchIncognito');
300   };
302   /**
303    * Authenticate for telemetry - used for screenlocker.
304    * @param {string} username Login username.
305    * @param {string} password Login password.
306    */
307   Oobe.authenticateForTesting = function(username, password) {
308     Oobe.disableSigninUI();
309     chrome.send('authenticateUser', [username, password]);
310   };
312   /**
313    * Gaia login screen for telemetry.
314    */
315   Oobe.addUserForTesting = function() {
316     Oobe.skipToLoginForTesting();
317     chrome.send('addUser');
318   };
320   /**
321    * Shows the add user dialog. Used in browser tests.
322    */
323   Oobe.showAddUserForTesting = function() {
324     chrome.send('showAddUser');
325   };
327   /**
328    * Hotrod requisition for telemetry.
329    */
330   Oobe.remoraRequisitionForTesting = function() {
331     chrome.send('setDeviceRequisition', ['remora']);
332   };
334   /**
335    * Begin enterprise enrollment for telemetry.
336    */
337   Oobe.switchToEnterpriseEnrollmentForTesting = function() {
338     chrome.send('toggleEnrollmentScreen');
339   };
341   /**
342    * Finish enterprise enrollment for telemetry.
343    */
344   Oobe.enterpriseEnrollmentDone = function() {
345     chrome.send('oauthEnrollClose', ['done']);
346   };
348   /**
349    * Returns true if enrollment was successful. Dismisses the enrollment
350    * attribute screen if it's present.
351    */
352   Oobe.isEnrollmentSuccessfulForTest = function() {
353     if (document.querySelector('.oauth-enroll-state-attribute-prompt'))
354       chrome.send('oauthEnrollAttributes', ['', '']);
356     return $('oauth-enrollment').classList.contains(
357       'oauth-enroll-state-success');
358   };
360   /**
361    * Shows/hides login UI control bar with buttons like [Shut down].
362    */
363   Oobe.showControlBar = function(show) {
364     Oobe.getInstance().headerHidden = !show;
365   };
367   /**
368    * Sets the current size of the client area (display size).
369    * @param {number} width client area width
370    * @param {number} height client area height
371    */
372   Oobe.setClientAreaSize = function(width, height) {
373     Oobe.getInstance().setClientAreaSize(width, height);
374   };
376   /**
377    * Checks whether the New Gaia flow is active.
378    */
379   Oobe.isNewGaiaFlow = function() {
380     return document.querySelector('.new-gaia-flow') != undefined;
381   };
383   // Export
384   return {
385     Oobe: Oobe
386   };
389 var Oobe = cr.ui.Oobe;
391 // Allow selection events on components with editable text (password field)
392 // bug (http://code.google.com/p/chromium/issues/detail?id=125863)
393 disableTextSelectAndDrag(function(e) {
394   var src = e.target;
395   return src instanceof HTMLTextAreaElement ||
396          src instanceof HTMLInputElement &&
397          /text|password|search/.test(src.type);
400 // Register assets for async loading.
402   id: SCREEN_OOBE_ENROLLMENT,
403   html: [{ url: 'chrome://oobe/enrollment.html', targetID: 'inner-container' }],
404   css: ['chrome://oobe/enrollment.css'],
405   js: ['chrome://oobe/enrollment.js']
406 }].forEach(cr.ui.login.ResourceLoader.registerAssets);
408 (function() {
409   'use strict';
411   document.addEventListener('DOMContentLoaded', function() {
412     // Immediately load async assets.
413     // TODO(dconnelly): remove this at some point and only load as needed.
414     // See crbug.com/236426
415     cr.ui.login.ResourceLoader.loadAssets(SCREEN_OOBE_ENROLLMENT, function() {
416       // This screen is async-loaded so we manually trigger i18n processing.
417       i18nTemplate.process($('oauth-enrollment'), loadTimeData);
418       // Delayed binding since this isn't defined yet.
419       login.OAuthEnrollmentScreen.register();
420     });
422     cr.ui.Oobe.initialize();
423   });
424 })();