Upstream version 5.34.92.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / resources / chromeos / login / screen_account_picker.js
1 // Copyright (c) 2012 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 Account picker screen implementation.
7  */
8
9 login.createScreen('AccountPickerScreen', 'account-picker', function() {
10   /**
11    * Maximum number of offline login failures before online login.
12    * @type {number}
13    * @const
14    */
15   var MAX_LOGIN_ATTEMPTS_IN_POD = 3;
16   /**
17    * Whether to preselect the first pod automatically on login screen.
18    * @type {boolean}
19    * @const
20    */
21   var PRESELECT_FIRST_POD = true;
22
23   return {
24     EXTERNAL_API: [
25       'loadUsers',
26       'updateUserImage',
27       'forceOnlineSignin',
28       'setCapsLockState',
29       'forceLockedUserPodFocus',
30       'removeUser',
31       'showBannerMessage',
32       'showUserPodButton',
33     ],
34
35     preferredWidth_: 0,
36     preferredHeight_: 0,
37
38     // Whether this screen is shown for the first time.
39     firstShown_: true,
40
41     /** @override */
42     decorate: function() {
43       login.PodRow.decorate($('pod-row'));
44     },
45
46     /** @override */
47     getPreferredSize: function() {
48       return {width: this.preferredWidth_, height: this.preferredHeight_};
49     },
50
51     /** @override */
52     onWindowResize: function() {
53       $('pod-row').onWindowResize();
54     },
55
56     /**
57      * Sets preferred size for account picker screen.
58      */
59     setPreferredSize: function(width, height) {
60       this.preferredWidth_ = width;
61       this.preferredHeight_ = height;
62     },
63
64     /**
65      * When the account picker is being used to lock the screen, pressing the
66      * exit accelerator key will sign out the active user as it would when
67      * they are signed in.
68      */
69     exit: function() {
70       // Check and disable the sign out button so that we can never have two
71       // sign out requests generated in a row.
72       if ($('pod-row').lockedPod && !$('sign-out-user-button').disabled) {
73         $('sign-out-user-button').disabled = true;
74         chrome.send('signOutUser');
75       }
76     },
77
78     /* Cancel user adding if ESC was pressed.
79      */
80     cancel: function() {
81       if (Oobe.getInstance().displayType() == DISPLAY_TYPE.USER_ADDING)
82         chrome.send('cancelUserAdding');
83     },
84
85     /**
86      * Event handler that is invoked just after the frame is shown.
87      * @param {string} data Screen init payload.
88      */
89     onAfterShow: function(data) {
90       $('pod-row').handleAfterShow();
91     },
92
93     /**
94      * Event handler that is invoked just before the frame is shown.
95      * @param {string} data Screen init payload.
96      */
97     onBeforeShow: function(data) {
98       chrome.send('loginUIStateChanged', ['account-picker', true]);
99       $('login-header-bar').signinUIState = SIGNIN_UI_STATE.ACCOUNT_PICKER;
100       chrome.send('hideCaptivePortal');
101       var podRow = $('pod-row');
102       podRow.handleBeforeShow();
103
104       // If this is showing for the lock screen display the sign out button,
105       // hide the add user button and activate the locked user's pod.
106       var lockedPod = podRow.lockedPod;
107       $('add-user-header-bar-item').hidden = !!lockedPod;
108       var signOutUserItem = $('sign-out-user-item');
109       if (signOutUserItem)
110         signOutUserItem.hidden = !lockedPod;
111       // In case of the preselected pod onShow will be called once pod
112       // receives focus.
113       if (!podRow.preselectedPod)
114         this.onShow();
115     },
116
117     /**
118      * Event handler invoked when the page is shown and ready.
119      */
120     onShow: function() {
121       if (!this.firstShown_) return;
122       this.firstShown_ = false;
123       // TODO(nkostylev): Enable animation back when session start jank
124       // is reduced. See http://crosbug.com/11116 http://crosbug.com/18307
125       // $('pod-row').startInitAnimation();
126
127       // Ensure that login is actually visible.
128       window.webkitRequestAnimationFrame(function() {
129         chrome.send('accountPickerReady');
130         chrome.send('loginVisible', ['account-picker']);
131       });
132     },
133
134     /**
135      * Event handler that is invoked just before the frame is hidden.
136      */
137     onBeforeHide: function() {
138       chrome.send('loginUIStateChanged', ['account-picker', false]);
139       $('login-header-bar').signinUIState = SIGNIN_UI_STATE.HIDDEN;
140       $('pod-row').handleHide();
141     },
142
143     /**
144      * Shows sign-in error bubble.
145      * @param {number} loginAttempts Number of login attemps tried.
146      * @param {HTMLElement} content Content to show in bubble.
147      */
148     showErrorBubble: function(loginAttempts, error) {
149       var activatedPod = $('pod-row').activatedPod;
150       if (!activatedPod) {
151         $('bubble').showContentForElement($('pod-row'),
152                                           cr.ui.Bubble.Attachment.RIGHT,
153                                           error);
154         return;
155       }
156       // Show web authentication if this is not a locally managed user.
157       if (loginAttempts > MAX_LOGIN_ATTEMPTS_IN_POD &&
158           !activatedPod.user.locallyManagedUser) {
159         activatedPod.showSigninUI();
160       } else {
161         // We want bubble's arrow to point to the first letter of input.
162         /** @const */ var BUBBLE_OFFSET = 7;
163         /** @const */ var BUBBLE_PADDING = 4;
164         $('bubble').showContentForElement(activatedPod.mainInput,
165                                           cr.ui.Bubble.Attachment.BOTTOM,
166                                           error,
167                                           BUBBLE_OFFSET, BUBBLE_PADDING);
168       }
169     },
170
171     /**
172      * Loads given users in pod row.
173      * @param {array} users Array of user.
174      * @param {boolean} animated Whether to use init animation.
175      * @param {boolean} showGuest Whether to show guest session button.
176      */
177     loadUsers: function(users, animated, showGuest) {
178       $('pod-row').loadPods(users, animated);
179       $('login-header-bar').showGuestButton = showGuest;
180
181       // The desktop User Manager can send the index of a pod that should be
182       // initially focused.
183       var hash = window.location.hash;
184       if (hash) {
185         var podIndex = hash.substr(1);
186         if (podIndex)
187           $('pod-row').focusPodByIndex(podIndex, false);
188       }
189     },
190
191     /**
192      * Updates current image of a user.
193      * @param {string} username User for which to update the image.
194      */
195     updateUserImage: function(username) {
196       $('pod-row').updateUserImage(username);
197     },
198
199     /**
200      * Indicates that the given user must authenticate against GAIA during the
201      * next sign-in.
202      * @param {string} username User for whom to enforce GAIA sign-in.
203      */
204     forceOnlineSignin: function(username) {
205       $('pod-row').forceOnlineSigninForUser(username);
206     },
207
208     /**
209      * Updates Caps Lock state (for Caps Lock hint in password input field).
210      * @param {boolean} enabled Whether Caps Lock is on.
211      */
212     setCapsLockState: function(enabled) {
213       $('pod-row').classList.toggle('capslock-on', enabled);
214     },
215
216     /**
217      * Enforces focus on user pod of locked user.
218      */
219     forceLockedUserPodFocus: function() {
220       var row = $('pod-row');
221       if (row.lockedPod)
222         row.focusPod(row.lockedPod, true);
223     },
224
225     /**
226      * Remove given user from pod row if it is there.
227      * @param {string} user name.
228      */
229     removeUser: function(username) {
230       $('pod-row').removeUserPod(username);
231     },
232
233     /**
234      * Displays a banner containing |message|. If the banner is already present
235      * this function updates the message in the banner. This function is used
236      * by the chrome.screenlockPrivate.showMessage API.
237      * @param {string} message Text to be displayed
238      */
239     showBannerMessage: function(message) {
240       var banner = $('signin-banner');
241       banner.textContent = message;
242       banner.classList.toggle('message-set', true);
243     },
244
245     /**
246      * Shows a button with an icon on the user pod of |username|. This function
247      * is used by the chrome.screenlockPrivate API.
248      * @param {string} username Username of pod to add button
249      * @param {string} iconURL URL of the button icon
250      */
251     showUserPodButton: function(username, iconURL) {
252       $('pod-row').showUserPodButton(username, iconURL);
253     }
254   };
255 });
256