Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / chrome / renderer / resources / extensions / event.js
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
3 // found in the LICENSE file.
4
5   var eventNatives = requireNative('event_natives');
6   var logging = requireNative('logging');
7   var schemaRegistry = requireNative('schema_registry');
8   var sendRequest = require('sendRequest').sendRequest;
9   var utils = require('utils');
10   var validate = require('schemaUtils').validate;
11   var unloadEvent = require('unload_event');
12
13   // Schemas for the rule-style functions on the events API that
14   // only need to be generated occasionally, so populate them lazily.
15   var ruleFunctionSchemas = {
16     // These values are set lazily:
17     // addRules: {},
18     // getRules: {},
19     // removeRules: {}
20   };
21
22   // This function ensures that |ruleFunctionSchemas| is populated.
23   function ensureRuleSchemasLoaded() {
24     if (ruleFunctionSchemas.addRules)
25       return;
26     var eventsSchema = schemaRegistry.GetSchema("events");
27     var eventType = utils.lookup(eventsSchema.types, 'id', 'events.Event');
28
29     ruleFunctionSchemas.addRules =
30         utils.lookup(eventType.functions, 'name', 'addRules');
31     ruleFunctionSchemas.getRules =
32         utils.lookup(eventType.functions, 'name', 'getRules');
33     ruleFunctionSchemas.removeRules =
34         utils.lookup(eventType.functions, 'name', 'removeRules');
35   }
36
37   // A map of event names to the event object that is registered to that name.
38   var attachedNamedEvents = {};
39
40   // An array of all attached event objects, used for detaching on unload.
41   var allAttachedEvents = [];
42
43   // A map of functions that massage event arguments before they are dispatched.
44   // Key is event name, value is function.
45   var eventArgumentMassagers = {};
46
47   // An attachment strategy for events that aren't attached to the browser.
48   // This applies to events with the "unmanaged" option and events without
49   // names.
50   var NullAttachmentStrategy = function(event) {
51     this.event_ = event;
52   };
53   NullAttachmentStrategy.prototype.onAddedListener =
54       function(listener) {
55   };
56   NullAttachmentStrategy.prototype.onRemovedListener =
57       function(listener) {
58   };
59   NullAttachmentStrategy.prototype.detach = function(manual) {
60   };
61   NullAttachmentStrategy.prototype.getListenersByIDs = function(ids) {
62     // |ids| is for filtered events only.
63     return this.event_.listeners;
64   };
65
66   // Handles adding/removing/dispatching listeners for unfiltered events.
67   var UnfilteredAttachmentStrategy = function(event) {
68     this.event_ = event;
69   };
70
71   UnfilteredAttachmentStrategy.prototype.onAddedListener =
72       function(listener) {
73     // Only attach / detach on the first / last listener removed.
74     if (this.event_.listeners.length == 0)
75       eventNatives.AttachEvent(this.event_.eventName);
76   };
77
78   UnfilteredAttachmentStrategy.prototype.onRemovedListener =
79       function(listener) {
80     if (this.event_.listeners.length == 0)
81       this.detach(true);
82   };
83
84   UnfilteredAttachmentStrategy.prototype.detach = function(manual) {
85     eventNatives.DetachEvent(this.event_.eventName, manual);
86   };
87
88   UnfilteredAttachmentStrategy.prototype.getListenersByIDs = function(ids) {
89     // |ids| is for filtered events only.
90     return this.event_.listeners;
91   };
92
93   var FilteredAttachmentStrategy = function(event) {
94     this.event_ = event;
95     this.listenerMap_ = {};
96   };
97
98   FilteredAttachmentStrategy.idToEventMap = {};
99
100   FilteredAttachmentStrategy.prototype.onAddedListener = function(listener) {
101     var id = eventNatives.AttachFilteredEvent(this.event_.eventName,
102                                               listener.filters || {});
103     if (id == -1)
104       throw new Error("Can't add listener");
105     listener.id = id;
106     this.listenerMap_[id] = listener;
107     FilteredAttachmentStrategy.idToEventMap[id] = this.event_;
108   };
109
110   FilteredAttachmentStrategy.prototype.onRemovedListener = function(listener) {
111     this.detachListener(listener, true);
112   };
113
114   FilteredAttachmentStrategy.prototype.detachListener =
115       function(listener, manual) {
116     if (listener.id == undefined)
117       throw new Error("listener.id undefined - '" + listener + "'");
118     var id = listener.id;
119     delete this.listenerMap_[id];
120     delete FilteredAttachmentStrategy.idToEventMap[id];
121     eventNatives.DetachFilteredEvent(id, manual);
122   };
123
124   FilteredAttachmentStrategy.prototype.detach = function(manual) {
125     for (var i in this.listenerMap_)
126       this.detachListener(this.listenerMap_[i], manual);
127   };
128
129   FilteredAttachmentStrategy.prototype.getListenersByIDs = function(ids) {
130     var result = [];
131     for (var i = 0; i < ids.length; i++)
132       $Array.push(result, this.listenerMap_[ids[i]]);
133     return result;
134   };
135
136   function parseEventOptions(opt_eventOptions) {
137     function merge(dest, src) {
138       for (var k in src) {
139         if (!$Object.hasOwnProperty(dest, k)) {
140           dest[k] = src[k];
141         }
142       }
143     }
144
145     var options = opt_eventOptions || {};
146     merge(options, {
147       // Event supports adding listeners with filters ("filtered events"), for
148       // example as used in the webNavigation API.
149       //
150       // event.addListener(listener, [filter1, filter2]);
151       supportsFilters: false,
152
153       // Events supports vanilla events. Most APIs use these.
154       //
155       // event.addListener(listener);
156       supportsListeners: true,
157
158       // Event supports adding rules ("declarative events") rather than
159       // listeners, for example as used in the declarativeWebRequest API.
160       //
161       // event.addRules([rule1, rule2]);
162       supportsRules: false,
163
164       // Event is unmanaged in that the browser has no knowledge of its
165       // existence; it's never invoked, doesn't keep the renderer alive, and
166       // the bindings system has no knowledge of it.
167       //
168       // Both events created by user code (new chrome.Event()) and messaging
169       // events are unmanaged, though in the latter case the browser *does*
170       // interact indirectly with them via IPCs written by hand.
171       unmanaged: false,
172     });
173     return options;
174   };
175
176   // Event object.  If opt_eventName is provided, this object represents
177   // the unique instance of that named event, and dispatching an event
178   // with that name will route through this object's listeners. Note that
179   // opt_eventName is required for events that support rules.
180   //
181   // Example:
182   //   var Event = require('event_bindings').Event;
183   //   chrome.tabs.onChanged = new Event("tab-changed");
184   //   chrome.tabs.onChanged.addListener(function(data) { alert(data); });
185   //   Event.dispatch("tab-changed", "hi");
186   // will result in an alert dialog that says 'hi'.
187   //
188   // If opt_eventOptions exists, it is a dictionary that contains the boolean
189   // entries "supportsListeners" and "supportsRules".
190   // If opt_webViewInstanceId exists, it is an integer uniquely identifying a
191   // <webview> tag within the embedder. If it does not exist, then this is an
192   // extension event rather than a <webview> event.
193   var EventImpl = function(opt_eventName, opt_argSchemas, opt_eventOptions,
194                            opt_webViewInstanceId) {
195     this.eventName = opt_eventName;
196     this.argSchemas = opt_argSchemas;
197     this.listeners = [];
198     this.eventOptions = parseEventOptions(opt_eventOptions);
199     this.webViewInstanceId = opt_webViewInstanceId || 0;
200
201     if (!this.eventName) {
202       if (this.eventOptions.supportsRules)
203         throw new Error("Events that support rules require an event name.");
204       // Events without names cannot be managed by the browser by definition
205       // (the browser has no way of identifying them).
206       this.eventOptions.unmanaged = true;
207     }
208
209     // Track whether the event has been destroyed to help track down the cause
210     // of http://crbug.com/258526.
211     // This variable will eventually hold the stack trace of the destroy call.
212     // TODO(kalman): Delete this and replace with more sound logic that catches
213     // when events are used without being *attached*.
214     this.destroyed = null;
215
216     if (this.eventOptions.unmanaged)
217       this.attachmentStrategy = new NullAttachmentStrategy(this);
218     else if (this.eventOptions.supportsFilters)
219       this.attachmentStrategy = new FilteredAttachmentStrategy(this);
220     else
221       this.attachmentStrategy = new UnfilteredAttachmentStrategy(this);
222   };
223
224   // callback is a function(args, dispatch). args are the args we receive from
225   // dispatchEvent(), and dispatch is a function(args) that dispatches args to
226   // its listeners.
227   function registerArgumentMassager(name, callback) {
228     if (eventArgumentMassagers[name])
229       throw new Error("Massager already registered for event: " + name);
230     eventArgumentMassagers[name] = callback;
231   }
232
233   // Dispatches a named event with the given argument array. The args array is
234   // the list of arguments that will be sent to the event callback.
235   function dispatchEvent(name, args, filteringInfo) {
236     var listenerIDs = [];
237
238     if (filteringInfo)
239       listenerIDs = eventNatives.MatchAgainstEventFilter(name, filteringInfo);
240
241     var event = attachedNamedEvents[name];
242     if (!event)
243       return;
244
245     var dispatchArgs = function(args) {
246       var result = event.dispatch_(args, listenerIDs);
247       if (result)
248         logging.DCHECK(!result.validationErrors, result.validationErrors);
249       return result;
250     };
251
252     if (eventArgumentMassagers[name])
253       eventArgumentMassagers[name](args, dispatchArgs);
254     else
255       dispatchArgs(args);
256   }
257
258   // Registers a callback to be called when this event is dispatched.
259   EventImpl.prototype.addListener = function(cb, filters) {
260     if (!this.eventOptions.supportsListeners)
261       throw new Error("This event does not support listeners.");
262     if (this.eventOptions.maxListeners &&
263         this.getListenerCount() >= this.eventOptions.maxListeners) {
264       throw new Error("Too many listeners for " + this.eventName);
265     }
266     if (filters) {
267       if (!this.eventOptions.supportsFilters)
268         throw new Error("This event does not support filters.");
269       if (filters.url && !(filters.url instanceof Array))
270         throw new Error("filters.url should be an array.");
271       if (filters.serviceType &&
272           !(typeof filters.serviceType === 'string')) {
273         throw new Error("filters.serviceType should be a string.")
274       }
275     }
276     var listener = {callback: cb, filters: filters};
277     this.attach_(listener);
278     $Array.push(this.listeners, listener);
279   };
280
281   EventImpl.prototype.attach_ = function(listener) {
282     this.attachmentStrategy.onAddedListener(listener);
283
284     if (this.listeners.length == 0) {
285       allAttachedEvents[allAttachedEvents.length] = this;
286       if (this.eventName) {
287         if (attachedNamedEvents[this.eventName]) {
288           throw new Error("Event '" + this.eventName +
289                           "' is already attached.");
290         }
291         attachedNamedEvents[this.eventName] = this;
292       }
293     }
294   };
295
296   // Unregisters a callback.
297   EventImpl.prototype.removeListener = function(cb) {
298     if (!this.eventOptions.supportsListeners)
299       throw new Error("This event does not support listeners.");
300
301     var idx = this.findListener_(cb);
302     if (idx == -1)
303       return;
304
305     var removedListener = $Array.splice(this.listeners, idx, 1)[0];
306     this.attachmentStrategy.onRemovedListener(removedListener);
307
308     if (this.listeners.length == 0) {
309       var i = $Array.indexOf(allAttachedEvents, this);
310       if (i >= 0)
311         delete allAttachedEvents[i];
312       if (this.eventName) {
313         if (!attachedNamedEvents[this.eventName]) {
314           throw new Error(
315               "Event '" + this.eventName + "' is not attached.");
316         }
317         delete attachedNamedEvents[this.eventName];
318       }
319     }
320   };
321
322   // Test if the given callback is registered for this event.
323   EventImpl.prototype.hasListener = function(cb) {
324     if (!this.eventOptions.supportsListeners)
325       throw new Error("This event does not support listeners.");
326     return this.findListener_(cb) > -1;
327   };
328
329   // Test if any callbacks are registered for this event.
330   EventImpl.prototype.hasListeners = function() {
331     return this.getListenerCount() > 0;
332   };
333
334   // Return the number of listeners on this event.
335   EventImpl.prototype.getListenerCount = function() {
336     if (!this.eventOptions.supportsListeners)
337       throw new Error("This event does not support listeners.");
338     return this.listeners.length;
339   };
340
341   // Returns the index of the given callback if registered, or -1 if not
342   // found.
343   EventImpl.prototype.findListener_ = function(cb) {
344     for (var i = 0; i < this.listeners.length; i++) {
345       if (this.listeners[i].callback == cb) {
346         return i;
347       }
348     }
349
350     return -1;
351   };
352
353   EventImpl.prototype.dispatch_ = function(args, listenerIDs) {
354     if (this.destroyed) {
355       throw new Error(this.eventName + ' was already destroyed at: ' +
356                       this.destroyed);
357     }
358     if (!this.eventOptions.supportsListeners)
359       throw new Error("This event does not support listeners.");
360
361     if (this.argSchemas && logging.DCHECK_IS_ON()) {
362       try {
363         validate(args, this.argSchemas);
364       } catch (e) {
365         e.message += ' in ' + this.eventName;
366         throw e;
367       }
368     }
369
370     // Make a copy of the listeners in case the listener list is modified
371     // while dispatching the event.
372     var listeners = $Array.slice(
373         this.attachmentStrategy.getListenersByIDs(listenerIDs));
374
375     var results = [];
376     for (var i = 0; i < listeners.length; i++) {
377       try {
378         var result = this.wrapper.dispatchToListener(listeners[i].callback,
379                                                      args);
380         if (result !== undefined)
381           $Array.push(results, result);
382       } catch (e) {
383         console.error(
384           'Error in event handler for ' +
385           (this.eventName ? this.eventName : '(unknown)') +
386           ': ' + e.message + '\nStack trace: ' + e.stack);
387       }
388     }
389     if (results.length)
390       return {results: results};
391   }
392
393   // Can be overridden to support custom dispatching.
394   EventImpl.prototype.dispatchToListener = function(callback, args) {
395     return $Function.apply(callback, null, args);
396   }
397
398   // Dispatches this event object to all listeners, passing all supplied
399   // arguments to this function each listener.
400   EventImpl.prototype.dispatch = function(varargs) {
401     return this.dispatch_($Array.slice(arguments), undefined);
402   };
403
404   // Detaches this event object from its name.
405   EventImpl.prototype.detach_ = function() {
406     this.attachmentStrategy.detach(false);
407   };
408
409   EventImpl.prototype.destroy_ = function() {
410     this.listeners.length = 0;
411     this.detach_();
412     this.destroyed = new Error().stack;
413   };
414
415   EventImpl.prototype.addRules = function(rules, opt_cb) {
416     if (!this.eventOptions.supportsRules)
417       throw new Error("This event does not support rules.");
418
419     // Takes a list of JSON datatype identifiers and returns a schema fragment
420     // that verifies that a JSON object corresponds to an array of only these
421     // data types.
422     function buildArrayOfChoicesSchema(typesList) {
423       return {
424         'type': 'array',
425         'items': {
426           'choices': typesList.map(function(el) {return {'$ref': el};})
427         }
428       };
429     };
430
431     // Validate conditions and actions against specific schemas of this
432     // event object type.
433     // |rules| is an array of JSON objects that follow the Rule type of the
434     // declarative extension APIs. |conditions| is an array of JSON type
435     // identifiers that are allowed to occur in the conditions attribute of each
436     // rule. Likewise, |actions| is an array of JSON type identifiers that are
437     // allowed to occur in the actions attribute of each rule.
438     function validateRules(rules, conditions, actions) {
439       var conditionsSchema = buildArrayOfChoicesSchema(conditions);
440       var actionsSchema = buildArrayOfChoicesSchema(actions);
441       $Array.forEach(rules, function(rule) {
442         validate([rule.conditions], [conditionsSchema]);
443         validate([rule.actions], [actionsSchema]);
444       });
445     };
446
447     if (!this.eventOptions.conditions || !this.eventOptions.actions) {
448       throw new Error('Event ' + this.eventName + ' misses ' +
449                       'conditions or actions in the API specification.');
450     }
451
452     validateRules(rules,
453                   this.eventOptions.conditions,
454                   this.eventOptions.actions);
455
456     ensureRuleSchemasLoaded();
457     // We remove the first parameter from the validation to give the user more
458     // meaningful error messages.
459     validate([this.webViewInstanceId, rules, opt_cb],
460              $Array.splice(
461                  $Array.slice(ruleFunctionSchemas.addRules.parameters), 1));
462     sendRequest(
463       "events.addRules",
464       [this.eventName, this.webViewInstanceId, rules,  opt_cb],
465       ruleFunctionSchemas.addRules.parameters);
466   }
467
468   EventImpl.prototype.removeRules = function(ruleIdentifiers, opt_cb) {
469     if (!this.eventOptions.supportsRules)
470       throw new Error("This event does not support rules.");
471     ensureRuleSchemasLoaded();
472     // We remove the first parameter from the validation to give the user more
473     // meaningful error messages.
474     validate([this.webViewInstanceId, ruleIdentifiers, opt_cb],
475              $Array.splice(
476                  $Array.slice(ruleFunctionSchemas.removeRules.parameters), 1));
477     sendRequest("events.removeRules",
478                 [this.eventName,
479                  this.webViewInstanceId,
480                  ruleIdentifiers,
481                  opt_cb],
482                 ruleFunctionSchemas.removeRules.parameters);
483   }
484
485   EventImpl.prototype.getRules = function(ruleIdentifiers, cb) {
486     if (!this.eventOptions.supportsRules)
487       throw new Error("This event does not support rules.");
488     ensureRuleSchemasLoaded();
489     // We remove the first parameter from the validation to give the user more
490     // meaningful error messages.
491     validate([this.webViewInstanceId, ruleIdentifiers, cb],
492              $Array.splice(
493                  $Array.slice(ruleFunctionSchemas.getRules.parameters), 1));
494
495     sendRequest(
496       "events.getRules",
497       [this.eventName, this.webViewInstanceId, ruleIdentifiers, cb],
498       ruleFunctionSchemas.getRules.parameters);
499   }
500
501   unloadEvent.addListener(function() {
502     for (var i = 0; i < allAttachedEvents.length; ++i) {
503       var event = allAttachedEvents[i];
504       if (event)
505         event.detach_();
506     }
507   });
508
509   var Event = utils.expose(EventImpl, [
510     'addListener',
511     'removeListener',
512     'hasListener',
513     'hasListeners',
514     'getListenerCount',
515     'dispatchToListener',
516     'dispatch',
517     'addRules',
518     'removeRules',
519     'getRules'
520   ]);
521
522   // NOTE: Event is (lazily) exposed as chrome.Event from dispatcher.cc.
523   exports.Event = Event;
524
525   exports.dispatchEvent = dispatchEvent;
526   exports.parseEventOptions = parseEventOptions;
527   exports.registerArgumentMassager = registerArgumentMassager;