|
1 /* -*- Mode: C++; tab-width: 3; indent-tabs-mode: nil; c-basic-offset: 2 -*- |
|
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/. */ |
|
6 |
|
7 #include <sys/types.h> |
|
8 #include <sys/stat.h> |
|
9 #include "nsOSHelperAppService.h" |
|
10 #include "nsObjCExceptions.h" |
|
11 #include "nsISupports.h" |
|
12 #include "nsString.h" |
|
13 #include "nsTArray.h" |
|
14 #include "nsXPIDLString.h" |
|
15 #include "nsIURL.h" |
|
16 #include "nsIFile.h" |
|
17 #include "nsILocalFileMac.h" |
|
18 #include "nsMimeTypes.h" |
|
19 #include "nsIStringBundle.h" |
|
20 #include "nsIPromptService.h" |
|
21 #include "nsMemory.h" |
|
22 #include "nsCRT.h" |
|
23 #include "nsMIMEInfoMac.h" |
|
24 #include "nsEmbedCID.h" |
|
25 |
|
26 #import <CoreFoundation/CoreFoundation.h> |
|
27 #import <ApplicationServices/ApplicationServices.h> |
|
28 |
|
29 // chrome URL's |
|
30 #define HELPERAPPLAUNCHER_BUNDLE_URL "chrome://global/locale/helperAppLauncher.properties" |
|
31 #define BRAND_BUNDLE_URL "chrome://branding/locale/brand.properties" |
|
32 |
|
33 /* This is an undocumented interface (in the Foundation framework) that has |
|
34 * been stable since at least 10.2.8 and is still present on SnowLeopard. |
|
35 * Furthermore WebKit has three public methods (in WebKitSystemInterface.h) |
|
36 * that are thin wrappers around this interface's last three methods. So |
|
37 * it's unlikely to change anytime soon. Now that we're no longer using |
|
38 * Internet Config Services, this is the only way to look up a MIME type |
|
39 * from an extension, or vice versa. |
|
40 */ |
|
41 @class NSURLFileTypeMappingsInternal; |
|
42 |
|
43 @interface NSURLFileTypeMappings : NSObject |
|
44 { |
|
45 NSURLFileTypeMappingsInternal *_internal; |
|
46 } |
|
47 |
|
48 + (NSURLFileTypeMappings*)sharedMappings; |
|
49 - (NSString*)MIMETypeForExtension:(NSString*)aString; |
|
50 - (NSString*)preferredExtensionForMIMEType:(NSString*)aString; |
|
51 - (NSArray*)extensionsForMIMEType:(NSString*)aString; |
|
52 @end |
|
53 |
|
54 nsOSHelperAppService::nsOSHelperAppService() : nsExternalHelperAppService() |
|
55 { |
|
56 mode_t mask = umask(0777); |
|
57 umask(mask); |
|
58 mPermissions = 0666 & ~mask; |
|
59 } |
|
60 |
|
61 nsOSHelperAppService::~nsOSHelperAppService() |
|
62 {} |
|
63 |
|
64 nsresult nsOSHelperAppService::OSProtocolHandlerExists(const char * aProtocolScheme, bool * aHandlerExists) |
|
65 { |
|
66 // CFStringCreateWithBytes() can fail even if we're not out of memory -- |
|
67 // for example if the 'bytes' parameter is something very wierd (like "ÿÿ~" |
|
68 // aka "\xFF\xFF~"), or possibly if it can't be interpreted as using what's |
|
69 // specified in the 'encoding' parameter. See bug 548719. |
|
70 CFStringRef schemeString = ::CFStringCreateWithBytes(kCFAllocatorDefault, |
|
71 (const UInt8*)aProtocolScheme, |
|
72 strlen(aProtocolScheme), |
|
73 kCFStringEncodingUTF8, |
|
74 false); |
|
75 if (schemeString) { |
|
76 // LSCopyDefaultHandlerForURLScheme() can fail to find the default handler |
|
77 // for aProtocolScheme when it's never been explicitly set (using |
|
78 // LSSetDefaultHandlerForURLScheme()). For example, Safari is the default |
|
79 // handler for the "http" scheme on a newly installed copy of OS X. But |
|
80 // this (presumably) wasn't done using LSSetDefaultHandlerForURLScheme(), |
|
81 // so LSCopyDefaultHandlerForURLScheme() will fail to find Safari. To get |
|
82 // around this we use LSCopyAllHandlersForURLScheme() instead -- which seems |
|
83 // never to fail. |
|
84 // http://lists.apple.com/archives/Carbon-dev/2007/May/msg00349.html |
|
85 // http://www.realsoftware.com/listarchives/realbasic-nug/2008-02/msg00119.html |
|
86 CFArrayRef handlerArray = ::LSCopyAllHandlersForURLScheme(schemeString); |
|
87 *aHandlerExists = !!handlerArray; |
|
88 if (handlerArray) |
|
89 ::CFRelease(handlerArray); |
|
90 ::CFRelease(schemeString); |
|
91 } else { |
|
92 *aHandlerExists = false; |
|
93 } |
|
94 return NS_OK; |
|
95 } |
|
96 |
|
97 NS_IMETHODIMP nsOSHelperAppService::GetApplicationDescription(const nsACString& aScheme, nsAString& _retval) |
|
98 { |
|
99 NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT; |
|
100 |
|
101 nsresult rv = NS_ERROR_NOT_AVAILABLE; |
|
102 |
|
103 CFStringRef schemeCFString = |
|
104 ::CFStringCreateWithBytes(kCFAllocatorDefault, |
|
105 (const UInt8 *)PromiseFlatCString(aScheme).get(), |
|
106 aScheme.Length(), |
|
107 kCFStringEncodingUTF8, |
|
108 false); |
|
109 |
|
110 if (schemeCFString) { |
|
111 CFStringRef lookupCFString = ::CFStringCreateWithFormat(NULL, NULL, CFSTR("%@:"), schemeCFString); |
|
112 |
|
113 if (lookupCFString) { |
|
114 CFURLRef lookupCFURL = ::CFURLCreateWithString(NULL, lookupCFString, NULL); |
|
115 |
|
116 if (lookupCFURL) { |
|
117 CFURLRef appCFURL = NULL; |
|
118 OSStatus theErr = ::LSGetApplicationForURL(lookupCFURL, kLSRolesAll, NULL, &appCFURL); |
|
119 |
|
120 if (theErr == noErr) { |
|
121 CFBundleRef handlerBundle = ::CFBundleCreate(NULL, appCFURL); |
|
122 |
|
123 if (handlerBundle) { |
|
124 // Get the human-readable name of the default handler bundle |
|
125 CFStringRef bundleName = |
|
126 (CFStringRef)::CFBundleGetValueForInfoDictionaryKey(handlerBundle, |
|
127 kCFBundleNameKey); |
|
128 |
|
129 if (bundleName) { |
|
130 nsAutoTArray<UniChar, 255> buffer; |
|
131 CFIndex bundleNameLength = ::CFStringGetLength(bundleName); |
|
132 buffer.SetLength(bundleNameLength); |
|
133 ::CFStringGetCharacters(bundleName, CFRangeMake(0, bundleNameLength), |
|
134 buffer.Elements()); |
|
135 _retval.Assign(reinterpret_cast<char16_t*>(buffer.Elements()), bundleNameLength); |
|
136 rv = NS_OK; |
|
137 } |
|
138 |
|
139 ::CFRelease(handlerBundle); |
|
140 } |
|
141 |
|
142 ::CFRelease(appCFURL); |
|
143 } |
|
144 |
|
145 ::CFRelease(lookupCFURL); |
|
146 } |
|
147 |
|
148 ::CFRelease(lookupCFString); |
|
149 } |
|
150 |
|
151 ::CFRelease(schemeCFString); |
|
152 } |
|
153 |
|
154 return rv; |
|
155 |
|
156 NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT; |
|
157 } |
|
158 |
|
159 nsresult nsOSHelperAppService::GetFileTokenForPath(const char16_t * aPlatformAppPath, nsIFile ** aFile) |
|
160 { |
|
161 NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT; |
|
162 |
|
163 nsresult rv; |
|
164 nsCOMPtr<nsILocalFileMac> localFile (do_CreateInstance(NS_LOCAL_FILE_CONTRACTID, &rv)); |
|
165 NS_ENSURE_SUCCESS(rv,rv); |
|
166 |
|
167 CFURLRef pathAsCFURL; |
|
168 CFStringRef pathAsCFString = ::CFStringCreateWithCharacters(NULL, |
|
169 reinterpret_cast<const UniChar*>(aPlatformAppPath), |
|
170 NS_strlen(aPlatformAppPath)); |
|
171 if (!pathAsCFString) |
|
172 return NS_ERROR_OUT_OF_MEMORY; |
|
173 |
|
174 if (::CFStringGetCharacterAtIndex(pathAsCFString, 0) == '/') { |
|
175 // we have a Posix path |
|
176 pathAsCFURL = ::CFURLCreateWithFileSystemPath(nullptr, pathAsCFString, |
|
177 kCFURLPOSIXPathStyle, false); |
|
178 if (!pathAsCFURL) { |
|
179 ::CFRelease(pathAsCFString); |
|
180 return NS_ERROR_OUT_OF_MEMORY; |
|
181 } |
|
182 } |
|
183 else { |
|
184 // if it doesn't start with a / it's not an absolute Posix path |
|
185 // let's check if it's a HFS path left over from old preferences |
|
186 |
|
187 // If it starts with a ':' char, it's not an absolute HFS path |
|
188 // so bail for that, and also if it's empty |
|
189 if (::CFStringGetLength(pathAsCFString) == 0 || |
|
190 ::CFStringGetCharacterAtIndex(pathAsCFString, 0) == ':') |
|
191 { |
|
192 ::CFRelease(pathAsCFString); |
|
193 return NS_ERROR_FILE_UNRECOGNIZED_PATH; |
|
194 } |
|
195 |
|
196 pathAsCFURL = ::CFURLCreateWithFileSystemPath(nullptr, pathAsCFString, |
|
197 kCFURLHFSPathStyle, false); |
|
198 if (!pathAsCFURL) { |
|
199 ::CFRelease(pathAsCFString); |
|
200 return NS_ERROR_OUT_OF_MEMORY; |
|
201 } |
|
202 } |
|
203 |
|
204 rv = localFile->InitWithCFURL(pathAsCFURL); |
|
205 ::CFRelease(pathAsCFString); |
|
206 ::CFRelease(pathAsCFURL); |
|
207 if (NS_FAILED(rv)) |
|
208 return rv; |
|
209 *aFile = localFile; |
|
210 NS_IF_ADDREF(*aFile); |
|
211 |
|
212 return NS_OK; |
|
213 |
|
214 NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT; |
|
215 } |
|
216 |
|
217 NS_IMETHODIMP nsOSHelperAppService::GetFromTypeAndExtension(const nsACString& aType, const nsACString& aFileExt, nsIMIMEInfo ** aMIMEInfo) |
|
218 { |
|
219 return nsExternalHelperAppService::GetFromTypeAndExtension(aType, aFileExt, aMIMEInfo); |
|
220 } |
|
221 |
|
222 // Returns the MIME types an application bundle explicitly claims to handle. |
|
223 // Returns NULL if aAppRef doesn't explicitly claim to handle any MIME types. |
|
224 // If the return value is non-NULL, the caller is responsible for freeing it. |
|
225 // This isn't necessarily the same as the MIME types the application bundle |
|
226 // is registered to handle in the Launch Services database. (For example |
|
227 // the Preview application is normally registered to handle the application/pdf |
|
228 // MIME type, even though it doesn't explicitly claim to handle *any* MIME |
|
229 // types in its Info.plist. This is probably because Preview does explicitly |
|
230 // claim to handle the com.adobe.pdf UTI, and Launch Services somehow |
|
231 // translates this into a claim to support the application/pdf MIME type. |
|
232 // Launch Services doesn't provide any APIs (documented or undocumented) to |
|
233 // query which MIME types a given application is registered to handle. So any |
|
234 // app that wants this information (e.g. the Default Apps pref pane) needs to |
|
235 // iterate through the entire Launch Services database -- a process which can |
|
236 // take several seconds.) |
|
237 static CFArrayRef GetMIMETypesHandledByApp(FSRef *aAppRef) |
|
238 { |
|
239 CFURLRef appURL = ::CFURLCreateFromFSRef(kCFAllocatorDefault, aAppRef); |
|
240 if (!appURL) { |
|
241 return NULL; |
|
242 } |
|
243 CFDictionaryRef infoDict = ::CFBundleCopyInfoDictionaryForURL(appURL); |
|
244 ::CFRelease(appURL); |
|
245 if (!infoDict) { |
|
246 return NULL; |
|
247 } |
|
248 CFTypeRef cfObject = ::CFDictionaryGetValue(infoDict, CFSTR("CFBundleDocumentTypes")); |
|
249 if (!cfObject || (::CFGetTypeID(cfObject) != ::CFArrayGetTypeID())) { |
|
250 ::CFRelease(infoDict); |
|
251 return NULL; |
|
252 } |
|
253 |
|
254 CFArrayRef docTypes = static_cast<CFArrayRef>(cfObject); |
|
255 CFIndex docTypesCount = ::CFArrayGetCount(docTypes); |
|
256 if (docTypesCount == 0) { |
|
257 ::CFRelease(infoDict); |
|
258 return NULL; |
|
259 } |
|
260 |
|
261 CFMutableArrayRef mimeTypes = |
|
262 ::CFArrayCreateMutable(kCFAllocatorDefault, 0, &kCFTypeArrayCallBacks); |
|
263 for (CFIndex i = 0; i < docTypesCount; ++i) { |
|
264 cfObject = ::CFArrayGetValueAtIndex(docTypes, i); |
|
265 if (!cfObject || (::CFGetTypeID(cfObject) != ::CFDictionaryGetTypeID())) { |
|
266 continue; |
|
267 } |
|
268 CFDictionaryRef typeDict = static_cast<CFDictionaryRef>(cfObject); |
|
269 |
|
270 // When this key is present (on OS X 10.5 and later), its contents |
|
271 // take precedence over CFBundleTypeMIMETypes (and CFBundleTypeExtensions |
|
272 // and CFBundleTypeOSTypes). |
|
273 cfObject = ::CFDictionaryGetValue(typeDict, CFSTR("LSItemContentTypes")); |
|
274 if (cfObject && (::CFGetTypeID(cfObject) == ::CFArrayGetTypeID())) { |
|
275 continue; |
|
276 } |
|
277 |
|
278 cfObject = ::CFDictionaryGetValue(typeDict, CFSTR("CFBundleTypeMIMETypes")); |
|
279 if (!cfObject || (::CFGetTypeID(cfObject) != ::CFArrayGetTypeID())) { |
|
280 continue; |
|
281 } |
|
282 CFArrayRef mimeTypeHolder = static_cast<CFArrayRef>(cfObject); |
|
283 CFArrayAppendArray(mimeTypes, mimeTypeHolder, |
|
284 ::CFRangeMake(0, ::CFArrayGetCount(mimeTypeHolder))); |
|
285 } |
|
286 |
|
287 ::CFRelease(infoDict); |
|
288 if (!::CFArrayGetCount(mimeTypes)) { |
|
289 ::CFRelease(mimeTypes); |
|
290 mimeTypes = NULL; |
|
291 } |
|
292 return mimeTypes; |
|
293 } |
|
294 |
|
295 // aMIMEType and aFileExt might not match, If they don't we set *aFound to |
|
296 // false and return a minimal nsIMIMEInfo structure. |
|
297 already_AddRefed<nsIMIMEInfo> |
|
298 nsOSHelperAppService::GetMIMEInfoFromOS(const nsACString& aMIMEType, |
|
299 const nsACString& aFileExt, |
|
300 bool * aFound) |
|
301 { |
|
302 NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSNULL; |
|
303 |
|
304 *aFound = false; |
|
305 |
|
306 const nsCString& flatType = PromiseFlatCString(aMIMEType); |
|
307 const nsCString& flatExt = PromiseFlatCString(aFileExt); |
|
308 |
|
309 PR_LOG(mLog, PR_LOG_DEBUG, ("Mac: HelperAppService lookup for type '%s' ext '%s'\n", |
|
310 flatType.get(), flatExt.get())); |
|
311 |
|
312 // Create a Mac-specific MIME info so we can use Mac-specific members. |
|
313 nsRefPtr<nsMIMEInfoMac> mimeInfoMac = new nsMIMEInfoMac(aMIMEType); |
|
314 |
|
315 NSAutoreleasePool *localPool = [[NSAutoreleasePool alloc] init]; |
|
316 |
|
317 OSStatus err; |
|
318 bool haveAppForType = false; |
|
319 bool haveAppForExt = false; |
|
320 bool typeAppIsDefault = false; |
|
321 bool extAppIsDefault = false; |
|
322 FSRef typeAppFSRef; |
|
323 FSRef extAppFSRef; |
|
324 |
|
325 CFStringRef cfMIMEType = NULL; |
|
326 |
|
327 if (!aMIMEType.IsEmpty()) { |
|
328 CFURLRef appURL = NULL; |
|
329 // CFStringCreateWithCString() can fail even if we're not out of memory -- |
|
330 // for example if the 'cStr' parameter is something very wierd (like "ÿÿ~" |
|
331 // aka "\xFF\xFF~"), or possibly if it can't be interpreted as using what's |
|
332 // specified in the 'encoding' parameter. See bug 548719. |
|
333 cfMIMEType = ::CFStringCreateWithCString(NULL, flatType.get(), |
|
334 kCFStringEncodingUTF8); |
|
335 if (cfMIMEType) { |
|
336 err = ::LSCopyApplicationForMIMEType(cfMIMEType, kLSRolesAll, &appURL); |
|
337 if ((err == noErr) && appURL && ::CFURLGetFSRef(appURL, &typeAppFSRef)) { |
|
338 haveAppForType = true; |
|
339 PR_LOG(mLog, PR_LOG_DEBUG, ("LSCopyApplicationForMIMEType found a default application\n")); |
|
340 } |
|
341 if (appURL) { |
|
342 ::CFRelease(appURL); |
|
343 } |
|
344 } |
|
345 } |
|
346 if (!aFileExt.IsEmpty()) { |
|
347 // CFStringCreateWithCString() can fail even if we're not out of memory -- |
|
348 // for example if the 'cStr' parameter is something very wierd (like "ÿÿ~" |
|
349 // aka "\xFF\xFF~"), or possibly if it can't be interpreted as using what's |
|
350 // specified in the 'encoding' parameter. See bug 548719. |
|
351 CFStringRef cfExt = ::CFStringCreateWithCString(NULL, flatExt.get(), kCFStringEncodingUTF8); |
|
352 if (cfExt) { |
|
353 err = ::LSGetApplicationForInfo(kLSUnknownType, kLSUnknownCreator, cfExt, |
|
354 kLSRolesAll, &extAppFSRef, nullptr); |
|
355 if (err == noErr) { |
|
356 haveAppForExt = true; |
|
357 PR_LOG(mLog, PR_LOG_DEBUG, ("LSGetApplicationForInfo found a default application\n")); |
|
358 } |
|
359 ::CFRelease(cfExt); |
|
360 } |
|
361 } |
|
362 |
|
363 if (haveAppForType && haveAppForExt) { |
|
364 // Do aMIMEType and aFileExt match? |
|
365 if (::FSCompareFSRefs((const FSRef *) &typeAppFSRef, (const FSRef *) &extAppFSRef) == noErr) { |
|
366 typeAppIsDefault = true; |
|
367 *aFound = true; |
|
368 } |
|
369 } else if (haveAppForType) { |
|
370 // If aFileExt isn't empty, it doesn't match aMIMEType. |
|
371 if (aFileExt.IsEmpty()) { |
|
372 typeAppIsDefault = true; |
|
373 *aFound = true; |
|
374 } |
|
375 } else if (haveAppForExt) { |
|
376 // If aMIMEType isn't empty, it doesn't match aFileExt, which should mean |
|
377 // that we haven't found a matching app. But make an exception for an app |
|
378 // that also explicitly claims to handle aMIMEType, or which doesn't claim |
|
379 // to handle any MIME types. This helps work around the following Apple |
|
380 // design flaw: |
|
381 // |
|
382 // Launch Services is somewhat unreliable about registering Apple apps to |
|
383 // handle MIME types. Probably this is because Apple has officially |
|
384 // deprecated support for MIME types (in favor of UTIs). As a result, |
|
385 // most of Apple's own apps don't explicitly claim to handle any MIME |
|
386 // types (instead they claim to handle one or more UTIs). So Launch |
|
387 // Services must contain logic to translate support for a given UTI into |
|
388 // support for one or more MIME types, and it doesn't always do this |
|
389 // correctly. For example DiskImageMounter isn't (by default) registered |
|
390 // to handle the application/x-apple-diskimage MIME type. See bug 675356. |
|
391 // |
|
392 // Apple has also deprecated support for file extensions, and Apple apps |
|
393 // also don't register to handle them. But for some reason Launch Services |
|
394 // is (apparently) better about translating support for a given UTI into |
|
395 // support for one or more file extensions. It's not at all clear why. |
|
396 if (aMIMEType.IsEmpty()) { |
|
397 extAppIsDefault = true; |
|
398 *aFound = true; |
|
399 } else { |
|
400 CFArrayRef extAppMIMETypes = GetMIMETypesHandledByApp(&extAppFSRef); |
|
401 if (extAppMIMETypes) { |
|
402 if (cfMIMEType) { |
|
403 if (::CFArrayContainsValue(extAppMIMETypes, |
|
404 ::CFRangeMake(0, ::CFArrayGetCount(extAppMIMETypes)), |
|
405 cfMIMEType)) { |
|
406 extAppIsDefault = true; |
|
407 *aFound = true; |
|
408 } |
|
409 } |
|
410 ::CFRelease(extAppMIMETypes); |
|
411 } else { |
|
412 extAppIsDefault = true; |
|
413 *aFound = true; |
|
414 } |
|
415 } |
|
416 } |
|
417 |
|
418 if (cfMIMEType) { |
|
419 ::CFRelease(cfMIMEType); |
|
420 } |
|
421 |
|
422 if (aMIMEType.IsEmpty()) { |
|
423 if (haveAppForExt) { |
|
424 // If aMIMEType is empty and we've found a default app for aFileExt, try |
|
425 // to get the MIME type from aFileExt. (It might also be worth doing |
|
426 // this when aMIMEType isn't empty but haveAppForType is false -- but |
|
427 // the doc for this method says that if we have a MIME type (in |
|
428 // aMIMEType), we need to give it preference.) |
|
429 NSURLFileTypeMappings *map = [NSURLFileTypeMappings sharedMappings]; |
|
430 NSString *extStr = [NSString stringWithCString:flatExt.get() encoding:NSASCIIStringEncoding]; |
|
431 NSString *typeStr = map ? [map MIMETypeForExtension:extStr] : NULL; |
|
432 if (typeStr) { |
|
433 nsAutoCString mimeType; |
|
434 mimeType.Assign((char *)[typeStr cStringUsingEncoding:NSASCIIStringEncoding]); |
|
435 mimeInfoMac->SetMIMEType(mimeType); |
|
436 haveAppForType = true; |
|
437 } else { |
|
438 // Sometimes the OS won't give us a MIME type for an extension that's |
|
439 // registered with Launch Services and has a default app: For example |
|
440 // Real Player registers itself for the "ogg" extension and for the |
|
441 // audio/x-ogg and application/x-ogg MIME types, but |
|
442 // MIMETypeForExtension returns nil for the "ogg" extension even on |
|
443 // systems where Real Player is installed. This is probably an Apple |
|
444 // bug. But bad things happen if we return an nsIMIMEInfo structure |
|
445 // with an empty MIME type and set *aFound to true. So in this |
|
446 // case we need to set it to false here. |
|
447 haveAppForExt = false; |
|
448 extAppIsDefault = false; |
|
449 *aFound = false; |
|
450 } |
|
451 } else { |
|
452 // Otherwise set the MIME type to a reasonable fallback. |
|
453 mimeInfoMac->SetMIMEType(NS_LITERAL_CSTRING(APPLICATION_OCTET_STREAM)); |
|
454 } |
|
455 } |
|
456 |
|
457 if (typeAppIsDefault || extAppIsDefault) { |
|
458 if (haveAppForExt) |
|
459 mimeInfoMac->AppendExtension(aFileExt); |
|
460 |
|
461 nsCOMPtr<nsILocalFileMac> app(do_CreateInstance(NS_LOCAL_FILE_CONTRACTID)); |
|
462 if (!app) { |
|
463 [localPool release]; |
|
464 return nullptr; |
|
465 } |
|
466 |
|
467 CFStringRef cfAppName = NULL; |
|
468 if (typeAppIsDefault) { |
|
469 app->InitWithFSRef(&typeAppFSRef); |
|
470 ::LSCopyItemAttribute((const FSRef *) &typeAppFSRef, kLSRolesAll, |
|
471 kLSItemDisplayName, (CFTypeRef *) &cfAppName); |
|
472 } else { |
|
473 app->InitWithFSRef(&extAppFSRef); |
|
474 ::LSCopyItemAttribute((const FSRef *) &extAppFSRef, kLSRolesAll, |
|
475 kLSItemDisplayName, (CFTypeRef *) &cfAppName); |
|
476 } |
|
477 if (cfAppName) { |
|
478 nsAutoTArray<UniChar, 255> buffer; |
|
479 CFIndex appNameLength = ::CFStringGetLength(cfAppName); |
|
480 buffer.SetLength(appNameLength); |
|
481 ::CFStringGetCharacters(cfAppName, CFRangeMake(0, appNameLength), |
|
482 buffer.Elements()); |
|
483 nsAutoString appName; |
|
484 appName.Assign(reinterpret_cast<char16_t*>(buffer.Elements()), appNameLength); |
|
485 mimeInfoMac->SetDefaultDescription(appName); |
|
486 ::CFRelease(cfAppName); |
|
487 } |
|
488 |
|
489 mimeInfoMac->SetDefaultApplication(app); |
|
490 mimeInfoMac->SetPreferredAction(nsIMIMEInfo::useSystemDefault); |
|
491 } else { |
|
492 mimeInfoMac->SetPreferredAction(nsIMIMEInfo::saveToDisk); |
|
493 } |
|
494 |
|
495 nsAutoCString mimeType; |
|
496 mimeInfoMac->GetMIMEType(mimeType); |
|
497 if (*aFound && !mimeType.IsEmpty()) { |
|
498 // If we have a MIME type, make sure its preferred extension is included |
|
499 // in our list. |
|
500 NSURLFileTypeMappings *map = [NSURLFileTypeMappings sharedMappings]; |
|
501 NSString *typeStr = [NSString stringWithCString:mimeType.get() encoding:NSASCIIStringEncoding]; |
|
502 NSString *extStr = map ? [map preferredExtensionForMIMEType:typeStr] : NULL; |
|
503 if (extStr) { |
|
504 nsAutoCString preferredExt; |
|
505 preferredExt.Assign((char *)[extStr cStringUsingEncoding:NSASCIIStringEncoding]); |
|
506 mimeInfoMac->AppendExtension(preferredExt); |
|
507 } |
|
508 |
|
509 CFStringRef cfType = ::CFStringCreateWithCString(NULL, mimeType.get(), kCFStringEncodingUTF8); |
|
510 if (cfType) { |
|
511 CFStringRef cfTypeDesc = NULL; |
|
512 if (::LSCopyKindStringForMIMEType(cfType, &cfTypeDesc) == noErr) { |
|
513 nsAutoTArray<UniChar, 255> buffer; |
|
514 CFIndex typeDescLength = ::CFStringGetLength(cfTypeDesc); |
|
515 buffer.SetLength(typeDescLength); |
|
516 ::CFStringGetCharacters(cfTypeDesc, CFRangeMake(0, typeDescLength), |
|
517 buffer.Elements()); |
|
518 nsAutoString typeDesc; |
|
519 typeDesc.Assign(reinterpret_cast<char16_t*>(buffer.Elements()), typeDescLength); |
|
520 mimeInfoMac->SetDescription(typeDesc); |
|
521 } |
|
522 if (cfTypeDesc) { |
|
523 ::CFRelease(cfTypeDesc); |
|
524 } |
|
525 ::CFRelease(cfType); |
|
526 } |
|
527 } |
|
528 |
|
529 PR_LOG(mLog, PR_LOG_DEBUG, ("OS gave us: type '%s' found '%i'\n", mimeType.get(), *aFound)); |
|
530 |
|
531 [localPool release]; |
|
532 return mimeInfoMac.forget(); |
|
533 |
|
534 NS_OBJC_END_TRY_ABORT_BLOCK_NSNULL; |
|
535 } |
|
536 |
|
537 NS_IMETHODIMP |
|
538 nsOSHelperAppService::GetProtocolHandlerInfoFromOS(const nsACString &aScheme, |
|
539 bool *found, |
|
540 nsIHandlerInfo **_retval) |
|
541 { |
|
542 NS_ASSERTION(!aScheme.IsEmpty(), "No scheme was specified!"); |
|
543 |
|
544 nsresult rv = OSProtocolHandlerExists(nsPromiseFlatCString(aScheme).get(), |
|
545 found); |
|
546 if (NS_FAILED(rv)) |
|
547 return rv; |
|
548 |
|
549 nsMIMEInfoMac *handlerInfo = |
|
550 new nsMIMEInfoMac(aScheme, nsMIMEInfoBase::eProtocolInfo); |
|
551 NS_ENSURE_TRUE(handlerInfo, NS_ERROR_OUT_OF_MEMORY); |
|
552 NS_ADDREF(*_retval = handlerInfo); |
|
553 |
|
554 if (!*found) { |
|
555 // Code that calls this requires an object regardless if the OS has |
|
556 // something for us, so we return the empty object. |
|
557 return NS_OK; |
|
558 } |
|
559 |
|
560 nsAutoString desc; |
|
561 GetApplicationDescription(aScheme, desc); |
|
562 handlerInfo->SetDefaultDescription(desc); |
|
563 |
|
564 return NS_OK; |
|
565 } |
|
566 |