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 #include "config.h" 30 #include "bindings/core/v8/V8DOMConfiguration.h" 31 32 #include "bindings/core/v8/V8ObjectConstructor.h" 33 #include "platform/TraceEvent.h" 34 35 namespace blink { 36 37 void V8DOMConfiguration::installAttributes(v8::Handle<v8::ObjectTemplate> instanceTemplate, v8::Handle<v8::ObjectTemplate> prototype, const AttributeConfiguration* attributes, size_t attributeCount, v8::Isolate* isolate) 38 { 39 for (size_t i = 0; i < attributeCount; ++i) 40 installAttribute(instanceTemplate, prototype, attributes[i], isolate); 41 } 42 43 void V8DOMConfiguration::installAccessors(v8::Handle<v8::ObjectTemplate> prototype, v8::Handle<v8::Signature> signature, const AccessorConfiguration* accessors, size_t accessorCount, v8::Isolate* isolate) 44 { 45 DOMWrapperWorld& world = DOMWrapperWorld::current(isolate); 46 for (size_t i = 0; i < accessorCount; ++i) { 47 if (accessors[i].exposeConfiguration == OnlyExposedToPrivateScript && !world.isPrivateScriptIsolatedWorld()) 48 continue; 49 50 v8::FunctionCallback getterCallback = accessors[i].getter; 51 v8::FunctionCallback setterCallback = accessors[i].setter; 52 if (world.isMainWorld()) { 53 if (accessors[i].getterForMainWorld) 54 getterCallback = accessors[i].getterForMainWorld; 55 if (accessors[i].setterForMainWorld) 56 setterCallback = accessors[i].setterForMainWorld; 57 } 58 59 v8::Local<v8::FunctionTemplate> getter; 60 if (getterCallback) { 61 getter = v8::FunctionTemplate::New(isolate, getterCallback, v8::External::New(isolate, const_cast<WrapperTypeInfo*>(accessors[i].data)), signature, 0); 62 getter->RemovePrototype(); 63 } 64 v8::Local<v8::FunctionTemplate> setter; 65 if (setterCallback) { 66 setter = v8::FunctionTemplate::New(isolate, setterCallback, v8::External::New(isolate, const_cast<WrapperTypeInfo*>(accessors[i].data)), signature, 1); 67 setter->RemovePrototype(); 68 } 69 prototype->SetAccessorProperty(v8AtomicString(isolate, accessors[i].name), getter, setter, accessors[i].attribute, accessors[i].settings); 70 } 71 } 72 73 // Constant installation 74 // 75 // installConstants() is be used for simple constants. It installs constants 76 // using v8::Template::Set(), which results in a property that is much faster to 77 // access from scripts. 78 // installConstant() is used when some C++ code needs to be executed when the 79 // constant is accessed, e.g. to handle deprecation or measuring usage. The 80 // property appears the same to scripts, but is slower to access. 81 82 void V8DOMConfiguration::installConstants(v8::Handle<v8::FunctionTemplate> functionDescriptor, v8::Handle<v8::ObjectTemplate> prototype, const ConstantConfiguration* constants, size_t constantCount, v8::Isolate* isolate) 83 { 84 for (size_t i = 0; i < constantCount; ++i) { 85 const ConstantConfiguration* constant = &constants[i]; 86 v8::Handle<v8::String> constantName = v8AtomicString(isolate, constant->name); 87 switch (constant->type) { 88 case ConstantTypeShort: 89 case ConstantTypeLong: 90 case ConstantTypeUnsignedShort: 91 functionDescriptor->Set(constantName, v8::Integer::New(isolate, constant->ivalue), static_cast<v8::PropertyAttribute>(v8::ReadOnly | v8::DontDelete)); 92 prototype->Set(constantName, v8::Integer::New(isolate, constant->ivalue), static_cast<v8::PropertyAttribute>(v8::ReadOnly | v8::DontDelete)); 93 break; 94 case ConstantTypeUnsignedLong: 95 functionDescriptor->Set(constantName, v8::Integer::NewFromUnsigned(isolate, constant->ivalue), static_cast<v8::PropertyAttribute>(v8::ReadOnly | v8::DontDelete)); 96 prototype->Set(constantName, v8::Integer::NewFromUnsigned(isolate, constant->ivalue), static_cast<v8::PropertyAttribute>(v8::ReadOnly | v8::DontDelete)); 97 break; 98 case ConstantTypeFloat: 99 case ConstantTypeDouble: 100 functionDescriptor->Set(constantName, v8::Number::New(isolate, constant->dvalue), static_cast<v8::PropertyAttribute>(v8::ReadOnly | v8::DontDelete)); 101 prototype->Set(constantName, v8::Number::New(isolate, constant->dvalue), static_cast<v8::PropertyAttribute>(v8::ReadOnly | v8::DontDelete)); 102 break; 103 case ConstantTypeString: 104 functionDescriptor->Set(constantName, v8::String::NewFromUtf8(isolate, constant->svalue), static_cast<v8::PropertyAttribute>(v8::ReadOnly | v8::DontDelete)); 105 prototype->Set(constantName, v8::String::NewFromUtf8(isolate, constant->svalue), static_cast<v8::PropertyAttribute>(v8::ReadOnly | v8::DontDelete)); 106 break; 107 default: 108 ASSERT_NOT_REACHED(); 109 } 110 } 111 } 112 113 void V8DOMConfiguration::installConstant(v8::Handle<v8::FunctionTemplate> functionDescriptor, v8::Handle<v8::ObjectTemplate> prototype, const char* name, v8::AccessorGetterCallback getter, v8::Isolate* isolate) 114 { 115 v8::Handle<v8::String> constantName = v8AtomicString(isolate, name); 116 functionDescriptor->SetNativeDataProperty(constantName, getter, 0, v8::Handle<v8::Value>(), static_cast<v8::PropertyAttribute>(v8::ReadOnly | v8::DontDelete)); 117 prototype->SetNativeDataProperty(constantName, getter, 0, v8::Handle<v8::Value>(), static_cast<v8::PropertyAttribute>(v8::ReadOnly | v8::DontDelete)); 118 } 119 120 void V8DOMConfiguration::installMethods(v8::Handle<v8::ObjectTemplate> prototype, v8::Handle<v8::Signature> signature, v8::PropertyAttribute attributes, const MethodConfiguration* callbacks, size_t callbackCount, v8::Isolate* isolate) 121 { 122 for (size_t i = 0; i < callbackCount; ++i) 123 installMethod(prototype, signature, attributes, callbacks[i], isolate); 124 } 125 126 v8::Handle<v8::FunctionTemplate> V8DOMConfiguration::functionTemplateForCallback(v8::Handle<v8::Signature> signature, v8::FunctionCallback callback, int length, v8::Isolate* isolate) 127 { 128 v8::Local<v8::FunctionTemplate> functionTemplate = v8::FunctionTemplate::New(isolate, callback, v8Undefined(), signature, length); 129 functionTemplate->RemovePrototype(); 130 return functionTemplate; 131 } 132 133 v8::Local<v8::Signature> V8DOMConfiguration::installDOMClassTemplate(v8::Handle<v8::FunctionTemplate> functionDescriptor, const char* interfaceName, v8::Handle<v8::FunctionTemplate> parentClass, size_t fieldCount, 134 const AttributeConfiguration* attributes, size_t attributeCount, 135 const AccessorConfiguration* accessors, size_t accessorCount, 136 const MethodConfiguration* callbacks, size_t callbackCount, 137 v8::Isolate* isolate) 138 { 139 functionDescriptor->SetClassName(v8AtomicString(isolate, interfaceName)); 140 v8::Local<v8::ObjectTemplate> instanceTemplate = functionDescriptor->InstanceTemplate(); 141 instanceTemplate->SetInternalFieldCount(fieldCount); 142 if (!parentClass.IsEmpty()) { 143 functionDescriptor->Inherit(parentClass); 144 // Marks the prototype object as one of native-backed objects. 145 // This is needed since bug 110436 asks WebKit to tell native-initiated prototypes from pure-JS ones. 146 // This doesn't mark kinds "root" classes like Node, where setting this changes prototype chain structure. 147 v8::Local<v8::ObjectTemplate> prototype = functionDescriptor->PrototypeTemplate(); 148 prototype->SetInternalFieldCount(v8PrototypeInternalFieldcount); 149 } 150 151 v8::Local<v8::Signature> defaultSignature = v8::Signature::New(isolate, functionDescriptor); 152 if (attributeCount) 153 installAttributes(instanceTemplate, functionDescriptor->PrototypeTemplate(), attributes, attributeCount, isolate); 154 if (accessorCount) 155 installAccessors(functionDescriptor->PrototypeTemplate(), defaultSignature, accessors, accessorCount, isolate); 156 if (callbackCount) 157 installMethods(functionDescriptor->PrototypeTemplate(), defaultSignature, static_cast<v8::PropertyAttribute>(v8::DontDelete), callbacks, callbackCount, isolate); 158 return defaultSignature; 159 } 160 161 v8::Handle<v8::FunctionTemplate> V8DOMConfiguration::domClassTemplate(v8::Isolate* isolate, WrapperTypeInfo* wrapperTypeInfo, void (*configureDOMClassTemplate)(v8::Handle<v8::FunctionTemplate>, v8::Isolate*)) 162 { 163 V8PerIsolateData* data = V8PerIsolateData::from(isolate); 164 v8::Local<v8::FunctionTemplate> result = data->existingDOMTemplate(wrapperTypeInfo); 165 if (!result.IsEmpty()) 166 return result; 167 168 TRACE_EVENT_SCOPED_SAMPLING_STATE("blink", "BuildDOMTemplate"); 169 result = v8::FunctionTemplate::New(isolate, V8ObjectConstructor::isValidConstructorMode); 170 configureDOMClassTemplate(result, isolate); 171 data->setDOMTemplate(wrapperTypeInfo, result); 172 return result; 173 } 174 175 } // namespace blink 176