Skip to content

Commit

Permalink
Re-add access to WKBrowsingContextController on iOS with linked-on-or…
Browse files Browse the repository at this point in the history
…-after check

https://bugs.webkit.org/show_bug.cgi?id=274417
rdar://128283322

Reviewed by Brady Eidson.

WKBrowsingContextController is SPI that does not exist in public headers, but at
least one app is doing some tricky ObjC runtime things to access and use it.  This
PR puts it back but with a linked-on-or-after check removing access so when the app
updates it will no longer be able to access the SPI.

The original motivation for disconnecting WKBrowsingContextController was to remove
use of ObjCObjectGraph, which was used to communicate with the injected bundle in
some early prototypes of multi-process WebKit.  I verified ObjCObjectGraph was not
used, which makes sense because there is no injected bundle to communicate with.
To make this re-adding of WKBrowsingContextController access not hold up the removal
of ObjCObjectGraph, I also removed the use of ObjCObjectGraph in WKBrowsingContextController.

* Source/WTF/wtf/cocoa/RuntimeApplicationChecksCocoa.h:
* Source/WebKit/UIProcess/API/Cocoa/WKBrowsingContextController.mm:
(-[WKBrowsingContextController loadRequest:userData:]):
(-[WKBrowsingContextController loadFileURL:restrictToFilesWithin:userData:]):
(-[WKBrowsingContextController loadHTMLString:baseURL:userData:]):
(-[WKBrowsingContextController loadData:MIMEType:textEncodingName:baseURL:userData:]):
* Source/WebKit/UIProcess/API/ios/WKWebViewIOS.mm:
(-[WKWebView browsingContextController]):
* Source/WebKit/UIProcess/ios/WKContentView.h:
* Source/WebKit/UIProcess/ios/WKContentView.mm:
(-[WKContentView browsingContextController]):

Canonical link: https://commits.webkit.org/279022@main
  • Loading branch information
achristensen07 committed May 20, 2024
1 parent b0efdb1 commit 4112a1e
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 20 deletions.
1 change: 1 addition & 0 deletions Source/WTF/wtf/cocoa/RuntimeApplicationChecksCocoa.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ enum class SDKAlignedBehavior {
ApplicationStateTrackerDoesNotObserveWindow,
AuthorizationHeaderOnSameOriginRedirects,
BlanksViewOnJSPrompt,
BrowsingContextControllerSPIAccessRemoved,
ContextMenuTriggersLinkActivationNavigationType,
ConvertsInvalidURLsToBlank,
DataURLFragmentRemoval,
Expand Down
28 changes: 8 additions & 20 deletions Source/WebKit/UIProcess/API/Cocoa/WKBrowsingContextController.mm
Original file line number Diff line number Diff line change
Expand Up @@ -142,11 +142,8 @@ - (void)loadRequest:(NSURLRequest *)request

- (void)loadRequest:(NSURLRequest *)request userData:(id)userData
{
RefPtr<WebKit::ObjCObjectGraph> wkUserData;
if (userData)
wkUserData = WebKit::ObjCObjectGraph::create(userData);

_page->loadRequest(request, WebCore::ShouldOpenExternalURLsPolicy::ShouldNotAllow, wkUserData.get());
ASSERT(!userData);
_page->loadRequest(request, WebCore::ShouldOpenExternalURLsPolicy::ShouldNotAllow, { });
}

- (void)loadFileURL:(NSURL *)URL restrictToFilesWithin:(NSURL *)allowedDirectory
Expand All @@ -156,14 +153,11 @@ - (void)loadFileURL:(NSURL *)URL restrictToFilesWithin:(NSURL *)allowedDirectory

- (void)loadFileURL:(NSURL *)URL restrictToFilesWithin:(NSURL *)allowedDirectory userData:(id)userData
{
ASSERT(!userData);
if (![URL isFileURL] || (allowedDirectory && ![allowedDirectory isFileURL]))
[NSException raise:NSInvalidArgumentException format:@"Attempted to load a non-file URL"];

RefPtr<WebKit::ObjCObjectGraph> wkUserData;
if (userData)
wkUserData = WebKit::ObjCObjectGraph::create(userData);

_page->loadFile(bytesAsString(bridge_cast(URL)), bytesAsString(bridge_cast(allowedDirectory)), wkUserData.get());
_page->loadFile(bytesAsString(bridge_cast(URL)), bytesAsString(bridge_cast(allowedDirectory)), { });
}

- (void)loadHTMLString:(NSString *)HTMLString baseURL:(NSURL *)baseURL
Expand All @@ -173,12 +167,9 @@ - (void)loadHTMLString:(NSString *)HTMLString baseURL:(NSURL *)baseURL

- (void)loadHTMLString:(NSString *)HTMLString baseURL:(NSURL *)baseURL userData:(id)userData
{
RefPtr<WebKit::ObjCObjectGraph> wkUserData;
if (userData)
wkUserData = WebKit::ObjCObjectGraph::create(userData);

ASSERT(!userData);
NSData *data = [HTMLString dataUsingEncoding:NSUTF8StringEncoding];
_page->loadData(span(data), "text/html"_s, "UTF-8"_s, bytesAsString(bridge_cast(baseURL)), wkUserData.get());
_page->loadData(span(data), "text/html"_s, "UTF-8"_s, bytesAsString(bridge_cast(baseURL)), { });
}

- (void)loadAlternateHTMLString:(NSString *)string baseURL:(NSURL *)baseURL forUnreachableURL:(NSURL *)unreachableURL
Expand All @@ -194,11 +185,8 @@ - (void)loadData:(NSData *)data MIMEType:(NSString *)MIMEType textEncodingName:(

- (void)loadData:(NSData *)data MIMEType:(NSString *)MIMEType textEncodingName:(NSString *)encodingName baseURL:(NSURL *)baseURL userData:(id)userData
{
RefPtr<WebKit::ObjCObjectGraph> wkUserData;
if (userData)
wkUserData = WebKit::ObjCObjectGraph::create(userData);

_page->loadData(span(data), MIMEType, encodingName, bytesAsString(bridge_cast(baseURL)), wkUserData.get());
ASSERT(!userData);
_page->loadData(span(data), MIMEType, encodingName, bytesAsString(bridge_cast(baseURL)), { });
}

- (void)stopLoading
Expand Down
9 changes: 9 additions & 0 deletions Source/WebKit/UIProcess/API/ios/WKWebViewIOS.mm
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,15 @@ - (BOOL)_isBackground
return [_contentView isBackground];
}

ALLOW_DEPRECATED_DECLARATIONS_BEGIN
- (WKBrowsingContextController *)browsingContextController
{
if (linkedOnOrAfterSDKWithBehavior(SDKAlignedBehavior::BrowsingContextControllerSPIAccessRemoved))
return nil;
return [_contentView browsingContextController];
}
ALLOW_DEPRECATED_DECLARATIONS_END

- (BOOL)becomeFirstResponder
{
#if PLATFORM(VISION)
Expand Down
4 changes: 4 additions & 0 deletions Source/WebKit/UIProcess/ios/WKContentView.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ enum class ViewStabilityFlag : uint8_t;
WeakObjCPtr<WKWebView> _webView;
}

ALLOW_DEPRECATED_DECLARATIONS_BEGIN
@property (nonatomic, readonly) WKBrowsingContextController *browsingContextController;
ALLOW_DEPRECATED_DECLARATIONS_END

@property (nonatomic, readonly) WebKit::WebPageProxy* page;
@property (nonatomic, readonly) BOOL isFocusingElement;
@property (nonatomic, getter=isShowingInspectorIndication) BOOL showingInspectorIndication;
Expand Down
13 changes: 13 additions & 0 deletions Source/WebKit/UIProcess/ios/WKContentView.mm
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,9 @@ - (void)redo

@implementation WKContentView {
std::unique_ptr<WebKit::PageClientImpl> _pageClient;
ALLOW_DEPRECATED_DECLARATIONS_BEGIN
RetainPtr<WKBrowsingContextController> _browsingContextController;
ALLOW_DEPRECATED_DECLARATIONS_END

RetainPtr<UIView> _rootContentView;
RetainPtr<UIView> _fixedClippingView;
Expand Down Expand Up @@ -542,6 +545,16 @@ - (void)didMoveToWindow
[self cleanUpInteractionPreviewContainers];
}

ALLOW_DEPRECATED_DECLARATIONS_BEGIN
- (WKBrowsingContextController *)browsingContextController
{
if (!_browsingContextController)
_browsingContextController = adoptNS([[WKBrowsingContextController alloc] _initWithPageRef:toAPI(_page.get())]);

return _browsingContextController.get();
}
ALLOW_DEPRECATED_DECLARATIONS_END

- (WKPageRef)_pageRef
{
return toAPI(_page.get());
Expand Down

0 comments on commit 4112a1e

Please sign in to comment.