Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / bindings / v8 / V8PerContextData.h
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:
65     static void install(v8::Handle<v8::Context> context, DOMWrapperWorld* world)
66     {
67         new V8PerContextDataHolder(context, world);
68     }
69
70     static V8PerContextDataHolder* from(v8::Handle<v8::Context> context)
71     {
72         return static_cast<V8PerContextDataHolder*>(context->GetAlignedPointerFromEmbedderData(v8ContextPerContextDataIndex));
73     }
74
75     V8PerContextData* perContextData() const { return m_perContextData; }
76     void setPerContextData(V8PerContextData* data) { m_perContextData = data; }
77
78     DOMWrapperWorld* world() const { return m_world; }
79
80 private:
81     V8PerContextDataHolder(v8::Handle<v8::Context> context, DOMWrapperWorld* world)
82         : m_context(v8::Isolate::GetCurrent(), context)
83         , m_perContextData(0)
84         , m_world(world)
85     {
86         m_context.setWeak(this, &V8PerContextDataHolder::weakCallback);
87         context->SetAlignedPointerInEmbedderData(v8ContextPerContextDataIndex, this);
88     }
89
90     ~V8PerContextDataHolder() {}
91
92     static void weakCallback(const v8::WeakCallbackData<v8::Context, V8PerContextDataHolder>& data)
93     {
94         data.GetValue()->SetAlignedPointerInEmbedderData(v8ContextPerContextDataIndex, 0);
95         delete data.GetParameter();
96     }
97
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;
104 };
105
106 class V8PerContextData {
107 public:
108     static PassOwnPtr<V8PerContextData> create(v8::Handle<v8::Context> context)
109     {
110         return adoptPtr(new V8PerContextData(context));
111     }
112
113     ~V8PerContextData();
114
115     bool init();
116
117     static V8PerContextData* from(v8::Handle<v8::Context> context)
118     {
119         return V8PerContextDataHolder::from(context)->perContextData();
120     }
121
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)
126     {
127         UnsafePersistent<v8::Object> boilerplate = m_wrapperBoilerplates.get(type);
128         return !boilerplate.isEmpty() ? boilerplate.newLocal(v8::Isolate::GetCurrent())->Clone() : createWrapperFromCacheSlowCase(type);
129     }
130
131     v8::Local<v8::Function> constructorForType(const WrapperTypeInfo* type)
132     {
133         UnsafePersistent<v8::Function> function = m_constructorMap.get(type);
134         if (!function.isEmpty())
135             return function.newLocal(v8::Isolate::GetCurrent());
136         return constructorForTypeSlowCase(type);
137     }
138
139     v8::Local<v8::Object> prototypeForType(const WrapperTypeInfo*);
140
141     V8NPObjectMap* v8NPObjectMap()
142     {
143         return &m_v8NPObjectMap;
144     }
145
146     V8DOMActivityLogger* activityLogger()
147     {
148         return m_activityLogger;
149     }
150
151     void setActivityLogger(V8DOMActivityLogger* logger)
152     {
153         m_activityLogger = logger;
154     }
155
156     void addCustomElementBinding(CustomElementDefinition*, PassOwnPtr<CustomElementBinding>);
157     void clearCustomElementBinding(CustomElementDefinition*);
158     CustomElementBinding* customElementBinding(CustomElementDefinition*);
159
160 private:
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()))
166     {
167     }
168
169     v8::Local<v8::Object> createWrapperFromCacheSlowCase(const WrapperTypeInfo*);
170     v8::Local<v8::Function> constructorForTypeSlowCase(const WrapperTypeInfo*);
171
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;
176
177     typedef WTF::HashMap<const WrapperTypeInfo*, UnsafePersistent<v8::Function> > ConstructorMap;
178     ConstructorMap m_constructorMap;
179
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;
188
189     typedef WTF::HashMap<CustomElementDefinition*, OwnPtr<CustomElementBinding> > CustomElementBindingMap;
190     OwnPtr<CustomElementBindingMap> m_customElementBindings;
191 };
192
193 class V8PerContextDebugData {
194 public:
195     static bool setContextDebugData(v8::Handle<v8::Context>, const char* worldName, int debugId);
196     static int contextDebugId(v8::Handle<v8::Context>);
197 };
198
199 } // namespace WebCore
200
201 #endif // V8PerContextData_h