• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "bindings/v8/DOMWrapperWorld.h"
35 #include "bindings/v8/ScopedPersistent.h"
36 #include "core/events/EventListener.h"
37 #include <v8.h>
38 #include "wtf/PassRefPtr.h"
39 #include "wtf/RefCounted.h"
40 
41 namespace WebCore {
42 
43     class Event;
44 
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.
49     //
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 {
54     public:
55         virtual ~V8AbstractEventListener();
56 
cast(const EventListener * listener)57         static const V8AbstractEventListener* cast(const EventListener* listener)
58         {
59             return listener->type() == JSEventListenerType
60                 ? static_cast<const V8AbstractEventListener*>(listener)
61                 : 0;
62         }
63 
cast(EventListener * listener)64         static V8AbstractEventListener* cast(EventListener* listener)
65         {
66             return const_cast<V8AbstractEventListener*>(cast(const_cast<const EventListener*>(listener)));
67         }
68 
69         // Implementation of EventListener interface.
70 
71         virtual bool operator==(const EventListener& other) OVERRIDE { return this == &other; }
72 
73         virtual void handleEvent(ExecutionContext*, Event*) OVERRIDE;
74 
isLazy()75         virtual bool isLazy() const { return false; }
76 
77         // Returns the listener object, either a function or an object.
getListenerObject(ExecutionContext * context)78         v8::Local<v8::Object> getListenerObject(ExecutionContext* context)
79         {
80             // prepareListenerObject can potentially deref this event listener
81             // as it may attempt to compile a function (lazy event listener), get an error
82             // and invoke onerror callback which can execute arbitrary JS code.
83             // Protect this event listener to keep it alive.
84             RefPtr<V8AbstractEventListener> guard(this);
85             prepareListenerObject(context);
86             return m_listener.newLocal(m_isolate);
87         }
88 
getExistingListenerObject()89         v8::Local<v8::Object> getExistingListenerObject()
90         {
91             return m_listener.newLocal(m_isolate);
92         }
93 
94         // Provides access to the underlying handle for GC. Returned
95         // value is a weak handle and so not guaranteed to stay alive.
existingListenerObjectPersistentHandle()96         v8::Persistent<v8::Object>& existingListenerObjectPersistentHandle()
97         {
98             return m_listener.getUnsafe();
99         }
100 
hasExistingListenerObject()101         bool hasExistingListenerObject()
102         {
103             return !m_listener.isEmpty();
104         }
105 
clearListenerObject()106         void clearListenerObject()
107         {
108             m_listener.clear();
109         }
110 
111         virtual bool belongsToTheCurrentWorld() const OVERRIDE FINAL;
isolate()112         v8::Isolate* isolate() const { return m_isolate; }
world()113         virtual DOMWrapperWorld& world() const { return scriptState()->world(); }
scriptState()114         ScriptState* scriptState() const
115         {
116             ASSERT(m_scriptState);
117             return m_scriptState.get();
118         }
setScriptState(ScriptState * scriptState)119         void setScriptState(ScriptState* scriptState) { m_scriptState = scriptState; }
120 
121     protected:
122         V8AbstractEventListener(bool isAttribute, ScriptState*);
123         V8AbstractEventListener(bool isAttribute, v8::Isolate*);
124 
prepareListenerObject(ExecutionContext *)125         virtual void prepareListenerObject(ExecutionContext*) { }
126 
127         void setListenerObject(v8::Handle<v8::Object> listener);
128 
129         void invokeEventHandler(Event*, v8::Local<v8::Value> jsEvent);
130 
131         // Get the receiver object to use for event listener call.
132         v8::Local<v8::Object> getReceiverObject(Event*);
133 
134     private:
135         // Implementation of EventListener function.
virtualisAttribute()136         virtual bool virtualisAttribute() const OVERRIDE { return m_isAttribute; }
137 
138         virtual v8::Local<v8::Value> callListenerFunction(v8::Handle<v8::Value> jsevent, Event*) = 0;
139 
140         virtual bool shouldPreventDefault(v8::Local<v8::Value> returnValue);
141 
142         static void setWeakCallback(const v8::WeakCallbackData<v8::Object, V8AbstractEventListener>&);
143 
144         ScopedPersistent<v8::Object> m_listener;
145 
146         // Indicates if this is an HTML type listener.
147         bool m_isAttribute;
148 
149         // For V8LazyEventListener, m_scriptState can be 0 until V8LazyEventListener is actually used.
150         // m_scriptState is set lazily because V8LazyEventListener doesn't know the associated frame
151         // until the listener is actually used.
152         RefPtr<ScriptState> m_scriptState;
153         v8::Isolate* m_isolate;
154     };
155 
156 } // namespace WebCore
157 
158 #endif // V8AbstractEventListener_h
159