Upstream version 7.35.139.0
[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    * 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   }
61
62   /**
63    * Closes the inline login dialog.
64    */
65   function closeDialog() {
66     chrome.send('DialogClose', ['']);
67   }
68
69   /**
70    * Invoked when failed to get oauth2 refresh token.
71    */
72   function handleOAuth2TokenFailure() {
73     // TODO(xiyuan): Show an error UI.
74     authExtHost.reload();
75     $('contents').classList.toggle('loading', true);
76   }
77
78   /**
79    * Returns the auth host instance, for testing purpose.
80    */
81   function getAuthExtHost() {
82     return authExtHost;
83   }
84
85   /**
86    * Returns whether the auth UI is ready, for testing purpose.
87    */
88   function isAuthReady() {
89     return authReadyFired;
90   }
91
92   return {
93     getAuthExtHost: getAuthExtHost,
94     isAuthReady: isAuthReady,
95     initialize: initialize,
96     loadAuthExtension: loadAuthExtension,
97     closeDialog: closeDialog,
98     handleOAuth2TokenFailure: handleOAuth2TokenFailure
99   };
100 });
101
102 document.addEventListener('DOMContentLoaded', inline.login.initialize);