Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / bindings / core / v8 / DOMDataStore.h
1 /*
2  * Copyright (C) 2009 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 DOMDataStore_h
32 #define DOMDataStore_h
33
34 #include "bindings/core/v8/DOMWrapperMap.h"
35 #include "bindings/core/v8/DOMWrapperWorld.h"
36 #include "bindings/core/v8/ScriptWrappable.h"
37 #include "bindings/core/v8/WrapperTypeInfo.h"
38 #include "wtf/Noncopyable.h"
39 #include "wtf/StdLibExtras.h"
40 #include <v8.h>
41
42 namespace blink {
43
44 class Node;
45
46 class DOMDataStore {
47     WTF_MAKE_NONCOPYABLE(DOMDataStore);
48 public:
49     explicit DOMDataStore(bool isMainWorld)
50         : m_isMainWorld(isMainWorld)
51         , m_wrapperMap(v8::Isolate::GetCurrent()) { }
52     ~DOMDataStore()
53     {
54         // We never actually destruct the main world's DOMDataStore.
55         ASSERT(!m_isMainWorld);
56         m_wrapperMap.clear();
57     }
58
59     static DOMDataStore& current(v8::Isolate* isolate)
60     {
61         return DOMWrapperWorld::current(isolate).domDataStore();
62     }
63
64     static bool setReturnValueFast(v8::ReturnValue<v8::Value> returnValue, ScriptWrappable* object, v8::Local<v8::Object> holder, const ScriptWrappable* wrappable)
65     {
66         // The second fastest way to check if we're in the main world is to check if
67         // the wrappable's wrapper is the same as the holder.
68         // FIXME: Investigate if it's worth having this check for performance.
69         if (holderContainsWrapper(holder, wrappable))
70             return object->setReturnValue(returnValue);
71         return current(returnValue.GetIsolate()).setReturnValueFrom(returnValue, object);
72     }
73
74     static bool setReturnValueFast(v8::ReturnValue<v8::Value> returnValue, Node* node, v8::Local<v8::Object> holder, const ScriptWrappable* wrappable)
75     {
76         if (canUseScriptWrappable(node)
77             // The second fastest way to check if we're in the main world is to
78             // check if the wrappable's wrapper is the same as the holder.
79             // FIXME: Investigate if it's worth having this check for performance.
80             || holderContainsWrapper(holder, wrappable))
81             return ScriptWrappable::fromNode(node)->setReturnValue(returnValue);
82         return current(returnValue.GetIsolate()).setReturnValueFrom(returnValue, ScriptWrappable::fromNode(node));
83     }
84
85     static bool setReturnValue(v8::ReturnValue<v8::Value> returnValue, ScriptWrappableBase* object)
86     {
87         return current(returnValue.GetIsolate()).setReturnValueFrom(returnValue, object);
88     }
89
90     static bool setReturnValue(v8::ReturnValue<v8::Value> returnValue, ScriptWrappable* object)
91     {
92         return current(returnValue.GetIsolate()).setReturnValueFrom(returnValue, object);
93     }
94
95     static bool setReturnValue(v8::ReturnValue<v8::Value> returnValue, Node* object)
96     {
97         if (canUseScriptWrappable(object))
98             return ScriptWrappable::fromNode(object)->setReturnValue(returnValue);
99         return current(returnValue.GetIsolate()).setReturnValueFrom(returnValue, ScriptWrappable::fromNode(object));
100     }
101
102     static bool setReturnValueForMainWorld(v8::ReturnValue<v8::Value> returnValue, ScriptWrappable* object)
103     {
104         return object->setReturnValue(returnValue);
105     }
106
107     static v8::Handle<v8::Object> getWrapper(ScriptWrappableBase* object, v8::Isolate* isolate)
108     {
109         return current(isolate).get(object, isolate);
110     }
111
112     static v8::Handle<v8::Object> getWrapper(ScriptWrappable* object, v8::Isolate* isolate)
113     {
114         return current(isolate).get(object, isolate);
115     }
116
117     static v8::Handle<v8::Object> getWrapper(Node* node, v8::Isolate* isolate)
118     {
119         if (canUseScriptWrappable(node)) {
120             v8::Handle<v8::Object> result = ScriptWrappable::fromNode(node)->newLocalWrapper(isolate);
121             // Security: always guard against malicious tampering.
122             ScriptWrappable::fromNode(node)->assertWrapperSanity(result);
123             return result;
124         }
125         return current(isolate).get(ScriptWrappable::fromNode(node), isolate);
126     }
127
128     static void setWrapperReference(const v8::Persistent<v8::Object>& parent, ScriptWrappableBase* child, v8::Isolate* isolate)
129     {
130         current(isolate).setReference(parent, child, isolate);
131     }
132
133     static void setWrapperReference(const v8::Persistent<v8::Object>& parent, ScriptWrappable* child, v8::Isolate* isolate)
134     {
135         current(isolate).setReference(parent, child, isolate);
136     }
137
138     static void setWrapperReference(const v8::Persistent<v8::Object>& parent, Node* child, v8::Isolate* isolate)
139     {
140         if (canUseScriptWrappable(child)) {
141             ScriptWrappable::fromNode(child)->setReference(parent, isolate);
142             return;
143         }
144         current(isolate).setReference(parent, ScriptWrappable::fromNode(child), isolate);
145     }
146
147     static void setWrapper(ScriptWrappableBase* object, v8::Handle<v8::Object> wrapper, v8::Isolate* isolate, const WrapperTypeInfo* wrapperTypeInfo)
148     {
149         return current(isolate).set(object, wrapper, isolate, wrapperTypeInfo);
150     }
151
152     static void setWrapper(ScriptWrappable* object, v8::Handle<v8::Object> wrapper, v8::Isolate* isolate, const WrapperTypeInfo* wrapperTypeInfo)
153     {
154         return current(isolate).set(object, wrapper, isolate, wrapperTypeInfo);
155     }
156
157     static void setWrapper(Node* node, v8::Handle<v8::Object> wrapper, v8::Isolate* isolate, const WrapperTypeInfo* wrapperTypeInfo)
158     {
159         if (canUseScriptWrappable(node)) {
160             ScriptWrappable::fromNode(node)->setWrapper(wrapper, isolate, wrapperTypeInfo);
161             return;
162         }
163         return current(isolate).set(ScriptWrappable::fromNode(node), wrapper, isolate, wrapperTypeInfo);
164     }
165
166     static bool containsWrapper(ScriptWrappableBase* object, v8::Isolate* isolate)
167     {
168         return current(isolate).containsWrapper(object);
169     }
170
171     static bool containsWrapper(ScriptWrappable* object, v8::Isolate* isolate)
172     {
173         return current(isolate).containsWrapper(object);
174     }
175
176     v8::Handle<v8::Object> get(ScriptWrappableBase* object, v8::Isolate* isolate)
177     {
178         return m_wrapperMap.newLocal(object->toScriptWrappableBase(), isolate);
179     }
180
181     v8::Handle<v8::Object> get(ScriptWrappable* object, v8::Isolate* isolate)
182     {
183         if (m_isMainWorld)
184             return object->newLocalWrapper(isolate);
185         return m_wrapperMap.newLocal(object->toScriptWrappableBase(), isolate);
186     }
187
188     void setReference(const v8::Persistent<v8::Object>& parent, ScriptWrappableBase* child, v8::Isolate* isolate)
189     {
190         m_wrapperMap.setReference(parent, child, isolate);
191     }
192
193     void setReference(const v8::Persistent<v8::Object>& parent, ScriptWrappable* child, v8::Isolate* isolate)
194     {
195         if (m_isMainWorld) {
196             child->setReference(parent, isolate);
197             return;
198         }
199         m_wrapperMap.setReference(parent, child->toScriptWrappableBase(), isolate);
200     }
201
202     bool setReturnValueFrom(v8::ReturnValue<v8::Value> returnValue, ScriptWrappableBase* object)
203     {
204         return m_wrapperMap.setReturnValueFrom(returnValue, object);
205     }
206
207     bool setReturnValueFrom(v8::ReturnValue<v8::Value> returnValue, ScriptWrappable* object)
208     {
209         if (m_isMainWorld)
210             return object->setReturnValue(returnValue);
211         return m_wrapperMap.setReturnValueFrom(returnValue, object->toScriptWrappableBase());
212     }
213
214     bool containsWrapper(ScriptWrappableBase* object)
215     {
216         return m_wrapperMap.containsKey(object->toScriptWrappableBase());
217     }
218
219     bool containsWrapper(ScriptWrappable* object)
220     {
221         if (m_isMainWorld)
222             return object->containsWrapper();
223         return m_wrapperMap.containsKey(object->toScriptWrappableBase());
224     }
225
226 private:
227     void set(ScriptWrappableBase* object, v8::Handle<v8::Object> wrapper, v8::Isolate* isolate, const WrapperTypeInfo* wrapperTypeInfo)
228     {
229         ASSERT(object);
230         ASSERT(!wrapper.IsEmpty());
231         m_wrapperMap.set(object->toScriptWrappableBase(), wrapper, wrapperTypeInfo);
232     }
233
234     void set(ScriptWrappable* object, v8::Handle<v8::Object> wrapper, v8::Isolate* isolate, const WrapperTypeInfo* wrapperTypeInfo)
235     {
236         ASSERT(object);
237         ASSERT(!wrapper.IsEmpty());
238         if (m_isMainWorld) {
239             object->setWrapper(wrapper, isolate, wrapperTypeInfo);
240             return;
241         }
242         m_wrapperMap.set(object->toScriptWrappableBase(), wrapper, wrapperTypeInfo);
243     }
244
245     // We can use a wrapper stored in a ScriptWrappable when we're in the main world.
246     // This method does the fast check if we're in the main world. If this method returns true,
247     // it is guaranteed that we're in the main world. On the other hand, if this method returns
248     // false, nothing is guaranteed (we might be in the main world).
249     static bool canUseScriptWrappable(Node*)
250     {
251         // This helper function itself doesn't use the argument, but we have to
252         // make sure that the argument is type of Node* because Node and its
253         // subclasses satisfy the following two conditions.
254         //   1. Nodes never exist in worker.
255         //   2. Node inherits from ScriptWrappable.
256         // and if there exists no isolated world, we're sure that we're in the
257         // main world and we can use ScriptWrappable's wrapper.
258         return !DOMWrapperWorld::isolatedWorldsExist();
259     }
260
261     static bool holderContainsWrapper(v8::Local<v8::Object> holder, const ScriptWrappable* wrappable)
262     {
263         // Verify our assumptions about the main world.
264         ASSERT(wrappable);
265         ASSERT(!wrappable->containsWrapper() || !wrappable->isEqualTo(holder) || current(v8::Isolate::GetCurrent()).m_isMainWorld);
266         return wrappable->isEqualTo(holder);
267     }
268
269     bool m_isMainWorld;
270     DOMWrapperMap<ScriptWrappableBase> m_wrapperMap;
271 };
272
273 template<>
274 inline void DOMWrapperMap<ScriptWrappableBase>::PersistentValueMapTraits::Dispose(
275     v8::Isolate* isolate,
276     v8::UniquePersistent<v8::Object> value,
277     ScriptWrappableBase* key)
278 {
279     RELEASE_ASSERT(!value.IsEmpty()); // See crbug.com/368095.
280     releaseObject(v8::Local<v8::Object>::New(isolate, value));
281 }
282
283 } // namespace blink
284
285 #endif // DOMDataStore_h