Skip to content

Commit

Permalink
Remove respondsToSelector checks from where they are no longer needed
Browse files Browse the repository at this point in the history
https://bugs.webkit.org/show_bug.cgi?id=252901

Reviewed by Aditya Keerthi.

In some cases, we know the selectors we are checking for exist because
they are unconditionally included in the header files.

Other times, we know they exist because the OSes that do not support
these methods are no longer supported.

According to https://build.webkit.org/dashboard/, we no longer support:
- Anything before macOS 12
- Anything before tvOS 16
- Anything before iOS 16
- Anything before watchOS 9

So any respondsToSelector calls for the sake of the OSes that do not fit
the criteria above can be removed.

* Source/WTF/wtf/PlatformHave.h: Added HAVE_NSSHARINGSERVICEPICKER
  macro.

* Source/WebCore/bridge/objc/objc_instance.mm:
  (JSC::Bindings::ObjcInstance::setValueOfUndefinedField):
  Remove comment because it is misleading.
  (JSC::Bindings::ObjcInstance::getValueOfUndefinedField const): Ditto.

* Source/WebCore/platform/graphics/avfoundation/objc/CDMInstanceFairPlayStreamingAVFObjC.mm:
  (WebCore::initTypeForRequest): Remove check because
  AVContentKeyRequest.options was added in 10.14.4, and earliest
  supported macOS is macOS 12.

* Source/WebCore/platform/graphics/avfoundation/objc/MediaPlayerPrivateMediaSourceAVFObjC.mm:
  (WebCore::MediaPlayerPrivateMediaSourceAVFObjC::playInternal):
  Remove check because setRate:time:atHostTime: is now a public API.
  (WebCore::MediaPlayerPrivateMediaSourceAVFObjC::pauseInternal): Ditto.

* Source/WebCore/platform/network/cocoa/CookieCocoa.mm: Remove check
  because sameSitePolicy is now a public API.

* Source/WebKit/Platform/cocoa/ImageAnalysisUtilities.mm:
  (WebKit::makeTextRecognitionResult): Remove check because
  isPassthrough is explicitly defined in the interface definition in the
  header.

* Source/WebKit/UIProcess/mac/WebContextMenuProxyMac.mm:
  (WebKit::WebContextMenuProxyMac::getShareMenuItem): Replace selector
  check with a check for HAVE_NSSHARINGSERVICEPICKER because
  standardShareMenuItem is definitely a Ventura addition. Because
  getStandardShareMenuItem cannot be called from macOS 13 and above due
  to NSSHARINGSERVICEPICKER being available, getStandardShareMenuItem
  has been wrapped around a check for not having NSSHARINGSERVICEPICKER.

* Source/WebKitLegacy/mac/WebView/WebView.mm:
  (-[WebView _handleContextMenuTranslation:]): Remove check because the
  modern counterpart does not check either, meaning this is safe to
  remove.

Canonical link: https://commits.webkit.org/266235@main
  • Loading branch information
AtariDreams authored and Ahmad Saleem committed Jul 23, 2023
1 parent 87011db commit b5e4644
Show file tree
Hide file tree
Showing 11 changed files with 21 additions and 39 deletions.
1 change: 1 addition & 0 deletions Source/WTF/wtf/PlatformHave.h
Original file line number Diff line number Diff line change
Expand Up @@ -1198,6 +1198,7 @@

#if PLATFORM(MAC) && __MAC_OS_X_VERSION_MIN_REQUIRED >= 130000
#define HAVE_SHARING_SERVICE_PICKER_POPOVER_SPI 1
#define HAVE_SHARING_SERVICE_PICKER_STANDARD_SHARE_MENU_ITEM 1
#endif

#if PLATFORM(MAC)
Expand Down
11 changes: 3 additions & 8 deletions Source/WebCore/bridge/objc/objc_instance.mm
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ - (NSString *)_web_description;
wrapperCache().remove((__bridge CFTypeRef)_instance.get());

if ([_instance respondsToSelector:@selector(finalizeForWebScript)])
[_instance performSelector:@selector(finalizeForWebScript)];
[_instance finalizeForWebScript];
_instance = 0;
}
}
Expand Down Expand Up @@ -404,14 +404,12 @@ void finishCreation(VM& vm, const String& name)

JSLock::DropAllLocks dropAllLocks(lexicalGlobalObject); // Can't put this inside the @try scope because it unwinds incorrectly.

// This check is not really necessary because NSObject implements
// setValue:forUndefinedKey:, and unfortunately the default implementation
// throws an exception.
if ([targetObject respondsToSelector:@selector(setValue:forUndefinedKey:)]){
setGlobalException(nil);

ObjcValue objcValue = convertValueToObjcValue(lexicalGlobalObject, aValue, ObjcObjectType);

// Default implementation throws an exception.
@try {
[targetObject setValue:(__bridge id)objcValue.objectValue forUndefinedKey:[NSString stringWithCString:name.ascii().data() encoding:NSASCIIStringEncoding]];
} @catch(NSException* localException) {
Expand All @@ -436,12 +434,9 @@ void finishCreation(VM& vm, const String& name)

JSLock::DropAllLocks dropAllLocks(lexicalGlobalObject); // Can't put this inside the @try scope because it unwinds incorrectly.

// This check is not really necessary because NSObject implements
// valueForUndefinedKey:, and unfortunately the default implementation
// throws an exception.
if ([targetObject respondsToSelector:@selector(valueForUndefinedKey:)]){
setGlobalException(nil);

// Default implementaion throws an exception.
@try {
id objcValue = [targetObject valueForUndefinedKey:[NSString stringWithCString:name.ascii().data() encoding:NSASCIIStringEncoding]];
result = convertObjcValueToValue(lexicalGlobalObject, &objcValue, ObjcObjectType, m_rootObject.get());
Expand Down
2 changes: 1 addition & 1 deletion Source/WebCore/editing/cocoa/HTMLConverter.mm
Original file line number Diff line number Diff line change
Expand Up @@ -1275,7 +1275,7 @@ static Class _WebMessageDocumentClass()
NSTextAttachment *mimeTextAttachment = nil;
[WebMessageDocumentClass document:NULL attachment:&mimeTextAttachment forURL:url];
if (mimeTextAttachment && [mimeTextAttachment respondsToSelector:@selector(fileWrapper)]) {
fileWrapper = [mimeTextAttachment performSelector:@selector(fileWrapper)];
fileWrapper = [mimeTextAttachment fileWrapper];
ignoreOrientation = NO;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,15 +204,7 @@ static AtomString initTypeForRequest(AVContentKeyRequest* request)
if ([request.identifier isKindOfClass:NSString.class] && [request.identifier hasPrefix:@"skd://"])
return CDMPrivateFairPlayStreaming::skdName();

if (![request respondsToSelector:@selector(options)]) {
// AVContentKeyRequest.options was added in 10.14.4; if we are running on a previous version
// we don't have support for 'cenc' anyway, so just assume 'sinf'.
return CDMPrivateFairPlayStreaming::sinfName();
}

ALLOW_NEW_API_WITHOUT_GUARDS_BEGIN
auto nsInitType = (NSString*)[request.options valueForKey:InitializationDataTypeKey];
ALLOW_NEW_API_WITHOUT_GUARDS_END
if (![nsInitType isKindOfClass:NSString.class]) {
// The only way for an initialization data to end up here without an appropriate key in
// the options dictionary is for that data to have been generated by the AVStreamDataParser
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -358,14 +358,12 @@ void getSupportedTypes(HashSet<String, ASCIICaseInsensitiveHash>& types) const f
if (!shouldBePlaying())
return;

ALLOW_NEW_API_WITHOUT_GUARDS_BEGIN
if (hostTime && [m_synchronizer respondsToSelector:@selector(setRate:time:atHostTime:)]) {
if (hostTime) {
auto cmHostTime = PAL::CMClockMakeHostTimeFromSystemUnits(hostTime->toMachAbsoluteTime());
ALWAYS_LOG(LOGIDENTIFIER, "setting rate to ", m_rate, " at host time ", PAL::CMTimeGetSeconds(cmHostTime));
[m_synchronizer setRate:m_rate time:PAL::kCMTimeInvalid atHostTime:cmHostTime];
} else
[m_synchronizer setRate:m_rate];
ALLOW_NEW_API_WITHOUT_GUARDS_END
}

void MediaPlayerPrivateMediaSourceAVFObjC::pause()
Expand All @@ -379,14 +377,12 @@ void getSupportedTypes(HashSet<String, ASCIICaseInsensitiveHash>& types) const f
ALWAYS_LOG(LOGIDENTIFIER);
m_playing = false;

ALLOW_NEW_API_WITHOUT_GUARDS_BEGIN
if (hostTime && [m_synchronizer respondsToSelector:@selector(setRate:time:atHostTime:)]) {
if (hostTime) {
auto cmHostTime = PAL::CMClockMakeHostTimeFromSystemUnits(hostTime->toMachAbsoluteTime());
ALWAYS_LOG(LOGIDENTIFIER, "setting rate to 0 at host time ", PAL::CMTimeGetSeconds(cmHostTime));
[m_synchronizer setRate:0 time:PAL::kCMTimeInvalid atHostTime:cmHostTime];
} else
[m_synchronizer setRate:0];
ALLOW_NEW_API_WITHOUT_GUARDS_END
}

bool MediaPlayerPrivateMediaSourceAVFObjC::paused() const
Expand Down
5 changes: 1 addition & 4 deletions Source/WebCore/platform/network/cocoa/CookieCocoa.mm
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,7 @@ static NSHTTPCookieStringPolicy _Nullable nsSameSitePolicy(Cookie::SameSitePolic
, commentURL { cookie.commentURL }
, ports { portVectorFromList(cookie.portList) }
{
ALLOW_NEW_API_WITHOUT_GUARDS_BEGIN
if ([cookie respondsToSelector:@selector(sameSitePolicy)])
sameSite = coreSameSitePolicy(cookie.sameSitePolicy);
ALLOW_NEW_API_WITHOUT_GUARDS_END
sameSite = coreSameSitePolicy(cookie.sameSitePolicy);
}

Cookie::operator NSHTTPCookie * _Nullable () const
Expand Down
2 changes: 1 addition & 1 deletion Source/WebKit/Platform/cocoa/ImageAnalysisUtilities.mm
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ static TextRecognitionResult makeTextRecognitionResult(VKCImageAnalysisTranslati
continue;
}

if ([paragraph respondsToSelector:@selector(isPassthrough)] && [paragraph isPassthrough])
if (paragraph.isPassthrough)
continue;

auto quad = floatQuad(paragraph.quad);
Expand Down
17 changes: 9 additions & 8 deletions Source/WebKit/UIProcess/mac/WebContextMenuProxyMac.mm
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,7 @@ - (void)menuDidClose:(NSMenu *)menu
#endif // ENABLE(IMAGE_ANALYSIS_ENHANCEMENTS)
}

#if !HAVE(SHARING_SERVICE_PICKER_STANDARD_SHARE_MENU_ITEM)
static void getStandardShareMenuItem(NSArray *items, void (^completionHandler)(NSMenuItem *))
{
#if HAVE(NSSHARINGSERVICEPICKER_ASYNC_MENUS)
Expand All @@ -403,6 +404,7 @@ static void getStandardShareMenuItem(NSArray *items, void (^completionHandler)(N
completionHandler([NSMenuItem standardShareMenuItemForItems:items]);
#endif
}
#endif

void WebContextMenuProxyMac::getShareMenuItem(CompletionHandler<void(NSMenuItem *)>&& completionHandler)
{
Expand Down Expand Up @@ -446,15 +448,13 @@ static void getStandardShareMenuItem(NSArray *items, void (^completionHandler)(N
return;
}

#if HAVE(SHARING_SERVICE_PICKER_STANDARD_SHARE_MENU_ITEM)
auto sharingServicePicker = adoptNS([[NSSharingServicePicker alloc] initWithItems:items.get()]);
if ([sharingServicePicker respondsToSelector:@selector(standardShareMenuItem)]) {
NSMenuItem *shareMenuItem = [sharingServicePicker standardShareMenuItem];
[shareMenuItem setRepresentedObject:sharingServicePicker.get()];
shareMenuItem.identifier = _WKMenuItemIdentifierShareMenu;
completionHandler(shareMenuItem);
return;
}

NSMenuItem *shareMenuItem = [sharingServicePicker standardShareMenuItem];
[shareMenuItem setRepresentedObject:sharingServicePicker.get()];
shareMenuItem.identifier = _WKMenuItemIdentifierShareMenu;
completionHandler(shareMenuItem);
#else
getStandardShareMenuItem(items.get(), makeBlockPtr([completionHandler = WTFMove(completionHandler), protectedThis = Ref { *this }, this](NSMenuItem *item) mutable {
if (!item) {
completionHandler(nil);
Expand All @@ -476,6 +476,7 @@ static void getStandardShareMenuItem(NSArray *items, void (^completionHandler)(N

completionHandler(item);
}).get());
#endif
}
#endif

Expand Down
2 changes: 1 addition & 1 deletion Source/WebKitLegacy/mac/WebView/WebDataSource.mm
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ - (void)_makeRepresentation

#if PLATFORM(IOS_FAMILY)
if ([repClass respondsToSelector:@selector(_representationClassForWebFrame:)])
repClass = [repClass performSelector:@selector(_representationClassForWebFrame:) withObject:[self webFrame]];
repClass = [repClass _representationClassForWebFrame:[self webFrame]];
#endif

// Check if the data source was already bound?
Expand Down
2 changes: 1 addition & 1 deletion Source/WebKitLegacy/mac/WebView/WebFrameView.mm
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ - (Class)_viewClassForMIMEType:(NSString *)MIMEType

#if PLATFORM(IOS_FAMILY)
if ([retVal respondsToSelector:@selector(_representationClassForWebFrame:)])
retVal = [retVal performSelector:@selector(_representationClassForWebFrame:) withObject:[self webFrame]];
retVal = [retVal _representationClassForWebFrame:[self webFrame]];
#endif

return retVal;
Expand Down
2 changes: 1 addition & 1 deletion Source/WebKitLegacy/mac/WebView/WebView.mm
Original file line number Diff line number Diff line change
Expand Up @@ -9586,7 +9586,7 @@ - (void)_handleContextMenuTranslation:(const WebCore::TranslationContextMenuInfo

auto translationViewController = adoptNS([PAL::allocLTUITranslationViewControllerInstance() init]);
[translationViewController setText:adoptNS([[NSAttributedString alloc] initWithString:info.text]).get()];
if (info.mode == WebCore::TranslationContextMenuMode::Editable && [translationViewController respondsToSelector:@selector(setReplacementHandler:)]) {
if (info.mode == WebCore::TranslationContextMenuMode::Editable) {
[translationViewController setIsSourceEditable:YES];
[translationViewController setReplacementHandler:[weakSelf = WeakObjCPtr<WebView>(self)](NSAttributedString *string) {
auto strongSelf = weakSelf.get();
Expand Down

0 comments on commit b5e4644

Please sign in to comment.