2 * Copyright (C) 2006, 2007, 2008, 2009 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
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
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.
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.
32 #include "bindings/core/v8/V8LazyEventListener.h"
34 #include "bindings/core/v8/ScriptController.h"
35 #include "bindings/core/v8/ScriptSourceCode.h"
36 #include "bindings/core/v8/V8Binding.h"
37 #include "bindings/core/v8/V8DOMWrapper.h"
38 #include "bindings/core/v8/V8Document.h"
39 #include "bindings/core/v8/V8HTMLFormElement.h"
40 #include "bindings/core/v8/V8HiddenValue.h"
41 #include "bindings/core/v8/V8Node.h"
42 #include "bindings/core/v8/V8ScriptRunner.h"
43 #include "core/dom/Document.h"
44 #include "core/dom/Node.h"
45 #include "core/frame/LocalFrame.h"
46 #include "core/frame/csp/ContentSecurityPolicy.h"
47 #include "core/html/HTMLElement.h"
48 #include "core/html/HTMLFormElement.h"
49 #include "core/inspector/InspectorInstrumentation.h"
51 #include "wtf/StdLibExtras.h"
55 V8LazyEventListener::V8LazyEventListener(const AtomicString& functionName, const AtomicString& eventParameterName, const String& code, const String sourceURL, const TextPosition& position, Node* node, v8::Isolate* isolate)
56 : V8AbstractEventListener(true, isolate)
57 , m_functionName(functionName)
58 , m_eventParameterName(eventParameterName)
60 , m_sourceURL(sourceURL)
62 , m_position(position)
67 v8::Handle<v8::Object> toObjectWrapper(T* domObject, ScriptState* scriptState)
70 return v8::Object::New(scriptState->isolate());
71 v8::Handle<v8::Value> value = toV8(domObject, scriptState->context()->Global(), scriptState->isolate());
73 return v8::Object::New(scriptState->isolate());
74 return v8::Local<v8::Object>::New(scriptState->isolate(), value.As<v8::Object>());
77 v8::Local<v8::Value> V8LazyEventListener::callListenerFunction(v8::Handle<v8::Value> jsEvent, Event* event)
79 v8::Local<v8::Object> listenerObject = getListenerObject(scriptState()->executionContext());
80 if (listenerObject.IsEmpty())
81 return v8::Local<v8::Value>();
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>();
88 if (!scriptState()->executionContext()->isDocument())
89 return v8::Local<v8::Value>();
91 LocalFrame* frame = toDocument(scriptState()->executionContext())->frame();
93 return v8::Local<v8::Value>();
95 if (!frame->script().canExecuteScripts(AboutToExecuteScript))
96 return v8::Local<v8::Value>();
98 v8::Handle<v8::Value> parameters[1] = { jsEvent };
99 return frame->script().callFunction(handlerFunction, receiver, WTF_ARRAY_LENGTH(parameters), parameters);
102 static void V8LazyEventListenerToString(const v8::FunctionCallbackInfo<v8::Value>& info)
104 v8SetReturnValue(info, V8HiddenValue::getHiddenValue(info.GetIsolate(), info.Holder(), V8HiddenValue::toStringString(info.GetIsolate())));
107 void V8LazyEventListener::handleEvent(ExecutionContext* context, Event* event)
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 V8AbstractEventListener.
112 v8::Local<v8::Context> v8Context = toV8Context(context, world());
113 if (v8Context.IsEmpty())
115 setScriptState(ScriptState::from(v8Context));
117 V8AbstractEventListener::handleEvent(context, event);
120 void V8LazyEventListener::prepareListenerObject(ExecutionContext* context)
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 V8AbstractEventListener.
125 v8::Local<v8::Context> v8Context = toV8Context(context, world());
126 if (v8Context.IsEmpty())
128 setScriptState(ScriptState::from(v8Context));
130 if (context->isDocument() && !toDocument(context)->allowInlineEventHandlers(m_node, this, m_sourceURL, m_position.m_line)) {
131 clearListenerObject();
135 if (hasExistingListenerObject())
138 ASSERT(context->isDocument());
140 ScriptState::Scope scope(scriptState());
141 String listenerSource = InspectorInstrumentation::preprocessEventListener(toDocument(context)->frame(), m_code, m_sourceURL, m_functionName);
143 // FIXME: Remove the following 'with' hack.
145 // Nodes other than the document object, when executing inline event
146 // handlers push document, form owner, and the target node on the scope chain.
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
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 environments so
155 // we have to do this hack! What if m_code escapes to run arbitrary script?
157 // Call with 4 arguments instead of 3, pass additional null as the last parameter.
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() {"
164 "return function(" + m_eventParameterName + ") {" +
165 listenerSource + "\n" // Insert '\n' otherwise //-style comments could break the handler.
169 v8::Handle<v8::String> codeExternalString = v8String(isolate(), code);
171 v8::Local<v8::Value> result = V8ScriptRunner::compileAndRunInternalScript(codeExternalString, isolate(), m_sourceURL, m_position);
172 if (result.IsEmpty())
175 // Call the outer function to get the inner function.
176 ASSERT(result->IsFunction());
177 v8::Local<v8::Function> intermediateFunction = result.As<v8::Function>();
179 HTMLFormElement* formElement = 0;
180 if (m_node && m_node->isHTMLElement())
181 formElement = toHTMLElement(m_node)->formOwner();
183 v8::Handle<v8::Object> nodeWrapper = toObjectWrapper<Node>(m_node, scriptState());
184 v8::Handle<v8::Object> formWrapper = toObjectWrapper<HTMLFormElement>(formElement, scriptState());
185 v8::Handle<v8::Object> documentWrapper = toObjectWrapper<Document>(m_node ? m_node->ownerDocument() : 0, scriptState());
187 v8::Local<v8::Object> thisObject = v8::Object::New(isolate());
188 if (thisObject.IsEmpty())
190 if (!thisObject->ForceSet(v8::Integer::New(isolate(), 0), nodeWrapper))
192 if (!thisObject->ForceSet(v8::Integer::New(isolate(), 1), formWrapper))
194 if (!thisObject->ForceSet(v8::Integer::New(isolate(), 2), documentWrapper))
197 // FIXME: Remove this code when we stop doing the 'with' hack above.
198 v8::Local<v8::Value> innerValue = V8ScriptRunner::callInternalFunction(intermediateFunction, thisObject, 0, 0, isolate());
199 if (innerValue.IsEmpty() || !innerValue->IsFunction())
202 v8::Local<v8::Function> wrappedFunction = innerValue.As<v8::Function>();
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
212 v8::Local<v8::Function> toStringFunction = v8::Function::New(isolate(), V8LazyEventListenerToString);
213 ASSERT(!toStringFunction.IsEmpty());
214 String toStringString = "function " + m_functionName + "(" + m_eventParameterName + ") {\n " + m_code + "\n}";
215 V8HiddenValue::setHiddenValue(isolate(), wrappedFunction, V8HiddenValue::toStringString(isolate()), v8String(isolate(), toStringString));
216 wrappedFunction->Set(v8AtomicString(isolate(), "toString"), toStringFunction);
217 wrappedFunction->SetName(v8String(isolate(), m_functionName));
219 // FIXME: Remove the following comment-outs.
220 // See https://bugs.webkit.org/show_bug.cgi?id=85152 for more details.
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 parsing around anymore.
225 // m_functionName = String();
226 // m_code = String();
227 // m_eventParameterName = String();
228 // m_sourceURL = String();
230 setListenerObject(wrappedFunction);