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.hasListener = function(listener) {
37 return this.findListener_(listener) > -1;
40 Event.prototype.hasListeners = function(listener) {
41 return this.listeners_.length > 0;
44 // Returns the index of the given listener, or -1 if not found.
45 Event.prototype.findListener_ = function(listener) {
46 for (var i = 0; i < this.listeners_.length; i++) {
47 if (this.listeners_[i] == listener) {
54 // Fires the event. Called by the actual event callback. Any
55 // exceptions thrown by a listener are caught and logged.
56 Event.prototype.fire = function() {
57 var args = Array.prototype.slice.call(arguments);
58 for (var i = 0; i < this.listeners_.length; i++) {
60 this.listeners_[i].apply(null, args);
62 if (e instanceof Error) {
63 // Non-standard, but useful.
64 console.error(e.stack);
72 chrome.sync.events = {
74 'onServiceStateChanged'
77 // See chrome/browser/sync/engine/syncapi.h for docs.
79 'onNotificationStateChange',
80 'onIncomingNotification'
86 'onSyncCycleCompleted',
87 'onConnectionStatusChange',
88 'onPassphraseRequired',
89 'onPassphraseAccepted',
90 'onInitializationComplete',
91 'onStopSyncingPermanently',
92 'onClearServerDataSucceeded',
93 'onClearServerDataFailed',
94 'onEncryptedTypesChanged',
95 'onEncryptionComplete',
100 'onTransactionWrite',
104 for (var eventType in chrome.sync.events) {
105 var events = chrome.sync.events[eventType];
106 for (var i = 0; i < events.length; ++i) {
107 var event = events[i];
108 chrome.sync[event] = new Event();
112 function makeSyncFunction(name) {
115 // Calls the function, assuming the last argument is a callback to be
116 // called with the return value.
117 var fn = function() {
118 var args = Array.prototype.slice.call(arguments);
119 callbacks.push(args.pop());
120 chrome.send(name, args);
123 // Handle a reply, assuming that messages are processed in FIFO order.
124 // Called by SyncInternalsUI::HandleJsReply().
125 fn.handleReply = function() {
126 var args = Array.prototype.slice.call(arguments);
127 // Remove the callback before we call it since the callback may
129 var callback = callbacks.shift();
130 callback.apply(null, args);
136 var syncFunctions = [
137 // Sync service functions.
140 // Notification functions. See chrome/browser/sync/engine/syncapi.h
142 'getNotificationState',
143 'getNotificationInfo',
145 // Client server communication logging functions.
146 'getClientServerTraffic',
148 // Node lookup functions. See chrome/browser/sync/engine/syncapi.h
150 'getRootNodeDetails',
151 'getNodeSummariesById',
152 'getNodeDetailsById',
157 for (var i = 0; i < syncFunctions.length; ++i) {
158 var syncFunction = syncFunctions[i];
159 chrome.sync[syncFunction] = makeSyncFunction(syncFunction);
163 * Returns an object which measures elapsed time.
165 chrome.sync.makeTimer = function() {
166 var start = new Date();
170 * @return {number} The number of seconds since the timer was
173 get elapsedSeconds() {
174 return ((new Date()).getTime() - start.getTime()) / 1000.0;