1/*
2 * Copyright (C) 2012 Google Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 *
8 * 1.  Redistributions of source code must retain the above copyright
9 *     notice, this list of conditions and the following disclaimer.
10 * 2.  Redistributions in binary form must reproduce the above copyright
11 *     notice, this list of conditions and the following disclaimer in the
12 *     documentation and/or other materials provided with the distribution.
13 * 3.  Neither the name of Apple Computer, Inc. ("Apple") nor the names of
14 *     its contributors may be used to endorse or promote products derived
15 *     from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
18 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
21 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#ifndef V8DOMConfiguration_h
30#define V8DOMConfiguration_h
31
32#include "bindings/v8/V8DOMWrapper.h"
33#include <v8.h>
34
35namespace WebCore {
36
37class V8DOMConfiguration {
38public:
39    // The following Configuration structs and install methods are used for
40    // setting multiple properties on an ObjectTemplate, used from the
41    // generated bindings initialization (ConfigureXXXTemplate). This greatly
42    // reduces the binary size by moving from code driven setup to data table
43    // driven setup.
44
45    // AttributeConfiguration translates into calls to SetAccessor() on either
46    // the instance or the prototype ObjectTemplate, based on |onPrototype|.
47    struct AttributeConfiguration {
48        const char* const name;
49        v8::AccessorGetterCallback getter;
50        v8::AccessorSetterCallback setter;
51        v8::AccessorGetterCallback getterForMainWorld;
52        v8::AccessorSetterCallback setterForMainWorld;
53        const WrapperTypeInfo* data;
54        v8::AccessControl settings;
55        v8::PropertyAttribute attribute;
56        bool onPrototype;
57    };
58
59    // AccessorConfiguration translates into calls to SetAccessorProperty()
60    // on prototype ObjectTemplate.
61    struct AccessorConfiguration {
62        const char* const name;
63        v8::FunctionCallback getter;
64        v8::FunctionCallback setter;
65        v8::FunctionCallback getterForMainWorld;
66        v8::FunctionCallback setterForMainWorld;
67        const WrapperTypeInfo* data;
68        v8::AccessControl settings;
69        v8::PropertyAttribute attribute;
70    };
71
72    static void installAttributes(v8::Handle<v8::ObjectTemplate>, v8::Handle<v8::ObjectTemplate>, const AttributeConfiguration*, size_t attributeCount, v8::Isolate*, WrapperWorldType currentWorldType);
73
74    template<class ObjectOrTemplate>
75    static inline void installAttribute(v8::Handle<ObjectOrTemplate> instanceTemplate, v8::Handle<ObjectOrTemplate> prototype, const AttributeConfiguration& attribute, v8::Isolate* isolate)
76    {
77        (attribute.onPrototype ? prototype : instanceTemplate)->SetAccessor(v8::String::NewFromUtf8(isolate, attribute.name, v8::String::kInternalizedString),
78                                                                    attribute.getter,
79                                                                    attribute.setter,
80                                                                    v8::External::New(isolate, const_cast<WrapperTypeInfo*>(attribute.data)),
81                                                                    attribute.settings,
82                                                                    attribute.attribute);
83    }
84
85    template<class ObjectOrTemplate>
86    static inline void installAttribute(v8::Handle<ObjectOrTemplate> instanceTemplate, v8::Handle<ObjectOrTemplate> prototype, const AttributeConfiguration& attribute, v8::Isolate* isolate, WrapperWorldType currentWorldType)
87    {
88        v8::AccessorGetterCallback getter = attribute.getter;
89        v8::AccessorSetterCallback setter = attribute.setter;
90        if (currentWorldType == MainWorld) {
91            if (attribute.getterForMainWorld)
92                getter = attribute.getterForMainWorld;
93            if (attribute.setterForMainWorld)
94                setter = attribute.setterForMainWorld;
95        }
96        (attribute.onPrototype ? prototype : instanceTemplate)->SetAccessor(v8::String::NewFromUtf8(isolate, attribute.name, v8::String::kInternalizedString),
97            getter,
98            setter,
99            v8::External::New(isolate, const_cast<WrapperTypeInfo*>(attribute.data)),
100            attribute.settings,
101            attribute.attribute);
102    }
103
104    // ConstantConfiguration translates into calls to Set() for setting up an
105    // object's constants. It sets the constant on both the FunctionTemplate and
106    // the ObjectTemplate. PropertyAttributes is always ReadOnly.
107    struct ConstantConfiguration {
108        const char* const name;
109        int value;
110    };
111
112    static void installConstants(v8::Handle<v8::FunctionTemplate>, v8::Handle<v8::ObjectTemplate>, const ConstantConfiguration*, size_t constantCount, v8::Isolate*);
113
114    // MethodConfiguration translates into calls to Set() on the prototype
115    // ObjectTemplate.
116    struct MethodConfiguration {
117        const char* const name;
118        v8::FunctionCallback callback;
119        v8::FunctionCallback callbackForMainWorld;
120        int length;
121    };
122
123    static void installCallbacks(v8::Handle<v8::ObjectTemplate>, v8::Handle<v8::Signature>, v8::PropertyAttribute, const MethodConfiguration*, size_t callbackCount, v8::Isolate*, WrapperWorldType);
124
125    static void installAccessors(v8::Handle<v8::ObjectTemplate>, v8::Handle<v8::Signature>, const AccessorConfiguration*, size_t accessorCount, v8::Isolate*, WrapperWorldType);
126
127    static v8::Local<v8::Signature> installDOMClassTemplate(v8::Handle<v8::FunctionTemplate>, const char* interfaceName, v8::Handle<v8::FunctionTemplate> parentClass, size_t fieldCount,
128        const AttributeConfiguration*, size_t attributeCount,
129        const AccessorConfiguration*, size_t accessorCount,
130        const MethodConfiguration*, size_t callbackCount,
131        v8::Isolate*, WrapperWorldType);
132};
133
134} // namespace WebCore
135
136#endif // V8DOMConfiguration_h
137