OLD | NEW |
| (Empty) |
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 #include "config.h" | |
32 #include "V8AbstractEventListener.h" | |
33 | |
34 #include "Document.h" | |
35 #include "Event.h" | |
36 #include "Frame.h" | |
37 #include "Tokenizer.h" | |
38 #include "V8Binding.h" | |
39 | |
40 namespace WebCore { | |
41 | |
42 V8AbstractEventListener::V8AbstractEventListener(Frame* frame, bool isInline) | |
43 : m_isInline(isInline) | |
44 , m_frame(frame) | |
45 , m_lineNumber(0) | |
46 , m_columnNumber(0) | |
47 { | |
48 if (!m_frame) | |
49 return; | |
50 | |
51 // Get the position in the source if any. | |
52 if (m_isInline && m_frame->document()->tokenizer()) { | |
53 m_lineNumber = m_frame->document()->tokenizer()->lineNumber(); | |
54 m_columnNumber = m_frame->document()->tokenizer()->columnNumber(); | |
55 } | |
56 } | |
57 | |
58 void V8AbstractEventListener::invokeEventHandler(v8::Handle<v8::Context> context
, Event* event, bool isWindowEvent) | |
59 { | |
60 // Enter the V8 context in which to perform the event handling. | |
61 v8::Context::Scope scope(context); | |
62 | |
63 // Get the V8 wrapper for the event object. | |
64 v8::Handle<v8::Value> jsEvent = V8Proxy::EventToV8Object(event); | |
65 | |
66 // For compatibility, we store the event object as a property on the window
called "event". Because this is the global namespace, we save away any | |
67 // existing "event" property, and then restore it after executing the javasc
ript handler. | |
68 v8::Local<v8::String> eventSymbol = v8::String::NewSymbol("event"); | |
69 v8::Local<v8::Value> returnValue; | |
70 | |
71 { | |
72 // Catch exceptions thrown in the event handler so they do not propagate
to javascript code that caused the event to fire. | |
73 // Setting and getting the 'event' property on the global object can thr
ow exceptions as well (for instance if accessors that | |
74 // throw exceptions are defined for 'event' using __defineGetter__ and _
_defineSetter__ on the global object). | |
75 v8::TryCatch tryCatch; | |
76 tryCatch.SetVerbose(true); | |
77 | |
78 // Save the old 'event' property so we can restore it later. | |
79 v8::Local<v8::Value> savedEvent = context->Global()->Get(eventSymbol); | |
80 tryCatch.Reset(); | |
81 | |
82 // Make the event available in the window object. | |
83 // | |
84 // FIXME: This does not work as it does with jsc bindings if the window.
event property is already set. We need to make sure that property | |
85 // access is intercepted correctly. | |
86 context->Global()->Set(eventSymbol, jsEvent); | |
87 tryCatch.Reset(); | |
88 | |
89 // Call the event handler. | |
90 returnValue = callListenerFunction(jsEvent, event, isWindowEvent); | |
91 tryCatch.Reset(); | |
92 | |
93 // Restore the old event. This must be done for all exit paths through t
his method. | |
94 if (savedEvent.IsEmpty()) | |
95 context->Global()->Set(eventSymbol, v8::Undefined()); | |
96 else | |
97 context->Global()->Set(eventSymbol, savedEvent); | |
98 tryCatch.Reset(); | |
99 } | |
100 | |
101 ASSERT(!V8Proxy::HandleOutOfMemory() || returnValue.IsEmpty()); | |
102 | |
103 if (returnValue.IsEmpty()) | |
104 return; | |
105 | |
106 if (!returnValue->IsNull() && !returnValue->IsUndefined() && event->storesRe
sultAsString()) | |
107 event->storeResult(toWebCoreString(returnValue)); | |
108 | |
109 // Prevent default action if the return value is false; | |
110 // FIXME: Add example, and reference to bug entry. | |
111 if (m_isInline && returnValue->IsBoolean() && !returnValue->BooleanValue()) | |
112 event->preventDefault(); | |
113 } | |
114 | |
115 void V8AbstractEventListener::handleEvent(Event* event, bool isWindowEvent) | |
116 { | |
117 // EventListener could be disconnected from the frame. | |
118 if (disconnected()) | |
119 return; | |
120 | |
121 // The callback function on XMLHttpRequest can clear the event listener and
destroys 'this' object. Keep a local reference to it. | |
122 // See issue 889829. | |
123 RefPtr<V8AbstractEventListener> protect(this); | |
124 | |
125 v8::HandleScope handleScope; | |
126 | |
127 v8::Handle<v8::Context> context = V8Proxy::GetContext(m_frame); | |
128 if (context.IsEmpty()) | |
129 return; | |
130 | |
131 // m_frame can removed by the callback function, protect it until the callba
ck function returns. | |
132 RefPtr<Frame> protectFrame(m_frame); | |
133 | |
134 invokeEventHandler(context, event, isWindowEvent); | |
135 | |
136 Document::updateDocumentsRendering(); | |
137 } | |
138 | |
139 void V8AbstractEventListener::disposeListenerObject() | |
140 { | |
141 if (!m_listener.IsEmpty()) { | |
142 #ifndef NDEBUG | |
143 V8Proxy::UnregisterGlobalHandle(this, m_listener); | |
144 #endif | |
145 m_listener.Dispose(); | |
146 m_listener.Clear(); | |
147 } | |
148 } | |
149 | |
150 v8::Local<v8::Object> V8AbstractEventListener::getReceiverObject(Event* event, b
ool isWindowEvent) | |
151 { | |
152 if (!m_listener.IsEmpty() && !m_listener->IsFunction()) | |
153 return v8::Local<v8::Object>::New(m_listener); | |
154 | |
155 if (isWindowEvent) | |
156 return v8::Context::GetCurrent()->Global(); | |
157 | |
158 EventTarget* target = event->currentTarget(); | |
159 v8::Handle<v8::Value> value = V8Proxy::EventTargetToV8Object(target); | |
160 if (value.IsEmpty()) | |
161 return v8::Local<v8::Object>(); | |
162 return v8::Local<v8::Object>::New(v8::Handle<v8::Object>::Cast(value)); | |
163 } | |
164 | |
165 } // namespace WebCore | |
OLD | NEW |