OSDN Git Service

e444d888400c87b9d7c7da98ac36a82b0a039835
[android-x86/external-webkit.git] / Source / WebCore / bindings / js / JSEventListener.cpp
1 /*
2  *  Copyright (C) 2001 Peter Kelly (pmk@post.com)
3  *  Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009 Apple Inc. All Rights Reserved.
4  *
5  *  This library is free software; you can redistribute it and/or
6  *  modify it under the terms of the GNU Lesser General Public
7  *  License as published by the Free Software Foundation; either
8  *  version 2 of the License, or (at your option) any later version.
9  *
10  *  This library is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  *  Lesser General Public License for more details.
14  *
15  *  You should have received a copy of the GNU Lesser General Public
16  *  License along with this library; if not, write to the Free Software
17  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
18  */
19
20 #include "config.h"
21 #include "JSEventListener.h"
22
23 #include "Event.h"
24 #include "Frame.h"
25 #include "JSEvent.h"
26 #include "JSEventTarget.h"
27 #include "JSMainThreadExecState.h"
28 #include <runtime/JSLock.h>
29 #include <wtf/RefCountedLeakCounter.h>
30
31 using namespace JSC;
32
33 namespace WebCore {
34
35 JSEventListener::JSEventListener(JSObject* function, JSObject* wrapper, bool isAttribute, DOMWrapperWorld* isolatedWorld)
36     : EventListener(JSEventListenerType)
37     , m_wrapper(*isolatedWorld->globalData(), wrapper)
38     , m_isAttribute(isAttribute)
39     , m_isolatedWorld(isolatedWorld)
40 {
41     m_jsFunction.set(*m_isolatedWorld->globalData(), wrapper, function);
42 }
43
44 JSEventListener::~JSEventListener()
45 {
46 }
47
48 JSObject* JSEventListener::initializeJSFunction(ScriptExecutionContext*) const
49 {
50     ASSERT_NOT_REACHED();
51     return 0;
52 }
53
54 void JSEventListener::markJSFunction(MarkStack& markStack)
55 {
56     if (m_jsFunction)
57         markStack.append(&m_jsFunction);
58 }
59
60 void JSEventListener::handleEvent(ScriptExecutionContext* scriptExecutionContext, Event* event)
61 {
62     ASSERT(scriptExecutionContext);
63     if (!scriptExecutionContext || scriptExecutionContext->isJSExecutionTerminated())
64         return;
65
66     JSLock lock(SilenceAssertionsOnly);
67
68     JSObject* jsFunction = this->jsFunction(scriptExecutionContext);
69     if (!jsFunction)
70         return;
71
72     JSDOMGlobalObject* globalObject = toJSDOMGlobalObject(scriptExecutionContext, m_isolatedWorld.get());
73     if (!globalObject)
74         return;
75
76     if (scriptExecutionContext->isDocument()) {
77         JSDOMWindow* window = static_cast<JSDOMWindow*>(globalObject);
78         Frame* frame = window->impl()->frame();
79         if (!frame)
80             return;
81         // The window must still be active in its frame. See <https://bugs.webkit.org/show_bug.cgi?id=21921>.
82         // FIXME: A better fix for this may be to change DOMWindow::frame() to not return a frame the detached window used to be in.
83         if (frame->domWindow() != window->impl())
84             return;
85         // FIXME: Is this check needed for other contexts?
86         ScriptController* script = frame->script();
87         if (!script->canExecuteScripts(AboutToExecuteScript) || script->isPaused())
88             return;
89     }
90
91     ExecState* exec = globalObject->globalExec();
92     JSValue handleEventFunction = jsFunction->get(exec, Identifier(exec, "handleEvent"));
93
94     CallData callData;
95     CallType callType = getCallData(handleEventFunction, callData);
96     if (callType == CallTypeNone) {
97         handleEventFunction = JSValue();
98         callType = jsFunction->getCallData(callData);
99     }
100
101     if (callType != CallTypeNone) {
102         ref();
103
104         MarkedArgumentBuffer args;
105         args.append(toJS(exec, globalObject, event));
106
107         Event* savedEvent = globalObject->currentEvent();
108         globalObject->setCurrentEvent(event);
109
110         JSGlobalData& globalData = globalObject->globalData();
111         DynamicGlobalObjectScope globalObjectScope(globalData, globalData.dynamicGlobalObject ? globalData.dynamicGlobalObject : globalObject);
112
113         globalData.timeoutChecker.start();
114         JSValue retval;
115         if (handleEventFunction) {
116             retval = scriptExecutionContext->isDocument()
117                 ? JSMainThreadExecState::call(exec, handleEventFunction, callType, callData, jsFunction, args)
118                 : JSC::call(exec, handleEventFunction, callType, callData, jsFunction, args);
119         } else {
120             JSValue currentTarget = toJS(exec, globalObject, event->currentTarget());
121             retval = scriptExecutionContext->isDocument()
122                 ? JSMainThreadExecState::call(exec, jsFunction, callType, callData, currentTarget, args)
123                 : JSC::call(exec, jsFunction, callType, callData, currentTarget, args);
124         }
125         globalData.timeoutChecker.stop();
126
127         globalObject->setCurrentEvent(savedEvent);
128
129         if (exec->hadException()) {
130             event->target()->uncaughtExceptionInEventHandler();
131             reportCurrentException(exec);
132         } else {
133             if (!retval.isUndefinedOrNull() && event->storesResultAsString())
134                 event->storeResult(ustringToString(retval.toString(exec)));
135             if (m_isAttribute) {
136                 bool retvalbool;
137                 if (retval.getBoolean(retvalbool) && !retvalbool)
138                     event->preventDefault();
139             }
140         }
141
142         deref();
143     }
144 }
145
146 bool JSEventListener::virtualisAttribute() const
147 {
148     return m_isAttribute;
149 }
150
151 bool JSEventListener::operator==(const EventListener& listener)
152 {
153     if (const JSEventListener* jsEventListener = JSEventListener::cast(&listener))
154         return m_jsFunction == jsEventListener->m_jsFunction && m_isAttribute == jsEventListener->m_isAttribute;
155     return false;
156 }
157
158 } // namespace WebCore