| 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 SKY_ENGINE_BINDINGS_CORE_V8_V8DOMCONFIGURATION_H_ | |
| 30 #define SKY_ENGINE_BINDINGS_CORE_V8_V8DOMCONFIGURATION_H_ | |
| 31 | |
| 32 #include "sky/engine/bindings/core/v8/V8Binding.h" | |
| 33 #include "sky/engine/bindings/core/v8/V8DOMWrapper.h" | |
| 34 #include "v8/include/v8.h" | |
| 35 | |
| 36 namespace blink { | |
| 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 enum ExposeConfiguration { | |
| 47 ExposedToAllScripts, | |
| 48 }; | |
| 49 | |
| 50 enum InstanceOrPrototypeConfiguration { | |
| 51 OnInstance, | |
| 52 OnPrototype, | |
| 53 }; | |
| 54 | |
| 55 // AttributeConfiguration translates into calls to SetAccessor() on either | |
| 56 // the instance or the prototype ObjectTemplate, based on |instanceOrPrototy
peConfiguration|. | |
| 57 struct AttributeConfiguration { | |
| 58 const char* const name; | |
| 59 v8::AccessorGetterCallback getter; | |
| 60 v8::AccessorSetterCallback setter; | |
| 61 v8::AccessorGetterCallback getterForMainWorld; | |
| 62 v8::AccessorSetterCallback setterForMainWorld; | |
| 63 const WrapperTypeInfo* data; | |
| 64 v8::AccessControl settings; | |
| 65 v8::PropertyAttribute attribute; | |
| 66 ExposeConfiguration exposeConfiguration; | |
| 67 InstanceOrPrototypeConfiguration instanceOrPrototypeConfiguration; | |
| 68 }; | |
| 69 | |
| 70 // AccessorConfiguration translates into calls to SetAccessorProperty() | |
| 71 // on prototype ObjectTemplate. | |
| 72 struct AccessorConfiguration { | |
| 73 const char* const name; | |
| 74 v8::FunctionCallback getter; | |
| 75 v8::FunctionCallback setter; | |
| 76 v8::FunctionCallback getterForMainWorld; | |
| 77 v8::FunctionCallback setterForMainWorld; | |
| 78 const WrapperTypeInfo* data; | |
| 79 v8::AccessControl settings; | |
| 80 v8::PropertyAttribute attribute; | |
| 81 ExposeConfiguration exposeConfiguration; | |
| 82 }; | |
| 83 | |
| 84 static void installAttributes(v8::Handle<v8::ObjectTemplate>, v8::Handle<v8:
:ObjectTemplate>, const AttributeConfiguration*, size_t attributeCount, v8::Isol
ate*); | |
| 85 | |
| 86 template<class ObjectOrTemplate> | |
| 87 static inline void installAttribute(v8::Handle<ObjectOrTemplate> instanceTem
plate, v8::Handle<ObjectOrTemplate> prototype, const AttributeConfiguration& att
ribute, v8::Isolate* isolate) | |
| 88 { | |
| 89 DOMWrapperWorld& world = DOMWrapperWorld::current(isolate); | |
| 90 v8::AccessorGetterCallback getter = attribute.getter; | |
| 91 v8::AccessorSetterCallback setter = attribute.setter; | |
| 92 if (world.isMainWorld()) { | |
| 93 if (attribute.getterForMainWorld) | |
| 94 getter = attribute.getterForMainWorld; | |
| 95 if (attribute.setterForMainWorld) | |
| 96 setter = attribute.setterForMainWorld; | |
| 97 } | |
| 98 (attribute.instanceOrPrototypeConfiguration == OnPrototype ? prototype :
instanceTemplate)->SetAccessor(v8AtomicString(isolate, attribute.name), | |
| 99 getter, | |
| 100 setter, | |
| 101 v8::External::New(isolate, const_cast<WrapperTypeInfo*>(attribute.da
ta)), | |
| 102 attribute.settings, | |
| 103 attribute.attribute); | |
| 104 } | |
| 105 | |
| 106 enum ConstantType { | |
| 107 ConstantTypeShort, | |
| 108 ConstantTypeLong, | |
| 109 ConstantTypeUnsignedShort, | |
| 110 ConstantTypeUnsignedLong, | |
| 111 ConstantTypeFloat, | |
| 112 ConstantTypeDouble, | |
| 113 ConstantTypeString | |
| 114 }; | |
| 115 | |
| 116 // ConstantConfiguration translates into calls to Set() for setting up an | |
| 117 // object's constants. It sets the constant on both the FunctionTemplate and | |
| 118 // the ObjectTemplate. PropertyAttributes is always ReadOnly. | |
| 119 struct ConstantConfiguration { | |
| 120 const char* const name; | |
| 121 int ivalue; | |
| 122 double dvalue; | |
| 123 const char* const svalue; | |
| 124 ConstantType type; | |
| 125 }; | |
| 126 | |
| 127 static void installConstants(v8::Handle<v8::FunctionTemplate>, v8::Handle<v8
::ObjectTemplate>, const ConstantConfiguration*, size_t constantCount, v8::Isola
te*); | |
| 128 | |
| 129 // MethodConfiguration translates into calls to Set() for setting up an | |
| 130 // object's callbacks. It sets the method on both the FunctionTemplate or | |
| 131 // the ObjectTemplate. | |
| 132 struct MethodConfiguration { | |
| 133 v8::Local<v8::Name> methodName(v8::Isolate* isolate) const { return v8At
omicString(isolate, name); } | |
| 134 v8::FunctionCallback callbackForWorld(const DOMWrapperWorld& world) cons
t | |
| 135 { | |
| 136 return world.isMainWorld() && callbackForMainWorld ? callbackForMain
World : callback; | |
| 137 } | |
| 138 | |
| 139 const char* const name; | |
| 140 v8::FunctionCallback callback; | |
| 141 v8::FunctionCallback callbackForMainWorld; | |
| 142 int length; | |
| 143 ExposeConfiguration exposeConfiguration; | |
| 144 }; | |
| 145 | |
| 146 struct SymbolKeyedMethodConfiguration { | |
| 147 v8::Local<v8::Name> methodName(v8::Isolate* isolate) const { return getS
ymbol(isolate); } | |
| 148 v8::FunctionCallback callbackForWorld(const DOMWrapperWorld&) const | |
| 149 { | |
| 150 return callback; | |
| 151 } | |
| 152 | |
| 153 v8::Local<v8::Symbol> (*getSymbol)(v8::Isolate*); | |
| 154 v8::FunctionCallback callback; | |
| 155 // SymbolKeyedMethodConfiguration doesn't support per-world bindings. | |
| 156 int length; | |
| 157 ExposeConfiguration exposeConfiguration; | |
| 158 }; | |
| 159 | |
| 160 static void installMethods(v8::Handle<v8::ObjectTemplate>, v8::Handle<v8::Si
gnature>, v8::PropertyAttribute, const MethodConfiguration*, size_t callbackCoun
t, v8::Isolate*); | |
| 161 | |
| 162 template <class ObjectOrTemplate, class Configuration> | |
| 163 static void installMethod(v8::Handle<ObjectOrTemplate> objectOrTemplate, v8:
:Handle<v8::Signature> signature, v8::PropertyAttribute attribute, const Configu
ration& callback, v8::Isolate* isolate) | |
| 164 { | |
| 165 DOMWrapperWorld& world = DOMWrapperWorld::current(isolate); | |
| 166 v8::Local<v8::FunctionTemplate> functionTemplate = functionTemplateForCa
llback(signature, callback.callbackForWorld(world), callback.length, isolate); | |
| 167 setMethod(objectOrTemplate, callback.methodName(isolate), functionTempla
te, attribute); | |
| 168 } | |
| 169 | |
| 170 static void installAccessors(v8::Handle<v8::ObjectTemplate>, v8::Handle<v8::
Signature>, const AccessorConfiguration*, size_t accessorCount, v8::Isolate*); | |
| 171 | |
| 172 static v8::Local<v8::Signature> installDOMClassTemplate(v8::Handle<v8::Funct
ionTemplate>, const char* interfaceName, v8::Handle<v8::FunctionTemplate> parent
Class, size_t fieldCount, | |
| 173 const AttributeConfiguration*, size_t attributeCount, | |
| 174 const AccessorConfiguration*, size_t accessorCount, | |
| 175 const MethodConfiguration*, size_t callbackCount, | |
| 176 v8::Isolate*); | |
| 177 | |
| 178 static v8::Handle<v8::FunctionTemplate> domClassTemplate(v8::Isolate*, Wrapp
erTypeInfo*, void (*)(v8::Handle<v8::FunctionTemplate>, v8::Isolate*)); | |
| 179 | |
| 180 private: | |
| 181 static void setMethod(v8::Handle<v8::Object> target, v8::Handle<v8::Name> na
me, v8::Handle<v8::FunctionTemplate> functionTemplate, v8::PropertyAttribute att
ribute) | |
| 182 { | |
| 183 target->Set(name, functionTemplate->GetFunction()); | |
| 184 } | |
| 185 static void setMethod(v8::Handle<v8::FunctionTemplate> target, v8::Handle<v8
::Name> name, v8::Handle<v8::FunctionTemplate> functionTemplate, v8::PropertyAtt
ribute attribute) | |
| 186 { | |
| 187 target->Set(name, functionTemplate, attribute); | |
| 188 } | |
| 189 static void setMethod(v8::Handle<v8::ObjectTemplate> target, v8::Handle<v8::
Name> name, v8::Handle<v8::FunctionTemplate> functionTemplate, v8::PropertyAttri
bute attribute) | |
| 190 { | |
| 191 target->Set(name, functionTemplate, attribute); | |
| 192 } | |
| 193 | |
| 194 static v8::Handle<v8::FunctionTemplate> functionTemplateForCallback(v8::Hand
le<v8::Signature>, v8::FunctionCallback, int length, v8::Isolate*); | |
| 195 }; | |
| 196 | |
| 197 } // namespace blink | |
| 198 | |
| 199 #endif // SKY_ENGINE_BINDINGS_CORE_V8_V8DOMCONFIGURATION_H_ | |
| OLD | NEW |