• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "Event.h"
35 #include "IsolatedWorld.h"
36 #include "Node.h"
37 #include "NodeFilter.h"
38 #include "PlatformString.h"
39 #include "V8CustomXPathNSResolver.h"
40 #include "V8Event.h"
41 #include "V8Utilities.h"
42 #include "V8XPathNSResolver.h"
43 #include "WrapperTypeInfo.h"
44 #include "XPathNSResolver.h"
45 #include <v8.h>
46 
47 namespace WebCore {
48 
49     class DOMWindow;
50     class EventTarget;
51     class Frame;
52     class Node;
53     class V8Proxy;
54     class WorkerContext;
55 
56     enum ListenerLookupType {
57         ListenerFindOnly,
58         ListenerFindOrCreate,
59     };
60 
61     class V8DOMWrapper {
62     public:
63 #ifndef NDEBUG
64         // Checks if a v8 value can be a DOM wrapper
65         static bool maybeDOMWrapper(v8::Handle<v8::Value>);
66 #endif
67 
68         // Sets contents of a DOM wrapper.
setDOMWrapper(v8::Handle<v8::Object> object,WrapperTypeInfo * type,void * cptr)69         static void setDOMWrapper(v8::Handle<v8::Object> object, WrapperTypeInfo* type, void* cptr)
70         {
71             ASSERT(object->InternalFieldCount() >= 2);
72             object->SetPointerInInternalField(v8DOMWrapperObjectIndex, cptr);
73             object->SetPointerInInternalField(v8DOMWrapperTypeIndex, type);
74         }
75 
lookupDOMWrapper(v8::Handle<v8::FunctionTemplate> functionTemplate,v8::Handle<v8::Object> object)76         static v8::Handle<v8::Object> lookupDOMWrapper(v8::Handle<v8::FunctionTemplate> functionTemplate, v8::Handle<v8::Object> object)
77         {
78             return object.IsEmpty() ? object : object->FindInstanceInPrototypeChain(functionTemplate);
79         }
80 
81         static WrapperTypeInfo* domWrapperType(v8::Handle<v8::Object>);
82 
convertEventTargetToV8Object(PassRefPtr<EventTarget> eventTarget)83         static v8::Handle<v8::Value> convertEventTargetToV8Object(PassRefPtr<EventTarget> eventTarget)
84         {
85             return convertEventTargetToV8Object(eventTarget.get());
86         }
87 
88         static v8::Handle<v8::Value> convertEventTargetToV8Object(EventTarget*);
89 
90         static PassRefPtr<EventListener> getEventListener(v8::Local<v8::Value> value, bool isAttribute, ListenerLookupType lookup);
91 
92 #if ENABLE(XPATH)
93         // XPath-related utilities
94         static RefPtr<XPathNSResolver> getXPathNSResolver(v8::Handle<v8::Value> value, V8Proxy* proxy = 0);
95 #endif
96 
97         // Wrap JS node filter in C++.
98         static PassRefPtr<NodeFilter> wrapNativeNodeFilter(v8::Handle<v8::Value>);
99 
100         static v8::Local<v8::Function> getConstructorForContext(WrapperTypeInfo*, v8::Handle<v8::Context>);
101         static v8::Local<v8::Function> getConstructor(WrapperTypeInfo*, v8::Handle<v8::Value> objectPrototype);
102         static v8::Local<v8::Function> getConstructor(WrapperTypeInfo*, DOMWindow*);
103 #if ENABLE(WORKERS)
104         static v8::Local<v8::Function> getConstructor(WrapperTypeInfo*, WorkerContext*);
105 #endif
106 
107         // Set JS wrapper of a DOM object, the caller in charge of increase ref.
108         static void setJSWrapperForDOMObject(void*, v8::Persistent<v8::Object>);
109         static void setJSWrapperForActiveDOMObject(void*, v8::Persistent<v8::Object>);
110         static void setJSWrapperForDOMNode(Node*, v8::Persistent<v8::Object>);
111 
112         static bool isValidDOMObject(v8::Handle<v8::Value>);
113 
114         // Check whether a V8 value is a wrapper of type |classType|.
115         static bool isWrapperOfType(v8::Handle<v8::Value>, WrapperTypeInfo*);
116 
117         static void setHiddenReference(v8::Handle<v8::Object> parent, v8::Handle<v8::Value> child);
118 
119         // Set hidden references in a DOMWindow object of a frame.
120         static void setHiddenWindowReference(Frame*, v8::Handle<v8::Value>);
121 
122         static v8::Local<v8::Object> instantiateV8Object(V8Proxy* proxy, WrapperTypeInfo*, void* impl);
123 
getWrapper(Node * node)124         static v8::Handle<v8::Object> getWrapper(Node* node)
125         {
126             ASSERT(WTF::isMainThread());
127             if (LIKELY(!IsolatedWorld::count())) {
128                 v8::Persistent<v8::Object>* wrapper = node->wrapper();
129                 if (wrapper)
130                     return *wrapper;
131             }
132             return getWrapperSlow(node);
133         }
134 
135     private:
136         static v8::Handle<v8::Object> getWrapperSlow(Node*);
137     };
138 
139 }
140 
141 #endif // V8DOMWrapper_h
142