1 /* -*- Mode: C++; tab-width: 3; indent-tabs-mode: nil; c-basic-offset: 2 -*-
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
9 #include "mozilla/net/NeckoCommon.h"
10 #include "nsOSHelperAppService.h"
11 #include "nsObjCExceptions.h"
12 #include "nsISupports.h"
17 #include "nsIHandlerService.h"
18 #include "nsILocalFileMac.h"
19 #include "nsMimeTypes.h"
20 #include "nsIStringBundle.h"
21 #include "nsIPromptService.h"
24 #include "nsMIMEInfoMac.h"
25 #include "nsEmbedCID.h"
27 #import <CoreFoundation/CoreFoundation.h>
28 #import <ApplicationServices/ApplicationServices.h>
31 #define HELPERAPPLAUNCHER_BUNDLE_URL "chrome://global/locale/helperAppLauncher.properties"
32 #define BRAND_BUNDLE_URL "chrome://branding/locale/brand.properties"
34 using mozilla::LogLevel;
36 /* This is an undocumented interface (in the Foundation framework) that has
37 * been stable since at least 10.2.8 and is still present on SnowLeopard.
38 * Furthermore WebKit has three public methods (in WebKitSystemInterface.h)
39 * that are thin wrappers around this interface's last three methods. So
40 * it's unlikely to change anytime soon. Now that we're no longer using
41 * Internet Config Services, this is the only way to look up a MIME type
42 * from an extension, or vice versa.
44 @class NSURLFileTypeMappingsInternal;
46 @interface NSURLFileTypeMappings : NSObject
48 NSURLFileTypeMappingsInternal *_internal;
51 + (NSURLFileTypeMappings*)sharedMappings;
52 - (NSString*)MIMETypeForExtension:(NSString*)aString;
53 - (NSString*)preferredExtensionForMIMEType:(NSString*)aString;
54 - (NSArray*)extensionsForMIMEType:(NSString*)aString;
57 nsOSHelperAppService::nsOSHelperAppService() : nsExternalHelperAppService()
59 mode_t mask = umask(0777);
61 mPermissions = 0666 & ~mask;
64 nsOSHelperAppService::~nsOSHelperAppService()
67 nsresult nsOSHelperAppService::OSProtocolHandlerExists(const char * aProtocolScheme, bool * aHandlerExists)
69 // CFStringCreateWithBytes() can fail even if we're not out of memory --
70 // for example if the 'bytes' parameter is something very wierd (like "ÿÿ~"
71 // aka "\xFF\xFF~"), or possibly if it can't be interpreted as using what's
72 // specified in the 'encoding' parameter. See bug 548719.
73 CFStringRef schemeString = ::CFStringCreateWithBytes(kCFAllocatorDefault,
74 (const UInt8*)aProtocolScheme,
75 strlen(aProtocolScheme),
76 kCFStringEncodingUTF8,
79 // LSCopyDefaultHandlerForURLScheme() can fail to find the default handler
80 // for aProtocolScheme when it's never been explicitly set (using
81 // LSSetDefaultHandlerForURLScheme()). For example, Safari is the default
82 // handler for the "http" scheme on a newly installed copy of OS X. But
83 // this (presumably) wasn't done using LSSetDefaultHandlerForURLScheme(),
84 // so LSCopyDefaultHandlerForURLScheme() will fail to find Safari. To get
85 // around this we use LSCopyAllHandlersForURLScheme() instead -- which seems
87 // http://lists.apple.com/archives/Carbon-dev/2007/May/msg00349.html
88 // http://www.realsoftware.com/listarchives/realbasic-nug/2008-02/msg00119.html
89 CFArrayRef handlerArray = ::LSCopyAllHandlersForURLScheme(schemeString);
90 *aHandlerExists = !!handlerArray;
92 ::CFRelease(handlerArray);
93 ::CFRelease(schemeString);
95 *aHandlerExists = false;
100 NS_IMETHODIMP nsOSHelperAppService::GetApplicationDescription(const nsACString& aScheme, nsAString& _retval)
102 NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
104 nsresult rv = NS_ERROR_NOT_AVAILABLE;
106 CFStringRef schemeCFString =
107 ::CFStringCreateWithBytes(kCFAllocatorDefault,
108 (const UInt8 *)PromiseFlatCString(aScheme).get(),
110 kCFStringEncodingUTF8,
113 if (schemeCFString) {
114 CFStringRef lookupCFString = ::CFStringCreateWithFormat(NULL, NULL, CFSTR("%@:"), schemeCFString);
116 if (lookupCFString) {
117 CFURLRef lookupCFURL = ::CFURLCreateWithString(NULL, lookupCFString, NULL);
120 CFURLRef appCFURL = NULL;
121 OSStatus theErr = ::LSGetApplicationForURL(lookupCFURL, kLSRolesAll, NULL, &appCFURL);
123 if (theErr == noErr) {
124 CFBundleRef handlerBundle = ::CFBundleCreate(NULL, appCFURL);
127 // Get the human-readable name of the default handler bundle
128 CFStringRef bundleName =
129 (CFStringRef)::CFBundleGetValueForInfoDictionaryKey(handlerBundle,
133 AutoTArray<UniChar, 255> buffer;
134 CFIndex bundleNameLength = ::CFStringGetLength(bundleName);
135 buffer.SetLength(bundleNameLength);
136 ::CFStringGetCharacters(bundleName, CFRangeMake(0, bundleNameLength),
138 _retval.Assign(reinterpret_cast<char16_t*>(buffer.Elements()), bundleNameLength);
142 ::CFRelease(handlerBundle);
145 ::CFRelease(appCFURL);
148 ::CFRelease(lookupCFURL);
151 ::CFRelease(lookupCFString);
154 ::CFRelease(schemeCFString);
159 NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
162 nsresult nsOSHelperAppService::GetFileTokenForPath(const char16_t * aPlatformAppPath, nsIFile ** aFile)
164 NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
167 nsCOMPtr<nsILocalFileMac> localFile (do_CreateInstance(NS_LOCAL_FILE_CONTRACTID, &rv));
168 NS_ENSURE_SUCCESS(rv,rv);
170 CFURLRef pathAsCFURL;
171 CFStringRef pathAsCFString = ::CFStringCreateWithCharacters(NULL,
172 reinterpret_cast<const UniChar*>(aPlatformAppPath),
173 NS_strlen(aPlatformAppPath));
175 return NS_ERROR_OUT_OF_MEMORY;
177 if (::CFStringGetCharacterAtIndex(pathAsCFString, 0) == '/') {
178 // we have a Posix path
179 pathAsCFURL = ::CFURLCreateWithFileSystemPath(nullptr, pathAsCFString,
180 kCFURLPOSIXPathStyle, false);
182 ::CFRelease(pathAsCFString);
183 return NS_ERROR_OUT_OF_MEMORY;
187 // if it doesn't start with a / it's not an absolute Posix path
188 // let's check if it's a HFS path left over from old preferences
190 // If it starts with a ':' char, it's not an absolute HFS path
191 // so bail for that, and also if it's empty
192 if (::CFStringGetLength(pathAsCFString) == 0 ||
193 ::CFStringGetCharacterAtIndex(pathAsCFString, 0) == ':')
195 ::CFRelease(pathAsCFString);
196 return NS_ERROR_FILE_UNRECOGNIZED_PATH;
199 pathAsCFURL = ::CFURLCreateWithFileSystemPath(nullptr, pathAsCFString,
200 kCFURLHFSPathStyle, false);
202 ::CFRelease(pathAsCFString);
203 return NS_ERROR_OUT_OF_MEMORY;
207 rv = localFile->InitWithCFURL(pathAsCFURL);
208 ::CFRelease(pathAsCFString);
209 ::CFRelease(pathAsCFURL);
213 NS_IF_ADDREF(*aFile);
217 NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
220 NS_IMETHODIMP nsOSHelperAppService::GetFromTypeAndExtension(const nsACString& aType, const nsACString& aFileExt, nsIMIMEInfo ** aMIMEInfo)
222 return nsExternalHelperAppService::GetFromTypeAndExtension(aType, aFileExt, aMIMEInfo);
225 // Returns the MIME types an application bundle explicitly claims to handle.
226 // Returns NULL if aAppRef doesn't explicitly claim to handle any MIME types.
227 // If the return value is non-NULL, the caller is responsible for freeing it.
228 // This isn't necessarily the same as the MIME types the application bundle
229 // is registered to handle in the Launch Services database. (For example
230 // the Preview application is normally registered to handle the application/pdf
231 // MIME type, even though it doesn't explicitly claim to handle *any* MIME
232 // types in its Info.plist. This is probably because Preview does explicitly
233 // claim to handle the com.adobe.pdf UTI, and Launch Services somehow
234 // translates this into a claim to support the application/pdf MIME type.
235 // Launch Services doesn't provide any APIs (documented or undocumented) to
236 // query which MIME types a given application is registered to handle. So any
237 // app that wants this information (e.g. the Default Apps pref pane) needs to
238 // iterate through the entire Launch Services database -- a process which can
239 // take several seconds.)
240 static CFArrayRef GetMIMETypesHandledByApp(FSRef *aAppRef)
242 CFURLRef appURL = ::CFURLCreateFromFSRef(kCFAllocatorDefault, aAppRef);
246 CFDictionaryRef infoDict = ::CFBundleCopyInfoDictionaryForURL(appURL);
251 CFTypeRef cfObject = ::CFDictionaryGetValue(infoDict, CFSTR("CFBundleDocumentTypes"));
252 if (!cfObject || (::CFGetTypeID(cfObject) != ::CFArrayGetTypeID())) {
253 ::CFRelease(infoDict);
257 CFArrayRef docTypes = static_cast<CFArrayRef>(cfObject);
258 CFIndex docTypesCount = ::CFArrayGetCount(docTypes);
259 if (docTypesCount == 0) {
260 ::CFRelease(infoDict);
264 CFMutableArrayRef mimeTypes =
265 ::CFArrayCreateMutable(kCFAllocatorDefault, 0, &kCFTypeArrayCallBacks);
266 for (CFIndex i = 0; i < docTypesCount; ++i) {
267 cfObject = ::CFArrayGetValueAtIndex(docTypes, i);
268 if (!cfObject || (::CFGetTypeID(cfObject) != ::CFDictionaryGetTypeID())) {
271 CFDictionaryRef typeDict = static_cast<CFDictionaryRef>(cfObject);
273 // When this key is present (on OS X 10.5 and later), its contents
274 // take precedence over CFBundleTypeMIMETypes (and CFBundleTypeExtensions
275 // and CFBundleTypeOSTypes).
276 cfObject = ::CFDictionaryGetValue(typeDict, CFSTR("LSItemContentTypes"));
277 if (cfObject && (::CFGetTypeID(cfObject) == ::CFArrayGetTypeID())) {
281 cfObject = ::CFDictionaryGetValue(typeDict, CFSTR("CFBundleTypeMIMETypes"));
282 if (!cfObject || (::CFGetTypeID(cfObject) != ::CFArrayGetTypeID())) {
285 CFArrayRef mimeTypeHolder = static_cast<CFArrayRef>(cfObject);
286 CFArrayAppendArray(mimeTypes, mimeTypeHolder,
287 ::CFRangeMake(0, ::CFArrayGetCount(mimeTypeHolder)));
290 ::CFRelease(infoDict);
291 if (!::CFArrayGetCount(mimeTypes)) {
292 ::CFRelease(mimeTypes);
298 // aMIMEType and aFileExt might not match, If they don't we set *aFound to
299 // false and return a minimal nsIMIMEInfo structure.
300 already_AddRefed<nsIMIMEInfo>
301 nsOSHelperAppService::GetMIMEInfoFromOS(const nsACString& aMIMEType,
302 const nsACString& aFileExt,
305 NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSNULL;
309 const nsCString& flatType = PromiseFlatCString(aMIMEType);
310 const nsCString& flatExt = PromiseFlatCString(aFileExt);
312 MOZ_LOG(mLog, LogLevel::Debug, ("Mac: HelperAppService lookup for type '%s' ext '%s'\n",
313 flatType.get(), flatExt.get()));
315 // Create a Mac-specific MIME info so we can use Mac-specific members.
316 RefPtr<nsMIMEInfoMac> mimeInfoMac = new nsMIMEInfoMac(aMIMEType);
318 NSAutoreleasePool *localPool = [[NSAutoreleasePool alloc] init];
321 bool haveAppForType = false;
322 bool haveAppForExt = false;
323 bool typeAppIsDefault = false;
324 bool extAppIsDefault = false;
328 CFStringRef cfMIMEType = NULL;
330 if (!aMIMEType.IsEmpty()) {
331 CFURLRef appURL = NULL;
332 // CFStringCreateWithCString() can fail even if we're not out of memory --
333 // for example if the 'cStr' parameter is something very weird (like "ÿÿ~"
334 // aka "\xFF\xFF~"), or possibly if it can't be interpreted as using what's
335 // specified in the 'encoding' parameter. See bug 548719.
336 cfMIMEType = ::CFStringCreateWithCString(NULL, flatType.get(),
337 kCFStringEncodingUTF8);
339 err = ::LSCopyApplicationForMIMEType(cfMIMEType, kLSRolesAll, &appURL);
340 if ((err == noErr) && appURL && ::CFURLGetFSRef(appURL, &typeAppFSRef)) {
341 haveAppForType = true;
342 MOZ_LOG(mLog, LogLevel::Debug, ("LSCopyApplicationForMIMEType found a default application\n"));
349 if (!aFileExt.IsEmpty()) {
350 // CFStringCreateWithCString() can fail even if we're not out of memory --
351 // for example if the 'cStr' parameter is something very wierd (like "ÿÿ~"
352 // aka "\xFF\xFF~"), or possibly if it can't be interpreted as using what's
353 // specified in the 'encoding' parameter. See bug 548719.
354 CFStringRef cfExt = ::CFStringCreateWithCString(NULL, flatExt.get(), kCFStringEncodingUTF8);
356 err = ::LSGetApplicationForInfo(kLSUnknownType, kLSUnknownCreator, cfExt,
357 kLSRolesAll, &extAppFSRef, nullptr);
359 haveAppForExt = true;
360 MOZ_LOG(mLog, LogLevel::Debug, ("LSGetApplicationForInfo found a default application\n"));
366 if (haveAppForType && haveAppForExt) {
367 // Do aMIMEType and aFileExt match?
368 if (::FSCompareFSRefs((const FSRef *) &typeAppFSRef, (const FSRef *) &extAppFSRef) == noErr) {
369 typeAppIsDefault = true;
372 } else if (haveAppForType) {
373 // If aFileExt isn't empty, it doesn't match aMIMEType.
374 if (aFileExt.IsEmpty()) {
375 typeAppIsDefault = true;
378 } else if (haveAppForExt) {
379 // If aMIMEType isn't empty, it doesn't match aFileExt, which should mean
380 // that we haven't found a matching app. But make an exception for an app
381 // that also explicitly claims to handle aMIMEType, or which doesn't claim
382 // to handle any MIME types. This helps work around the following Apple
385 // Launch Services is somewhat unreliable about registering Apple apps to
386 // handle MIME types. Probably this is because Apple has officially
387 // deprecated support for MIME types (in favor of UTIs). As a result,
388 // most of Apple's own apps don't explicitly claim to handle any MIME
389 // types (instead they claim to handle one or more UTIs). So Launch
390 // Services must contain logic to translate support for a given UTI into
391 // support for one or more MIME types, and it doesn't always do this
392 // correctly. For example DiskImageMounter isn't (by default) registered
393 // to handle the application/x-apple-diskimage MIME type. See bug 675356.
395 // Apple has also deprecated support for file extensions, and Apple apps
396 // also don't register to handle them. But for some reason Launch Services
397 // is (apparently) better about translating support for a given UTI into
398 // support for one or more file extensions. It's not at all clear why.
399 if (aMIMEType.IsEmpty()) {
400 extAppIsDefault = true;
403 CFArrayRef extAppMIMETypes = GetMIMETypesHandledByApp(&extAppFSRef);
404 if (extAppMIMETypes) {
406 if (::CFArrayContainsValue(extAppMIMETypes,
407 ::CFRangeMake(0, ::CFArrayGetCount(extAppMIMETypes)),
409 extAppIsDefault = true;
413 ::CFRelease(extAppMIMETypes);
415 extAppIsDefault = true;
422 ::CFRelease(cfMIMEType);
425 if (aMIMEType.IsEmpty()) {
427 // If aMIMEType is empty and we've found a default app for aFileExt, try
428 // to get the MIME type from aFileExt. (It might also be worth doing
429 // this when aMIMEType isn't empty but haveAppForType is false -- but
430 // the doc for this method says that if we have a MIME type (in
431 // aMIMEType), we need to give it preference.)
432 NSURLFileTypeMappings *map = [NSURLFileTypeMappings sharedMappings];
433 NSString *extStr = [NSString stringWithCString:flatExt.get() encoding:NSASCIIStringEncoding];
434 NSString *typeStr = map ? [map MIMETypeForExtension:extStr] : NULL;
436 nsAutoCString mimeType;
437 mimeType.Assign((char *)[typeStr cStringUsingEncoding:NSASCIIStringEncoding]);
438 mimeInfoMac->SetMIMEType(mimeType);
439 haveAppForType = true;
441 // Sometimes the OS won't give us a MIME type for an extension that's
442 // registered with Launch Services and has a default app: For example
443 // Real Player registers itself for the "ogg" extension and for the
444 // audio/x-ogg and application/x-ogg MIME types, but
445 // MIMETypeForExtension returns nil for the "ogg" extension even on
446 // systems where Real Player is installed. This is probably an Apple
447 // bug. But bad things happen if we return an nsIMIMEInfo structure
448 // with an empty MIME type and set *aFound to true. So in this
449 // case we need to set it to false here.
450 haveAppForExt = false;
451 extAppIsDefault = false;
455 // Otherwise set the MIME type to a reasonable fallback.
456 mimeInfoMac->SetMIMEType(NS_LITERAL_CSTRING(APPLICATION_OCTET_STREAM));
460 if (typeAppIsDefault || extAppIsDefault) {
462 mimeInfoMac->AppendExtension(aFileExt);
464 nsCOMPtr<nsILocalFileMac> app(do_CreateInstance(NS_LOCAL_FILE_CONTRACTID));
470 CFStringRef cfAppName = NULL;
471 if (typeAppIsDefault) {
472 app->InitWithFSRef(&typeAppFSRef);
473 ::LSCopyItemAttribute((const FSRef *) &typeAppFSRef, kLSRolesAll,
474 kLSItemDisplayName, (CFTypeRef *) &cfAppName);
476 app->InitWithFSRef(&extAppFSRef);
477 ::LSCopyItemAttribute((const FSRef *) &extAppFSRef, kLSRolesAll,
478 kLSItemDisplayName, (CFTypeRef *) &cfAppName);
481 AutoTArray<UniChar, 255> buffer;
482 CFIndex appNameLength = ::CFStringGetLength(cfAppName);
483 buffer.SetLength(appNameLength);
484 ::CFStringGetCharacters(cfAppName, CFRangeMake(0, appNameLength),
486 nsAutoString appName;
487 appName.Assign(reinterpret_cast<char16_t*>(buffer.Elements()), appNameLength);
488 mimeInfoMac->SetDefaultDescription(appName);
489 ::CFRelease(cfAppName);
492 mimeInfoMac->SetDefaultApplication(app);
493 mimeInfoMac->SetPreferredAction(nsIMIMEInfo::useSystemDefault);
495 mimeInfoMac->SetPreferredAction(nsIMIMEInfo::saveToDisk);
498 nsAutoCString mimeType;
499 mimeInfoMac->GetMIMEType(mimeType);
500 if (*aFound && !mimeType.IsEmpty()) {
501 // If we have a MIME type, make sure its extension list is included in our
503 NSURLFileTypeMappings *map = [NSURLFileTypeMappings sharedMappings];
504 NSString *typeStr = [NSString stringWithCString:mimeType.get() encoding:NSASCIIStringEncoding];
505 NSArray *extensionsList = map ? [map extensionsForMIMEType:typeStr] : NULL;
506 if (extensionsList) {
507 for (NSString* extension in extensionsList) {
509 ext.Assign((char *)[extension cStringUsingEncoding:NSASCIIStringEncoding]);
510 mimeInfoMac->AppendExtension(ext);
514 CFStringRef cfType = ::CFStringCreateWithCString(NULL, mimeType.get(), kCFStringEncodingUTF8);
516 CFStringRef cfTypeDesc = NULL;
517 if (::LSCopyKindStringForMIMEType(cfType, &cfTypeDesc) == noErr) {
518 AutoTArray<UniChar, 255> buffer;
519 CFIndex typeDescLength = ::CFStringGetLength(cfTypeDesc);
520 buffer.SetLength(typeDescLength);
521 ::CFStringGetCharacters(cfTypeDesc, CFRangeMake(0, typeDescLength),
523 nsAutoString typeDesc;
524 typeDesc.Assign(reinterpret_cast<char16_t*>(buffer.Elements()), typeDescLength);
525 mimeInfoMac->SetDescription(typeDesc);
528 ::CFRelease(cfTypeDesc);
534 MOZ_LOG(mLog, LogLevel::Debug, ("OS gave us: type '%s' found '%i'\n", mimeType.get(), *aFound));
537 return mimeInfoMac.forget();
539 NS_OBJC_END_TRY_ABORT_BLOCK_NSNULL;
543 nsOSHelperAppService::GetProtocolHandlerInfoFromOS(const nsACString &aScheme,
545 nsIHandlerInfo **_retval)
547 NS_ASSERTION(!aScheme.IsEmpty(), "No scheme was specified!");
549 nsresult rv = OSProtocolHandlerExists(nsPromiseFlatCString(aScheme).get(),
554 nsMIMEInfoMac *handlerInfo =
555 new nsMIMEInfoMac(aScheme, nsMIMEInfoBase::eProtocolInfo);
556 NS_ENSURE_TRUE(handlerInfo, NS_ERROR_OUT_OF_MEMORY);
557 NS_ADDREF(*_retval = handlerInfo);
560 // Code that calls this requires an object regardless if the OS has
561 // something for us, so we return the empty object.
565 // As a workaround for the OS X problem described in bug 1391186, don't
566 // attempt to get/set the application description from the child process.
567 if (!mozilla::net::IsNeckoChild()) {
569 rv = GetApplicationDescription(aScheme, desc);
570 NS_WARNING_ASSERTION(NS_SUCCEEDED(rv), "GetApplicationDescription failed");
571 handlerInfo->SetDefaultDescription(desc);
578 * Override GetMIMETypeFromOSForExtension() so that we can proxy requests for
579 * the MIME type to the parent when we're executing in the child process. If
580 * we're in the parent process, query the OS directly.
583 nsOSHelperAppService::GetMIMETypeFromOSForExtension(const nsACString& aExtension,
584 nsACString& aMIMEType)
586 if (XRE_IsParentProcess()) {
587 return nsExternalHelperAppService::GetMIMETypeFromOSForExtension(aExtension,
591 nsCOMPtr<nsIHandlerService> handlerSvc =
592 do_GetService(NS_HANDLERSERVICE_CONTRACTID);
593 if (NS_WARN_IF(!handlerSvc)) {
597 nsresult rv = handlerSvc->GetTypeFromExtension(aExtension, aMIMEType);
598 if (NS_WARN_IF(NS_FAILED(rv))) {
602 if (aMIMEType.IsEmpty()) {