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
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   * 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(
52        false /* useOffline */, data, onAuthCompleted);
53    $('contents').classList.toggle('loading', true);
54  }
55
56  /**
57   * Closes the inline login dialog.
58   */
59  function closeDialog() {
60    chrome.send('DialogClose', ['']);
61  }
62
63  /**
64   * Invoked when failed to get oauth2 refresh token.
65   */
66  function handleOAuth2TokenFailure() {
67    // TODO(xiyuan): Show an error UI.
68    authExtHost.reload();
69    $('contents').classList.toggle('loading', true);
70  }
71
72  return {
73    initialize: initialize,
74    loadAuthExtension: loadAuthExtension,
75    closeDialog: closeDialog,
76    handleOAuth2TokenFailure: handleOAuth2TokenFailure
77  };
78});
79
80document.addEventListener('DOMContentLoaded', inline.login.initialize);
81