18e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project/*
2231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block * Copyright (C) 2007, 2008, 2009 Apple Inc. All rights reserved.
38e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project *
48e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project * Redistribution and use in source and binary forms, with or without
58e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project * modification, are permitted provided that the following conditions
68e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project * are met:
78e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project *
88e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project * 1.  Redistributions of source code must retain the above copyright
98e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project *     notice, this list of conditions and the following disclaimer.
108e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project * 2.  Redistributions in binary form must reproduce the above copyright
118e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project *     notice, this list of conditions and the following disclaimer in the
128e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project *     documentation and/or other materials provided with the distribution.
138e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project * 3.  Neither the name of Apple Computer, Inc. ("Apple") nor the names of
148e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project *     its contributors may be used to endorse or promote products derived
158e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project *     from this software without specific prior written permission.
168e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project *
178e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
188e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
198e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
208e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
218e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
228e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
238e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
248e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
258e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
268e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
278e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project */
288e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project
29231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block#include "config.h"
30231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block#include "JSCallbackData.h"
318e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project
328e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project#include "Document.h"
33231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block#include "JSDOMBinding.h"
34e78cbe89e6f337f2f1fe40315be88f742b547151Steve Block#include "JSMainThreadExecState.h"
358e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project
36231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Blockusing namespace JSC;
37231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block
388e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Projectnamespace WebCore {
398e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project
40231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Blockvoid JSCallbackData::deleteData(void* context)
41231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block{
42231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block    delete static_cast<JSCallbackData*>(context);
43231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block}
448e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project
45231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve BlockJSValue JSCallbackData::invokeCallback(MarkedArgumentBuffer& args, bool* raisedException)
46231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block{
47231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block    ASSERT(callback());
48231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block    ASSERT(globalObject());
498e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project
50231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block    ExecState* exec = globalObject()->globalExec();
51643ca7872b450ea4efacab6188849e5aac2ba161Steve Block    JSValue function = callback()->get(exec, Identifier(exec, "handleEvent"));
52643ca7872b450ea4efacab6188849e5aac2ba161Steve Block
53231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block    CallData callData;
54545e470e52f0ac6a3a072bf559c796b42c6066b6Ben Murdoch    CallType callType = getCallData(function, callData);
55231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block    if (callType == CallTypeNone) {
56231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block        callType = callback()->getCallData(callData);
57231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block        if (callType == CallTypeNone)
58231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block            return JSValue();
59231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block        function = callback();
60231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block    }
61231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block
62e14391e94c850b8bd03680c23b38978db68687a8John Reck    globalObject()->globalData().timeoutChecker.start();
63545e470e52f0ac6a3a072bf559c796b42c6066b6Ben Murdoch    ScriptExecutionContext* context = globalObject()->scriptExecutionContext();
64545e470e52f0ac6a3a072bf559c796b42c6066b6Ben Murdoch    // We will fail to get the context if the frame has been detached.
65545e470e52f0ac6a3a072bf559c796b42c6066b6Ben Murdoch    if (!context)
66545e470e52f0ac6a3a072bf559c796b42c6066b6Ben Murdoch        return JSValue();
67545e470e52f0ac6a3a072bf559c796b42c6066b6Ben Murdoch
6828040489d744e0c5d475a88663056c9040ed5320Teng-Hui Zhu    bool contextIsDocument = context->isDocument();
6928040489d744e0c5d475a88663056c9040ed5320Teng-Hui Zhu    JSValue result = contextIsDocument
70e78cbe89e6f337f2f1fe40315be88f742b547151Steve Block        ? JSMainThreadExecState::call(exec, function, callType, callData, callback(), args)
71e78cbe89e6f337f2f1fe40315be88f742b547151Steve Block        : JSC::call(exec, function, callType, callData, callback(), args);
72e14391e94c850b8bd03680c23b38978db68687a8John Reck    globalObject()->globalData().timeoutChecker.stop();
738e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project
7428040489d744e0c5d475a88663056c9040ed5320Teng-Hui Zhu    if (contextIsDocument)
7528040489d744e0c5d475a88663056c9040ed5320Teng-Hui Zhu        Document::updateStyleForAllDocuments();
768e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project
77231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block    if (exec->hadException()) {
78231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block        reportCurrentException(exec);
79231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block        if (raisedException)
80231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block            *raisedException = true;
81231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block        return result;
82231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block    }
83231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block
84231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block    return result;
85231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block}
86231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block
87231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block} // namespace WebCore
88