1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/uriloader/exthandler/mac/nsMIMEInfoMac.mm Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,114 @@ 1.4 +/* -*- Mode: C++; tab-width: 3; indent-tabs-mode: nil; c-basic-offset: 2 -*- 1.5 + * 1.6 + * This Source Code Form is subject to the terms of the Mozilla Public 1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.9 + 1.10 +#import <ApplicationServices/ApplicationServices.h> 1.11 + 1.12 +#include "nsObjCExceptions.h" 1.13 +#include "nsMIMEInfoMac.h" 1.14 +#include "nsILocalFileMac.h" 1.15 +#include "nsIFileURL.h" 1.16 + 1.17 +// We override this to make sure app bundles display their pretty name (without .app suffix) 1.18 +NS_IMETHODIMP nsMIMEInfoMac::GetDefaultDescription(nsAString& aDefaultDescription) 1.19 +{ 1.20 + if (mDefaultApplication) { 1.21 + nsCOMPtr<nsILocalFileMac> macFile = do_QueryInterface(mDefaultApplication); 1.22 + if (macFile) { 1.23 + bool isPackage; 1.24 + (void)macFile->IsPackage(&isPackage); 1.25 + if (isPackage) 1.26 + return macFile->GetBundleDisplayName(aDefaultDescription); 1.27 + } 1.28 + } 1.29 + 1.30 + return nsMIMEInfoImpl::GetDefaultDescription(aDefaultDescription); 1.31 +} 1.32 + 1.33 +NS_IMETHODIMP 1.34 +nsMIMEInfoMac::LaunchWithFile(nsIFile *aFile) 1.35 +{ 1.36 + NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT; 1.37 + 1.38 + nsCOMPtr<nsIFile> application; 1.39 + nsresult rv; 1.40 + 1.41 + NS_ASSERTION(mClass == eMIMEInfo, "only MIME infos are currently allowed" 1.42 + "to pass content by value"); 1.43 + 1.44 + if (mPreferredAction == useHelperApp) { 1.45 + 1.46 + // we don't yet support passing content by value (rather than reference) 1.47 + // to web apps. at some point, we will probably want to. 1.48 + nsCOMPtr<nsILocalHandlerApp> localHandlerApp = 1.49 + do_QueryInterface(mPreferredApplication, &rv); 1.50 + NS_ENSURE_SUCCESS(rv, rv); 1.51 + 1.52 + rv = localHandlerApp->GetExecutable(getter_AddRefs(application)); 1.53 + NS_ENSURE_SUCCESS(rv, rv); 1.54 + 1.55 + } else if (mPreferredAction == useSystemDefault) { 1.56 + application = mDefaultApplication; 1.57 + } 1.58 + else 1.59 + return NS_ERROR_INVALID_ARG; 1.60 + 1.61 + // if we've already got an app, just QI so we have the launchWithDoc method 1.62 + nsCOMPtr<nsILocalFileMac> app; 1.63 + if (application) { 1.64 + app = do_QueryInterface(application, &rv); 1.65 + if (NS_FAILED(rv)) return rv; 1.66 + } else { 1.67 + // otherwise ask LaunchServices for an app directly 1.68 + nsCOMPtr<nsILocalFileMac> tempFile = do_QueryInterface(aFile, &rv); 1.69 + if (NS_FAILED(rv)) return rv; 1.70 + 1.71 + FSRef tempFileRef; 1.72 + tempFile->GetFSRef(&tempFileRef); 1.73 + 1.74 + FSRef appFSRef; 1.75 + if (::LSGetApplicationForItem(&tempFileRef, kLSRolesAll, &appFSRef, nullptr) == noErr) 1.76 + { 1.77 + app = (do_CreateInstance("@mozilla.org/file/local;1")); 1.78 + if (!app) return NS_ERROR_FAILURE; 1.79 + app->InitWithFSRef(&appFSRef); 1.80 + } else { 1.81 + return NS_ERROR_FAILURE; 1.82 + } 1.83 + } 1.84 + return app->LaunchWithDoc(aFile, false); 1.85 + 1.86 + NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT; 1.87 +} 1.88 + 1.89 +nsresult 1.90 +nsMIMEInfoMac::LoadUriInternal(nsIURI *aURI) 1.91 +{ 1.92 + NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT; 1.93 + 1.94 + NS_ENSURE_ARG_POINTER(aURI); 1.95 + 1.96 + nsresult rv = NS_ERROR_FAILURE; 1.97 + 1.98 + nsAutoCString uri; 1.99 + aURI->GetSpec(uri); 1.100 + if (!uri.IsEmpty()) { 1.101 + CFURLRef myURLRef = ::CFURLCreateWithBytes(kCFAllocatorDefault, 1.102 + (const UInt8*)uri.get(), 1.103 + strlen(uri.get()), 1.104 + kCFStringEncodingUTF8, 1.105 + NULL); 1.106 + if (myURLRef) { 1.107 + OSStatus status = ::LSOpenCFURLRef(myURLRef, NULL); 1.108 + if (status == noErr) 1.109 + rv = NS_OK; 1.110 + ::CFRelease(myURLRef); 1.111 + } 1.112 + } 1.113 + 1.114 + return rv; 1.115 + 1.116 + NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT; 1.117 +}