Allow only one bookmark to be added for multiple fast starring
[chromium-blink-merge.git] / chrome / browser / resources / chromeos / login / oobe_screen_oauth_enrollment.js
blob7ac1826601cc56da3d88087c19e889edb6607ca3
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_ATTRIBUTE_PROMPT = 'attribute-prompt';
9 /** @const */ var STEP_ERROR = 'error';
10 /** @const */ var STEP_SUCCESS = 'success';
12 /** @const */ var HELP_TOPIC_ENROLLMENT = 4631259;
14 return {
15 EXTERNAL_API: [
16 'showStep',
17 'showError',
18 'doReload',
19 'showAttributePromptStep',
22 /**
23 * URL to load in the sign in frame.
25 signInUrl_: null,
27 /**
28 * Gaia auth params for sign in frame.
30 signInParams_: {},
32 /**
33 * The current step. This is the last value passed to showStep().
35 currentStep_: null,
37 /**
38 * The help topic to show when the user clicks the learn more link.
40 learnMoreHelpTopicID_: null,
42 /**
43 * We block esc, back button and cancel button until gaia is loaded to
44 * prevent multiple cancel events.
46 isCancelDisabled_: null,
48 get isCancelDisabled() { return this.isCancelDisabled_ },
49 set isCancelDisabled(disabled) {
50 if (disabled == this.isCancelDisabled)
51 return;
52 this.isCancelDisabled_ = disabled;
54 $('oauth-enroll-back-button').disabled = disabled;
55 $('oauth-enroll-back-button').
56 classList.toggle('preserve-disabled-state', disabled);
58 $('oauth-enroll-cancel-button').disabled = disabled;
59 $('oauth-enroll-cancel-button').
60 classList.toggle('preserve-disabled-state', disabled);
63 /** @override */
64 decorate: function() {
65 window.addEventListener('message',
66 this.onMessage_.bind(this), false);
67 $('oauth-enroll-error-retry').addEventListener('click',
68 this.doRetry_.bind(this));
69 $('oauth-enroll-learn-more-link').addEventListener(
70 'click', this.launchLearnMoreHelp_.bind(this));
72 this.updateLocalizedContent();
75 /**
76 * Updates localized strings.
78 updateLocalizedContent: function() {
79 $('oauth-enroll-re-enrollment-text').innerHTML =
80 loadTimeData.getStringF(
81 'oauthEnrollReEnrollmentText',
82 '<b id="oauth-enroll-management-domain"></b>');
83 $('oauth-enroll-management-domain').textContent = this.managementDomain_;
84 $('oauth-enroll-re-enrollment-text').hidden = !this.managementDomain_;
87 /**
88 * Header text of the screen.
89 * @type {string}
91 get header() {
92 return loadTimeData.getString('oauthEnrollScreenTitle');
95 /**
96 * Buttons in oobe wizard's button strip.
97 * @type {array} Array of Buttons.
99 get buttons() {
100 var buttons = [];
101 var ownerDocument = this.ownerDocument;
103 function makeButton(id, classes, label, handler) {
104 var button = ownerDocument.createElement('button');
105 button.id = id;
106 button.classList.add('oauth-enroll-button');
107 button.classList.add.apply(button.classList, classes);
108 button.textContent = label;
109 button.addEventListener('click', handler);
110 buttons.push(button);
113 makeButton(
114 'oauth-enroll-cancel-button',
115 ['oauth-enroll-focus-on-error'],
116 loadTimeData.getString('oauthEnrollCancel'),
117 function() {
118 chrome.send('oauthEnrollClose', ['cancel']);
121 makeButton(
122 'oauth-enroll-back-button',
123 ['oauth-enroll-focus-on-error'],
124 loadTimeData.getString('oauthEnrollBack'),
125 function() {
126 this.isCancelDisabled = true;
127 chrome.send('oauthEnrollClose', ['cancel']);
128 }.bind(this));
130 makeButton(
131 'oauth-enroll-done-button',
132 ['oauth-enroll-focus-on-success'],
133 loadTimeData.getString('oauthEnrollDone'),
134 function() {
135 chrome.send('oauthEnrollClose', ['done']);
138 makeButton(
139 'oauth-enroll-continue-button',
140 ['oauth-enroll-focus-on-attribute-prompt'],
141 loadTimeData.getString('oauthEnrollContinue'),
142 function() {
143 chrome.send('oauthEnrollAttributes',
144 [$('oauth-enroll-asset-id').value,
145 $('oauth-enroll-location').value]);
148 return buttons;
152 * Event handler that is invoked just before the frame is shown.
153 * @param {Object} data Screen init payload, contains the signin frame
154 * URL.
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;
168 this.doReload();
169 this.learnMoreHelpTopicID_ = data.learn_more_help_topic_id;
170 this.updateLocalizedContent();
171 this.showStep(STEP_SIGNIN);
175 * Shows attribute-prompt step with pre-filled asset ID and location.
177 showAttributePromptStep: function(annotated_asset_id, annotated_location) {
178 $('oauth-enroll-attribute-prompt-message').innerHTML =
179 loadTimeData.getString('oauthEnrollAttributes');
181 $('oauth-enroll-asset-id').value = annotated_asset_id;
182 $('oauth-enroll-location').value = annotated_location;
184 this.showStep(STEP_ATTRIBUTE_PROMPT);
188 * Cancels enrollment and drops the user back to the login screen.
190 cancel: function() {
191 if (this.isCancelDisabled)
192 return;
193 this.isCancelDisabled = true;
194 chrome.send('oauthEnrollClose', ['cancel']);
198 * Returns whether |step| is the device attribute update error or not.
200 isAttributeUpdateError: function(step) {
201 return this.currentStep_ == STEP_ATTRIBUTE_PROMPT && step == STEP_ERROR;
205 * Shows cancel or done button.
207 hideCancelShowDone: function(hide) {
208 $('oauth-enroll-cancel-button').hidden = hide;
209 $('oauth-enroll-cancel-button').disabled = hide;
211 $('oauth-enroll-done-button').hidden = !hide;
212 $('oauth-enroll-done-button').disabled = !hide;
216 * Switches between the different steps in the enrollment flow.
217 * @param {string} step the steps to show, one of "signin", "working",
218 * "attribute-prompt", "error", "success".
220 showStep: function(step) {
221 var focusStep = step;
223 this.classList.toggle('oauth-enroll-state-' + this.currentStep_, false);
224 this.classList.toggle('oauth-enroll-state-' + step, true);
227 * In case of device attribute update error done button should be shown
228 * instead of cancel button and focus on success,
229 * because enrollment has completed
231 if (this.isAttributeUpdateError(step)) {
232 focusStep = STEP_SUCCESS;
233 this.hideCancelShowDone(true);
234 } else {
235 if (step == STEP_ERROR)
236 this.hideCancelShowDone(false);
239 var focusElements =
240 this.querySelectorAll('.oauth-enroll-focus-on-' + focusStep);
241 for (var i = 0; i < focusElements.length; ++i) {
242 if (getComputedStyle(focusElements[i])['display'] != 'none') {
243 focusElements[i].focus();
244 break;
247 this.currentStep_ = step;
251 * Sets an error message and switches to the error screen.
252 * @param {string} message the error message.
253 * @param {boolean} retry whether the retry link should be shown.
255 showError: function(message, retry) {
256 $('oauth-enroll-error-message').textContent = message;
257 $('oauth-enroll-error-retry').hidden = !retry;
258 this.showStep(STEP_ERROR);
261 doReload: function() {
262 var signInFrame = $('oauth-enroll-signin-frame');
264 var sendParamsOnLoad = function() {
265 signInFrame.removeEventListener('load', sendParamsOnLoad);
266 signInFrame.contentWindow.postMessage(this.signInParams_,
267 'chrome-extension://mfffpogegjflfpflabcdkioaeobkgjik');
268 }.bind(this);
270 signInFrame.addEventListener('load', sendParamsOnLoad);
271 signInFrame.contentWindow.location.href = this.signInUrl_;
275 * Retries the enrollment process after an error occurred in a previous
276 * attempt. This goes to the C++ side through |chrome| first to clean up the
277 * profile, so that the next attempt is performed with a clean state.
279 doRetry_: function() {
280 chrome.send('oauthEnrollRetry');
284 * Checks if a given HTML5 message comes from the URL loaded into the signin
285 * frame.
286 * @param {Object} m HTML5 message.
287 * @type {boolean} whether the message comes from the signin frame.
289 isSigninMessage_: function(m) {
290 return this.signInUrl_ != null &&
291 this.signInUrl_.indexOf(m.origin) == 0 &&
292 m.source == $('oauth-enroll-signin-frame').contentWindow;
296 * Event handler for HTML5 messages.
297 * @param {Object} m HTML5 message.
299 onMessage_: function(m) {
300 if (!this.isSigninMessage_(m))
301 return;
303 var msg = m.data;
305 if (msg.method == 'completeLogin') {
306 // A user has successfully authenticated via regular GAIA or SAML.
307 chrome.send('oauthEnrollCompleteLogin', [msg.email,
308 '' /* auth_code */]);
311 if (msg.method == 'authPageLoaded' && this.currentStep_ == STEP_SIGNIN) {
312 if (msg.isSAML) {
313 $('oauth-saml-notice-message').textContent = loadTimeData.getStringF(
314 'samlNotice',
315 msg.domain);
317 this.classList.toggle('saml', msg.isSAML);
320 if (msg.method == 'resetAuthFlow') {
321 this.classList.remove('saml');
324 if (msg.method == 'loginUILoaded' && this.currentStep_ == STEP_SIGNIN) {
325 this.isCancelDisabled = false;
326 chrome.send('frameLoadingCompleted', [0]);
329 if (msg.method == 'insecureContentBlocked') {
330 this.showError(
331 loadTimeData.getStringF('insecureURLEnrollmentError', msg.url),
332 false);
335 if (msg.method == 'missingGaiaInfo') {
336 this.showError(
337 loadTimeData.getString('fatalEnrollmentError'),
338 false);
343 * Opens the learn more help topic.
345 launchLearnMoreHelp_: function() {
346 if (this.learnMoreHelpTopicID_) {
347 chrome.send('launchHelpApp', [this.learnMoreHelpTopicID_]);