|
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/. */ |
|
4 |
|
5 #import <Cocoa/Cocoa.h> |
|
6 |
|
7 #include "nsMacWebAppUtils.h" |
|
8 #include "nsCOMPtr.h" |
|
9 #include "nsCocoaUtils.h" |
|
10 #include "nsString.h" |
|
11 |
|
12 // This must be included last: |
|
13 #include "nsObjCExceptions.h" |
|
14 |
|
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. |
|
18 |
|
19 NS_IMPL_ISUPPORTS(nsMacWebAppUtils, nsIMacWebAppUtils) |
|
20 |
|
21 NS_IMETHODIMP nsMacWebAppUtils::PathForAppWithIdentifier(const nsAString& bundleIdentifier, nsAString& outPath) { |
|
22 NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT; |
|
23 |
|
24 outPath.Truncate(); |
|
25 |
|
26 nsAutoreleasePool localPool; |
|
27 |
|
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()]]; |
|
32 |
|
33 if (temp) { |
|
34 // Copy out the resultant absolute path into outPath if non-nil. |
|
35 nsCocoaUtils::GetStringForNSString(temp, outPath); |
|
36 } |
|
37 |
|
38 return NS_OK; |
|
39 |
|
40 NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT; |
|
41 } |
|
42 |
|
43 NS_IMETHODIMP nsMacWebAppUtils::LaunchAppWithIdentifier(const nsAString& bundleIdentifier) { |
|
44 NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT; |
|
45 |
|
46 nsAutoreleasePool localPool; |
|
47 |
|
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]; |
|
55 |
|
56 return success ? NS_OK : NS_ERROR_FAILURE; |
|
57 |
|
58 NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT; |
|
59 } |
|
60 |
|
61 NS_IMETHODIMP nsMacWebAppUtils::TrashApp(const nsAString& path, nsITrashAppCallback* aCallback) { |
|
62 NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT; |
|
63 |
|
64 if (NS_WARN_IF(!aCallback)) { |
|
65 return NS_ERROR_INVALID_ARG; |
|
66 } |
|
67 |
|
68 nsCOMPtr<nsITrashAppCallback> callback = aCallback; |
|
69 |
|
70 NSString* tempString = [NSString stringWithCharacters:reinterpret_cast<const unichar*>(((nsString)path).get()) |
|
71 length:path.Length()]; |
|
72 |
|
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 }]; |
|
78 |
|
79 return NS_OK; |
|
80 |
|
81 NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT; |
|
82 } |