Skip to content

Commit

Permalink
[Site Isolation] Reduce code duplication when sending IPC to a web page
Browse files Browse the repository at this point in the history
https://bugs.webkit.org/show_bug.cgi?id=264788
rdar://118368823

Reviewed by Alex Christensen.

With site isolation we often need to check if a RemotePageProxy exists for a given frame before sending
IPC to the process hosting the frame. We should have a helper function instead of duplicating this logic
throughout WebPageProxy.

* Source/WebKit/UIProcess/RemotePageProxy.cpp:
(WebKit::RemotePageProxy::sendMouseEvent): Deleted.
* Source/WebKit/UIProcess/RemotePageProxy.h:
* Source/WebKit/UIProcess/WebPageProxy.cpp:
(WebKit::WebPageProxy::performDragControllerAction):
(WebKit::WebPageProxy::dragEnded):
(WebKit::WebPageProxy::handleMouseEventReply):
(WebKit::WebPageProxy::sendMouseEvent):
(WebKit::WebPageProxy::sendKeyEvent):
(WebKit::WebPageProxy::runJavaScriptInFrameInScriptWorld):
(WebKit::WebPageProxy::dispatchLoadEventToFrameOwnerElement):
(WebKit::WebPageProxy::sendToWebPage):
(WebKit::WebPageProxy::sendToProcessContainingFrame):
* Source/WebKit/UIProcess/WebPageProxy.h:

Canonical link: https://commits.webkit.org/270723@main
  • Loading branch information
charliewolfe committed Nov 14, 2023
1 parent dfe3191 commit 7d27cdd
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 73 deletions.
12 changes: 0 additions & 12 deletions Source/WebKit/UIProcess/RemotePageProxy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -196,18 +196,6 @@ bool RemotePageProxy::didReceiveSyncMessage(IPC::Connection& connection, IPC::De
return false;
}

void RemotePageProxy::sendMouseEvent(const WebCore::FrameIdentifier& frameID, const NativeWebMouseEvent& event, std::optional<Vector<SandboxExtensionHandle>>&& sandboxExtensions)
{
sendWithAsyncReply(Messages::WebPage::MouseEvent(frameID, event, WTFMove(sandboxExtensions)), [this, protectedThis = Ref { *this }] (std::optional<WebEventType> eventType, bool handled, std::optional<WebCore::RemoteUserInputEventData> remoteUserInputEventData) mutable {
if (!m_page)
return;
if (!eventType)
return;
// FIXME: If these sandbox extensions are important, find a way to get them to the iframe process.
m_page->handleMouseEventReply(*eventType, handled, remoteUserInputEventData, { });
});
}

Ref<WebProcessProxy> RemotePageProxy::protectedProcess() const
{
return m_process;
Expand Down
2 changes: 0 additions & 2 deletions Source/WebKit/UIProcess/RemotePageProxy.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,6 @@ class RemotePageProxy : public RefCounted<RemotePageProxy>, public IPC::MessageR
WebCore::PageIdentifier pageID() const { return m_webPageID; }
const WebCore::RegistrableDomain& domain() const { return m_domain; }

void sendMouseEvent(const WebCore::FrameIdentifier&, const NativeWebMouseEvent&, std::optional<Vector<SandboxExtensionHandle>>&&);

private:
RemotePageProxy(WebPageProxy&, WebProcessProxy&, const WebCore::RegistrableDomain&, WebPageProxyMessageReceiverRegistration*);
void didReceiveMessage(IPC::Connection&, IPC::Decoder&) final;
Expand Down
102 changes: 43 additions & 59 deletions Source/WebKit/UIProcess/WebPageProxy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3181,18 +3181,7 @@ void WebPageProxy::performDragControllerAction(DragControllerAction action, Drag
ASSERT(dragData.platformData());
sendWithAsyncReply(Messages::WebPage::PerformDragControllerAction(action, dragData.clientPosition(), dragData.globalPosition(), dragData.draggingSourceOperationMask(), *dragData.platformData(), dragData.flags()), WTFMove(completionHandler));
#else
if (frameID) {
if (RefPtr frame = WebFrameProxy::webFrame(*frameID)) {
if (RefPtr remotePageProxy = frame->remotePageProxy()) {
remotePageProxy->sendWithAsyncReply(Messages::WebPage::PerformDragControllerAction(frameID, action, dragData), WTFMove(completionHandler));
return;
}
} else {
ASSERT_NOT_REACHED();
return;
}
}
sendWithAsyncReply(Messages::WebPage::PerformDragControllerAction(frameID, action, dragData), WTFMove(completionHandler));
sendToProcessContainingFrame(frameID, Messages::WebPage::PerformDragControllerAction(frameID, action, dragData), WTFMove(completionHandler));
#endif
}

Expand Down Expand Up @@ -3229,19 +3218,7 @@ void WebPageProxy::dragEnded(const IntPoint& clientPosition, const IntPoint& glo
dragEnded(remoteUserInputEventData->transformedPoint, globalPosition, dragOperationMask, remoteUserInputEventData->targetFrameID);
};

if (frameID) {
if (RefPtr frame = WebFrameProxy::webFrame(*frameID)) {
if (RefPtr remotePageProxy = frame->remotePageProxy()) {
remotePageProxy->sendWithAsyncReply(Messages::WebPage::DragEnded(frameID, clientPosition, globalPosition, dragOperationMask), WTFMove(completionHandler));
setDragCaretRect({ });
return;
}
} else {
ASSERT_NOT_REACHED();
return;
}
}
sendWithAsyncReply(Messages::WebPage::DragEnded(frameID, clientPosition, globalPosition, dragOperationMask), WTFMove(completionHandler));
sendToProcessContainingFrame(frameID, Messages::WebPage::DragEnded(frameID, clientPosition, globalPosition, dragOperationMask), WTFMove(completionHandler));
setDragCaretRect({ });
}

Expand Down Expand Up @@ -3308,28 +3285,17 @@ static bool removeOldRedundantEvent(Deque<NativeWebMouseEvent>& queue, WebEventT
void WebPageProxy::handleMouseEventReply(WebEventType eventType, bool handled, const std::optional<WebCore::RemoteUserInputEventData>& remoteUserInputEventData, std::optional<Vector<SandboxExtensionHandle>>&& sandboxExtensions)
{
if (remoteUserInputEventData) {
RefPtr frame = WebFrameProxy::webFrame(remoteUserInputEventData->targetFrameID);
if (!frame)
return;

auto& event = internals().mouseEventQueue.first();
event.setPosition(remoteUserInputEventData->transformedPoint);

RefPtr remotePageProxy = frame->remotePageProxy();
if (!remotePageProxy) {
sendMouseEvent(remoteUserInputEventData->targetFrameID, event, WTFMove(sandboxExtensions));
return;
}

remotePageProxy->sendMouseEvent(remoteUserInputEventData->targetFrameID, event, WTFMove(sandboxExtensions));
sendMouseEvent(remoteUserInputEventData->targetFrameID, event, WTFMove(sandboxExtensions));
return;
}
didReceiveEvent(eventType, handled);
}

void WebPageProxy::sendMouseEvent(const WebCore::FrameIdentifier& frameID, const NativeWebMouseEvent& event, std::optional<Vector<SandboxExtensionHandle>>&& sandboxExtensions)
{
sendWithAsyncReply(Messages::WebPage::MouseEvent(frameID, event, WTFMove(sandboxExtensions)), [this, protectedThis = Ref { *this }] (std::optional<WebEventType> eventType, bool handled, std::optional<WebCore::RemoteUserInputEventData> remoteUserInputEventData) mutable {
sendToProcessContainingFrame(frameID, Messages::WebPage::MouseEvent(frameID, event, WTFMove(sandboxExtensions)), [this, protectedThis = Ref { *this }] (std::optional<WebEventType> eventType, bool handled, std::optional<WebCore::RemoteUserInputEventData> remoteUserInputEventData) {
if (!m_pageClient)
return;
if (!eventType) {
Expand Down Expand Up @@ -3677,14 +3643,8 @@ void WebPageProxy::sendKeyEvent(const NativeWebKeyboardEvent& event)
didReceiveEvent(*eventType, handled);
};

if (m_focusedFrame) {
if (RefPtr remotePageProxy = m_focusedFrame->remotePageProxy()) {
remotePageProxy->sendWithAsyncReply(Messages::WebPage::KeyEvent(m_focusedFrame->frameID(), event), WTFMove(handleKeyEventReply));
return;
}
}

sendWithAsyncReply(Messages::WebPage::KeyEvent(m_focusedFrame ? m_focusedFrame->frameID() : m_mainFrame->frameID(), event), WTFMove(handleKeyEventReply));
auto targetFrameID = m_focusedFrame ? m_focusedFrame->frameID() : m_mainFrame->frameID();
sendToProcessContainingFrame(targetFrameID, Messages::WebPage::KeyEvent(targetFrameID, event), WTFMove(handleKeyEventReply));
}

bool WebPageProxy::handleKeyboardEvent(const NativeWebKeyboardEvent& event)
Expand Down Expand Up @@ -5297,12 +5257,7 @@ void WebPageProxy::runJavaScriptInFrameInScriptWorld(RunJavaScriptParameters&& p
callbackFunction({ API::SerializedScriptValue::createFromWireBytes(Vector(dataReference)).ptr() });
};

if (RefPtr frame = frameID ? WebFrameProxy::webFrame(*frameID) : nullptr) {
if (RefPtr remotePageProxy = frame->remotePageProxy())
return remotePageProxy->sendWithAsyncReply(Messages::WebPage::RunJavaScriptInFrameInScriptWorld(parameters, frameID, world.worldData()), WTFMove(completionHandler));
}

sendWithAsyncReply(Messages::WebPage::RunJavaScriptInFrameInScriptWorld(parameters, frameID, world.worldData()), WTFMove(completionHandler));
sendToProcessContainingFrame(frameID, Messages::WebPage::RunJavaScriptInFrameInScriptWorld(parameters, frameID, world.worldData()), WTFMove(completionHandler));
}

void WebPageProxy::getRenderTreeExternalRepresentation(CompletionHandler<void(const String&)>&& callback)
Expand Down Expand Up @@ -13229,13 +13184,7 @@ void WebPageProxy::dispatchLoadEventToFrameOwnerElement(WebCore::FrameIdentifier
if (!parentFrame)
return;

RefPtr remotePageProxy = parentFrame->remotePageProxy();
if (!remotePageProxy) {
send(Messages::WebPage::DispatchLoadEventToFrameOwnerElement(frameID));
return;
}

remotePageProxy->send(Messages::WebPage::DispatchLoadEventToFrameOwnerElement(frameID));
sendToProcessContainingFrame(parentFrame->frameID(), Messages::WebPage::DispatchLoadEventToFrameOwnerElement(frameID));
}

Ref<VisitedLinkStore> WebPageProxy::protectedVisitedLinkStore()
Expand Down Expand Up @@ -13276,6 +13225,41 @@ Ref<WebBackForwardList> WebPageProxy::protectedBackForwardList() const
return m_backForwardList;
}

template<typename F>
void WebPageProxy::sendToWebPage(std::optional<FrameIdentifier> frameID, F&& sendFunction)
{
if (frameID) {
if (RefPtr frame = WebFrameProxy::webFrame(*frameID)) {
if (RefPtr remotePageProxy = frame->remotePageProxy()) {
sendFunction(*remotePageProxy);
return;
}
} else
ASSERT_NOT_REACHED();
}
sendFunction(*this);
}

template<typename M, typename C>
void WebPageProxy::sendToProcessContainingFrame(std::optional<FrameIdentifier> frameID, M&& message, C&& completionHandler)
{
sendToWebPage(frameID,
[&message, &completionHandler] (auto& targetPage) {
targetPage.sendWithAsyncReply(std::forward<M>(message), std::forward<C>(completionHandler));
}
);
}

template<typename M>
void WebPageProxy::sendToProcessContainingFrame(std::optional<FrameIdentifier> frameID, M&& message)
{
sendToWebPage(frameID,
[&message] (auto& targetPage) {
targetPage.send(std::forward<M>(message));
}
);
}

} // namespace WebKit

#undef WEBPAGEPROXY_RELEASE_LOG
Expand Down
4 changes: 4 additions & 0 deletions Source/WebKit/UIProcess/WebPageProxy.h
Original file line number Diff line number Diff line change
Expand Up @@ -2849,6 +2849,10 @@ class WebPageProxy final : public API::ObjectImpl<API::Object::Type::Page>, publ

void dispatchLoadEventToFrameOwnerElement(WebCore::FrameIdentifier);

template<typename F> void sendToWebPage(std::optional<WebCore::FrameIdentifier>, F&&);
template<typename M, typename C> void sendToProcessContainingFrame(std::optional<WebCore::FrameIdentifier>, M&&, C&&);
template<typename M> void sendToProcessContainingFrame(std::optional<WebCore::FrameIdentifier>, M&&);

struct Internals;
Internals& internals() { return m_internals; }
const Internals& internals() const { return m_internals; }
Expand Down

0 comments on commit 7d27cdd

Please sign in to comment.