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 * The help topic to show when the user clicks the learn more link.
38 learnMoreHelpTopicID_: null,
41 * We block esc, back button and cancel button until gaia is loaded to
42 * prevent multiple cancel events.
44 isCancelDisabled_: null,
46 get isCancelDisabled() { return this.isCancelDisabled_ },
47 set isCancelDisabled(disabled) {
48 if (disabled == this.isCancelDisabled)
50 this.isCancelDisabled_ = disabled;
52 $('oauth-enroll-back-button').disabled = disabled;
53 $('oauth-enroll-back-button').
54 classList.toggle('preserve-disabled-state', disabled);
56 $('oauth-enroll-cancel-button').disabled = disabled;
57 $('oauth-enroll-cancel-button').
58 classList.toggle('preserve-disabled-state', disabled);
62 decorate: function() {
63 window.addEventListener('message',
64 this.onMessage_.bind(this), false);
65 $('oauth-enroll-error-retry').addEventListener('click',
66 this.doRetry_.bind(this));
67 $('oauth-enroll-learn-more-link').addEventListener(
68 'click', this.launchLearnMoreHelp_.bind(this));
70 this.updateLocalizedContent();
74 * Updates localized strings.
76 updateLocalizedContent: function() {
77 $('oauth-enroll-re-enrollment-text').innerHTML =
78 loadTimeData.getStringF(
79 'oauthEnrollReEnrollmentText',
80 '<b id="oauth-enroll-management-domain"></b>');
81 $('oauth-enroll-management-domain').textContent = this.managementDomain_;
82 $('oauth-enroll-re-enrollment-text').hidden = !this.managementDomain_;
86 * Header text of the screen.
90 return loadTimeData.getString('oauthEnrollScreenTitle');
94 * Buttons in oobe wizard's button strip.
95 * @type {array} Array of Buttons.
99 var ownerDocument = this.ownerDocument;
101 function makeButton(id, classes, label, handler) {
102 var button = ownerDocument.createElement('button');
104 button.classList.add('oauth-enroll-button');
105 button.classList.add.apply(button.classList, classes);
106 button.textContent = label;
107 button.addEventListener('click', handler);
108 buttons.push(button);
112 'oauth-enroll-cancel-button',
113 ['oauth-enroll-focus-on-error'],
114 loadTimeData.getString('oauthEnrollCancel'),
116 chrome.send('oauthEnrollClose', ['cancel']);
120 'oauth-enroll-back-button',
121 ['oauth-enroll-focus-on-error'],
122 loadTimeData.getString('oauthEnrollBack'),
124 this.isCancelDisabled = true;
125 chrome.send('oauthEnrollClose', ['cancel']);
129 'oauth-enroll-done-button',
130 ['oauth-enroll-focus-on-success'],
131 loadTimeData.getString('oauthEnrollDone'),
133 chrome.send('oauthEnrollClose', ['done']);
140 * Event handler that is invoked just before the frame is shown.
141 * @param {Object} data Screen init payload, contains the signin frame
144 onBeforeShow: function(data) {
145 this.signInParams_ = {};
146 this.signInParams_['gaiaUrl'] = data.gaiaUrl;
147 this.signInParams_['needPassword'] = false;
148 this.signInUrl_ = data.signin_url;
149 var modes = ['manual', 'forced', 'recovery'];
150 for (var i = 0; i < modes.length; ++i) {
151 this.classList.toggle('mode-' + modes[i],
152 data.enrollment_mode == modes[i]);
154 this.managementDomain_ = data.management_domain;
155 this.isCancelDisabled = true;
157 this.learnMoreHelpTopicID_ = data.learn_more_help_topic_id;
158 this.updateLocalizedContent();
159 this.showStep(STEP_SIGNIN);
163 * Cancels enrollment and drops the user back to the login screen.
166 if (this.isCancelDisabled)
168 this.isCancelDisabled = true;
169 chrome.send('oauthEnrollClose', ['cancel']);
173 * Switches between the different steps in the enrollment flow.
174 * @param {string} step the steps to show, one of "signin", "working",
175 * "error", "success".
177 showStep: function(step) {
178 this.classList.toggle('oauth-enroll-state-' + this.currentStep_, false);
179 this.classList.toggle('oauth-enroll-state-' + step, true);
181 this.querySelectorAll('.oauth-enroll-focus-on-' + step);
182 for (var i = 0; i < focusElements.length; ++i) {
183 if (getComputedStyle(focusElements[i])['display'] != 'none') {
184 focusElements[i].focus();
188 this.currentStep_ = step;
192 * Sets an error message and switches to the error screen.
193 * @param {string} message the error message.
194 * @param {boolean} retry whether the retry link should be shown.
196 showError: function(message, retry) {
197 $('oauth-enroll-error-message').textContent = message;
198 $('oauth-enroll-error-retry').hidden = !retry;
199 this.showStep(STEP_ERROR);
202 doReload: function() {
203 var signInFrame = $('oauth-enroll-signin-frame');
205 var sendParamsOnLoad = function() {
206 signInFrame.removeEventListener('load', sendParamsOnLoad);
207 signInFrame.contentWindow.postMessage(this.signInParams_,
208 'chrome-extension://mfffpogegjflfpflabcdkioaeobkgjik');
211 signInFrame.addEventListener('load', sendParamsOnLoad);
212 signInFrame.contentWindow.location.href = this.signInUrl_;
216 * Retries the enrollment process after an error occurred in a previous
217 * attempt. This goes to the C++ side through |chrome| first to clean up the
218 * profile, so that the next attempt is performed with a clean state.
220 doRetry_: function() {
221 chrome.send('oauthEnrollRetry');
225 * Checks if a given HTML5 message comes from the URL loaded into the signin
227 * @param {Object} m HTML5 message.
228 * @type {boolean} whether the message comes from the signin frame.
230 isSigninMessage_: function(m) {
231 return this.signInUrl_ != null &&
232 this.signInUrl_.indexOf(m.origin) == 0 &&
233 m.source == $('oauth-enroll-signin-frame').contentWindow;
237 * Event handler for HTML5 messages.
238 * @param {Object} m HTML5 message.
240 onMessage_: function(m) {
241 if (!this.isSigninMessage_(m))
246 if (msg.method == 'completeLogin') {
247 // A user has successfully authenticated via regular GAIA or SAML.
248 chrome.send('oauthEnrollCompleteLogin', [msg.email,
249 '' /* auth_code */]);
252 if (msg.method == 'authPageLoaded' && this.currentStep_ == STEP_SIGNIN) {
254 $('oauth-saml-notice-message').textContent = loadTimeData.getStringF(
258 this.classList.toggle('saml', msg.isSAML);
261 if (msg.method == 'resetAuthFlow') {
262 this.classList.remove('saml');
265 if (msg.method == 'loginUILoaded' && this.currentStep_ == STEP_SIGNIN) {
266 this.isCancelDisabled = false;
267 chrome.send('frameLoadingCompleted', [0]);
270 if (msg.method == 'insecureContentBlocked') {
272 loadTimeData.getStringF('insecureURLEnrollmentError', msg.url),
276 if (msg.method == 'missingGaiaInfo') {
278 loadTimeData.getString('fatalEnrollmentError'),
284 * Opens the learn more help topic.
286 launchLearnMoreHelp_: function() {
287 if (this.learnMoreHelpTopicID_) {
288 chrome.send('launchHelpApp', [this.learnMoreHelpTopicID_]);