2 * Copyright (C) 2012 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
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
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.
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.
31 #ifndef V8PerContextData_h
32 #define V8PerContextData_h
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"
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"
50 class CustomElementDefinition;
51 class DOMWrapperWorld;
52 class V8PerContextData;
54 typedef WTF::Vector<V8NPObject*> V8NPObjectVector;
55 typedef WTF::HashMap<int, V8NPObjectVector> V8NPObjectMap;
57 enum V8ContextEmbedderDataField {
58 v8ContextDebugIdIndex = static_cast<int>(gin::kDebugIdIndex),
59 v8ContextPerContextDataIndex = static_cast<int>(gin::kPerContextDataStartIndex + gin::kEmbedderBlink),
62 class V8PerContextDataHolder {
63 WTF_MAKE_NONCOPYABLE(V8PerContextDataHolder);
65 static void install(v8::Handle<v8::Context> context)
67 new V8PerContextDataHolder(context);
70 static V8PerContextDataHolder* from(v8::Handle<v8::Context> context)
72 return static_cast<V8PerContextDataHolder*>(context->GetAlignedPointerFromEmbedderData(v8ContextPerContextDataIndex));
75 V8PerContextData* perContextData() const { return m_perContextData; }
76 void setPerContextData(V8PerContextData* data) { m_perContextData = data; }
78 DOMWrapperWorld* isolatedWorld() const { return m_isolatedWorld; }
79 void setIsolatedWorld(DOMWrapperWorld* world) { m_isolatedWorld = world; }
82 explicit V8PerContextDataHolder(v8::Handle<v8::Context> context)
83 : m_context(v8::Isolate::GetCurrent(), context)
87 m_context.SetWeak(this, &V8PerContextDataHolder::weakCallback);
88 context->SetAlignedPointerInEmbedderData(v8ContextPerContextDataIndex, this);
91 ~V8PerContextDataHolder() {}
93 static void weakCallback(const v8::WeakCallbackData<v8::Context, V8PerContextDataHolder>& data)
95 data.GetValue()->SetAlignedPointerInEmbedderData(v8ContextPerContextDataIndex, 0);
96 data.GetParameter()->m_context.Reset();
97 delete data.GetParameter();
100 v8::Persistent<v8::Context> m_context;
101 V8PerContextData* m_perContextData;
102 DOMWrapperWorld* m_isolatedWorld;
105 class V8PerContextData {
107 static PassOwnPtr<V8PerContextData> create(v8::Handle<v8::Context> context)
109 return adoptPtr(new V8PerContextData(context));
119 static V8PerContextData* from(v8::Handle<v8::Context> context)
121 return V8PerContextDataHolder::from(context)->perContextData();
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.
127 v8::Local<v8::Object> createWrapperFromCache(const WrapperTypeInfo* type)
129 UnsafePersistent<v8::Object> boilerplate = m_wrapperBoilerplates.get(type);
130 return !boilerplate.isEmpty() ? boilerplate.newLocal(v8::Isolate::GetCurrent())->Clone() : createWrapperFromCacheSlowCase(type);
133 v8::Local<v8::Function> constructorForType(const WrapperTypeInfo* type)
135 UnsafePersistent<v8::Function> function = m_constructorMap.get(type);
136 if (!function.isEmpty())
137 return function.newLocal(v8::Isolate::GetCurrent());
138 return constructorForTypeSlowCase(type);
141 v8::Local<v8::Object> prototypeForType(const WrapperTypeInfo*);
143 V8NPObjectMap* v8NPObjectMap()
145 return &m_v8NPObjectMap;
148 V8DOMActivityLogger* activityLogger()
150 return m_activityLogger;
153 void setActivityLogger(V8DOMActivityLogger* logger)
155 m_activityLogger = logger;
158 void addCustomElementBinding(CustomElementDefinition*, PassOwnPtr<CustomElementBinding>);
159 void clearCustomElementBinding(CustomElementDefinition*);
160 CustomElementBinding* customElementBinding(CustomElementDefinition*);
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()))
173 v8::Local<v8::Object> createWrapperFromCacheSlowCase(const WrapperTypeInfo*);
174 v8::Local<v8::Function> constructorForTypeSlowCase(const WrapperTypeInfo*);
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;
181 typedef WTF::HashMap<const WrapperTypeInfo*, UnsafePersistent<v8::Function> > ConstructorMap;
182 ConstructorMap m_constructorMap;
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;
193 typedef WTF::HashMap<CustomElementDefinition*, OwnPtr<CustomElementBinding> > CustomElementBindingMap;
194 OwnPtr<CustomElementBindingMap> m_customElementBindings;
197 class V8PerContextDebugData {
199 static bool setContextDebugData(v8::Handle<v8::Context>, const char* worldName, int debugId);
200 static int contextDebugId(v8::Handle<v8::Context>);
203 } // namespace WebCore
205 #endif // V8PerContextData_h