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, DOMWrapperWorld* world)
67 new V8PerContextDataHolder(context, world);
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* world() const { return m_world; }
81 V8PerContextDataHolder(v8::Handle<v8::Context> context, DOMWrapperWorld* world)
82 : m_context(v8::Isolate::GetCurrent(), context)
86 m_context.setWeak(this, &V8PerContextDataHolder::weakCallback);
87 context->SetAlignedPointerInEmbedderData(v8ContextPerContextDataIndex, this);
90 ~V8PerContextDataHolder() {}
92 static void weakCallback(const v8::WeakCallbackData<v8::Context, V8PerContextDataHolder>& data)
94 data.GetValue()->SetAlignedPointerInEmbedderData(v8ContextPerContextDataIndex, 0);
95 delete data.GetParameter();
98 ScopedPersistent<v8::Context> m_context;
99 V8PerContextData* m_perContextData;
100 // This should not be a RefPtr. Otherwise, it creates a cycle:
101 // V8PerContextData => DOMWrapperWorld => DOMDataStore => global objects
102 // => Window or WorkerGlobalScope => V8PerContextData.
103 DOMWrapperWorld* m_world;
106 class V8PerContextData {
108 static PassOwnPtr<V8PerContextData> create(v8::Handle<v8::Context> context)
110 return adoptPtr(new V8PerContextData(context));
117 static V8PerContextData* from(v8::Handle<v8::Context> context)
119 return V8PerContextDataHolder::from(context)->perContextData();
122 // To create JS Wrapper objects, we create a cache of a 'boiler plate'
123 // object, and then simply Clone that object each time we need a new one.
124 // This is faster than going through the full object creation process.
125 v8::Local<v8::Object> createWrapperFromCache(const WrapperTypeInfo* type)
127 UnsafePersistent<v8::Object> boilerplate = m_wrapperBoilerplates.get(type);
128 return !boilerplate.isEmpty() ? boilerplate.newLocal(v8::Isolate::GetCurrent())->Clone() : createWrapperFromCacheSlowCase(type);
131 v8::Local<v8::Function> constructorForType(const WrapperTypeInfo* type)
133 UnsafePersistent<v8::Function> function = m_constructorMap.get(type);
134 if (!function.isEmpty())
135 return function.newLocal(v8::Isolate::GetCurrent());
136 return constructorForTypeSlowCase(type);
139 v8::Local<v8::Object> prototypeForType(const WrapperTypeInfo*);
141 V8NPObjectMap* v8NPObjectMap()
143 return &m_v8NPObjectMap;
146 V8DOMActivityLogger* activityLogger()
148 return m_activityLogger;
151 void setActivityLogger(V8DOMActivityLogger* logger)
153 m_activityLogger = logger;
156 void addCustomElementBinding(CustomElementDefinition*, PassOwnPtr<CustomElementBinding>);
157 void clearCustomElementBinding(CustomElementDefinition*);
158 CustomElementBinding* customElementBinding(CustomElementDefinition*);
161 explicit V8PerContextData(v8::Handle<v8::Context> context)
162 : m_activityLogger(0)
163 , m_isolate(v8::Isolate::GetCurrent())
164 , m_context(m_isolate, context)
165 , m_customElementBindings(adoptPtr(new CustomElementBindingMap()))
169 v8::Local<v8::Object> createWrapperFromCacheSlowCase(const WrapperTypeInfo*);
170 v8::Local<v8::Function> constructorForTypeSlowCase(const WrapperTypeInfo*);
172 // For each possible type of wrapper, we keep a boilerplate object.
173 // The boilerplate is used to create additional wrappers of the same type.
174 typedef WTF::HashMap<const WrapperTypeInfo*, UnsafePersistent<v8::Object> > WrapperBoilerplateMap;
175 WrapperBoilerplateMap m_wrapperBoilerplates;
177 typedef WTF::HashMap<const WrapperTypeInfo*, UnsafePersistent<v8::Function> > ConstructorMap;
178 ConstructorMap m_constructorMap;
180 V8NPObjectMap m_v8NPObjectMap;
181 // We cache a pointer to the V8DOMActivityLogger associated with the world
182 // corresponding to this context. The ownership of the pointer is retained
183 // by the DOMActivityLoggerMap in DOMWrapperWorld.
184 V8DOMActivityLogger* m_activityLogger;
185 v8::Isolate* m_isolate;
186 ScopedPersistent<v8::Context> m_context;
187 ScopedPersistent<v8::Value> m_errorPrototype;
189 typedef WTF::HashMap<CustomElementDefinition*, OwnPtr<CustomElementBinding> > CustomElementBindingMap;
190 OwnPtr<CustomElementBindingMap> m_customElementBindings;
193 class V8PerContextDebugData {
195 static bool setContextDebugData(v8::Handle<v8::Context>, const char* worldName, int debugId);
196 static int contextDebugId(v8::Handle<v8::Context>);
199 } // namespace WebCore
201 #endif // V8PerContextData_h