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_SUCCESS = 'success';
11 /** @const */ var HELP_TOPIC_ENROLLMENT = 4631259;
21 * URL to load in the sign in frame.
26 * Gaia auth params for sign in frame.
31 * The current step. This is the last value passed to showStep().
36 * Opaque token used to correlate request and response while retrieving the
37 * authenticated user's e-mail address from GAIA.
42 * The help topic to show when the user clicks the learn more link.
44 learnMoreHelpTopicID_: null,
47 * We block esc, back button and cancel button until gaia is loaded to
48 * prevent multiple cancel events.
50 isCancelDisabled_: null,
52 get isCancelDisabled() { return this.isCancelDisabled_ },
53 set isCancelDisabled(disabled) {
54 if (disabled == this.isCancelDisabled)
56 this.isCancelDisabled_ = disabled;
58 $('oauth-enroll-back-button').disabled = disabled;
59 $('oauth-enroll-back-button').
60 classList.toggle('preserve-disabled-state', disabled);
62 $('oauth-enroll-cancel-button').disabled = disabled;
63 $('oauth-enroll-cancel-button').
64 classList.toggle('preserve-disabled-state', disabled);
68 decorate: function() {
69 window.addEventListener('message',
70 this.onMessage_.bind(this), false);
71 $('oauth-enroll-error-retry').addEventListener('click',
72 this.doRetry_.bind(this));
73 $('oauth-enroll-learn-more-link').addEventListener(
74 'click', this.launchLearnMoreHelp_.bind(this));
76 this.updateLocalizedContent();
80 * Updates localized strings.
82 updateLocalizedContent: function() {
83 $('oauth-enroll-re-enrollment-text').innerHTML =
84 loadTimeData.getStringF(
85 'oauthEnrollReEnrollmentText',
86 '<b id="oauth-enroll-management-domain"></b>');
87 $('oauth-enroll-management-domain').textContent = this.managementDomain_;
88 $('oauth-enroll-re-enrollment-text').hidden = !this.managementDomain_;
92 * Header text of the screen.
96 return loadTimeData.getString('oauthEnrollScreenTitle');
100 * Buttons in oobe wizard's button strip.
101 * @type {array} Array of Buttons.
105 var ownerDocument = this.ownerDocument;
107 function makeButton(id, classes, label, handler) {
108 var button = ownerDocument.createElement('button');
110 button.classList.add('oauth-enroll-button');
111 button.classList.add.apply(button.classList, classes);
112 button.textContent = label;
113 button.addEventListener('click', handler);
114 buttons.push(button);
118 'oauth-enroll-cancel-button',
119 ['oauth-enroll-focus-on-error'],
120 loadTimeData.getString('oauthEnrollCancel'),
122 chrome.send('oauthEnrollClose', ['cancel']);
126 'oauth-enroll-back-button',
127 ['oauth-enroll-focus-on-error'],
128 loadTimeData.getString('oauthEnrollBack'),
130 this.isCancelDisabled = true;
131 chrome.send('oauthEnrollClose', ['cancel']);
135 'oauth-enroll-retry-button',
136 ['oauth-enroll-focus-on-error'],
137 loadTimeData.getString('oauthEnrollRetry'),
138 this.doRetry_.bind(this));
141 'oauth-enroll-done-button',
142 ['oauth-enroll-focus-on-success'],
143 loadTimeData.getString('oauthEnrollDone'),
145 chrome.send('oauthEnrollClose', ['done']);
152 * Event handler that is invoked just before the frame is shown.
153 * @param {Object} data Screen init payload, contains the signin frame
156 onBeforeShow: function(data) {
157 this.signInParams_ = {};
158 this.signInParams_['gaiaUrl'] = data.gaiaUrl;
159 this.signInParams_['needPassword'] = false;
160 this.signInUrl_ = data.signin_url;
161 var modes = ['manual', 'forced', 'recovery'];
162 for (var i = 0; i < modes.length; ++i) {
163 this.classList.toggle('mode-' + modes[i],
164 data.enrollment_mode == modes[i]);
166 this.managementDomain_ = data.management_domain;
167 this.isCancelDisabled = true;
169 this.learnMoreHelpTopicID_ = data.learn_more_help_topic_id;
170 this.updateLocalizedContent();
171 this.showStep(STEP_SIGNIN);
175 * Cancels enrollment and drops the user back to the login screen.
178 if (this.isCancelDisabled)
180 this.isCancelDisabled = true;
181 chrome.send('oauthEnrollClose', ['cancel']);
185 * Switches between the different steps in the enrollment flow.
186 * @param {string} step the steps to show, one of "signin", "working",
187 * "error", "success".
189 showStep: function(step) {
190 this.classList.toggle('oauth-enroll-state-' + this.currentStep_, false);
191 this.classList.toggle('oauth-enroll-state-' + step, true);
193 this.querySelectorAll('.oauth-enroll-focus-on-' + step);
194 for (var i = 0; i < focusElements.length; ++i) {
195 if (getComputedStyle(focusElements[i])['display'] != 'none') {
196 focusElements[i].focus();
200 this.currentStep_ = step;
204 * Sets an error message and switches to the error screen.
205 * @param {string} message the error message.
206 * @param {boolean} retry whether the retry link should be shown.
208 showError: function(message, retry) {
209 $('oauth-enroll-error-message').textContent = message;
210 $('oauth-enroll-error-retry').hidden = !retry;
211 this.showStep(STEP_ERROR);
214 doReload: function() {
215 var signInFrame = $('oauth-enroll-signin-frame');
217 var sendParamsOnLoad = function() {
218 signInFrame.removeEventListener('load', sendParamsOnLoad);
219 signInFrame.contentWindow.postMessage(this.signInParams_,
220 'chrome-extension://mfffpogegjflfpflabcdkioaeobkgjik');
223 signInFrame.addEventListener('load', sendParamsOnLoad);
224 signInFrame.contentWindow.location.href = this.signInUrl_;
228 * Retries the enrollment process after an error occurred in a previous
229 * attempt. This goes to the C++ side through |chrome| first to clean up the
230 * profile, so that the next attempt is performed with a clean state.
232 doRetry_: function() {
233 chrome.send('oauthEnrollRetry');
237 * Checks if a given HTML5 message comes from the URL loaded into the signin
239 * @param {Object} m HTML5 message.
240 * @type {boolean} whether the message comes from the signin frame.
242 isSigninMessage_: function(m) {
243 return this.signInUrl_ != null &&
244 this.signInUrl_.indexOf(m.origin) == 0 &&
245 m.source == $('oauth-enroll-signin-frame').contentWindow;
249 * Event handler for HTML5 messages.
250 * @param {Object} m HTML5 message.
252 onMessage_: function(m) {
253 if (!this.isSigninMessage_(m))
258 if (msg.method == 'completeLogin') {
259 // A user has successfully authenticated via regular GAIA or SAML.
260 chrome.send('oauthEnrollCompleteLogin', [msg.email]);
263 if (msg.method == 'authPageLoaded' && this.currentStep_ == STEP_SIGNIN) {
265 $('oauth-saml-notice-message').textContent = loadTimeData.getStringF(
269 this.classList.toggle('saml', msg.isSAML);
272 if (msg.method == 'resetAuthFlow') {
273 this.classList.remove('saml');
276 if (msg.method == 'loginUILoaded' && this.currentStep_ == STEP_SIGNIN) {
277 this.isCancelDisabled = false;
278 chrome.send('frameLoadingCompleted', [0]);
281 if (msg.method == 'insecureContentBlocked') {
283 loadTimeData.getStringF('insecureURLEnrollmentError', msg.url),
287 if (msg.method == 'missingGaiaInfo') {
289 loadTimeData.getString('fatalEnrollmentError'),
295 * Opens the learn more help topic.
297 launchLearnMoreHelp_: function() {
298 if (this.learnMoreHelpTopicID_) {
299 chrome.send('launchHelpApp', [this.learnMoreHelpTopicID_]);