Home | History | Annotate | Download | only in extensions
      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 binding for the declarativeContent API.
      6 
      7 var binding = require('binding').Binding.create('declarativeContent');
      8 
      9 var utils = require('utils');
     10 var validate = require('schemaUtils').validate;
     11 
     12 binding.registerCustomHook( function(api) {
     13   var declarativeContent = api.compiledApi;
     14 
     15   // Returns the schema definition of type |typeId| defined in |namespace|.
     16   function getSchema(typeId) {
     17     return utils.lookup(api.schema.types,
     18                         'id',
     19                         'declarativeContent.' + typeId);
     20   }
     21 
     22   // Helper function for the constructor of concrete datatypes of the
     23   // declarative content API.
     24   // Makes sure that |this| contains the union of parameters and
     25   // {'instanceType': 'declarativeContent.' + 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];
     31       }
     32     }
     33     instance.instanceType = 'declarativeContent.' + typeId;
     34     var schema = getSchema(typeId);
     35     validate([instance], [schema]);
     36   }
     37 
     38   // Setup all data types for the declarative content API.
     39   declarativeContent.PageStateMatcher = function(parameters) {
     40     setupInstance(this, parameters, 'PageStateMatcher');
     41   };
     42   declarativeContent.ShowPageAction = function(parameters) {
     43     setupInstance(this, parameters, 'ShowPageAction');
     44   };
     45 });
     46 
     47 exports.binding = binding.generate();
     48