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
33 #if ENABLE(WORKERS)
34
35 #include "V8WorkerContextEventListener.h"
36
37 #include "V8Binding.h"
38 #include "V8DOMWrapper.h"
39 #include "V8Event.h"
40 #include "WorkerContext.h"
41 #include "WorkerContextExecutionProxy.h"
42
43 namespace WebCore {
44
workerProxy(ScriptExecutionContext * context)45 static WorkerContextExecutionProxy* workerProxy(ScriptExecutionContext* context)
46 {
47 ASSERT(context->isWorkerContext());
48 WorkerContext* workerContext = static_cast<WorkerContext*>(context);
49 return workerContext->script()->proxy();
50 }
51
V8WorkerContextEventListener(v8::Local<v8::Object> listener,bool isInline,const WorldContextHandle & worldContext)52 V8WorkerContextEventListener::V8WorkerContextEventListener(v8::Local<v8::Object> listener, bool isInline, const WorldContextHandle& worldContext)
53 : V8EventListener(listener, isInline, worldContext)
54 {
55 }
56
handleEvent(ScriptExecutionContext * context,Event * event)57 void V8WorkerContextEventListener::handleEvent(ScriptExecutionContext* context, Event* event)
58 {
59 if (!context)
60 return;
61
62 // The callback function on XMLHttpRequest can clear the event listener and destroys 'this' object. Keep a local reference to it.
63 // See issue 889829.
64 RefPtr<V8AbstractEventListener> protect(this);
65
66 v8::HandleScope handleScope;
67
68 WorkerContextExecutionProxy* proxy = workerProxy(context);
69 if (!proxy)
70 return;
71
72 v8::Handle<v8::Context> v8Context = proxy->context();
73 if (v8Context.IsEmpty())
74 return;
75
76 // Enter the V8 context in which to perform the event handling.
77 v8::Context::Scope scope(v8Context);
78
79 // Get the V8 wrapper for the event object.
80 v8::Handle<v8::Value> jsEvent = toV8(event);
81
82 invokeEventHandler(context, event, jsEvent);
83 }
84
callListenerFunction(ScriptExecutionContext * context,v8::Handle<v8::Value> jsEvent,Event * event)85 v8::Local<v8::Value> V8WorkerContextEventListener::callListenerFunction(ScriptExecutionContext* context, v8::Handle<v8::Value> jsEvent, Event* event)
86 {
87 v8::Local<v8::Function> handlerFunction = getListenerFunction(context);
88 v8::Local<v8::Object> receiver = getReceiverObject(context, event);
89 if (handlerFunction.IsEmpty() || receiver.IsEmpty())
90 return v8::Local<v8::Value>();
91
92 v8::Handle<v8::Value> parameters[1] = { jsEvent };
93 v8::Local<v8::Value> result = handlerFunction->Call(receiver, 1, parameters);
94
95 if (WorkerContextExecutionProxy* proxy = workerProxy(context))
96 proxy->trackEvent(event);
97
98 return result;
99 }
100
getReceiverObject(ScriptExecutionContext * context,Event * event)101 v8::Local<v8::Object> V8WorkerContextEventListener::getReceiverObject(ScriptExecutionContext* context, Event* event)
102 {
103 v8::Local<v8::Object> listener = getListenerObject(context);
104
105 if (!listener.IsEmpty() && !listener->IsFunction())
106 return listener;
107
108 EventTarget* target = event->currentTarget();
109 v8::Handle<v8::Value> value = V8DOMWrapper::convertEventTargetToV8Object(target);
110 if (value.IsEmpty())
111 return v8::Local<v8::Object>();
112 return v8::Local<v8::Object>::New(v8::Handle<v8::Object>::Cast(value));
113 }
114
115 } // namespace WebCore
116
117 #endif // WORKERS
118