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