• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
4
5// Custom binding for the webViewRequest API.
6
7var binding = require('binding').Binding.create('webViewRequest');
8
9var declarativeWebRequestSchema =
10    requireNative('schema_registry').GetSchema('declarativeWebRequest');
11var utils = require('utils');
12var validate = require('schemaUtils').validate;
13
14binding.registerCustomHook(function(api) {
15  var webViewRequest = api.compiledApi;
16
17  // Returns the schema definition of type |typeId| defined in
18  // |declarativeWebRequestScheme.types|.
19  function getSchema(typeId) {
20    return utils.lookup(declarativeWebRequestSchema.types,
21                        'id',
22                        'declarativeWebRequest.' + typeId);
23  }
24
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];
34      }
35    }
36
37    instance.instanceType = 'declarativeWebRequest.' + typeId;
38    var schema = getSchema(typeId);
39    validate([instance], [schema]);
40  }
41
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);
49      };
50    }(typeId);
51    webViewRequest[typeId] = action;
52  }
53});
54
55exports.binding = binding.generate();
56