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

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

Issue 9192029: Bindings layer for declarative events API (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Continued Created 8 years, 11 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 45d7080a6c478b2fbc9002e6ccba527ea80c57a2..8ab4d70569b73e971159764b9ed8c3a62446f733 100644
--- a/chrome/renderer/resources/extensions/event.js
+++ b/chrome/renderer/resources/extensions/event.js
@@ -8,6 +8,8 @@ var chrome = chrome || {};
native function AttachEvent(eventName);
native function DetachEvent(eventName);
native function Print();
+ //native function AddRules(rules);
+ //native function RemoveRules(rules);
Matt Perry 2012/01/24 22:39:10 dead code
battre 2012/01/25 19:25:08 Done.
var chromeHidden = GetChromeHidden();
@@ -58,9 +60,22 @@ var chrome = chrome || {};
// chrome.tabs.onChanged.addListener(function(data) { alert(data); });
// chromeHidden.Event.dispatch("tab-changed", "hi");
// will result in an alert dialog that says 'hi'.
- chrome.Event = function(opt_eventName, opt_argSchemas) {
+ //
+ // 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,
+ opt_typesAPI) {
this.eventName_ = opt_eventName;
this.listeners_ = [];
+ this.eventOptions_ = opt_eventOptions ||
+ {"supportsListeners": true, "supportsRules": false};
+ if (opt_typesAPI) {
+ this.sendRequest_ = opt_typesAPI.sendRequest;
+ this.apiDefinitions_ = opt_typesAPI.apiDefinitions;
+ } else {
+ this.sendRequest_ = function() {};
+ this.apiDefinitions_ = {};
+ }
// Validate event parameters if we are in debug.
if (opt_argSchemas &&
@@ -75,7 +90,12 @@ var chrome = chrome || {};
exception;
}
};
+ } else {
+ this.validate_ = function() {}
}
+
+ this.rule_ids_ = {};
+ this.last_generated_rule_id_ = 0;
Matt Perry 2012/01/24 22:39:10 camelCase
battre 2012/01/25 19:25:08 Done.
};
// A map of event names to the event object that is registered to that name.
@@ -127,6 +147,8 @@ var chrome = chrome || {};
// Registers a callback to be called when this event is dispatched.
chrome.Event.prototype.addListener = function(cb) {
+ if (!this.eventOptions_.supportsListeners)
+ throw new Error("This event does not support listeners.");
if (this.listeners_.length == 0) {
this.attach_();
}
@@ -135,6 +157,8 @@ var chrome = chrome || {};
// Unregisters a callback.
chrome.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;
@@ -148,11 +172,15 @@ var chrome = chrome || {};
// Test if the given callback is registered for this event.
chrome.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.
- chrome.Event.prototype.hasListeners = function(cb) {
+ chrome.Event.prototype.hasListeners = function() {
+ if (!this.eventOptions_.supportsListeners)
+ throw new Error("This event does not support listeners.");
return this.listeners_.length > 0;
};
@@ -171,6 +199,8 @@ var chrome = chrome || {};
// Dispatches this event object to all listeners, passing all supplied
// arguments to this function each listener.
chrome.Event.prototype.dispatch = function(varargs) {
+ if (!this.eventOptions_.supportsListeners)
+ throw new Error("This event does not support listeners.");
var args = Array.prototype.slice.call(arguments);
if (this.validate_) {
var validationErrors = this.validate_(args);
@@ -227,6 +257,102 @@ var chrome = chrome || {};
this.detach_();
};
+ chrome.Event.prototype.getFunctionDefinition_ =
+ function(namespace, functionName) {
+ var filterNamespace = function(val) {return val.namespace === namespace;};
+ var apiSchema = this.apiDefinitions_.filter(filterNamespace)[0];
+ var filterFunctionName = function (val) {return val.name === functionName;};
+ return apiSchema.functions.filter(filterFunctionName)[0];
+ }
+
+ // 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.
+ chrome.Event.prototype.buildArrayOfChoicesSchema_ = function(typesList) {
+ return {
+ "type": "array",
+ "items": {
+ "choices": typesList.forEach(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.
+ chrome.Event.prototype.validateRules_ = function(rules, conditions, actions) {
+ if (!conditions || !actions) {
+ throw new Error("Error in API specification.");
+ }
+ var conditionsSchema = this.buildArrayOfChoicesSchema_(conditions);
+ var actionsSchema = this.buildArrayOfChoicesSchema_(actions);
+ rules.forEach(function(rule) {
+ chromeHidden.validate([rule.conditions], [conditionsSchema]);
+ chromeHidden.validate([rule.actions], [actionsSchema]);
+ })
+ }
+
+ chrome.Event.prototype.addMissingIds_ = function(rules) {
+ for (var i = 0; i < rules.lengh; ++i) {
+ // TODO(battre): check for "".
+ if (!("id" in rule[i])) {
+ // Generate a unique ID.
+ var new_rule_id = "";
+ do {
+ new_rule_id = "_" + (this.last_generated_rule_id_++) + "_"
+ } while (new_rule_id in this.rule_ids_);
+ // And store it.
+ rule[i]["id"] = new_rule_id;
+ }
+ this.rule_ids_[rule[i]["id"]] = 1;
+ }
+ }
+
+ chrome.Event.prototype.addRules = function(rules, opt_cb) {
+ if (!this.eventOptions_.supportsRules)
+ throw new Error("This event does not support rules.");
+
+ this.validateRules_(rules,
+ this.eventOptions_.conditions,
+ this.eventOptions_.actions);
+ this.addMissingIds_(rules);
+ var callback = opt_cb ? opt_cb.bind(undefined, rules) : undefined;
+
+ var functionDef =
+ this.getFunctionDefinition_("experimental.declarative", "addRules");
+ return this.sendRequest_.call(this,
+ "experimental.declarative.addRules",
+ [this.eventName_, rules, callback],
+ functionDef.parameters);
+ }
+
+ chrome.Event.prototype.removeRules = function(rule_identifiers, opt_cb) {
+ if (!this.eventOptions_.supportsRules)
+ throw new Error("This event does not support rules.");
+ var functionDef =
+ this.getFunctionDefinition_("experimental.declarative", "removeRules");
+ console.log(JSON.stringify(functionDef));
Matt Perry 2012/01/24 22:39:10 remove debug code?
battre 2012/01/25 19:25:08 Done.
+ return this.sendRequest_.call(this,
+ "experimental.declarative.removeRules",
+ [this.eventName_, rule_identifiers, opt_cb],
+ functionDef.parameters);
+ }
+
+ chrome.Event.prototype.getRules = function(rule_identifiers, cb) {
+ if (!this.eventOptions_.supportsRules)
+ throw new Error("This event does not support rules.");
+ var functionDef =
+ this.getFunctionDefinition_("experimental.declarative", "getRules");
+ return this.sendRequest_.call(this,
+ "experimental.declarative.getRules",
+ [this.eventName_, rule_identifiers, cb],
+ functionDef.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).
@@ -245,6 +371,8 @@ var chrome = chrome || {};
var event = allAttachedEvents[i];
if (event)
event.detach_();
+ if (event && event.eventOptions_.supportsRules)
+ event.removeRules([]);
}
};

Powered by Google App Engine
This is Rietveld 408576698