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.
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';
12 /** @const */ var HELP_TOPIC_ENROLLMENT = 4631259;
19 'setAuthenticatedUserEmail',
24 * URL to load in the sign in frame.
29 * Dialog to confirm that auto-enrollment should really be cancelled.
30 * This is only created the first time it's used.
35 * The current step. This is the last value passed to showStep().
40 * Opaque token used to correlate request and response while retrieving the
41 * authenticated user's e-mail address from GAIA.
46 decorate: function() {
47 window.addEventListener('message',
48 this.onMessage_.bind(this), false);
49 $('oauth-enroll-error-retry').addEventListener('click',
50 this.doRetry_.bind(this));
51 $('oauth-enroll-learn-more-link').addEventListener(
54 chrome.send('launchHelpApp', [HELP_TOPIC_ENROLLMENT]);
56 var links = document.querySelectorAll('.oauth-enroll-explain-link');
57 for (var i = 0; i < links.length; i++) {
58 links[i].addEventListener('click',
59 this.showStep.bind(this, STEP_EXPLAIN));
62 this.updateLocalizedContent();
66 * Updates localized strings.
68 updateLocalizedContent: function() {
69 $('oauth-enroll-re-enrollment-text').innerHTML =
70 loadTimeData.getStringF(
71 'oauthEnrollReEnrollmentText',
72 '<b id="oauth-enroll-management-domain"></b>');
73 $('oauth-enroll-management-domain').textContent = this.managementDomain_;
74 $('oauth-enroll-re-enrollment-text').hidden = !this.managementDomain_;
78 * Header text of the screen.
82 return loadTimeData.getString('oauthEnrollScreenTitle');
86 * Buttons in oobe wizard's button strip.
87 * @type {array} Array of Buttons.
91 var ownerDocument = this.ownerDocument;
93 function makeButton(id, classes, label, handler) {
94 var button = ownerDocument.createElement('button');
96 button.classList.add('oauth-enroll-button');
97 button.classList.add.apply(button.classList, classes);
98 button.textContent = label;
99 button.addEventListener('click', handler);
100 buttons.push(button);
104 'oauth-enroll-cancel-button',
105 ['oauth-enroll-focus-on-error'],
106 loadTimeData.getString('oauthEnrollCancel'),
108 chrome.send('oauthEnrollClose', ['cancel']);
112 'oauth-enroll-back-button',
113 ['oauth-enroll-focus-on-error'],
114 loadTimeData.getString('oauthEnrollCancelAutoEnrollmentGoBack'),
116 chrome.send('oauthEnrollClose', ['cancel']);
120 'oauth-enroll-retry-button',
121 ['oauth-enroll-focus-on-error'],
122 loadTimeData.getString('oauthEnrollRetry'),
123 this.doRetry_.bind(this));
126 'oauth-enroll-explain-retry-button',
127 ['oauth-enroll-focus-on-explain'],
128 loadTimeData.getString('oauthEnrollExplainButton'),
129 this.doRetry_.bind(this));
132 'oauth-enroll-done-button',
133 ['oauth-enroll-focus-on-success'],
134 loadTimeData.getString('oauthEnrollDone'),
136 chrome.send('oauthEnrollClose', ['done']);
143 * Event handler that is invoked just before the frame is shown.
144 * @param {Object} data Screen init payload, contains the signin frame
147 onBeforeShow: function(data) {
148 var url = data.signin_url;
149 url += '?gaiaUrl=' + encodeURIComponent(data.gaiaUrl);
150 this.signInUrl_ = url;
151 var modes = ['manual', 'forced', 'auto'];
152 for (var i = 0; i < modes.length; ++i) {
153 this.classList.toggle('mode-' + modes[i],
154 data.enrollment_mode == modes[i]);
156 this.managementDomain_ = data.management_domain;
157 $('oauth-enroll-signin-frame').contentWindow.location.href =
159 this.updateLocalizedContent();
160 this.showStep(STEP_SIGNIN);
164 * Cancels enrollment and drops the user back to the login screen.
167 chrome.send('oauthEnrollClose', ['cancel']);
171 * Switches between the different steps in the enrollment flow.
172 * @param {string} step the steps to show, one of "signin", "working",
173 * "error", "success".
175 showStep: function(step) {
176 this.classList.toggle('oauth-enroll-state-' + this.currentStep_, false);
177 this.classList.toggle('oauth-enroll-state-' + step, true);
179 this.querySelectorAll('.oauth-enroll-focus-on-' + step);
180 for (var i = 0; i < focusElements.length; ++i) {
181 if (getComputedStyle(focusElements[i])['display'] != 'none') {
182 focusElements[i].focus();
186 this.currentStep_ = step;
190 * Sets an error message and switches to the error screen.
191 * @param {string} message the error message.
192 * @param {boolean} retry whether the retry link should be shown.
194 showError: function(message, retry) {
195 $('oauth-enroll-error-message').textContent = message;
196 $('oauth-enroll-error-retry').hidden = !retry;
197 this.showStep(STEP_ERROR);
201 * Sets a progress message and switches to the working screen.
202 * @param {string} message the progress message.
204 showWorking: function(message) {
205 $('oauth-enroll-working-message').textContent = message;
206 this.showStep(STEP_WORKING);
210 * Invoked when the authenticated user's e-mail address has been retrieved.
211 * This completes SAML authentication.
212 * @param {number} attemptToken An opaque token used to correlate this
213 * method invocation with the corresponding request to retrieve the
214 * user's e-mail address.
215 * @param {string} email The authenticated user's e-mail address.
217 setAuthenticatedUserEmail: function(attemptToken, email) {
218 if (this.attemptToken_ != attemptToken)
222 this.showError(loadTimeData.getString('fatalEnrollmentError'), false);
224 chrome.send('oauthEnrollCompleteLogin', [email]);
227 doReload: function() {
228 $('oauth-enroll-signin-frame').contentWindow.location.href =
233 * Handler for cancellations of an enforced auto-enrollment.
235 cancelAutoEnrollment: function() {
236 // Only to be activated for the explain step in auto-enrollment.
237 if (this.currentStep_ !== STEP_EXPLAIN)
240 if (!this.confirmDialog_) {
241 this.confirmDialog_ = new cr.ui.dialogs.ConfirmDialog(document.body);
242 this.confirmDialog_.setOkLabel(
243 loadTimeData.getString('oauthEnrollCancelAutoEnrollmentConfirm'));
244 this.confirmDialog_.setCancelLabel(
245 loadTimeData.getString('oauthEnrollCancelAutoEnrollmentGoBack'));
246 this.confirmDialog_.setInitialFocusOnCancel();
248 this.confirmDialog_.show(
249 loadTimeData.getString('oauthEnrollCancelAutoEnrollmentReally'),
250 this.onConfirmCancelAutoEnrollment_.bind(this));
254 * Retries the enrollment process after an error occurred in a previous
255 * attempt. This goes to the C++ side through |chrome| first to clean up the
256 * profile, so that the next attempt is performed with a clean state.
258 doRetry_: function() {
259 chrome.send('oauthEnrollRetry');
263 * Handler for confirmation of cancellation of auto-enrollment.
265 onConfirmCancelAutoEnrollment_: function() {
266 chrome.send('oauthEnrollClose', ['autocancel']);
270 * Checks if a given HTML5 message comes from the URL loaded into the signin
272 * @param {Object} m HTML5 message.
273 * @type {boolean} whether the message comes from the signin frame.
275 isSigninMessage_: function(m) {
276 return this.signInUrl_ != null &&
277 this.signInUrl_.indexOf(m.origin) == 0 &&
278 m.source == $('oauth-enroll-signin-frame').contentWindow;
282 * Event handler for HTML5 messages.
283 * @param {Object} m HTML5 message.
285 onMessage_: function(m) {
286 if (!this.isSigninMessage_(m))
291 if (msg.method == 'completeLogin') {
292 // A user has successfully authenticated via regular GAIA.
293 chrome.send('oauthEnrollCompleteLogin', [msg.email]);
296 if (msg.method == 'retrieveAuthenticatedUserEmail') {
297 // A user has successfully authenticated via SAML. However, the user's
298 // identity is not known. Instead of reporting success immediately,
299 // retrieve the user's e-mail address first.
300 this.attemptToken_ = msg.attemptToken;
301 this.showWorking(null);
302 chrome.send('oauthEnrollRetrieveAuthenticatedUserEmail',
306 if (msg.method == 'authPageLoaded' && this.currentStep_ == STEP_SIGNIN) {
308 $('oauth-saml-notice-message').textContent = loadTimeData.getStringF(
312 this.classList.toggle('saml', msg.isSAML);
313 chrome.send('frameLoadingCompleted', [0]);
316 if (msg.method == 'insecureContentBlocked') {
318 loadTimeData.getStringF('insecureURLEnrollmentError', msg.url),