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.
5 // Custom binding for the declarativeWebRequest API.
7 var binding
= require('binding').Binding
.create('declarativeWebRequest');
9 var utils
= require('utils');
10 var validate
= require('schemaUtils').validate
;
12 binding
.registerCustomHook(function(api
) {
13 var declarativeWebRequest
= api
.compiledApi
;
15 // Returns the schema definition of type |typeId| defined in |namespace|.
16 function getSchema(typeId
) {
17 return utils
.lookup(api
.schema
.types
,
19 'declarativeWebRequest.' + typeId
);
22 // Helper function for the constructor of concrete datatypes of the
23 // declarative webRequest API.
24 // Makes sure that |this| contains the union of parameters and
25 // {'instanceType': 'declarativeWebRequest.' + typeId} and validates the
26 // generated union dictionary against the schema for |typeId|.
27 function setupInstance(instance
, parameters
, typeId
) {
28 for (var key
in parameters
) {
29 if ($Object
.hasOwnProperty(parameters
, key
)) {
30 instance
[key
] = parameters
[key
];
33 instance
.instanceType
= 'declarativeWebRequest.' + typeId
;
34 var schema
= getSchema(typeId
);
35 validate([instance
], [schema
]);
38 // Setup all data types for the declarative webRequest API.
39 declarativeWebRequest
.RequestMatcher = function(parameters
) {
40 setupInstance(this, parameters
, 'RequestMatcher');
42 declarativeWebRequest
.CancelRequest = function(parameters
) {
43 setupInstance(this, parameters
, 'CancelRequest');
45 declarativeWebRequest
.RedirectRequest = function(parameters
) {
46 setupInstance(this, parameters
, 'RedirectRequest');
48 declarativeWebRequest
.SetRequestHeader = function(parameters
) {
49 setupInstance(this, parameters
, 'SetRequestHeader');
51 declarativeWebRequest
.RemoveRequestHeader = function(parameters
) {
52 setupInstance(this, parameters
, 'RemoveRequestHeader');
54 declarativeWebRequest
.AddResponseHeader = function(parameters
) {
55 setupInstance(this, parameters
, 'AddResponseHeader');
57 declarativeWebRequest
.RemoveResponseHeader = function(parameters
) {
58 setupInstance(this, parameters
, 'RemoveResponseHeader');
60 declarativeWebRequest
.RedirectToTransparentImage
=
61 function(parameters
) {
62 setupInstance(this, parameters
, 'RedirectToTransparentImage');
64 declarativeWebRequest
.RedirectToEmptyDocument = function(parameters
) {
65 setupInstance(this, parameters
, 'RedirectToEmptyDocument');
67 declarativeWebRequest
.RedirectByRegEx = function(parameters
) {
68 setupInstance(this, parameters
, 'RedirectByRegEx');
70 declarativeWebRequest
.IgnoreRules = function(parameters
) {
71 setupInstance(this, parameters
, 'IgnoreRules');
73 declarativeWebRequest
.AddRequestCookie = function(parameters
) {
74 setupInstance(this, parameters
, 'AddRequestCookie');
76 declarativeWebRequest
.AddResponseCookie = function(parameters
) {
77 setupInstance(this, parameters
, 'AddResponseCookie');
79 declarativeWebRequest
.EditRequestCookie = function(parameters
) {
80 setupInstance(this, parameters
, 'EditRequestCookie');
82 declarativeWebRequest
.EditResponseCookie = function(parameters
) {
83 setupInstance(this, parameters
, 'EditResponseCookie');
85 declarativeWebRequest
.RemoveRequestCookie = function(parameters
) {
86 setupInstance(this, parameters
, 'RemoveRequestCookie');
88 declarativeWebRequest
.RemoveResponseCookie = function(parameters
) {
89 setupInstance(this, parameters
, 'RemoveResponseCookie');
91 declarativeWebRequest
.SendMessageToExtension = function(parameters
) {
92 setupInstance(this, parameters
, 'SendMessageToExtension');
96 exports
.binding
= binding
.generate();