1 /*
2 * Copyright (C) 2007-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 #include "config.h"
32 #include "Node.h"
33
34 #include "Document.h"
35 #include "EventListener.h"
36
37 #include "V8AbstractEventListener.h"
38 #include "V8Binding.h"
39 #include "V8CustomBinding.h"
40 #include "V8CustomEventListener.h"
41 #include "V8Node.h"
42 #include "V8ObjectEventListener.h"
43 #include "V8Proxy.h"
44
45 #include <wtf/RefPtr.h>
46
47 namespace WebCore {
48
toEventType(v8::Local<v8::String> value)49 static inline String toEventType(v8::Local<v8::String> value)
50 {
51 String key = toWebCoreString(value);
52 ASSERT(key.startsWith("on"));
53 return key.substring(2);
54 }
55
getEventListener(Node * node,v8::Local<v8::Value> value,bool isAttribute,bool findOnly)56 static PassRefPtr<EventListener> getEventListener(Node* node, v8::Local<v8::Value> value, bool isAttribute, bool findOnly)
57 {
58 V8Proxy* proxy = V8Proxy::retrieve(node->scriptExecutionContext());
59 // The document might be created using createDocument, which does
60 // not have a frame, use the active frame.
61 if (!proxy)
62 proxy = V8Proxy::retrieve(V8Proxy::retrieveFrameForEnteredContext());
63
64 if (proxy) {
65 V8EventListenerList* list = proxy->objectListeners();
66 return findOnly ? list->findWrapper(value, isAttribute) : list->findOrCreateWrapper<V8ObjectEventListener>(proxy->frame(), value, isAttribute);
67 }
68
69 return 0;
70 }
71
ACCESSOR_SETTER(NodeEventHandler)72 ACCESSOR_SETTER(NodeEventHandler)
73 {
74 Node* node = V8DOMWrapper::convertDOMWrapperToNode<Node>(info.Holder());
75 String eventType = toEventType(name);
76
77 // Remove hidden dependency on the old event handler.
78 if (EventListener* listener = node->getAttributeEventListener(eventType)) {
79 if (static_cast<V8AbstractEventListener*>(listener)->isObjectListener()) {
80 v8::Local<v8::Object> v8Listener = static_cast<V8ObjectEventListener*>(listener)->getListenerObject();
81 removeHiddenDependency(info.Holder(), v8Listener, V8Custom::kNodeEventListenerCacheIndex);
82 }
83 }
84
85 // Set handler if the value is a function.
86 if (value->IsFunction()) {
87 RefPtr<EventListener> listener = getEventListener(node, value, true, false);
88 if (listener) {
89 node->setAttributeEventListener(eventType, listener);
90 createHiddenDependency(info.Holder(), value, V8Custom::kNodeEventListenerCacheIndex);
91 }
92 } else {
93 // Otherwise, clear the handler.
94 node->clearAttributeEventListener(eventType);
95 }
96 }
97
ACCESSOR_GETTER(NodeEventHandler)98 ACCESSOR_GETTER(NodeEventHandler)
99 {
100 Node* node = V8DOMWrapper::convertDOMWrapperToNode<Node>(info.Holder());
101
102 EventListener* listener = node->getAttributeEventListener(toEventType(name));
103 return V8DOMWrapper::convertEventListenerToV8Object(listener);
104 }
105
CALLBACK_FUNC_DECL(NodeAddEventListener)106 CALLBACK_FUNC_DECL(NodeAddEventListener)
107 {
108 INC_STATS("DOM.Node.addEventListener()");
109 Node* node = V8DOMWrapper::convertDOMWrapperToNode<Node>(args.Holder());
110
111 RefPtr<EventListener> listener = getEventListener(node, args[1], false, false);
112 if (listener) {
113 String type = toWebCoreString(args[0]);
114 bool useCapture = args[2]->BooleanValue();
115 node->addEventListener(type, listener, useCapture);
116 createHiddenDependency(args.Holder(), args[1], V8Custom::kNodeEventListenerCacheIndex);
117 }
118 return v8::Undefined();
119 }
120
CALLBACK_FUNC_DECL(NodeRemoveEventListener)121 CALLBACK_FUNC_DECL(NodeRemoveEventListener)
122 {
123 INC_STATS("DOM.Node.removeEventListener()");
124 Node* node = V8DOMWrapper::convertDOMWrapperToNode<Node>(args.Holder());
125
126 // It is possbile that the owner document of the node is detached
127 // from the frame.
128 // See issue http://b/878909
129 RefPtr<EventListener> listener = getEventListener(node, args[1], false, true);
130 if (listener) {
131 String type = toWebCoreString(args[0]);
132 bool useCapture = args[2]->BooleanValue();
133 node->removeEventListener(type, listener.get(), useCapture);
134 removeHiddenDependency(args.Holder(), args[1], V8Custom::kNodeEventListenerCacheIndex);
135 }
136
137 return v8::Undefined();
138 }
139
140 // This function is customized to take advantage of the optional 4th argument: shouldLazyAttach
CALLBACK_FUNC_DECL(NodeInsertBefore)141 CALLBACK_FUNC_DECL(NodeInsertBefore)
142 {
143 INC_STATS("DOM.Node.insertBefore");
144 v8::Handle<v8::Object> holder = args.Holder();
145 Node* imp = V8DOMWrapper::convertDOMWrapperToNode<Node>(holder);
146 ExceptionCode ec = 0;
147 Node* newChild = V8Node::HasInstance(args[0]) ? V8DOMWrapper::convertDOMWrapperToNode<Node>(v8::Handle<v8::Object>::Cast(args[0])) : 0;
148 Node* refChild = V8Node::HasInstance(args[1]) ? V8DOMWrapper::convertDOMWrapperToNode<Node>(v8::Handle<v8::Object>::Cast(args[1])) : 0;
149 bool success = imp->insertBefore(newChild, refChild, ec, true);
150 if (ec) {
151 V8Proxy::setDOMException(ec);
152 return v8::Handle<v8::Value>();
153 }
154 if (success)
155 return args[0];
156 return v8::Null();
157 }
158
159 // This function is customized to take advantage of the optional 4th argument: shouldLazyAttach
CALLBACK_FUNC_DECL(NodeReplaceChild)160 CALLBACK_FUNC_DECL(NodeReplaceChild)
161 {
162 INC_STATS("DOM.Node.replaceChild");
163 v8::Handle<v8::Object> holder = args.Holder();
164 Node* imp = V8DOMWrapper::convertDOMWrapperToNode<Node>(holder);
165 ExceptionCode ec = 0;
166 Node* newChild = V8Node::HasInstance(args[0]) ? V8DOMWrapper::convertDOMWrapperToNode<Node>(v8::Handle<v8::Object>::Cast(args[0])) : 0;
167 Node* oldChild = V8Node::HasInstance(args[1]) ? V8DOMWrapper::convertDOMWrapperToNode<Node>(v8::Handle<v8::Object>::Cast(args[1])) : 0;
168 bool success = imp->replaceChild(newChild, oldChild, ec, true);
169 if (ec) {
170 V8Proxy::setDOMException(ec);
171 return v8::Handle<v8::Value>();
172 }
173 if (success)
174 return args[1];
175 return v8::Null();
176 }
177
CALLBACK_FUNC_DECL(NodeRemoveChild)178 CALLBACK_FUNC_DECL(NodeRemoveChild)
179 {
180 INC_STATS("DOM.Node.removeChild");
181 v8::Handle<v8::Object> holder = args.Holder();
182 Node* imp = V8DOMWrapper::convertDOMWrapperToNode<Node>(holder);
183 ExceptionCode ec = 0;
184 Node* oldChild = V8Node::HasInstance(args[0]) ? V8DOMWrapper::convertDOMWrapperToNode<Node>(v8::Handle<v8::Object>::Cast(args[0])) : 0;
185 bool success = imp->removeChild(oldChild, ec);
186 if (ec) {
187 V8Proxy::setDOMException(ec);
188 return v8::Handle<v8::Value>();
189 }
190 if (success)
191 return args[0];
192 return v8::Null();
193 }
194
195 // This function is customized to take advantage of the optional 4th argument: shouldLazyAttach
CALLBACK_FUNC_DECL(NodeAppendChild)196 CALLBACK_FUNC_DECL(NodeAppendChild)
197 {
198 INC_STATS("DOM.Node.appendChild");
199 v8::Handle<v8::Object> holder = args.Holder();
200 Node* imp = V8DOMWrapper::convertDOMWrapperToNode<Node>(holder);
201 ExceptionCode ec = 0;
202 Node* newChild = V8Node::HasInstance(args[0]) ? V8DOMWrapper::convertDOMWrapperToNode<Node>(v8::Handle<v8::Object>::Cast(args[0])) : 0;
203 bool success = imp->appendChild(newChild, ec, true );
204 if (ec) {
205 V8Proxy::setDOMException(ec);
206 return v8::Handle<v8::Value>();
207 }
208 if (success)
209 return args[0];
210 return v8::Null();
211 }
212
213 } // namespace WebCore
214