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. 4 5/** 6 * @fileoverview Inline login UI. 7 */ 8 9<include src="../gaia_auth_host/gaia_auth_host.js"> 10 11cr.define('inline.login', function() { 12 'use strict'; 13 14 /** 15 * The auth extension host instance. 16 * @type {Object} 17 */ 18 var authExtHost; 19 20 /** 21 * Whether the auth ready event has been fired, for testing purpose. 22 */ 23 var authReadyFired; 24 25 /** 26 * Handler of auth host 'ready' event. 27 */ 28 function onAuthReady() { 29 $('contents').classList.toggle('loading', false); 30 authReadyFired = true; 31 } 32 33 /** 34 * Handler of auth host 'completed' event. 35 * @param {!Object} credentials Credentials of the completed authentication. 36 */ 37 function onAuthCompleted(credentials) { 38 chrome.send('completeLogin', [credentials]); 39 $('contents').classList.toggle('loading', true); 40 } 41 42 /** 43 * Initialize the UI. 44 */ 45 function initialize() { 46 authExtHost = new cr.login.GaiaAuthHost('signin-frame'); 47 authExtHost.addEventListener('ready', onAuthReady); 48 49 chrome.send('initialize'); 50 } 51 52 /** 53 * Loads auth extension. 54 * @param {Object} data Parameters for auth extension. 55 */ 56 function loadAuthExtension(data) { 57 authExtHost.load(data.authMode, data, onAuthCompleted); 58 $('contents').classList.toggle('loading', 59 data.authMode != cr.login.GaiaAuthHost.AuthMode.DESKTOP || 60 data.constrained == '1'); 61 } 62 63 /** 64 * Closes the inline login dialog. 65 */ 66 function closeDialog() { 67 chrome.send('dialogClose', ['']); 68 } 69 70 /** 71 * Invoked when failed to get oauth2 refresh token. 72 */ 73 function handleOAuth2TokenFailure() { 74 // TODO(xiyuan): Show an error UI. 75 authExtHost.reload(); 76 $('contents').classList.toggle('loading', true); 77 } 78 79 /** 80 * Returns the auth host instance, for testing purpose. 81 */ 82 function getAuthExtHost() { 83 return authExtHost; 84 } 85 86 /** 87 * Returns whether the auth UI is ready, for testing purpose. 88 */ 89 function isAuthReady() { 90 return authReadyFired; 91 } 92 93 return { 94 getAuthExtHost: getAuthExtHost, 95 isAuthReady: isAuthReady, 96 initialize: initialize, 97 loadAuthExtension: loadAuthExtension, 98 closeDialog: closeDialog, 99 handleOAuth2TokenFailure: handleOAuth2TokenFailure 100 }; 101}); 102 103document.addEventListener('DOMContentLoaded', inline.login.initialize); 104