OLD | NEW |
| (Empty) |
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/V8Binding.h" | |
33 #include "bindings/v8/V8DOMWrapper.h" | |
34 #include <v8.h> | |
35 | |
36 namespace WebCore { | |
37 | |
38 class V8DOMConfiguration { | |
39 public: | |
40 // The following Configuration structs and install methods are used for | |
41 // setting multiple properties on an ObjectTemplate, used from the | |
42 // generated bindings initialization (ConfigureXXXTemplate). This greatly | |
43 // reduces the binary size by moving from code driven setup to data table | |
44 // driven setup. | |
45 | |
46 // AttributeConfiguration translates into calls to SetAccessor() on either | |
47 // the instance or the prototype ObjectTemplate, based on |onPrototype|. | |
48 struct AttributeConfiguration { | |
49 const char* const name; | |
50 v8::AccessorGetterCallback getter; | |
51 v8::AccessorSetterCallback setter; | |
52 v8::AccessorGetterCallback getterForMainWorld; | |
53 v8::AccessorSetterCallback setterForMainWorld; | |
54 const WrapperTypeInfo* data; | |
55 v8::AccessControl settings; | |
56 v8::PropertyAttribute attribute; | |
57 bool onPrototype; | |
58 }; | |
59 | |
60 // AccessorConfiguration translates into calls to SetAccessorProperty() | |
61 // on prototype ObjectTemplate. | |
62 struct AccessorConfiguration { | |
63 const char* const name; | |
64 v8::FunctionCallback getter; | |
65 v8::FunctionCallback setter; | |
66 v8::FunctionCallback getterForMainWorld; | |
67 v8::FunctionCallback setterForMainWorld; | |
68 const WrapperTypeInfo* data; | |
69 v8::AccessControl settings; | |
70 v8::PropertyAttribute attribute; | |
71 }; | |
72 | |
73 static void installAttributes(v8::Handle<v8::ObjectTemplate>, v8::Handle<v8:
:ObjectTemplate>, const AttributeConfiguration*, size_t attributeCount, v8::Isol
ate*); | |
74 | |
75 template<class ObjectOrTemplate> | |
76 static inline void installAttribute(v8::Handle<ObjectOrTemplate> instanceTem
plate, v8::Handle<ObjectOrTemplate> prototype, const AttributeConfiguration& att
ribute, v8::Isolate* isolate) | |
77 { | |
78 v8::AccessorGetterCallback getter = attribute.getter; | |
79 v8::AccessorSetterCallback setter = attribute.setter; | |
80 if (DOMWrapperWorld::current(isolate).isMainWorld()) { | |
81 if (attribute.getterForMainWorld) | |
82 getter = attribute.getterForMainWorld; | |
83 if (attribute.setterForMainWorld) | |
84 setter = attribute.setterForMainWorld; | |
85 } | |
86 (attribute.onPrototype ? prototype : instanceTemplate)->SetAccessor(v8At
omicString(isolate, attribute.name), | |
87 getter, | |
88 setter, | |
89 v8::External::New(isolate, const_cast<WrapperTypeInfo*>(attribute.da
ta)), | |
90 attribute.settings, | |
91 attribute.attribute); | |
92 } | |
93 | |
94 // ConstantConfiguration translates into calls to Set() for setting up an | |
95 // object's constants. It sets the constant on both the FunctionTemplate and | |
96 // the ObjectTemplate. PropertyAttributes is always ReadOnly. | |
97 struct ConstantConfiguration { | |
98 const char* const name; | |
99 int value; | |
100 }; | |
101 | |
102 static void installConstants(v8::Handle<v8::FunctionTemplate>, v8::Handle<v8
::ObjectTemplate>, const ConstantConfiguration*, size_t constantCount, v8::Isola
te*); | |
103 | |
104 // MethodConfiguration translates into calls to Set() for setting up an | |
105 // object's callbacks. It sets the method on both the FunctionTemplate or | |
106 // the ObjectTemplate. | |
107 struct MethodConfiguration { | |
108 const char* const name; | |
109 v8::FunctionCallback callback; | |
110 v8::FunctionCallback callbackForMainWorld; | |
111 int length; | |
112 }; | |
113 | |
114 static void installMethods(v8::Handle<v8::ObjectTemplate>, v8::Handle<v8::Si
gnature>, v8::PropertyAttribute, const MethodConfiguration*, size_t callbackCoun
t, v8::Isolate*); | |
115 | |
116 template<class ObjectOrTemplate> | |
117 static void installMethodCustomSignature(v8::Handle<ObjectOrTemplate> instan
ceTemplate, v8::Handle<v8::Signature> signature, v8::PropertyAttribute attribute
, const MethodConfiguration& configuration, v8::Isolate* isolate) | |
118 { | |
119 v8::FunctionCallback callback = configuration.callback; | |
120 if (DOMWrapperWorld::current(isolate).isMainWorld() && configuration.cal
lbackForMainWorld) | |
121 callback = configuration.callbackForMainWorld; | |
122 v8::Local<v8::FunctionTemplate> functionTemplate = v8::FunctionTemplate:
:New(isolate, callback, v8Undefined(), signature, configuration.length); | |
123 instanceTemplate->Set(v8AtomicString(isolate, configuration.name), funct
ionTemplate, attribute); | |
124 } | |
125 | |
126 static void installAccessors(v8::Handle<v8::ObjectTemplate>, v8::Handle<v8::
Signature>, const AccessorConfiguration*, size_t accessorCount, v8::Isolate*); | |
127 | |
128 static v8::Local<v8::Signature> installDOMClassTemplate(v8::Handle<v8::Funct
ionTemplate>, const char* interfaceName, v8::Handle<v8::FunctionTemplate> parent
Class, size_t fieldCount, | |
129 const AttributeConfiguration*, size_t attributeCount, | |
130 const AccessorConfiguration*, size_t accessorCount, | |
131 const MethodConfiguration*, size_t callbackCount, | |
132 v8::Isolate*); | |
133 | |
134 static v8::Handle<v8::FunctionTemplate> domClassTemplate(v8::Isolate*, Wrapp
erTypeInfo*, void (*)(v8::Handle<v8::FunctionTemplate>, v8::Isolate*)); | |
135 }; | |
136 | |
137 } // namespace WebCore | |
138 | |
139 #endif // V8DOMConfiguration_h | |
OLD | NEW |