Upstream version 5.34.98.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / bindings / v8 / V8WorkerGlobalScopeEventListener.cpp
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 #include "bindings/v8/V8WorkerGlobalScopeEventListener.h"
34
35 #include "V8Event.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"
44
45 namespace WebCore {
46
47 V8WorkerGlobalScopeEventListener::V8WorkerGlobalScopeEventListener(v8::Local<v8::Object> listener, bool isInline, v8::Isolate* isolate)
48     : V8EventListener(listener, isInline, isolate)
49 {
50 }
51
52 void V8WorkerGlobalScopeEventListener::handleEvent(ExecutionContext* context, Event* event)
53 {
54     if (!context)
55         return;
56
57     // The callback function on XMLHttpRequest can clear the event listener and destroys 'this' object. Keep a local reference to it.
58     // See issue 889829.
59     RefPtr<V8AbstractEventListener> protect(this);
60
61     v8::Isolate* isolate = toIsolate(context);
62     v8::HandleScope handleScope(isolate);
63
64     WorkerScriptController* script = toWorkerGlobalScope(context)->script();
65     if (!script)
66         return;
67
68     v8::Handle<v8::Context> v8Context = script->context();
69     if (v8Context.IsEmpty())
70         return;
71
72     // Enter the V8 context in which to perform the event handling.
73     v8::Context::Scope scope(v8Context);
74
75     // Get the V8 wrapper for the event object.
76     v8::Handle<v8::Value> jsEvent = toV8(event, v8::Handle<v8::Object>(), isolate);
77
78     invokeEventHandler(context, event, v8::Local<v8::Value>::New(isolate, jsEvent));
79 }
80
81 v8::Local<v8::Value> V8WorkerGlobalScopeEventListener::callListenerFunction(ExecutionContext* context, v8::Handle<v8::Value> jsEvent, Event* event)
82 {
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>();
87
88     InspectorInstrumentationCookie cookie;
89     if (InspectorInstrumentation::timelineAgentEnabled(context)) {
90         String resourceName("undefined");
91         int lineNumber = 1;
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;
98         }
99         cookie = InspectorInstrumentation::willCallFunction(context, resourceName, lineNumber);
100     }
101
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);
105
106     InspectorInstrumentation::didCallFunction(cookie);
107
108     return result;
109 }
110
111 v8::Local<v8::Object> V8WorkerGlobalScopeEventListener::getReceiverObject(ExecutionContext* context, Event* event)
112 {
113     v8::Local<v8::Object> listener = getListenerObject(context);
114
115     if (!listener.IsEmpty() && !listener->IsFunction())
116         return listener;
117
118     EventTarget* target = event->currentTarget();
119     v8::Isolate* isolate = toIsolate(context);
120     v8::Handle<v8::Value> value = toV8(target, v8::Handle<v8::Object>(), isolate);
121     if (value.IsEmpty())
122         return v8::Local<v8::Object>();
123     return v8::Local<v8::Object>::New(isolate, v8::Handle<v8::Object>::Cast(value));
124 }
125
126 } // namespace WebCore