| OLD | NEW |
| 1 var chrome = chrome || {}; | 1 var chrome = chrome || {}; |
| 2 // TODO(akalin): Add mocking code for e.g. chrome.send() so that we | 2 // TODO(akalin): Add mocking code for e.g. chrome.send() so that we |
| 3 // can test this without rebuilding chrome. | 3 // can test this without rebuilding chrome. |
| 4 chrome.sync = chrome.sync || {}; | 4 chrome.sync = chrome.sync || {}; |
| 5 (function () { | 5 (function () { |
| 6 | 6 |
| 7 // This Event class is a simplified version of the one from | 7 // This Event class is a simplified version of the one from |
| 8 // event_bindings.js. | 8 // event_bindings.js. |
| 9 function Event() { | 9 function Event() { |
| 10 this.listeners_ = []; | 10 this.listeners_ = []; |
| (...skipping 22 matching lines...) Expand all Loading... |
| 33 // Returns the index of the given listener, or -1 if not found. | 33 // Returns the index of the given listener, or -1 if not found. |
| 34 Event.prototype.findListener_ = function(listener) { | 34 Event.prototype.findListener_ = function(listener) { |
| 35 for (var i = 0; i < this.listeners_.length; i++) { | 35 for (var i = 0; i < this.listeners_.length; i++) { |
| 36 if (this.listeners_[i] == listener) { | 36 if (this.listeners_[i] == listener) { |
| 37 return i; | 37 return i; |
| 38 } | 38 } |
| 39 } | 39 } |
| 40 return -1; | 40 return -1; |
| 41 }; | 41 }; |
| 42 | 42 |
| 43 // Fires the event. Called by the actual event callback. | 43 // Fires the event. Called by the actual event callback. Any |
| 44 // exceptions thrown by a listener are caught and logged. |
| 44 Event.prototype.dispatch_ = function() { | 45 Event.prototype.dispatch_ = function() { |
| 45 var args = Array.prototype.slice.call(arguments); | 46 var args = Array.prototype.slice.call(arguments); |
| 46 for (var i = 0; i < this.listeners_.length; i++) { | 47 for (var i = 0; i < this.listeners_.length; i++) { |
| 47 try { | 48 try { |
| 48 this.listeners_[i].apply(null, args); | 49 this.listeners_[i].apply(null, args); |
| 49 } catch (e) { | 50 } catch (e) { |
| 50 console.error(e); | 51 if (e instanceof Error) { |
| 52 // Non-standard, but useful. |
| 53 console.error(e.stack); |
| 54 } else { |
| 55 console.error(e); |
| 56 } |
| 51 } | 57 } |
| 52 } | 58 } |
| 53 }; | 59 }; |
| 54 | 60 |
| 55 // Sync service events. | 61 // Sync service events. |
| 56 chrome.sync.onSyncServiceStateChanged = new Event(); | 62 chrome.sync.onSyncServiceStateChanged = new Event(); |
| 57 | 63 |
| 58 // Notification events. | 64 // Notification events. |
| 59 chrome.sync.onSyncNotificationStateChange = new Event(); | 65 chrome.sync.onSyncNotificationStateChange = new Event(); |
| 60 chrome.sync.onSyncIncomingNotification = new Event(); | 66 chrome.sync.onSyncIncomingNotification = new Event(); |
| (...skipping 22 matching lines...) Expand all Loading... |
| 83 // called with the return value. | 89 // called with the return value. |
| 84 AsyncFunction.prototype.call = function() { | 90 AsyncFunction.prototype.call = function() { |
| 85 var args = Array.prototype.slice.call(arguments); | 91 var args = Array.prototype.slice.call(arguments); |
| 86 this.callbacks_.push(args.pop()); | 92 this.callbacks_.push(args.pop()); |
| 87 chrome.send(this.name_, args); | 93 chrome.send(this.name_, args); |
| 88 } | 94 } |
| 89 | 95 |
| 90 // Handle a reply, assuming that messages are processed in FIFO order. | 96 // Handle a reply, assuming that messages are processed in FIFO order. |
| 91 AsyncFunction.prototype.handleReply = function() { | 97 AsyncFunction.prototype.handleReply = function() { |
| 92 var args = Array.prototype.slice.call(arguments); | 98 var args = Array.prototype.slice.call(arguments); |
| 99 // Remove the callback before we call it since the callback may |
| 100 // throw. |
| 93 var callback = this.callbacks_.shift(); | 101 var callback = this.callbacks_.shift(); |
| 94 try { | 102 callback.apply(null, args); |
| 95 callback.apply(null, args); | |
| 96 } catch (e) { | |
| 97 console.error(e); | |
| 98 } | |
| 99 } | 103 } |
| 100 | 104 |
| 101 // Sync service functions. | 105 // Sync service functions. |
| 102 chrome.sync.getAboutInfo_ = new AsyncFunction('getAboutInfo'); | 106 chrome.sync.getAboutInfo_ = new AsyncFunction('getAboutInfo'); |
| 103 chrome.sync.getAboutInfo = function(callback) { | 107 chrome.sync.getAboutInfo = function(callback) { |
| 104 chrome.sync.getAboutInfo_.call(callback); | 108 chrome.sync.getAboutInfo_.call(callback); |
| 105 } | 109 } |
| 106 | 110 |
| 107 // Notification functions. | 111 // Notification functions. |
| 108 chrome.sync.getNotificationState_ = | 112 chrome.sync.getNotificationState_ = |
| (...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 206 chrome.sync.getNotificationState_.handleReply(notificationState); | 210 chrome.sync.getNotificationState_.handleReply(notificationState); |
| 207 } | 211 } |
| 208 | 212 |
| 209 function onGetRootNodeFinished(rootNode) { | 213 function onGetRootNodeFinished(rootNode) { |
| 210 chrome.sync.getRootNode_.handleReply(rootNode); | 214 chrome.sync.getRootNode_.handleReply(rootNode); |
| 211 } | 215 } |
| 212 | 216 |
| 213 function onGetNodeByIdFinished(node) { | 217 function onGetNodeByIdFinished(node) { |
| 214 chrome.sync.getNodeById_.handleReply(node); | 218 chrome.sync.getNodeById_.handleReply(node); |
| 215 } | 219 } |
| OLD | NEW |