OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 // TODO(robwu): Fix indentation. | 5 // TODO(robwu): Fix indentation. |
6 | 6 |
7 var exceptionHandler = require('uncaught_exception_handler'); | 7 var exceptionHandler = require('uncaught_exception_handler'); |
8 var eventNatives = requireNative('event_natives'); | 8 var eventNatives = requireNative('event_natives'); |
9 var logging = requireNative('logging'); | 9 var logging = requireNative('logging'); |
10 var schemaRegistry = requireNative('schema_registry'); | 10 var schemaRegistry = requireNative('schema_registry'); |
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
83 // Handles adding/removing/dispatching listeners for unfiltered events. | 83 // Handles adding/removing/dispatching listeners for unfiltered events. |
84 function UnfilteredAttachmentStrategy(event) { | 84 function UnfilteredAttachmentStrategy(event) { |
85 this.event_ = event; | 85 this.event_ = event; |
86 } | 86 } |
87 $Object.setPrototypeOf(UnfilteredAttachmentStrategy.prototype, null); | 87 $Object.setPrototypeOf(UnfilteredAttachmentStrategy.prototype, null); |
88 | 88 |
89 UnfilteredAttachmentStrategy.prototype.onAddedListener = | 89 UnfilteredAttachmentStrategy.prototype.onAddedListener = |
90 function(listener) { | 90 function(listener) { |
91 // Only attach / detach on the first / last listener removed. | 91 // Only attach / detach on the first / last listener removed. |
92 if (this.event_.listeners.length == 0) | 92 if (this.event_.listeners.length == 0) |
93 eventNatives.AttachEvent(this.event_.eventName); | 93 eventNatives.AttachEvent(this.event_.eventName, |
| 94 this.event_.eventOptions.supportsLazyListeners); |
94 }; | 95 }; |
95 | 96 |
96 UnfilteredAttachmentStrategy.prototype.onRemovedListener = | 97 UnfilteredAttachmentStrategy.prototype.onRemovedListener = |
97 function(listener) { | 98 function(listener) { |
98 if (this.event_.listeners.length == 0) | 99 if (this.event_.listeners.length == 0) |
99 this.detach(true); | 100 this.detach(true); |
100 }; | 101 }; |
101 | 102 |
102 UnfilteredAttachmentStrategy.prototype.detach = function(manual) { | 103 UnfilteredAttachmentStrategy.prototype.detach = function(manual) { |
103 eventNatives.DetachEvent(this.event_.eventName, manual); | 104 eventNatives.DetachEvent(this.event_.eventName, manual, |
| 105 this.event_.eventOptions.supportsLazyListeners); |
104 }; | 106 }; |
105 | 107 |
106 UnfilteredAttachmentStrategy.prototype.getListenersByIDs = function(ids) { | 108 UnfilteredAttachmentStrategy.prototype.getListenersByIDs = function(ids) { |
107 // |ids| is for filtered events only. | 109 // |ids| is for filtered events only. |
108 return this.event_.listeners; | 110 return this.event_.listeners; |
109 }; | 111 }; |
110 | 112 |
111 function FilteredAttachmentStrategy(event) { | 113 function FilteredAttachmentStrategy(event) { |
112 this.event_ = event; | 114 this.event_ = event; |
113 this.listenerMap_ = {__proto__: null}; | 115 this.listenerMap_ = {__proto__: null}; |
114 } | 116 } |
115 $Object.setPrototypeOf(FilteredAttachmentStrategy.prototype, null); | 117 $Object.setPrototypeOf(FilteredAttachmentStrategy.prototype, null); |
116 | 118 |
117 utils.defineProperty(FilteredAttachmentStrategy, 'idToEventMap', | 119 utils.defineProperty(FilteredAttachmentStrategy, 'idToEventMap', |
118 {__proto__: null}); | 120 {__proto__: null}); |
119 | 121 |
120 FilteredAttachmentStrategy.prototype.onAddedListener = function(listener) { | 122 FilteredAttachmentStrategy.prototype.onAddedListener = function(listener) { |
121 var id = eventNatives.AttachFilteredEvent(this.event_.eventName, | 123 var id = eventNatives.AttachFilteredEvent( |
122 listener.filters || {}); | 124 this.event_.eventName, listener.filters || {}, |
| 125 this.event_.eventOptions.supportsLazyListeners); |
123 if (id == -1) | 126 if (id == -1) |
124 throw new Error("Can't add listener"); | 127 throw new Error("Can't add listener"); |
125 listener.id = id; | 128 listener.id = id; |
126 this.listenerMap_[id] = listener; | 129 this.listenerMap_[id] = listener; |
127 FilteredAttachmentStrategy.idToEventMap[id] = this.event_; | 130 FilteredAttachmentStrategy.idToEventMap[id] = this.event_; |
128 }; | 131 }; |
129 | 132 |
130 FilteredAttachmentStrategy.prototype.onRemovedListener = function(listener) { | 133 FilteredAttachmentStrategy.prototype.onRemovedListener = function(listener) { |
131 this.detachListener(listener, true); | 134 this.detachListener(listener, true); |
132 }; | 135 }; |
133 | 136 |
134 FilteredAttachmentStrategy.prototype.detachListener = | 137 FilteredAttachmentStrategy.prototype.detachListener = |
135 function(listener, manual) { | 138 function(listener, manual) { |
136 if (listener.id == undefined) | 139 if (listener.id == undefined) |
137 throw new Error("listener.id undefined - '" + listener + "'"); | 140 throw new Error("listener.id undefined - '" + listener + "'"); |
138 var id = listener.id; | 141 var id = listener.id; |
139 delete this.listenerMap_[id]; | 142 delete this.listenerMap_[id]; |
140 delete FilteredAttachmentStrategy.idToEventMap[id]; | 143 delete FilteredAttachmentStrategy.idToEventMap[id]; |
141 eventNatives.DetachFilteredEvent(id, manual); | 144 eventNatives.DetachFilteredEvent( |
| 145 id, manual, this.event_.eventOptions.supportsLazyListeners); |
142 }; | 146 }; |
143 | 147 |
144 FilteredAttachmentStrategy.prototype.detach = function(manual) { | 148 FilteredAttachmentStrategy.prototype.detach = function(manual) { |
145 for (var i in this.listenerMap_) | 149 for (var i in this.listenerMap_) |
146 this.detachListener(this.listenerMap_[i], manual); | 150 this.detachListener(this.listenerMap_[i], manual); |
147 }; | 151 }; |
148 | 152 |
149 FilteredAttachmentStrategy.prototype.getListenersByIDs = function(ids) { | 153 FilteredAttachmentStrategy.prototype.getListenersByIDs = function(ids) { |
150 var result = []; | 154 var result = []; |
151 for (var i = 0; i < ids.length; i++) | 155 for (var i = 0; i < ids.length; i++) |
152 $Array.push(result, this.listenerMap_[ids[i]]); | 156 $Array.push(result, this.listenerMap_[ids[i]]); |
153 return result; | 157 return result; |
154 }; | 158 }; |
155 | 159 |
156 function parseEventOptions(opt_eventOptions) { | 160 function parseEventOptions(opt_eventOptions) { |
157 return $Object.assign({ | 161 return $Object.assign({ |
158 __proto__: null, | 162 __proto__: null, |
159 }, { | 163 }, { |
160 // Event supports adding listeners with filters ("filtered events"), for | 164 // Event supports adding listeners with filters ("filtered events"), for |
161 // example as used in the webNavigation API. | 165 // example as used in the webNavigation API. |
162 // | 166 // |
163 // event.addListener(listener, [filter1, filter2]); | 167 // event.addListener(listener, [filter1, filter2]); |
164 supportsFilters: false, | 168 supportsFilters: false, |
165 | 169 |
166 // Events supports vanilla events. Most APIs use these. | 170 // Events supports vanilla events. Most APIs use these. |
167 // | 171 // |
168 // event.addListener(listener); | 172 // event.addListener(listener); |
169 supportsListeners: true, | 173 supportsListeners: true, |
170 | 174 |
| 175 // Event supports lazy listeners, where an extension can register a |
| 176 // listener to be used to "wake up" a lazy context. |
| 177 supportsLazyListeners: true, |
| 178 |
171 // Event supports adding rules ("declarative events") rather than | 179 // Event supports adding rules ("declarative events") rather than |
172 // listeners, for example as used in the declarativeWebRequest API. | 180 // listeners, for example as used in the declarativeWebRequest API. |
173 // | 181 // |
174 // event.addRules([rule1, rule2]); | 182 // event.addRules([rule1, rule2]); |
175 supportsRules: false, | 183 supportsRules: false, |
176 | 184 |
177 // Event is unmanaged in that the browser has no knowledge of its | 185 // Event is unmanaged in that the browser has no knowledge of its |
178 // existence; it's never invoked, doesn't keep the renderer alive, and | 186 // existence; it's never invoked, doesn't keep the renderer alive, and |
179 // the bindings system has no knowledge of it. | 187 // the bindings system has no knowledge of it. |
180 // | 188 // |
(...skipping 337 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
518 'getRules', | 526 'getRules', |
519 ], | 527 ], |
520 }); | 528 }); |
521 | 529 |
522 // NOTE: Event is (lazily) exposed as chrome.Event from dispatcher.cc. | 530 // NOTE: Event is (lazily) exposed as chrome.Event from dispatcher.cc. |
523 exports.$set('Event', Event); | 531 exports.$set('Event', Event); |
524 | 532 |
525 exports.$set('dispatchEvent', dispatchEvent); | 533 exports.$set('dispatchEvent', dispatchEvent); |
526 exports.$set('parseEventOptions', parseEventOptions); | 534 exports.$set('parseEventOptions', parseEventOptions); |
527 exports.$set('registerArgumentMassager', registerArgumentMassager); | 535 exports.$set('registerArgumentMassager', registerArgumentMassager); |
OLD | NEW |