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 declarativeContent API.
7 var binding
= require('binding').Binding
.create('declarativeContent');
9 var utils
= require('utils');
10 var validate
= require('schemaUtils').validate
;
11 var canonicalizeCompoundSelector
=
12 requireNative('css_natives').CanonicalizeCompoundSelector
;
13 var setIcon
= require('setIcon').setIcon
;
15 binding
.registerCustomHook( function(api
) {
16 var declarativeContent
= api
.compiledApi
;
18 // Returns the schema definition of type |typeId| defined in |namespace|.
19 function getSchema(typeId
) {
20 return utils
.lookup(api
.schema
.types
,
22 'declarativeContent.' + typeId
);
25 // Helper function for the constructor of concrete datatypes of the
26 // declarative content API.
27 // Makes sure that |this| contains the union of parameters and
28 // {'instanceType': 'declarativeContent.' + 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
];
36 instance
.instanceType
= 'declarativeContent.' + typeId
;
37 var schema
= getSchema(typeId
);
38 validate([instance
], [schema
]);
41 function canonicalizeCssSelectors(selectors
) {
42 for (var i
= 0; i
< selectors
.length
; i
++) {
43 var canonicalizedSelector
= canonicalizeCompoundSelector(selectors
[i
]);
44 if (canonicalizedSelector
== '') {
46 'Element of \'css\' array must be a ' +
47 'list of valid compound selectors: ' +
50 selectors
[i
] = canonicalizedSelector
;
54 // Setup all data types for the declarative content API.
55 declarativeContent
.PageStateMatcher = function(parameters
) {
56 setupInstance(this, parameters
, 'PageStateMatcher');
57 if ($Object
.hasOwnProperty(this, 'css')) {
58 canonicalizeCssSelectors(this.css
);
61 declarativeContent
.ShowPageAction = function(parameters
) {
62 setupInstance(this, parameters
, 'ShowPageAction');
64 declarativeContent
.RequestContentScript = function(parameters
) {
65 setupInstance(this, parameters
, 'RequestContentScript');
67 // TODO(rockot): Do not expose this in M39 stable. Making this restriction
68 // possible will take some extra work. See http://crbug.com/415315
69 declarativeContent
.SetIcon = function(parameters
) {
70 setIcon(parameters
, function (data
) {
71 setupInstance(this, data
, 'SetIcon');
76 exports
.binding
= binding
.generate();