| 1 | /* |
| 2 | * Copyright (C) 2007-2009, 2015 Apple 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 | * |
| 8 | * 1. Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
| 10 | * 2. Redistributions in binary form must reproduce the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer in the |
| 12 | * documentation and/or other materials provided with the distribution. |
| 13 | * 3. Neither the name of Apple Inc. ("Apple") nor the names of |
| 14 | * its contributors may be used to endorse or promote products derived |
| 15 | * from this software without specific prior written permission. |
| 16 | * |
| 17 | * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY |
| 18 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 19 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 20 | * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY |
| 21 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 22 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 23 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
| 24 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | */ |
| 28 | |
| 29 | #pragma once |
| 30 | |
| 31 | #include "JSDOMBinding.h" |
| 32 | #include "JSDOMGlobalObject.h" |
| 33 | #include "ScriptExecutionContext.h" |
| 34 | #include <JavaScriptCore/JSObject.h> |
| 35 | #include <JavaScriptCore/Strong.h> |
| 36 | #include <JavaScriptCore/StrongInlines.h> |
| 37 | #include <wtf/Threading.h> |
| 38 | |
| 39 | namespace WebCore { |
| 40 | |
| 41 | // We have to clean up this data on the context thread because unprotecting a |
| 42 | // JSObject on the wrong thread without synchronization would corrupt the heap |
| 43 | // (and synchronization would be slow). |
| 44 | |
| 45 | class JSCallbackData { |
| 46 | public: |
| 47 | enum class CallbackType { Function, Object, FunctionOrObject }; |
| 48 | |
| 49 | JSDOMGlobalObject* globalObject() { return m_globalObject.get(); } |
| 50 | |
| 51 | protected: |
| 52 | explicit JSCallbackData(JSDOMGlobalObject* globalObject) |
| 53 | : m_globalObject(globalObject) |
| 54 | { |
| 55 | } |
| 56 | |
| 57 | ~JSCallbackData() |
| 58 | { |
| 59 | #if !PLATFORM(IOS_FAMILY) |
| 60 | ASSERT(m_thread.ptr() == &Thread::current()); |
| 61 | #endif |
| 62 | } |
| 63 | |
| 64 | static JSC::JSValue invokeCallback(JSDOMGlobalObject&, JSC::JSObject* callback, JSC::JSValue thisValue, JSC::MarkedArgumentBuffer&, CallbackType, JSC::PropertyName functionName, NakedPtr<JSC::Exception>& returnedException); |
| 65 | |
| 66 | private: |
| 67 | JSC::Weak<JSDOMGlobalObject> m_globalObject; |
| 68 | #ifndef NDEBUG |
| 69 | Ref<Thread> m_thread { Thread::current() }; |
| 70 | #endif |
| 71 | }; |
| 72 | |
| 73 | class JSCallbackDataStrong : public JSCallbackData { |
| 74 | public: |
| 75 | JSCallbackDataStrong(JSC::JSObject* callback, JSDOMGlobalObject* globalObject, void*) |
| 76 | : JSCallbackData(globalObject) |
| 77 | , m_callback(globalObject->vm(), callback) |
| 78 | { |
| 79 | } |
| 80 | |
| 81 | JSC::JSObject* callback() { return m_callback.get(); } |
| 82 | |
| 83 | JSC::JSValue invokeCallback(JSC::JSValue thisValue, JSC::MarkedArgumentBuffer& args, CallbackType callbackType, JSC::PropertyName functionName, NakedPtr<JSC::Exception>& returnedException) |
| 84 | { |
| 85 | auto* globalObject = this->globalObject(); |
| 86 | if (!globalObject) |
| 87 | return { }; |
| 88 | |
| 89 | return JSCallbackData::invokeCallback(*globalObject, callback(), thisValue, args, callbackType, functionName, returnedException); |
| 90 | } |
| 91 | |
| 92 | private: |
| 93 | JSC::Strong<JSC::JSObject> m_callback; |
| 94 | }; |
| 95 | |
| 96 | class JSCallbackDataWeak : public JSCallbackData { |
| 97 | public: |
| 98 | JSCallbackDataWeak(JSC::JSObject* callback, JSDOMGlobalObject* globalObject, void* owner) |
| 99 | : JSCallbackData(globalObject) |
| 100 | , m_callback(callback, &m_weakOwner, owner) |
| 101 | { |
| 102 | } |
| 103 | |
| 104 | JSC::JSObject* callback() { return m_callback.get(); } |
| 105 | |
| 106 | JSC::JSValue invokeCallback(JSC::JSValue thisValue, JSC::MarkedArgumentBuffer& args, CallbackType callbackType, JSC::PropertyName functionName, NakedPtr<JSC::Exception>& returnedException) |
| 107 | { |
| 108 | auto* globalObject = this->globalObject(); |
| 109 | if (!globalObject) |
| 110 | return { }; |
| 111 | |
| 112 | return JSCallbackData::invokeCallback(*globalObject, callback(), thisValue, args, callbackType, functionName, returnedException); |
| 113 | } |
| 114 | |
| 115 | void visitJSFunction(JSC::SlotVisitor&); |
| 116 | |
| 117 | private: |
| 118 | class WeakOwner : public JSC::WeakHandleOwner { |
| 119 | bool isReachableFromOpaqueRoots(JSC::Handle<JSC::Unknown>, void* context, JSC::SlotVisitor&, const char**) override; |
| 120 | }; |
| 121 | WeakOwner m_weakOwner; |
| 122 | JSC::Weak<JSC::JSObject> m_callback; |
| 123 | }; |
| 124 | |
| 125 | class DeleteCallbackDataTask : public ScriptExecutionContext::Task { |
| 126 | public: |
| 127 | template <typename CallbackDataType> |
| 128 | explicit DeleteCallbackDataTask(CallbackDataType* data) |
| 129 | : ScriptExecutionContext::Task(ScriptExecutionContext::Task::CleanupTask, [data = std::unique_ptr<CallbackDataType>(data)] (ScriptExecutionContext&) { |
| 130 | }) |
| 131 | { |
| 132 | } |
| 133 | }; |
| 134 | |
| 135 | } // namespace WebCore |
| 136 | |