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 var canonicalizeCompoundSelector =
     12     requireNative('css_natives').CanonicalizeCompoundSelector;
     13 
     14 binding.registerCustomHook( function(api) {
     15   var declarativeContent = api.compiledApi;
     16 
     17   // Returns the schema definition of type |typeId| defined in |namespace|.
     18   function getSchema(typeId) {
     19     return utils.lookup(api.schema.types,
     20                         'id',
     21                         'declarativeContent.' + typeId);
     22   }
     23 
     24   // Helper function for the constructor of concrete datatypes of the
     25   // declarative content API.
     26   // Makes sure that |this| contains the union of parameters and
     27   // {'instanceType': 'declarativeContent.' + typeId} and validates the
     28   // generated union dictionary against the schema for |typeId|.
     29   function setupInstance(instance, parameters, typeId) {
     30     for (var key in parameters) {
     31       if ($Object.hasOwnProperty(parameters, key)) {
     32         instance[key] = parameters[key];
     33       }
     34     }
     35     instance.instanceType = 'declarativeContent.' + typeId;
     36     var schema = getSchema(typeId);
     37     validate([instance], [schema]);
     38   }
     39 
     40   function canonicalizeCssSelectors(selectors) {
     41     for (var i = 0; i < selectors.length; i++) {
     42       var canonicalizedSelector = canonicalizeCompoundSelector(selectors[i]);
     43       if (canonicalizedSelector == '') {
     44         throw new Error(
     45             'Element of \'css\' array must be a ' +
     46             'list of valid compound selectors: ' +
     47             selectors[i]);
     48       }
     49       selectors[i] = canonicalizedSelector;
     50     }
     51   }
     52 
     53   // Setup all data types for the declarative content API.
     54   declarativeContent.PageStateMatcher = function(parameters) {
     55     setupInstance(this, parameters, 'PageStateMatcher');
     56     if ($Object.hasOwnProperty(this, 'css')) {
     57       canonicalizeCssSelectors(this.css);
     58     }
     59   };
     60   declarativeContent.ShowPageAction = function(parameters) {
     61     setupInstance(this, parameters, 'ShowPageAction');
     62   };
     63 });
     64 
     65 exports.binding = binding.generate();
     66