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 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 #ifndef V8PerContextData_h 32 #define V8PerContextData_h 33 34 #include "bindings/v8/CustomElementBinding.h" 35 #include "bindings/v8/ScopedPersistent.h" 36 #include "bindings/v8/UnsafePersistent.h" 37 #include "bindings/v8/V8DOMActivityLogger.h" 38 #include "bindings/v8/WrapperTypeInfo.h" 39 #include "gin/public/context_holder.h" 40 #include "gin/public/gin_embedders.h" 41 #include <v8.h> 42 #include "wtf/HashMap.h" 43 #include "wtf/PassOwnPtr.h" 44 #include "wtf/Vector.h" 45 #include "wtf/text/AtomicString.h" 46 #include "wtf/text/AtomicStringHash.h" 47 48 namespace WebCore { 49 50 class CustomElementDefinition; 51 class DOMWrapperWorld; 52 class V8PerContextData; 53 struct V8NPObject; 54 typedef WTF::Vector<V8NPObject*> V8NPObjectVector; 55 typedef WTF::HashMap<int, V8NPObjectVector> V8NPObjectMap; 56 57 enum V8ContextEmbedderDataField { 58 v8ContextDebugIdIndex = static_cast<int>(gin::kDebugIdIndex), 59 v8ContextPerContextDataIndex = static_cast<int>(gin::kPerContextDataStartIndex + gin::kEmbedderBlink), 60 }; 61 62 class V8PerContextDataHolder { 63 WTF_MAKE_NONCOPYABLE(V8PerContextDataHolder); 64 public: install(v8::Handle<v8::Context> context)65 static void install(v8::Handle<v8::Context> context) 66 { 67 new V8PerContextDataHolder(context); 68 } 69 from(v8::Handle<v8::Context> context)70 static V8PerContextDataHolder* from(v8::Handle<v8::Context> context) 71 { 72 return static_cast<V8PerContextDataHolder*>(context->GetAlignedPointerFromEmbedderData(v8ContextPerContextDataIndex)); 73 } 74 perContextData()75 V8PerContextData* perContextData() const { return m_perContextData; } setPerContextData(V8PerContextData * data)76 void setPerContextData(V8PerContextData* data) { m_perContextData = data; } 77 isolatedWorld()78 DOMWrapperWorld* isolatedWorld() const { return m_isolatedWorld; } setIsolatedWorld(DOMWrapperWorld * world)79 void setIsolatedWorld(DOMWrapperWorld* world) { m_isolatedWorld = world; } 80 81 private: V8PerContextDataHolder(v8::Handle<v8::Context> context)82 explicit V8PerContextDataHolder(v8::Handle<v8::Context> context) 83 : m_context(v8::Isolate::GetCurrent(), context) 84 , m_perContextData(0) 85 , m_isolatedWorld(0) 86 { 87 m_context.SetWeak(this, &V8PerContextDataHolder::weakCallback); 88 context->SetAlignedPointerInEmbedderData(v8ContextPerContextDataIndex, this); 89 } 90 ~V8PerContextDataHolder()91 ~V8PerContextDataHolder() {} 92 weakCallback(const v8::WeakCallbackData<v8::Context,V8PerContextDataHolder> & data)93 static void weakCallback(const v8::WeakCallbackData<v8::Context, V8PerContextDataHolder>& data) 94 { 95 data.GetValue()->SetAlignedPointerInEmbedderData(v8ContextPerContextDataIndex, 0); 96 data.GetParameter()->m_context.Reset(); 97 delete data.GetParameter(); 98 } 99 100 v8::Persistent<v8::Context> m_context; 101 V8PerContextData* m_perContextData; 102 DOMWrapperWorld* m_isolatedWorld; 103 }; 104 105 class V8PerContextData { 106 public: create(v8::Handle<v8::Context> context)107 static PassOwnPtr<V8PerContextData> create(v8::Handle<v8::Context> context) 108 { 109 return adoptPtr(new V8PerContextData(context)); 110 } 111 ~V8PerContextData()112 ~V8PerContextData() 113 { 114 dispose(); 115 } 116 117 bool init(); 118 from(v8::Handle<v8::Context> context)119 static V8PerContextData* from(v8::Handle<v8::Context> context) 120 { 121 return V8PerContextDataHolder::from(context)->perContextData(); 122 } 123 124 // To create JS Wrapper objects, we create a cache of a 'boiler plate' 125 // object, and then simply Clone that object each time we need a new one. 126 // This is faster than going through the full object creation process. createWrapperFromCache(const WrapperTypeInfo * type)127 v8::Local<v8::Object> createWrapperFromCache(const WrapperTypeInfo* type) 128 { 129 UnsafePersistent<v8::Object> boilerplate = m_wrapperBoilerplates.get(type); 130 return !boilerplate.isEmpty() ? boilerplate.newLocal(v8::Isolate::GetCurrent())->Clone() : createWrapperFromCacheSlowCase(type); 131 } 132 constructorForType(const WrapperTypeInfo * type)133 v8::Local<v8::Function> constructorForType(const WrapperTypeInfo* type) 134 { 135 UnsafePersistent<v8::Function> function = m_constructorMap.get(type); 136 if (!function.isEmpty()) 137 return function.newLocal(v8::Isolate::GetCurrent()); 138 return constructorForTypeSlowCase(type); 139 } 140 141 v8::Local<v8::Object> prototypeForType(const WrapperTypeInfo*); 142 v8NPObjectMap()143 V8NPObjectMap* v8NPObjectMap() 144 { 145 return &m_v8NPObjectMap; 146 } 147 activityLogger()148 V8DOMActivityLogger* activityLogger() 149 { 150 return m_activityLogger; 151 } 152 setActivityLogger(V8DOMActivityLogger * logger)153 void setActivityLogger(V8DOMActivityLogger* logger) 154 { 155 m_activityLogger = logger; 156 } 157 158 void addCustomElementBinding(CustomElementDefinition*, PassOwnPtr<CustomElementBinding>); 159 void clearCustomElementBinding(CustomElementDefinition*); 160 CustomElementBinding* customElementBinding(CustomElementDefinition*); 161 162 private: V8PerContextData(v8::Handle<v8::Context> context)163 explicit V8PerContextData(v8::Handle<v8::Context> context) 164 : m_activityLogger(0) 165 , m_isolate(v8::Isolate::GetCurrent()) 166 , m_context(m_isolate, context) 167 , m_customElementBindings(adoptPtr(new CustomElementBindingMap())) 168 { 169 } 170 171 void dispose(); 172 173 v8::Local<v8::Object> createWrapperFromCacheSlowCase(const WrapperTypeInfo*); 174 v8::Local<v8::Function> constructorForTypeSlowCase(const WrapperTypeInfo*); 175 176 // For each possible type of wrapper, we keep a boilerplate object. 177 // The boilerplate is used to create additional wrappers of the same type. 178 typedef WTF::HashMap<const WrapperTypeInfo*, UnsafePersistent<v8::Object> > WrapperBoilerplateMap; 179 WrapperBoilerplateMap m_wrapperBoilerplates; 180 181 typedef WTF::HashMap<const WrapperTypeInfo*, UnsafePersistent<v8::Function> > ConstructorMap; 182 ConstructorMap m_constructorMap; 183 184 V8NPObjectMap m_v8NPObjectMap; 185 // We cache a pointer to the V8DOMActivityLogger associated with the world 186 // corresponding to this context. The ownership of the pointer is retained 187 // by the DOMActivityLoggerMap in DOMWrapperWorld. 188 V8DOMActivityLogger* m_activityLogger; 189 v8::Isolate* m_isolate; 190 v8::Persistent<v8::Context> m_context; 191 ScopedPersistent<v8::Value> m_errorPrototype; 192 193 typedef WTF::HashMap<CustomElementDefinition*, OwnPtr<CustomElementBinding> > CustomElementBindingMap; 194 OwnPtr<CustomElementBindingMap> m_customElementBindings; 195 }; 196 197 class V8PerContextDebugData { 198 public: 199 static bool setContextDebugData(v8::Handle<v8::Context>, const char* worldName, int debugId); 200 static int contextDebugId(v8::Handle<v8::Context>); 201 }; 202 203 } // namespace WebCore 204 205 #endif // V8PerContextData_h 206