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"
40 #include "wtf/HashMap.h"
41 #include "wtf/PassOwnPtr.h"
42 #include "wtf/Vector.h"
43 #include "wtf/text/AtomicString.h"
44 #include "wtf/text/AtomicStringHash.h"
48 class CustomElementDefinition;
50 typedef WTF::Vector<V8NPObject*> V8NPObjectVector;
51 typedef WTF::HashMap<int, V8NPObjectVector> V8NPObjectMap;
53 enum V8ContextEmbedderDataField {
54 v8ContextDebugIdIndex,
55 v8ContextPerContextDataIndex,
56 v8ContextIsolatedWorld,
57 // Rather than adding more embedder data fields to v8::Context,
58 // consider adding the data to V8PerContextData instead.
61 class V8PerContextData {
63 static PassOwnPtr<V8PerContextData> create(v8::Handle<v8::Context> context)
65 return adoptPtr(new V8PerContextData(context));
75 static V8PerContextData* from(v8::Handle<v8::Context> context)
77 return static_cast<V8PerContextData*>(context->GetAlignedPointerFromEmbedderData(v8ContextPerContextDataIndex));
80 // To create JS Wrapper objects, we create a cache of a 'boiler plate'
81 // object, and then simply Clone that object each time we need a new one.
82 // This is faster than going through the full object creation process.
83 v8::Local<v8::Object> createWrapperFromCache(const WrapperTypeInfo* type)
85 UnsafePersistent<v8::Object> boilerplate = m_wrapperBoilerplates.get(type);
86 return !boilerplate.isEmpty() ? boilerplate.newLocal(v8::Isolate::GetCurrent())->Clone() : createWrapperFromCacheSlowCase(type);
89 v8::Local<v8::Function> constructorForType(const WrapperTypeInfo* type)
91 UnsafePersistent<v8::Function> function = m_constructorMap.get(type);
92 if (!function.isEmpty())
93 return function.newLocal(v8::Isolate::GetCurrent());
94 return constructorForTypeSlowCase(type);
97 v8::Local<v8::Object> prototypeForType(const WrapperTypeInfo*);
99 V8NPObjectMap* v8NPObjectMap()
101 return &m_v8NPObjectMap;
104 V8DOMActivityLogger* activityLogger()
106 return m_activityLogger;
109 void setActivityLogger(V8DOMActivityLogger* logger)
111 m_activityLogger = logger;
114 void addCustomElementBinding(CustomElementDefinition*, PassOwnPtr<CustomElementBinding>);
115 void clearCustomElementBinding(CustomElementDefinition*);
116 CustomElementBinding* customElementBinding(CustomElementDefinition*);
119 explicit V8PerContextData(v8::Handle<v8::Context> context)
120 : m_activityLogger(0)
121 , m_isolate(v8::Isolate::GetCurrent())
122 , m_context(m_isolate, context)
123 , m_customElementBindings(adoptPtr(new CustomElementBindingMap()))
129 v8::Local<v8::Object> createWrapperFromCacheSlowCase(const WrapperTypeInfo*);
130 v8::Local<v8::Function> constructorForTypeSlowCase(const WrapperTypeInfo*);
132 // For each possible type of wrapper, we keep a boilerplate object.
133 // The boilerplate is used to create additional wrappers of the same type.
134 typedef WTF::HashMap<const WrapperTypeInfo*, UnsafePersistent<v8::Object> > WrapperBoilerplateMap;
135 WrapperBoilerplateMap m_wrapperBoilerplates;
137 typedef WTF::HashMap<const WrapperTypeInfo*, UnsafePersistent<v8::Function> > ConstructorMap;
138 ConstructorMap m_constructorMap;
140 V8NPObjectMap m_v8NPObjectMap;
141 // We cache a pointer to the V8DOMActivityLogger associated with the world
142 // corresponding to this context. The ownership of the pointer is retained
143 // by the DOMActivityLoggerMap in DOMWrapperWorld.
144 V8DOMActivityLogger* m_activityLogger;
145 v8::Isolate* m_isolate;
146 v8::Persistent<v8::Context> m_context;
147 ScopedPersistent<v8::Value> m_errorPrototype;
149 typedef WTF::HashMap<CustomElementDefinition*, OwnPtr<CustomElementBinding> > CustomElementBindingMap;
150 OwnPtr<CustomElementBindingMap> m_customElementBindings;
153 class V8PerContextDebugData {
155 static bool setContextDebugData(v8::Handle<v8::Context>, const char* worldName, int debugId);
156 static int contextDebugId(v8::Handle<v8::Context>);
159 } // namespace WebCore
161 #endif // V8PerContextData_h