OLD | NEW |
| (Empty) |
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 #include "bindings/v8/V8LazyEventListener.h" | |
33 | |
34 #include "bindings/core/v8/V8Document.h" | |
35 #include "bindings/core/v8/V8HTMLFormElement.h" | |
36 #include "bindings/core/v8/V8Node.h" | |
37 #include "bindings/v8/ScriptController.h" | |
38 #include "bindings/v8/ScriptSourceCode.h" | |
39 #include "bindings/v8/V8Binding.h" | |
40 #include "bindings/v8/V8DOMWrapper.h" | |
41 #include "bindings/v8/V8HiddenValue.h" | |
42 #include "bindings/v8/V8ScriptRunner.h" | |
43 #include "core/dom/Document.h" | |
44 #include "core/dom/Node.h" | |
45 #include "core/html/HTMLElement.h" | |
46 #include "core/html/HTMLFormElement.h" | |
47 #include "core/inspector/InspectorInstrumentation.h" | |
48 #include "core/frame/LocalFrame.h" | |
49 #include "core/frame/csp/ContentSecurityPolicy.h" | |
50 | |
51 #include "wtf/StdLibExtras.h" | |
52 | |
53 namespace WebCore { | |
54 | |
55 V8LazyEventListener::V8LazyEventListener(const AtomicString& functionName, const
AtomicString& eventParameterName, const String& code, const String sourceURL, c
onst TextPosition& position, Node* node, v8::Isolate* isolate) | |
56 : V8AbstractEventListener(true, isolate) | |
57 , m_functionName(functionName) | |
58 , m_eventParameterName(eventParameterName) | |
59 , m_code(code) | |
60 , m_sourceURL(sourceURL) | |
61 , m_node(node) | |
62 , m_position(position) | |
63 { | |
64 } | |
65 | |
66 template<typename T> | |
67 v8::Handle<v8::Object> toObjectWrapper(T* domObject, ScriptState* scriptState) | |
68 { | |
69 if (!domObject) | |
70 return v8::Object::New(scriptState->isolate()); | |
71 v8::Handle<v8::Value> value = toV8(domObject, scriptState->context()->Global
(), scriptState->isolate()); | |
72 if (value.IsEmpty()) | |
73 return v8::Object::New(scriptState->isolate()); | |
74 return v8::Local<v8::Object>::New(scriptState->isolate(), value.As<v8::Objec
t>()); | |
75 } | |
76 | |
77 v8::Local<v8::Value> V8LazyEventListener::callListenerFunction(v8::Handle<v8::Va
lue> jsEvent, Event* event) | |
78 { | |
79 v8::Local<v8::Object> listenerObject = getListenerObject(scriptState()->exec
utionContext()); | |
80 if (listenerObject.IsEmpty()) | |
81 return v8::Local<v8::Value>(); | |
82 | |
83 v8::Local<v8::Function> handlerFunction = listenerObject.As<v8::Function>(); | |
84 v8::Local<v8::Object> receiver = getReceiverObject(event); | |
85 if (handlerFunction.IsEmpty() || receiver.IsEmpty()) | |
86 return v8::Local<v8::Value>(); | |
87 | |
88 if (!scriptState()->executionContext()->isDocument()) | |
89 return v8::Local<v8::Value>(); | |
90 | |
91 LocalFrame* frame = toDocument(scriptState()->executionContext())->frame(); | |
92 if (!frame) | |
93 return v8::Local<v8::Value>(); | |
94 | |
95 if (!frame->script().canExecuteScripts(AboutToExecuteScript)) | |
96 return v8::Local<v8::Value>(); | |
97 | |
98 v8::Handle<v8::Value> parameters[1] = { jsEvent }; | |
99 return frame->script().callFunction(handlerFunction, receiver, WTF_ARRAY_LEN
GTH(parameters), parameters); | |
100 } | |
101 | |
102 static void V8LazyEventListenerToString(const v8::FunctionCallbackInfo<v8::Value
>& info) | |
103 { | |
104 v8SetReturnValue(info, V8HiddenValue::getHiddenValue(info.GetIsolate(), info
.Holder(), V8HiddenValue::toStringString(info.GetIsolate()))); | |
105 } | |
106 | |
107 void V8LazyEventListener::handleEvent(ExecutionContext* context, Event* event) | |
108 { | |
109 v8::HandleScope handleScope(toIsolate(context)); | |
110 // V8LazyEventListener doesn't know the associated context when created. | |
111 // Thus we lazily get the associated context and set a ScriptState on V8Abst
ractEventListener. | |
112 v8::Local<v8::Context> v8Context = toV8Context(context, world()); | |
113 if (v8Context.IsEmpty()) | |
114 return; | |
115 setScriptState(ScriptState::from(v8Context)); | |
116 | |
117 V8AbstractEventListener::handleEvent(context, event); | |
118 } | |
119 | |
120 void V8LazyEventListener::prepareListenerObject(ExecutionContext* context) | |
121 { | |
122 v8::HandleScope handleScope(toIsolate(context)); | |
123 // V8LazyEventListener doesn't know the associated context when created. | |
124 // Thus we lazily get the associated context and set a ScriptState on V8Abst
ractEventListener. | |
125 v8::Local<v8::Context> v8Context = toV8Context(context, world()); | |
126 if (v8Context.IsEmpty()) | |
127 return; | |
128 setScriptState(ScriptState::from(v8Context)); | |
129 | |
130 if (context->isDocument() && !toDocument(context)->allowInlineEventHandlers(
m_node, this, m_sourceURL, m_position.m_line)) { | |
131 clearListenerObject(); | |
132 return; | |
133 } | |
134 | |
135 if (hasExistingListenerObject()) | |
136 return; | |
137 | |
138 ASSERT(context->isDocument()); | |
139 | |
140 ScriptState::Scope scope(scriptState()); | |
141 String listenerSource = InspectorInstrumentation::preprocessEventListener(t
oDocument(context)->frame(), m_code, m_sourceURL, m_functionName); | |
142 | |
143 // FIXME: Remove the following 'with' hack. | |
144 // | |
145 // Nodes other than the document object, when executing inline event | |
146 // handlers push document, form owner, and the target node on the scope chai
n. | |
147 // We do this by using 'with' statement. | |
148 // See chrome/fast/forms/form-action.html | |
149 // chrome/fast/forms/selected-index-value.html | |
150 // base/fast/overflow/onscroll-layer-self-destruct.html | |
151 // | |
152 // Don't use new lines so that lines in the modified handler | |
153 // have the same numbers as in the original code. | |
154 // FIXME: V8 does not allow us to programmatically create object environment
s so | |
155 // we have to do this hack! What if m_code escapes to run arbitrary s
cript? | |
156 // | |
157 // Call with 4 arguments instead of 3, pass additional null as the last para
meter. | |
158 // By calling the function with 4 arguments, we create a setter on arguments
object | |
159 // which would shadow property "3" on the prototype. | |
160 String code = "(function() {" | |
161 "with (this[2]) {" | |
162 "with (this[1]) {" | |
163 "with (this[0]) {" | |
164 "return function(" + m_eventParameterName + ") {" + | |
165 listenerSource + "\n" // Insert '\n' otherwise //-style comments
could break the handler. | |
166 "};" | |
167 "}}}})"; | |
168 | |
169 v8::Handle<v8::String> codeExternalString = v8String(isolate(), code); | |
170 | |
171 v8::Local<v8::Value> result = V8ScriptRunner::compileAndRunInternalScript(co
deExternalString, isolate(), m_sourceURL, m_position); | |
172 if (result.IsEmpty()) | |
173 return; | |
174 | |
175 // Call the outer function to get the inner function. | |
176 ASSERT(result->IsFunction()); | |
177 v8::Local<v8::Function> intermediateFunction = result.As<v8::Function>(); | |
178 | |
179 HTMLFormElement* formElement = 0; | |
180 if (m_node && m_node->isHTMLElement()) | |
181 formElement = toHTMLElement(m_node)->formOwner(); | |
182 | |
183 v8::Handle<v8::Object> nodeWrapper = toObjectWrapper<Node>(m_node, scriptSta
te()); | |
184 v8::Handle<v8::Object> formWrapper = toObjectWrapper<HTMLFormElement>(formEl
ement, scriptState()); | |
185 v8::Handle<v8::Object> documentWrapper = toObjectWrapper<Document>(m_node ?
m_node->ownerDocument() : 0, scriptState()); | |
186 | |
187 v8::Local<v8::Object> thisObject = v8::Object::New(isolate()); | |
188 if (thisObject.IsEmpty()) | |
189 return; | |
190 if (!thisObject->ForceSet(v8::Integer::New(isolate(), 0), nodeWrapper)) | |
191 return; | |
192 if (!thisObject->ForceSet(v8::Integer::New(isolate(), 1), formWrapper)) | |
193 return; | |
194 if (!thisObject->ForceSet(v8::Integer::New(isolate(), 2), documentWrapper)) | |
195 return; | |
196 | |
197 // FIXME: Remove this code when we stop doing the 'with' hack above. | |
198 v8::Local<v8::Value> innerValue = V8ScriptRunner::callInternalFunction(inter
mediateFunction, thisObject, 0, 0, isolate()); | |
199 if (innerValue.IsEmpty() || !innerValue->IsFunction()) | |
200 return; | |
201 | |
202 v8::Local<v8::Function> wrappedFunction = innerValue.As<v8::Function>(); | |
203 | |
204 // Change the toString function on the wrapper function to avoid it | |
205 // returning the source for the actual wrapper function. Instead it | |
206 // returns source for a clean wrapper function with the event | |
207 // argument wrapping the event source code. The reason for this is | |
208 // that some web sites use toString on event functions and eval the | |
209 // source returned (sometimes a RegExp is applied as well) for some | |
210 // other use. That fails miserably if the actual wrapper source is | |
211 // returned. | |
212 v8::Local<v8::Function> toStringFunction = v8::Function::New(isolate(), V8La
zyEventListenerToString); | |
213 ASSERT(!toStringFunction.IsEmpty()); | |
214 String toStringString = "function " + m_functionName + "(" + m_eventParamete
rName + ") {\n " + m_code + "\n}"; | |
215 V8HiddenValue::setHiddenValue(isolate(), wrappedFunction, V8HiddenValue::toS
tringString(isolate()), v8String(isolate(), toStringString)); | |
216 wrappedFunction->Set(v8AtomicString(isolate(), "toString"), toStringFunction
); | |
217 wrappedFunction->SetName(v8String(isolate(), m_functionName)); | |
218 | |
219 // FIXME: Remove the following comment-outs. | |
220 // See https://bugs.webkit.org/show_bug.cgi?id=85152 for more details. | |
221 // | |
222 // For the time being, we comment out the following code since the | |
223 // second parsing can happen. | |
224 // // Since we only parse once, there's no need to keep data used for parsin
g around anymore. | |
225 // m_functionName = String(); | |
226 // m_code = String(); | |
227 // m_eventParameterName = String(); | |
228 // m_sourceURL = String(); | |
229 | |
230 setListenerObject(wrappedFunction); | |
231 } | |
232 | |
233 } // namespace WebCore | |
OLD | NEW |