kjs_events.cpp 9.93 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"
weinig's avatar
weinig committed
24
#include "Chrome.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"
andersca's avatar
andersca committed
34
#include "Page.h"
darin's avatar
darin committed
35
#include "kjs_proxy.h"
ggaren@apple.com's avatar
ggaren@apple.com committed
36
#include <kjs/function_object.h>
kocienda's avatar
kocienda committed
37

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

weinig's avatar
weinig committed
40 41
namespace WebCore {

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

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

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

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

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

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

71
    JSGlobalObject* globalObject = scriptProxy->globalObject();
ggaren@apple.com's avatar
ggaren@apple.com committed
72
    ExecState* exec = globalObject->globalExec();
weinig's avatar
weinig committed
73

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

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

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

darin's avatar
darin committed
88
        window->setCurrentEvent(event);
weinig's avatar
weinig committed
89

darin's avatar
darin committed
90
        JSValue* retval;
andersca's avatar
andersca committed
91
        if (handleEventFunc) {
ggaren@apple.com's avatar
ggaren@apple.com committed
92
            globalObject->startTimeoutCheck();
darin's avatar
darin committed
93
            retval = handleEventFunc->call(exec, listener, args);
andersca's avatar
andersca committed
94
        } else {
darin's avatar
darin committed
95 96 97 98
            JSObject* thisObj;
            if (isWindowEvent)
                thisObj = window;
            else
darin's avatar
darin committed
99
                thisObj = static_cast<JSObject*>(toJS(exec, event->currentTarget()));
ggaren@apple.com's avatar
ggaren@apple.com committed
100
            globalObject->startTimeoutCheck();
darin's avatar
darin committed
101 102
            retval = listener->call(exec, thisObj, args);
        }
ggaren@apple.com's avatar
ggaren@apple.com committed
103
        globalObject->stopTimeoutCheck();
darin's avatar
darin committed
104 105 106 107 108

        window->setCurrentEvent(0);

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

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

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

mjs's avatar
mjs committed
137 138
// -------------------------------------------------------------------------

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

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

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

weinig@apple.com's avatar
weinig@apple.com committed
165
JSDOMWindow* JSUnprotectedEventListener::windowObj() const
mjs's avatar
mjs committed
166
{
weinig's avatar
weinig committed
167
    return m_win;
mjs's avatar
mjs committed
168 169
}

sullivan's avatar
sullivan committed
170 171
void JSUnprotectedEventListener::clearWindowObj()
{
weinig's avatar
weinig committed
172
    m_win = 0;
sullivan's avatar
sullivan committed
173 174
}

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

weinig's avatar
weinig committed
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
#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
199 200
// -------------------------------------------------------------------------

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

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

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

weinig@apple.com's avatar
weinig@apple.com committed
233
JSDOMWindow* JSEventListener::windowObj() const
mjs's avatar
mjs committed
234
{
weinig's avatar
weinig committed
235
    return m_win;
mjs's avatar
mjs committed
236 237
}

sullivan's avatar
sullivan committed
238 239
void JSEventListener::clearWindowObj()
{
weinig's avatar
weinig committed
240
    m_win = 0;
sullivan's avatar
sullivan committed
241 242
}

mjs's avatar
mjs committed
243 244
// -------------------------------------------------------------------------

weinig@apple.com's avatar
weinig@apple.com committed
245
JSLazyEventListener::JSLazyEventListener(const String& functionName, const String& code, JSDOMWindow* win, Node* node, int lineNumber)
weinig's avatar
weinig committed
246 247 248 249 250 251
    : JSEventListener(0, win, true)
    , m_functionName(functionName)
    , m_code(code)
    , m_parsed(false)
    , m_lineNumber(lineNumber)
    , m_originalNode(node)
mjs's avatar
mjs committed
252
{
darin's avatar
darin committed
253
    // We don't retain the original node because we assume it
mjs's avatar
mjs committed
254 255 256 257
    // 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
258 259
}

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

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

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

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

        JSLock lock;
alice.liu@apple.com's avatar
alice.liu@apple.com committed
283
        JSObject* constr = frame->scriptProxy()->globalObject()->functionConstructor();
darin's avatar
darin committed
284 285
        List args;

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

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

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

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

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

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

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

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

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