kjs_events.cpp 10.2 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);
111
            frame->domWindow()->console()->addMessage(JSMessageSource, ErrorMessageLevel, message, lineNumber, sourceURL);
darin's avatar
darin committed
112 113 114
            exec->clearException();
        } else {
            if (!retval->isUndefinedOrNull() && event->storesResultAsString())
darin's avatar
darin committed
115
                event->storeResult(retval->toString(exec));
weinig's avatar
weinig committed
116
            if (m_html) {
darin's avatar
darin committed
117 118
                bool retvalbool;
                if (retval->getBoolean(retvalbool) && !retvalbool)
darin's avatar
darin committed
119
                    event->preventDefault();
120
            }
darin's avatar
darin committed
121 122
        }

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

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

mjs's avatar
mjs committed
133 134
// -------------------------------------------------------------------------

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

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

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

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

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

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

weinig's avatar
weinig committed
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
#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
195 196
// -------------------------------------------------------------------------

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

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

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

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

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

mjs's avatar
mjs committed
239 240
// -------------------------------------------------------------------------

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

    // A JSLazyEventListener can be created with a line number of zero when it is created with
    // a setAttribute call from JavaScript, so make the line number 1 in that case.
    if (m_lineNumber == 0)
        m_lineNumber = 1;
mjs's avatar
mjs committed
259 260
}

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

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

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

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

        JSLock lock;
284
        JSObject* constr = window()->functionConstructor();
darin's avatar
darin committed
285 286
        List args;

weinig@apple.com's avatar
weinig@apple.com committed
287
        UString sourceURL(frame->loader()->url().string());
darin's avatar
darin committed
288
        args.append(eventParameterName());
289
        args.append(jsString(m_code));
290 291 292

        // FIXME: Passing the document's URL to construct is not always correct, since this event listener might
        // have been added with setAttribute from a script, and we should pass String() in that case.
weinig's avatar
weinig committed
293
        m_listener = constr->construct(exec, args, m_functionName, sourceURL, m_lineNumber); // FIXME: is globalExec ok ?
darin's avatar
darin committed
294

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

darin's avatar
darin committed
297 298 299 300
        if (exec->hadException()) {
            exec->clearException();

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

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

    // no more need to keep the unparsed code around
darin's avatar
darin committed
316
    m_functionName = String();
weinig's avatar
weinig committed
317
    m_code = String();
darin's avatar
darin committed
318

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

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

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