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 || {};
7 // TODO(akalin): Add mocking code for e.g. chrome.send() so that we
8 // can test this without rebuilding chrome.
11 * Organize sync event listeners and asynchronous requests.
12 * This object is one of a kind; its constructor is not public.
15 chrome.sync = chrome.sync || {};
18 // This Event class is a simplified version of the one from
24 Event.prototype.addListener = function(listener) {
25 this.listeners_.push(listener);
28 Event.prototype.removeListener = function(listener) {
29 var i = this.findListener_(listener);
33 this.listeners_.splice(i, 1);
36 Event.prototype.removeListeners = function() {
40 Event.prototype.hasListener = function(listener) {
41 return this.findListener_(listener) > -1;
44 Event.prototype.hasListeners = function(listener) {
45 return this.listeners_.length > 0;
48 // Returns the index of the given listener, or -1 if not found.
49 Event.prototype.findListener_ = function(listener) {
50 for (var i = 0; i < this.listeners_.length; i++) {
51 if (this.listeners_[i] == listener) {
58 // Fires the event. Called by the actual event callback. Any
59 // exceptions thrown by a listener are caught and logged.
60 Event.prototype.fire = function() {
61 var args = Array.prototype.slice.call(arguments);
62 for (var i = 0; i < this.listeners_.length; i++) {
64 this.listeners_[i].apply(null, args);
66 if (e instanceof Error) {
67 // Non-standard, but useful.
68 console.error(e.stack);
76 chrome.sync.events = {
78 'onServiceStateChanged'
81 // See chrome/browser/sync/engine/syncapi.h for docs.
83 'onNotificationStateChange',
84 'onIncomingNotification'
90 'onSyncCycleCompleted',
91 'onConnectionStatusChange',
92 'onPassphraseRequired',
93 'onPassphraseAccepted',
94 'onInitializationComplete',
95 'onStopSyncingPermanently',
96 'onClearServerDataSucceeded',
97 'onClearServerDataFailed',
98 'onEncryptedTypesChanged',
99 'onEncryptionComplete',
104 'onTransactionWrite',
108 for (var eventType in chrome.sync.events) {
109 var events = chrome.sync.events[eventType];
110 for (var i = 0; i < events.length; ++i) {
111 var event = events[i];
112 chrome.sync[event] = new Event();
116 function makeSyncFunction(name) {
119 // Calls the function, assuming the last argument is a callback to be
120 // called with the return value.
121 var fn = function() {
122 var args = Array.prototype.slice.call(arguments);
123 callbacks.push(args.pop());
124 chrome.send(name, args);
127 // Handle a reply, assuming that messages are processed in FIFO order.
128 // Called by SyncInternalsUI::HandleJsReply().
129 fn.handleReply = function() {
130 var args = Array.prototype.slice.call(arguments);
131 // Remove the callback before we call it since the callback may
133 var callback = callbacks.shift();
134 callback.apply(null, args);
140 var syncFunctions = [
141 // Sync service functions.
144 // Notification functions. See chrome/browser/sync/engine/syncapi.h
146 'getNotificationState',
147 'getNotificationInfo',
149 // Get a static list of available data types.
152 // Client server communication logging functions.
153 'getClientServerTraffic',
155 // Get an array containing a JSON representations of all known sync nodes.
159 for (var i = 0; i < syncFunctions.length; ++i) {
160 var syncFunction = syncFunctions[i];
161 chrome.sync[syncFunction] = makeSyncFunction(syncFunction);
165 * Returns an object which measures elapsed time.
167 chrome.sync.makeTimer = function() {
168 var start = new Date();
172 * @return {number} The number of seconds since the timer was
175 get elapsedSeconds() {
176 return ((new Date()).getTime() - start.getTime()) / 1000.0;