1 // Copyright 2013 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.
6 * @fileoverview Inline login UI.
9 cr.define('inline.login', function() {
13 * The auth extension host instance.
14 * @type {cr.login.GaiaAuthHost}
19 * Whether the auth ready event has been fired, for testing purpose.
24 * A listener class for authentication events from GaiaAuthHost.
26 * @implements {cr.login.GaiaAuthHost.Listener}
28 function GaiaAuthHostListener() {}
31 GaiaAuthHostListener.prototype.onSuccess = function(credentials) {
32 onAuthCompleted(credentials);
36 GaiaAuthHostListener.prototype.onReady = function(e) {
41 GaiaAuthHostListener.prototype.onResize = function(url) {
42 chrome.send('switchToFullTab', url);
46 GaiaAuthHostListener.prototype.onNewWindow = function(e) {
47 window.open(e.targetUrl, '_blank');
52 * Handler of auth host 'ready' event.
54 function onAuthReady() {
55 $('contents').classList.toggle('loading', false);
56 authReadyFired = true;
60 * Handler of auth host 'completed' event.
61 * @param {!Object} credentials Credentials of the completed authentication.
63 function onAuthCompleted(credentials) {
64 chrome.send('completeLogin', [credentials]);
65 $('contents').classList.toggle('loading', true);
71 function initialize() {
72 authExtHost = new cr.login.GaiaAuthHost(
73 'signin-frame', new GaiaAuthHostListener());
74 authExtHost.addEventListener('ready', onAuthReady);
76 chrome.send('initialize');
80 * Loads auth extension.
81 * @param {Object} data Parameters for auth extension.
83 function loadAuthExtension(data) {
84 authExtHost.load(data.authMode, data, onAuthCompleted);
85 $('contents').classList.toggle('loading',
86 data.authMode != cr.login.GaiaAuthHost.AuthMode.DESKTOP ||
87 data.constrained == '1');
91 * Closes the inline login dialog.
93 function closeDialog() {
94 chrome.send('dialogClose', ['']);
98 * Invoked when failed to get oauth2 refresh token.
100 function handleOAuth2TokenFailure() {
101 // TODO(xiyuan): Show an error UI.
102 authExtHost.reload();
103 $('contents').classList.toggle('loading', true);
107 * Returns the auth host instance, for testing purpose.
109 function getAuthExtHost() {
114 * Returns whether the auth UI is ready, for testing purpose.
116 function isAuthReady() {
117 return authReadyFired;
121 getAuthExtHost: getAuthExtHost,
122 isAuthReady: isAuthReady,
123 initialize: initialize,
124 loadAuthExtension: loadAuthExtension,
125 closeDialog: closeDialog,
126 handleOAuth2TokenFailure: handleOAuth2TokenFailure
130 document.addEventListener('DOMContentLoaded', inline.login.initialize);