Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(76)

Side by Side Diff: third_party/WebKit/WebCore/bindings/v8/V8AbstractEventListener.cpp

Issue 46097: WebKit merge 41660:41709 (WebKit side).... (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: Created 11 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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, v8::Handle<v8::Value> jsEvent, bool isWindowEvent)
59 {
60 // 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
61 // existing "event" property, and then restore it after executing the javasc ript handler.
62 v8::Local<v8::String> eventSymbol = v8::String::NewSymbol("event");
63 v8::Local<v8::Value> returnValue;
64
65 {
66 // Catch exceptions thrown in the event handler so they do not propagate to javascript code that caused the event to fire.
67 // Setting and getting the 'event' property on the global object can thr ow exceptions as well (for instance if accessors that
68 // throw exceptions are defined for 'event' using __defineGetter__ and _ _defineSetter__ on the global object).
69 v8::TryCatch tryCatch;
70 tryCatch.SetVerbose(true);
71
72 // Save the old 'event' property so we can restore it later.
73 v8::Local<v8::Value> savedEvent = context->Global()->Get(eventSymbol);
74 tryCatch.Reset();
75
76 // Make the event available in the window object.
77 //
78 // 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
79 // access is intercepted correctly.
80 context->Global()->Set(eventSymbol, jsEvent);
81 tryCatch.Reset();
82
83 // Call the event handler.
84 returnValue = callListenerFunction(jsEvent, event, isWindowEvent);
85 tryCatch.Reset();
86
87 // Restore the old event. This must be done for all exit paths through t his method.
88 if (savedEvent.IsEmpty())
89 context->Global()->Set(eventSymbol, v8::Undefined());
90 else
91 context->Global()->Set(eventSymbol, savedEvent);
92 tryCatch.Reset();
93 }
94
95 ASSERT(!V8Proxy::HandleOutOfMemory() || returnValue.IsEmpty());
96
97 if (returnValue.IsEmpty())
98 return;
99
100 if (!returnValue->IsNull() && !returnValue->IsUndefined() && event->storesRe sultAsString())
101 event->storeResult(toWebCoreString(returnValue));
102
103 // Prevent default action if the return value is false;
104 // FIXME: Add example, and reference to bug entry.
105 if (m_isInline && returnValue->IsBoolean() && !returnValue->BooleanValue())
106 event->preventDefault();
107 }
108
109 void V8AbstractEventListener::handleEvent(Event* event, bool isWindowEvent)
110 {
111 // EventListener could be disconnected from the frame.
112 if (disconnected())
113 return;
114
115 // The callback function on XMLHttpRequest can clear the event listener and destroys 'this' object. Keep a local reference to it.
116 // See issue 889829.
117 RefPtr<V8AbstractEventListener> protect(this);
118
119 v8::HandleScope handleScope;
120
121 v8::Handle<v8::Context> context = V8Proxy::GetContext(m_frame);
122 if (context.IsEmpty())
123 return;
124
125 // m_frame can removed by the callback function, protect it until the callba ck function returns.
126 RefPtr<Frame> protectFrame(m_frame);
127
128 // Enter the V8 context in which to perform the event handling.
129 v8::Context::Scope scope(context);
130
131 // Get the V8 wrapper for the event object.
132 v8::Handle<v8::Value> jsEvent = V8Proxy::EventToV8Object(event);
133
134 invokeEventHandler(context, event, jsEvent, 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
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698