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

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

Issue 9192029: Bindings layer for declarative events API (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Check whether eventName may be optional 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
(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) {
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 var conditionsSchema = buildArrayOfChoicesSchema(conditions);
61 var actionsSchema = buildArrayOfChoicesSchema(actions);
62 rules.forEach(function(rule) {
63 chromeHidden.validate([rule.conditions], [conditionsSchema]);
64 chromeHidden.validate([rule.actions], [actionsSchema]);
65 })
66 }
67
68 apiFunctions.setHandleRequest("experimental.declarative.addRules",
69 function(eventName, rules, opt_callback) {
70 var eventOptions = getEventOptions(eventName);
71 if (!eventOptions.conditions || !eventOptions.actions) {
72 throw new Error("Event " + eventName + " misses conditions or " +
73 "actions in the API specification.");
74 }
75 validateRules(rules,
76 eventOptions.conditions,
77 eventOptions.actions);
78 sendRequest(this.name, [eventName, rules, opt_callback],
79 this.definition.parameters);
80 });
81 });
82
83 })();
OLDNEW
« no previous file with comments | « chrome/renderer/resources/extensions/event.js ('k') | chrome/renderer/resources/extensions/schema_generated_bindings.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698