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

Side by Side Diff: chrome/renderer/resources/extensions/experimental.declarative_custom_bindings.js

Issue 9423049: Make registering custom hooks with schema_generated_bindings.js safer and (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: . Created 8 years, 10 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 unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 // Custom bindings for the declarative API. 5 // Custom bindings for the declarative API.
6 6
7 (function() { 7 (function() {
8 8
9 native function GetChromeHidden(); 9 native function GetChromeHidden();
10 10
11 var chromeHidden = GetChromeHidden(); 11 var chromeHidden = GetChromeHidden();
12 12
13 chromeHidden.registerCustomHook( 13 chromeHidden.registerCustomHook('experimental.declarative',
14 'experimental.declarative', 14 function(bindingsAPI) {
15 function(bindingsAPI) {
16 var apiFunctions = bindingsAPI.apiFunctions; 15 var apiFunctions = bindingsAPI.apiFunctions;
17 var sendRequest = bindingsAPI.sendRequest; 16 var sendRequest = bindingsAPI.sendRequest;
18 var apiDefinitions = bindingsAPI.apiDefinitions; 17 var apiDefinitions = bindingsAPI.apiDefinitions;
19 var cachedEventOptions = {}; 18 var cachedEventOptions = {};
20 19
21 function getEventOptions(qualifiedEventName) { 20 function getEventOptions(qualifiedEventName) {
22 if (cachedEventOptions[qualifiedEventName]) 21 if (cachedEventOptions[qualifiedEventName])
23 return cachedEventOptions[qualifiedEventName]; 22 return cachedEventOptions[qualifiedEventName];
24 23
25 // Parse qualifiedEventName into namespace and event name. 24 // Parse qualifiedEventName into namespace and event name.
26 var lastSeparator = qualifiedEventName.lastIndexOf("."); 25 var lastSeparator = qualifiedEventName.lastIndexOf('.');
27 var eventName = qualifiedEventName.substr(lastSeparator + 1); 26 var eventName = qualifiedEventName.substr(lastSeparator + 1);
28 var namespace = qualifiedEventName.substr(0, lastSeparator); 27 var namespace = qualifiedEventName.substr(0, lastSeparator);
29 28
30 // Lookup schema definition. 29 // Lookup schema definition.
31 var filterNamespace = function(val) {return val.namespace === namespace;}; 30 var filterNamespace = function(val) {return val.namespace === namespace;};
32 var apiSchema = apiDefinitions.filter(filterNamespace)[0]; 31 var apiSchema = apiDefinitions.filter(filterNamespace)[0];
33 var filterEventName = function (val) {return val.name === eventName;}; 32 var filterEventName = function (val) {return val.name === eventName;};
34 var eventSchema = apiSchema.events.filter(filterEventName)[0]; 33 var eventSchema = apiSchema.events.filter(filterEventName)[0];
35 34
36 cachedEventOptions[qualifiedEventName] = eventSchema.options; 35 cachedEventOptions[qualifiedEventName] = eventSchema.options;
37 return eventSchema.options; 36 return eventSchema.options;
38 } 37 }
39 38
40 // Takes a list of JSON datatype identifiers and returns a schema fragment 39 // 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 40 // that verifies that a JSON object corresponds to an array of only these
42 // data types. 41 // data types.
43 function buildArrayOfChoicesSchema(typesList) { 42 function buildArrayOfChoicesSchema(typesList) {
44 return { 43 return {
45 "type": "array", 44 'type': 'array',
46 "items": { 45 'items': {
47 "choices": typesList.map(function(el) {return {"$ref": el};}) 46 'choices': typesList.map(function(el) {return {'$ref': el};})
48 } 47 }
49 }; 48 };
50 } 49 }
51 50
52 // Validate conditions and actions against specific schemas of this 51 // Validate conditions and actions against specific schemas of this
53 // event object type. 52 // event object type.
54 // |rules| is an array of JSON objects that follow the Rule type of the 53 // |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 54 // declarative extension APIs. |conditions| is an array of JSON type
56 // identifiers that are allowed to occur in the conditions attribute of each 55 // 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 56 // rule. Likewise, |actions| is an array of JSON type identifiers that are
58 // allowed to occur in the actions attribute of each rule. 57 // allowed to occur in the actions attribute of each rule.
59 function validateRules(rules, conditions, actions) { 58 function validateRules(rules, conditions, actions) {
60 var conditionsSchema = buildArrayOfChoicesSchema(conditions); 59 var conditionsSchema = buildArrayOfChoicesSchema(conditions);
61 var actionsSchema = buildArrayOfChoicesSchema(actions); 60 var actionsSchema = buildArrayOfChoicesSchema(actions);
62 rules.forEach(function(rule) { 61 rules.forEach(function(rule) {
63 chromeHidden.validate([rule.conditions], [conditionsSchema]); 62 chromeHidden.validate([rule.conditions], [conditionsSchema]);
64 chromeHidden.validate([rule.actions], [actionsSchema]); 63 chromeHidden.validate([rule.actions], [actionsSchema]);
65 }) 64 })
66 } 65 }
67 66
68 apiFunctions.setHandleRequest("experimental.declarative.addRules", 67 apiFunctions.setHandleRequest('addRules',
69 function(eventName, rules, opt_callback) { 68 function(eventName, rules, opt_callback) {
70 var eventOptions = getEventOptions(eventName); 69 var eventOptions = getEventOptions(eventName);
71 if (!eventOptions.conditions || !eventOptions.actions) { 70 if (!eventOptions.conditions || !eventOptions.actions) {
72 throw new Error("Event " + eventName + " misses conditions or " + 71 throw new Error('Event ' + eventName + ' misses conditions or ' +
73 "actions in the API specification."); 72 'actions in the API specification.');
74 } 73 }
75 validateRules(rules, 74 validateRules(rules,
76 eventOptions.conditions, 75 eventOptions.conditions,
77 eventOptions.actions); 76 eventOptions.actions);
78 sendRequest(this.name, [eventName, rules, opt_callback], 77 sendRequest(this.name, [eventName, rules, opt_callback],
79 this.definition.parameters); 78 this.definition.parameters);
80 }); 79 });
81 }); 80 });
82 81
83 })(); 82 })();
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698