Upstream version 5.34.98.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / bindings / v8 / V8DOMWrapper.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 V8DOMWrapper_h
32 #define V8DOMWrapper_h
33
34 #include "bindings/v8/DOMDataStore.h"
35 #include <v8.h>
36 #include "wtf/PassRefPtr.h"
37 #include "wtf/text/AtomicString.h"
38
39 namespace WebCore {
40
41 struct WrapperTypeInfo;
42
43     class V8DOMWrapper {
44     public:
45 #ifndef NDEBUG
46         // Checks if a v8 value can be a DOM wrapper
47         static bool maybeDOMWrapper(v8::Handle<v8::Value>);
48 #endif
49
50         static v8::Local<v8::Object> createWrapper(v8::Handle<v8::Object> creationContext, const WrapperTypeInfo*, void*, v8::Isolate*);
51
52         template<typename V8T, typename T>
53         static inline v8::Handle<v8::Object> associateObjectWithWrapper(PassRefPtr<T>, const WrapperTypeInfo*, v8::Handle<v8::Object>, v8::Isolate*, WrapperConfiguration::Lifetime);
54         static inline void setNativeInfo(v8::Handle<v8::Object>, const WrapperTypeInfo*, void*);
55         static inline void clearNativeInfo(v8::Handle<v8::Object>, const WrapperTypeInfo*);
56
57         static bool isDOMWrapper(v8::Handle<v8::Value>);
58         static bool isWrapperOfType(v8::Handle<v8::Value>, const WrapperTypeInfo*);
59     };
60
61     inline void V8DOMWrapper::setNativeInfo(v8::Handle<v8::Object> wrapper, const WrapperTypeInfo* type, void* object)
62     {
63         ASSERT(wrapper->InternalFieldCount() >= 2);
64         ASSERT(object);
65         ASSERT(type);
66         wrapper->SetAlignedPointerInInternalField(v8DOMWrapperObjectIndex, object);
67         wrapper->SetAlignedPointerInInternalField(v8DOMWrapperTypeIndex, const_cast<WrapperTypeInfo*>(type));
68     }
69
70     inline void V8DOMWrapper::clearNativeInfo(v8::Handle<v8::Object> wrapper, const WrapperTypeInfo* type)
71     {
72         ASSERT(wrapper->InternalFieldCount() >= 2);
73         ASSERT(type);
74         wrapper->SetAlignedPointerInInternalField(v8DOMWrapperTypeIndex, const_cast<WrapperTypeInfo*>(type));
75         wrapper->SetAlignedPointerInInternalField(v8DOMWrapperObjectIndex, 0);
76     }
77
78     template<typename V8T, typename T>
79     inline v8::Handle<v8::Object> V8DOMWrapper::associateObjectWithWrapper(PassRefPtr<T> object, const WrapperTypeInfo* type, v8::Handle<v8::Object> wrapper, v8::Isolate* isolate, WrapperConfiguration::Lifetime lifetime)
80     {
81         setNativeInfo(wrapper, type, V8T::toInternalPointer(object.get()));
82         ASSERT(maybeDOMWrapper(wrapper));
83         WrapperConfiguration configuration = buildWrapperConfiguration(object.get(), lifetime);
84         DOMDataStore::setWrapper<V8T>(object.leakRef(), wrapper, isolate, configuration);
85         return wrapper;
86     }
87
88     class V8WrapperInstantiationScope {
89     public:
90         V8WrapperInstantiationScope(v8::Handle<v8::Object> creationContext, v8::Isolate* isolate)
91             : m_didEnterContext(false)
92             , m_context(isolate->GetCurrentContext())
93         {
94             // FIXME: Remove all empty creationContexts from caller sites.
95             // If a creationContext is empty, we will end up creating a new object
96             // in the context currently entered. This is wrong.
97             if (creationContext.IsEmpty())
98                 return;
99             v8::Handle<v8::Context> contextForWrapper = creationContext->CreationContext();
100             // For performance, we enter the context only if the currently running context
101             // is different from the context that we are about to enter.
102             if (contextForWrapper == m_context)
103                 return;
104             m_context = v8::Local<v8::Context>::New(isolate, contextForWrapper);
105             m_didEnterContext = true;
106             m_context->Enter();
107         }
108
109         ~V8WrapperInstantiationScope()
110         {
111             if (!m_didEnterContext)
112                 return;
113             m_context->Exit();
114         }
115
116         v8::Handle<v8::Context> context() const { return m_context; }
117
118     private:
119         bool m_didEnterContext;
120         v8::Handle<v8::Context> m_context;
121     };
122
123 }
124
125 #endif // V8DOMWrapper_h