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 Batch structs and methods are used for setting multiple
40    // properties on an ObjectTemplate, used from the generated bindings
41    // initialization (ConfigureXXXTemplate). This greatly reduces the binary
42    // size by moving from code driven setup to data table driven setup.
43
44    // BatchedAttribute translates into calls to SetAccessor() on either the
45    // instance or the prototype ObjectTemplate, based on |onPrototype|.
46    struct BatchedAttribute {
47        const char* const name;
48        v8::AccessorGetterCallback getter;
49        v8::AccessorSetterCallback setter;
50        v8::AccessorGetterCallback getterForMainWorld;
51        v8::AccessorSetterCallback setterForMainWorld;
52        WrapperTypeInfo* data;
53        v8::AccessControl settings;
54        v8::PropertyAttribute attribute;
55        bool onPrototype;
56    };
57
58    static void batchConfigureAttributes(v8::Handle<v8::ObjectTemplate>, v8::Handle<v8::ObjectTemplate>, const BatchedAttribute*, size_t attributeCount, v8::Isolate*, WrapperWorldType currentWorldType);
59
60    template<class ObjectOrTemplate>
61    static inline void configureAttribute(v8::Handle<ObjectOrTemplate> instance, v8::Handle<ObjectOrTemplate> prototype, const BatchedAttribute& attribute, v8::Isolate*)
62    {
63        (attribute.onPrototype ? prototype : instance)->SetAccessor(v8::String::NewSymbol(attribute.name),
64                                                                    attribute.getter,
65                                                                    attribute.setter,
66                                                                    v8::External::New(attribute.data),
67                                                                    attribute.settings,
68                                                                    attribute.attribute);
69    }
70
71    template<class ObjectOrTemplate>
72    static inline void configureAttribute(v8::Handle<ObjectOrTemplate> instance, v8::Handle<ObjectOrTemplate> prototype, const BatchedAttribute& attribute, v8::Isolate*, WrapperWorldType currentWorldType)
73    {
74        v8::AccessorGetterCallback getter = attribute.getter;
75        v8::AccessorSetterCallback setter = attribute.setter;
76        if (currentWorldType == MainWorld) {
77            if (attribute.getterForMainWorld)
78                getter = attribute.getterForMainWorld;
79            if (attribute.setterForMainWorld)
80                setter = attribute.setterForMainWorld;
81        }
82        (attribute.onPrototype ? prototype : instance)->SetAccessor(v8::String::NewSymbol(attribute.name),
83            getter,
84            setter,
85            v8::External::New(attribute.data),
86            attribute.settings,
87            attribute.attribute);
88    }
89
90    // BatchedConstant translates into calls to Set() for setting up an object's
91    // constants. It sets the constant on both the FunctionTemplate and the
92    // ObjectTemplate. PropertyAttributes is always ReadOnly.
93    struct BatchedConstant {
94        const char* const name;
95        int value;
96    };
97
98    static void batchConfigureConstants(v8::Handle<v8::FunctionTemplate>, v8::Handle<v8::ObjectTemplate>, const BatchedConstant*, size_t constantCount, v8::Isolate*);
99
100    // BatchedMethod translates into calls to Set() on the prototype ObjectTemplate.
101    struct BatchedMethod {
102        const char* const name;
103        v8::FunctionCallback callback;
104        v8::FunctionCallback callbackForMainWorld;
105        int length;
106    };
107
108    static void batchConfigureCallbacks(v8::Handle<v8::ObjectTemplate>, v8::Handle<v8::Signature>, v8::PropertyAttribute, const BatchedMethod*, size_t callbackCount, v8::Isolate*, WrapperWorldType);
109
110    static v8::Local<v8::Signature> configureTemplate(v8::Handle<v8::FunctionTemplate>, const char* interfaceName, v8::Handle<v8::FunctionTemplate> parentClass, size_t fieldCount, const BatchedAttribute*, size_t attributeCount, const BatchedMethod*, size_t callbackCount, v8::Isolate*, WrapperWorldType);
111};
112
113} // namespace WebCore
114
115#endif // V8DOMConfiguration_h
116