commit 56af64582232109aee0558c5b7b0906bf0be76f5
parent 2f8accd2ead8db716892cd663eafa0b9e4aebebd
Author: Josh Aas <jaas@kflag.net>
Date: Fri, 14 Nov 2025 14:39:06 +0000
Bug 1998588 - improve nsOSHelperAppService::OSProtocolHandlerExists on macOS. r=spohl
Differential Revision: https://phabricator.services.mozilla.com/D271534
Diffstat:
1 file changed, 15 insertions(+), 25 deletions(-)
diff --git a/uriloader/exthandler/mac/nsOSHelperAppService.mm b/uriloader/exthandler/mac/nsOSHelperAppService.mm
@@ -20,7 +20,9 @@
#include "nsCRT.h"
#include "nsMIMEInfoMac.h"
#include "nsEmbedCID.h"
+#include "nsCocoaUtils.h"
+#import <Cocoa/Cocoa.h>
#import <CoreFoundation/CoreFoundation.h>
#import <ApplicationServices/ApplicationServices.h>
@@ -88,37 +90,26 @@ nsOSHelperAppService::~nsOSHelperAppService() {}
nsresult nsOSHelperAppService::OSProtocolHandlerExists(
const char* aProtocolScheme, bool* aHandlerExists) {
+ NS_OBJC_BEGIN_TRY_BLOCK_RETURN;
+
*aHandlerExists = false;
- // CFStringCreateWithBytes() can fail even if we're not out of memory --
- // for example if the 'bytes' parameter is something very weird (like
- // "\xFF\xFF~"), or possibly if it can't be interpreted as using what's
- // specified in the 'encoding' parameter. See bug 548719.
- CFStringRef schemeString = ::CFStringCreateWithBytes(
- kCFAllocatorDefault, (const UInt8*)aProtocolScheme,
- strlen(aProtocolScheme), kCFStringEncodingUTF8, false);
+ nsAutoreleasePool localPool;
+
+ NSString* schemeString = [NSString stringWithCString:aProtocolScheme
+ encoding:NSUTF8StringEncoding];
+ schemeString = [schemeString stringByAppendingString:@"://"];
if (!schemeString) {
return NS_ERROR_FAILURE;
}
- // LSCopyDefaultHandlerForURLScheme() can fail to find the default handler
- // for aProtocolScheme when it's never been explicitly set (using
- // LSSetDefaultHandlerForURLScheme()). For example, Safari is the default
- // handler for the "http" scheme on a newly installed copy of OS X. But
- // this (presumably) wasn't done using LSSetDefaultHandlerForURLScheme(),
- // so LSCopyDefaultHandlerForURLScheme() will fail to find Safari. To get
- // around this we use LSCopyAllHandlersForURLScheme() instead -- which seems
- // never to fail.
- // http://lists.apple.com/archives/Carbon-dev/2007/May/msg00349.html
- // http://www.realsoftware.com/listarchives/realbasic-nug/2008-02/msg00119.html
- CFArrayRef handlerArray = ::LSCopyAllHandlersForURLScheme(schemeString);
- ::CFRelease(schemeString);
- if (handlerArray) {
- *aHandlerExists = true;
- ::CFRelease(handlerArray);
- }
+ NSURL* url = [[NSWorkspace sharedWorkspace]
+ URLForApplicationToOpenURL:[NSURL URLWithString:schemeString]];
+ *aHandlerExists = !!url;
return NS_OK;
+
+ NS_OBJC_END_TRY_BLOCK_RETURN(NS_ERROR_FAILURE);
}
NS_IMETHODIMP nsOSHelperAppService::GetApplicationDescription(
@@ -333,7 +324,7 @@ nsresult nsOSHelperAppService::GetMIMEInfoFromOS(const nsACString& aMIMEType,
// Create a Mac-specific MIME info so we can use Mac-specific members.
RefPtr<nsMIMEInfoMac> mimeInfoMac = new nsMIMEInfoMac(aMIMEType);
- NSAutoreleasePool* localPool = [[NSAutoreleasePool alloc] init];
+ nsAutoreleasePool localPool;
OSStatus err;
bool haveAppForType = false;
@@ -557,7 +548,6 @@ nsresult nsOSHelperAppService::GetMIMEInfoFromOS(const nsACString& aMIMEType,
MOZ_LOG(sLog, LogLevel::Debug,
("OS gave us: type '%s' found '%i'\n", mimeType.get(), *aFound));
- [localPool release];
mimeInfoMac.forget(aMIMEInfo);
return NS_OK;