1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
var DCHECK = requireNative('logging').DCHECK;
// TODO(cduvall/kalman): json_schema shouldn't put things on chromeHidden.
require('json_schema');
var eventBindingsNatives = requireNative('event_bindings');
var AttachEvent = eventBindingsNatives.AttachEvent;
var DetachEvent = eventBindingsNatives.DetachEvent;
var AttachFilteredEvent = eventBindingsNatives.AttachFilteredEvent;
var DetachFilteredEvent = eventBindingsNatives.DetachFilteredEvent;
var MatchAgainstEventFilter = eventBindingsNatives.MatchAgainstEventFilter;
var sendRequest = require('sendRequest').sendRequest;
var utils = require('utils');
var validate = require('schemaUtils').validate;
var chromeHidden = requireNative('chrome_hidden').GetChromeHidden();
var chrome = requireNative('chrome').GetChrome();
var schemaRegistry = requireNative('schema_registry');
// Schemas for the rule-style functions on the events API that
// only need to be generated occasionally, so populate them lazily.
var ruleFunctionSchemas = {
// These values are set lazily:
// addRules: {},
// getRules: {},
// removeRules: {}
};
// This function ensures that |ruleFunctionSchemas| is populated.
function ensureRuleSchemasLoaded() {
if (ruleFunctionSchemas.addRules)
return;
var eventsSchema = schemaRegistry.GetSchema("events");
var eventType = utils.lookup(eventsSchema.types, 'id', 'events.Event');
ruleFunctionSchemas.addRules =
utils.lookup(eventType.functions, 'name', 'addRules');
ruleFunctionSchemas.getRules =
utils.lookup(eventType.functions, 'name', 'getRules');
ruleFunctionSchemas.removeRules =
utils.lookup(eventType.functions, 'name', 'removeRules');
}
// A map of event names to the event object that is registered to that name.
var attachedNamedEvents = {};
// An array of all attached event objects, used for detaching on unload.
var allAttachedEvents = [];
// A map of functions that massage event arguments before they are dispatched.
// Key is event name, value is function.
var eventArgumentMassagers = {};
// Handles adding/removing/dispatching listeners for unfiltered events.
var UnfilteredAttachmentStrategy = function(event) {
this.event_ = event;
};
UnfilteredAttachmentStrategy.prototype.onAddedListener =
function(listener) {
// Only attach / detach on the first / last listener removed.
if (this.event_.listeners_.length == 0)
AttachEvent(this.event_.eventName_);
};
UnfilteredAttachmentStrategy.prototype.onRemovedListener =
function(listener) {
if (this.event_.listeners_.length == 0)
this.detach(true);
};
UnfilteredAttachmentStrategy.prototype.detach = function(manual) {
DetachEvent(this.event_.eventName_, manual);
};
UnfilteredAttachmentStrategy.prototype.getListenersByIDs = function(ids) {
return this.event_.listeners_;
};
var FilteredAttachmentStrategy = function(event) {
this.event_ = event;
this.listenerMap_ = {};
};
FilteredAttachmentStrategy.idToEventMap = {};
FilteredAttachmentStrategy.prototype.onAddedListener = function(listener) {
var id = AttachFilteredEvent(this.event_.eventName_,
listener.filters || {});
if (id == -1)
throw new Error("Can't add listener");
listener.id = id;
this.listenerMap_[id] = listener;
FilteredAttachmentStrategy.idToEventMap[id] = this.event_;
};
FilteredAttachmentStrategy.prototype.onRemovedListener = function(listener) {
this.detachListener(listener, true);
};
FilteredAttachmentStrategy.prototype.detachListener =
function(listener, manual) {
if (listener.id == undefined)
throw new Error("listener.id undefined - '" + listener + "'");
var id = listener.id;
delete this.listenerMap_[id];
delete FilteredAttachmentStrategy.idToEventMap[id];
DetachFilteredEvent(id, manual);
};
FilteredAttachmentStrategy.prototype.detach = function(manual) {
for (var i in this.listenerMap_)
this.detachListener(this.listenerMap_[i], manual);
};
FilteredAttachmentStrategy.prototype.getListenersByIDs = function(ids) {
var result = [];
for (var i = 0; i < ids.length; i++)
result.push(this.listenerMap_[ids[i]]);
return result;
};
chromeHidden.parseEventOptions = function(opt_eventOptions) {
function merge(dest, src) {
for (var k in src) {
if (!dest.hasOwnProperty(k)) {
dest[k] = src[k];
}
}
}
var options = opt_eventOptions || {};
merge(options,
{supportsFilters: false,
supportsListeners: true,
supportsRules: false,
});
return options;
};
// Event object. If opt_eventName is provided, this object represents
// the unique instance of that named event, and dispatching an event
// with that name will route through this object's listeners. Note that
// opt_eventName is required for events that support rules.
//
// Example:
// chrome.tabs.onChanged = new chrome.Event("tab-changed");
// chrome.tabs.onChanged.addListener(function(data) { alert(data); });
// chromeHidden.Event.dispatch("tab-changed", "hi");
// will result in an alert dialog that says 'hi'.
//
// If opt_eventOptions exists, it is a dictionary that contains the boolean
// entries "supportsListeners" and "supportsRules".
var Event = function(opt_eventName, opt_argSchemas, opt_eventOptions) {
this.eventName_ = opt_eventName;
this.listeners_ = [];
this.eventOptions_ = chromeHidden.parseEventOptions(opt_eventOptions);
if (this.eventOptions_.supportsRules && !opt_eventName)
throw new Error("Events that support rules require an event name.");
if (this.eventOptions_.supportsFilters) {
this.attachmentStrategy_ = new FilteredAttachmentStrategy(this);
} else {
this.attachmentStrategy_ = new UnfilteredAttachmentStrategy(this);
}
// Validate event arguments (the data that is passed to the callbacks)
// if we are in debug.
if (opt_argSchemas &&
chromeHidden.validateCallbacks) {
this.validateEventArgs_ = function(args) {
try {
validate(args, opt_argSchemas);
} catch (exception) {
return "Event validation error during " + opt_eventName + " -- " +
exception;
}
};
} else {
this.validateEventArgs_ = function() {}
}
};
chromeHidden.Event = {};
// callback is a function(args, dispatch). args are the args we receive from
// dispatchEvent(), and dispatch is a function(args) that dispatches args to
// its listeners.
chromeHidden.Event.registerArgumentMassager = function(name, callback) {
if (eventArgumentMassagers[name])
throw new Error("Massager already registered for event: " + name);
eventArgumentMassagers[name] = callback;
};
// Dispatches a named event with the given argument array. The args array is
// the list of arguments that will be sent to the event callback.
chromeHidden.Event.dispatchEvent = function(name, args, filteringInfo) {
var listenerIDs = null;
if (filteringInfo)
listenerIDs = MatchAgainstEventFilter(name, filteringInfo);
var event = attachedNamedEvents[name];
if (!event)
return;
var dispatchArgs = function(args) {
var result = event.dispatch_(args, listenerIDs);
if (result)
DCHECK(!result.validationErrors, result.validationErrors);
return result;
};
if (eventArgumentMassagers[name])
eventArgumentMassagers[name](args, dispatchArgs);
else
dispatchArgs(args);
};
// Test if a named event has any listeners.
chromeHidden.Event.hasListener = function(name) {
return (attachedNamedEvents[name] &&
attachedNamedEvents[name].listeners_.length > 0);
};
// Registers a callback to be called when this event is dispatched.
Event.prototype.addListener = function(cb, filters) {
if (!this.eventOptions_.supportsListeners)
throw new Error("This event does not support listeners.");
if (this.eventOptions_.maxListeners &&
this.getListenerCount() >= this.eventOptions_.maxListeners)
throw new Error("Too many listeners for " + this.eventName_);
if (filters) {
if (!this.eventOptions_.supportsFilters)
throw new Error("This event does not support filters.");
if (filters.url && !(filters.url instanceof Array))
throw new Error("filters.url should be an array");
}
var listener = {callback: cb, filters: filters};
this.attach_(listener);
this.listeners_.push(listener);
};
Event.prototype.attach_ = function(listener) {
this.attachmentStrategy_.onAddedListener(listener);
if (this.listeners_.length == 0) {
allAttachedEvents[allAttachedEvents.length] = this;
if (!this.eventName_)
return;
if (attachedNamedEvents[this.eventName_]) {
throw new Error("chrome.Event '" + this.eventName_ +
"' is already attached.");
}
attachedNamedEvents[this.eventName_] = this;
}
};
// Unregisters a callback.
Event.prototype.removeListener = function(cb) {
if (!this.eventOptions_.supportsListeners)
throw new Error("This event does not support listeners.");
var idx = this.findListener_(cb);
if (idx == -1) {
return;
}
var removedListener = this.listeners_.splice(idx, 1)[0];
this.attachmentStrategy_.onRemovedListener(removedListener);
if (this.listeners_.length == 0) {
var i = allAttachedEvents.indexOf(this);
if (i >= 0)
delete allAttachedEvents[i];
if (!this.eventName_)
return;
if (!attachedNamedEvents[this.eventName_]) {
throw new Error("chrome.Event '" + this.eventName_ +
"' is not attached.");
}
delete attachedNamedEvents[this.eventName_];
}
};
// Test if the given callback is registered for this event.
Event.prototype.hasListener = function(cb) {
if (!this.eventOptions_.supportsListeners)
throw new Error("This event does not support listeners.");
return this.findListener_(cb) > -1;
};
// Test if any callbacks are registered for this event.
Event.prototype.hasListeners = function() {
return this.getListenerCount() > 0;
};
// Return the number of listeners on this event.
Event.prototype.getListenerCount = function() {
if (!this.eventOptions_.supportsListeners)
throw new Error("This event does not support listeners.");
return this.listeners_.length;
};
// Returns the index of the given callback if registered, or -1 if not
// found.
Event.prototype.findListener_ = function(cb) {
for (var i = 0; i < this.listeners_.length; i++) {
if (this.listeners_[i].callback == cb) {
return i;
}
}
return -1;
};
Event.prototype.dispatch_ = function(args, listenerIDs) {
if (!this.eventOptions_.supportsListeners)
throw new Error("This event does not support listeners.");
var validationErrors = this.validateEventArgs_(args);
if (validationErrors) {
console.error(validationErrors);
return {validationErrors: validationErrors};
}
// Make a copy of the listeners in case the listener list is modified
// while dispatching the event.
var listeners =
this.attachmentStrategy_.getListenersByIDs(listenerIDs).slice();
var results = [];
for (var i = 0; i < listeners.length; i++) {
try {
var result = this.dispatchToListener(listeners[i].callback, args);
if (result !== undefined)
results.push(result);
} catch (e) {
console.error("Error in event handler for '" + this.eventName_ +
"': " + e.message + ' ' + e.stack);
}
}
if (results.length)
return {results: results};
}
// Can be overridden to support custom dispatching.
Event.prototype.dispatchToListener = function(callback, args) {
return callback.apply(null, args);
}
// Dispatches this event object to all listeners, passing all supplied
// arguments to this function each listener.
Event.prototype.dispatch = function(varargs) {
return this.dispatch_(Array.prototype.slice.call(arguments), undefined);
};
// Detaches this event object from its name.
Event.prototype.detach_ = function() {
this.attachmentStrategy_.detach(false);
};
Event.prototype.destroy_ = function() {
this.listeners_ = [];
this.validateEventArgs_ = [];
this.detach_(false);
};
Event.prototype.addRules = function(rules, opt_cb) {
if (!this.eventOptions_.supportsRules)
throw new Error("This event does not support rules.");
// Takes a list of JSON datatype identifiers and returns a schema fragment
// that verifies that a JSON object corresponds to an array of only these
// data types.
function buildArrayOfChoicesSchema(typesList) {
return {
'type': 'array',
'items': {
'choices': typesList.map(function(el) {return {'$ref': el};})
}
};
};
// Validate conditions and actions against specific schemas of this
// event object type.
// |rules| is an array of JSON objects that follow the Rule type of the
// declarative extension APIs. |conditions| is an array of JSON type
// identifiers that are allowed to occur in the conditions attribute of each
// rule. Likewise, |actions| is an array of JSON type identifiers that are
// allowed to occur in the actions attribute of each rule.
function validateRules(rules, conditions, actions) {
var conditionsSchema = buildArrayOfChoicesSchema(conditions);
var actionsSchema = buildArrayOfChoicesSchema(actions);
rules.forEach(function(rule) {
validate([rule.conditions], [conditionsSchema]);
validate([rule.actions], [actionsSchema]);
})
};
if (!this.eventOptions_.conditions || !this.eventOptions_.actions) {
throw new Error('Event ' + this.eventName_ + ' misses conditions or ' +
'actions in the API specification.');
}
validateRules(rules,
this.eventOptions_.conditions,
this.eventOptions_.actions);
ensureRuleSchemasLoaded();
// We remove the first parameter from the validation to give the user more
// meaningful error messages.
validate([rules, opt_cb],
ruleFunctionSchemas.addRules.parameters.slice().splice(1));
sendRequest("events.addRules", [this.eventName_, rules, opt_cb],
ruleFunctionSchemas.addRules.parameters);
}
Event.prototype.removeRules = function(ruleIdentifiers, opt_cb) {
if (!this.eventOptions_.supportsRules)
throw new Error("This event does not support rules.");
ensureRuleSchemasLoaded();
// We remove the first parameter from the validation to give the user more
// meaningful error messages.
validate([ruleIdentifiers, opt_cb],
ruleFunctionSchemas.removeRules.parameters.slice().splice(1));
sendRequest("events.removeRules",
[this.eventName_, ruleIdentifiers, opt_cb],
ruleFunctionSchemas.removeRules.parameters);
}
Event.prototype.getRules = function(ruleIdentifiers, cb) {
if (!this.eventOptions_.supportsRules)
throw new Error("This event does not support rules.");
ensureRuleSchemasLoaded();
// We remove the first parameter from the validation to give the user more
// meaningful error messages.
validate([ruleIdentifiers, cb],
ruleFunctionSchemas.getRules.parameters.slice().splice(1));
sendRequest("events.getRules",
[this.eventName_, ruleIdentifiers, cb],
ruleFunctionSchemas.getRules.parameters);
}
// Special load events: we don't use the DOM unload because that slows
// down tab shutdown. On the other hand, onUnload might not always fire,
// since Chrome will terminate renderers on shutdown (SuddenTermination).
chromeHidden.onLoad = new Event();
chromeHidden.onUnload = new Event();
chromeHidden.dispatchOnLoad =
chromeHidden.onLoad.dispatch.bind(chromeHidden.onLoad);
chromeHidden.dispatchOnUnload = function() {
chromeHidden.onUnload.dispatch();
chromeHidden.wasUnloaded = true;
for (var i = 0; i < allAttachedEvents.length; ++i) {
var event = allAttachedEvents[i];
if (event)
event.detach_();
}
};
chromeHidden.dispatchError = function(msg) {
console.error(msg);
};
chrome.Event = Event;
|