OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 var chrome = chrome || {}; | 5 // require cr.js |
6 // require cr/event_target.js | |
7 // requier cr/util.js | |
6 | 8 |
7 // TODO(akalin): Add mocking code for e.g. chrome.send() so that we | 9 cr.define('chrome.sync', function() { |
8 // can test this without rebuilding chrome. | 10 'use strict'; |
9 | 11 |
10 /** | 12 function makeSyncFunction(name) { |
11 * Organize sync event listeners and asynchronous requests. | 13 var callbacks = []; |
12 * This object is one of a kind; its constructor is not public. | |
13 * @type {Object} | |
14 */ | |
15 chrome.sync = chrome.sync || {}; | |
16 (function() { | |
17 | 14 |
18 // This Event class is a simplified version of the one from | 15 // Calls the function, assuming the last argument is a callback to be |
19 // event_bindings.js. | 16 // called with the return value. |
20 function Event() { | 17 var fn = function() { |
21 this.listeners_ = []; | 18 var args = Array.prototype.slice.call(arguments); |
22 } | 19 callbacks.push(args.pop()); |
20 chrome.send(name, args); | |
21 }; | |
23 | 22 |
24 Event.prototype.addListener = function(listener) { | 23 // Handle a reply, assuming that messages are processed in FIFO order. |
Dan Beam
2014/02/11 23:21:48
^ this is an odd assumption
rlarocque
2014/02/12 00:39:55
I agree.
Somehow we've been able to get away with
| |
25 this.listeners_.push(listener); | 24 // Called by SyncInternalsUI::HandleJsReply(). |
26 }; | 25 fn.handleReply = function() { |
26 var args = Array.prototype.slice.call(arguments); | |
27 // Remove the callback before we call it since the callback may | |
28 // throw. | |
29 var callback = callbacks.shift(); | |
30 callback.apply(null, args); | |
31 }; | |
27 | 32 |
28 Event.prototype.removeListener = function(listener) { | 33 return fn; |
29 var i = this.findListener_(listener); | |
30 if (i == -1) { | |
31 return; | |
32 } | 34 } |
33 this.listeners_.splice(i, 1); | |
34 }; | |
35 | 35 |
36 Event.prototype.removeListeners = function() { | 36 var syncFunctions = [ |
37 this.listeners_ = []; | 37 // Sync service functions. |
38 } | 38 'getAboutInfo', |
39 | 39 |
40 Event.prototype.hasListener = function(listener) { | 40 // Notification functions. See chrome/browser/sync/engine/syncapi.h |
41 return this.findListener_(listener) > -1; | 41 // for docs. |
42 }; | 42 'getNotificationState', |
43 'getNotificationInfo', | |
43 | 44 |
44 Event.prototype.hasListeners = function(listener) { | 45 // Get a static list of available data types. |
45 return this.listeners_.length > 0; | 46 'getListOfTypes', |
46 }; | |
47 | 47 |
48 // Returns the index of the given listener, or -1 if not found. | 48 // Client server communication logging functions. |
49 Event.prototype.findListener_ = function(listener) { | 49 'getClientServerTraffic', |
50 for (var i = 0; i < this.listeners_.length; i++) { | 50 |
51 if (this.listeners_[i] == listener) { | 51 // Get an array containing a JSON representations of all known sync nodes. |
52 return i; | 52 'getAllNodes', |
53 } | 53 ]; |
54 | |
55 for (var i = 0; i < syncFunctions.length; ++i) { | |
56 var syncFunction = syncFunctions[i]; | |
57 chrome.sync[syncFunction] = makeSyncFunction(syncFunction); | |
54 } | 58 } |
55 return -1; | |
56 }; | |
57 | 59 |
58 // Fires the event. Called by the actual event callback. Any | 60 /** |
59 // exceptions thrown by a listener are caught and logged. | 61 * Returns an object which measures elapsed time. |
60 Event.prototype.fire = function() { | 62 */ |
61 var args = Array.prototype.slice.call(arguments); | 63 chrome.sync.makeTimer = function() { |
62 for (var i = 0; i < this.listeners_.length; i++) { | 64 var start = new Date(); |
Dan Beam
2014/02/11 23:21:48
var start = Date.now();
rlarocque
2014/02/12 00:39:55
Done.
| |
63 try { | 65 |
64 this.listeners_[i].apply(null, args); | 66 return { |
65 } catch (e) { | 67 /** |
66 if (e instanceof Error) { | 68 * @return {number} The number of seconds since the timer was |
67 // Non-standard, but useful. | 69 * created. |
68 console.error(e.stack); | 70 */ |
69 } else { | 71 get elapsedSeconds() { |
70 console.error(e); | 72 return ((new Date()).getTime() - start.getTime()) / 1000.0; |
Dan Beam
2014/02/11 23:21:48
FYI: all numbers in JavaScript are doubles
return
rlarocque
2014/02/12 00:39:55
Done.
| |
71 } | 73 } |
72 } | 74 }; |
73 } | |
74 }; | |
75 | |
76 chrome.sync.events = { | |
77 'service': [ | |
78 'onServiceStateChanged' | |
79 ], | |
80 | |
81 // See chrome/browser/sync/engine/syncapi.h for docs. | |
82 'notifier': [ | |
83 'onNotificationStateChange', | |
84 'onIncomingNotification' | |
85 ], | |
86 | |
87 'manager': [ | |
88 'onChangesApplied', | |
89 'onChangesComplete', | |
90 'onSyncCycleCompleted', | |
91 'onConnectionStatusChange', | |
92 'onPassphraseRequired', | |
93 'onPassphraseAccepted', | |
94 'onInitializationComplete', | |
95 'onStopSyncingPermanently', | |
96 'onClearServerDataSucceeded', | |
97 'onClearServerDataFailed', | |
98 'onEncryptedTypesChanged', | |
99 'onEncryptionComplete', | |
100 'onActionableError', | |
101 ], | |
102 | |
103 'transaction': [ | |
104 'onTransactionWrite', | |
105 ] | |
106 }; | |
107 | |
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(); | |
113 } | |
114 } | |
115 | |
116 function makeSyncFunction(name) { | |
117 var callbacks = []; | |
118 | |
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); | |
125 }; | 75 }; |
126 | 76 |
127 // Handle a reply, assuming that messages are processed in FIFO order. | 77 /** |
128 // Called by SyncInternalsUI::HandleJsReply(). | 78 * @param {string} name The name of the event type. |
129 fn.handleReply = function() { | 79 * @param {dictionary} details A collection of event-specific details. |
Dan Beam
2014/02/11 23:21:48
@param {Object} details ...
or
@param {!Object
rlarocque
2014/02/12 00:39:55
The C++ code currently guarantees that it will con
| |
130 var args = Array.prototype.slice.call(arguments); | 80 */ |
131 // Remove the callback before we call it since the callback may | 81 var handleEvent = function(name, details) { |
Dan Beam
2014/02/11 23:21:48
nit: s/handleEvent/dispatchEvent/
rlarocque
2014/02/12 00:39:55
Done.
| |
132 // throw. | 82 var e = new Event(name); |
133 var callback = callbacks.shift(); | 83 e.details = details; |
134 callback.apply(null, args); | 84 chrome.sync.events.dispatchEvent(e); |
135 }; | 85 } |
Dan Beam
2014/02/11 23:21:48
};
rlarocque
2014/02/12 00:39:55
Done.
| |
136 | 86 |
137 return fn; | 87 /** |
138 } | 88 * Asks the browser to refresh our snapshot of sync state. Should result |
139 | 89 * in an onServiceStateChanged event being emitted. |
140 var syncFunctions = [ | 90 */ |
141 // Sync service functions. | 91 var getAboutInfo = function() { |
142 'getAboutInfo', | 92 chrome.send('getAboutInfo'); |
143 | 93 } |
Dan Beam
2014/02/11 23:21:48
^ where is this actually used?
rlarocque
2014/02/12 00:39:55
That's left over from a bad rebase. Removed.
| |
144 // Notification functions. See chrome/browser/sync/engine/syncapi.h | |
145 // for docs. | |
146 'getNotificationState', | |
147 'getNotificationInfo', | |
148 | |
149 // Get a static list of available data types. | |
150 'getListOfTypes', | |
151 | |
152 // Client server communication logging functions. | |
153 'getClientServerTraffic', | |
154 | |
155 // Get an array containing a JSON representations of all known sync nodes. | |
156 'getAllNodes', | |
157 ]; | |
158 | |
159 for (var i = 0; i < syncFunctions.length; ++i) { | |
160 var syncFunction = syncFunctions[i]; | |
161 chrome.sync[syncFunction] = makeSyncFunction(syncFunction); | |
162 } | |
163 | |
164 /** | |
165 * Returns an object which measures elapsed time. | |
166 */ | |
167 chrome.sync.makeTimer = function() { | |
168 var start = new Date(); | |
169 | 94 |
170 return { | 95 return { |
171 /** | 96 handleEvent: handleEvent, |
172 * @return {number} The number of seconds since the timer was | 97 events: new cr.EventTarget(), |
173 * created. | |
174 */ | |
175 get elapsedSeconds() { | |
176 return ((new Date()).getTime() - start.getTime()) / 1000.0; | |
177 } | |
178 }; | 98 }; |
179 }; | 99 }); |
180 | |
181 })(); | |
OLD | NEW |