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 | #include "config.h" |
28 | #include "WorkletGlobalScope.h" |
29 | |
30 | #if ENABLE(CSS_PAINTING_API) |
31 | |
32 | #include "Document.h" |
33 | #include "Frame.h" |
34 | #include "InspectorInstrumentation.h" |
35 | #include "JSWorkletGlobalScope.h" |
36 | #include "PageConsoleClient.h" |
37 | #include "SecurityOriginPolicy.h" |
38 | #include "Settings.h" |
39 | #include "WorkletScriptController.h" |
40 | |
41 | #include <JavaScriptCore/Exception.h> |
42 | #include <JavaScriptCore/JSLock.h> |
43 | #include <JavaScriptCore/ScriptCallStack.h> |
44 | #include <wtf/IsoMallocInlines.h> |
45 | |
46 | namespace WebCore { |
47 | using namespace Inspector; |
48 | |
49 | WTF_MAKE_ISO_ALLOCATED_IMPL(WorkletGlobalScope); |
50 | |
51 | WorkletGlobalScope::WorkletGlobalScope(Document& document, ScriptSourceCode&& code) |
52 | : m_document(makeWeakPtr(document)) |
53 | , m_sessionID(m_document->sessionID()) |
54 | , m_script(std::make_unique<WorkletScriptController>(this)) |
55 | , m_topOrigin(SecurityOrigin::createUnique()) |
56 | , m_eventQueue(*this) |
57 | , m_code(WTFMove(code)) |
58 | { |
59 | auto addResult = allWorkletGlobalScopesSet().add(this); |
60 | ASSERT_UNUSED(addResult, addResult); |
61 | |
62 | auto* frame = document.frame(); |
63 | m_jsRuntimeFlags = frame ? frame->settings().javaScriptRuntimeFlags() : JSC::RuntimeFlags(); |
64 | ASSERT(document.page()); |
65 | |
66 | setSecurityOriginPolicy(SecurityOriginPolicy::create(m_topOrigin.copyRef())); |
67 | setContentSecurityPolicy(std::make_unique<ContentSecurityPolicy>(URL { m_code.url() }, *this)); |
68 | } |
69 | |
70 | WorkletGlobalScope::~WorkletGlobalScope() |
71 | { |
72 | ASSERT(!m_script); |
73 | removeFromContextsMap(); |
74 | auto removeResult = allWorkletGlobalScopesSet().remove(this); |
75 | ASSERT_UNUSED(removeResult, removeResult); |
76 | } |
77 | |
78 | void WorkletGlobalScope::prepareForDestruction() |
79 | { |
80 | if (!m_script) |
81 | return; |
82 | stopActiveDOMObjects(); |
83 | removeRejectedPromiseTracker(); |
84 | removeAllEventListeners(); |
85 | m_script->vm().notifyNeedTermination(); |
86 | m_script = nullptr; |
87 | } |
88 | |
89 | auto WorkletGlobalScope::allWorkletGlobalScopesSet() -> WorkletGlobalScopesSet& |
90 | { |
91 | static NeverDestroyed<WorkletGlobalScopesSet> scopes; |
92 | return scopes; |
93 | } |
94 | |
95 | String WorkletGlobalScope::origin() const |
96 | { |
97 | return m_topOrigin->toString(); |
98 | } |
99 | |
100 | String WorkletGlobalScope::userAgent(const URL& url) const |
101 | { |
102 | if (!m_document) |
103 | return "" ; |
104 | return m_document->userAgent(url); |
105 | } |
106 | |
107 | void WorkletGlobalScope::evaluate() |
108 | { |
109 | m_script->evaluate(m_code); |
110 | } |
111 | |
112 | bool WorkletGlobalScope::isJSExecutionForbidden() const |
113 | { |
114 | return !m_script || m_script->isExecutionForbidden(); |
115 | } |
116 | |
117 | void WorkletGlobalScope::disableEval(const String& errorMessage) |
118 | { |
119 | m_script->disableEval(errorMessage); |
120 | } |
121 | |
122 | void WorkletGlobalScope::disableWebAssembly(const String& errorMessage) |
123 | { |
124 | m_script->disableWebAssembly(errorMessage); |
125 | } |
126 | |
127 | URL WorkletGlobalScope::completeURL(const String& url) const |
128 | { |
129 | if (url.isNull()) |
130 | return URL(); |
131 | return URL(m_code.url(), url); |
132 | } |
133 | |
134 | void WorkletGlobalScope::logExceptionToConsole(const String& errorMessage, const String& sourceURL, int lineNumber, int columnNumber, RefPtr<ScriptCallStack>&& stack) |
135 | { |
136 | if (!m_document || isJSExecutionForbidden()) |
137 | return; |
138 | m_document->logExceptionToConsole(errorMessage, sourceURL, lineNumber, columnNumber, WTFMove(stack)); |
139 | } |
140 | |
141 | void WorkletGlobalScope::addConsoleMessage(std::unique_ptr<Inspector::ConsoleMessage>&& message) |
142 | { |
143 | if (!m_document || isJSExecutionForbidden() || !message) |
144 | return; |
145 | m_document->addConsoleMessage(std::make_unique<Inspector::ConsoleMessage>(message->source(), message->type(), message->level(), message->message(), 0)); |
146 | } |
147 | |
148 | void WorkletGlobalScope::addConsoleMessage(MessageSource source, MessageLevel level, const String& message, unsigned long requestIdentifier) |
149 | { |
150 | if (!m_document || isJSExecutionForbidden()) |
151 | return; |
152 | m_document->addConsoleMessage(source, level, message, requestIdentifier); |
153 | } |
154 | |
155 | void WorkletGlobalScope::addMessage(MessageSource source, MessageLevel level, const String& messageText, const String& sourceURL, unsigned lineNumber, unsigned columnNumber, RefPtr<ScriptCallStack>&& callStack, JSC::ExecState*, unsigned long requestIdentifier) |
156 | { |
157 | if (!m_document || isJSExecutionForbidden()) |
158 | return; |
159 | m_document->addMessage(source, level, messageText, sourceURL, lineNumber, columnNumber, WTFMove(callStack), nullptr, requestIdentifier); |
160 | } |
161 | |
162 | } // namespace WebCore |
163 | #endif |
164 | |