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.
31 #ifndef V8AbstractEventListener_h
32 #define V8AbstractEventListener_h
34 #include "bindings/v8/DOMWrapperWorld.h"
35 #include "bindings/v8/ScopedPersistent.h"
36 #include "bindings/v8/V8Utilities.h"
37 #include "core/events/EventListener.h"
39 #include "wtf/PassRefPtr.h"
40 #include "wtf/RefCounted.h"
46 // There are two kinds of event listeners: HTML or non-HMTL. onload,
47 // onfocus, etc (attributes) are always HTML event handler type; Event
48 // listeners added by Window.addEventListener or
49 // EventTargetNode::addEventListener are non-HTML type.
51 // Why does this matter?
52 // WebKit does not allow duplicated HTML event handlers of the same type,
53 // but ALLOWs duplicated non-HTML event handlers.
54 class V8AbstractEventListener : public EventListener {
56 virtual ~V8AbstractEventListener();
58 static const V8AbstractEventListener* cast(const EventListener* listener)
60 return listener->type() == JSEventListenerType
61 ? static_cast<const V8AbstractEventListener*>(listener)
65 static V8AbstractEventListener* cast(EventListener* listener)
67 return const_cast<V8AbstractEventListener*>(cast(const_cast<const EventListener*>(listener)));
70 // Implementation of EventListener interface.
72 virtual bool operator==(const EventListener& other) OVERRIDE { return this == &other; }
74 virtual void handleEvent(ExecutionContext*, Event*) OVERRIDE;
76 virtual bool isLazy() const { return false; }
78 // Returns the listener object, either a function or an object.
79 v8::Local<v8::Object> getListenerObject(ExecutionContext* context)
81 // prepareListenerObject can potentially deref this event listener
82 // as it may attempt to compile a function (lazy event listener), get an error
83 // and invoke onerror callback which can execute arbitrary JS code.
84 // Protect this event listener to keep it alive.
85 RefPtr<V8AbstractEventListener> guard(this);
86 prepareListenerObject(context);
87 return m_listener.newLocal(m_isolate);
90 v8::Local<v8::Object> getExistingListenerObject()
92 return m_listener.newLocal(m_isolate);
95 // Provides access to the underlying handle for GC. Returned
96 // value is a weak handle and so not guaranteed to stay alive.
97 v8::Persistent<v8::Object>& existingListenerObjectPersistentHandle()
99 return m_listener.getUnsafe();
102 bool hasExistingListenerObject()
104 return !m_listener.isEmpty();
107 void clearListenerObject()
112 virtual DOMWrapperWorld* world() const OVERRIDE FINAL { return m_world.get(); }
113 v8::Isolate* isolate() const { return m_isolate; }
116 V8AbstractEventListener(bool isAttribute, PassRefPtr<DOMWrapperWorld>, v8::Isolate*);
118 virtual void prepareListenerObject(ExecutionContext*) { }
120 void setListenerObject(v8::Handle<v8::Object> listener);
122 void invokeEventHandler(ExecutionContext*, Event*, v8::Local<v8::Value> jsEvent);
124 // Get the receiver object to use for event listener call.
125 v8::Local<v8::Object> getReceiverObject(ExecutionContext*, Event*);
128 // Implementation of EventListener function.
129 virtual bool virtualisAttribute() const OVERRIDE { return m_isAttribute; }
131 virtual v8::Local<v8::Value> callListenerFunction(ExecutionContext*, v8::Handle<v8::Value> jsevent, Event*) = 0;
133 virtual bool shouldPreventDefault(v8::Local<v8::Value> returnValue);
135 static void setWeakCallback(const v8::WeakCallbackData<v8::Object, V8AbstractEventListener>&);
137 ScopedPersistent<v8::Object> m_listener;
139 // Indicates if this is an HTML type listener.
142 RefPtr<DOMWrapperWorld> m_world;
143 v8::Isolate* m_isolate;
146 } // namespace WebCore
148 #endif // V8AbstractEventListener_h