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
eventParameterName(bool isSVGEvent)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
createAttributeEventListener(Node * node,const QualifiedName & name,const AtomicString & value)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
createAttributeEventListener(Frame * frame,const QualifiedName & name,const AtomicString & value)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
eventListenerHandlerBody(Document * document,EventListener * listener)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
eventListenerHandler(Document * document,EventListener * listener)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
eventListenerHandlerScriptState(Frame * frame,EventListener * listener)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
eventListenerHandlerLocation(Document * document,EventListener * listener,String & sourceName,String & scriptId,int & lineNumber)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 int scriptIdValue = function->ScriptId();
155 scriptId = String::number(scriptIdValue);
156 v8::ScriptOrigin origin = function->GetScriptOrigin();
157 if (!origin.ResourceName().IsEmpty() && origin.ResourceName()->IsString())
158 sourceName = toCoreString(origin.ResourceName().As<v8::String>());
159 else
160 sourceName = "";
161 lineNumber = function->GetScriptLineNumber();
162 return true;
163 }
164
165 } // namespace WebCore
166