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 // Adds keys in |src| that aren't present in |dest| to |dest|. | |
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 // Event object. If opt_eventName is provided, this object represents | 161 // Event object. If opt_eventName is provided, this object represents |
171 // the unique instance of that named event, and dispatching an event | 162 // the unique instance of that named event, and dispatching an event |
172 // with that name will route through this object's listeners. Note that | 163 // with that name will route through this object's listeners. Note that |
173 // opt_eventName is required for events that support rules. | 164 // opt_eventName is required for events that support rules. |
174 // | 165 // |
175 // Example: | 166 // Example: |
176 // chrome.tabs.onChanged = new chrome.Event("tab-changed"); | 167 // chrome.tabs.onChanged = new chrome.Event("tab-changed"); |
177 // chrome.tabs.onChanged.addListener(function(data) { alert(data); }); | 168 // chrome.tabs.onChanged.addListener(function(data) { alert(data); }); |
178 // chromeHidden.Event.dispatch("tab-changed", "hi"); | 169 // chromeHidden.Event.dispatch("tab-changed", "hi"); |
179 // will result in an alert dialog that says 'hi'. | 170 // will result in an alert dialog that says 'hi'. |
180 // | 171 // |
181 // If opt_eventOptions exists, it is a dictionary that contains the boolean | 172 // If opt_eventOptions exists, it is a dictionary that contains the boolean |
182 // entries "supportsListeners" and "supportsRules". | 173 // entries "supportsListeners" and "supportsRules". |
183 chrome.Event = function(opt_eventName, opt_argSchemas, opt_eventOptions) { | 174 chrome.Event = function(opt_eventName, opt_argSchemas, opt_eventOptions) { |
184 this.eventName_ = opt_eventName; | 175 this.eventName_ = opt_eventName; |
185 this.listeners_ = []; | 176 this.listeners_ = []; |
186 this.eventOptions_ = opt_eventOptions || {}; | 177 this.eventOptions_ = opt_eventOptions || |
187 merge(this.eventOptions_, | |
188 {supportsFilters: false, | 178 {supportsFilters: false, |
189 supportsListeners: true, | 179 supportsListeners: true, |
190 supportsRules: false, | 180 supportsRules: false, |
191 }); | 181 }; |
192 | 182 |
193 if (this.eventOptions_.supportsRules && !opt_eventName) | 183 if (this.eventOptions_.supportsRules && !opt_eventName) |
194 throw new Error("Events that support rules require an event name."); | 184 throw new Error("Events that support rules require an event name."); |
195 | 185 |
196 if (this.eventOptions_.supportsFilters) { | 186 if (this.eventOptions_.supportsFilters) { |
197 this.attachmentStrategy_ = new FilteredAttachmentStrategy(this); | 187 this.attachmentStrategy_ = new FilteredAttachmentStrategy(this); |
198 } else { | 188 } else { |
199 this.attachmentStrategy_ = new UnfilteredAttachmentStrategy(this); | 189 this.attachmentStrategy_ = new UnfilteredAttachmentStrategy(this); |
200 } | 190 } |
201 | 191 |
(...skipping 277 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
479 if (event) | 469 if (event) |
480 event.detach_(); | 470 event.detach_(); |
481 } | 471 } |
482 }; | 472 }; |
483 | 473 |
484 chromeHidden.dispatchError = function(msg) { | 474 chromeHidden.dispatchError = function(msg) { |
485 console.error(msg); | 475 console.error(msg); |
486 }; | 476 }; |
487 | 477 |
488 exports.Event = chrome.Event; | 478 exports.Event = chrome.Event; |
OLD | NEW |