FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
WebKit/Source/WebCore/workers/WorkerThread.cpp at main · WebKit/WebKit · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
WebKit
/
WebKit
Public
Notifications
You must be signed in to change notification settings
Fork
2.1k
Star
10.1k
Code
Pull requests
2.6k
Actions
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
WebKit
/
Source
/
WebCore
/
workers
/
WorkerThread.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
222 lines (198 loc) · 9.33 KB
Breadcrumbs
WebKit
/
Source
/
WebCore
/
workers
/
WorkerThread.cpp
Copy path
File metadata and controls
222 lines (198 loc) · 9.33 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/*
* Copyright (C) 2008-2017 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
#
include
"
config.h
"
#
include
"
WorkerThread.h
"
#
include
"
AdvancedPrivacyProtections.h
"
#
include
"
IDBConnectionProxy.h
"
#
include
"
JSDOMExceptionHandling.h
"
#
include
"
JSDOMGlobalObject.h
"
#
include
"
ScriptSourceCode.h
"
#
include
"
SecurityOrigin.h
"
#
include
"
SocketProvider.h
"
#
include
"
WorkerBadgeProxy.h
"
#
include
"
WorkerDebuggerProxy.h
"
#
include
"
WorkerGlobalScope.h
"
#
include
"
WorkerLoaderProxy.h
"
#
include
"
WorkerReportingProxy.h
"
#
include
"
WorkerScriptFetcher.h
"
#
include
<
JavaScriptCore/ScriptCallStack.h
>
#
include
<
wtf/SetForScope.h
>
#
include
<
wtf/TZoneMallocInlines.h
>
#
include
<
wtf/Threading.h
>
namespace
WebCore
{
static
std::atomic<
unsigned
> workerThreadCounter {
0
};
unsigned
WorkerThread::workerThreadCount
()
{
return
workerThreadCounter;
}
WorkerParameters
WorkerParameters::isolatedCopy
()
const
{
return
{
scriptURL.
isolatedCopy
(),
ownerURL.
isolatedCopy
(),
name.
isolatedCopy
(),
inspectorIdentifier.
isolatedCopy
(),
userAgent.
isolatedCopy
(),
isOnline,
contentSecurityPolicyResponseHeaders.
isolatedCopy
(),
shouldBypassMainWorldContentSecurityPolicy,
crossOriginEmbedderPolicy.
isolatedCopy
(),
timeOrigin,
referrerPolicy,
workerType,
credentials,
settingsValues.
isolatedCopy
(),
workerThreadMode,
sessionID,
crossThreadCopy
(serviceWorkerData),
clientIdentifier,
advancedPrivacyProtections,
noiseInjectionHashSalt,
agentClusterID.
isolatedCopy
()
};
}
struct
WorkerThreadStartupData
{
WTF_MAKE_TZONE_ALLOCATED_INLINE
(WorkerThreadStartupData);
WTF_MAKE_NONCOPYABLE
(WorkerThreadStartupData);
public:
WorkerThreadStartupData
(
const
WorkerParameters& params,
const
ScriptBuffer& sourceCode, WorkerThreadStartMode,
const
SecurityOrigin& topOrigin);
WorkerParameters params;
Ref<SecurityOrigin> origin;
ScriptBuffer sourceCode;
WorkerThreadStartMode startMode;
Ref<SecurityOrigin> topOrigin;
};
WorkerThreadStartupData::WorkerThreadStartupData
(
const
WorkerParameters& other,
const
ScriptBuffer& sourceCode, WorkerThreadStartMode startMode,
const
SecurityOrigin& topOrigin)
: params(other.isolatedCopy())
, origin(SecurityOrigin::create(other.scriptURL)->
isolatedCopy
())
, sourceCode(sourceCode.isolatedCopy())
, startMode(startMode)
, topOrigin(topOrigin.isolatedCopy())
{
}
WorkerThread::WorkerThread
(
const
WorkerParameters& params,
const
ScriptBuffer& sourceCode, WorkerLoaderProxy& workerLoaderProxy, WorkerDebuggerProxy& workerDebuggerProxy, WorkerReportingProxy& workerReportingProxy, WorkerBadgeProxy& badgeProxy, WorkerThreadStartMode startMode,
const
SecurityOrigin& topOrigin, IDBClient::IDBConnectionProxy* connectionProxy, SocketProvider* socketProvider,
JSC
::RuntimeFlags runtimeFlags)
: WorkerOrWorkletThread(params.inspectorIdentifier.isolatedCopy(), params.workerThreadMode)
, m_workerLoaderProxy(&workerLoaderProxy)
, m_workerDebuggerProxy(&workerDebuggerProxy)
, m_workerReportingProxy(&workerReportingProxy)
, m_workerBadgeProxy(&badgeProxy)
, m_runtimeFlags(runtimeFlags)
, m_startupData(makeUnique<WorkerThreadStartupData>(params, sourceCode, startMode, topOrigin))
, m_idbConnectionProxy(connectionProxy)
, m_socketProvider(socketProvider)
{
++workerThreadCounter;
}
WorkerThread::~WorkerThread
()
{
ASSERT
(workerThreadCounter);
--workerThreadCounter;
}
Ref<Thread>
WorkerThread::createThread
()
{
if
(is<WorkerMainRunLoop>(
runLoop
())) {
//
This worker should run on the main thread.
RunLoop::mainSingleton
().
dispatch
([protectedThis = Ref { *
this
}] {
protectedThis->
workerOrWorkletThread
();
});
ASSERT
(
isMainThread
());
return
Thread::currentSingleton
();
}
//
WorkerOrWorkletThread::workerOrWorkletThread destroys the worker and expects a single reference to this.
SUPPRESS_UNCOUNTED_LAMBDA_CAPTURE
return
Thread::create
(
threadName
(), [
this
] {
workerOrWorkletThread
();
}, ThreadType::JavaScript);
}
RefPtr<WorkerOrWorkletGlobalScope>
WorkerThread::createGlobalScope
()
{
return
createWorkerGlobalScope
(m_startupData->
params
,
WTF::move
(m_startupData->
origin
),
WTF::move
(m_startupData->
topOrigin
));
}
bool
WorkerThread::shouldWaitForWebInspectorOnStartup
()
const
{
return
m_startupData->
startMode
== WorkerThreadStartMode::WaitForInspector;
}
void
WorkerThread::evaluateScriptIfNecessary
(String& exceptionMessage)
{
SetForScope
isInStaticScriptEvaluation
(m_isInStaticScriptEvaluation,
true
);
//
We are currently holding only the initial script code. If the WorkerType is Module, we should fetch the entire graph before executing the rest of this.
//
We invoke module loader as if we are executing inline module script tag in Document.
Ref globalScope = *
this
->
globalScope
();
WeakPtr<ScriptBufferSourceProvider> sourceProvider;
if
(m_startupData->
params
.
workerType
== WorkerType::Classic) {
ScriptSourceCode
sourceCode
(m_startupData->
sourceCode
,
URL
(m_startupData->
params
.
scriptURL
));
sourceProvider = downcast<ScriptBufferSourceProvider>(sourceCode.
provider
());
NakedPtr<
JSC
::Exception> uncaughtException;
auto
parseResult = globalScope->
script
()->
evaluate
(sourceCode, uncaughtException, &exceptionMessage);
if
(uncaughtException && !globalScope->
vm
().
isTerminationException
(uncaughtException)) {
//
Per the spec, if the script has a parse error, fire a simple Event at
//
the Worker object rather than an ErrorEvent.
if
(globalScope->
settingsValues
().
workerParseErrorReportingEnabled
&& parseResult == WorkerOrWorkletScriptController::ParseResult::Failed)
globalScope->
reportErrorToWorkerObject
(exceptionMessage);
else
{
JSC
::JSLockHolder
lock
(globalScope->
vm
());
reportException
(globalScope->
script
()->
globalScopeWrapper
(), uncaughtException);
}
}
finishedEvaluatingScript
();
}
else
{
auto
parameters =
ModuleFetchParameters::create
(
JSC
::ScriptFetchParameters::Type::JavaScript,
emptyString
(),
/*
isTopLevelModule
*/
true
);
auto
scriptFetcher =
WorkerScriptFetcher::create
(
WTF::move
(parameters), globalScope->
credentials
(), globalScope->
destination
(), globalScope->
referrerPolicy
());
ScriptSourceCode
sourceCode
(m_startupData->
sourceCode
,
URL
(m_startupData->
params
.
scriptURL
), { }, { },
JSC
::SourceProviderSourceType::Module, scriptFetcher.
copyRef
());
sourceProvider = downcast<ScriptBufferSourceProvider>(sourceCode.
provider
());
bool
success = globalScope->
script
()->
loadModuleSynchronously
(scriptFetcher.
get
(), sourceCode);
if
(success) {
if
(
auto
error = scriptFetcher->
error
()) {
if
(std::optional<LoadableScript::ConsoleMessage> message = error->
consoleMessage
)
exceptionMessage = message->
message
;
else
exceptionMessage =
"
Importing a module script failed.
"
_s;
globalScope->
reportErrorToWorkerObject
(exceptionMessage);
}
else
if
(!scriptFetcher->
wasCanceled
()) {
globalScope->
script
()->
linkAndEvaluateModule
(scriptFetcher.
get
(), sourceCode, &exceptionMessage);
finishedEvaluatingScript
();
}
}
}
if
(sourceProvider)
globalScope->
setMainScriptSourceProvider
(*sourceProvider);
//
Free the startup data to cause its member variable deref's happen on the worker's thread (since
//
all ref/derefs of these objects are happening on the thread at this point). Note that
//
WorkerThread::~WorkerThread happens on a different thread where it was created.
m_startupData =
nullptr
;
}
WorkerGlobalScope*
WorkerThread::globalScope
()
{
ASSERT
(!
thread
() ||
thread
() == &
Thread::currentSingleton
());
return
downcast<WorkerGlobalScope>(
WorkerOrWorkletThread::globalScope
());
}
void
WorkerThread::clearProxies
()
{
m_workerLoaderProxy =
nullptr
;
m_workerDebuggerProxy =
nullptr
;
m_workerReportingProxy =
nullptr
;
m_workerBadgeProxy =
nullptr
;
}
}
//
namespace WebCore
Back
|
FazBrowse Home
|
New Git URL