michael@0: /* -*- Mode: C++; tab-width: 3; indent-tabs-mode: nil; c-basic-offset: 2 -*- michael@0: * michael@0: * This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include michael@0: #include michael@0: #include "nsOSHelperAppService.h" michael@0: #include "nsObjCExceptions.h" michael@0: #include "nsISupports.h" michael@0: #include "nsString.h" michael@0: #include "nsTArray.h" michael@0: #include "nsXPIDLString.h" michael@0: #include "nsIURL.h" michael@0: #include "nsIFile.h" michael@0: #include "nsILocalFileMac.h" michael@0: #include "nsMimeTypes.h" michael@0: #include "nsIStringBundle.h" michael@0: #include "nsIPromptService.h" michael@0: #include "nsMemory.h" michael@0: #include "nsCRT.h" michael@0: #include "nsMIMEInfoMac.h" michael@0: #include "nsEmbedCID.h" michael@0: michael@0: #import michael@0: #import michael@0: michael@0: // chrome URL's michael@0: #define HELPERAPPLAUNCHER_BUNDLE_URL "chrome://global/locale/helperAppLauncher.properties" michael@0: #define BRAND_BUNDLE_URL "chrome://branding/locale/brand.properties" michael@0: michael@0: /* This is an undocumented interface (in the Foundation framework) that has michael@0: * been stable since at least 10.2.8 and is still present on SnowLeopard. michael@0: * Furthermore WebKit has three public methods (in WebKitSystemInterface.h) michael@0: * that are thin wrappers around this interface's last three methods. So michael@0: * it's unlikely to change anytime soon. Now that we're no longer using michael@0: * Internet Config Services, this is the only way to look up a MIME type michael@0: * from an extension, or vice versa. michael@0: */ michael@0: @class NSURLFileTypeMappingsInternal; michael@0: michael@0: @interface NSURLFileTypeMappings : NSObject michael@0: { michael@0: NSURLFileTypeMappingsInternal *_internal; michael@0: } michael@0: michael@0: + (NSURLFileTypeMappings*)sharedMappings; michael@0: - (NSString*)MIMETypeForExtension:(NSString*)aString; michael@0: - (NSString*)preferredExtensionForMIMEType:(NSString*)aString; michael@0: - (NSArray*)extensionsForMIMEType:(NSString*)aString; michael@0: @end michael@0: michael@0: nsOSHelperAppService::nsOSHelperAppService() : nsExternalHelperAppService() michael@0: { michael@0: mode_t mask = umask(0777); michael@0: umask(mask); michael@0: mPermissions = 0666 & ~mask; michael@0: } michael@0: michael@0: nsOSHelperAppService::~nsOSHelperAppService() michael@0: {} michael@0: michael@0: nsresult nsOSHelperAppService::OSProtocolHandlerExists(const char * aProtocolScheme, bool * aHandlerExists) michael@0: { michael@0: // CFStringCreateWithBytes() can fail even if we're not out of memory -- michael@0: // for example if the 'bytes' parameter is something very wierd (like "ÿÿ~" michael@0: // aka "\xFF\xFF~"), or possibly if it can't be interpreted as using what's michael@0: // specified in the 'encoding' parameter. See bug 548719. michael@0: CFStringRef schemeString = ::CFStringCreateWithBytes(kCFAllocatorDefault, michael@0: (const UInt8*)aProtocolScheme, michael@0: strlen(aProtocolScheme), michael@0: kCFStringEncodingUTF8, michael@0: false); michael@0: if (schemeString) { michael@0: // LSCopyDefaultHandlerForURLScheme() can fail to find the default handler michael@0: // for aProtocolScheme when it's never been explicitly set (using michael@0: // LSSetDefaultHandlerForURLScheme()). For example, Safari is the default michael@0: // handler for the "http" scheme on a newly installed copy of OS X. But michael@0: // this (presumably) wasn't done using LSSetDefaultHandlerForURLScheme(), michael@0: // so LSCopyDefaultHandlerForURLScheme() will fail to find Safari. To get michael@0: // around this we use LSCopyAllHandlersForURLScheme() instead -- which seems michael@0: // never to fail. michael@0: // http://lists.apple.com/archives/Carbon-dev/2007/May/msg00349.html michael@0: // http://www.realsoftware.com/listarchives/realbasic-nug/2008-02/msg00119.html michael@0: CFArrayRef handlerArray = ::LSCopyAllHandlersForURLScheme(schemeString); michael@0: *aHandlerExists = !!handlerArray; michael@0: if (handlerArray) michael@0: ::CFRelease(handlerArray); michael@0: ::CFRelease(schemeString); michael@0: } else { michael@0: *aHandlerExists = false; michael@0: } michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP nsOSHelperAppService::GetApplicationDescription(const nsACString& aScheme, nsAString& _retval) michael@0: { michael@0: NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT; michael@0: michael@0: nsresult rv = NS_ERROR_NOT_AVAILABLE; michael@0: michael@0: CFStringRef schemeCFString = michael@0: ::CFStringCreateWithBytes(kCFAllocatorDefault, michael@0: (const UInt8 *)PromiseFlatCString(aScheme).get(), michael@0: aScheme.Length(), michael@0: kCFStringEncodingUTF8, michael@0: false); michael@0: michael@0: if (schemeCFString) { michael@0: CFStringRef lookupCFString = ::CFStringCreateWithFormat(NULL, NULL, CFSTR("%@:"), schemeCFString); michael@0: michael@0: if (lookupCFString) { michael@0: CFURLRef lookupCFURL = ::CFURLCreateWithString(NULL, lookupCFString, NULL); michael@0: michael@0: if (lookupCFURL) { michael@0: CFURLRef appCFURL = NULL; michael@0: OSStatus theErr = ::LSGetApplicationForURL(lookupCFURL, kLSRolesAll, NULL, &appCFURL); michael@0: michael@0: if (theErr == noErr) { michael@0: CFBundleRef handlerBundle = ::CFBundleCreate(NULL, appCFURL); michael@0: michael@0: if (handlerBundle) { michael@0: // Get the human-readable name of the default handler bundle michael@0: CFStringRef bundleName = michael@0: (CFStringRef)::CFBundleGetValueForInfoDictionaryKey(handlerBundle, michael@0: kCFBundleNameKey); michael@0: michael@0: if (bundleName) { michael@0: nsAutoTArray buffer; michael@0: CFIndex bundleNameLength = ::CFStringGetLength(bundleName); michael@0: buffer.SetLength(bundleNameLength); michael@0: ::CFStringGetCharacters(bundleName, CFRangeMake(0, bundleNameLength), michael@0: buffer.Elements()); michael@0: _retval.Assign(reinterpret_cast(buffer.Elements()), bundleNameLength); michael@0: rv = NS_OK; michael@0: } michael@0: michael@0: ::CFRelease(handlerBundle); michael@0: } michael@0: michael@0: ::CFRelease(appCFURL); michael@0: } michael@0: michael@0: ::CFRelease(lookupCFURL); michael@0: } michael@0: michael@0: ::CFRelease(lookupCFString); michael@0: } michael@0: michael@0: ::CFRelease(schemeCFString); michael@0: } michael@0: michael@0: return rv; michael@0: michael@0: NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT; michael@0: } michael@0: michael@0: nsresult nsOSHelperAppService::GetFileTokenForPath(const char16_t * aPlatformAppPath, nsIFile ** aFile) michael@0: { michael@0: NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT; michael@0: michael@0: nsresult rv; michael@0: nsCOMPtr localFile (do_CreateInstance(NS_LOCAL_FILE_CONTRACTID, &rv)); michael@0: NS_ENSURE_SUCCESS(rv,rv); michael@0: michael@0: CFURLRef pathAsCFURL; michael@0: CFStringRef pathAsCFString = ::CFStringCreateWithCharacters(NULL, michael@0: reinterpret_cast(aPlatformAppPath), michael@0: NS_strlen(aPlatformAppPath)); michael@0: if (!pathAsCFString) michael@0: return NS_ERROR_OUT_OF_MEMORY; michael@0: michael@0: if (::CFStringGetCharacterAtIndex(pathAsCFString, 0) == '/') { michael@0: // we have a Posix path michael@0: pathAsCFURL = ::CFURLCreateWithFileSystemPath(nullptr, pathAsCFString, michael@0: kCFURLPOSIXPathStyle, false); michael@0: if (!pathAsCFURL) { michael@0: ::CFRelease(pathAsCFString); michael@0: return NS_ERROR_OUT_OF_MEMORY; michael@0: } michael@0: } michael@0: else { michael@0: // if it doesn't start with a / it's not an absolute Posix path michael@0: // let's check if it's a HFS path left over from old preferences michael@0: michael@0: // If it starts with a ':' char, it's not an absolute HFS path michael@0: // so bail for that, and also if it's empty michael@0: if (::CFStringGetLength(pathAsCFString) == 0 || michael@0: ::CFStringGetCharacterAtIndex(pathAsCFString, 0) == ':') michael@0: { michael@0: ::CFRelease(pathAsCFString); michael@0: return NS_ERROR_FILE_UNRECOGNIZED_PATH; michael@0: } michael@0: michael@0: pathAsCFURL = ::CFURLCreateWithFileSystemPath(nullptr, pathAsCFString, michael@0: kCFURLHFSPathStyle, false); michael@0: if (!pathAsCFURL) { michael@0: ::CFRelease(pathAsCFString); michael@0: return NS_ERROR_OUT_OF_MEMORY; michael@0: } michael@0: } michael@0: michael@0: rv = localFile->InitWithCFURL(pathAsCFURL); michael@0: ::CFRelease(pathAsCFString); michael@0: ::CFRelease(pathAsCFURL); michael@0: if (NS_FAILED(rv)) michael@0: return rv; michael@0: *aFile = localFile; michael@0: NS_IF_ADDREF(*aFile); michael@0: michael@0: return NS_OK; michael@0: michael@0: NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT; michael@0: } michael@0: michael@0: NS_IMETHODIMP nsOSHelperAppService::GetFromTypeAndExtension(const nsACString& aType, const nsACString& aFileExt, nsIMIMEInfo ** aMIMEInfo) michael@0: { michael@0: return nsExternalHelperAppService::GetFromTypeAndExtension(aType, aFileExt, aMIMEInfo); michael@0: } michael@0: michael@0: // Returns the MIME types an application bundle explicitly claims to handle. michael@0: // Returns NULL if aAppRef doesn't explicitly claim to handle any MIME types. michael@0: // If the return value is non-NULL, the caller is responsible for freeing it. michael@0: // This isn't necessarily the same as the MIME types the application bundle michael@0: // is registered to handle in the Launch Services database. (For example michael@0: // the Preview application is normally registered to handle the application/pdf michael@0: // MIME type, even though it doesn't explicitly claim to handle *any* MIME michael@0: // types in its Info.plist. This is probably because Preview does explicitly michael@0: // claim to handle the com.adobe.pdf UTI, and Launch Services somehow michael@0: // translates this into a claim to support the application/pdf MIME type. michael@0: // Launch Services doesn't provide any APIs (documented or undocumented) to michael@0: // query which MIME types a given application is registered to handle. So any michael@0: // app that wants this information (e.g. the Default Apps pref pane) needs to michael@0: // iterate through the entire Launch Services database -- a process which can michael@0: // take several seconds.) michael@0: static CFArrayRef GetMIMETypesHandledByApp(FSRef *aAppRef) michael@0: { michael@0: CFURLRef appURL = ::CFURLCreateFromFSRef(kCFAllocatorDefault, aAppRef); michael@0: if (!appURL) { michael@0: return NULL; michael@0: } michael@0: CFDictionaryRef infoDict = ::CFBundleCopyInfoDictionaryForURL(appURL); michael@0: ::CFRelease(appURL); michael@0: if (!infoDict) { michael@0: return NULL; michael@0: } michael@0: CFTypeRef cfObject = ::CFDictionaryGetValue(infoDict, CFSTR("CFBundleDocumentTypes")); michael@0: if (!cfObject || (::CFGetTypeID(cfObject) != ::CFArrayGetTypeID())) { michael@0: ::CFRelease(infoDict); michael@0: return NULL; michael@0: } michael@0: michael@0: CFArrayRef docTypes = static_cast(cfObject); michael@0: CFIndex docTypesCount = ::CFArrayGetCount(docTypes); michael@0: if (docTypesCount == 0) { michael@0: ::CFRelease(infoDict); michael@0: return NULL; michael@0: } michael@0: michael@0: CFMutableArrayRef mimeTypes = michael@0: ::CFArrayCreateMutable(kCFAllocatorDefault, 0, &kCFTypeArrayCallBacks); michael@0: for (CFIndex i = 0; i < docTypesCount; ++i) { michael@0: cfObject = ::CFArrayGetValueAtIndex(docTypes, i); michael@0: if (!cfObject || (::CFGetTypeID(cfObject) != ::CFDictionaryGetTypeID())) { michael@0: continue; michael@0: } michael@0: CFDictionaryRef typeDict = static_cast(cfObject); michael@0: michael@0: // When this key is present (on OS X 10.5 and later), its contents michael@0: // take precedence over CFBundleTypeMIMETypes (and CFBundleTypeExtensions michael@0: // and CFBundleTypeOSTypes). michael@0: cfObject = ::CFDictionaryGetValue(typeDict, CFSTR("LSItemContentTypes")); michael@0: if (cfObject && (::CFGetTypeID(cfObject) == ::CFArrayGetTypeID())) { michael@0: continue; michael@0: } michael@0: michael@0: cfObject = ::CFDictionaryGetValue(typeDict, CFSTR("CFBundleTypeMIMETypes")); michael@0: if (!cfObject || (::CFGetTypeID(cfObject) != ::CFArrayGetTypeID())) { michael@0: continue; michael@0: } michael@0: CFArrayRef mimeTypeHolder = static_cast(cfObject); michael@0: CFArrayAppendArray(mimeTypes, mimeTypeHolder, michael@0: ::CFRangeMake(0, ::CFArrayGetCount(mimeTypeHolder))); michael@0: } michael@0: michael@0: ::CFRelease(infoDict); michael@0: if (!::CFArrayGetCount(mimeTypes)) { michael@0: ::CFRelease(mimeTypes); michael@0: mimeTypes = NULL; michael@0: } michael@0: return mimeTypes; michael@0: } michael@0: michael@0: // aMIMEType and aFileExt might not match, If they don't we set *aFound to michael@0: // false and return a minimal nsIMIMEInfo structure. michael@0: already_AddRefed michael@0: nsOSHelperAppService::GetMIMEInfoFromOS(const nsACString& aMIMEType, michael@0: const nsACString& aFileExt, michael@0: bool * aFound) michael@0: { michael@0: NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSNULL; michael@0: michael@0: *aFound = false; michael@0: michael@0: const nsCString& flatType = PromiseFlatCString(aMIMEType); michael@0: const nsCString& flatExt = PromiseFlatCString(aFileExt); michael@0: michael@0: PR_LOG(mLog, PR_LOG_DEBUG, ("Mac: HelperAppService lookup for type '%s' ext '%s'\n", michael@0: flatType.get(), flatExt.get())); michael@0: michael@0: // Create a Mac-specific MIME info so we can use Mac-specific members. michael@0: nsRefPtr mimeInfoMac = new nsMIMEInfoMac(aMIMEType); michael@0: michael@0: NSAutoreleasePool *localPool = [[NSAutoreleasePool alloc] init]; michael@0: michael@0: OSStatus err; michael@0: bool haveAppForType = false; michael@0: bool haveAppForExt = false; michael@0: bool typeAppIsDefault = false; michael@0: bool extAppIsDefault = false; michael@0: FSRef typeAppFSRef; michael@0: FSRef extAppFSRef; michael@0: michael@0: CFStringRef cfMIMEType = NULL; michael@0: michael@0: if (!aMIMEType.IsEmpty()) { michael@0: CFURLRef appURL = NULL; michael@0: // CFStringCreateWithCString() can fail even if we're not out of memory -- michael@0: // for example if the 'cStr' parameter is something very wierd (like "ÿÿ~" michael@0: // aka "\xFF\xFF~"), or possibly if it can't be interpreted as using what's michael@0: // specified in the 'encoding' parameter. See bug 548719. michael@0: cfMIMEType = ::CFStringCreateWithCString(NULL, flatType.get(), michael@0: kCFStringEncodingUTF8); michael@0: if (cfMIMEType) { michael@0: err = ::LSCopyApplicationForMIMEType(cfMIMEType, kLSRolesAll, &appURL); michael@0: if ((err == noErr) && appURL && ::CFURLGetFSRef(appURL, &typeAppFSRef)) { michael@0: haveAppForType = true; michael@0: PR_LOG(mLog, PR_LOG_DEBUG, ("LSCopyApplicationForMIMEType found a default application\n")); michael@0: } michael@0: if (appURL) { michael@0: ::CFRelease(appURL); michael@0: } michael@0: } michael@0: } michael@0: if (!aFileExt.IsEmpty()) { michael@0: // CFStringCreateWithCString() can fail even if we're not out of memory -- michael@0: // for example if the 'cStr' parameter is something very wierd (like "ÿÿ~" michael@0: // aka "\xFF\xFF~"), or possibly if it can't be interpreted as using what's michael@0: // specified in the 'encoding' parameter. See bug 548719. michael@0: CFStringRef cfExt = ::CFStringCreateWithCString(NULL, flatExt.get(), kCFStringEncodingUTF8); michael@0: if (cfExt) { michael@0: err = ::LSGetApplicationForInfo(kLSUnknownType, kLSUnknownCreator, cfExt, michael@0: kLSRolesAll, &extAppFSRef, nullptr); michael@0: if (err == noErr) { michael@0: haveAppForExt = true; michael@0: PR_LOG(mLog, PR_LOG_DEBUG, ("LSGetApplicationForInfo found a default application\n")); michael@0: } michael@0: ::CFRelease(cfExt); michael@0: } michael@0: } michael@0: michael@0: if (haveAppForType && haveAppForExt) { michael@0: // Do aMIMEType and aFileExt match? michael@0: if (::FSCompareFSRefs((const FSRef *) &typeAppFSRef, (const FSRef *) &extAppFSRef) == noErr) { michael@0: typeAppIsDefault = true; michael@0: *aFound = true; michael@0: } michael@0: } else if (haveAppForType) { michael@0: // If aFileExt isn't empty, it doesn't match aMIMEType. michael@0: if (aFileExt.IsEmpty()) { michael@0: typeAppIsDefault = true; michael@0: *aFound = true; michael@0: } michael@0: } else if (haveAppForExt) { michael@0: // If aMIMEType isn't empty, it doesn't match aFileExt, which should mean michael@0: // that we haven't found a matching app. But make an exception for an app michael@0: // that also explicitly claims to handle aMIMEType, or which doesn't claim michael@0: // to handle any MIME types. This helps work around the following Apple michael@0: // design flaw: michael@0: // michael@0: // Launch Services is somewhat unreliable about registering Apple apps to michael@0: // handle MIME types. Probably this is because Apple has officially michael@0: // deprecated support for MIME types (in favor of UTIs). As a result, michael@0: // most of Apple's own apps don't explicitly claim to handle any MIME michael@0: // types (instead they claim to handle one or more UTIs). So Launch michael@0: // Services must contain logic to translate support for a given UTI into michael@0: // support for one or more MIME types, and it doesn't always do this michael@0: // correctly. For example DiskImageMounter isn't (by default) registered michael@0: // to handle the application/x-apple-diskimage MIME type. See bug 675356. michael@0: // michael@0: // Apple has also deprecated support for file extensions, and Apple apps michael@0: // also don't register to handle them. But for some reason Launch Services michael@0: // is (apparently) better about translating support for a given UTI into michael@0: // support for one or more file extensions. It's not at all clear why. michael@0: if (aMIMEType.IsEmpty()) { michael@0: extAppIsDefault = true; michael@0: *aFound = true; michael@0: } else { michael@0: CFArrayRef extAppMIMETypes = GetMIMETypesHandledByApp(&extAppFSRef); michael@0: if (extAppMIMETypes) { michael@0: if (cfMIMEType) { michael@0: if (::CFArrayContainsValue(extAppMIMETypes, michael@0: ::CFRangeMake(0, ::CFArrayGetCount(extAppMIMETypes)), michael@0: cfMIMEType)) { michael@0: extAppIsDefault = true; michael@0: *aFound = true; michael@0: } michael@0: } michael@0: ::CFRelease(extAppMIMETypes); michael@0: } else { michael@0: extAppIsDefault = true; michael@0: *aFound = true; michael@0: } michael@0: } michael@0: } michael@0: michael@0: if (cfMIMEType) { michael@0: ::CFRelease(cfMIMEType); michael@0: } michael@0: michael@0: if (aMIMEType.IsEmpty()) { michael@0: if (haveAppForExt) { michael@0: // If aMIMEType is empty and we've found a default app for aFileExt, try michael@0: // to get the MIME type from aFileExt. (It might also be worth doing michael@0: // this when aMIMEType isn't empty but haveAppForType is false -- but michael@0: // the doc for this method says that if we have a MIME type (in michael@0: // aMIMEType), we need to give it preference.) michael@0: NSURLFileTypeMappings *map = [NSURLFileTypeMappings sharedMappings]; michael@0: NSString *extStr = [NSString stringWithCString:flatExt.get() encoding:NSASCIIStringEncoding]; michael@0: NSString *typeStr = map ? [map MIMETypeForExtension:extStr] : NULL; michael@0: if (typeStr) { michael@0: nsAutoCString mimeType; michael@0: mimeType.Assign((char *)[typeStr cStringUsingEncoding:NSASCIIStringEncoding]); michael@0: mimeInfoMac->SetMIMEType(mimeType); michael@0: haveAppForType = true; michael@0: } else { michael@0: // Sometimes the OS won't give us a MIME type for an extension that's michael@0: // registered with Launch Services and has a default app: For example michael@0: // Real Player registers itself for the "ogg" extension and for the michael@0: // audio/x-ogg and application/x-ogg MIME types, but michael@0: // MIMETypeForExtension returns nil for the "ogg" extension even on michael@0: // systems where Real Player is installed. This is probably an Apple michael@0: // bug. But bad things happen if we return an nsIMIMEInfo structure michael@0: // with an empty MIME type and set *aFound to true. So in this michael@0: // case we need to set it to false here. michael@0: haveAppForExt = false; michael@0: extAppIsDefault = false; michael@0: *aFound = false; michael@0: } michael@0: } else { michael@0: // Otherwise set the MIME type to a reasonable fallback. michael@0: mimeInfoMac->SetMIMEType(NS_LITERAL_CSTRING(APPLICATION_OCTET_STREAM)); michael@0: } michael@0: } michael@0: michael@0: if (typeAppIsDefault || extAppIsDefault) { michael@0: if (haveAppForExt) michael@0: mimeInfoMac->AppendExtension(aFileExt); michael@0: michael@0: nsCOMPtr app(do_CreateInstance(NS_LOCAL_FILE_CONTRACTID)); michael@0: if (!app) { michael@0: [localPool release]; michael@0: return nullptr; michael@0: } michael@0: michael@0: CFStringRef cfAppName = NULL; michael@0: if (typeAppIsDefault) { michael@0: app->InitWithFSRef(&typeAppFSRef); michael@0: ::LSCopyItemAttribute((const FSRef *) &typeAppFSRef, kLSRolesAll, michael@0: kLSItemDisplayName, (CFTypeRef *) &cfAppName); michael@0: } else { michael@0: app->InitWithFSRef(&extAppFSRef); michael@0: ::LSCopyItemAttribute((const FSRef *) &extAppFSRef, kLSRolesAll, michael@0: kLSItemDisplayName, (CFTypeRef *) &cfAppName); michael@0: } michael@0: if (cfAppName) { michael@0: nsAutoTArray buffer; michael@0: CFIndex appNameLength = ::CFStringGetLength(cfAppName); michael@0: buffer.SetLength(appNameLength); michael@0: ::CFStringGetCharacters(cfAppName, CFRangeMake(0, appNameLength), michael@0: buffer.Elements()); michael@0: nsAutoString appName; michael@0: appName.Assign(reinterpret_cast(buffer.Elements()), appNameLength); michael@0: mimeInfoMac->SetDefaultDescription(appName); michael@0: ::CFRelease(cfAppName); michael@0: } michael@0: michael@0: mimeInfoMac->SetDefaultApplication(app); michael@0: mimeInfoMac->SetPreferredAction(nsIMIMEInfo::useSystemDefault); michael@0: } else { michael@0: mimeInfoMac->SetPreferredAction(nsIMIMEInfo::saveToDisk); michael@0: } michael@0: michael@0: nsAutoCString mimeType; michael@0: mimeInfoMac->GetMIMEType(mimeType); michael@0: if (*aFound && !mimeType.IsEmpty()) { michael@0: // If we have a MIME type, make sure its preferred extension is included michael@0: // in our list. michael@0: NSURLFileTypeMappings *map = [NSURLFileTypeMappings sharedMappings]; michael@0: NSString *typeStr = [NSString stringWithCString:mimeType.get() encoding:NSASCIIStringEncoding]; michael@0: NSString *extStr = map ? [map preferredExtensionForMIMEType:typeStr] : NULL; michael@0: if (extStr) { michael@0: nsAutoCString preferredExt; michael@0: preferredExt.Assign((char *)[extStr cStringUsingEncoding:NSASCIIStringEncoding]); michael@0: mimeInfoMac->AppendExtension(preferredExt); michael@0: } michael@0: michael@0: CFStringRef cfType = ::CFStringCreateWithCString(NULL, mimeType.get(), kCFStringEncodingUTF8); michael@0: if (cfType) { michael@0: CFStringRef cfTypeDesc = NULL; michael@0: if (::LSCopyKindStringForMIMEType(cfType, &cfTypeDesc) == noErr) { michael@0: nsAutoTArray buffer; michael@0: CFIndex typeDescLength = ::CFStringGetLength(cfTypeDesc); michael@0: buffer.SetLength(typeDescLength); michael@0: ::CFStringGetCharacters(cfTypeDesc, CFRangeMake(0, typeDescLength), michael@0: buffer.Elements()); michael@0: nsAutoString typeDesc; michael@0: typeDesc.Assign(reinterpret_cast(buffer.Elements()), typeDescLength); michael@0: mimeInfoMac->SetDescription(typeDesc); michael@0: } michael@0: if (cfTypeDesc) { michael@0: ::CFRelease(cfTypeDesc); michael@0: } michael@0: ::CFRelease(cfType); michael@0: } michael@0: } michael@0: michael@0: PR_LOG(mLog, PR_LOG_DEBUG, ("OS gave us: type '%s' found '%i'\n", mimeType.get(), *aFound)); michael@0: michael@0: [localPool release]; michael@0: return mimeInfoMac.forget(); michael@0: michael@0: NS_OBJC_END_TRY_ABORT_BLOCK_NSNULL; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsOSHelperAppService::GetProtocolHandlerInfoFromOS(const nsACString &aScheme, michael@0: bool *found, michael@0: nsIHandlerInfo **_retval) michael@0: { michael@0: NS_ASSERTION(!aScheme.IsEmpty(), "No scheme was specified!"); michael@0: michael@0: nsresult rv = OSProtocolHandlerExists(nsPromiseFlatCString(aScheme).get(), michael@0: found); michael@0: if (NS_FAILED(rv)) michael@0: return rv; michael@0: michael@0: nsMIMEInfoMac *handlerInfo = michael@0: new nsMIMEInfoMac(aScheme, nsMIMEInfoBase::eProtocolInfo); michael@0: NS_ENSURE_TRUE(handlerInfo, NS_ERROR_OUT_OF_MEMORY); michael@0: NS_ADDREF(*_retval = handlerInfo); michael@0: michael@0: if (!*found) { michael@0: // Code that calls this requires an object regardless if the OS has michael@0: // something for us, so we return the empty object. michael@0: return NS_OK; michael@0: } michael@0: michael@0: nsAutoString desc; michael@0: GetApplicationDescription(aScheme, desc); michael@0: handlerInfo->SetDefaultDescription(desc); michael@0: michael@0: return NS_OK; michael@0: } michael@0: