1 /* 2 * Copyright (C) 2009 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 are 6 * met: 7 * 8 * * Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * * Redistributions in binary form must reproduce the above 11 * copyright notice, this list of conditions and the following disclaimer 12 * in the documentation and/or other materials provided with the 13 * distribution. 14 * * Neither the name of Google Inc. nor the names of its 15 * contributors may be used to endorse or promote products derived from 16 * this software without specific prior written permission. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 */ 30 31 #include "config.h" 32 #include "bindings/v8/V8DOMWrapper.h" 33 34 #include "V8HTMLCollection.h" 35 #include "V8HTMLDocument.h" 36 #include "V8Window.h" 37 #include "bindings/v8/V8Binding.h" 38 #include "bindings/v8/V8HiddenPropertyName.h" 39 #include "bindings/v8/V8ObjectConstructor.h" 40 #include "bindings/v8/V8PerContextData.h" 41 #include "bindings/v8/V8ScriptRunner.h" 42 43 namespace WebCore { 44 45 static v8::Local<v8::Object> wrapInShadowTemplate(v8::Local<v8::Object> wrapper, Node* impl, v8::Isolate* isolate) 46 { 47 // This is only for getting a unique pointer which we can pass to privateTemplate. 48 static int shadowTemplateUniqueKey; 49 WrapperWorldType currentWorldType = worldType(isolate); 50 V8PerIsolateData* data = V8PerIsolateData::from(isolate); 51 v8::Handle<v8::FunctionTemplate> shadowTemplate = data->privateTemplateIfExists(currentWorldType, &shadowTemplateUniqueKey); 52 if (shadowTemplate.IsEmpty()) { 53 shadowTemplate = v8::FunctionTemplate::New(isolate); 54 if (shadowTemplate.IsEmpty()) 55 return v8::Local<v8::Object>(); 56 shadowTemplate->SetClassName(v8AtomicString(isolate, "HTMLDocument")); 57 shadowTemplate->Inherit(V8HTMLDocument::domTemplate(isolate, currentWorldType)); 58 shadowTemplate->InstanceTemplate()->SetInternalFieldCount(V8HTMLDocument::internalFieldCount); 59 data->setPrivateTemplate(currentWorldType, &shadowTemplateUniqueKey, shadowTemplate); 60 } 61 62 v8::Local<v8::Function> shadowConstructor = shadowTemplate->GetFunction(); 63 if (shadowConstructor.IsEmpty()) 64 return v8::Local<v8::Object>(); 65 66 v8::Local<v8::Object> shadow = V8ScriptRunner::instantiateObject(shadowConstructor); 67 if (shadow.IsEmpty()) 68 return v8::Local<v8::Object>(); 69 shadow->SetPrototype(wrapper); 70 V8DOMWrapper::setNativeInfo(wrapper, &V8HTMLDocument::wrapperTypeInfo, impl); 71 return shadow; 72 } 73 74 v8::Local<v8::Object> V8DOMWrapper::createWrapper(v8::Handle<v8::Object> creationContext, const WrapperTypeInfo* type, void* impl, v8::Isolate* isolate) 75 { 76 V8WrapperInstantiationScope scope(creationContext, isolate); 77 78 V8PerContextData* perContextData = V8PerContextData::from(scope.context()); 79 v8::Local<v8::Object> wrapper = perContextData ? perContextData->createWrapperFromCache(type) : V8ObjectConstructor::newInstance(type->domTemplate(isolate, worldTypeInMainThread(isolate))->GetFunction()); 80 81 if (type == &V8HTMLDocument::wrapperTypeInfo && !wrapper.IsEmpty()) 82 wrapper = wrapInShadowTemplate(wrapper, static_cast<Node*>(impl), isolate); 83 84 return wrapper; 85 } 86 87 static bool hasInternalField(v8::Handle<v8::Value> value) 88 { 89 if (value.IsEmpty() || !value->IsObject()) 90 return false; 91 return v8::Handle<v8::Object>::Cast(value)->InternalFieldCount(); 92 } 93 94 #ifndef NDEBUG 95 bool V8DOMWrapper::maybeDOMWrapper(v8::Handle<v8::Value> value) 96 { 97 if (!hasInternalField(value)) 98 return false; 99 100 v8::Handle<v8::Object> object = v8::Handle<v8::Object>::Cast(value); 101 ASSERT(object->InternalFieldCount() >= v8DefaultWrapperInternalFieldCount); 102 103 v8::HandleScope scope(v8::Isolate::GetCurrent()); 104 ASSERT(object->GetAlignedPointerFromInternalField(v8DOMWrapperObjectIndex)); 105 106 const WrapperTypeInfo* typeInfo = static_cast<const WrapperTypeInfo*>(object->GetAlignedPointerFromInternalField(v8DOMWrapperTypeIndex)); 107 108 return typeInfo->ginEmbedder == gin::kEmbedderBlink; 109 } 110 #endif 111 112 bool V8DOMWrapper::isDOMWrapper(v8::Handle<v8::Value> value) 113 { 114 if (value.IsEmpty() || !value->IsObject()) 115 return false; 116 117 v8::Handle<v8::Object> wrapper = v8::Handle<v8::Object>::Cast(value); 118 if (wrapper->InternalFieldCount() < v8DefaultWrapperInternalFieldCount) 119 return false; 120 ASSERT(wrapper->GetAlignedPointerFromInternalField(v8DOMWrapperObjectIndex)); 121 ASSERT(wrapper->GetAlignedPointerFromInternalField(v8DOMWrapperTypeIndex)); 122 123 const WrapperTypeInfo* typeInfo = static_cast<const WrapperTypeInfo*>(wrapper->GetAlignedPointerFromInternalField(v8DOMWrapperTypeIndex)); 124 125 // FIXME: Add class id checks. 126 return typeInfo->ginEmbedder == gin::kEmbedderBlink; 127 } 128 129 bool V8DOMWrapper::isWrapperOfType(v8::Handle<v8::Value> value, const WrapperTypeInfo* type) 130 { 131 if (!hasInternalField(value)) 132 return false; 133 134 v8::Handle<v8::Object> wrapper = v8::Handle<v8::Object>::Cast(value); 135 ASSERT(wrapper->InternalFieldCount() >= v8DefaultWrapperInternalFieldCount); 136 ASSERT(wrapper->GetAlignedPointerFromInternalField(v8DOMWrapperObjectIndex)); 137 138 const WrapperTypeInfo* typeInfo = static_cast<const WrapperTypeInfo*>(wrapper->GetAlignedPointerFromInternalField(v8DOMWrapperTypeIndex)); 139 return typeInfo->ginEmbedder == gin::kEmbedderBlink && typeInfo == type; 140 } 141 142 } // namespace WebCore 143