kjs_events.cpp 9.84 KB
Newer Older
kocienda's avatar
kocienda committed
1 2
/*
 *  Copyright (C) 2001 Peter Kelly (pmk@post.com)
weinig@apple.com's avatar
weinig@apple.com committed
3
 *  Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008 Apple Inc. All Rights Reserved.
kocienda's avatar
kocienda committed
4 5 6 7 8 9 10 11 12 13 14 15 16
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 2 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
ddkilzer's avatar
ddkilzer committed
17
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
kocienda's avatar
kocienda committed
18
 */
darin's avatar
darin committed
19

mjs's avatar
mjs committed
20
#include "config.h"
kocienda's avatar
kocienda committed
21
#include "kjs_events.h"
darin's avatar
darin committed
22

darin's avatar
darin committed
23
#include "CString.h"
24
#include "Console.h"
weinig's avatar
weinig committed
25
#include "DOMWindow.h"
darin's avatar
darin committed
26
#include "Document.h"
weinig's avatar
 
weinig committed
27
#include "Event.h"
darin's avatar
darin committed
28 29
#include "EventNames.h"
#include "Frame.h"
darin's avatar
darin committed
30
#include "FrameLoader.h"
weinig@apple.com's avatar
weinig@apple.com committed
31
#include "JSDOMWindow.h"
ggaren's avatar
ggaren committed
32
#include "JSEvent.h"
weinig's avatar
weinig committed
33
#include "JSEventTargetNode.h"
darin's avatar
darin committed
34
#include "kjs_proxy.h"
ggaren@apple.com's avatar
ggaren@apple.com committed
35
#include <kjs/function_object.h>
kocienda's avatar
kocienda committed
36

weinig@apple.com's avatar
weinig@apple.com committed
37 38
using namespace KJS;

weinig's avatar
weinig committed
39 40
namespace WebCore {

darin's avatar
darin committed
41
using namespace EventNames;
darin's avatar
darin committed
42

weinig's avatar
weinig committed
43 44
JSAbstractEventListener::JSAbstractEventListener(bool html)
    : m_html(html)
kocienda's avatar
kocienda committed
45 46 47
{
}

darin's avatar
darin committed
48
void JSAbstractEventListener::handleEvent(Event* ele, bool isWindowEvent)
kocienda's avatar
kocienda committed
49
{
weinig's avatar
weinig committed
50
    Event* event = ele;
51

darin's avatar
darin committed
52 53 54
    JSObject* listener = listenerObj();
    if (!listener)
        return;
mjs's avatar
mjs committed
55

56 57
    JSDOMWindow* window = this->window();
    // Null check as clearWindow() can clear this and we still get called back by
andrew's avatar
andrew committed
58
    // xmlhttprequest objects. See http://bugs.webkit.org/show_bug.cgi?id=13275
59
    if (!window)
andrew's avatar
andrew committed
60
        return;
61
    Frame* frame = window->impl()->frame();
mjs's avatar
mjs committed
62
    if (!frame)
darin's avatar
darin committed
63
        return;
64 65
    KJSProxy* scriptProxy = frame->scriptProxy();
    if (!scriptProxy->isEnabled() || scriptProxy->isPaused())
darin's avatar
darin committed
66
        return;
darin's avatar
darin committed
67

darin's avatar
darin committed
68
    JSLock lock;
weinig's avatar
weinig committed
69

70
    ExecState* exec = window->globalExec();
weinig's avatar
weinig committed
71

darin's avatar
darin committed
72 73
    JSValue* handleEventFuncValue = listener->get(exec, "handleEvent");
    JSObject* handleEventFunc = 0;
weinig's avatar
weinig committed
74
    if (handleEventFuncValue->isObject()) {
darin's avatar
darin committed
75 76 77 78
        handleEventFunc = static_cast<JSObject*>(handleEventFuncValue);
        if (!handleEventFunc->implementsCall())
            handleEventFunc = 0;
    }
weinig's avatar
weinig committed
79

darin's avatar
darin committed
80 81
    if (handleEventFunc || listener->implementsCall()) {
        ref();
weinig's avatar
weinig committed
82

darin's avatar
darin committed
83
        List args;
darin's avatar
darin committed
84
        args.append(toJS(exec, event));
weinig's avatar
weinig committed
85

86 87
        Event* savedEvent = window->currentEvent();
        window->setCurrentEvent(event);
weinig's avatar
weinig committed
88

darin's avatar
darin committed
89
        JSValue* retval;
andersca's avatar
andersca committed
90
        if (handleEventFunc) {
91
            window->startTimeoutCheck();
darin's avatar
darin committed
92
            retval = handleEventFunc->call(exec, listener, args);
andersca's avatar
andersca committed
93
        } else {
darin's avatar
darin committed
94 95
            JSObject* thisObj;
            if (isWindowEvent)
96
                thisObj = window->shell();
darin's avatar
darin committed
97
            else
darin's avatar
darin committed
98
                thisObj = static_cast<JSObject*>(toJS(exec, event->currentTarget()));
99
            window->startTimeoutCheck();
darin's avatar
darin committed
100 101
            retval = listener->call(exec, thisObj, args);
        }
102
        window->stopTimeoutCheck();
darin's avatar
darin committed
103

104
        window->setCurrentEvent(savedEvent);
darin's avatar
darin committed
105 106 107

        if (exec->hadException()) {
            JSObject* exception = exec->exception()->toObject(exec);
mjs's avatar
mjs committed
108
            String message = exception->get(exec, exec->propertyNames().message)->toString(exec);
darin's avatar
darin committed
109
            int lineNumber = exception->get(exec, "line")->toInt32(exec);
darin's avatar
darin committed
110
            String sourceURL = exception->get(exec, "sourceURL")->toString(exec);
darin's avatar
darin committed
111
            if (Interpreter::shouldPrintExceptions())
112
                printf("(event handler):%s\n", message.utf8().data());
113
            frame->domWindow()->console()->addMessage(JSMessageSource, ErrorMessageLevel, message, lineNumber, sourceURL);
darin's avatar
darin committed
114 115 116
            exec->clearException();
        } else {
            if (!retval->isUndefinedOrNull() && event->storesResultAsString())
darin's avatar
darin committed
117
                event->storeResult(retval->toString(exec));
weinig's avatar
weinig committed
118
            if (m_html) {
darin's avatar
darin committed
119 120
                bool retvalbool;
                if (retval->getBoolean(retvalbool) && !retvalbool)
darin's avatar
darin committed
121
                    event->preventDefault();
122
            }
darin's avatar
darin committed
123 124
        }

darin's avatar
darin committed
125
        Document::updateDocumentsRendering();
darin's avatar
darin committed
126 127
        deref();
    }
kocienda's avatar
kocienda committed
128 129
}

darin's avatar
darin committed
130
bool JSAbstractEventListener::isHTMLEventListener() const
kocienda's avatar
kocienda committed
131
{
weinig's avatar
weinig committed
132
    return m_html;
kocienda's avatar
kocienda committed
133 134
}

mjs's avatar
mjs committed
135 136
// -------------------------------------------------------------------------

137
JSUnprotectedEventListener::JSUnprotectedEventListener(JSObject* listener, JSDOMWindow* window, bool html)
weinig's avatar
weinig committed
138 139
    : JSAbstractEventListener(html)
    , m_listener(listener)
140
    , m_window(window)
mjs's avatar
mjs committed
141
{
weinig's avatar
weinig committed
142
    if (m_listener) {
weinig@apple.com's avatar
weinig@apple.com committed
143
        JSDOMWindow::UnprotectedListenersMap& listeners = html
144
            ? window->jsUnprotectedHTMLEventListeners() : window->jsUnprotectedEventListeners();
weinig's avatar
weinig committed
145
        listeners.set(m_listener, this);
darin's avatar
darin committed
146
    }
mjs's avatar
mjs committed
147 148 149 150
}

JSUnprotectedEventListener::~JSUnprotectedEventListener()
{
151
    if (m_listener && m_window) {
weinig@apple.com's avatar
weinig@apple.com committed
152
        JSDOMWindow::UnprotectedListenersMap& listeners = isHTMLEventListener()
153
            ? m_window->jsUnprotectedHTMLEventListeners() : m_window->jsUnprotectedEventListeners();
weinig's avatar
weinig committed
154
        listeners.remove(m_listener);
darin's avatar
darin committed
155
    }
mjs's avatar
mjs committed
156 157
}

darin's avatar
darin committed
158
JSObject* JSUnprotectedEventListener::listenerObj() const
weinig's avatar
weinig committed
159 160
{
    return m_listener;
mjs's avatar
mjs committed
161 162
}

163
JSDOMWindow* JSUnprotectedEventListener::window() const
mjs's avatar
mjs committed
164
{
165
    return m_window;
mjs's avatar
mjs committed
166 167
}

168
void JSUnprotectedEventListener::clearWindow()
sullivan's avatar
sullivan committed
169
{
170
    m_window = 0;
sullivan's avatar
sullivan committed
171 172
}

173
void JSUnprotectedEventListener::mark()
mjs's avatar
mjs committed
174
{
175 176
    if (m_listener && !m_listener->marked())
        m_listener->mark();
mjs's avatar
mjs committed
177 178
}

weinig's avatar
weinig committed
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
#ifndef NDEBUG
#ifndef LOG_CHANNEL_PREFIX
#define LOG_CHANNEL_PREFIX Log
#endif
WTFLogChannel LogWebCoreEventListenerLeaks = { 0x00000000, "", WTFLogChannelOn };

struct EventListenerCounter {
    static unsigned count;
    ~EventListenerCounter()
    {
        if (count)
            LOG(WebCoreEventListenerLeaks, "LEAK: %u EventListeners\n", count);
    }
};
unsigned EventListenerCounter::count = 0;
static EventListenerCounter eventListenerCounter;
#endif

mjs's avatar
mjs committed
197 198
// -------------------------------------------------------------------------

199
JSEventListener::JSEventListener(JSObject* listener, JSDOMWindow* window, bool html)
weinig's avatar
weinig committed
200 201
    : JSAbstractEventListener(html)
    , m_listener(listener)
202
    , m_window(window)
mjs's avatar
mjs committed
203
{
weinig's avatar
weinig committed
204
    if (m_listener) {
weinig@apple.com's avatar
weinig@apple.com committed
205
        JSDOMWindow::ListenersMap& listeners = html
206
            ? m_window->jsHTMLEventListeners() : m_window->jsEventListeners();
weinig's avatar
weinig committed
207
        listeners.set(m_listener, this);
darin's avatar
darin committed
208
    }
weinig's avatar
weinig committed
209 210 211
#ifndef NDEBUG
    ++eventListenerCounter.count;
#endif
mjs's avatar
mjs committed
212 213 214 215
}

JSEventListener::~JSEventListener()
{
216
    if (m_listener && m_window) {
weinig@apple.com's avatar
weinig@apple.com committed
217
        JSDOMWindow::ListenersMap& listeners = isHTMLEventListener()
218
            ? m_window->jsHTMLEventListeners() : m_window->jsEventListeners();
weinig's avatar
weinig committed
219
        listeners.remove(m_listener);
darin's avatar
darin committed
220
    }
weinig's avatar
weinig committed
221 222 223
#ifndef NDEBUG
    --eventListenerCounter.count;
#endif
mjs's avatar
mjs committed
224
}
mjs's avatar
mjs committed
225

darin's avatar
darin committed
226
JSObject* JSEventListener::listenerObj() const
weinig's avatar
weinig committed
227 228
{
    return m_listener;
mjs's avatar
mjs committed
229 230
}

231
JSDOMWindow* JSEventListener::window() const
mjs's avatar
mjs committed
232
{
233
    return m_window;
mjs's avatar
mjs committed
234 235
}

236
void JSEventListener::clearWindow()
sullivan's avatar
sullivan committed
237
{
238
    m_window = 0;
sullivan's avatar
sullivan committed
239 240
}

mjs's avatar
mjs committed
241 242
// -------------------------------------------------------------------------

243 244
JSLazyEventListener::JSLazyEventListener(const String& functionName, const String& code, JSDOMWindow* window, Node* node, int lineNumber)
    : JSEventListener(0, window, true)
weinig's avatar
weinig committed
245 246 247 248 249
    , m_functionName(functionName)
    , m_code(code)
    , m_parsed(false)
    , m_lineNumber(lineNumber)
    , m_originalNode(node)
mjs's avatar
mjs committed
250
{
darin's avatar
darin committed
251
    // We don't retain the original node because we assume it
mjs's avatar
mjs committed
252 253 254 255
    // will stay alive as long as this handler object is around
    // and we need to avoid a reference cycle. If JS transfers
    // this handler to another node, parseCode will be called and
    // then originalNode is no longer needed.
mjs's avatar
mjs committed
256 257
}

darin's avatar
darin committed
258
JSObject* JSLazyEventListener::listenerObj() const
mjs's avatar
mjs committed
259
{
darin's avatar
darin committed
260
    parseCode();
weinig's avatar
weinig committed
261
    return m_listener;
mjs's avatar
mjs committed
262 263
}

darin's avatar
darin committed
264
JSValue* JSLazyEventListener::eventParameterName() const
eseidel's avatar
eseidel committed
265
{
266
    static ProtectedPtr<JSValue> eventString = jsString("event");
eseidel's avatar
eseidel committed
267 268 269
    return eventString.get();
}

mjs's avatar
mjs committed
270 271
void JSLazyEventListener::parseCode() const
{
weinig's avatar
weinig committed
272
    if (m_parsed)
darin's avatar
darin committed
273
        return;
weinig's avatar
weinig committed
274
    m_parsed = true;
darin's avatar
darin committed
275

276
    Frame* frame = window()->impl()->frame();
alice.liu@apple.com's avatar
alice.liu@apple.com committed
277
    if (frame && frame->scriptProxy()->isEnabled()) {
278
        ExecState* exec = window()->globalExec();
darin's avatar
darin committed
279 280

        JSLock lock;
281
        JSObject* constr = window()->functionConstructor();
darin's avatar
darin committed
282 283
        List args;

weinig@apple.com's avatar
weinig@apple.com committed
284
        UString sourceURL(frame->loader()->url().string());
darin's avatar
darin committed
285
        args.append(eventParameterName());
286
        args.append(jsString(m_code));
weinig's avatar
weinig committed
287
        m_listener = constr->construct(exec, args, m_functionName, sourceURL, m_lineNumber); // FIXME: is globalExec ok ?
darin's avatar
darin committed
288

weinig's avatar
weinig committed
289
        FunctionImp* listenerAsFunction = static_cast<FunctionImp*>(m_listener.get());
mjs's avatar
mjs committed
290

darin's avatar
darin committed
291 292 293 294
        if (exec->hadException()) {
            exec->clearException();

            // failed to parse, so let's just make this listener a no-op
weinig's avatar
weinig committed
295 296
            m_listener = 0;
        } else if (m_originalNode) {
darin's avatar
darin committed
297
            // Add the event's home element to the scope
darin's avatar
darin committed
298
            // (and the document, and the form - see JSHTMLElement::eventHandlerScope)
mjs's avatar
mjs committed
299
            ScopeChain scope = listenerAsFunction->scope();
darin's avatar
darin committed
300

weinig's avatar
weinig committed
301
            JSValue* thisObj = toJS(exec, m_originalNode);
darin's avatar
darin committed
302
            if (thisObj->isObject()) {
weinig's avatar
weinig committed
303
                static_cast<JSEventTargetNode*>(thisObj)->pushEventHandlerScope(exec, scope);
mjs's avatar
mjs committed
304
                listenerAsFunction->setScope(scope);
darin's avatar
darin committed
305
            }
mjs's avatar
mjs committed
306
        }
mjs's avatar
mjs committed
307 308 309
    }

    // no more need to keep the unparsed code around
darin's avatar
darin committed
310
    m_functionName = String();
weinig's avatar
weinig committed
311
    m_code = String();
darin's avatar
darin committed
312

weinig's avatar
weinig committed
313
    if (m_listener) {
weinig@apple.com's avatar
weinig@apple.com committed
314
        JSDOMWindow::ListenersMap& listeners = isHTMLEventListener()
315
            ? window()->jsHTMLEventListeners() : window()->jsEventListeners();
weinig's avatar
weinig committed
316
        listeners.set(m_listener, const_cast<JSLazyEventListener*>(this));
darin's avatar
darin committed
317
    }
mjs's avatar
mjs committed
318 319
}

darin's avatar
darin committed
320
JSValue* getNodeEventListener(EventTargetNode* n, const AtomicString& eventType)
kocienda's avatar
kocienda committed
321
{
weinig's avatar
weinig committed
322
    if (JSAbstractEventListener* listener = static_cast<JSAbstractEventListener*>(n->getHTMLEventListener(eventType))) {
darin's avatar
darin committed
323 324
        if (JSValue* obj = listener->listenerObj())
            return obj;
weinig's avatar
weinig committed
325
    }
darin's avatar
darin committed
326
    return jsNull();
kocienda's avatar
kocienda committed
327 328
}

weinig's avatar
weinig committed
329
} // namespace WebCore