1fa7bb969fa04e0318c10e759b6fda85789b9f30
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / 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 "bindings/v8/DOMWrapperWorld.h"
35 #include "bindings/v8/ScopedPersistent.h"
36 #include "bindings/v8/V8Utilities.h"
37 #include "core/events/EventListener.h"
38 #include <v8.h>
39 #include "wtf/PassRefPtr.h"
40 #include "wtf/RefCounted.h"
41
42 namespace WebCore {
43
44     class Event;
45
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.
50     //
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 {
55     public:
56         virtual ~V8AbstractEventListener();
57
58         static const V8AbstractEventListener* cast(const EventListener* listener)
59         {
60             return listener->type() == JSEventListenerType
61                 ? static_cast<const V8AbstractEventListener*>(listener)
62                 : 0;
63         }
64
65         static V8AbstractEventListener* cast(EventListener* listener)
66         {
67             return const_cast<V8AbstractEventListener*>(cast(const_cast<const EventListener*>(listener)));
68         }
69
70         // Implementation of EventListener interface.
71
72         virtual bool operator==(const EventListener& other) OVERRIDE { return this == &other; }
73
74         virtual void handleEvent(ExecutionContext*, Event*) OVERRIDE;
75
76         virtual bool isLazy() const { return false; }
77
78         // Returns the listener object, either a function or an object.
79         v8::Local<v8::Object> getListenerObject(ExecutionContext* context)
80         {
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);
88         }
89
90         v8::Local<v8::Object> getExistingListenerObject()
91         {
92             return m_listener.newLocal(m_isolate);
93         }
94
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()
98         {
99             return m_listener.getUnsafe();
100         }
101
102         bool hasExistingListenerObject()
103         {
104             return !m_listener.isEmpty();
105         }
106
107         void clearListenerObject()
108         {
109             m_listener.clear();
110         }
111
112         virtual DOMWrapperWorld* world() const OVERRIDE FINAL { return m_world.get(); }
113         v8::Isolate* isolate() const { return m_isolate; }
114
115     protected:
116         V8AbstractEventListener(bool isAttribute, PassRefPtr<DOMWrapperWorld>, v8::Isolate*);
117
118         virtual void prepareListenerObject(ExecutionContext*) { }
119
120         void setListenerObject(v8::Handle<v8::Object> listener);
121
122         void invokeEventHandler(ExecutionContext*, Event*, v8::Local<v8::Value> jsEvent);
123
124         // Get the receiver object to use for event listener call.
125         v8::Local<v8::Object> getReceiverObject(ExecutionContext*, Event*);
126
127     private:
128         // Implementation of EventListener function.
129         virtual bool virtualisAttribute() const OVERRIDE { return m_isAttribute; }
130
131         virtual v8::Local<v8::Value> callListenerFunction(ExecutionContext*, v8::Handle<v8::Value> jsevent, Event*) = 0;
132
133         virtual bool shouldPreventDefault(v8::Local<v8::Value> returnValue);
134
135         static void setWeakCallback(const v8::WeakCallbackData<v8::Object, V8AbstractEventListener>&);
136
137         ScopedPersistent<v8::Object> m_listener;
138
139         // Indicates if this is an HTML type listener.
140         bool m_isAttribute;
141
142         RefPtr<DOMWrapperWorld> m_world;
143         v8::Isolate* m_isolate;
144     };
145
146 } // namespace WebCore
147
148 #endif // V8AbstractEventListener_h