Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / resources / chromeos / login / oobe_screen_oauth_enrollment.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 login.createScreen('OAuthEnrollmentScreen', 'oauth-enrollment', function() {
6   /** @const */ var STEP_SIGNIN = 'signin';
7   /** @const */ var STEP_WORKING = 'working';
8   /** @const */ var STEP_ERROR = 'error';
9   /** @const */ var STEP_EXPLAIN = 'explain';
10   /** @const */ var STEP_SUCCESS = 'success';
11
12   return {
13     EXTERNAL_API: [
14       'setIsAutoEnrollment',
15       'showStep',
16       'showError',
17       'showWorking',
18       'setAuthenticatedUserEmail',
19     ],
20
21     /**
22      * URL to load in the sign in frame.
23      */
24     signInUrl_: null,
25
26     /**
27      * Whether this is a manual or auto enrollment.
28      */
29     isAutoEnrollment_: false,
30
31     /**
32      * True if enrollment cancellation should be prevented.
33      */
34     preventCancellation_: false,
35
36     /**
37      * Enrollment steps with names and buttons to show.
38      */
39     steps_: null,
40
41     /**
42      * Dialog to confirm that auto-enrollment should really be cancelled.
43      * This is only created the first time it's used.
44      */
45     confirmDialog_: null,
46
47     /**
48      * The current step. This is the last value passed to showStep().
49      */
50     currentStep_: null,
51
52     /**
53      * Opaque token used to correlate request and response while retrieving the
54      * authenticated user's e-mail address from GAIA.
55      */
56     attemptToken_: null,
57
58     /** @override */
59     decorate: function() {
60       window.addEventListener('message',
61                               this.onMessage_.bind(this), false);
62       $('oauth-enroll-error-retry').addEventListener('click',
63                                                      this.doRetry_.bind(this));
64       var links = document.querySelectorAll('.oauth-enroll-explain-link');
65       for (var i = 0; i < links.length; i++) {
66         links[i].addEventListener('click',
67                                   this.showStep.bind(this, STEP_EXPLAIN));
68       }
69     },
70
71     /**
72      * Header text of the screen.
73      * @type {string}
74      */
75     get header() {
76       return loadTimeData.getString('oauthEnrollScreenTitle');
77     },
78
79     /**
80      * Buttons in oobe wizard's button strip.
81      * @type {array} Array of Buttons.
82      */
83     get buttons() {
84       var buttons = [];
85
86       var cancelButton = this.ownerDocument.createElement('button');
87       cancelButton.id = 'oauth-enroll-cancel-button';
88       cancelButton.textContent = loadTimeData.getString('oauthEnrollCancel');
89
90       cancelButton.addEventListener('click', function(e) {
91         chrome.send('oauthEnrollClose', ['cancel']);
92       }.bind(this));
93       buttons.push(cancelButton);
94
95       var tryAgainButton = this.ownerDocument.createElement('button');
96       tryAgainButton.id = 'oauth-enroll-try-again-button';
97       tryAgainButton.hidden = true;
98       tryAgainButton.textContent =
99           loadTimeData.getString('oauthEnrollRetry');
100       tryAgainButton.addEventListener('click', this.doRetry_.bind(this));
101       buttons.push(tryAgainButton);
102
103       var explainButton = this.ownerDocument.createElement('button');
104       explainButton.id = 'oauth-enroll-explain-button';
105       explainButton.hidden = true;
106       explainButton.textContent =
107           loadTimeData.getString('oauthEnrollExplainButton');
108       explainButton.addEventListener('click', this.doRetry_.bind(this));
109       buttons.push(explainButton);
110
111       var doneButton = this.ownerDocument.createElement('button');
112       doneButton.id = 'oauth-enroll-done-button';
113       doneButton.hidden = true;
114       doneButton.textContent =
115           loadTimeData.getString('oauthEnrollDone');
116       doneButton.addEventListener('click', function(e) {
117         chrome.send('oauthEnrollClose', ['done']);
118       });
119       buttons.push(doneButton);
120
121       return buttons;
122     },
123
124     /**
125      * Sets the |isAutoEnrollment| flag of the OAuthEnrollmentScreen class and
126      * updates the UI.
127      * @param {boolean} is_auto_enrollment the new value of the flag.
128      */
129     setIsAutoEnrollment: function(is_auto_enrollment) {
130       this.isAutoEnrollment_ = is_auto_enrollment;
131       // The cancel button is not available during auto-enrollment.
132       var cancel = this.isAutoEnrollment_ ? null : 'cancel';
133       // During auto-enrollment the user must try again from the error screen.
134       var errorCancel = this.isAutoEnrollment_ ? 'try-again' : 'cancel';
135       this.steps_ = [
136         {
137           name: STEP_SIGNIN,
138           button: cancel
139         },
140         {
141           name: STEP_WORKING,
142           button: cancel
143         },
144         {
145           name: STEP_ERROR,
146           button: errorCancel,
147           focusButton: this.isAutoEnrollment_
148         },
149         {
150           name: STEP_EXPLAIN,
151           button: 'explain',
152           focusButton: true
153         },
154         {
155           name: STEP_SUCCESS,
156           button: 'done',
157           focusButton: true
158         },
159       ];
160
161       var links = document.querySelectorAll('.oauth-enroll-explain-link');
162       for (var i = 0; i < links.length; i++) {
163         links[i].hidden = !this.isAutoEnrollment_;
164       }
165     },
166
167     /**
168      * Event handler that is invoked just before the frame is shown.
169      * @param {Object} data Screen init payload, contains the signin frame
170      * URL.
171      */
172     onBeforeShow: function(data) {
173       var url = data.signin_url;
174       url += '?gaiaUrl=' + encodeURIComponent(data.gaiaUrl);
175       this.signInUrl_ = url;
176       this.setIsAutoEnrollment(data.is_auto_enrollment);
177       this.preventCancellation_ = data.prevent_cancellation;
178       $('oauth-enroll-signin-frame').contentWindow.location.href =
179           this.signInUrl_;
180       if (this.preventCancellation_) {
181         $('oauth-enroll-cancel-button').textContent =
182             loadTimeData.getString('oauthEnrollCancelAutoEnrollmentGoBack');
183       }
184       this.classList.toggle('saml', false);
185
186       this.showStep(STEP_SIGNIN);
187     },
188
189     /**
190      * Cancels enrollment and drops the user back to the login screen.
191      */
192     cancel: function() {
193       if (this.isAutoEnrollment_)
194         return;
195
196       chrome.send('oauthEnrollClose', ['cancel']);
197     },
198
199     /**
200      * Switches between the different steps in the enrollment flow.
201      * @param {string} step the steps to show, one of "signin", "working",
202      * "error", "success".
203      */
204     showStep: function(step) {
205       this.currentStep_ = step;
206       $('oauth-enroll-cancel-button').hidden = true;
207       $('oauth-enroll-try-again-button').hidden = true;
208       $('oauth-enroll-explain-button').hidden = true;
209       $('oauth-enroll-done-button').hidden = true;
210       for (var i = 0; i < this.steps_.length; i++) {
211         var theStep = this.steps_[i];
212         var active = (theStep.name == step);
213         $('oauth-enroll-step-' + theStep.name).hidden = !active;
214         if (active && theStep.button) {
215           var button = $('oauth-enroll-' + theStep.button + '-button');
216           button.hidden = false;
217           if (theStep.focusButton)
218             button.focus();
219         }
220       }
221     },
222
223     /**
224      * Sets an error message and switches to the error screen.
225      * @param {string} message the error message.
226      * @param {boolean} retry whether the retry link should be shown.
227      */
228     showError: function(message, retry) {
229       $('oauth-enroll-error-message').textContent = message;
230       $('oauth-enroll-error-retry').hidden = !retry || this.isAutoEnrollment_;
231       this.showStep(STEP_ERROR);
232     },
233
234     /**
235      * Sets a progressing message and switches to the working screen.
236      * @param {string} message the progress message.
237      */
238     showWorking: function(message) {
239       $('oauth-enroll-working-message').textContent = message;
240       this.showStep(STEP_WORKING);
241     },
242
243     /**
244      * Invoked when the authenticated user's e-mail address has been retrieved.
245      * This completes SAML authentication.
246      * @param {number} attemptToken An opaque token used to correlate this
247      *     method invocation with the corresponding request to retrieve the
248      *     user's e-mail address.
249      * @param {string} email The authenticated user's e-mail address.
250      */
251     setAuthenticatedUserEmail: function(attemptToken, email) {
252       if (this.attemptToken_ == attemptToken)
253         chrome.send('oauthEnrollCompleteLogin', [email]);
254     },
255
256     /**
257      * Handler for cancellations of an enforced auto-enrollment.
258      */
259     cancelAutoEnrollment: function() {
260       // Check if this is forced enrollment flow for a kiosk app.
261       if (this.preventCancellation_)
262         return;
263
264       // The dialog to confirm cancellation of auto-enrollment is only shown
265       // if this is an auto-enrollment, and if the user is currently in the
266       // 'explain' step.
267       if (!this.isAutoEnrollment_ || this.currentStep_ !== STEP_EXPLAIN)
268         return;
269       if (!this.confirmDialog_) {
270         this.confirmDialog_ = new cr.ui.dialogs.ConfirmDialog(document.body);
271         this.confirmDialog_.setOkLabel(
272             loadTimeData.getString('oauthEnrollCancelAutoEnrollmentConfirm'));
273         this.confirmDialog_.setCancelLabel(
274             loadTimeData.getString('oauthEnrollCancelAutoEnrollmentGoBack'));
275         this.confirmDialog_.setInitialFocusOnCancel();
276       }
277       this.confirmDialog_.show(
278           loadTimeData.getString('oauthEnrollCancelAutoEnrollmentReally'),
279           this.onConfirmCancelAutoEnrollment_.bind(this));
280     },
281
282     /**
283      * Retries the enrollment process after an error occurred in a previous
284      * attempt. This goes to the C++ side through |chrome| first to clean up the
285      * profile, so that the next attempt is performed with a clean state.
286      */
287     doRetry_: function() {
288       chrome.send('oauthEnrollRetry');
289     },
290
291     /**
292      * Handler for confirmation of cancellation of auto-enrollment.
293      */
294     onConfirmCancelAutoEnrollment_: function() {
295       chrome.send('oauthEnrollClose', ['autocancel']);
296     },
297
298     /**
299      * Checks if a given HTML5 message comes from the URL loaded into the signin
300      * frame.
301      * @param {Object} m HTML5 message.
302      * @type {boolean} whether the message comes from the signin frame.
303      */
304     isSigninMessage_: function(m) {
305       return this.signInUrl_ != null &&
306           this.signInUrl_.indexOf(m.origin) == 0 &&
307           m.source == $('oauth-enroll-signin-frame').contentWindow;
308     },
309
310     /**
311      * Event handler for HTML5 messages.
312      * @param {Object} m HTML5 message.
313      */
314     onMessage_: function(m) {
315       if (!this.isSigninMessage_(m))
316         return;
317
318       var msg = m.data;
319
320       if (msg.method == 'completeLogin') {
321         // A user has successfully authenticated via regular GAIA.
322         chrome.send('oauthEnrollCompleteLogin', [msg.email]);
323       }
324
325       if (msg.method == 'retrieveAuthenticatedUserEmail') {
326         // A user has successfully authenticated via SAML. However, the user's
327         // identity is not known. Instead of reporting success immediately,
328         // retrieve the user's e-mail address first.
329         this.attemptToken_ = msg.attemptToken;
330         this.showWorking(null);
331         chrome.send('oauthEnrollRetrieveAuthenticatedUserEmail',
332                     [msg.attemptToken]);
333       }
334
335       if (msg.method == 'authPageLoaded') {
336         if (msg.isSAML) {
337           $('oauth-saml-notice-message').textContent = loadTimeData.getStringF(
338               'samlNotice',
339               msg.domain);
340         }
341         this.classList.toggle('saml', msg.isSAML);
342       }
343     }
344   };
345 });
346