2 * Copyright (C) 2006, 2007, 2008, 2009 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
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
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.
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.
32 #include "V8AbstractEventListener.h"
37 #include "Tokenizer.h"
38 #include "V8Binding.h"
39 #include "V8Utilities.h"
43 V8AbstractEventListener::V8AbstractEventListener(Frame* frame, bool isAttribute)
44 : m_isAttribute(isAttribute)
52 // We might be called directly from the parser.
53 v8::HandleScope handleScope;
55 m_context.set(V8Proxy::context(m_frame));
58 // Get the position in the source if any.
59 if (m_isAttribute && m_frame->document()->tokenizer()) {
60 m_lineNumber = m_frame->document()->tokenizer()->lineNumber();
61 m_columnNumber = m_frame->document()->tokenizer()->columnNumber();
65 void V8AbstractEventListener::invokeEventHandler(v8::Handle<v8::Context> v8Context, Event* event, v8::Handle<v8::Value> jsEvent, bool isWindowEvent)
67 // We push the event being processed into the global object, so that it can be exposed by DOMWindow's bindings.
68 v8::Local<v8::String> eventSymbol = v8::String::NewSymbol("event");
69 v8::Local<v8::Value> returnValue;
72 // Catch exceptions thrown in the event handler so they do not propagate to javascript code that caused the event to fire.
73 v8::TryCatch tryCatch;
74 tryCatch.SetVerbose(true);
76 // Save the old 'event' property so we can restore it later.
77 v8::Local<v8::Value> savedEvent = v8Context->Global()->GetHiddenValue(eventSymbol);
80 // Make the event available in the global object, so DOMWindow can expose it.
81 v8Context->Global()->SetHiddenValue(eventSymbol, jsEvent);
84 // Call the event handler.
85 tryCatch.SetVerbose(false); // We do not want to report the exception to the inspector console.
86 returnValue = callListenerFunction(jsEvent, event, isWindowEvent);
88 // If an error occurs while handling the event, it should be reported.
89 if (tryCatch.HasCaught()) {
90 reportException(0, tryCatch);
94 // Restore the old event. This must be done for all exit paths through this method.
95 tryCatch.SetVerbose(true);
96 if (savedEvent.IsEmpty())
97 v8Context->Global()->SetHiddenValue(eventSymbol, v8::Undefined());
99 v8Context->Global()->SetHiddenValue(eventSymbol, savedEvent);
103 ASSERT(!V8Proxy::handleOutOfMemory() || returnValue.IsEmpty());
105 if (returnValue.IsEmpty())
108 if (!returnValue->IsNull() && !returnValue->IsUndefined() && event->storesResultAsString())
109 event->storeResult(toWebCoreString(returnValue));
111 // Prevent default action if the return value is false;
112 // FIXME: Add example, and reference to bug entry.
113 if (m_isAttribute && returnValue->IsBoolean() && !returnValue->BooleanValue())
114 event->preventDefault();
117 void V8AbstractEventListener::handleEvent(Event* event, bool isWindowEvent)
119 // EventListener could be disconnected from the frame.
123 // The callback function on XMLHttpRequest can clear the event listener and destroys 'this' object. Keep a local reference to it.
125 RefPtr<V8AbstractEventListener> protect(this);
127 v8::HandleScope handleScope;
129 v8::Handle<v8::Context> v8Context = m_context.get();
130 if (v8Context.IsEmpty())
133 // m_frame can removed by the callback function, protect it until the callback function returns.
134 RefPtr<Frame> protectFrame(m_frame);
136 // Enter the V8 context in which to perform the event handling.
137 v8::Context::Scope scope(v8Context);
139 // Get the V8 wrapper for the event object.
140 v8::Handle<v8::Value> jsEvent = V8DOMWrapper::convertEventToV8Object(event);
142 invokeEventHandler(v8Context, event, jsEvent, isWindowEvent);
144 Document::updateStyleForAllDocuments();
147 void V8AbstractEventListener::disposeListenerObject()
149 if (!m_listener.IsEmpty()) {
151 V8GCController::unregisterGlobalHandle(this, m_listener);
153 m_listener.Dispose();
158 v8::Local<v8::Object> V8AbstractEventListener::getReceiverObject(Event* event, bool isWindowEvent)
160 if (!m_listener.IsEmpty() && !m_listener->IsFunction())
161 return v8::Local<v8::Object>::New(m_listener);
164 return v8::Context::GetCurrent()->Global();
166 EventTarget* target = event->currentTarget();
167 v8::Handle<v8::Value> value = V8DOMWrapper::convertEventTargetToV8Object(target);
169 return v8::Local<v8::Object>();
170 return v8::Local<v8::Object>::New(v8::Handle<v8::Object>::Cast(value));
173 } // namespace WebCore