1 // Copyright 2013 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.
5 // Custom binding for the webViewRequest API.
7 var binding = require('binding').Binding.create('webViewRequest');
9 var declarativeWebRequestSchema =
10 requireNative('schema_registry').GetSchema('declarativeWebRequest');
11 var utils = require('utils');
12 var validate = require('schemaUtils').validate;
14 binding.registerCustomHook(function(api) {
15 var webViewRequest = api.compiledApi;
17 // Returns the schema definition of type |typeId| defined in
18 // |declarativeWebRequestScheme.types|.
19 function getSchema(typeId) {
20 return utils.lookup(declarativeWebRequestSchema.types,
22 'declarativeWebRequest.' + typeId);
25 // Helper function for the constructor of concrete datatypes of the
26 // declarative webRequest API.
27 // Makes sure that |this| contains the union of parameters and
28 // {'instanceType': 'declarativeWebRequest.' + typeId} and validates the
29 // generated union dictionary against the schema for |typeId|.
30 function setupInstance(instance, parameters, typeId) {
31 for (var key in parameters) {
32 if ($Object.hasOwnProperty(parameters, key)) {
33 instance[key] = parameters[key];
37 instance.instanceType = 'declarativeWebRequest.' + typeId;
38 var schema = getSchema(typeId);
39 validate([instance], [schema]);
42 // Setup all data types for the declarative webRequest API from the schema.
43 for (var i = 0; i < declarativeWebRequestSchema.types.length; ++i) {
44 var typeSchema = declarativeWebRequestSchema.types[i];
45 var typeId = typeSchema.id.replace('declarativeWebRequest.', '');
46 var action = function(typeId) {
47 return function(parameters) {
48 setupInstance(this, parameters, typeId);
51 webViewRequest[typeId] = action;
55 exports.binding = binding.generate();