| OLD | NEW |
| (Empty) |
| 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 // Custom bindings for the declarative API. | |
| 6 | |
| 7 var chromeHidden = requireNative('chrome_hidden').GetChromeHidden(); | |
| 8 var sendRequest = require('sendRequest').sendRequest; | |
| 9 | |
| 10 chromeHidden.registerCustomHook('experimental.declarative', | |
| 11 function(bindingsAPI) { | |
| 12 var apiFunctions = bindingsAPI.apiFunctions; | |
| 13 var apiDefinitions = bindingsAPI.apiDefinitions; | |
| 14 var cachedEventOptions = {}; | |
| 15 | |
| 16 function getEventOptions(qualifiedEventName) { | |
| 17 if (cachedEventOptions[qualifiedEventName]) | |
| 18 return cachedEventOptions[qualifiedEventName]; | |
| 19 | |
| 20 // Parse qualifiedEventName into namespace and event name. | |
| 21 var lastSeparator = qualifiedEventName.lastIndexOf('.'); | |
| 22 var eventName = qualifiedEventName.substr(lastSeparator + 1); | |
| 23 var namespace = qualifiedEventName.substr(0, lastSeparator); | |
| 24 | |
| 25 // Lookup schema definition. | |
| 26 var filterNamespace = function(val) {return val.namespace === namespace;}; | |
| 27 var apiSchema = apiDefinitions.filter(filterNamespace)[0]; | |
| 28 var filterEventName = function (val) {return val.name === eventName;}; | |
| 29 var eventSchema = apiSchema.events.filter(filterEventName)[0]; | |
| 30 | |
| 31 cachedEventOptions[qualifiedEventName] = eventSchema.options; | |
| 32 return eventSchema.options; | |
| 33 } | |
| 34 | |
| 35 // Takes a list of JSON datatype identifiers and returns a schema fragment | |
| 36 // that verifies that a JSON object corresponds to an array of only these | |
| 37 // data types. | |
| 38 function buildArrayOfChoicesSchema(typesList) { | |
| 39 return { | |
| 40 'type': 'array', | |
| 41 'items': { | |
| 42 'choices': typesList.map(function(el) {return {'$ref': el};}) | |
| 43 } | |
| 44 }; | |
| 45 } | |
| 46 | |
| 47 // Validate conditions and actions against specific schemas of this | |
| 48 // event object type. | |
| 49 // |rules| is an array of JSON objects that follow the Rule type of the | |
| 50 // declarative extension APIs. |conditions| is an array of JSON type | |
| 51 // identifiers that are allowed to occur in the conditions attribute of each | |
| 52 // rule. Likewise, |actions| is an array of JSON type identifiers that are | |
| 53 // allowed to occur in the actions attribute of each rule. | |
| 54 function validateRules(rules, conditions, actions) { | |
| 55 var conditionsSchema = buildArrayOfChoicesSchema(conditions); | |
| 56 var actionsSchema = buildArrayOfChoicesSchema(actions); | |
| 57 rules.forEach(function(rule) { | |
| 58 chromeHidden.validate([rule.conditions], [conditionsSchema]); | |
| 59 chromeHidden.validate([rule.actions], [actionsSchema]); | |
| 60 }) | |
| 61 } | |
| 62 | |
| 63 apiFunctions.setHandleRequest('addRules', | |
| 64 function(eventName, rules, opt_callback) { | |
| 65 var eventOptions = getEventOptions(eventName); | |
| 66 if (!eventOptions.conditions || !eventOptions.actions) { | |
| 67 throw new Error('Event ' + eventName + ' misses conditions or ' + | |
| 68 'actions in the API specification.'); | |
| 69 } | |
| 70 validateRules(rules, | |
| 71 eventOptions.conditions, | |
| 72 eventOptions.actions); | |
| 73 sendRequest(this.name, [eventName, rules, opt_callback], | |
| 74 this.definition.parameters); | |
| 75 }); | |
| 76 }); | |
| OLD | NEW |