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/ScriptState.h" 36 #include "bindings/v8/V8AbstractEventListener.h" 37 #include "bindings/v8/V8Binding.h" 38 #include "bindings/v8/V8WindowShell.h" 39 #include "core/dom/Document.h" 40 #include "core/dom/DocumentParser.h" 41 #include "core/events/EventListener.h" 42 #include "core/frame/LocalFrame.h" 43 #include <v8.h> 44 45 namespace WebCore { 46 47 PassRefPtr<V8LazyEventListener> createAttributeEventListener(Node* node, const QualifiedName& name, const AtomicString& value, const AtomicString& eventParameterName) 48 { 49 ASSERT(node); 50 if (value.isNull()) 51 return nullptr; 52 53 // FIXME: Very strange: we initialize zero-based number with '1'. 54 TextPosition position(OrdinalNumber::fromZeroBasedInt(1), OrdinalNumber::first()); 55 String sourceURL; 56 57 v8::Isolate* isolate; 58 if (LocalFrame* frame = node->document().frame()) { 59 isolate = toIsolate(frame); 60 ScriptController& scriptController = frame->script(); 61 if (!scriptController.canExecuteScripts(AboutToExecuteScript)) 62 return nullptr; 63 position = scriptController.eventHandlerPosition(); 64 sourceURL = node->document().url().string(); 65 } else { 66 isolate = v8::Isolate::GetCurrent(); 67 } 68 69 return V8LazyEventListener::create(name.localName(), eventParameterName, value, sourceURL, position, node, isolate); 70 } 71 72 PassRefPtr<V8LazyEventListener> createAttributeEventListener(LocalFrame* frame, const QualifiedName& name, const AtomicString& value, const AtomicString& eventParameterName) 73 { 74 if (!frame) 75 return nullptr; 76 77 if (value.isNull()) 78 return nullptr; 79 80 ScriptController& scriptController = frame->script(); 81 if (!scriptController.canExecuteScripts(AboutToExecuteScript)) 82 return nullptr; 83 84 TextPosition position = scriptController.eventHandlerPosition(); 85 String sourceURL = frame->document()->url().string(); 86 87 return V8LazyEventListener::create(name.localName(), eventParameterName, value, sourceURL, position, 0, toIsolate(frame)); 88 } 89 90 static v8::Handle<v8::Function> eventListenerEffectiveFunction(v8::Isolate* isolate, v8::Handle<v8::Object> listenerObject) 91 { 92 v8::Handle<v8::Function> function; 93 if (listenerObject->IsFunction()) { 94 function = v8::Handle<v8::Function>::Cast(listenerObject); 95 } else if (listenerObject->IsObject()) { 96 // Try the "handleEvent" method (EventListener interface). 97 v8::Handle<v8::Value> property = listenerObject->Get(v8AtomicString(isolate, "handleEvent")); 98 if (property.IsEmpty() || !property->IsFunction()) { 99 // Fall back to the "constructor" property. 100 property = listenerObject->Get(v8AtomicString(isolate, "constructor")); 101 } 102 if (!property.IsEmpty() && property->IsFunction()) 103 function = v8::Handle<v8::Function>::Cast(property); 104 } 105 return function; 106 } 107 108 String eventListenerHandlerBody(Document* document, EventListener* listener) 109 { 110 if (listener->type() != EventListener::JSEventListenerType) 111 return ""; 112 113 v8::HandleScope scope(toIsolate(document)); 114 V8AbstractEventListener* v8Listener = static_cast<V8AbstractEventListener*>(listener); 115 v8::Handle<v8::Context> context = toV8Context(document, v8Listener->world()); 116 v8::Context::Scope contextScope(context); 117 v8::Handle<v8::Object> object = v8Listener->getListenerObject(document); 118 if (object.IsEmpty()) 119 return ""; 120 v8::Handle<v8::Function> function = eventListenerEffectiveFunction(scope.GetIsolate(), object); 121 if (function.IsEmpty()) 122 return ""; 123 124 TOSTRING_DEFAULT(V8StringResource<WithNullCheck>, functionString, function, ""); 125 return functionString; 126 } 127 128 ScriptValue eventListenerHandler(Document* document, EventListener* listener) 129 { 130 if (listener->type() != EventListener::JSEventListenerType) 131 return ScriptValue(); 132 133 v8::Isolate* isolate = toIsolate(document); 134 v8::HandleScope scope(isolate); 135 V8AbstractEventListener* v8Listener = static_cast<V8AbstractEventListener*>(listener); 136 v8::Handle<v8::Context> context = toV8Context(document, v8Listener->world()); 137 v8::Context::Scope contextScope(context); 138 v8::Handle<v8::Object> function = v8Listener->getListenerObject(document); 139 if (function.IsEmpty()) 140 return ScriptValue(); 141 return ScriptValue(ScriptState::from(context), function); 142 } 143 144 ScriptState* eventListenerHandlerScriptState(LocalFrame* frame, EventListener* listener) 145 { 146 if (listener->type() != EventListener::JSEventListenerType) 147 return 0; 148 V8AbstractEventListener* v8Listener = static_cast<V8AbstractEventListener*>(listener); 149 v8::HandleScope scope(toIsolate(frame)); 150 v8::Handle<v8::Context> v8Context = frame->script().windowShell(v8Listener->world())->context(); 151 return ScriptState::from(v8Context); 152 } 153 154 bool eventListenerHandlerLocation(Document* document, EventListener* listener, String& sourceName, String& scriptId, int& lineNumber) 155 { 156 if (listener->type() != EventListener::JSEventListenerType) 157 return false; 158 159 v8::HandleScope scope(toIsolate(document)); 160 V8AbstractEventListener* v8Listener = static_cast<V8AbstractEventListener*>(listener); 161 v8::Handle<v8::Context> context = toV8Context(document, v8Listener->world()); 162 v8::Context::Scope contextScope(context); 163 v8::Local<v8::Object> object = v8Listener->getListenerObject(document); 164 if (object.IsEmpty()) 165 return false; 166 v8::Handle<v8::Function> function = eventListenerEffectiveFunction(scope.GetIsolate(), object); 167 if (function.IsEmpty()) 168 return false; 169 v8::Handle<v8::Function> originalFunction = getBoundFunction(function); 170 int scriptIdValue = originalFunction->ScriptId(); 171 scriptId = String::number(scriptIdValue); 172 v8::ScriptOrigin origin = originalFunction->GetScriptOrigin(); 173 if (!origin.ResourceName().IsEmpty() && origin.ResourceName()->IsString()) 174 sourceName = toCoreString(origin.ResourceName().As<v8::String>()); 175 else 176 sourceName = ""; 177 lineNumber = originalFunction->GetScriptLineNumber(); 178 return true; 179 } 180 181 } // namespace WebCore 182