Upstream version 5.34.104.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 static v8::Handle<v8::Function> eventListenerEffectiveFunction(v8::Isolate* isolate, v8::Handle<v8::Object> listenerObject)
98 {
99     v8::Handle<v8::Function> function;
100     if (listenerObject->IsFunction()) {
101         function = v8::Handle<v8::Function>::Cast(listenerObject);
102     } else if (listenerObject->IsObject()) {
103         // Try the "handleEvent" method (EventListener interface).
104         v8::Handle<v8::Value> property = listenerObject->Get(v8AtomicString(isolate, "handleEvent"));
105         if (property.IsEmpty() || !property->IsFunction()) {
106             // Fall back to the "constructor" property.
107             property = listenerObject->Get(v8AtomicString(isolate, "constructor"));
108         }
109         if (!property.IsEmpty() && property->IsFunction())
110             function = v8::Handle<v8::Function>::Cast(property);
111     }
112     return function;
113 }
114
115 String eventListenerHandlerBody(Document* document, EventListener* listener)
116 {
117     if (listener->type() != EventListener::JSEventListenerType)
118         return "";
119
120     v8::HandleScope scope(toIsolate(document));
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> object = v8Listener->getListenerObject(document);
125     if (object.IsEmpty())
126         return "";
127     v8::Handle<v8::Function> function = eventListenerEffectiveFunction(scope.GetIsolate(), object);
128     if (function.IsEmpty())
129         return "";
130
131     V8TRYCATCH_FOR_V8STRINGRESOURCE_RETURN(V8StringResource<WithNullCheck>, functionString, function, "");
132     return functionString;
133 }
134
135 ScriptValue eventListenerHandler(Document* document, EventListener* listener)
136 {
137     if (listener->type() != EventListener::JSEventListenerType)
138         return ScriptValue();
139
140     v8::Isolate* isolate = toIsolate(document);
141     v8::HandleScope scope(isolate);
142     V8AbstractEventListener* v8Listener = static_cast<V8AbstractEventListener*>(listener);
143     v8::Handle<v8::Context> context = toV8Context(document, v8Listener->world());
144     v8::Context::Scope contextScope(context);
145     v8::Handle<v8::Object> function = v8Listener->getListenerObject(document);
146     if (function.IsEmpty())
147         return ScriptValue();
148     return ScriptValue(function, isolate);
149 }
150
151 ScriptState* eventListenerHandlerScriptState(Frame* frame, EventListener* listener)
152 {
153     if (listener->type() != EventListener::JSEventListenerType)
154         return 0;
155     V8AbstractEventListener* v8Listener = static_cast<V8AbstractEventListener*>(listener);
156     v8::HandleScope scope(toIsolate(frame));
157     v8::Handle<v8::Context> v8Context = frame->script().windowShell(v8Listener->world())->context();
158     return ScriptState::forContext(v8Context);
159 }
160
161 bool eventListenerHandlerLocation(Document* document, EventListener* listener, String& sourceName, String& scriptId, int& lineNumber)
162 {
163     if (listener->type() != EventListener::JSEventListenerType)
164         return false;
165
166     v8::HandleScope scope(toIsolate(document));
167     V8AbstractEventListener* v8Listener = static_cast<V8AbstractEventListener*>(listener);
168     v8::Handle<v8::Context> context = toV8Context(document, v8Listener->world());
169     v8::Context::Scope contextScope(context);
170     v8::Local<v8::Object> object = v8Listener->getListenerObject(document);
171     if (object.IsEmpty())
172         return false;
173     v8::Handle<v8::Function> function = eventListenerEffectiveFunction(scope.GetIsolate(), object);
174     if (function.IsEmpty())
175         return false;
176     v8::Handle<v8::Function> originalFunction = getBoundFunction(function);
177     int scriptIdValue = originalFunction->ScriptId();
178     scriptId = String::number(scriptIdValue);
179     v8::ScriptOrigin origin = originalFunction->GetScriptOrigin();
180     if (!origin.ResourceName().IsEmpty() && origin.ResourceName()->IsString())
181         sourceName = toCoreString(origin.ResourceName().As<v8::String>());
182     else
183         sourceName = "";
184     lineNumber = originalFunction->GetScriptLineNumber();
185     return true;
186 }
187
188 } // namespace WebCore