initial import
[vuplus_webkit] / Source / WebCore / bindings / v8 / V8AbstractEventListener.h
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         // Provides access to the underlying handle for GC. Returned
92         // value is a weak handle and so not guaranteed to stay alive.
93         v8::Persistent<v8::Object> existingListenerObjectPersistentHandle()
94         {
95             return m_listener;
96         }
97
98         bool hasExistingListenerObject()
99         {
100             return !m_listener.IsEmpty();
101         }
102
103         // Dispose listener object and clear the handle.
104         void disposeListenerObject();
105
106         const WorldContextHandle& worldContext() const { return m_worldContext; }
107
108     protected:
109         V8AbstractEventListener(bool isAttribute, const WorldContextHandle& worldContext);
110
111         virtual void prepareListenerObject(ScriptExecutionContext*) { }
112
113         void setListenerObject(v8::Handle<v8::Object> listener);
114
115         void invokeEventHandler(ScriptExecutionContext*, Event*, v8::Handle<v8::Value> jsEvent);
116
117         // Get the receiver object to use for event listener call.
118         v8::Local<v8::Object> getReceiverObject(Event*);
119
120     private:
121         // Implementation of EventListener function.
122         virtual bool virtualisAttribute() const { return m_isAttribute; }
123
124         virtual v8::Local<v8::Value> callListenerFunction(ScriptExecutionContext*, v8::Handle<v8::Value> jsevent, Event*) = 0;
125
126         virtual bool shouldPreventDefault(v8::Local<v8::Value> returnValue);
127
128         v8::Persistent<v8::Object> m_listener;
129
130         // Indicates if this is an HTML type listener.
131         bool m_isAttribute;
132
133         WorldContextHandle m_worldContext;
134     };
135
136 } // namespace WebCore
137
138 #endif // V8AbstractEventListener_h