1 /*
2 * Copyright (C) 2006, 2007, 2008, 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 "bindings/core/v8/V8AbstractEventListener.h"
33
34 #include "bindings/core/v8/V8Binding.h"
35 #include "bindings/core/v8/V8Event.h"
36 #include "bindings/core/v8/V8EventListenerList.h"
37 #include "bindings/core/v8/V8EventTarget.h"
38 #include "bindings/core/v8/V8HiddenValue.h"
39 #include "core/events/BeforeUnloadEvent.h"
40 #include "core/events/Event.h"
41 #include "core/inspector/InspectorCounters.h"
42 #include "core/workers/WorkerGlobalScope.h"
43
44 namespace blink {
45
V8AbstractEventListener(bool isAttribute,ScriptState * scriptState)46 V8AbstractEventListener::V8AbstractEventListener(bool isAttribute, ScriptState* scriptState)
47 : EventListener(JSEventListenerType)
48 , m_isAttribute(isAttribute)
49 , m_scriptState(scriptState)
50 , m_isolate(scriptState->isolate())
51 {
52 if (isMainThread())
53 InspectorCounters::incrementCounter(InspectorCounters::JSEventListenerCounter);
54 }
55
V8AbstractEventListener(bool isAttribute,v8::Isolate * isolate)56 V8AbstractEventListener::V8AbstractEventListener(bool isAttribute, v8::Isolate* isolate)
57 : EventListener(JSEventListenerType)
58 , m_isAttribute(isAttribute)
59 , m_scriptState(nullptr)
60 , m_isolate(isolate)
61 {
62 if (isMainThread())
63 InspectorCounters::incrementCounter(InspectorCounters::JSEventListenerCounter);
64 }
65
~V8AbstractEventListener()66 V8AbstractEventListener::~V8AbstractEventListener()
67 {
68 if (!m_listener.isEmpty()) {
69 v8::HandleScope scope(m_isolate);
70 V8EventListenerList::clearWrapper(m_listener.newLocal(isolate()), m_isAttribute, isolate());
71 }
72 if (isMainThread())
73 InspectorCounters::decrementCounter(InspectorCounters::JSEventListenerCounter);
74 }
75
handleEvent(ExecutionContext *,Event * event)76 void V8AbstractEventListener::handleEvent(ExecutionContext*, Event* event)
77 {
78 if (scriptState()->contextIsValid())
79 return;
80 if (!scriptState()->executionContext())
81 return;
82 // Don't reenter V8 if execution was terminated in this instance of V8.
83 if (scriptState()->executionContext()->isJSExecutionForbidden())
84 return;
85
86 ASSERT(event);
87
88 // The callback function on XMLHttpRequest can clear the event listener and destroys 'this' object. Keep a local reference to it.
89 // See issue 889829.
90 RefPtr<V8AbstractEventListener> protect(this);
91
92 ScriptState::Scope scope(scriptState());
93
94 // Get the V8 wrapper for the event object.
95 v8::Handle<v8::Value> jsEvent = toV8(event, scriptState()->context()->Global(), isolate());
96 if (jsEvent.IsEmpty())
97 return;
98 invokeEventHandler(event, v8::Local<v8::Value>::New(isolate(), jsEvent));
99 }
100
setListenerObject(v8::Handle<v8::Object> listener)101 void V8AbstractEventListener::setListenerObject(v8::Handle<v8::Object> listener)
102 {
103 m_listener.set(isolate(), listener);
104 m_listener.setWeak(this, &setWeakCallback);
105 }
106
invokeEventHandler(Event * event,v8::Local<v8::Value> jsEvent)107 void V8AbstractEventListener::invokeEventHandler(Event* event, v8::Local<v8::Value> jsEvent)
108 {
109 // If jsEvent is empty, attempt to set it as a hidden value would crash v8.
110 if (jsEvent.IsEmpty())
111 return;
112
113 ASSERT(!scriptState()->contextIsValid());
114 v8::Local<v8::Value> returnValue;
115 {
116 // Catch exceptions thrown in the event handler so they do not propagate to javascript code that caused the event to fire.
117 v8::TryCatch tryCatch;
118 tryCatch.SetVerbose(true);
119
120 // Save the old 'event' property so we can restore it later.
121 v8::Local<v8::Value> savedEvent = V8HiddenValue::getHiddenValue(isolate(), scriptState()->context()->Global(), V8HiddenValue::event(isolate()));
122 tryCatch.Reset();
123
124 // Make the event available in the global object, so LocalDOMWindow can expose it.
125 V8HiddenValue::setHiddenValue(isolate(), scriptState()->context()->Global(), V8HiddenValue::event(isolate()), jsEvent);
126 tryCatch.Reset();
127
128 returnValue = callListenerFunction(jsEvent, event);
129 if (tryCatch.HasCaught())
130 event->target()->uncaughtExceptionInEventHandler();
131
132 if (!tryCatch.CanContinue()) { // Result of TerminateExecution().
133 if (scriptState()->executionContext()->isWorkerGlobalScope())
134 toWorkerGlobalScope(scriptState()->executionContext())->script()->forbidExecution();
135 return;
136 }
137 tryCatch.Reset();
138
139 // Restore the old event. This must be done for all exit paths through this method.
140 if (savedEvent.IsEmpty())
141 V8HiddenValue::setHiddenValue(isolate(), scriptState()->context()->Global(), V8HiddenValue::event(isolate()), v8::Undefined(isolate()));
142 else
143 V8HiddenValue::setHiddenValue(isolate(), scriptState()->context()->Global(), V8HiddenValue::event(isolate()), savedEvent);
144 tryCatch.Reset();
145 }
146
147 if (returnValue.IsEmpty())
148 return;
149
150 if (m_isAttribute && !returnValue->IsNull() && !returnValue->IsUndefined() && event->isBeforeUnloadEvent()) {
151 TOSTRING_VOID(V8StringResource<>, stringReturnValue, returnValue);
152 toBeforeUnloadEvent(event)->setReturnValue(stringReturnValue);
153 }
154
155 if (m_isAttribute && shouldPreventDefault(returnValue))
156 event->preventDefault();
157 }
158
shouldPreventDefault(v8::Local<v8::Value> returnValue)159 bool V8AbstractEventListener::shouldPreventDefault(v8::Local<v8::Value> returnValue)
160 {
161 // Prevent default action if the return value is false in accord with the spec
162 // http://www.w3.org/TR/html5/webappapis.html#event-handler-attributes
163 return returnValue->IsBoolean() && !returnValue->BooleanValue();
164 }
165
getReceiverObject(Event * event)166 v8::Local<v8::Object> V8AbstractEventListener::getReceiverObject(Event* event)
167 {
168 v8::Local<v8::Object> listener = m_listener.newLocal(isolate());
169 if (!m_listener.isEmpty() && !listener->IsFunction())
170 return listener;
171
172 EventTarget* target = event->currentTarget();
173 v8::Handle<v8::Value> value = toV8(target, scriptState()->context()->Global(), isolate());
174 if (value.IsEmpty())
175 return v8::Local<v8::Object>();
176 return v8::Local<v8::Object>::New(isolate(), v8::Handle<v8::Object>::Cast(value));
177 }
178
belongsToTheCurrentWorld() const179 bool V8AbstractEventListener::belongsToTheCurrentWorld() const
180 {
181 return isolate()->InContext() && &world() == &DOMWrapperWorld::current(isolate());
182 }
183
setWeakCallback(const v8::WeakCallbackData<v8::Object,V8AbstractEventListener> & data)184 void V8AbstractEventListener::setWeakCallback(const v8::WeakCallbackData<v8::Object, V8AbstractEventListener> &data)
185 {
186 data.GetParameter()->m_listener.clear();
187 }
188
189 } // namespace blink
190