Chromium Code Reviews| Index: chrome/renderer/resources/extensions/event.js |
| diff --git a/chrome/renderer/resources/extensions/event.js b/chrome/renderer/resources/extensions/event.js |
| index 81c465b0d83d5150c4402719784de7eb72825fda..80df0e9b95cec5b0f83344164252ecdbd3d7d702 100644 |
| --- a/chrome/renderer/resources/extensions/event.js |
| +++ b/chrome/renderer/resources/extensions/event.js |
| @@ -5,8 +5,32 @@ |
| var eventBindingsNatives = requireNative('event_bindings'); |
| var AttachEvent = eventBindingsNatives.AttachEvent; |
| var DetachEvent = eventBindingsNatives.DetachEvent; |
| + var sendRequest = require('sendRequest').sendRequest; |
| + var utils = require('utils'); |
| var chromeHidden = requireNative('chrome_hidden').GetChromeHidden(); |
| + var GetExtensionAPIDefinition = |
| + requireNative('apiDefinitions').GetExtensionAPIDefinition; |
| + |
| + // Schema definitions for the parameters of functions of Event. |
| + var addRulesParams; |
| + var getRulesParams; |
| + var removeRulesParams; |
|
not at google - send to devlin
2012/05/14 07:29:38
I find this a bit funny to look at out of context.
battre
2012/05/15 16:50:09
Done.
|
| + |
| + // This function needs to be called once (only in case Events with rules are |
| + // instantiated) to provide the schema definitions for function calls. |
| + // |eventsSchema| is the schema of the "events" namespace. |
| + function storeFunctionSchemes(eventsSchema) { |
| + // Schema definition of Events object. |
|
not at google - send to devlin
2012/05/14 07:29:38
Schemes -> Schemas?
battre
2012/05/15 16:50:09
Done.
|
| + var eventType = utils.lookup(eventsSchema.types, 'id', 'Event'); |
| + |
| + addRulesParams = |
| + utils.lookup(eventType.functions, 'name', 'addRules').parameters; |
| + getRulesParams = |
| + utils.lookup(eventType.functions, 'name', 'getRules').parameters; |
| + removeRulesParams = |
| + utils.lookup(eventType.functions, 'name', 'removeRules').parameters; |
| + } |
| // Local implementation of JSON.parse & JSON.stringify that protect us |
| // from being clobbered by an extension. |
| @@ -68,12 +92,13 @@ |
| if (this.eventOptions_.supportsRules && !opt_eventName) |
| throw new Error("Events that support rules require an event name."); |
| - // Validate event parameters if we are in debug. |
| + // Validate event arguments (the data that is passed to the callbacks) |
| + // if we are in debug. |
| if (opt_argSchemas && |
| chromeHidden.validateCallbacks && |
| chromeHidden.validate) { |
| - this.validate_ = function(args) { |
| + this.validateEventArgs_ = function(args) { |
| try { |
| chromeHidden.validate(args, opt_argSchemas); |
| } catch (exception) { |
| @@ -82,7 +107,12 @@ |
| } |
| }; |
| } else { |
| - this.validate_ = function() {} |
| + this.validateEventArgs_ = function() {} |
| + } |
| + |
| + // Initialize schema for functions lazily. |
| + if (this.eventOptions_.supportsRules && !addRulesParams) { |
| + storeFunctionSchemes(GetExtensionAPIDefinition("events")[0]); |
|
not at google - send to devlin
2012/05/14 07:29:38
You really only need to load these right before ca
battre
2012/05/15 16:50:09
Done.
|
| } |
| }; |
| @@ -192,7 +222,7 @@ |
| if (!this.eventOptions_.supportsListeners) |
| throw new Error("This event does not support listeners."); |
| var args = Array.prototype.slice.call(arguments); |
| - var validationErrors = this.validate_(args); |
| + var validationErrors = this.validateEventArgs_(args); |
| if (validationErrors) { |
| return {validationErrors: validationErrors}; |
| } |
| @@ -246,49 +276,69 @@ |
| chrome.Event.prototype.destroy_ = function() { |
| this.listeners_ = []; |
| - this.validate_ = []; |
| + this.validateEventArgs_ = []; |
| this.detach_(false); |
| }; |
| - // Gets the declarative API object, or undefined if this extension doesn't |
| - // have access to it. |
| - // |
| - // This is defined as a function (rather than a variable) because it isn't |
| - // accessible until the schema bindings have been generated. |
| - function getDeclarativeAPI() { |
| - return chromeHidden.internalAPIs.declarative; |
| - } |
| - |
| chrome.Event.prototype.addRules = function(rules, opt_cb) { |
| if (!this.eventOptions_.supportsRules) |
| throw new Error("This event does not support rules."); |
| - if (!getDeclarativeAPI()) { |
| - throw new Error("You must have permission to use the declarative " + |
| - "API to support rules in events"); |
| + |
| + // 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) { |
| + chromeHidden.validate([rule.conditions], [conditionsSchema]); |
| + chromeHidden.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.'); |
| } |
| - getDeclarativeAPI().addRules(this.eventName_, rules, opt_cb); |
| + |
| + validateRules(rules, |
| + this.eventOptions_.conditions, |
| + this.eventOptions_.actions); |
| + |
| + sendRequest("events.addRules", [this.eventName_, rules, opt_cb], |
| + addRulesParams); |
| } |
| chrome.Event.prototype.removeRules = function(ruleIdentifiers, opt_cb) { |
| if (!this.eventOptions_.supportsRules) |
| throw new Error("This event does not support rules."); |
| - if (!getDeclarativeAPI()) { |
| - throw new Error("You must have permission to use the declarative " + |
| - "API to support rules in events"); |
| - } |
| - getDeclarativeAPI().removeRules( |
| - this.eventName_, ruleIdentifiers, opt_cb); |
| + sendRequest("events.removeRules", |
| + [this.eventName_, ruleIdentifiers, opt_cb], |
| + removeRulesParams); |
| } |
| chrome.Event.prototype.getRules = function(ruleIdentifiers, cb) { |
| if (!this.eventOptions_.supportsRules) |
| throw new Error("This event does not support rules."); |
| - if (!getDeclarativeAPI()) { |
| - throw new Error("You must have permission to use the declarative " + |
| - "API to support rules in events"); |
| - } |
| - getDeclarativeAPI().getRules( |
| - this.eventName_, ruleIdentifiers, cb); |
| + sendRequest("events.getRules", |
| + [this.eventName_, ruleIdentifiers, cb], |
| + getRulesParams); |
| } |
| // Special load events: we don't use the DOM unload because that slows |