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