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/V8PersistentValueMap.h" 37 #include "bindings/v8/WrapperTypeInfo.h" 38 #include "gin/public/context_holder.h" 39 #include "gin/public/gin_embedders.h" 40 #include <v8.h> 41 #include "wtf/HashMap.h" 42 #include "wtf/PassOwnPtr.h" 43 #include "wtf/Vector.h" 44 #include "wtf/text/AtomicString.h" 45 #include "wtf/text/AtomicStringHash.h" 46 47 namespace WebCore { 48 49 class CustomElementDefinition; 50 class V8DOMActivityLogger; 51 class V8PerContextData; 52 struct V8NPObject; 53 typedef WTF::Vector<V8NPObject*> V8NPObjectVector; 54 typedef WTF::HashMap<int, V8NPObjectVector> V8NPObjectMap; 55 56 enum V8ContextEmbedderDataField { 57 v8ContextDebugIdIndex = static_cast<int>(gin::kDebugIdIndex), 58 v8ContextPerContextDataIndex = static_cast<int>(gin::kPerContextDataStartIndex + gin::kEmbedderBlink), 59 }; 60 61 class V8PerContextData { 62 public: 63 static PassOwnPtr<V8PerContextData> create(v8::Handle<v8::Context>); 64 65 static V8PerContextData* from(v8::Handle<v8::Context>); 66 67 ~V8PerContextData(); 68 context()69 v8::Handle<v8::Context> context() { return m_context.newLocal(m_isolate); } 70 71 // To create JS Wrapper objects, we create a cache of a 'boiler plate' 72 // object, and then simply Clone that object each time we need a new one. 73 // This is faster than going through the full object creation process. createWrapperFromCache(const WrapperTypeInfo * type)74 v8::Local<v8::Object> createWrapperFromCache(const WrapperTypeInfo* type) 75 { 76 v8::Local<v8::Object> boilerplate = m_wrapperBoilerplates.Get(type); 77 return !boilerplate.IsEmpty() ? boilerplate->Clone() : createWrapperFromCacheSlowCase(type); 78 } 79 constructorForType(const WrapperTypeInfo * type)80 v8::Local<v8::Function> constructorForType(const WrapperTypeInfo* type) 81 { 82 v8::Local<v8::Function> function = m_constructorMap.Get(type); 83 return (!function.IsEmpty()) ? function : constructorForTypeSlowCase(type); 84 } 85 86 v8::Local<v8::Object> prototypeForType(const WrapperTypeInfo*); 87 v8NPObjectMap()88 V8NPObjectMap* v8NPObjectMap() { return &m_v8NPObjectMap; } 89 90 void addCustomElementBinding(CustomElementDefinition*, PassOwnPtr<CustomElementBinding>); 91 void clearCustomElementBinding(CustomElementDefinition*); 92 CustomElementBinding* customElementBinding(CustomElementDefinition*); 93 activityLogger()94 V8DOMActivityLogger* activityLogger() const { return m_activityLogger; } setActivityLogger(V8DOMActivityLogger * activityLogger)95 void setActivityLogger(V8DOMActivityLogger* activityLogger) { m_activityLogger = activityLogger; } 96 97 private: 98 V8PerContextData(v8::Handle<v8::Context>); 99 100 v8::Local<v8::Object> createWrapperFromCacheSlowCase(const WrapperTypeInfo*); 101 v8::Local<v8::Function> constructorForTypeSlowCase(const WrapperTypeInfo*); 102 103 // For each possible type of wrapper, we keep a boilerplate object. 104 // The boilerplate is used to create additional wrappers of the same type. 105 typedef V8PersistentValueMap<const WrapperTypeInfo*, v8::Object, false> WrapperBoilerplateMap; 106 WrapperBoilerplateMap m_wrapperBoilerplates; 107 108 typedef V8PersistentValueMap<const WrapperTypeInfo*, v8::Function, false> ConstructorMap; 109 ConstructorMap m_constructorMap; 110 111 V8NPObjectMap m_v8NPObjectMap; 112 113 v8::Isolate* m_isolate; 114 OwnPtr<gin::ContextHolder> m_contextHolder; 115 116 ScopedPersistent<v8::Context> m_context; 117 ScopedPersistent<v8::Value> m_errorPrototype; 118 119 typedef WTF::HashMap<CustomElementDefinition*, OwnPtr<CustomElementBinding> > CustomElementBindingMap; 120 OwnPtr<CustomElementBindingMap> m_customElementBindings; 121 122 // This is owned by a static hash map in V8DOMActivityLogger. 123 V8DOMActivityLogger* m_activityLogger; 124 }; 125 126 class V8PerContextDebugData { 127 public: 128 static bool setContextDebugData(v8::Handle<v8::Context>, const char* worldName, int debugId); 129 static int contextDebugId(v8::Handle<v8::Context>); 130 }; 131 132 } // namespace WebCore 133 134 #endif // V8PerContextData_h 135