Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / bindings / v8 / V8AbstractEventListener.cpp
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/v8/V8AbstractEventListener.h"
33
34 #include "V8Event.h"
35 #include "V8EventTarget.h"
36 #include "bindings/v8/V8Binding.h"
37 #include "bindings/v8/V8EventListenerList.h"
38 #include "core/events/BeforeUnloadEvent.h"
39 #include "core/events/Event.h"
40 #include "core/events/ThreadLocalEventNames.h"
41 #include "core/inspector/InspectorCounters.h"
42 #include "core/workers/WorkerGlobalScope.h"
43
44 namespace WebCore {
45
46 V8AbstractEventListener::V8AbstractEventListener(bool isAttribute, PassRefPtr<DOMWrapperWorld> world, v8::Isolate* isolate)
47     : EventListener(JSEventListenerType)
48     , m_isAttribute(isAttribute)
49     , m_world(world)
50     , m_isolate(isolate)
51 {
52     if (isMainThread())
53         InspectorCounters::incrementCounter(InspectorCounters::JSEventListenerCounter);
54 }
55
56 V8AbstractEventListener::~V8AbstractEventListener()
57 {
58     if (!m_listener.isEmpty()) {
59         v8::HandleScope scope(m_isolate);
60         V8EventListenerList::clearWrapper(m_listener.newLocal(m_isolate), m_isAttribute, m_isolate);
61     }
62     if (isMainThread())
63         InspectorCounters::decrementCounter(InspectorCounters::JSEventListenerCounter);
64 }
65
66 void V8AbstractEventListener::handleEvent(ExecutionContext* context, Event* event)
67 {
68     // Don't reenter V8 if execution was terminated in this instance of V8.
69     if (context->isJSExecutionForbidden())
70         return;
71
72     ASSERT(event);
73
74     // The callback function on XMLHttpRequest can clear the event listener and destroys 'this' object. Keep a local reference to it.
75     // See issue 889829.
76     RefPtr<V8AbstractEventListener> protect(this);
77
78     v8::HandleScope handleScope(m_isolate);
79
80     v8::Local<v8::Context> v8Context = toV8Context(context, world());
81     if (v8Context.IsEmpty())
82         return;
83
84     // Enter the V8 context in which to perform the event handling.
85     v8::Context::Scope scope(v8Context);
86
87     // Get the V8 wrapper for the event object.
88     v8::Isolate* isolate = v8Context->GetIsolate();
89     v8::Handle<v8::Value> jsEvent = toV8(event, v8::Handle<v8::Object>(), isolate);
90     if (jsEvent.IsEmpty())
91         return;
92     invokeEventHandler(context, event, v8::Local<v8::Value>::New(isolate, jsEvent));
93 }
94
95 void V8AbstractEventListener::setListenerObject(v8::Handle<v8::Object> listener)
96 {
97     m_listener.set(m_isolate, listener);
98     m_listener.setWeak(this, &setWeakCallback);
99 }
100
101 void V8AbstractEventListener::invokeEventHandler(ExecutionContext* context, Event* event, v8::Local<v8::Value> jsEvent)
102 {
103     // If jsEvent is empty, attempt to set it as a hidden value would crash v8.
104     if (jsEvent.IsEmpty())
105         return;
106
107     v8::Local<v8::Context> v8Context = toV8Context(context, world());
108     if (v8Context.IsEmpty())
109         return;
110
111     // We push the event being processed into the global object, so that it can be exposed by DOMWindow's bindings.
112     v8::Handle<v8::String> eventSymbol = v8AtomicString(v8Context->GetIsolate(), "event");
113     v8::Local<v8::Value> returnValue;
114
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 = getHiddenValue(v8Context->GetIsolate(), v8Context->Global(), eventSymbol);
122         tryCatch.Reset();
123
124         // Make the event available in the global object, so DOMWindow can expose it.
125         setHiddenValue(v8Context->GetIsolate(), v8Context->Global(), eventSymbol, jsEvent);
126         tryCatch.Reset();
127
128         returnValue = callListenerFunction(context, jsEvent, event);
129         if (tryCatch.HasCaught())
130             event->target()->uncaughtExceptionInEventHandler();
131
132         if (!tryCatch.CanContinue()) { // Result of TerminateExecution().
133             if (context->isWorkerGlobalScope())
134                 toWorkerGlobalScope(context)->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             setHiddenValue(v8Context->GetIsolate(), v8Context->Global(), eventSymbol, v8::Undefined(v8Context->GetIsolate()));
142         else
143             setHiddenValue(v8Context->GetIsolate(), v8Context->Global(), eventSymbol, savedEvent);
144         tryCatch.Reset();
145     }
146
147     ASSERT(!handleOutOfMemory() || returnValue.IsEmpty());
148
149     if (returnValue.IsEmpty())
150         return;
151
152     if (m_isAttribute && !returnValue->IsNull() && !returnValue->IsUndefined() && event->isBeforeUnloadEvent()) {
153         V8TRYCATCH_FOR_V8STRINGRESOURCE_VOID(V8StringResource<>, stringReturnValue, returnValue);
154         toBeforeUnloadEvent(event)->setReturnValue(stringReturnValue);
155     }
156
157     if (m_isAttribute && shouldPreventDefault(returnValue))
158         event->preventDefault();
159 }
160
161 bool V8AbstractEventListener::shouldPreventDefault(v8::Local<v8::Value> returnValue)
162 {
163     // Prevent default action if the return value is false in accord with the spec
164     // http://www.w3.org/TR/html5/webappapis.html#event-handler-attributes
165     return returnValue->IsBoolean() && !returnValue->BooleanValue();
166 }
167
168 v8::Local<v8::Object> V8AbstractEventListener::getReceiverObject(ExecutionContext* context, Event* event)
169 {
170     v8::Isolate* isolate = toV8Context(context, world())->GetIsolate();
171     v8::Local<v8::Object> listener = m_listener.newLocal(isolate);
172     if (!m_listener.isEmpty() && !listener->IsFunction())
173         return listener;
174
175     EventTarget* target = event->currentTarget();
176     v8::Handle<v8::Value> value = toV8(target, v8::Handle<v8::Object>(), isolate);
177     if (value.IsEmpty())
178         return v8::Local<v8::Object>();
179     return v8::Local<v8::Object>::New(isolate, v8::Handle<v8::Object>::Cast(value));
180 }
181
182 bool V8AbstractEventListener::belongsToTheCurrentWorld() const
183 {
184     return m_isolate->InContext() && m_world == DOMWrapperWorld::current(m_isolate);
185 }
186
187 void V8AbstractEventListener::setWeakCallback(const v8::WeakCallbackData<v8::Object, V8AbstractEventListener> &data)
188 {
189     data.GetParameter()->m_listener.clear();
190 }
191
192 } // namespace WebCore