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.
35 #include "V8WorkerContextEventListener.h"
38 #include "V8Binding.h"
39 #include "WorkerContextExecutionProxy.h"
43 V8WorkerContextEventListener::V8WorkerContextEventListener(WorkerContextExecutionProxy* proxy, v8::Local<v8::Object> listener, bool isInline)
44 : V8EventListener(0, listener, isInline)
49 V8WorkerContextEventListener::~V8WorkerContextEventListener()
52 m_proxy->RemoveEventListener(this);
53 disposeListenerObject();
56 void V8WorkerContextEventListener::handleEvent(Event* event, bool isWindowEvent)
58 // Is the EventListener disconnected?
62 // The callback function on XMLHttpRequest can clear the event listener and destroys 'this' object. Keep a local reference to it.
64 RefPtr<V8AbstractEventListener> protect(this);
66 v8::HandleScope handleScope;
68 v8::Handle<v8::Context> context = m_proxy->GetContext();
69 if (context.IsEmpty())
72 // Enter the V8 context in which to perform the event handling.
73 v8::Context::Scope scope(context);
75 // Get the V8 wrapper for the event object.
76 v8::Handle<v8::Value> jsEvent = WorkerContextExecutionProxy::EventToV8Object(event);
78 invokeEventHandler(context, event, jsEvent, isWindowEvent);
81 bool V8WorkerContextEventListener::reportError(const String& message, const String& url, int lineNumber)
83 // Is the EventListener disconnected?
87 // The callback function can clear the event listener and destroy 'this' object. Keep a local reference to it.
88 RefPtr<V8AbstractEventListener> protect(this);
90 v8::HandleScope handleScope;
92 v8::Handle<v8::Context> context = m_proxy->GetContext();
93 if (context.IsEmpty())
96 // Enter the V8 context in which to perform the event handling.
97 v8::Context::Scope scope(context);
99 v8::Local<v8::Value> returnValue;
101 // Catch exceptions thrown in calling the function so they do not propagate to javascript code that caused the event to fire.
102 v8::TryCatch tryCatch;
103 tryCatch.SetVerbose(true);
105 // Call the function.
106 if (!m_listener.IsEmpty() && m_listener->IsFunction()) {
107 v8::Local<v8::Function> callFunction = v8::Local<v8::Function>::New(v8::Persistent<v8::Function>::Cast(m_listener));
108 v8::Local<v8::Object> thisValue = v8::Context::GetCurrent()->Global();
110 v8::Handle<v8::Value> parameters[3] = { v8String(message), v8String(url), v8::Integer::New(lineNumber) };
111 returnValue = callFunction->Call(thisValue, 3, parameters);
114 // If an error occurs while handling the script error, it should be bubbled up.
115 if (tryCatch.HasCaught()) {
121 // If the function returns false, then the error is handled. Otherwise, the error is not handled.
122 bool errorHandled = returnValue->IsBoolean() && !returnValue->BooleanValue();
127 v8::Local<v8::Value> V8WorkerContextEventListener::callListenerFunction(v8::Handle<v8::Value> jsEvent, Event* event, bool isWindowEvent)
129 v8::Local<v8::Function> handlerFunction = getListenerFunction();
130 v8::Local<v8::Object> receiver = getReceiverObject(event, isWindowEvent);
131 if (handlerFunction.IsEmpty() || receiver.IsEmpty())
132 return v8::Local<v8::Value>();
134 v8::Handle<v8::Value> parameters[1] = { jsEvent };
135 v8::Local<v8::Value> result = handlerFunction->Call(receiver, 1, parameters);
137 m_proxy->trackEvent(event);
142 v8::Local<v8::Object> V8WorkerContextEventListener::getReceiverObject(Event* event, bool isWindowEvent)
144 if (!m_listener.IsEmpty() && !m_listener->IsFunction())
145 return v8::Local<v8::Object>::New(m_listener);
148 return v8::Context::GetCurrent()->Global();
150 EventTarget* target = event->currentTarget();
151 v8::Handle<v8::Value> value = WorkerContextExecutionProxy::EventTargetToV8Object(target);
153 return v8::Local<v8::Object>();
154 return v8::Local<v8::Object>::New(v8::Handle<v8::Object>::Cast(value));
157 } // namespace WebCore