uriloader/exthandler/mac/nsLocalHandlerAppMac.mm

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/uriloader/exthandler/mac/nsLocalHandlerAppMac.mm	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,84 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +#import <CoreFoundation/CoreFoundation.h>
     1.9 +#import <ApplicationServices/ApplicationServices.h>
    1.10 +
    1.11 +#include "nsObjCExceptions.h"
    1.12 +#include "nsLocalHandlerAppMac.h"
    1.13 +#include "nsILocalFileMac.h"
    1.14 +#include "nsIURI.h"
    1.15 +
    1.16 +// We override this to make sure app bundles display their pretty name (without .app suffix)
    1.17 +NS_IMETHODIMP nsLocalHandlerAppMac::GetName(nsAString& aName)
    1.18 +{
    1.19 +  if (mExecutable) {
    1.20 +    nsCOMPtr<nsILocalFileMac> macFile = do_QueryInterface(mExecutable);
    1.21 +    if (macFile) {
    1.22 +      bool isPackage;
    1.23 +      (void)macFile->IsPackage(&isPackage);
    1.24 +      if (isPackage)
    1.25 +        return macFile->GetBundleDisplayName(aName);
    1.26 +    }
    1.27 +  }
    1.28 +
    1.29 +  return nsLocalHandlerApp::GetName(aName);
    1.30 +}
    1.31 +
    1.32 +/** 
    1.33 + * mostly copy/pasted from nsMacShellService.cpp (which is in browser/,
    1.34 + * so we can't depend on it here).  This code probably really wants to live
    1.35 + * somewhere more central (see bug 389922).
    1.36 + */
    1.37 +NS_IMETHODIMP
    1.38 +nsLocalHandlerAppMac::LaunchWithURI(nsIURI *aURI,
    1.39 +                                    nsIInterfaceRequestor *aWindowContext)
    1.40 +{
    1.41 +  NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
    1.42 +
    1.43 +  nsresult rv;
    1.44 +  nsCOMPtr<nsILocalFileMac> lfm(do_QueryInterface(mExecutable, &rv));
    1.45 +  NS_ENSURE_SUCCESS(rv, rv);
    1.46 +  
    1.47 +  CFURLRef appURL;
    1.48 +  rv = lfm->GetCFURL(&appURL);
    1.49 +  if (NS_FAILED(rv))
    1.50 +    return rv;
    1.51 +  
    1.52 +  nsAutoCString uriSpec;
    1.53 +  aURI->GetAsciiSpec(uriSpec);
    1.54 +
    1.55 +  const UInt8* uriString = reinterpret_cast<const UInt8*>(uriSpec.get());
    1.56 +  CFURLRef uri = ::CFURLCreateWithBytes(NULL, uriString, uriSpec.Length(),
    1.57 +                                        kCFStringEncodingUTF8, NULL);
    1.58 +  if (!uri) {
    1.59 +    ::CFRelease(appURL);
    1.60 +    return NS_ERROR_OUT_OF_MEMORY;
    1.61 +  }
    1.62 +  
    1.63 +  CFArrayRef uris = ::CFArrayCreate(NULL, reinterpret_cast<const void**>(&uri),
    1.64 +                                    1, NULL);
    1.65 +  if (!uris) {
    1.66 +    ::CFRelease(uri);
    1.67 +    ::CFRelease(appURL);
    1.68 +    return NS_ERROR_OUT_OF_MEMORY;
    1.69 +  }
    1.70 +  
    1.71 +  LSLaunchURLSpec launchSpec;
    1.72 +  launchSpec.appURL = appURL;
    1.73 +  launchSpec.itemURLs = uris;
    1.74 +  launchSpec.passThruParams = NULL;
    1.75 +  launchSpec.launchFlags = kLSLaunchDefaults;
    1.76 +  launchSpec.asyncRefCon = NULL;
    1.77 +  
    1.78 +  OSErr err = ::LSOpenFromURLSpec(&launchSpec, NULL);
    1.79 +  
    1.80 +  ::CFRelease(uris);
    1.81 +  ::CFRelease(uri);
    1.82 +  ::CFRelease(appURL);
    1.83 +  
    1.84 +  return err != noErr ? NS_ERROR_FAILURE : NS_OK;
    1.85 +
    1.86 +  NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
    1.87 +}

mercurial