|
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 #import <ApplicationServices/ApplicationServices.h> |
|
8 |
|
9 #include "nsObjCExceptions.h" |
|
10 #include "nsMIMEInfoMac.h" |
|
11 #include "nsILocalFileMac.h" |
|
12 #include "nsIFileURL.h" |
|
13 |
|
14 // We override this to make sure app bundles display their pretty name (without .app suffix) |
|
15 NS_IMETHODIMP nsMIMEInfoMac::GetDefaultDescription(nsAString& aDefaultDescription) |
|
16 { |
|
17 if (mDefaultApplication) { |
|
18 nsCOMPtr<nsILocalFileMac> macFile = do_QueryInterface(mDefaultApplication); |
|
19 if (macFile) { |
|
20 bool isPackage; |
|
21 (void)macFile->IsPackage(&isPackage); |
|
22 if (isPackage) |
|
23 return macFile->GetBundleDisplayName(aDefaultDescription); |
|
24 } |
|
25 } |
|
26 |
|
27 return nsMIMEInfoImpl::GetDefaultDescription(aDefaultDescription); |
|
28 } |
|
29 |
|
30 NS_IMETHODIMP |
|
31 nsMIMEInfoMac::LaunchWithFile(nsIFile *aFile) |
|
32 { |
|
33 NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT; |
|
34 |
|
35 nsCOMPtr<nsIFile> application; |
|
36 nsresult rv; |
|
37 |
|
38 NS_ASSERTION(mClass == eMIMEInfo, "only MIME infos are currently allowed" |
|
39 "to pass content by value"); |
|
40 |
|
41 if (mPreferredAction == useHelperApp) { |
|
42 |
|
43 // we don't yet support passing content by value (rather than reference) |
|
44 // to web apps. at some point, we will probably want to. |
|
45 nsCOMPtr<nsILocalHandlerApp> localHandlerApp = |
|
46 do_QueryInterface(mPreferredApplication, &rv); |
|
47 NS_ENSURE_SUCCESS(rv, rv); |
|
48 |
|
49 rv = localHandlerApp->GetExecutable(getter_AddRefs(application)); |
|
50 NS_ENSURE_SUCCESS(rv, rv); |
|
51 |
|
52 } else if (mPreferredAction == useSystemDefault) { |
|
53 application = mDefaultApplication; |
|
54 } |
|
55 else |
|
56 return NS_ERROR_INVALID_ARG; |
|
57 |
|
58 // if we've already got an app, just QI so we have the launchWithDoc method |
|
59 nsCOMPtr<nsILocalFileMac> app; |
|
60 if (application) { |
|
61 app = do_QueryInterface(application, &rv); |
|
62 if (NS_FAILED(rv)) return rv; |
|
63 } else { |
|
64 // otherwise ask LaunchServices for an app directly |
|
65 nsCOMPtr<nsILocalFileMac> tempFile = do_QueryInterface(aFile, &rv); |
|
66 if (NS_FAILED(rv)) return rv; |
|
67 |
|
68 FSRef tempFileRef; |
|
69 tempFile->GetFSRef(&tempFileRef); |
|
70 |
|
71 FSRef appFSRef; |
|
72 if (::LSGetApplicationForItem(&tempFileRef, kLSRolesAll, &appFSRef, nullptr) == noErr) |
|
73 { |
|
74 app = (do_CreateInstance("@mozilla.org/file/local;1")); |
|
75 if (!app) return NS_ERROR_FAILURE; |
|
76 app->InitWithFSRef(&appFSRef); |
|
77 } else { |
|
78 return NS_ERROR_FAILURE; |
|
79 } |
|
80 } |
|
81 return app->LaunchWithDoc(aFile, false); |
|
82 |
|
83 NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT; |
|
84 } |
|
85 |
|
86 nsresult |
|
87 nsMIMEInfoMac::LoadUriInternal(nsIURI *aURI) |
|
88 { |
|
89 NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT; |
|
90 |
|
91 NS_ENSURE_ARG_POINTER(aURI); |
|
92 |
|
93 nsresult rv = NS_ERROR_FAILURE; |
|
94 |
|
95 nsAutoCString uri; |
|
96 aURI->GetSpec(uri); |
|
97 if (!uri.IsEmpty()) { |
|
98 CFURLRef myURLRef = ::CFURLCreateWithBytes(kCFAllocatorDefault, |
|
99 (const UInt8*)uri.get(), |
|
100 strlen(uri.get()), |
|
101 kCFStringEncodingUTF8, |
|
102 NULL); |
|
103 if (myURLRef) { |
|
104 OSStatus status = ::LSOpenCFURLRef(myURLRef, NULL); |
|
105 if (status == noErr) |
|
106 rv = NS_OK; |
|
107 ::CFRelease(myURLRef); |
|
108 } |
|
109 } |
|
110 |
|
111 return rv; |
|
112 |
|
113 NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT; |
|
114 } |