Skip to content

Commit

Permalink
Adopt dynamicDowncast<> in worker code
Browse files Browse the repository at this point in the history
https://bugs.webkit.org/show_bug.cgi?id=270446

Reviewed by Chris Dumez.

For security & performance.

* Source/WebCore/workers/WorkerConsoleClient.cpp:
(WebCore::WorkerConsoleClient::count):
(WebCore::WorkerConsoleClient::countReset):
(WebCore::WorkerConsoleClient::time):
(WebCore::WorkerConsoleClient::timeLog):
(WebCore::WorkerConsoleClient::timeEnd):
* Source/WebCore/workers/WorkerGlobalScope.cpp:
(WebCore::WorkerGlobalScope::importScripts):
(WebCore::WorkerGlobalScope::beginLoadingFontSoon):
* Source/WebCore/workers/WorkerOrWorkletThread.cpp:
(WebCore::WorkerOrWorkletThread::runEventLoop):
* Source/WebCore/workers/WorkerScriptLoader.cpp:
(WebCore::WorkerScriptLoader::didReceiveResponse):
* Source/WebCore/workers/WorkerThread.cpp:
(WebCore::WorkerThread::evaluateScriptIfNecessary):

Cache globalScope() call to avoid repeated downcasting.

* Source/WebCore/workers/service/SWClientConnection.cpp:
(WebCore::SWClientConnection::fromScriptExecutionContext):
* Source/WebCore/workers/service/ServiceWorker.cpp:
(WebCore::ServiceWorker::swConnection):
(WebCore::ServiceWorker::postMessage):
* Source/WebCore/workers/service/ServiceWorkerClientData.cpp:
(WebCore::toServiceWorkerClientFrameType):

Canonical link: https://commits.webkit.org/275678@main
  • Loading branch information
annevk committed Mar 5, 2024
1 parent 1f38182 commit 767834b
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 34 deletions.
20 changes: 10 additions & 10 deletions Source/WebCore/workers/WorkerConsoleClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,36 +54,36 @@ void WorkerConsoleClient::messageWithTypeAndLevel(MessageType type, MessageLevel
void WorkerConsoleClient::count(JSC::JSGlobalObject* exec, const String& label)
{
// FIXME: Add support for WorkletGlobalScope.
if (is<WorkerGlobalScope>(m_globalScope))
InspectorInstrumentation::consoleCount(downcast<WorkerGlobalScope>(m_globalScope), exec, label);
if (auto* worker = dynamicDowncast<WorkerGlobalScope>(m_globalScope))
InspectorInstrumentation::consoleCount(*worker, exec, label);
}

void WorkerConsoleClient::countReset(JSC::JSGlobalObject* exec, const String& label)
{
// FIXME: Add support for WorkletGlobalScope.
if (is<WorkerGlobalScope>(m_globalScope))
InspectorInstrumentation::consoleCountReset(downcast<WorkerGlobalScope>(m_globalScope), exec, label);
if (auto* worker = dynamicDowncast<WorkerGlobalScope>(m_globalScope))
InspectorInstrumentation::consoleCountReset(*worker, exec, label);
}

void WorkerConsoleClient::time(JSC::JSGlobalObject* exec, const String& label)
{
// FIXME: Add support for WorkletGlobalScope.
if (is<WorkerGlobalScope>(m_globalScope))
InspectorInstrumentation::startConsoleTiming(downcast<WorkerGlobalScope>(m_globalScope), exec, label);
if (auto* worker = dynamicDowncast<WorkerGlobalScope>(m_globalScope))
InspectorInstrumentation::startConsoleTiming(*worker, exec, label);
}

void WorkerConsoleClient::timeLog(JSC::JSGlobalObject* exec, const String& label, Ref<ScriptArguments>&& arguments)
{
// FIXME: Add support for WorkletGlobalScope.
if (is<WorkerGlobalScope>(m_globalScope))
InspectorInstrumentation::logConsoleTiming(downcast<WorkerGlobalScope>(m_globalScope), exec, label, WTFMove(arguments));
if (auto* worker = dynamicDowncast<WorkerGlobalScope>(m_globalScope))
InspectorInstrumentation::logConsoleTiming(*worker, exec, label, WTFMove(arguments));
}

void WorkerConsoleClient::timeEnd(JSC::JSGlobalObject* exec, const String& label)
{
// FIXME: Add support for WorkletGlobalScope.
if (is<WorkerGlobalScope>(m_globalScope))
InspectorInstrumentation::stopConsoleTiming(downcast<WorkerGlobalScope>(m_globalScope), exec, label);
if (auto* worker = dynamicDowncast<WorkerGlobalScope>(m_globalScope))
InspectorInstrumentation::stopConsoleTiming(*worker, exec, label);
}

// FIXME: <https://webkit.org/b/153499> Web Inspector: console.profile should use the new Sampling Profiler
Expand Down
7 changes: 2 additions & 5 deletions Source/WebCore/workers/WorkerGlobalScope.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -385,12 +385,10 @@ ExceptionOr<void> WorkerGlobalScope::importScripts(const FixedVector<String>& ur

FetchOptions::Cache cachePolicy = FetchOptions::Cache::Default;

bool isServiceWorkerGlobalScope = is<ServiceWorkerGlobalScope>(*this);
if (isServiceWorkerGlobalScope) {
if (auto* serviceWorkerGlobalScope = dynamicDowncast<ServiceWorkerGlobalScope>(*this)) {
// FIXME: We need to add support for the 'imported scripts updated' flag as per:
// https://w3c.github.io/ServiceWorker/#importscripts
auto& serviceWorkerGlobalScope = downcast<ServiceWorkerGlobalScope>(*this);
auto& registration = serviceWorkerGlobalScope.registration();
auto& registration = serviceWorkerGlobalScope->registration();
if (registration.updateViaCache() == ServiceWorkerUpdateViaCache::None || registration.needsUpdate())
cachePolicy = FetchOptions::Cache::NoCache;
}
Expand Down Expand Up @@ -581,7 +579,6 @@ std::unique_ptr<FontLoadRequest> WorkerGlobalScope::fontLoadRequest(const String

void WorkerGlobalScope::beginLoadingFontSoon(FontLoadRequest& request)
{
ASSERT(is<WorkerFontLoadRequest>(request));
downcast<WorkerFontLoadRequest>(request).load(*this);
}

Expand Down
4 changes: 2 additions & 2 deletions Source/WebCore/workers/WorkerOrWorkletThread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,8 @@ void WorkerOrWorkletThread::stopRunningDebuggerTasks()
void WorkerOrWorkletThread::runEventLoop()
{
// Does not return until terminated.
if (is<WorkerDedicatedRunLoop>(m_runLoop.get()))
downcast<WorkerDedicatedRunLoop>(m_runLoop.get()).run(m_globalScope.get());
if (auto* runLoop = dynamicDowncast<WorkerDedicatedRunLoop>(m_runLoop.get()))
runLoop->run(m_globalScope.get());
}

void WorkerOrWorkletThread::workerOrWorkletThread()
Expand Down
3 changes: 2 additions & 1 deletion Source/WebCore/workers/WorkerScriptLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,8 @@ void WorkerScriptLoader::didReceiveResponse(ResourceLoaderIdentifier identifier,

if (m_topOriginForServiceWorkerRegistration && response.source() == ResourceResponse::Source::MemoryCache && m_context) {
m_isMatchingServiceWorkerRegistration = true;
auto& swConnection = is<WorkerGlobalScope>(m_context) ? static_cast<SWClientConnection&>(downcast<WorkerGlobalScope>(*m_context).swClientConnection()) : ServiceWorkerProvider::singleton().serviceWorkerConnection();
auto* worker = dynamicDowncast<WorkerGlobalScope>(*m_context);
auto& swConnection = worker ? static_cast<SWClientConnection&>(worker->swClientConnection()) : ServiceWorkerProvider::singleton().serviceWorkerConnection();
swConnection.matchRegistration(WTFMove(*m_topOriginForServiceWorkerRegistration), response.url(), [this, protectedThis = Ref { *this }, response, identifier](auto&& registrationData) mutable {
m_isMatchingServiceWorkerRegistration = false;
if (registrationData && registrationData->activeWorker)
Expand Down
14 changes: 8 additions & 6 deletions Source/WebCore/workers/WorkerThread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -145,33 +145,35 @@ void WorkerThread::evaluateScriptIfNecessary(String& exceptionMessage)
// 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 = static_cast<ScriptBufferSourceProvider&>(sourceCode.provider());
globalScope()->script()->evaluate(sourceCode, &exceptionMessage);
globalScope->script()->evaluate(sourceCode, &exceptionMessage);
finishedEvaluatingScript();
} else {
auto parameters = ModuleFetchParameters::create(JSC::ScriptFetchParameters::Type::JavaScript, emptyString(), /* isTopLevelModule */ true);
auto scriptFetcher = WorkerScriptFetcher::create(WTFMove(parameters), globalScope()->credentials(), globalScope()->destination(), globalScope()->referrerPolicy());
auto scriptFetcher = WorkerScriptFetcher::create(WTFMove(parameters), globalScope->credentials(), globalScope->destination(), globalScope->referrerPolicy());
ScriptSourceCode sourceCode(m_startupData->sourceCode, URL(m_startupData->params.scriptURL), { }, { }, JSC::SourceProviderSourceType::Module, scriptFetcher.copyRef());
sourceProvider = static_cast<ScriptBufferSourceProvider&>(sourceCode.provider());
bool success = globalScope()->script()->loadModuleSynchronously(scriptFetcher.get(), sourceCode);
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);
globalScope->reportErrorToWorkerObject(exceptionMessage);
} else if (!scriptFetcher->wasCanceled()) {
globalScope()->script()->linkAndEvaluateModule(scriptFetcher.get(), sourceCode, &exceptionMessage);
globalScope->script()->linkAndEvaluateModule(scriptFetcher.get(), sourceCode, &exceptionMessage);
finishedEvaluatingScript();
}
}
}
if (sourceProvider)
globalScope()->setMainScriptSourceProvider(*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
Expand Down
4 changes: 2 additions & 2 deletions Source/WebCore/workers/service/SWClientConnection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ static bool dispatchToContextThreadIfNecessary(const ServiceWorkerOrClientIdenti

Ref<SWClientConnection> SWClientConnection::fromScriptExecutionContext(ScriptExecutionContext& context)
{
if (is<WorkerGlobalScope>(context))
return static_cast<SWClientConnection&>(downcast<WorkerGlobalScope>(context).swClientConnection());
if (auto* worker = dynamicDowncast<WorkerGlobalScope>(context))
return static_cast<SWClientConnection&>(worker->swClientConnection());

return ServiceWorkerProvider::singleton().serviceWorkerConnection();
}
Expand Down
8 changes: 4 additions & 4 deletions Source/WebCore/workers/service/ServiceWorker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,8 @@ void ServiceWorker::updateState(State state)
SWClientConnection& ServiceWorker::swConnection()
{
ASSERT(scriptExecutionContext());
if (is<WorkerGlobalScope>(scriptExecutionContext()))
return downcast<WorkerGlobalScope>(scriptExecutionContext())->swClientConnection();
if (auto* worker = dynamicDowncast<WorkerGlobalScope>(scriptExecutionContext()))
return worker->swClientConnection();
return ServiceWorkerProvider::singleton().serviceWorkerConnection();
}

Expand All @@ -117,8 +117,8 @@ ExceptionOr<void> ServiceWorker::postMessage(JSC::JSGlobalObject& globalObject,
auto& context = *scriptExecutionContext();
// FIXME: Maybe we could use a ScriptExecutionContextIdentifier for service workers too.
ServiceWorkerOrClientIdentifier sourceIdentifier;
if (is<ServiceWorkerGlobalScope>(context))
sourceIdentifier = downcast<ServiceWorkerGlobalScope>(context).thread().identifier();
if (auto* serviceWorker = dynamicDowncast<ServiceWorkerGlobalScope>(context))
sourceIdentifier = serviceWorker->thread().identifier();
else
sourceIdentifier = context.identifier();

Expand Down
8 changes: 4 additions & 4 deletions Source/WebCore/workers/service/ServiceWorkerClientData.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,16 @@ namespace WebCore {

static ServiceWorkerClientFrameType toServiceWorkerClientFrameType(ScriptExecutionContext& context)
{
if (!is<Document>(context))
auto* document = dynamicDowncast<Document>(context);
if (!document)
return ServiceWorkerClientFrameType::None;

auto& document = downcast<Document>(context);
auto* frame = document.frame();
auto* frame = document->frame();
if (!frame)
return ServiceWorkerClientFrameType::None;

if (frame->isMainFrame()) {
if (auto* window = document.domWindow()) {
if (auto* window = document->domWindow()) {
if (window->opener())
return ServiceWorkerClientFrameType::Auxiliary;
}
Expand Down

0 comments on commit 767834b

Please sign in to comment.