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 DCHECK = requireNative('logging').DCHECK; | 5 var DCHECK = requireNative('logging').DCHECK; |
6 var eventBindingsNatives = requireNative('event_bindings'); | 6 var eventBindingsNatives = requireNative('event_bindings'); |
7 var AttachEvent = eventBindingsNatives.AttachEvent; | 7 var AttachEvent = eventBindingsNatives.AttachEvent; |
8 var DetachEvent = eventBindingsNatives.DetachEvent; | 8 var DetachEvent = eventBindingsNatives.DetachEvent; |
9 var AttachFilteredEvent = eventBindingsNatives.AttachFilteredEvent; | 9 var AttachFilteredEvent = eventBindingsNatives.AttachFilteredEvent; |
10 var DetachFilteredEvent = eventBindingsNatives.DetachFilteredEvent; | 10 var DetachFilteredEvent = eventBindingsNatives.DetachFilteredEvent; |
(...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
151 this.detachListener(this.listenerMap_[i], manual); | 151 this.detachListener(this.listenerMap_[i], manual); |
152 }; | 152 }; |
153 | 153 |
154 FilteredAttachmentStrategy.prototype.getListenersByIDs = function(ids) { | 154 FilteredAttachmentStrategy.prototype.getListenersByIDs = function(ids) { |
155 var result = []; | 155 var result = []; |
156 for (var i = 0; i < ids.length; i++) | 156 for (var i = 0; i < ids.length; i++) |
157 result.push(this.listenerMap_[ids[i]]); | 157 result.push(this.listenerMap_[ids[i]]); |
158 return result; | 158 return result; |
159 }; | 159 }; |
160 | 160 |
| 161 chromeHidden.parseEventOptions = function(opt_eventOptions) { |
| 162 function merge(dest, src) { |
| 163 for (var k in src) { |
| 164 if (!dest.hasOwnProperty(k)) { |
| 165 dest[k] = src[k]; |
| 166 } |
| 167 } |
| 168 } |
| 169 |
| 170 var options = opt_eventOptions || {}; |
| 171 merge(options, |
| 172 {supportsFilters: false, |
| 173 supportsListeners: true, |
| 174 supportsRules: false, |
| 175 }); |
| 176 return options; |
| 177 }; |
| 178 |
161 // Event object. If opt_eventName is provided, this object represents | 179 // Event object. If opt_eventName is provided, this object represents |
162 // the unique instance of that named event, and dispatching an event | 180 // the unique instance of that named event, and dispatching an event |
163 // with that name will route through this object's listeners. Note that | 181 // with that name will route through this object's listeners. Note that |
164 // opt_eventName is required for events that support rules. | 182 // opt_eventName is required for events that support rules. |
165 // | 183 // |
166 // Example: | 184 // Example: |
167 // chrome.tabs.onChanged = new chrome.Event("tab-changed"); | 185 // chrome.tabs.onChanged = new chrome.Event("tab-changed"); |
168 // chrome.tabs.onChanged.addListener(function(data) { alert(data); }); | 186 // chrome.tabs.onChanged.addListener(function(data) { alert(data); }); |
169 // chromeHidden.Event.dispatch("tab-changed", "hi"); | 187 // chromeHidden.Event.dispatch("tab-changed", "hi"); |
170 // will result in an alert dialog that says 'hi'. | 188 // will result in an alert dialog that says 'hi'. |
171 // | 189 // |
172 // If opt_eventOptions exists, it is a dictionary that contains the boolean | 190 // If opt_eventOptions exists, it is a dictionary that contains the boolean |
173 // entries "supportsListeners" and "supportsRules". | 191 // entries "supportsListeners" and "supportsRules". |
174 chrome.Event = function(opt_eventName, opt_argSchemas, opt_eventOptions) { | 192 chrome.Event = function(opt_eventName, opt_argSchemas, opt_eventOptions) { |
175 this.eventName_ = opt_eventName; | 193 this.eventName_ = opt_eventName; |
176 this.listeners_ = []; | 194 this.listeners_ = []; |
177 this.eventOptions_ = opt_eventOptions || | 195 this.eventOptions_ = chromeHidden.parseEventOptions(opt_eventOptions); |
178 {supportsFilters: false, | |
179 supportsListeners: true, | |
180 supportsRules: false, | |
181 }; | |
182 | 196 |
183 if (this.eventOptions_.supportsRules && !opt_eventName) | 197 if (this.eventOptions_.supportsRules && !opt_eventName) |
184 throw new Error("Events that support rules require an event name."); | 198 throw new Error("Events that support rules require an event name."); |
185 | 199 |
186 if (this.eventOptions_.supportsFilters) { | 200 if (this.eventOptions_.supportsFilters) { |
187 this.attachmentStrategy_ = new FilteredAttachmentStrategy(this); | 201 this.attachmentStrategy_ = new FilteredAttachmentStrategy(this); |
188 } else { | 202 } else { |
189 this.attachmentStrategy_ = new UnfilteredAttachmentStrategy(this); | 203 this.attachmentStrategy_ = new UnfilteredAttachmentStrategy(this); |
190 } | 204 } |
191 | 205 |
192 // Validate event arguments (the data that is passed to the callbacks) | 206 // Validate event arguments (the data that is passed to the callbacks) |
193 // if we are in debug. | 207 // if we are in debug. |
194 if (opt_argSchemas && | 208 if (opt_argSchemas && |
195 chromeHidden.validateCallbacks) { | 209 chromeHidden.validateCallbacks) { |
196 | 210 |
197 this.validateEventArgs_ = function(args) { | 211 this.validateEventArgs_ = function(args) { |
198 try { | 212 try { |
199 validate(args, opt_argSchemas); | 213 validate(args, opt_argSchemas); |
200 } catch (exception) { | 214 } catch (exception) { |
201 return "Event validation error during " + opt_eventName + " -- " + | 215 return "Event validation error during " + opt_eventName + " -- " + |
202 exception; | 216 exception; |
203 } | 217 } |
204 }; | 218 }; |
205 } else { | 219 } else { |
206 this.validateEventArgs_ = function() {} | 220 this.validateEventArgs_ = function() {} |
207 } | 221 } |
208 }; | 222 }; |
209 | 223 |
| 224 |
210 chromeHidden.Event = {}; | 225 chromeHidden.Event = {}; |
211 | 226 |
212 // callback is a function(args, dispatch). args are the args we receive from | 227 // callback is a function(args, dispatch). args are the args we receive from |
213 // dispatchEvent(), and dispatch is a function(args) that dispatches args to | 228 // dispatchEvent(), and dispatch is a function(args) that dispatches args to |
214 // its listeners. | 229 // its listeners. |
215 chromeHidden.Event.registerArgumentMassager = function(name, callback) { | 230 chromeHidden.Event.registerArgumentMassager = function(name, callback) { |
216 if (eventArgumentMassagers[name]) | 231 if (eventArgumentMassagers[name]) |
217 throw new Error("Massager already registered for event: " + name); | 232 throw new Error("Massager already registered for event: " + name); |
218 eventArgumentMassagers[name] = callback; | 233 eventArgumentMassagers[name] = callback; |
219 }; | 234 }; |
(...skipping 249 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
469 if (event) | 484 if (event) |
470 event.detach_(); | 485 event.detach_(); |
471 } | 486 } |
472 }; | 487 }; |
473 | 488 |
474 chromeHidden.dispatchError = function(msg) { | 489 chromeHidden.dispatchError = function(msg) { |
475 console.error(msg); | 490 console.error(msg); |
476 }; | 491 }; |
477 | 492 |
478 exports.Event = chrome.Event; | 493 exports.Event = chrome.Event; |
OLD | NEW |