Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / bindings / core / 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/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"
39 #include <v8.h>
40
41 namespace blink {
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
57     static const V8AbstractEventListener* cast(const EventListener* listener)
58     {
59         return listener->type() == JSEventListenerType
60             ? static_cast<const V8AbstractEventListener*>(listener)
61             : 0;
62     }
63
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
75     // Returns the listener object, either a function or an object.
76     v8::Local<v8::Object> getListenerObject(ExecutionContext* context)
77     {
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);
85     }
86
87     v8::Local<v8::Object> getExistingListenerObject()
88     {
89         return m_listener.newLocal(m_isolate);
90     }
91
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()
95     {
96         return m_listener.getUnsafe();
97     }
98
99     bool hasExistingListenerObject()
100     {
101         return !m_listener.isEmpty();
102     }
103
104     void clearListenerObject()
105     {
106         m_listener.clear();
107     }
108
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
113     {
114         ASSERT(m_scriptState);
115         return m_scriptState.get();
116     }
117     void setScriptState(ScriptState* scriptState) { m_scriptState = scriptState; }
118
119 protected:
120     V8AbstractEventListener(bool isAttribute, ScriptState*);
121     V8AbstractEventListener(bool isAttribute, v8::Isolate*);
122
123     virtual void prepareListenerObject(ExecutionContext*) { }
124
125     void setListenerObject(v8::Handle<v8::Object>);
126
127     void invokeEventHandler(Event*, v8::Local<v8::Value> jsEvent);
128
129     // Get the receiver object to use for event listener call.
130     v8::Local<v8::Object> getReceiverObject(Event*);
131
132 private:
133     // Implementation of EventListener function.
134     virtual bool virtualisAttribute() const override { return m_isAttribute; }
135
136     virtual v8::Local<v8::Value> callListenerFunction(v8::Handle<v8::Value> jsevent, Event*) = 0;
137
138     virtual bool shouldPreventDefault(v8::Local<v8::Value> returnValue);
139
140     static void setWeakCallback(const v8::WeakCallbackData<v8::Object, V8AbstractEventListener>&);
141
142     ScopedPersistent<v8::Object> m_listener;
143
144     // Indicates if this is an HTML type listener.
145     bool m_isAttribute;
146
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;
152 };
153
154 } // namespace blink
155
156 #endif // V8AbstractEventListener_h