Bumping gaia.json for 2 gaia revision(s) a=gaia-bump
[gecko.git] / widget / cocoa / nsMacWebAppUtils.mm
blob1b98cef7cc23348ae048852952443cb02e6e07c2
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2  * License, v. 2.0. If a copy of the MPL was not distributed with this
3  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 #import <Cocoa/Cocoa.h>
7 #include "nsMacWebAppUtils.h"
8 #include "nsCOMPtr.h"
9 #include "nsCocoaUtils.h"
10 #include "nsString.h"
12 // This must be included last:
13 #include "nsObjCExceptions.h"
15 // Find the path to the app with the given bundleIdentifier, if any.
16 // Note that the OS will return the path to the newest binary, if there is more than one.
17 // The determination of 'newest' is complex and beyond the scope of this comment.
19 NS_IMPL_ISUPPORTS(nsMacWebAppUtils, nsIMacWebAppUtils)
21 NS_IMETHODIMP nsMacWebAppUtils::PathForAppWithIdentifier(const nsAString& bundleIdentifier, nsAString& outPath) {
22   NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
24   outPath.Truncate();
26   nsAutoreleasePool localPool;
28   //note that the result of this expression might be nil, meaning no matching app was found. 
29   NSString* temp = [[NSWorkspace sharedWorkspace] absolutePathForAppBundleWithIdentifier:
30                         [NSString stringWithCharacters:reinterpret_cast<const unichar*>(((nsString)bundleIdentifier).get())
31                                                 length:((nsString)bundleIdentifier).Length()]];
33   if (temp) {
34     // Copy out the resultant absolute path into outPath if non-nil.
35     nsCocoaUtils::GetStringForNSString(temp, outPath);
36   }
38   return NS_OK;
40   NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
43 NS_IMETHODIMP nsMacWebAppUtils::LaunchAppWithIdentifier(const nsAString& bundleIdentifier) {
44   NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
46   nsAutoreleasePool localPool;
48   // Note this might return false, meaning the app wasnt launched for some reason. 
49   BOOL success = [[NSWorkspace sharedWorkspace] launchAppWithBundleIdentifier:
50                         [NSString stringWithCharacters:reinterpret_cast<const unichar*>(((nsString)bundleIdentifier).get())
51                                                 length:((nsString)bundleIdentifier).Length()]
52                         options: (NSWorkspaceLaunchOptions)0
53                         additionalEventParamDescriptor: nil
54                         launchIdentifier: NULL];
56   return success ? NS_OK : NS_ERROR_FAILURE;
58   NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
61 NS_IMETHODIMP nsMacWebAppUtils::TrashApp(const nsAString& path, nsITrashAppCallback* aCallback) {
62   NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
64   if (NS_WARN_IF(!aCallback)) {
65     return NS_ERROR_INVALID_ARG;
66   }
68   nsCOMPtr<nsITrashAppCallback> callback = aCallback;
70   NSString* tempString = [NSString stringWithCharacters:reinterpret_cast<const unichar*>(((nsString)path).get())
71                                    length:path.Length()];
73   [[NSWorkspace sharedWorkspace] recycleURLs: [NSArray arrayWithObject:[NSURL fileURLWithPath:tempString]]
74     completionHandler: ^(NSDictionary *newURLs, NSError *error) {
75       nsresult rv = (error == nil) ? NS_OK : NS_ERROR_FAILURE;
76       callback->TrashAppFinished(rv);
77     }];
79   return NS_OK;
81   NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;