Bug 627938: Fix nsGlobalChromeWindow cleanup. (r=smaug, a=jst)
[mozilla-central.git] / uriloader / exthandler / mac / nsLocalHandlerAppMac.mm
blob8aa23930eeea98488fa90fb7d18e310c79877cfc
1 /* ***** BEGIN LICENSE BLOCK *****
2  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
3  *
4  * The contents of this file are subject to the Mozilla Public License Version
5  * 1.1 (the "License"); you may not use this file except in compliance with
6  * the License. You may obtain a copy of the License at
7  * http://www.mozilla.org/MPL/
8  *
9  * Software distributed under the License is distributed on an "AS IS" basis,
10  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11  * for the specific language governing rights and limitations under the
12  * License.
13  *
14  * The Original Code is Mozilla code.
15  *
16  * The Initial Developer of the Original Code is
17  * the Mozilla Foundation.
18  * Portions created by the Initial Developer are Copyright (C) 2007
19  * the Initial Developer. All Rights Reserved.
20  *
21  * Contributor(s):
22  *   Dan Mosedale <dmose@mozilla.org>
23  *
24  * Alternatively, the contents of this file may be used under the terms of
25  * either the GNU General Public License Version 2 or later (the "GPL"), or
26  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27  * in which case the provisions of the GPL or the LGPL are applicable instead
28  * of those above. If you wish to allow use of your version of this file only
29  * under the terms of either the GPL or the LGPL, and not to allow others to
30  * use your version of this file under the terms of the MPL, indicate your
31  * decision by deleting the provisions above and replace them with the notice
32  * and other provisions required by the GPL or the LGPL. If you do not delete
33  * the provisions above, a recipient may use your version of this file under
34  * the terms of any one of the MPL, the GPL or the LGPL.
35  *
36  * ***** END LICENSE BLOCK ***** */
38 #import <CoreFoundation/CoreFoundation.h>
39 #import <ApplicationServices/ApplicationServices.h>
41 #include "nsObjCExceptions.h"
42 #include "nsLocalHandlerAppMac.h"
43 #include "nsILocalFileMac.h"
44 #include "nsIURI.h"
46 // We override this to make sure app bundles display their pretty name (without .app suffix)
47 NS_IMETHODIMP nsLocalHandlerAppMac::GetName(nsAString& aName)
49   if (mExecutable) {
50     nsCOMPtr<nsILocalFileMac> macFile = do_QueryInterface(mExecutable);
51     if (macFile) {
52       PRBool isPackage;
53       (void)macFile->IsPackage(&isPackage);
54       if (isPackage)
55         return macFile->GetBundleDisplayName(aName);
56     }
57   }
59   return nsLocalHandlerApp::GetName(aName);
62 /** 
63  * mostly copy/pasted from nsMacShellService.cpp (which is in browser/,
64  * so we can't depend on it here).  This code probably really wants to live
65  * somewhere more central (see bug 389922).
66  */
67 NS_IMETHODIMP
68 nsLocalHandlerAppMac::LaunchWithURI(nsIURI *aURI,
69                                     nsIInterfaceRequestor *aWindowContext)
71   NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
73   nsresult rv;
74   nsCOMPtr<nsILocalFileMac> lfm(do_QueryInterface(mExecutable, &rv));
75   NS_ENSURE_SUCCESS(rv, rv);
76   
77   CFURLRef appURL;
78   rv = lfm->GetCFURL(&appURL);
79   if (NS_FAILED(rv))
80     return rv;
81   
82   nsCAutoString uriSpec;
83   aURI->GetAsciiSpec(uriSpec);
85   const UInt8* uriString = reinterpret_cast<const UInt8*>(uriSpec.get());
86   CFURLRef uri = ::CFURLCreateWithBytes(NULL, uriString, uriSpec.Length(),
87                                         kCFStringEncodingUTF8, NULL);
88   if (!uri) {
89     ::CFRelease(appURL);
90     return NS_ERROR_OUT_OF_MEMORY;
91   }
92   
93   CFArrayRef uris = ::CFArrayCreate(NULL, reinterpret_cast<const void**>(&uri),
94                                     1, NULL);
95   if (!uris) {
96     ::CFRelease(uri);
97     ::CFRelease(appURL);
98     return NS_ERROR_OUT_OF_MEMORY;
99   }
100   
101   LSLaunchURLSpec launchSpec;
102   launchSpec.appURL = appURL;
103   launchSpec.itemURLs = uris;
104   launchSpec.passThruParams = NULL;
105   launchSpec.launchFlags = kLSLaunchDefaults;
106   launchSpec.asyncRefCon = NULL;
107   
108   OSErr err = ::LSOpenFromURLSpec(&launchSpec, NULL);
109   
110   ::CFRelease(uris);
111   ::CFRelease(uri);
112   ::CFRelease(appURL);
113   
114   return err != noErr ? NS_ERROR_FAILURE : NS_OK;
116   NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;