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 // Calls the function, assuming the last argument is a callback to be |
| 83 // called with the return value. |
| 84 AsyncFunction.prototype.call = function() { |
| 85 var args = Array.prototype.slice.call(arguments); |
| 86 this.callbacks_.push(args.pop()); |
| 87 chrome.send(this.name_, args); |
| 88 } |
| 89 |
| 90 // Handle a reply, assuming that messages are processed in FIFO order. |
| 91 AsyncFunction.prototype.handleReply = function() { |
| 92 var args = Array.prototype.slice.call(arguments); |
| 93 var callback = this.callbacks_.shift(); |
| 94 try { |
| 95 callback.apply(null, args); |
| 96 } catch (e) { |
| 97 console.error(e); |
| 98 } |
| 99 } |
| 100 |
| 101 // Sync service functions. |
| 102 chrome.sync.getAboutInfo_ = new AsyncFunction('getAboutInfo'); |
| 103 chrome.sync.getAboutInfo = function(callback) { |
| 104 chrome.sync.getAboutInfo_.call(callback); |
| 105 } |
| 106 |
| 107 // Notification functions. |
| 108 chrome.sync.getNotificationState_ = |
| 109 new AsyncFunction('getNotificationState'); |
| 110 chrome.sync.getNotificationState = function(callback) { |
| 111 chrome.sync.getNotificationState_.call(callback); |
| 112 } |
| 113 |
| 114 // Node lookup functions. |
| 115 chrome.sync.getRootNode_ = new AsyncFunction('getRootNode'); |
| 116 chrome.sync.getRootNode = function(callback) { |
| 117 chrome.sync.getRootNode_.call(callback); |
| 118 } |
| 119 |
| 120 chrome.sync.getNodeById_ = new AsyncFunction('getNodeById'); |
| 121 chrome.sync.getNodeById = function(id, callback) { |
| 122 chrome.sync.getNodeById_.call(id, callback); |
| 123 } |
| 124 |
| 125 })(); |
| 126 |
| 127 // TODO(akalin): Rewrite the C++ side to not need the handlers below. |
| 128 |
| 129 // Sync service event handlers. |
| 130 |
| 131 function onSyncServiceStateChanged() { |
| 132 chrome.sync.onSyncServiceStateChanged.dispatch_(); |
| 133 } |
| 134 |
| 135 // Notification event handlers. |
| 136 |
| 137 function onSyncNotificationStateChange(notificationsEnabled) { |
| 138 chrome.sync.onSyncNotificationStateChange.dispatch_(notificationsEnabled); |
| 139 } |
| 140 |
| 141 function onSyncIncomingNotification(changedTypes) { |
| 142 chrome.sync.onSyncIncomingNotification.dispatch_(changedTypes); |
| 143 } |
| 144 |
| 145 // Sync manager event handlers. |
| 146 |
| 147 function onChangesApplied(modelType, changes) { |
| 148 chrome.sync.onChangesApplied.dispatch_(modelType, changes); |
| 149 } |
| 150 |
| 151 function onChangesComplete(modelType) { |
| 152 chrome.sync.onChangesComplete.dispatch_(modelType); |
| 153 } |
| 154 |
| 155 function onSyncCycleCompleted(snapshot) { |
| 156 chrome.sync.onSyncCycleCompleted.dispatch_(snapshot); |
| 157 } |
| 158 |
| 159 function onAuthError(authError) { |
| 160 chrome.sync.onAuthError.dispatch_(authError); |
| 161 } |
| 162 |
| 163 function onUpdatedToken(token) { |
| 164 chrome.sync.onUpdatedToken.dispatch_(token); |
| 165 } |
| 166 |
| 167 function onPassphraseRequired(forDecryption) { |
| 168 chrome.sync.onPassphraseRequired.dispatch_(forDecryption); |
| 169 } |
| 170 |
| 171 function onPassphraseAccepted(bootstrapToken) { |
| 172 chrome.sync.onPassphraseAccepted.dispatch_(bootstrapToken); |
| 173 } |
| 174 |
| 175 function onInitializationComplete() { |
| 176 chrome.sync.onInitializationComplete.dispatch_(); |
| 177 } |
| 178 |
| 179 function onPaused() { |
| 180 chrome.sync.onPaused.dispatch_(); |
| 181 } |
| 182 |
| 183 function onResumed() { |
| 184 chrome.sync.onResumed.dispatch_(); |
| 185 } |
| 186 |
| 187 function onStopSyncingPermanently() { |
| 188 chrome.sync.onStopSyncingPermanently.dispatch_(); |
| 189 } |
| 190 |
| 191 function onClearServerDataSucceeded() { |
| 192 chrome.sync.onClearServerDataSucceeded(); |
| 193 } |
| 194 |
| 195 function onClearServerDataFailed() { |
| 196 chrome.sync.onClearServerDataFailed(); |
| 197 } |
| 198 |
| 199 // Function reply handlers. |
| 200 |
| 201 function onGetAboutInfoFinished(aboutInfo) { |
| 202 chrome.sync.getAboutInfo_.handleReply(aboutInfo); |
| 203 } |
| 204 |
| 205 function onGetNotificationStateFinished(notificationState) { |
| 206 chrome.sync.getNotificationState_.handleReply(notificationState); |
| 207 } |
| 208 |
| 209 function onGetRootNodeFinished(rootNode) { |
| 210 chrome.sync.getRootNode_.handleReply(rootNode); |
| 211 } |
| 212 |
| 213 function onGetNodeByIdFinished(node) { |
| 214 chrome.sync.getNodeById_.handleReply(node); |
| 215 } |
OLD | NEW |