Wed, 31 Dec 2014 07:53:36 +0100
Correct small whitespace inconsistency, lost while renaming variables.
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/. */
7 #import <ApplicationServices/ApplicationServices.h>
9 #include "nsObjCExceptions.h"
10 #include "nsMIMEInfoMac.h"
11 #include "nsILocalFileMac.h"
12 #include "nsIFileURL.h"
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 }
27 return nsMIMEInfoImpl::GetDefaultDescription(aDefaultDescription);
28 }
30 NS_IMETHODIMP
31 nsMIMEInfoMac::LaunchWithFile(nsIFile *aFile)
32 {
33 NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
35 nsCOMPtr<nsIFile> application;
36 nsresult rv;
38 NS_ASSERTION(mClass == eMIMEInfo, "only MIME infos are currently allowed"
39 "to pass content by value");
41 if (mPreferredAction == useHelperApp) {
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);
49 rv = localHandlerApp->GetExecutable(getter_AddRefs(application));
50 NS_ENSURE_SUCCESS(rv, rv);
52 } else if (mPreferredAction == useSystemDefault) {
53 application = mDefaultApplication;
54 }
55 else
56 return NS_ERROR_INVALID_ARG;
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;
68 FSRef tempFileRef;
69 tempFile->GetFSRef(&tempFileRef);
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);
83 NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
84 }
86 nsresult
87 nsMIMEInfoMac::LoadUriInternal(nsIURI *aURI)
88 {
89 NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
91 NS_ENSURE_ARG_POINTER(aURI);
93 nsresult rv = NS_ERROR_FAILURE;
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 }
111 return rv;
113 NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
114 }