| Index: chrome/browser/resources/gaia_auth/main.js
|
| ===================================================================
|
| --- chrome/browser/resources/gaia_auth/main.js (revision 251944)
|
| +++ chrome/browser/resources/gaia_auth/main.js (working copy)
|
| @@ -36,8 +36,7 @@
|
| intputEmail_: undefined,
|
|
|
| isSAMLFlow_: false,
|
| - isSAMLEnabled_: false,
|
| - supportChannel_: null,
|
| + samlSupportChannel_: null,
|
|
|
| GAIA_URL: 'https://accounts.google.com/',
|
| GAIA_PAGE_PATH: 'ServiceLogin?skipvpage=true&sarp=1&rm=hide',
|
| @@ -55,29 +54,15 @@
|
| this.inputEmail_ = params.email;
|
| this.service_ = params.service || this.SERVICE_ID;
|
| this.continueUrl_ = params.continueUrl || this.CONTINUE_URL;
|
| - this.desktopMode_ = params.desktopMode == '1';
|
| - this.isConstrainedWindow_ = params.constrained == '1';
|
| + this.continueUrlWithoutParams_ = stripParams(this.continueUrl_);
|
| + this.inlineMode_ = params.inlineMode == '1';
|
| + this.constrained_ = params.constrained == '1';
|
| + this.partitionId_ = params.partitionId || '';
|
| this.initialFrameUrl_ = params.frameUrl || this.constructInitialFrameUrl_();
|
| this.initialFrameUrlWithoutParams_ = stripParams(this.initialFrameUrl_);
|
| + this.loaded_ = false;
|
|
|
| - if (this.desktopMode_) {
|
| - this.supportChannel_ = new Channel();
|
| - this.supportChannel_.connect('authMain');
|
| -
|
| - this.supportChannel_.send({
|
| - name: 'initDesktopFlow',
|
| - gaiaUrl: this.gaiaUrl_,
|
| - continueUrl: stripParams(this.continueUrl_),
|
| - isConstrainedWindow: this.isConstrainedWindow_
|
| - });
|
| -
|
| - this.supportChannel_.registerMessage(
|
| - 'switchToFullTab', this.switchToFullTab_.bind(this));
|
| - this.supportChannel_.registerMessage(
|
| - 'completeLogin', this.completeLogin_.bind(this));
|
| - }
|
| -
|
| - document.addEventListener('DOMContentLoaded', this.onPageLoad_.bind(this));
|
| + document.addEventListener('DOMContentLoaded', this.onPageLoad.bind(this));
|
| document.addEventListener('enableSAML', this.onEnableSAML_.bind(this));
|
| },
|
|
|
| @@ -104,87 +89,129 @@
|
| url = appendParam(url, 'hl', this.inputLang_);
|
| if (this.inputEmail_)
|
| url = appendParam(url, 'Email', this.inputEmail_);
|
| - if (this.isConstrainedWindow_)
|
| + if (this.constrained_)
|
| url = appendParam(url, 'source', this.CONSTRAINED_FLOW_SOURCE);
|
| return url;
|
| },
|
|
|
| - onPageLoad_: function() {
|
| - window.addEventListener('message', this.onMessage.bind(this), false);
|
| - this.loadFrame_();
|
| - },
|
| -
|
| - loadFrame_: function() {
|
| - var gaiaFrame = $('gaia-frame');
|
| - gaiaFrame.src = this.initialFrameUrl_;
|
| - if (this.desktopMode_) {
|
| - var handler = function() {
|
| - this.onLoginUILoaded_();
|
| - gaiaFrame.removeEventListener('load', handler);
|
| - }.bind(this);
|
| - gaiaFrame.addEventListener('load', handler);
|
| + /** Callback when all loads in the gaia webview is complete. */
|
| + onWebviewLoadstop_: function(gaiaFrame) {
|
| + if (gaiaFrame.src.lastIndexOf(this.continueUrlWithoutParams_, 0) == 0) {
|
| + // Detect when login is finished by the load stop event of the continue
|
| + // URL. Cannot reuse the login complete flow in success.html, because
|
| + // webview does not support extension pages yet.
|
| + var skipForNow = false;
|
| + if (this.inlineMode_ && gaiaFrame.src.indexOf('ntp=1') >= 0) {
|
| + skipForNow = true;
|
| + }
|
| + msg = {
|
| + 'method': 'completeLogin',
|
| + 'skipForNow': skipForNow
|
| + };
|
| + window.parent.postMessage(msg, this.parentPage_);
|
| + // Do no report state to the parent for the continue URL, since it is a
|
| + // blank page.
|
| + return;
|
| }
|
| - },
|
|
|
| - /**
|
| - * Invoked when the login UI is initialized or reset.
|
| - */
|
| - onLoginUILoaded_: function() {
|
| + // Report the current state to the parent which will then update the
|
| + // browser history so that later it could respond properly to back/forward.
|
| var msg = {
|
| - 'method': 'loginUILoaded'
|
| + 'method': 'reportState',
|
| + 'src': gaiaFrame.src
|
| };
|
| window.parent.postMessage(msg, this.parentPage_);
|
| +
|
| + if (gaiaFrame.src.lastIndexOf(this.gaiaUrl_, 0) == 0) {
|
| + gaiaFrame.executeScript({file: 'inline_injected.js'}, function() {
|
| + // Send an initial message to gaia so that it has an JavaScript
|
| + // reference to the embedder.
|
| + gaiaFrame.contentWindow.postMessage('', gaiaFrame.src);
|
| + });
|
| + if (this.constrained_) {
|
| + var preventContextMenu = 'document.addEventListener("contextmenu", ' +
|
| + 'function(e) {e.preventDefault();})';
|
| + gaiaFrame.executeScript({code: preventContextMenu});
|
| + }
|
| + }
|
| +
|
| + this.loaded_ || this.onLoginUILoaded();
|
| },
|
|
|
| /**
|
| - * Invoked when the background script sends a message to indicate that the
|
| - * current content does not fit in a constrained window.
|
| - * @param {Object=} opt_extraMsg Optional extra info to send.
|
| + * Callback when the gaia webview attempts to open a new window.
|
| */
|
| - switchToFullTab_: function(msg) {
|
| - var parentMsg = {
|
| + onWebviewNewWindow_: function(gaiaFrame, e) {
|
| + window.open(e.targetUrl, '_blank');
|
| + e.window.discard();
|
| + },
|
| +
|
| + onWebviewRequestCompleted_: function(details) {
|
| + if (details.url.lastIndexOf(this.continueUrlWithoutParams_, 0) == 0) {
|
| + return;
|
| + }
|
| +
|
| + var headers = details.responseHeaders;
|
| + for (var i = 0; headers && i < headers.length; ++i) {
|
| + if (headers[i].name.toLowerCase() == 'google-accounts-embedded') {
|
| + return;
|
| + }
|
| + }
|
| + var msg = {
|
| 'method': 'switchToFullTab',
|
| - 'url': msg.url
|
| + 'url': details.url
|
| };
|
| - window.parent.postMessage(parentMsg, this.parentPage_);
|
| + window.parent.postMessage(msg, this.parentPage_);
|
| },
|
|
|
| - /**
|
| - * Invoked when the signin flow is complete.
|
| - * @param {Object=} opt_extraMsg Optional extra info to send.
|
| - */
|
| - completeLogin_: function(opt_extraMsg) {
|
| + loadFrame_: function() {
|
| + var gaiaFrame = $('gaia-frame');
|
| + gaiaFrame.partition = this.partitionId_;
|
| + gaiaFrame.src = this.initialFrameUrl_;
|
| + if (this.inlineMode_) {
|
| + gaiaFrame.addEventListener(
|
| + 'loadstop', this.onWebviewLoadstop_.bind(this, gaiaFrame));
|
| + gaiaFrame.addEventListener(
|
| + 'newwindow', this.onWebviewNewWindow_.bind(this, gaiaFrame));
|
| + }
|
| + if (this.constrained_) {
|
| + gaiaFrame.request.onCompleted.addListener(
|
| + this.onWebviewRequestCompleted_.bind(this),
|
| + {urls: ['<all_urls>'], types: ['main_frame']},
|
| + ['responseHeaders']);
|
| + }
|
| + },
|
| +
|
| + completeLogin: function() {
|
| var msg = {
|
| 'method': 'completeLogin',
|
| - 'email': (opt_extraMsg && opt_extraMsg.email) || this.email_,
|
| + 'email': this.email_,
|
| 'password': this.password_,
|
| - 'usingSAML': this.isSAMLFlow_,
|
| - 'chooseWhatToSync': this.chooseWhatToSync_ || false,
|
| - 'skipForNow': opt_extraMsg && opt_extraMsg.skipForNow,
|
| - 'sessionIndex': opt_extraMsg && opt_extraMsg.sessionIndex
|
| + 'usingSAML': this.isSAMLFlow_
|
| };
|
| window.parent.postMessage(msg, this.parentPage_);
|
| - if (this.isSAMLEnabled_)
|
| - this.supportChannel_.send({name: 'resetAuth'});
|
| + if (this.samlSupportChannel_)
|
| + this.samlSupportChannel_.send({name: 'resetAuth'});
|
| },
|
|
|
| + onPageLoad: function(e) {
|
| + window.addEventListener('message', this.onMessage.bind(this), false);
|
| + this.loadFrame_();
|
| + },
|
| +
|
| /**
|
| * Invoked when 'enableSAML' event is received to initialize SAML support.
|
| */
|
| onEnableSAML_: function() {
|
| - this.isSAMLEnabled_ = true;
|
| this.isSAMLFlow_ = false;
|
|
|
| - if (!this.supportChannel_) {
|
| - this.supportChannel_ = new Channel();
|
| - this.supportChannel_.connect('authMain');
|
| - }
|
| -
|
| - this.supportChannel_.registerMessage(
|
| + this.samlSupportChannel_ = new Channel();
|
| + this.samlSupportChannel_.connect('authMain');
|
| + this.samlSupportChannel_.registerMessage(
|
| 'onAuthPageLoaded', this.onAuthPageLoaded_.bind(this));
|
| - this.supportChannel_.registerMessage(
|
| + this.samlSupportChannel_.registerMessage(
|
| 'apiCall', this.onAPICall_.bind(this));
|
| - this.supportChannel_.send({
|
| + this.samlSupportChannel_.send({
|
| name: 'setGaiaUrl',
|
| gaiaUrl: this.gaiaUrl_
|
| });
|
| @@ -232,9 +259,26 @@
|
| }
|
| },
|
|
|
| + onLoginUILoaded: function() {
|
| + var msg = {
|
| + 'method': 'loginUILoaded'
|
| + };
|
| + window.parent.postMessage(msg, this.parentPage_);
|
| + if (this.inlineMode_) {
|
| + // TODO(guohui): temporary workaround until webview team fixes the focus
|
| + // on their side.
|
| + var gaiaFrame = $('gaia-frame');
|
| + gaiaFrame.focus();
|
| + gaiaFrame.onblur = function() {
|
| + gaiaFrame.focus();
|
| + };
|
| + }
|
| + this.loaded_ = true;
|
| + },
|
| +
|
| onConfirmLogin_: function() {
|
| if (!this.isSAMLFlow_) {
|
| - this.completeLogin_();
|
| + this.completeLogin();
|
| return;
|
| }
|
|
|
| @@ -247,7 +291,7 @@
|
| this.parentPage_);
|
|
|
| if (!apiUsed) {
|
| - this.supportChannel_.sendWithCallback(
|
| + this.samlSupportChannel_.sendWithCallback(
|
| {name: 'getScrapedPasswords'},
|
| function(passwords) {
|
| if (passwords.length == 0) {
|
| @@ -268,11 +312,11 @@
|
| // SAML login is complete when the user's e-mail address has been retrieved
|
| // from GAIA and the user has successfully confirmed the password.
|
| if (this.email_ !== null && this.password_ !== null)
|
| - this.completeLogin_();
|
| + this.completeLogin();
|
| },
|
|
|
| onVerifyConfirmedPassword_: function(password) {
|
| - this.supportChannel_.sendWithCallback(
|
| + this.samlSupportChannel_.sendWithCallback(
|
| {name: 'getScrapedPasswords'},
|
| function(passwords) {
|
| for (var i = 0; i < passwords.length; ++i) {
|
| @@ -294,18 +338,17 @@
|
| this.email_ = msg.email;
|
| this.password_ = msg.password;
|
| this.attemptToken_ = msg.attemptToken;
|
| - this.chooseWhatToSync_ = msg.chooseWhatToSync;
|
| this.isSAMLFlow_ = false;
|
| - if (this.isSAMLEnabled_)
|
| - this.supportChannel_.send({name: 'startAuth'});
|
| + if (this.samlSupportChannel_)
|
| + this.samlSupportChannel_.send({name: 'startAuth'});
|
| } else if (msg.method == 'clearOldAttempts' && this.isGaiaMessage_(e)) {
|
| this.email_ = null;
|
| this.password_ = null;
|
| this.attemptToken_ = null;
|
| this.isSAMLFlow_ = false;
|
| - this.onLoginUILoaded_();
|
| - if (this.isSAMLEnabled_)
|
| - this.supportChannel_.send({name: 'resetAuth'});
|
| + this.onLoginUILoaded();
|
| + if (this.samlSupportChannel_)
|
| + this.samlSupportChannel_.send({name: 'resetAuth'});
|
| } else if (msg.method == 'setAuthenticatedUserEmail' &&
|
| this.isParentMessage_(e)) {
|
| if (this.attemptToken_ == msg.attemptToken) {
|
|
|