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/core/v8/V8WorkerGlobalScopeEventListener.h"
35 #include "bindings/core/v8/V8Binding.h"
36 #include "bindings/core/v8/V8DOMWrapper.h"
37 #include "bindings/core/v8/V8Event.h"
38 #include "bindings/core/v8/V8EventTarget.h"
39 #include "bindings/core/v8/V8GCController.h"
40 #include "bindings/core/v8/V8ScriptRunner.h"
41 #include "bindings/core/v8/WorkerScriptController.h"
42 #include "core/inspector/InspectorInstrumentation.h"
43 #include "core/inspector/InspectorTraceEvents.h"
44 #include "core/workers/WorkerGlobalScope.h"
48 V8WorkerGlobalScopeEventListener::V8WorkerGlobalScopeEventListener(v8::Local<v8::Object> listener, bool isInline, ScriptState* scriptState)
49 : V8EventListener(listener, isInline, scriptState)
53 void V8WorkerGlobalScopeEventListener::handleEvent(ExecutionContext*, Event* event)
55 // The callback function on XMLHttpRequest can clear the event listener and destroys 'this' object. Keep a local reference to it.
57 RefPtr<V8AbstractEventListener> protect(this);
59 WorkerScriptController* script = toWorkerGlobalScope(scriptState()->executionContext())->script();
63 if (scriptState()->contextIsEmpty())
65 ScriptState::Scope scope(scriptState());
67 // Get the V8 wrapper for the event object.
68 v8::Handle<v8::Value> jsEvent = toV8(event, scriptState()->context()->Global(), isolate());
70 invokeEventHandler(event, v8::Local<v8::Value>::New(isolate(), jsEvent));
73 v8::Local<v8::Value> V8WorkerGlobalScopeEventListener::callListenerFunction(v8::Handle<v8::Value> jsEvent, Event* event)
75 v8::Local<v8::Function> handlerFunction = getListenerFunction(scriptState()->executionContext());
76 v8::Local<v8::Object> receiver = getReceiverObject(event);
77 if (handlerFunction.IsEmpty() || receiver.IsEmpty())
78 return v8::Local<v8::Value>();
80 TRACE_EVENT1(TRACE_DISABLED_BY_DEFAULT("devtools.timeline"), "FunctionCall", "data", devToolsTraceEventData(scriptState()->executionContext(), handlerFunction, isolate()));
81 TRACE_EVENT_INSTANT1(TRACE_DISABLED_BY_DEFAULT("devtools.timeline.stack"), "CallStack", "stack", InspectorCallStackEvent::currentCallStack());
82 // FIXME(361045): remove InspectorInstrumentation calls once DevTools Timeline migrates to tracing.
83 InspectorInstrumentationCookie cookie;
84 if (InspectorInstrumentation::timelineAgentEnabled(scriptState()->executionContext())) {
88 GetDevToolsFunctionInfo(handlerFunction, isolate(), scriptId, resourceName, lineNumber);
89 cookie = InspectorInstrumentation::willCallFunction(scriptState()->executionContext(), scriptId, resourceName, lineNumber);
92 v8::Handle<v8::Value> parameters[1] = { jsEvent };
93 v8::Local<v8::Value> result = V8ScriptRunner::callFunction(handlerFunction, scriptState()->executionContext(), receiver, WTF_ARRAY_LENGTH(parameters), parameters, isolate());
95 InspectorInstrumentation::didCallFunction(cookie);
96 TRACE_EVENT_INSTANT1(TRACE_DISABLED_BY_DEFAULT("devtools.timeline"), "UpdateCounters", "data", InspectorUpdateCountersEvent::data());
101 v8::Local<v8::Object> V8WorkerGlobalScopeEventListener::getReceiverObject(Event* event)
103 v8::Local<v8::Object> listener = getListenerObject(scriptState()->executionContext());
105 if (!listener.IsEmpty() && !listener->IsFunction())
108 EventTarget* target = event->currentTarget();
109 v8::Handle<v8::Value> value = toV8(target, scriptState()->context()->Global(), isolate());
111 return v8::Local<v8::Object>();
112 return v8::Local<v8::Object>::New(isolate(), v8::Handle<v8::Object>::Cast(value));