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 "V8Document.h" 35 #include "V8HTMLFormElement.h" 36 #include "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/V8HiddenPropertyName.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/ContentSecurityPolicy.h" 49 #include "core/frame/Frame.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, const TextPosition& position, Node* node, v8::Isolate* isolate) 56 : V8AbstractEventListener(true, mainThreadNormalWorld(), 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, v8::Isolate* isolate) 68 { 69 if (!domObject) 70 return v8::Object::New(); 71 v8::Handle<v8::Value> value = toV8(domObject, v8::Handle<v8::Object>(), isolate); 72 if (value.IsEmpty()) 73 return v8::Object::New(); 74 return v8::Local<v8::Object>::New(isolate, value.As<v8::Object>()); 75 } 76 77 v8::Local<v8::Value> V8LazyEventListener::callListenerFunction(ExecutionContext* context, v8::Handle<v8::Value> jsEvent, Event* event) 78 { 79 v8::Local<v8::Object> listenerObject = getListenerObject(context); 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(context, event); 85 if (handlerFunction.IsEmpty() || receiver.IsEmpty()) 86 return v8::Local<v8::Value>(); 87 88 // FIXME: Can |context| be 0 here? 89 if (!context) 90 return v8::Local<v8::Value>(); 91 92 if (!context->isDocument()) 93 return v8::Local<v8::Value>(); 94 95 Frame* frame = toDocument(context)->frame(); 96 if (!frame) 97 return v8::Local<v8::Value>(); 98 99 if (!frame->script().canExecuteScripts(AboutToExecuteScript)) 100 return v8::Local<v8::Value>(); 101 102 v8::Handle<v8::Value> parameters[1] = { jsEvent }; 103 return frame->script().callFunction(handlerFunction, receiver, WTF_ARRAY_LENGTH(parameters), parameters); 104 } 105 106 static void V8LazyEventListenerToString(const v8::FunctionCallbackInfo<v8::Value>& info) 107 { 108 v8SetReturnValue(info, info.Holder()->GetHiddenValue(V8HiddenPropertyName::toStringString(info.GetIsolate()))); 109 } 110 111 void V8LazyEventListener::prepareListenerObject(ExecutionContext* context) 112 { 113 if (context->isDocument() && !toDocument(context)->allowInlineEventHandlers(m_node, this, m_sourceURL, m_position.m_line)) { 114 clearListenerObject(); 115 return; 116 } 117 118 if (hasExistingListenerObject()) 119 return; 120 121 ASSERT(context->isDocument()); 122 123 v8::Isolate* isolate = toIsolate(context); 124 v8::HandleScope handleScope(isolate); 125 126 // Use the outer scope to hold context. 127 v8::Local<v8::Context> v8Context = toV8Context(context, world()); 128 // Bail out if we cannot get the context. 129 if (v8Context.IsEmpty()) 130 return; 131 132 v8::Context::Scope scope(v8Context); 133 134 String listenerSource = InspectorInstrumentation::preprocessEventListener(toDocument(context)->frame(), m_code, m_sourceURL, m_functionName); 135 136 // FIXME: Remove the following 'with' hack. 137 // 138 // Nodes other than the document object, when executing inline event 139 // handlers push document, form owner, and the target node on the scope chain. 140 // We do this by using 'with' statement. 141 // See chrome/fast/forms/form-action.html 142 // chrome/fast/forms/selected-index-value.html 143 // base/fast/overflow/onscroll-layer-self-destruct.html 144 // 145 // Don't use new lines so that lines in the modified handler 146 // have the same numbers as in the original code. 147 // FIXME: V8 does not allow us to programmatically create object environments so 148 // we have to do this hack! What if m_code escapes to run arbitrary script? 149 // 150 // Call with 4 arguments instead of 3, pass additional null as the last parameter. 151 // By calling the function with 4 arguments, we create a setter on arguments object 152 // which would shadow property "3" on the prototype. 153 String code = "(function() {" 154 "with (this[2]) {" 155 "with (this[1]) {" 156 "with (this[0]) {" 157 "return function(" + m_eventParameterName + ") {" + 158 listenerSource + "\n" // Insert '\n' otherwise //-style comments could break the handler. 159 "};" 160 "}}}})"; 161 162 v8::Handle<v8::String> codeExternalString = v8String(isolate, code); 163 164 v8::Local<v8::Value> result = V8ScriptRunner::compileAndRunInternalScript(codeExternalString, isolate, m_sourceURL, m_position, 0); 165 if (result.IsEmpty()) 166 return; 167 168 // Call the outer function to get the inner function. 169 ASSERT(result->IsFunction()); 170 v8::Local<v8::Function> intermediateFunction = result.As<v8::Function>(); 171 172 HTMLFormElement* formElement = 0; 173 if (m_node && m_node->isHTMLElement()) 174 formElement = toHTMLElement(m_node)->formOwner(); 175 176 v8::Handle<v8::Object> nodeWrapper = toObjectWrapper<Node>(m_node, isolate); 177 v8::Handle<v8::Object> formWrapper = toObjectWrapper<HTMLFormElement>(formElement, isolate); 178 v8::Handle<v8::Object> documentWrapper = toObjectWrapper<Document>(m_node ? m_node->ownerDocument() : 0, isolate); 179 180 v8::Local<v8::Object> thisObject = v8::Object::New(); 181 if (thisObject.IsEmpty()) 182 return; 183 if (!thisObject->ForceSet(v8::Integer::New(0, isolate), nodeWrapper)) 184 return; 185 if (!thisObject->ForceSet(v8::Integer::New(1, isolate), formWrapper)) 186 return; 187 if (!thisObject->ForceSet(v8::Integer::New(2, isolate), documentWrapper)) 188 return; 189 190 // FIXME: Remove this code when we stop doing the 'with' hack above. 191 v8::Local<v8::Value> innerValue = V8ScriptRunner::callInternalFunction(intermediateFunction, thisObject, 0, 0, isolate); 192 if (innerValue.IsEmpty() || !innerValue->IsFunction()) 193 return; 194 195 v8::Local<v8::Function> wrappedFunction = innerValue.As<v8::Function>(); 196 197 // Change the toString function on the wrapper function to avoid it 198 // returning the source for the actual wrapper function. Instead it 199 // returns source for a clean wrapper function with the event 200 // argument wrapping the event source code. The reason for this is 201 // that some web sites use toString on event functions and eval the 202 // source returned (sometimes a RegExp is applied as well) for some 203 // other use. That fails miserably if the actual wrapper source is 204 // returned. 205 v8::Handle<v8::FunctionTemplate> toStringTemplate = 206 V8PerIsolateData::current()->lazyEventListenerToStringTemplate(); 207 if (toStringTemplate.IsEmpty()) 208 toStringTemplate = v8::FunctionTemplate::New(isolate, V8LazyEventListenerToString); 209 v8::Local<v8::Function> toStringFunction; 210 if (!toStringTemplate.IsEmpty()) 211 toStringFunction = toStringTemplate->GetFunction(); 212 if (!toStringFunction.IsEmpty()) { 213 String toStringString = "function " + m_functionName + "(" + m_eventParameterName + ") {\n " + m_code + "\n}"; 214 wrappedFunction->SetHiddenValue(V8HiddenPropertyName::toStringString(isolate), v8String(isolate, toStringString)); 215 wrappedFunction->Set(v8AtomicString(isolate, "toString"), toStringFunction); 216 } 217 218 wrappedFunction->SetName(v8String(isolate, m_functionName)); 219 220 // FIXME: Remove the following comment-outs. 221 // See https://bugs.webkit.org/show_bug.cgi?id=85152 for more details. 222 // 223 // For the time being, we comment out the following code since the 224 // second parsing can happen. 225 // // Since we only parse once, there's no need to keep data used for parsing around anymore. 226 // m_functionName = String(); 227 // m_code = String(); 228 // m_eventParameterName = String(); 229 // m_sourceURL = String(); 230 231 setListenerObject(wrappedFunction); 232 } 233 234 } // namespace WebCore 235