Skip to content

Commit

Permalink
Simplify use of WKWebViewContentProviderRegistry on iOS
Browse files Browse the repository at this point in the history
https://bugs.webkit.org/show_bug.cgi?id=271397
rdar://125177028

Reviewed by Tim Horton.

WKWebViewContentProviderRegistry is a small and simple class that allows a
WKWebView to get an ObjC class from a MIME type based on its preferences.
It does not need to be a member of WKWebViewConfiguration.
It does not need to know about its pages.
It does not need to add MIME types with the WebPage::AddMIMETypeWithCustomContentProvider
message because all the MIME types are sent in WebPageCreationParameters's
mimeTypesWithCustomContentProviders.

* Source/WebKit/UIProcess/API/Cocoa/WKWebView.mm:
(-[WKWebView _initializeWithConfiguration:]):
(-[WKWebView dealloc]):
* Source/WebKit/UIProcess/API/Cocoa/WKWebViewConfiguration.mm:
(-[WKWebViewConfiguration copyWithZone:]):
(-[WKWebViewConfiguration setLimitsNavigationsToAppBoundDomains:]):
(-[WKWebViewConfiguration _contentProviderRegistry]): Deleted.
(-[WKWebViewConfiguration _setContentProviderRegistry:]): Deleted.
* Source/WebKit/UIProcess/API/Cocoa/WKWebViewConfigurationInternal.h:
* Source/WebKit/UIProcess/API/Cocoa/WKWebViewInternal.h:
* Source/WebKit/UIProcess/API/ios/WKWebViewIOS.mm:
(-[WKWebView _contentProviderRegistry]):
(-[WKWebView _setHasCustomContentView:loadedMIMEType:]):
(-[WKWebView _isDisplayingPDF]):
* Source/WebKit/UIProcess/Cocoa/WKWebViewContentProviderRegistry.h:
* Source/WebKit/UIProcess/Cocoa/WKWebViewContentProviderRegistry.mm:
(-[WKWebViewContentProviderRegistry registerProvider:forMIMEType:]):
(-[WKWebViewContentProviderRegistry addPage:]): Deleted.
(-[WKWebViewContentProviderRegistry removePage:]): Deleted.
* Source/WebKit/UIProcess/WebPageProxy.cpp:
(WebKit::WebPageProxy::addMIMETypeWithCustomContentProvider): Deleted.
* Source/WebKit/UIProcess/WebPageProxy.h:
* Source/WebKit/WebProcess/WebPage/WebPage.cpp:
(WebKit::WebPage::addMIMETypeWithCustomContentProvider): Deleted.
* Source/WebKit/WebProcess/WebPage/WebPage.h:
* Source/WebKit/WebProcess/WebPage/WebPage.messages.in:

Canonical link: https://commits.webkit.org/276486@main
  • Loading branch information
achristensen07 committed Mar 21, 2024
1 parent b4de27e commit 29b1ce3
Show file tree
Hide file tree
Showing 12 changed files with 7 additions and 100 deletions.
2 changes: 0 additions & 2 deletions Source/WebKit/UIProcess/API/Cocoa/WKWebView.mm
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,6 @@ - (void)_initializeWithConfiguration:(WKWebViewConfiguration *)configuration

_contentView = adoptNS([[WKContentView alloc] initWithFrame:self.bounds processPool:processPool configuration:pageConfiguration.copyRef() webView:self]);
_page = [_contentView page];
[[_configuration _contentProviderRegistry] addPage:*_page];

[self _setupScrollAndContentViews];
if (!self.opaque || !pageConfiguration->drawsBackground())
Expand Down Expand Up @@ -654,7 +653,6 @@ - (void)dealloc

#if PLATFORM(IOS_FAMILY)
[_remoteObjectRegistry _invalidate];
[[_configuration _contentProviderRegistry] removePage:*_page];
[[NSNotificationCenter defaultCenter] removeObserver:self];
[_scrollView setInternalDelegate:nil];

Expand Down
51 changes: 0 additions & 51 deletions Source/WebKit/UIProcess/API/Cocoa/WKWebViewConfiguration.mm
Original file line number Diff line number Diff line change
Expand Up @@ -63,43 +63,6 @@
#import "_WKWebExtensionControllerInternal.h"
#endif

template<typename T> class LazyInitialized {
public:
typedef typename WTF::GetPtrHelper<T>::PtrType PtrType;

template<typename F>
PtrType get(F&& f)
{
if (!m_isInitialized) {
m_value = f();
m_isInitialized = true;
}

return m_value.get();
}

void set(PtrType t)
{
m_value = t;
m_isInitialized = true;
}

void set(T&& t)
{
m_value = WTFMove(t);
m_isInitialized = true;
}

PtrType peek()
{
return m_value.get();
}

private:
bool m_isInitialized = false;
T m_value;
};

#if PLATFORM(IOS_FAMILY)

static _WKDragLiftDelay toDragLiftDelay(NSUInteger value)
Expand Down Expand Up @@ -132,7 +95,6 @@ @implementation WKWebViewConfiguration {
std::optional<RetainPtr<NSString>> _applicationNameForUserAgent;

#if PLATFORM(IOS_FAMILY)
LazyInitialized<RetainPtr<WKWebViewContentProviderRegistry>> _contentProviderRegistry;
BOOL _allowsInlineMediaPlayback;
BOOL _inlineMediaPlaybackRequiresPlaysInlineAttribute;
BOOL _allowsInlineMediaPlaybackAfterFullscreen;
Expand Down Expand Up @@ -341,9 +303,6 @@ - (id)copyWithZone:(NSZone *)zone
configuration._relatedWebView = _relatedWebView.get().get();
configuration._webViewToCloneSessionStorageFrom = _webViewToCloneSessionStorageFrom.get().get();
configuration._alternateWebViewForNavigationGestures = _alternateWebViewForNavigationGestures.get().get();
#if PLATFORM(IOS_FAMILY)
configuration._contentProviderRegistry = self._contentProviderRegistry;
#endif

configuration->_suppressesIncrementalRendering = self->_suppressesIncrementalRendering;
configuration->_applicationNameForUserAgent = self->_applicationNameForUserAgent;
Expand Down Expand Up @@ -580,16 +539,6 @@ - (void)setLimitsNavigationsToAppBoundDomains:(BOOL)limitsToAppBoundDomains
{
_pageConfiguration->setLimitsNavigationsToAppBoundDomains(limitsToAppBoundDomains);
}

- (WKWebViewContentProviderRegistry *)_contentProviderRegistry
{
return _contentProviderRegistry.get([self] { return adoptNS([[WKWebViewContentProviderRegistry alloc] initWithConfiguration:self]); });
}

- (void)_setContentProviderRegistry:(WKWebViewContentProviderRegistry *)registry
{
_contentProviderRegistry.set(registry);
}
#endif

- (Ref<API::PageConfiguration>)copyPageConfiguration
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,6 @@

@interface WKWebViewConfiguration ()

#if PLATFORM(IOS_FAMILY)
@property (nonatomic, setter=_setContentProviderRegistry:) WKWebViewContentProviderRegistry *_contentProviderRegistry;
#endif
@property (nonatomic, readonly) NSString *_applicationNameForDesktopUserAgent;

- (Ref<API::PageConfiguration>)copyPageConfiguration;
Expand Down
2 changes: 1 addition & 1 deletion Source/WebKit/UIProcess/API/Cocoa/WKWebViewInternal.h
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ struct PerWebProcessState {
RetainPtr<WKContentView> _contentView;
std::unique_ptr<WebKit::ViewGestureController> _gestureController;
Vector<BlockPtr<void ()>> _visibleContentRectUpdateCallbacks;

RetainPtr<WKWebViewContentProviderRegistry> _contentProviderRegistry;
#if ENABLE(FULLSCREEN_API)
RetainPtr<WKFullScreenWindowController> _fullScreenWindowController;
#endif
Expand Down
8 changes: 5 additions & 3 deletions Source/WebKit/UIProcess/API/ios/WKWebViewIOS.mm
Original file line number Diff line number Diff line change
Expand Up @@ -479,7 +479,9 @@ - (UIView *)_currentContentView

- (WKWebViewContentProviderRegistry *)_contentProviderRegistry
{
return [_configuration _contentProviderRegistry];
if (!self->_contentProviderRegistry)
self->_contentProviderRegistry = adoptNS([[WKWebViewContentProviderRegistry alloc] initWithConfiguration:self.configuration]);
return self->_contentProviderRegistry.get();
}

- (WKSelectionGranularity)_selectionGranularity
Expand All @@ -491,7 +493,7 @@ - (void)_setHasCustomContentView:(BOOL)pageHasCustomContentView loadedMIMEType:(
{
Class representationClass = nil;
if (pageHasCustomContentView)
representationClass = [[_configuration _contentProviderRegistry] providerForMIMEType:mimeType];
representationClass = [[self _contentProviderRegistry] providerForMIMEType:mimeType];

if (pageHasCustomContentView && representationClass) {
[_customContentView removeFromSuperview];
Expand Down Expand Up @@ -3922,7 +3924,7 @@ - (BOOL)_allowsViewportShrinkToFit
- (BOOL)_isDisplayingPDF
{
for (auto& type : WebCore::MIMETypeRegistry::pdfMIMETypes()) {
Class providerClass = [[_configuration _contentProviderRegistry] providerForMIMEType:@(type.characters())];
Class providerClass = [[self _contentProviderRegistry] providerForMIMEType:@(type.characters())];
if ([_customContentView isKindOfClass:providerClass])
return YES;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,7 @@

#if PLATFORM(IOS_FAMILY)

#import <wtf/text/WTFString.h>

namespace WebKit {
class WebPageProxy;
}
#import <wtf/Forward.h>

@class WKWebViewConfiguration;
@protocol WKWebViewContentProvider;
Expand All @@ -40,10 +36,6 @@ class WebPageProxy;

- (instancetype)initWithConfiguration:(WKWebViewConfiguration *)configuration;

- (void)addPage:(WebKit::WebPageProxy&)page;
- (void)removePage:(WebKit::WebPageProxy&)page;

- (void)registerProvider:(Class <WKWebViewContentProvider>)contentProvider forMIMEType:(const String&)mimeType;
- (Class <WKWebViewContentProvider>)providerForMIMEType:(const String&)mimeType;

- (Vector<String>)_mimeTypesWithCustomContentProviders;
Expand Down
16 changes: 0 additions & 16 deletions Source/WebKit/UIProcess/Cocoa/WKWebViewContentProviderRegistry.mm
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@

@implementation WKWebViewContentProviderRegistry {
HashMap<String, Class <WKWebViewContentProvider>, ASCIICaseInsensitiveHash> _contentProviderForMIMEType;
HashCountedSet<WebKit::WebPageProxy*> _pages;
}

- (instancetype)initWithConfiguration:(WKWebViewConfiguration *)configuration
Expand All @@ -68,24 +67,9 @@ - (instancetype)initWithConfiguration:(WKWebViewConfiguration *)configuration
return self;
}

- (void)addPage:(WebKit::WebPageProxy&)page
{
ASSERT(!_pages.contains(&page));
_pages.add(&page);
}

- (void)removePage:(WebKit::WebPageProxy&)page
{
ASSERT(_pages.contains(&page));
_pages.remove(&page);
}

- (void)registerProvider:(Class <WKWebViewContentProvider>)contentProvider forMIMEType:(const String&)mimeType
{
_contentProviderForMIMEType.set(mimeType, contentProvider);

for (auto& page : _pages)
page.key->addMIMETypeWithCustomContentProvider(mimeType);
}

- (Class <WKWebViewContentProvider>)providerForMIMEType:(const String&)mimeType
Expand Down
5 changes: 0 additions & 5 deletions Source/WebKit/UIProcess/WebPageProxy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11273,11 +11273,6 @@ void WebPageProxy::unwrapCryptoKey(const Vector<uint8_t>& wrappedKey, Completion
completionHandler(std::optional<Vector<uint8_t>>());
}

void WebPageProxy::addMIMETypeWithCustomContentProvider(const String& mimeType)
{
send(Messages::WebPage::AddMIMETypeWithCustomContentProvider(mimeType));
}

void WebPageProxy::changeFontAttributes(WebCore::FontAttributeChanges&& changes)
{
if (!hasRunningProcess())
Expand Down
2 changes: 0 additions & 2 deletions Source/WebKit/UIProcess/WebPageProxy.h
Original file line number Diff line number Diff line change
Expand Up @@ -894,8 +894,6 @@ class WebPageProxy final : public API::ObjectImpl<API::Object::Type::Page>, publ

WindowKind windowKind() const;

void addMIMETypeWithCustomContentProvider(const String& mimeType);

void selectAll();
void executeEditCommand(const String& commandName, const String& argument = String());
void executeEditCommand(const String& commandName, const String& argument, CompletionHandler<void()>&&);
Expand Down
5 changes: 0 additions & 5 deletions Source/WebKit/WebProcess/WebPage/WebPage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5878,11 +5878,6 @@ bool WebPage::mainFrameHasCustomContentProvider() const
return false;
}

void WebPage::addMIMETypeWithCustomContentProvider(const String& mimeType)
{
m_mimeTypesWithCustomContentProviders.add(mimeType);
}

void WebPage::updateMainFrameScrollOffsetPinning()
{
RefPtr frameView = localMainFrameView();
Expand Down
1 change: 0 additions & 1 deletion Source/WebKit/WebProcess/WebPage/WebPage.h
Original file line number Diff line number Diff line change
Expand Up @@ -1200,7 +1200,6 @@ class WebPage : public API::ObjectImpl<API::Object::Type::BundlePage>, public IP
void updateMainFrameScrollOffsetPinning();

bool mainFrameHasCustomContentProvider() const;
void addMIMETypeWithCustomContentProvider(const String&);

void mainFrameDidLayout();

Expand Down
2 changes: 0 additions & 2 deletions Source/WebKit/WebProcess/WebPage/WebPage.messages.in
Original file line number Diff line number Diff line change
Expand Up @@ -353,8 +353,6 @@ GenerateSyntheticEditingCommand(enum:uint8_t WebKit::SyntheticEditingCommandType
RequestRectForFoundTextRange(struct WebKit::WebFoundTextRange range) -> (WebCore::FloatRect rect)
AddLayerForFindOverlay() -> (WebCore::PlatformLayerIdentifier findLayerID)
RemoveLayerForFindOverlay() -> ()

AddMIMETypeWithCustomContentProvider(String mimeType)

# Drag and drop.
#if PLATFORM(GTK) && ENABLE(DRAG_SUPPORT)
Expand Down

0 comments on commit 29b1ce3

Please sign in to comment.