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.
33 #include "bindings/v8/V8WorkerGlobalScopeEventListener.h"
36 #include "V8EventTarget.h"
37 #include "bindings/v8/V8Binding.h"
38 #include "bindings/v8/V8DOMWrapper.h"
39 #include "bindings/v8/V8GCController.h"
40 #include "bindings/v8/V8ScriptRunner.h"
41 #include "bindings/v8/WorkerScriptController.h"
42 #include "core/inspector/InspectorInstrumentation.h"
43 #include "core/workers/WorkerGlobalScope.h"
47 V8WorkerGlobalScopeEventListener::V8WorkerGlobalScopeEventListener(v8::Local<v8::Object> listener, bool isInline, v8::Isolate* isolate)
48 : V8EventListener(listener, isInline, isolate)
52 void V8WorkerGlobalScopeEventListener::handleEvent(ExecutionContext* context, Event* event)
57 // The callback function on XMLHttpRequest can clear the event listener and destroys 'this' object. Keep a local reference to it.
59 RefPtr<V8AbstractEventListener> protect(this);
61 v8::Isolate* isolate = toIsolate(context);
62 v8::HandleScope handleScope(isolate);
64 WorkerScriptController* script = toWorkerGlobalScope(context)->script();
68 v8::Handle<v8::Context> v8Context = script->context();
69 if (v8Context.IsEmpty())
72 // Enter the V8 context in which to perform the event handling.
73 v8::Context::Scope scope(v8Context);
75 // Get the V8 wrapper for the event object.
76 v8::Handle<v8::Value> jsEvent = toV8(event, v8::Handle<v8::Object>(), isolate);
78 invokeEventHandler(context, event, v8::Local<v8::Value>::New(isolate, jsEvent));
81 v8::Local<v8::Value> V8WorkerGlobalScopeEventListener::callListenerFunction(ExecutionContext* context, v8::Handle<v8::Value> jsEvent, Event* event)
83 v8::Local<v8::Function> handlerFunction = getListenerFunction(context);
84 v8::Local<v8::Object> receiver = getReceiverObject(context, event);
85 if (handlerFunction.IsEmpty() || receiver.IsEmpty())
86 return v8::Local<v8::Value>();
88 InspectorInstrumentationCookie cookie;
89 if (InspectorInstrumentation::timelineAgentEnabled(context)) {
90 String resourceName("undefined");
92 v8::Handle<v8::Function> originalFunction = getBoundFunction(handlerFunction);
93 v8::ScriptOrigin origin = originalFunction->GetScriptOrigin();
94 if (!origin.ResourceName().IsEmpty()) {
95 V8TRYCATCH_FOR_V8STRINGRESOURCE_RETURN(V8StringResource<>, stringResourceName, origin.ResourceName(), v8::Local<v8::Value>());
96 resourceName = stringResourceName;
97 lineNumber = originalFunction->GetScriptLineNumber() + 1;
99 cookie = InspectorInstrumentation::willCallFunction(context, resourceName, lineNumber);
102 v8::Isolate* isolate = toIsolate(context);
103 v8::Handle<v8::Value> parameters[1] = { jsEvent };
104 v8::Local<v8::Value> result = V8ScriptRunner::callFunction(handlerFunction, context, receiver, WTF_ARRAY_LENGTH(parameters), parameters, isolate);
106 InspectorInstrumentation::didCallFunction(cookie);
111 v8::Local<v8::Object> V8WorkerGlobalScopeEventListener::getReceiverObject(ExecutionContext* context, Event* event)
113 v8::Local<v8::Object> listener = getListenerObject(context);
115 if (!listener.IsEmpty() && !listener->IsFunction())
118 EventTarget* target = event->currentTarget();
119 v8::Isolate* isolate = toIsolate(context);
120 v8::Handle<v8::Value> value = toV8(target, v8::Handle<v8::Object>(), isolate);
122 return v8::Local<v8::Object>();
123 return v8::Local<v8::Object>::New(isolate, v8::Handle<v8::Object>::Cast(value));
126 } // namespace WebCore