Chromium Code Reviews| Index: chrome/renderer/resources/extensions/experimental.declarative_custom_bindings.js |
| diff --git a/chrome/renderer/resources/extensions/experimental.declarative_custom_bindings.js b/chrome/renderer/resources/extensions/experimental.declarative_custom_bindings.js |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..d43ea6b43e3f36d555f156f4dd978ddf6b0f7290 |
| --- /dev/null |
| +++ b/chrome/renderer/resources/extensions/experimental.declarative_custom_bindings.js |
| @@ -0,0 +1,82 @@ |
| +// 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. |
| + |
| +// Custom bindings for the declarative API. |
| + |
| +(function() { |
| + |
| +native function GetChromeHidden(); |
| + |
| +var chromeHidden = GetChromeHidden(); |
| + |
| +chromeHidden.registerCustomHook( |
| + 'experimental.declarative', |
| + function(bindingsAPI) { |
|
not at google - send to devlin
2012/02/01 23:07:23
single line for all of this?
battre
2012/02/02 13:49:59
Does not fit (3 characters too much). I have left
|
| + var apiFunctions = bindingsAPI.apiFunctions; |
| + var sendRequest = bindingsAPI.sendRequest; |
| + var apiDefinitions = bindingsAPI.apiDefinitions; |
| + var cachedEventOptions = {}; |
| + |
| + function getEventOptions(qualifiedEventName) { |
| + if (cachedEventOptions[qualifiedEventName]) |
| + return cachedEventOptions[qualifiedEventName]; |
| + |
| + // Parse qualifiedEventName into namespace and event name. |
| + var lastSeparator = qualifiedEventName.lastIndexOf("."); |
| + var eventName = qualifiedEventName.substr(lastSeparator + 1); |
| + var namespace = qualifiedEventName.substr(0, lastSeparator); |
| + |
| + // Lookup schema definition. |
| + var filterNamespace = function(val) {return val.namespace === namespace;}; |
| + var apiSchema = apiDefinitions.filter(filterNamespace)[0]; |
| + var filterEventName = function (val) {return val.name === eventName;}; |
| + var eventSchema = apiSchema.events.filter(filterEventName)[0]; |
| + |
| + cachedEventOptions[qualifiedEventName] = eventSchema.options; |
| + return eventSchema.options; |
| + } |
| + |
| + // 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) { |
| + if (!conditions || !actions) { |
| + throw new Error("Error in API specification."); |
|
not at google - send to devlin
2012/02/01 23:07:23
it might be nice to have a slightly more helpful (
battre
2012/02/02 13:49:59
Done.
|
| + } |
| + var conditionsSchema = buildArrayOfChoicesSchema(conditions); |
| + var actionsSchema = buildArrayOfChoicesSchema(actions); |
| + rules.forEach(function(rule) { |
| + chromeHidden.validate([rule.conditions], [conditionsSchema]); |
| + chromeHidden.validate([rule.actions], [actionsSchema]); |
| + }) |
| + } |
| + |
| + apiFunctions.setHandleRequest("experimental.declarative.addRules", |
| + function(eventName, rules, opt_callback) { |
| + var eventOptions = getEventOptions(eventName); |
| + validateRules(rules, |
| + eventOptions.conditions, |
| + eventOptions.actions); |
| + sendRequest(this.name, [eventName, rules, opt_callback], |
| + this.definition.parameters); |
| + }); |
| +}); |
| + |
| +})(); |