OLD | NEW |
---|---|
(Empty) | |
1 var chrome = chrome || {}; | |
2 // TODO(akalin): Add mocking code for e.g. chrome.send() so that we | |
3 // can test this without rebuilding chrome. | |
4 chrome.sync = chrome.sync || {}; | |
5 (function () { | |
6 | |
7 // This Event class is a simplified version of the one from | |
8 // event_bindings.js. | |
9 function Event() { | |
10 this.listeners_ = []; | |
11 } | |
12 | |
13 Event.prototype.addListener = function(listener) { | |
14 this.listeners_.push(listener); | |
15 }; | |
16 | |
17 Event.prototype.removeListener = function(listener) { | |
18 var i = this.findListener_(listener); | |
19 if (i == -1) { | |
20 return; | |
21 } | |
22 this.listeners_.splice(i, 1); | |
23 }; | |
24 | |
25 Event.prototype.hasListener = function(listener) { | |
26 return this.findListener_(listener) > -1; | |
27 }; | |
28 | |
29 Event.prototype.hasListeners = function(listener) { | |
30 return this.listeners_.length > 0; | |
31 }; | |
32 | |
33 // Returns the index of the given listener, or -1 if not found. | |
34 Event.prototype.findListener_ = function(listener) { | |
35 for (var i = 0; i < this.listeners_.length; i++) { | |
36 if (this.listeners_[i] == listener) { | |
37 return i; | |
38 } | |
39 } | |
40 return -1; | |
41 }; | |
42 | |
43 // Fires the event. Called by the actual event callback. | |
44 Event.prototype.dispatch_ = function() { | |
45 var args = Array.prototype.slice.call(arguments); | |
46 for (var i = 0; i < this.listeners_.length; i++) { | |
47 try { | |
48 this.listeners_[i].apply(null, args); | |
49 } catch (e) { | |
50 console.error(e); | |
51 } | |
52 } | |
53 }; | |
54 | |
55 // Sync service events. | |
56 chrome.sync.onSyncServiceStateChanged = new Event(); | |
57 | |
58 // Notification events. | |
59 chrome.sync.onSyncNotificationStateChange = new Event(); | |
60 chrome.sync.onSyncIncomingNotification = new Event(); | |
61 | |
62 // Sync manager events. | |
63 chrome.sync.onChangesApplied = new Event(); | |
64 chrome.sync.onChangesComplete = new Event(); | |
65 chrome.sync.onSyncCycleCompleted = new Event(); | |
66 chrome.sync.onAuthError = new Event(); | |
67 chrome.sync.onUpdatedToken = new Event(); | |
68 chrome.sync.onPassphraseRequired = new Event(); | |
69 chrome.sync.onPassphraseAccepted = new Event(); | |
70 chrome.sync.onInitializationComplete = new Event(); | |
71 chrome.sync.onPaused = new Event(); | |
72 chrome.sync.onResumed = new Event(); | |
73 chrome.sync.onStopSyncingPermanently = new Event(); | |
74 chrome.sync.onClearServerDataSucceeded = new Event(); | |
75 chrome.sync.onClearServerDataFailed = new Event(); | |
76 | |
77 function AsyncFunction(name) { | |
78 this.name_ = name; | |
79 this.callbacks_ = []; | |
80 } | |
81 | |
82 AsyncFunction.prototype.call = function(args) { | |
83 this.callbacks_.push(args.pop()); | |
84 chrome.send(this.name_, args); | |
85 } | |
86 | |
87 AsyncFunction.prototype.handleReply = function(args) { | |
88 var callback = this.callbacks_.shift(); | |
John Gregg
2011/02/17 20:49:38
i guess you assume that the DOMUI message handlers
akalin
2011/02/18 00:36:22
Yeah, forgot to document that assumption. Added c
| |
89 try { | |
90 callback.apply(null, args); | |
91 } catch (e) { | |
92 console.error(e); | |
93 } | |
94 } | |
95 | |
96 // Sync service functions. | |
97 chrome.sync.getAboutInfo_ = new AsyncFunction('getAboutInfo'); | |
John Gregg
2011/02/17 20:49:38
i think these are unnecessarily generic. are the
akalin
2011/02/18 00:36:22
Done.
| |
98 chrome.sync.getAboutInfo = function() { | |
99 var args = Array.prototype.slice.call(arguments); | |
100 chrome.sync.getAboutInfo_.call(args); | |
101 } | |
102 | |
103 // Notification functions. | |
104 chrome.sync.getNotificationState_ = | |
105 new AsyncFunction('getNotificationState'); | |
106 chrome.sync.getNotificationState = function() { | |
107 var args = Array.prototype.slice.call(arguments); | |
108 chrome.sync.getNotificationState_.call(args); | |
109 } | |
110 | |
111 // Node lookup functions. | |
112 chrome.sync.getRootNode_ = new AsyncFunction('getRootNode'); | |
113 chrome.sync.getRootNode = function() { | |
114 var args = Array.prototype.slice.call(arguments); | |
115 chrome.sync.getRootNode_.call(args); | |
116 } | |
117 | |
118 chrome.sync.getNodeById_ = new AsyncFunction('getNodeById'); | |
119 chrome.sync.getNodeById = function() { | |
120 var args = Array.prototype.slice.call(arguments); | |
121 chrome.sync.getNodeById_.call(args); | |
122 } | |
123 | |
124 })(); | |
125 | |
126 // TODO(akalin): Rewrite the C++ side to not need the handlers below. | |
127 | |
128 // Sync service event handlers. | |
129 | |
130 function onSyncServiceStateChanged() { | |
131 chrome.sync.onSyncServiceStateChanged.dispatch_(); | |
132 } | |
133 | |
134 // Notification event handlers. | |
135 | |
136 function onSyncNotificationStateChange(notificationsEnabled) { | |
137 chrome.sync.onSyncNotificationStateChange.dispatch_(notificationsEnabled); | |
138 } | |
139 | |
140 function onSyncIncomingNotification(changedTypes) { | |
141 chrome.sync.onSyncIncomingNotification.dispatch_(changedTypes); | |
142 } | |
143 | |
144 // Sync manager event handlers. | |
145 | |
146 function onChangesApplied(modelType, changes) { | |
147 chrome.sync.onChangesApplied.dispatch_(modelType, changes); | |
148 } | |
149 | |
150 function onChangesComplete(modelType) { | |
151 chrome.sync.onChangesComplete.dispatch_(modelType); | |
152 } | |
153 | |
154 function onSyncCycleCompleted(snapshot) { | |
155 chrome.sync.onSyncCycleCompleted.dispatch_(snapshot); | |
156 } | |
157 | |
158 function onAuthError(authError) { | |
159 chrome.sync.onAuthError.dispatch_(authError); | |
160 } | |
161 | |
162 function onUpdatedToken(token) { | |
163 chrome.sync.onUpdatedToken.dispatch_(token); | |
164 } | |
165 | |
166 function onPassphraseRequired(forDecryption) { | |
167 chrome.sync.onPassphraseRequired.dispatch_(forDecryption); | |
168 } | |
169 | |
170 function onPassphraseAccepted(bootstrapToken) { | |
171 chrome.sync.onPassphraseAccepted.dispatch_(bootstrapToken); | |
172 } | |
173 | |
174 function onInitializationComplete() { | |
175 chrome.sync.onInitializationComplete.dispatch_(); | |
176 } | |
177 | |
178 function onPaused() { | |
179 chrome.sync.onPaused.dispatch_(); | |
180 } | |
181 | |
182 function onResumed() { | |
183 chrome.sync.onResumed.dispatch_(); | |
184 } | |
185 | |
186 function onStopSyncingPermanently() { | |
187 chrome.sync.onStopSyncingPermanently.dispatch_(); | |
188 } | |
189 | |
190 function onClearServerDataSucceeded() { | |
191 chrome.sync.onClearServerDataSucceeded(); | |
192 } | |
193 | |
194 function onClearServerDataFailed() { | |
195 chrome.sync.onClearServerDataFailed(); | |
196 } | |
197 | |
198 // Function reply handlers. | |
199 | |
200 function onGetAboutInfoFinished() { | |
201 var args = Array.prototype.slice.call(arguments); | |
202 chrome.sync.getAboutInfo_.handleReply(args); | |
203 } | |
204 | |
205 function onGetNotificationStateFinished() { | |
206 var args = Array.prototype.slice.call(arguments); | |
207 chrome.sync.getNotificationState_.handleReply(args); | |
208 } | |
209 | |
210 function onGetRootNodeFinished() { | |
211 var args = Array.prototype.slice.call(arguments); | |
212 chrome.sync.getRootNode_.handleReply(args); | |
213 } | |
214 | |
215 function onGetNodeByIdFinished() { | |
216 var args = Array.prototype.slice.call(arguments); | |
217 chrome.sync.getNodeById_.handleReply(args); | |
218 } | |
OLD | NEW |