Update To 11.40.268.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 cr.define('inline.login', function() {
10   'use strict';
11
12   /**
13    * The auth extension host instance.
14    * @type {cr.login.GaiaAuthHost}
15    */
16   var authExtHost;
17
18   /**
19    * Whether the auth ready event has been fired, for testing purpose.
20    */
21   var authReadyFired;
22
23   /**
24    * A listener class for authentication events from GaiaAuthHost.
25    * @constructor
26    * @implements {cr.login.GaiaAuthHost.Listener}
27    */
28   function GaiaAuthHostListener() {}
29
30   /** @override */
31   GaiaAuthHostListener.prototype.onSuccess = function(credentials) {
32     onAuthCompleted(credentials);
33   };
34
35   /** @override */
36   GaiaAuthHostListener.prototype.onReady = function(e) {
37     onAuthReady();
38   };
39
40   /** @override */
41   GaiaAuthHostListener.prototype.onResize = function(url) {
42     chrome.send('switchToFullTab', url);
43   };
44
45   /** @override */
46   GaiaAuthHostListener.prototype.onNewWindow = function(e) {
47     window.open(e.targetUrl, '_blank');
48     e.window.discard();
49   };
50
51   /**
52    * Handler of auth host 'ready' event.
53    */
54   function onAuthReady() {
55     $('contents').classList.toggle('loading', false);
56     authReadyFired = true;
57   }
58
59   /**
60    * Handler of auth host 'completed' event.
61    * @param {!Object} credentials Credentials of the completed authentication.
62    */
63   function onAuthCompleted(credentials) {
64     chrome.send('completeLogin', [credentials]);
65     $('contents').classList.toggle('loading', true);
66   }
67
68   /**
69    * Initialize the UI.
70    */
71   function initialize() {
72     authExtHost = new cr.login.GaiaAuthHost(
73         'signin-frame', new GaiaAuthHostListener());
74     authExtHost.addEventListener('ready', onAuthReady);
75
76     chrome.send('initialize');
77   }
78
79   /**
80    * Loads auth extension.
81    * @param {Object} data Parameters for auth extension.
82    */
83   function loadAuthExtension(data) {
84     authExtHost.load(data.authMode, data, onAuthCompleted);
85     $('contents').classList.toggle('loading',
86         data.authMode != cr.login.GaiaAuthHost.AuthMode.DESKTOP ||
87         data.constrained == '1');
88   }
89
90   /**
91    * Closes the inline login dialog.
92    */
93   function closeDialog() {
94     chrome.send('dialogClose', ['']);
95   }
96
97   /**
98    * Invoked when failed to get oauth2 refresh token.
99    */
100   function handleOAuth2TokenFailure() {
101     // TODO(xiyuan): Show an error UI.
102     authExtHost.reload();
103     $('contents').classList.toggle('loading', true);
104   }
105
106   /**
107    * Returns the auth host instance, for testing purpose.
108    */
109   function getAuthExtHost() {
110     return authExtHost;
111   }
112
113   /**
114    * Returns whether the auth UI is ready, for testing purpose.
115    */
116   function isAuthReady() {
117     return authReadyFired;
118   }
119
120   return {
121     getAuthExtHost: getAuthExtHost,
122     isAuthReady: isAuthReady,
123     initialize: initialize,
124     loadAuthExtension: loadAuthExtension,
125     closeDialog: closeDialog,
126     handleOAuth2TokenFailure: handleOAuth2TokenFailure
127   };
128 });
129
130 document.addEventListener('DOMContentLoaded', inline.login.initialize);