Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(51)

Unified Diff: chrome/renderer/resources/extensions/event.js

Issue 10392008: Move declarative API into events API (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Addressed comments Created 8 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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..39e1a26c350160c52acf6d25590c0bc338a1c961 100644
--- a/chrome/renderer/resources/extensions/event.js
+++ b/chrome/renderer/resources/extensions/event.js
@@ -5,6 +5,8 @@
var eventBindingsNatives = requireNative('event_bindings');
var AttachEvent = eventBindingsNatives.AttachEvent;
var DetachEvent = eventBindingsNatives.DetachEvent;
+ var sendRequest = require('sendRequest').sendRequest;
+ var lookup = require('utils').lookup;
var chromeHidden = requireNative('chrome_hidden').GetChromeHidden();
@@ -59,21 +61,24 @@
//
// If opt_eventOptions exists, it is a dictionary that contains the boolean
// entries "supportsListeners" and "supportsRules".
- chrome.Event = function(opt_eventName, opt_argSchemas, opt_eventOptions) {
+ chrome.Event = function(
+ opt_eventName, opt_argSchemas, opt_eventOptions, opt_eventsSchema) {
Aaron Boodman 2012/05/10 17:14:49 The need for the 'event' type's schema is an imple
battre 2012/05/10 18:05:22 I am just concerned that we might end up with mult
Aaron Boodman 2012/05/10 18:27:50 V8SchemaRegistry caches the js objects across the
this.eventName_ = opt_eventName;
this.listeners_ = [];
this.eventOptions_ = opt_eventOptions ||
{"supportsListeners": true, "supportsRules": false};
+ this.eventsSchema_ = opt_eventsSchema;
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 +87,26 @@
}
};
} else {
- this.validate_ = function() {}
+ this.validateEventArgs_ = function() {}
+ }
+
+ if (this.eventOptions_.supportsRules) {
+ if (!this.eventsSchema_) {
+ throw new Error("Events with rules support require the schema of the " +
+ "chrome.events namespace");
+ }
+ // Determine schemas of function parameters.
+ var eventType = lookup(this.eventsSchema_.types, 'id', 'Event');
Aaron Boodman 2012/05/10 17:14:49 This lookup is done for every event instance. It w
battre 2012/05/10 18:05:22 Done.
+ this.addRulesParams_ =
+ lookup(eventType.functions, 'name', 'addRules').parameters;
+ this.getRulesParams_ =
+ lookup(eventType.functions, 'name', 'getRules').parameters;
+ this.removeRulesParams_ =
+ lookup(eventType.functions, 'name', 'removeRules').parameters;
+ } else {
+ this.addRulesParams_ = [];
+ this.getRulesParams_ = [];
+ this.removeRulesParams_ = [];
}
};
@@ -192,7 +216,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 +270,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],
+ this.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],
+ this.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],
+ this.getRulesParams_);
}
// Special load events: we don't use the DOM unload because that slows

Powered by Google App Engine
This is Rietveld 408576698