1 // Copyright (c) 2012 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 var chrome = chrome || {};
8 * Organizes all signin event listeners and asynchronous requests.
9 * This object has no public constructor.
12 chrome.signin = chrome.signin || {};
16 // TODO(vishwath): This function is identical to the one in sync_internals.js
17 // Merge both if possible.
18 // Accepts a DOM node and sets its highlighted attribute oldVal != newVal
19 function highlightIfChanged(node, oldVal, newVal) {
20 var oldStr = oldVal.toString();
21 var newStr = newVal.toString();
22 if (oldStr != '' && oldStr != newStr) {
23 // Note the addListener function does not end up creating duplicate
24 // listeners. There can be only one listener per event at a time.
25 // Reference: https://developer.mozilla.org/en/DOM/element.addEventListener
26 node.addEventListener('webkitAnimationEnd',
27 function() { this.removeAttribute('highlighted'); },
29 node.setAttribute('highlighted', '');
33 // Wraps highlightIfChanged for multiple conditions.
34 function highlightIfAnyChanged(node, oldToNewValList) {
35 for (var i = 0; i < oldToNewValList.length; i++)
36 highlightIfChanged(node, oldToNewValList[i][0], oldToNewValList[i][1]);
39 function setClassFromValue(value) {
42 if (value == 'Successful')
48 // Allow signin_index.html to access the functions above using the
49 // corresponding chrome.signin<method> calls.
50 chrome.signin['highlightIfChanged'] = highlightIfChanged;
51 chrome.signin['highlightIfAnyChanged'] = highlightIfAnyChanged;
52 chrome.signin['setClassFromValue'] = setClassFromValue;
54 // Simplified Event class, borrowed (ok, stolen) from chrome_sync.js
59 // Add a new listener to the list.
60 Event.prototype.addListener = function(listener) {
61 this.listeners_.push(listener);
64 // Remove a listener from the list.
65 Event.prototype.removeListener = function(listener) {
66 var i = this.findListener_(listener);
70 this.listeners_.splice(i, 1);
73 // Check if the listener has already been registered so we can prevent
74 // duplicate registrations.
75 Event.prototype.hasListener = function(listener) {
76 return this.findListener_(listener) > -1;
79 // Are there any listeners registered yet?
80 Event.prototype.hasListeners = function() {
81 return this.listeners_.length > 0;
84 // Returns the index of the given listener, or -1 if not found.
85 Event.prototype.findListener_ = function(listener) {
86 for (var i = 0; i < this.listeners_.length; i++) {
87 if (this.listeners_[i] == listener) {
94 // Fires the event. Called by the actual event callback. Any
95 // exceptions thrown by a listener are caught and logged.
96 Event.prototype.fire = function() {
97 var args = Array.prototype.slice.call(arguments);
98 for (var i = 0; i < this.listeners_.length; i++) {
100 this.listeners_[i].apply(null, args);
102 if (e instanceof Error) {
103 // Non-standard, but useful.
104 console.error(e.stack);
112 // These are the events that will be registered.
113 chrome.signin.events = {
115 'onSigninInfoChanged',
116 'onCookieAccountsFetched'
120 for (var eventType in chrome.signin.events) {
121 var events = chrome.signin.events[eventType];
122 for (var i = 0; i < events.length; ++i) {
123 var event = events[i];
124 chrome.signin[event] = new Event();
128 // Creates functions that call into SigninInternalsUI.
129 function makeSigninFunction(name) {
132 // Calls the function, assuming the last argument is a callback to be
133 // called with the return value.
134 var fn = function() {
135 var args = Array.prototype.slice.call(arguments);
136 callbacks.push(args.pop());
137 chrome.send(name, args);
140 // Handle a reply, assuming that messages are processed in FIFO order.
141 // Called by SigninInternalsUI::HandleJsReply().
142 fn.handleReply = function() {
143 var args = Array.prototype.slice.call(arguments);
144 // Remove the callback before we call it since the callback may
146 var callback = callbacks.shift();
147 callback.apply(null, args);
153 // The list of js functions that call into SigninInternalsUI
154 var signinFunctions = [
155 // Signin Summary Info
159 for (var i = 0; i < signinFunctions.length; ++i) {
160 var signinFunction = signinFunctions[i];
161 chrome.signin[signinFunction] = makeSigninFunction(signinFunction);
164 chrome.signin.internalsInfo = {};
166 // Replace the displayed values with the latest fetched ones.
167 function refreshSigninInfo(signinInfo) {
168 chrome.signin.internalsInfo = signinInfo;
169 jstProcess(new JsEvalContext(signinInfo), $('signin-info'));
170 jstProcess(new JsEvalContext(signinInfo), $('token-info'));
173 // Replace the cookie information with the fetched values.
174 function updateCookieAccounts(cookieAccountsInfo) {
175 jstProcess(new JsEvalContext(cookieAccountsInfo), $('cookie-info'));
178 // On load, do an initial refresh and register refreshSigninInfo to be invoked
179 // whenever we get new signin information from SigninInternalsUI.
181 chrome.signin.getSigninInfo(refreshSigninInfo);
183 chrome.signin.onSigninInfoChanged.addListener(refreshSigninInfo);
184 chrome.signin.onCookieAccountsFetched.addListener(updateCookieAccounts);
187 document.addEventListener('DOMContentLoaded', onLoad, false);