Skip to content

Commit

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

Reviewed by Chris Dumez.

For performance & security.

* Source/WebKit/WebProcess/GPU/graphics/RemoteResourceCacheProxy.cpp:
(WebKit::RemoteResourceCacheProxy::clearNativeImageMap):
* Source/WebKit/WebProcess/GPU/media/MediaPlayerPrivateRemote.cpp:
(WebKit::MediaPlayerPrivateRemote::cdmInstanceAttached):
(WebKit::MediaPlayerPrivateRemote::cdmInstanceDetached):
(WebKit::MediaPlayerPrivateRemote::attemptToDecryptWithInstance):
* Source/WebKit/WebProcess/GPU/webrtc/SharedVideoFrame.cpp:
(WebKit::SharedVideoFrameWriter::writeBuffer):
* Source/WebKit/WebProcess/Network/WebLoaderStrategy.cpp:
(WebKit::WebLoaderStrategy::scheduleLoad):
(WebKit::WebLoaderStrategy::scheduleLoadFromNetworkProcess):
* Source/WebKit/WebProcess/Plugins/PDF/PDFPluginBase.mm:
(WebKit::PDFPluginBase::isFullFramePlugin const):
* Source/WebKit/WebProcess/Plugins/PDF/PDFPluginPasswordField.mm:
(WebKit::PDFPluginPasswordField::handleEvent):
* Source/WebKit/WebProcess/Plugins/PDF/PDFPluginTextAnnotation.mm:
(WebKit::PDFPluginTextAnnotation::handleEvent):
* Source/WebKit/WebProcess/Speech/SpeechRecognitionRealtimeMediaSourceManager.cpp:
* Source/WebKit/WebProcess/WebCoreSupport/ShareableBitmapUtilities.cpp:
(WebKit::createShareableBitmap):
* Source/WebKit/WebProcess/WebCoreSupport/ios/WebChromeClientIOS.mm:
(WebKit::WebChromeClient::showDataDetectorsUIForElement):

Canonical link: https://commits.webkit.org/275436@main
  • Loading branch information
annevk committed Feb 28, 2024
1 parent 2fd8234 commit f4ba330
Show file tree
Hide file tree
Showing 10 changed files with 31 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -218,11 +218,11 @@ void RemoteResourceCacheProxy::clearRenderingResourceMap()
void RemoteResourceCacheProxy::clearNativeImageMap()
{
m_renderingResources.removeIf([&] (auto& keyValuePair) {
if (!is<NativeImage>(keyValuePair.value.get()))
RefPtr nativeImage = dynamicDowncast<NativeImage>(keyValuePair.value.get());
if (!nativeImage)
return false;

auto& nativeImage = downcast<NativeImage>(*keyValuePair.value.get());
nativeImage.removeObserver(*this);
nativeImage->removeObserver(*this);
return true;
});
}
Expand Down
12 changes: 6 additions & 6 deletions Source/WebKit/WebProcess/GPU/media/MediaPlayerPrivateRemote.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1384,20 +1384,20 @@ void MediaPlayerPrivateRemote::mediaPlayerKeyNeeded(std::span<const uint8_t> mes
#if ENABLE(ENCRYPTED_MEDIA)
void MediaPlayerPrivateRemote::cdmInstanceAttached(CDMInstance& instance)
{
if (is<RemoteCDMInstance>(instance))
connection().send(Messages::RemoteMediaPlayerProxy::CdmInstanceAttached(downcast<RemoteCDMInstance>(instance).identifier()), m_id);
if (auto* remoteInstance = dynamicDowncast<RemoteCDMInstance>(instance))
connection().send(Messages::RemoteMediaPlayerProxy::CdmInstanceAttached(remoteInstance->identifier()), m_id);
}

void MediaPlayerPrivateRemote::cdmInstanceDetached(CDMInstance& instance)
{
if (is<RemoteCDMInstance>(instance))
connection().send(Messages::RemoteMediaPlayerProxy::CdmInstanceDetached(downcast<RemoteCDMInstance>(instance).identifier()), m_id);
if (auto* remoteInstance = dynamicDowncast<RemoteCDMInstance>(instance))
connection().send(Messages::RemoteMediaPlayerProxy::CdmInstanceDetached(remoteInstance->identifier()), m_id);
}

void MediaPlayerPrivateRemote::attemptToDecryptWithInstance(CDMInstance& instance)
{
if (is<RemoteCDMInstance>(instance))
connection().send(Messages::RemoteMediaPlayerProxy::AttemptToDecryptWithInstance(downcast<RemoteCDMInstance>(instance).identifier()), m_id);
if (auto* remoteInstance = dynamicDowncast<RemoteCDMInstance>(instance))
connection().send(Messages::RemoteMediaPlayerProxy::AttemptToDecryptWithInstance(remoteInstance->identifier()), m_id);
}

void MediaPlayerPrivateRemote::waitingForKeyChanged(bool waitingForKey)
Expand Down
8 changes: 4 additions & 4 deletions Source/WebKit/WebProcess/GPU/webrtc/SharedVideoFrame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,12 +116,12 @@ std::optional<SharedVideoFrame> SharedVideoFrameWriter::write(const VideoFrame&

std::optional<SharedVideoFrame::Buffer> SharedVideoFrameWriter::writeBuffer(const VideoFrame& frame, const Function<void(IPC::Semaphore&)>& newSemaphoreCallback, const Function<void(SharedMemory::Handle&&)>& newMemoryCallback)
{
if (is<RemoteVideoFrameProxy>(frame))
return downcast<RemoteVideoFrameProxy>(frame).newReadReference();
if (auto* frameProxy = dynamicDowncast<RemoteVideoFrameProxy>(frame))
return frameProxy->newReadReference();

#if USE(LIBWEBRTC)
if (is<VideoFrameLibWebRTC>(frame))
return writeBuffer(*downcast<VideoFrameLibWebRTC>(frame).buffer(), newSemaphoreCallback, newMemoryCallback);
if (auto* webrtcFrame = dynamicDowncast<VideoFrameLibWebRTC>(frame))
return writeBuffer(*webrtcFrame->buffer(), newSemaphoreCallback, newMemoryCallback);
#endif

return writeBuffer(frame.pixelBuffer(), newSemaphoreCallback, newMemoryCallback);
Expand Down
11 changes: 5 additions & 6 deletions Source/WebKit/WebProcess/Network/WebLoaderStrategy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,8 @@ void WebLoaderStrategy::scheduleLoad(ResourceLoader& resourceLoader, CachedResou
WebResourceLoader::TrackingParameters trackingParameters;
if (auto* webFrameLoaderClient = toWebLocalFrameLoaderClient(frameLoaderClient))
trackingParameters.webPageProxyID = valueOrDefault(webFrameLoaderClient->webPageProxyID());
else if (is<RemoteWorkerFrameLoaderClient>(frameLoaderClient))
trackingParameters.webPageProxyID = downcast<RemoteWorkerFrameLoaderClient>(frameLoaderClient).webPageProxyID();
else if (auto* workerFrameLoaderClient = dynamicDowncast<RemoteWorkerFrameLoaderClient>(frameLoaderClient))
trackingParameters.webPageProxyID = workerFrameLoaderClient->webPageProxyID();
trackingParameters.pageID = valueOrDefault(frameLoader->frame().pageID());
trackingParameters.frameID = frameLoader->frameID();
trackingParameters.resourceID = identifier;
Expand Down Expand Up @@ -449,11 +449,10 @@ void WebLoaderStrategy::scheduleLoadFromNetworkProcess(ResourceLoader& resourceL

// FIXME: All loaders should provide their origin if navigation mode is cors/no-cors/same-origin.
// As a temporary approach, we use the document origin if available or the HTTP Origin header otherwise.
if (is<SubresourceLoader>(resourceLoader)) {
auto& loader = downcast<SubresourceLoader>(resourceLoader);
loadParameters.sourceOrigin = loader.origin();
if (auto* loader = dynamicDowncast<SubresourceLoader>(resourceLoader)) {
loadParameters.sourceOrigin = loader->origin();

if (auto* headers = loader.originalHeaders())
if (auto* headers = loader->originalHeaders())
loadParameters.originalRequestHeaders = *headers;
}

Expand Down
6 changes: 2 additions & 4 deletions Source/WebKit/WebProcess/Plugins/PDF/PDFPluginBase.mm
Original file line number Diff line number Diff line change
Expand Up @@ -183,10 +183,8 @@
if (!m_frame || !m_frame->coreLocalFrame())
return false;

RefPtr document = m_frame->coreLocalFrame()->document();
if (!is<PluginDocument>(document))
return false;
return downcast<PluginDocument>(*document).pluginWidget() == m_view;
RefPtr document = dynamicDowncast<PluginDocument>(m_frame->coreLocalFrame()->document());
return document && document->pluginWidget() == m_view;
}

bool PDFPluginBase::handlesPageScaleFactor() const
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,8 @@

bool PDFPluginPasswordField::handleEvent(WebCore::Event& event)
{
if (event.isKeyboardEvent() && event.type() == eventNames().keyupEvent) {
auto& keyboardEvent = downcast<KeyboardEvent>(event);

if (keyboardEvent.keyIdentifier() == "Enter"_s) {
if (auto* keyboardEvent = dynamicDowncast<KeyboardEvent>(event); keyboardEvent && keyboardEvent->type() == eventNames().keyupEvent) {
if (keyboardEvent->keyIdentifier() == "Enter"_s) {
plugin()->attemptToUnlockPDF(value());
event.preventDefault();
return true;
Expand Down
10 changes: 4 additions & 6 deletions Source/WebKit/WebProcess/Plugins/PDF/PDFPluginTextAnnotation.mm
Original file line number Diff line number Diff line change
Expand Up @@ -133,14 +133,12 @@ static const String cssAlignmentValueForNSTextAlignment(NSTextAlignment alignmen
if (PDFPluginAnnotation::handleEvent(event))
return true;

if (event.isKeyboardEvent() && event.type() == eventNames().keydownEvent) {
auto& keyboardEvent = downcast<KeyboardEvent>(event);

if (keyboardEvent.keyIdentifier() == "U+0009"_s) {
if (keyboardEvent.ctrlKey() || keyboardEvent.metaKey())
if (auto* keyboardEvent = dynamicDowncast<KeyboardEvent>(event); keyboardEvent && keyboardEvent->type() == eventNames().keydownEvent) {
if (keyboardEvent->keyIdentifier() == "U+0009"_s) {
if (keyboardEvent->ctrlKey() || keyboardEvent->metaKey())
return false;

if (keyboardEvent.shiftKey())
if (keyboardEvent->shiftKey())
plugin()->focusPreviousAnnotation();
else
plugin()->focusNextAnnotation();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,6 @@ class SpeechRecognitionRealtimeMediaSourceManager::Source
m_connection->send(Messages::SpeechRecognitionRemoteRealtimeMediaSourceManager::SetStorage(m_identifier, WTFMove(handle), format), 0);
}

ASSERT(is<WebAudioBufferList>(audioData));
m_ringBuffer->store(downcast<WebAudioBufferList>(audioData).list(), numberOfFrames, time.timeValue());
m_connection->send(Messages::SpeechRecognitionRemoteRealtimeMediaSourceManager::RemoteAudioSamplesAvailable(m_identifier, time, numberOfFrames), 0);
#else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,8 @@ RefPtr<ShareableBitmap> createShareableBitmap(RenderImage& renderImage, CreateSh
}

#if ENABLE(VIDEO)
if (is<RenderVideo>(renderImage)) {
auto& renderVideo = downcast<RenderVideo>(renderImage);
Ref video = renderVideo.videoElement();
if (auto* renderVideo = dynamicDowncast<RenderVideo>(renderImage)) {
Ref video = renderVideo->videoElement();
auto image = video->nativeImageForCurrentTime();
if (!image)
return { };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,12 +166,12 @@

bool WebChromeClient::showDataDetectorsUIForElement(const Element& element, const Event& event)
{
if (!event.isMouseEvent())
auto* mouseEvent = dynamicDowncast<MouseEvent>(event);
if (!mouseEvent)
return false;

// FIXME: Ideally, we would be able to generate InteractionInformationAtPosition without re-hit-testing the element.
auto& mouseEvent = downcast<MouseEvent>(event);
auto request = InteractionInformationRequest { roundedIntPoint(mouseEvent.locationInRootViewCoordinates()) };
auto request = InteractionInformationRequest { roundedIntPoint(mouseEvent->locationInRootViewCoordinates()) };
request.includeLinkIndicator = true;
auto page = protectedPage();
auto positionInformation = page->positionInformation(request);
Expand Down

0 comments on commit f4ba330

Please sign in to comment.