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 35 namespace WebCore { 36 37 class V8DOMConfiguration { 38 public: 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*); 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 v8::AccessorGetterCallback getter = attribute.getter; 78 v8::AccessorSetterCallback setter = attribute.setter; 79 if (DOMWrapperWorld::current(isolate).isMainWorld()) { 80 if (attribute.getterForMainWorld) 81 getter = attribute.getterForMainWorld; 82 if (attribute.setterForMainWorld) 83 setter = attribute.setterForMainWorld; 84 } 85 (attribute.onPrototype ? prototype : instanceTemplate)->SetAccessor(v8::String::NewFromUtf8(isolate, attribute.name, v8::String::kInternalizedString), 86 getter, 87 setter, 88 v8::External::New(isolate, const_cast<WrapperTypeInfo*>(attribute.data)), 89 attribute.settings, 90 attribute.attribute); 91 } 92 93 // ConstantConfiguration translates into calls to Set() for setting up an 94 // object's constants. It sets the constant on both the FunctionTemplate and 95 // the ObjectTemplate. PropertyAttributes is always ReadOnly. 96 struct ConstantConfiguration { 97 const char* const name; 98 int value; 99 }; 100 101 static void installConstants(v8::Handle<v8::FunctionTemplate>, v8::Handle<v8::ObjectTemplate>, const ConstantConfiguration*, size_t constantCount, v8::Isolate*); 102 103 // MethodConfiguration translates into calls to Set() on the prototype 104 // ObjectTemplate. 105 struct MethodConfiguration { 106 const char* const name; 107 v8::FunctionCallback callback; 108 v8::FunctionCallback callbackForMainWorld; 109 int length; 110 }; 111 112 static void installCallbacks(v8::Handle<v8::ObjectTemplate>, v8::Handle<v8::Signature>, v8::PropertyAttribute, const MethodConfiguration*, size_t callbackCount, v8::Isolate*); 113 114 static void installAccessors(v8::Handle<v8::ObjectTemplate>, v8::Handle<v8::Signature>, const AccessorConfiguration*, size_t accessorCount, v8::Isolate*); 115 116 static v8::Local<v8::Signature> installDOMClassTemplate(v8::Handle<v8::FunctionTemplate>, const char* interfaceName, v8::Handle<v8::FunctionTemplate> parentClass, size_t fieldCount, 117 const AttributeConfiguration*, size_t attributeCount, 118 const AccessorConfiguration*, size_t accessorCount, 119 const MethodConfiguration*, size_t callbackCount, 120 v8::Isolate*); 121 122 static v8::Handle<v8::FunctionTemplate> domClassTemplate(v8::Isolate*, WrapperTypeInfo*, void (*)(v8::Handle<v8::FunctionTemplate>, v8::Isolate*)); 123 }; 124 125 } // namespace WebCore 126 127 #endif // V8DOMConfiguration_h 128