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/V8AbstractEventListener.h" 33 34 #include "V8Event.h" 35 #include "V8EventTarget.h" 36 #include "bindings/v8/DateExtension.h" 37 #include "bindings/v8/V8Binding.h" 38 #include "bindings/v8/V8EventListenerList.h" 39 #include "bindings/v8/V8HiddenPropertyName.h" 40 #include "core/dom/Document.h" 41 #include "core/dom/Event.h" 42 #include "core/dom/EventNames.h" 43 #include "core/inspector/InspectorCounters.h" 44 #include "core/page/Frame.h" 45 #include "core/workers/WorkerGlobalScope.h" 46 47 namespace WebCore { 48 49 V8AbstractEventListener::V8AbstractEventListener(bool isAttribute, PassRefPtr<DOMWrapperWorld> world, v8::Isolate* isolate) 50 : EventListener(JSEventListenerType) 51 , m_isAttribute(isAttribute) 52 , m_world(world) 53 , m_isolate(isolate) 54 { 55 ThreadLocalInspectorCounters::current().incrementCounter(ThreadLocalInspectorCounters::JSEventListenerCounter); 56 } 57 58 V8AbstractEventListener::~V8AbstractEventListener() 59 { 60 if (!m_listener.isEmpty()) { 61 v8::HandleScope scope(m_isolate); 62 V8EventListenerList::clearWrapper(m_listener.newLocal(m_isolate), m_isAttribute); 63 } 64 ThreadLocalInspectorCounters::current().decrementCounter(ThreadLocalInspectorCounters::JSEventListenerCounter); 65 } 66 67 void V8AbstractEventListener::handleEvent(ScriptExecutionContext* context, Event* event) 68 { 69 // Don't reenter V8 if execution was terminated in this instance of V8. 70 if (context->isJSExecutionForbidden()) 71 return; 72 73 ASSERT(event); 74 75 // The callback function on XMLHttpRequest can clear the event listener and destroys 'this' object. Keep a local reference to it. 76 // See issue 889829. 77 RefPtr<V8AbstractEventListener> protect(this); 78 79 v8::HandleScope handleScope(m_isolate); 80 81 v8::Local<v8::Context> v8Context = toV8Context(context, world()); 82 if (v8Context.IsEmpty()) 83 return; 84 85 // Enter the V8 context in which to perform the event handling. 86 v8::Context::Scope scope(v8Context); 87 88 // Get the V8 wrapper for the event object. 89 v8::Isolate* isolate = v8Context->GetIsolate(); 90 v8::Handle<v8::Value> jsEvent = toV8(event, v8::Handle<v8::Object>(), isolate); 91 if (jsEvent.IsEmpty()) 92 return; 93 invokeEventHandler(context, event, v8::Local<v8::Value>::New(isolate, jsEvent)); 94 } 95 96 void V8AbstractEventListener::setListenerObject(v8::Handle<v8::Object> listener) 97 { 98 m_listener.set(m_isolate, listener); 99 m_listener.makeWeak(this, &makeWeakCallback); 100 } 101 102 void V8AbstractEventListener::invokeEventHandler(ScriptExecutionContext* context, Event* event, v8::Local<v8::Value> jsEvent) 103 { 104 // If jsEvent is empty, attempt to set it as a hidden value would crash v8. 105 if (jsEvent.IsEmpty()) 106 return; 107 108 v8::Local<v8::Context> v8Context = toV8Context(context, world()); 109 if (v8Context.IsEmpty()) 110 return; 111 112 // We push the event being processed into the global object, so that it can be exposed by DOMWindow's bindings. 113 v8::Handle<v8::String> eventSymbol = V8HiddenPropertyName::event(); 114 v8::Local<v8::Value> returnValue; 115 116 // In beforeunload/unload handlers, we want to avoid sleeps which do tight loops of calling Date.getTime(). 117 if (event->type() == eventNames().beforeunloadEvent || event->type() == eventNames().unloadEvent) 118 DateExtension::get()->setAllowSleep(false, v8Context->GetIsolate()); 119 120 { 121 // Catch exceptions thrown in the event handler so they do not propagate to javascript code that caused the event to fire. 122 v8::TryCatch tryCatch; 123 tryCatch.SetVerbose(true); 124 125 // Save the old 'event' property so we can restore it later. 126 v8::Local<v8::Value> savedEvent = v8Context->Global()->GetHiddenValue(eventSymbol); 127 tryCatch.Reset(); 128 129 // Make the event available in the global object, so DOMWindow can expose it. 130 v8Context->Global()->SetHiddenValue(eventSymbol, jsEvent); 131 tryCatch.Reset(); 132 133 returnValue = callListenerFunction(context, jsEvent, event); 134 if (tryCatch.HasCaught()) 135 event->target()->uncaughtExceptionInEventHandler(); 136 137 if (!tryCatch.CanContinue()) { // Result of TerminateExecution(). 138 if (context->isWorkerGlobalScope()) 139 toWorkerGlobalScope(context)->script()->forbidExecution(); 140 return; 141 } 142 tryCatch.Reset(); 143 144 // Restore the old event. This must be done for all exit paths through this method. 145 if (savedEvent.IsEmpty()) 146 v8Context->Global()->SetHiddenValue(eventSymbol, v8::Undefined()); 147 else 148 v8Context->Global()->SetHiddenValue(eventSymbol, savedEvent); 149 tryCatch.Reset(); 150 } 151 152 if (event->type() == eventNames().beforeunloadEvent || event->type() == eventNames().unloadEvent) 153 DateExtension::get()->setAllowSleep(true, v8Context->GetIsolate()); 154 155 ASSERT(!handleOutOfMemory() || returnValue.IsEmpty()); 156 157 if (returnValue.IsEmpty()) 158 return; 159 160 if (!returnValue->IsNull() && !returnValue->IsUndefined() && event->storesResultAsString()) 161 event->storeResult(toWebCoreString(returnValue)); 162 163 if (m_isAttribute && shouldPreventDefault(returnValue)) 164 event->preventDefault(); 165 } 166 167 bool V8AbstractEventListener::shouldPreventDefault(v8::Local<v8::Value> returnValue) 168 { 169 // Prevent default action if the return value is false in accord with the spec 170 // http://www.w3.org/TR/html5/webappapis.html#event-handler-attributes 171 return returnValue->IsBoolean() && !returnValue->BooleanValue(); 172 } 173 174 v8::Local<v8::Object> V8AbstractEventListener::getReceiverObject(ScriptExecutionContext* context, Event* event) 175 { 176 v8::Isolate* isolate = toV8Context(context, world())->GetIsolate(); 177 v8::Local<v8::Object> listener = m_listener.newLocal(isolate); 178 if (!m_listener.isEmpty() && !listener->IsFunction()) 179 return listener; 180 181 EventTarget* target = event->currentTarget(); 182 v8::Handle<v8::Value> value = toV8(target, v8::Handle<v8::Object>(), isolate); 183 if (value.IsEmpty()) 184 return v8::Local<v8::Object>(); 185 return v8::Local<v8::Object>::New(v8::Handle<v8::Object>::Cast(value)); 186 } 187 188 void V8AbstractEventListener::makeWeakCallback(v8::Isolate*, v8::Persistent<v8::Object>*, V8AbstractEventListener* listener) 189 { 190 listener->m_listener.clear(); 191 } 192 193 } // namespace WebCore 194