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/core/v8/DOMWrapperWorld.h"
35 #include "bindings/core/v8/ScopedPersistent.h"
36 #include "core/events/EventListener.h"
37 #include "wtf/PassRefPtr.h"
38 #include "wtf/RefCounted.h"
45 // There are two kinds of event listeners: HTML or non-HMTL. onload,
46 // onfocus, etc (attributes) are always HTML event handler type; Event
47 // listeners added by Window.addEventListener or
48 // EventTargetNode::addEventListener are non-HTML type.
50 // Why does this matter?
51 // WebKit does not allow duplicated HTML event handlers of the same type,
52 // but ALLOWs duplicated non-HTML event handlers.
53 class V8AbstractEventListener : public EventListener {
55 virtual ~V8AbstractEventListener();
57 static const V8AbstractEventListener* cast(const EventListener* listener)
59 return listener->type() == JSEventListenerType
60 ? static_cast<const V8AbstractEventListener*>(listener)
64 static V8AbstractEventListener* cast(EventListener* listener)
66 return const_cast<V8AbstractEventListener*>(cast(const_cast<const EventListener*>(listener)));
69 // Implementation of EventListener interface.
71 virtual bool operator==(const EventListener& other) override { return this == &other; }
73 virtual void handleEvent(ExecutionContext*, Event*) override;
75 // Returns the listener object, either a function or an object.
76 v8::Local<v8::Object> getListenerObject(ExecutionContext* context)
78 // prepareListenerObject can potentially deref this event listener
79 // as it may attempt to compile a function (lazy event listener), get an error
80 // and invoke onerror callback which can execute arbitrary JS code.
81 // Protect this event listener to keep it alive.
82 RefPtr<V8AbstractEventListener> guard(this);
83 prepareListenerObject(context);
84 return m_listener.newLocal(m_isolate);
87 v8::Local<v8::Object> getExistingListenerObject()
89 return m_listener.newLocal(m_isolate);
92 // Provides access to the underlying handle for GC. Returned
93 // value is a weak handle and so not guaranteed to stay alive.
94 v8::Persistent<v8::Object>& existingListenerObjectPersistentHandle()
96 return m_listener.getUnsafe();
99 bool hasExistingListenerObject()
101 return !m_listener.isEmpty();
104 void clearListenerObject()
109 virtual bool belongsToTheCurrentWorld() const override final;
110 v8::Isolate* isolate() const { return m_isolate; }
111 virtual DOMWrapperWorld& world() const { return scriptState()->world(); }
112 ScriptState* scriptState() const
114 ASSERT(m_scriptState);
115 return m_scriptState.get();
117 void setScriptState(ScriptState* scriptState) { m_scriptState = scriptState; }
120 V8AbstractEventListener(bool isAttribute, ScriptState*);
121 V8AbstractEventListener(bool isAttribute, v8::Isolate*);
123 virtual void prepareListenerObject(ExecutionContext*) { }
125 void setListenerObject(v8::Handle<v8::Object>);
127 void invokeEventHandler(Event*, v8::Local<v8::Value> jsEvent);
129 // Get the receiver object to use for event listener call.
130 v8::Local<v8::Object> getReceiverObject(Event*);
133 // Implementation of EventListener function.
134 virtual bool virtualisAttribute() const override { return m_isAttribute; }
136 virtual v8::Local<v8::Value> callListenerFunction(v8::Handle<v8::Value> jsevent, Event*) = 0;
138 virtual bool shouldPreventDefault(v8::Local<v8::Value> returnValue);
140 static void setWeakCallback(const v8::WeakCallbackData<v8::Object, V8AbstractEventListener>&);
142 ScopedPersistent<v8::Object> m_listener;
144 // Indicates if this is an HTML type listener.
147 // For V8LazyEventListener, m_scriptState can be 0 until V8LazyEventListener is actually used.
148 // m_scriptState is set lazily because V8LazyEventListener doesn't know the associated frame
149 // until the listener is actually used.
150 RefPtr<ScriptState> m_scriptState;
151 v8::Isolate* m_isolate;
156 #endif // V8AbstractEventListener_h