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"
9 #include "nsCocoaUtils.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
17 // than one. The determination of 'newest' is complex and beyond the scope of
20 NS_IMPL_ISUPPORTS(nsMacWebAppUtils, nsIMacWebAppUtils)
22 NS_IMETHODIMP nsMacWebAppUtils::PathForAppWithIdentifier(
23 const nsAString& bundleIdentifier, nsAString& outPath) {
24 NS_OBJC_BEGIN_TRY_BLOCK_RETURN;
28 nsAutoreleasePool localPool;
30 // note that the result of this expression might be nil, meaning no matching
32 NSString* temp = [[NSWorkspace sharedWorkspace]
33 absolutePathForAppBundleWithIdentifier:
35 stringWithCharacters:reinterpret_cast<const unichar*>(
36 ((nsString)bundleIdentifier).get())
37 length:((nsString)bundleIdentifier).Length()]];
40 // Copy out the resultant absolute path into outPath if non-nil.
41 nsCocoaUtils::GetStringForNSString(temp, outPath);
46 NS_OBJC_END_TRY_BLOCK_RETURN(NS_ERROR_FAILURE);
49 NS_IMETHODIMP nsMacWebAppUtils::LaunchAppWithIdentifier(
50 const nsAString& bundleIdentifier) {
51 NS_OBJC_BEGIN_TRY_BLOCK_RETURN;
53 nsAutoreleasePool localPool;
55 // Note this might return false, meaning the app wasnt launched for some
57 BOOL success = [[NSWorkspace sharedWorkspace]
58 launchAppWithBundleIdentifier:
60 stringWithCharacters:reinterpret_cast<const unichar*>(
61 ((nsString)bundleIdentifier).get())
62 length:((nsString)bundleIdentifier).Length()]
63 options:(NSWorkspaceLaunchOptions)0
64 additionalEventParamDescriptor:nil
65 launchIdentifier:NULL];
67 return success ? NS_OK : NS_ERROR_FAILURE;
69 NS_OBJC_END_TRY_BLOCK_RETURN(NS_ERROR_FAILURE);
72 NS_IMETHODIMP nsMacWebAppUtils::TrashApp(const nsAString& path,
73 nsITrashAppCallback* aCallback) {
74 NS_OBJC_BEGIN_TRY_BLOCK_RETURN;
76 if (NS_WARN_IF(!aCallback)) {
77 return NS_ERROR_INVALID_ARG;
80 nsCOMPtr<nsITrashAppCallback> callback = aCallback;
82 NSString* tempString =
83 [NSString stringWithCharacters:reinterpret_cast<const unichar*>(
84 ((nsString)path).get())
85 length:path.Length()];
87 [[NSWorkspace sharedWorkspace]
89 arrayWithObject:[NSURL fileURLWithPath:tempString]]
90 completionHandler:^(NSDictionary* newURLs, NSError* error) {
91 nsresult rv = (error == nil) ? NS_OK : NS_ERROR_FAILURE;
92 callback->TrashAppFinished(rv);
97 NS_OBJC_END_TRY_BLOCK_RETURN(NS_ERROR_FAILURE);