Roll src/third_party/WebKit bf18a82:a9cee16 (svn 185297:185304)
[chromium-blink-merge.git] / chrome / browser / resources / inline_login / inline_login.js
blobfe41e0b1b937a3773efdbc53899b3c4932a42a7a
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.
5 /**
6 * @fileoverview Inline login UI.
7 */
9 cr.define('inline.login', function() {
10 'use strict';
12 /**
13 * The auth extension host instance.
14 * @type {cr.login.GaiaAuthHost}
16 var authExtHost;
18 /**
19 * Whether the auth ready event has been fired, for testing purpose.
21 var authReadyFired;
23 /**
24 * A listener class for authentication events from GaiaAuthHost.
25 * @constructor
26 * @implements {cr.login.GaiaAuthHost.Listener}
28 function GaiaAuthHostListener() {}
30 /** @override */
31 GaiaAuthHostListener.prototype.onSuccess = function(credentials) {
32 onAuthCompleted(credentials);
35 /** @override */
36 GaiaAuthHostListener.prototype.onReady = function(e) {
37 onAuthReady();
40 /** @override */
41 GaiaAuthHostListener.prototype.onResize = function(url) {
42 chrome.send('switchToFullTab', url);
45 /** @override */
46 GaiaAuthHostListener.prototype.onNewWindow = function(e) {
47 window.open(e.targetUrl, '_blank');
48 e.window.discard();
51 /**
52 * Handler of auth host 'ready' event.
54 function onAuthReady() {
55 $('contents').classList.toggle('loading', false);
56 authReadyFired = true;
59 /**
60 * Handler of auth host 'completed' event.
61 * @param {!Object} credentials Credentials of the completed authentication.
63 function onAuthCompleted(credentials) {
64 chrome.send('completeLogin', [credentials]);
65 $('contents').classList.toggle('loading', true);
68 /**
69 * Initialize the UI.
71 function initialize() {
72 authExtHost = new cr.login.GaiaAuthHost(
73 'signin-frame', new GaiaAuthHostListener());
74 authExtHost.addEventListener('ready', onAuthReady);
76 chrome.send('initialize');
79 /**
80 * Loads auth extension.
81 * @param {Object} data Parameters for auth extension.
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');
90 /**
91 * Closes the inline login dialog.
93 function closeDialog() {
94 chrome.send('dialogClose', ['']);
97 /**
98 * Invoked when failed to get oauth2 refresh token.
100 function handleOAuth2TokenFailure() {
101 // TODO(xiyuan): Show an error UI.
102 authExtHost.reload();
103 $('contents').classList.toggle('loading', true);
107 * Returns the auth host instance, for testing purpose.
109 function getAuthExtHost() {
110 return authExtHost;
114 * Returns whether the auth UI is ready, for testing purpose.
116 function isAuthReady() {
117 return authReadyFired;
120 return {
121 getAuthExtHost: getAuthExtHost,
122 isAuthReady: isAuthReady,
123 initialize: initialize,
124 loadAuthExtension: loadAuthExtension,
125 closeDialog: closeDialog,
126 handleOAuth2TokenFailure: handleOAuth2TokenFailure
130 document.addEventListener('DOMContentLoaded', inline.login.initialize);