1/*
2 * Copyright (C) 2013 Google Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 *    notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 *    notice, this list of conditions and the following disclaimer in the
11 *    documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26
27#include "config.h"
28#include "JSMutationCallback.h"
29
30#include "JSDOMGlobalObject.h"
31#include "JSMainThreadExecState.h"
32#include "JSMutationObserver.h"
33#include "JSMutationRecord.h"
34#include "ScriptExecutionContext.h"
35#include <heap/WeakInlines.h>
36#include <runtime/JSLock.h>
37
38using namespace JSC;
39
40namespace WebCore {
41
42JSMutationCallback::JSMutationCallback(JSObject* callback, JSDOMGlobalObject* globalObject)
43    : ActiveDOMCallback(globalObject->scriptExecutionContext())
44    , m_callback(PassWeak<JSObject>(callback))
45    , m_isolatedWorld(globalObject->world())
46{
47}
48
49JSMutationCallback::~JSMutationCallback()
50{
51}
52
53void JSMutationCallback::call(const Vector<RefPtr<MutationRecord> >& mutations, MutationObserver* observer)
54{
55    if (!canInvokeCallback())
56        return;
57
58    RefPtr<JSMutationCallback> protect(this);
59
60    JSLockHolder lock(m_isolatedWorld->vm());
61
62    if (!m_callback)
63        return;
64
65    JSValue callback = m_callback.get();
66    CallData callData;
67    CallType callType = getCallData(callback, callData);
68    if (callType == CallTypeNone) {
69        ASSERT_NOT_REACHED();
70        return;
71    }
72
73    ScriptExecutionContext* context = scriptExecutionContext();
74    if (!context)
75        return;
76    ASSERT(context->isDocument());
77
78    JSDOMGlobalObject* globalObject = toJSDOMGlobalObject(context, m_isolatedWorld.get());
79    ExecState* exec = globalObject->globalExec();
80
81    JSValue jsObserver = toJS(exec, globalObject, observer);
82
83    MarkedArgumentBuffer args;
84    args.append(jsArray(exec, globalObject, mutations));
85    args.append(jsObserver);
86
87    InspectorInstrumentationCookie cookie = JSMainThreadExecState::instrumentFunctionCall(context, callType, callData);
88
89    JSMainThreadExecState::call(exec, callback, callType, callData, jsObserver, args);
90
91    InspectorInstrumentation::didCallFunction(cookie);
92
93    if (exec->hadException())
94        reportCurrentException(exec);
95}
96
97} // namespace WebCore
98