| 1 | /* |
| 2 | * Copyright (C) 2013-2017 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 | * 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 | #include "config.h" |
| 27 | #include "JSDOMGlobalObjectTask.h" |
| 28 | |
| 29 | #include "ActiveDOMCallback.h" |
| 30 | #include "JSDOMGlobalObject.h" |
| 31 | #include "JSExecState.h" |
| 32 | #include <JavaScriptCore/Microtask.h> |
| 33 | #include <JavaScriptCore/StrongInlines.h> |
| 34 | #include <wtf/Ref.h> |
| 35 | |
| 36 | namespace WebCore { |
| 37 | using namespace JSC; |
| 38 | |
| 39 | class JSGlobalObjectCallback final : public RefCounted<JSGlobalObjectCallback>, private ActiveDOMCallback { |
| 40 | public: |
| 41 | static Ref<JSGlobalObjectCallback> create(JSDOMGlobalObject& globalObject, Ref<Microtask>&& task) |
| 42 | { |
| 43 | return adoptRef(*new JSGlobalObjectCallback(globalObject, WTFMove(task))); |
| 44 | } |
| 45 | |
| 46 | void call() |
| 47 | { |
| 48 | if (!canInvokeCallback()) |
| 49 | return; |
| 50 | |
| 51 | Ref<JSGlobalObjectCallback> protectedThis(*this); |
| 52 | VM& vm = m_globalObject->vm(); |
| 53 | JSLockHolder lock(vm); |
| 54 | auto scope = DECLARE_THROW_SCOPE(vm); |
| 55 | |
| 56 | ExecState* exec = m_globalObject->globalExec(); |
| 57 | |
| 58 | ScriptExecutionContext* context = m_globalObject->scriptExecutionContext(); |
| 59 | // We will fail to get the context if the frame has been detached. |
| 60 | if (!context) |
| 61 | return; |
| 62 | JSExecState::runTask(exec, m_task); |
| 63 | scope.assertNoException(); |
| 64 | } |
| 65 | |
| 66 | private: |
| 67 | JSGlobalObjectCallback(JSDOMGlobalObject& globalObject, Ref<Microtask>&& task) |
| 68 | : ActiveDOMCallback { globalObject.scriptExecutionContext() } |
| 69 | , m_globalObject { globalObject.vm(), &globalObject } |
| 70 | , m_task { WTFMove(task) } |
| 71 | { |
| 72 | } |
| 73 | |
| 74 | Strong<JSDOMGlobalObject> m_globalObject; |
| 75 | Ref<Microtask> m_task; |
| 76 | }; |
| 77 | |
| 78 | JSGlobalObjectTask::JSGlobalObjectTask(JSDOMGlobalObject& globalObject, Ref<Microtask>&& task) |
| 79 | : ScriptExecutionContext::Task({ }) |
| 80 | { |
| 81 | auto callback = JSGlobalObjectCallback::create(globalObject, WTFMove(task)); |
| 82 | m_task = [callback = WTFMove(callback)] (ScriptExecutionContext&) { |
| 83 | callback->call(); |
| 84 | }; |
| 85 | } |
| 86 | |
| 87 | } // namespace WebCore |
| 88 | |