Home | History | Annotate | Download | only in inline_login
      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"></include>
     10 
     11 cr.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    * Handler of auth host 'ready' event.
     22    */
     23   function onAuthReady() {
     24     $('contents').classList.toggle('loading', false);
     25   }
     26 
     27   /**
     28    * Handler of auth host 'completed' event.
     29    * @param {!Object} credentials Credentials of the completed authentication.
     30    */
     31   function onAuthCompleted(credentials) {
     32     chrome.send('completeLogin', [credentials]);
     33     $('contents').classList.toggle('loading', true);
     34   }
     35 
     36   /**
     37    * Initialize the UI.
     38    */
     39   function initialize() {
     40     authExtHost = new cr.login.GaiaAuthHost('signin-frame');
     41     authExtHost.addEventListener('ready', onAuthReady);
     42 
     43     chrome.send('initialize');
     44   }
     45 
     46   /**
     47    * Loads auth extension.
     48    * @param {Object} data Parameters for auth extension.
     49    */
     50   function loadAuthExtension(data) {
     51     authExtHost.load(data.authMode, data, onAuthCompleted);
     52     // Do not show loading spinner to give user a faster response
     53     // with inline flows.
     54     $('contents').classList.toggle('loading',
     55         data.authMode != cr.login.GaiaAuthHost.AuthMode.INLINE);
     56   }
     57 
     58   /**
     59    * Closes the inline login dialog.
     60    */
     61   function closeDialog() {
     62     chrome.send('DialogClose', ['']);
     63   }
     64 
     65   /**
     66    * Invoked when failed to get oauth2 refresh token.
     67    */
     68   function handleOAuth2TokenFailure() {
     69     // TODO(xiyuan): Show an error UI.
     70     authExtHost.reload();
     71     $('contents').classList.toggle('loading', true);
     72   }
     73 
     74   return {
     75     initialize: initialize,
     76     loadAuthExtension: loadAuthExtension,
     77     closeDialog: closeDialog,
     78     handleOAuth2TokenFailure: handleOAuth2TokenFailure
     79   };
     80 });
     81 
     82 document.addEventListener('DOMContentLoaded', inline.login.initialize);
     83