OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2011 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 cr.define('oobe', function() { |
| 6 /** |
| 7 * Creates a new oobe screen div. |
| 8 * @constructor |
| 9 * @extends {HTMLDivElement} |
| 10 */ |
| 11 var OAuthEnrollmentScreen = cr.ui.define('div'); |
| 12 |
| 13 /** |
| 14 * Registers with Oobe. |
| 15 */ |
| 16 OAuthEnrollmentScreen.register = function() { |
| 17 var screen = $('oauth-enrollment'); |
| 18 OAuthEnrollmentScreen.decorate(screen); |
| 19 Oobe.getInstance().registerScreen(screen); |
| 20 window.addEventListener('message', |
| 21 screen.onMessage_.bind(screen), false); |
| 22 }; |
| 23 |
| 24 /** |
| 25 * Switches between the different steps in the enrollment flow. |
| 26 * @param screen {string} the steps to show, one of "signin", "working", |
| 27 * "error", "success". |
| 28 */ |
| 29 OAuthEnrollmentScreen.showStep = function(step) { |
| 30 $('oauth-enrollment').showStep(step); |
| 31 }; |
| 32 |
| 33 /** |
| 34 * Sets an error message and switches to the error screen. |
| 35 * @param message {string} the error message. |
| 36 * @param retry {bool} whether the retry link should be shown. |
| 37 */ |
| 38 OAuthEnrollmentScreen.showError = function(message, retry) { |
| 39 $('oauth-enrollment').showError(message, retry); |
| 40 }; |
| 41 |
| 42 OAuthEnrollmentScreen.prototype = { |
| 43 __proto__: HTMLDivElement.prototype, |
| 44 |
| 45 /** |
| 46 * URL to load in the sign in frame. |
| 47 */ |
| 48 signin_url_ : null, |
| 49 |
| 50 /** |
| 51 * Enrollment steps with names and buttons to show. |
| 52 */ |
| 53 steps_ : [ |
| 54 { name: 'signin', |
| 55 button: 'cancel' }, |
| 56 { name: 'working', |
| 57 button: 'cancel' }, |
| 58 { name: 'error', |
| 59 button: 'cancel' }, |
| 60 { name: 'success', |
| 61 button: 'done' } |
| 62 ], |
| 63 |
| 64 /** @inheritDoc */ |
| 65 decorate: function() { |
| 66 $('oauth-enroll-error-retry').addEventListener('click', function() { |
| 67 chrome.send('oauthEnrollRetry', []); |
| 68 }); |
| 69 }, |
| 70 |
| 71 /** |
| 72 * Header text of the screen. |
| 73 * @type {string} |
| 74 */ |
| 75 get header() { |
| 76 return localStrings.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 = |
| 89 localStrings.getString('oauthEnrollCancel'); |
| 90 cancelButton.addEventListener('click', function(e) { |
| 91 chrome.send('oauthEnrollClose', []); |
| 92 }); |
| 93 buttons.push(cancelButton); |
| 94 |
| 95 var doneButton = this.ownerDocument.createElement('button'); |
| 96 doneButton.id = 'oauth-enroll-done-button'; |
| 97 doneButton.hidden = true; |
| 98 doneButton.textContent = |
| 99 localStrings.getString('oauthEnrollDone'); |
| 100 doneButton.addEventListener('click', function(e) { |
| 101 chrome.send('oauthEnrollClose', []); |
| 102 }); |
| 103 buttons.push(doneButton); |
| 104 |
| 105 return buttons; |
| 106 }, |
| 107 |
| 108 /** |
| 109 * Event handler that is invoked just before the frame is shown. |
| 110 * @param data {dictionary} Screen init payload, contains the signin frame |
| 111 * URL. |
| 112 */ |
| 113 onBeforeShow: function(data) { |
| 114 this.signin_url_ = data.signin_url; |
| 115 $('oauth-enroll-signin-frame').contentWindow.location.href = |
| 116 this.signin_url_; |
| 117 this.showStep('signin'); |
| 118 }, |
| 119 |
| 120 /** |
| 121 * Switches between the different steps in the enrollment flow. |
| 122 * @param screen {string} the steps to show, one of "signin", "working", |
| 123 * "error", "success". |
| 124 */ |
| 125 showStep: function(step) { |
| 126 $('oauth-enroll-cancel-button').hidden = true; |
| 127 $('oauth-enroll-done-button').hidden = true; |
| 128 for (var i = 0; i < this.steps_.length; i++) { |
| 129 var the_step = this.steps_[i]; |
| 130 var active = (the_step.name == step); |
| 131 $('oauth-enroll-step-' + the_step.name).hidden = !active; |
| 132 if (active) |
| 133 $('oauth-enroll-' + the_step.button + '-button').hidden = false; |
| 134 } |
| 135 }, |
| 136 |
| 137 /** |
| 138 * Sets an error message and switches to the error screen. |
| 139 * @param message {string} the error message. |
| 140 * @param retry {bool} whether the retry link should be shown. |
| 141 */ |
| 142 showError: function(message, retry) { |
| 143 $('oauth-enroll-error-message').textContent = message; |
| 144 $('oauth-enroll-error-retry').hidden = !retry; |
| 145 this.showStep('error'); |
| 146 }, |
| 147 |
| 148 /** |
| 149 * Checks if a given HTML5 message comes from the URL loaded into the signin |
| 150 * frame. |
| 151 * @param m {object} HTML5 message. |
| 152 * @type {bool} whether the message comes from the signin frame. |
| 153 */ |
| 154 isSigninMessage_: function(m) { |
| 155 return this.signin_url_ != null && |
| 156 this.signin_url_.indexOf(m.origin) == 0 && |
| 157 m.source == $('oauth-enroll-signin-frame').contentWindow; |
| 158 }, |
| 159 |
| 160 /** |
| 161 * Event handler for HTML5 messages. |
| 162 * @param m {object} HTML5 message. |
| 163 */ |
| 164 onMessage_: function(m) { |
| 165 var msg = m.data; |
| 166 if (msg.method == 'completeLogin' && this.isSigninMessage_(m)) |
| 167 chrome.send('oauthEnrollCompleteLogin', [ msg.email, msg.password ]); |
| 168 } |
| 169 }; |
| 170 |
| 171 return { |
| 172 OAuthEnrollmentScreen: OAuthEnrollmentScreen |
| 173 }; |
| 174 }); |
OLD | NEW |