1/*
2 * Copyright (C) 2018 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. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 *
25 */
26
27#pragma once
28
29#if ENABLE(CSS_PAINTING_API)
30
31#include "EventTarget.h"
32#include "ExceptionOr.h"
33#include "ScriptExecutionContext.h"
34#include "ScriptSourceCode.h"
35#include "WorkerEventQueue.h"
36
37#include <JavaScriptCore/ConsoleMessage.h>
38#include <JavaScriptCore/RuntimeFlags.h>
39#include <pal/SessionID.h>
40#include <wtf/URL.h>
41#include <wtf/ObjectIdentifier.h>
42#include <wtf/WeakPtr.h>
43
44namespace WebCore {
45class WorkletScriptController;
46class ScriptSourceCode;
47
48enum WorkletGlobalScopeIdentifierType { };
49using WorkletGlobalScopeIdentifier = ObjectIdentifier<WorkletGlobalScopeIdentifierType>;
50
51class WorkletGlobalScope : public RefCounted<WorkletGlobalScope>, public ScriptExecutionContext, public EventTargetWithInlineData {
52 WTF_MAKE_ISO_ALLOCATED(WorkletGlobalScope);
53public:
54 virtual ~WorkletGlobalScope();
55
56 using WorkletGlobalScopesSet = HashSet<const WorkletGlobalScope*>;
57 WEBCORE_EXPORT static WorkletGlobalScopesSet& allWorkletGlobalScopesSet();
58
59 virtual bool isPaintWorkletGlobalScope() const { return false; }
60
61 const URL& url() const final { return m_code.url(); }
62 String origin() const final;
63
64 void evaluate();
65
66 using RefCounted::ref;
67 using RefCounted::deref;
68
69 WorkletScriptController* script() { return m_script.get(); }
70
71 void addConsoleMessage(std::unique_ptr<Inspector::ConsoleMessage>&&) final;
72
73 bool isJSExecutionForbidden() const final;
74 SecurityOrigin& topOrigin() const final { return m_topOrigin.get(); }
75
76 SocketProvider* socketProvider() final { return nullptr; }
77
78 bool isContextThread() const final { return true; }
79 bool isSecureContext() const final { return false; }
80
81 JSC::RuntimeFlags jsRuntimeFlags() const { return m_jsRuntimeFlags; }
82
83 virtual void prepareForDestruction();
84
85protected:
86 WorkletGlobalScope(Document&, ScriptSourceCode&&);
87 WorkletGlobalScope(const WorkletGlobalScope&) = delete;
88 WorkletGlobalScope(WorkletGlobalScope&&) = delete;
89
90 Document* responsibleDocument() { return m_document.get(); }
91 const Document* responsibleDocument() const { return m_document.get(); }
92
93private:
94#if ENABLE(INDEXED_DATABASE)
95 IDBClient::IDBConnectionProxy* idbConnectionProxy() final { ASSERT_NOT_REACHED(); return nullptr; }
96#endif
97
98 void postTask(Task&&) final { ASSERT_NOT_REACHED(); }
99
100 void refScriptExecutionContext() final { ref(); }
101 void derefScriptExecutionContext() final { deref(); }
102
103 void refEventTarget() final { ref(); }
104 void derefEventTarget() final { deref(); }
105
106 ScriptExecutionContext* scriptExecutionContext() const final { return const_cast<WorkletGlobalScope*>(this); }
107 EventTargetInterface eventTargetInterface() const final { return WorkletGlobalScopeEventTargetInterfaceType; }
108
109 bool isWorkletGlobalScope() const final { return true; }
110
111 void logExceptionToConsole(const String& errorMessage, const String&, int, int, RefPtr<Inspector::ScriptCallStack>&&) final;
112 void addMessage(MessageSource, MessageLevel, const String&, const String&, unsigned, unsigned, RefPtr<Inspector::ScriptCallStack>&&, JSC::ExecState*, unsigned long) final;
113 void addConsoleMessage(MessageSource, MessageLevel, const String&, unsigned long) final;
114
115 EventTarget* errorEventTarget() final { return this; }
116 EventQueue& eventQueue() const final { ASSERT_NOT_REACHED(); return m_eventQueue; }
117
118#if ENABLE(WEB_CRYPTO)
119 bool wrapCryptoKey(const Vector<uint8_t>&, Vector<uint8_t>&) final { RELEASE_ASSERT_NOT_REACHED(); return false; }
120 bool unwrapCryptoKey(const Vector<uint8_t>&, Vector<uint8_t>&) final { RELEASE_ASSERT_NOT_REACHED(); return false; }
121#endif
122 URL completeURL(const String&) const final;
123 PAL::SessionID sessionID() const final { return m_sessionID; }
124 String userAgent(const URL&) const final;
125 void disableEval(const String&) final;
126 void disableWebAssembly(const String&) final;
127
128 WeakPtr<Document> m_document;
129
130 PAL::SessionID m_sessionID;
131 std::unique_ptr<WorkletScriptController> m_script;
132
133 Ref<SecurityOrigin> m_topOrigin;
134
135 // FIXME: This is not implemented properly, it just satisfies the compiler.
136 // https://bugs.webkit.org/show_bug.cgi?id=191136
137 mutable WorkerEventQueue m_eventQueue;
138
139 JSC::RuntimeFlags m_jsRuntimeFlags;
140 ScriptSourceCode m_code;
141};
142
143} // namespace WebCore
144
145SPECIALIZE_TYPE_TRAITS_BEGIN(WebCore::WorkletGlobalScope)
146static bool isType(const WebCore::ScriptExecutionContext& context) { return context.isWorkletGlobalScope(); }
147SPECIALIZE_TYPE_TRAITS_END()
148#endif
149