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 #ifndef V8AbstractEventListener_h 32 #define V8AbstractEventListener_h 33 34 #include "EventListener.h" 35 #include "WorldContextHandle.h" 36 37 #include <v8.h> 38 #include <wtf/PassRefPtr.h> 39 #include <wtf/RefCounted.h> 40 41 namespace WebCore { 42 43 class Event; 44 class Frame; 45 class V8Proxy; 46 47 // There are two kinds of event listeners: HTML or non-HMTL. onload, 48 // onfocus, etc (attributes) are always HTML event handler type; Event 49 // listeners added by Window.addEventListener or 50 // EventTargetNode::addEventListener are non-HTML type. 51 // 52 // Why does this matter? 53 // WebKit does not allow duplicated HTML event handlers of the same type, 54 // but ALLOWs duplicated non-HTML event handlers. 55 class V8AbstractEventListener : public EventListener { 56 public: 57 virtual ~V8AbstractEventListener(); 58 59 static const V8AbstractEventListener* cast(const EventListener* listener) 60 { 61 return listener->type() == JSEventListenerType 62 ? static_cast<const V8AbstractEventListener*>(listener) 63 : 0; 64 } 65 66 static V8AbstractEventListener* cast(EventListener* listener) 67 { 68 return const_cast<V8AbstractEventListener*>(cast(const_cast<const EventListener*>(listener))); 69 } 70 71 // Implementation of EventListener interface. 72 73 virtual bool operator==(const EventListener& other) { return this == &other; } 74 75 virtual void handleEvent(ScriptExecutionContext*, Event*); 76 77 virtual bool isLazy() const { return false; } 78 79 // Returns the listener object, either a function or an object. 80 v8::Local<v8::Object> getListenerObject(ScriptExecutionContext* context) 81 { 82 prepareListenerObject(context); 83 return v8::Local<v8::Object>::New(m_listener); 84 } 85 86 v8::Local<v8::Object> getExistingListenerObject() 87 { 88 return v8::Local<v8::Object>::New(m_listener); 89 } 90 91 bool hasExistingListenerObject() 92 { 93 return !m_listener.IsEmpty(); 94 } 95 96 // Dispose listener object and clear the handle. 97 void disposeListenerObject(); 98 99 const WorldContextHandle& worldContext() const { return m_worldContext; } 100 101 protected: 102 V8AbstractEventListener(bool isAttribute, const WorldContextHandle& worldContext); 103 104 virtual void prepareListenerObject(ScriptExecutionContext*) { } 105 106 void setListenerObject(v8::Handle<v8::Object> listener); 107 108 void invokeEventHandler(ScriptExecutionContext*, Event*, v8::Handle<v8::Value> jsEvent); 109 110 // Get the receiver object to use for event listener call. 111 v8::Local<v8::Object> getReceiverObject(Event*); 112 113 private: 114 // Implementation of EventListener function. 115 virtual bool virtualisAttribute() const { return m_isAttribute; } 116 117 virtual v8::Local<v8::Value> callListenerFunction(ScriptExecutionContext*, v8::Handle<v8::Value> jsevent, Event*) = 0; 118 119 v8::Persistent<v8::Object> m_listener; 120 121 // Indicates if the above handle is weak. 122 bool m_isWeak; 123 124 // Indicates if this is an HTML type listener. 125 bool m_isAttribute; 126 127 WorldContextHandle m_worldContext; 128 }; 129 130 } // namespace WebCore 131 132 #endif // V8AbstractEventListener_h 133