Upstream version 5.34.98.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / bindings / v8 / ScriptEventListener.cpp
1 /*
2  * Copyright (C) 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 #include "bindings/v8/ScriptEventListener.h"
33
34 #include "bindings/v8/ScriptController.h"
35 #include "bindings/v8/ScriptScope.h"
36 #include "bindings/v8/ScriptState.h"
37 #include "bindings/v8/V8AbstractEventListener.h"
38 #include "bindings/v8/V8Binding.h"
39 #include "bindings/v8/V8WindowShell.h"
40 #include "core/dom/Document.h"
41 #include "core/dom/DocumentParser.h"
42 #include "core/events/EventListener.h"
43 #include "core/frame/Frame.h"
44
45 namespace WebCore {
46
47 static const AtomicString& eventParameterName(bool isSVGEvent)
48 {
49     DEFINE_STATIC_LOCAL(const AtomicString, eventString, ("event"));
50     DEFINE_STATIC_LOCAL(const AtomicString, evtString, ("evt"));
51     return isSVGEvent ? evtString : eventString;
52 }
53
54 PassRefPtr<V8LazyEventListener> createAttributeEventListener(Node* node, const QualifiedName& name, const AtomicString& value)
55 {
56     ASSERT(node);
57     if (value.isNull())
58         return 0;
59
60     // FIXME: Very strange: we initialize zero-based number with '1'.
61     TextPosition position(OrdinalNumber::fromZeroBasedInt(1), OrdinalNumber::first());
62     String sourceURL;
63
64     v8::Isolate* isolate;
65     if (Frame* frame = node->document().frame()) {
66         isolate = toIsolate(frame);
67         ScriptController& scriptController = frame->script();
68         if (!scriptController.canExecuteScripts(AboutToExecuteScript))
69             return 0;
70         position = scriptController.eventHandlerPosition();
71         sourceURL = node->document().url().string();
72     } else {
73         isolate = v8::Isolate::GetCurrent();
74     }
75
76     return V8LazyEventListener::create(name.localName(), eventParameterName(node->isSVGElement()), value, sourceURL, position, node, isolate);
77 }
78
79 PassRefPtr<V8LazyEventListener> createAttributeEventListener(Frame* frame, const QualifiedName& name, const AtomicString& value)
80 {
81     if (!frame)
82         return 0;
83
84     if (value.isNull())
85         return 0;
86
87     ScriptController& scriptController = frame->script();
88     if (!scriptController.canExecuteScripts(AboutToExecuteScript))
89         return 0;
90
91     TextPosition position = scriptController.eventHandlerPosition();
92     String sourceURL = frame->document()->url().string();
93
94     return V8LazyEventListener::create(name.localName(), eventParameterName(frame->document()->isSVGDocument()), value, sourceURL, position, 0, toIsolate(frame));
95 }
96
97 String eventListenerHandlerBody(Document* document, EventListener* listener)
98 {
99     if (listener->type() != EventListener::JSEventListenerType)
100         return "";
101
102     v8::HandleScope scope(toIsolate(document));
103     V8AbstractEventListener* v8Listener = static_cast<V8AbstractEventListener*>(listener);
104     v8::Handle<v8::Context> context = toV8Context(document, v8Listener->world());
105     v8::Context::Scope contextScope(context);
106     v8::Handle<v8::Value> function = v8Listener->getListenerObject(document);
107     if (function.IsEmpty())
108         return "";
109
110     V8TRYCATCH_FOR_V8STRINGRESOURCE_RETURN(V8StringResource<WithNullCheck>, functionString, function, "");
111     return functionString;
112 }
113
114 ScriptValue eventListenerHandler(Document* document, EventListener* listener)
115 {
116     if (listener->type() != EventListener::JSEventListenerType)
117         return ScriptValue();
118
119     v8::Isolate* isolate = toIsolate(document);
120     v8::HandleScope scope(isolate);
121     V8AbstractEventListener* v8Listener = static_cast<V8AbstractEventListener*>(listener);
122     v8::Handle<v8::Context> context = toV8Context(document, v8Listener->world());
123     v8::Context::Scope contextScope(context);
124     v8::Handle<v8::Object> function = v8Listener->getListenerObject(document);
125     if (function.IsEmpty())
126         return ScriptValue();
127     return ScriptValue(function, isolate);
128 }
129
130 ScriptState* eventListenerHandlerScriptState(Frame* frame, EventListener* listener)
131 {
132     if (listener->type() != EventListener::JSEventListenerType)
133         return 0;
134     V8AbstractEventListener* v8Listener = static_cast<V8AbstractEventListener*>(listener);
135     v8::HandleScope scope(toIsolate(frame));
136     v8::Handle<v8::Context> v8Context = frame->script().windowShell(v8Listener->world())->context();
137     return ScriptState::forContext(v8Context);
138 }
139
140 bool eventListenerHandlerLocation(Document* document, EventListener* listener, String& sourceName, String& scriptId, int& lineNumber)
141 {
142     if (listener->type() != EventListener::JSEventListenerType)
143         return false;
144
145     v8::HandleScope scope(toIsolate(document));
146     V8AbstractEventListener* v8Listener = static_cast<V8AbstractEventListener*>(listener);
147     v8::Handle<v8::Context> context = toV8Context(document, v8Listener->world());
148     v8::Context::Scope contextScope(context);
149     v8::Handle<v8::Object> object = v8Listener->getListenerObject(document);
150     if (object.IsEmpty() || !object->IsFunction())
151         return false;
152
153     v8::Handle<v8::Function> function = v8::Handle<v8::Function>::Cast(object);
154     v8::Handle<v8::Function> originalFunction = getBoundFunction(function);
155     int scriptIdValue = originalFunction->ScriptId();
156     scriptId = String::number(scriptIdValue);
157     v8::ScriptOrigin origin = originalFunction->GetScriptOrigin();
158     if (!origin.ResourceName().IsEmpty() && origin.ResourceName()->IsString())
159         sourceName = toCoreString(origin.ResourceName().As<v8::String>());
160     else
161         sourceName = "";
162     lineNumber = originalFunction->GetScriptLineNumber();
163     return true;
164 }
165
166 } // namespace WebCore