Skip to content

Commit

Permalink
[Site Isolation] Begin implementing window.blur()
Browse files Browse the repository at this point in the history
https://bugs.webkit.org/show_bug.cgi?id=268122
rdar://121629787

Reviewed by Alex Christensen.

window.blur() won't remove focus from the frame in the web process, so we just need to call
`WebPageProxy::setFocus()` and verify the delegate is called.

* Source/WebCore/page/RemoteDOMWindow.cpp:
(WebCore::RemoteDOMWindow::blur):
* Source/WebCore/page/RemoteFrameClient.h:
* Source/WebKit/WebProcess/WebCoreSupport/WebRemoteFrameClient.cpp:
(WebKit::WebRemoteFrameClient::unfocus):
* Source/WebKit/WebProcess/WebCoreSupport/WebRemoteFrameClient.h:
* Tools/TestWebKitAPI/Tests/WebKitCocoa/SiteIsolation.mm:
(TestWebKitAPI::TEST):
* Tools/TestWebKitAPI/cocoa/TestUIDelegate.h:
* Tools/TestWebKitAPI/cocoa/TestUIDelegate.mm:
(-[TestUIDelegate _unfocusWebView:]):

Canonical link: https://commits.webkit.org/273549@main
  • Loading branch information
charliewolfe committed Jan 26, 2024
1 parent 4c07faa commit afeef8f
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 1 deletion.
4 changes: 3 additions & 1 deletion Source/WebCore/page/RemoteDOMWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,9 @@ void RemoteDOMWindow::focus(LocalDOMWindow&)

void RemoteDOMWindow::blur()
{
// FIXME: Implemented this. <rdar://116203970>
// FIXME(268121): Add security checks here equivalent to LocalDOMWindow::blur().
if (m_frame && m_frame->isMainFrame())
m_frame->client().unfocus();
}

unsigned RemoteDOMWindow::length() const
Expand Down
1 change: 1 addition & 0 deletions Source/WebCore/page/RemoteFrameClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ class RemoteFrameClient : public FrameLoaderClient {
virtual void broadcastFrameRemovalToOtherProcesses() = 0;
virtual void close() = 0;
virtual void focus() = 0;
virtual void unfocus() = 0;
virtual ~RemoteFrameClient() { }
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,12 @@ void WebRemoteFrameClient::focus()
page->send(Messages::WebPageProxy::FocusRemoteFrame(m_frame->frameID()));
}

void WebRemoteFrameClient::unfocus()
{
if (auto* page = m_frame->page())
page->send(Messages::WebPageProxy::SetFocus(false));
}

void WebRemoteFrameClient::dispatchDecidePolicyForNavigationAction(const NavigationAction& navigationAction, const ResourceRequest& request, const ResourceResponse& redirectResponse,
FormState* formState, const String& clientRedirectSourceForHistory, uint64_t navigationID, std::optional<HitTestResult>&& hitTestResult, bool hasOpener, SandboxFlags sandboxFlags, PolicyDecisionMode policyDecisionMode, FramePolicyFunction&& function)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ class WebRemoteFrameClient final : public WebCore::RemoteFrameClient, public Web
void broadcastFrameRemovalToOtherProcesses() final;
void close() final;
void focus() final;
void unfocus() final;
void dispatchDecidePolicyForNavigationAction(const WebCore::NavigationAction&, const WebCore::ResourceRequest&, const WebCore::ResourceResponse& redirectResponse, WebCore::FormState*, const String& clientRedirectSourceForHistory, uint64_t navigationID, std::optional<WebCore::HitTestResult>&&, bool hasOpener, WebCore::SandboxFlags, WebCore::PolicyDecisionMode, WebCore::FramePolicyFunction&&) final;

ScopeExit<Function<void()>> m_frameInvalidator;
Expand Down
30 changes: 30 additions & 0 deletions Tools/TestWebKitAPI/Tests/WebKitCocoa/SiteIsolation.mm
Original file line number Diff line number Diff line change
Expand Up @@ -1692,6 +1692,36 @@ HTTPServer server({
EXPECT_FALSE([openerInfo _isFocused]);
}

#if PLATFORM(MAC)
TEST(SiteIsolation, OpenedWindowFocusDelegates)
{
auto openerHTML = "<script>"
" let w = window.open('https://domain2.com/opened');"
"</script>"_s;
HTTPServer server({
{ "/example"_s, { openerHTML } },
{ "/opened"_s, { ""_s } }
}, HTTPServer::Protocol::HttpsProxy);
auto [opener, opened] = openerAndOpenedViews(server);

__block bool calledFocusWebView = false;
[opened.uiDelegate setFocusWebView:^(WKWebView *viewToFocus) {
calledFocusWebView = true;
}];

__block bool calledUnfocusWebView = false;
[opened.uiDelegate setUnfocusWebView:^(WKWebView *viewToFocus) {
calledUnfocusWebView = true;
}];

[opener.webView.get() evaluateJavaScript:@"w.focus()" completionHandler:nil];
Util::run(&calledFocusWebView);

[opener.webView.get() evaluateJavaScript:@"w.blur()" completionHandler:nil];
Util::run(&calledUnfocusWebView);
}
#endif

TEST(SiteIsolation, FindStringInFrame)
{
HTTPServer server({
Expand Down
1 change: 1 addition & 0 deletions Tools/TestWebKitAPI/cocoa/TestUIDelegate.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
#endif
@property (nonatomic, copy) void (^saveDataToFile)(WKWebView *, NSData *, NSString *, NSString *, NSURL *);
@property (nonatomic, copy) void (^focusWebView)(WKWebView *);
@property (nonatomic, copy) void (^unfocusWebView)(WKWebView *);
@property (nonatomic, copy) void (^webViewDidClose)(WKWebView *);

- (NSString *)waitForAlert;
Expand Down
6 changes: 6 additions & 0 deletions Tools/TestWebKitAPI/cocoa/TestUIDelegate.mm
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,12 @@ - (void)_focusWebView:(WKWebView *)webView
if (_focusWebView)
_focusWebView(webView);
}

- (void)_unfocusWebView:(WKWebView *)webView
{
if (_unfocusWebView)
_unfocusWebView(webView);
}
#endif // PLATFORM(MAC)

- (void)webViewDidClose:(WKWebView *)webView
Expand Down

0 comments on commit afeef8f

Please sign in to comment.