Bug 1861709 replace AudioCallbackDriver::ThreadRunning() assertions that mean to...
[gecko.git] / widget / cocoa / nsMacWebAppUtils.mm
blobe1a06f873d6e257dc489a0cd15aa00d1416e414f
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
17 // than one. The determination of 'newest' is complex and beyond the scope of
18 // this comment.
20 NS_IMPL_ISUPPORTS(nsMacWebAppUtils, nsIMacWebAppUtils)
22 NS_IMETHODIMP nsMacWebAppUtils::PathForAppWithIdentifier(
23     const nsAString& bundleIdentifier, nsAString& outPath) {
24   NS_OBJC_BEGIN_TRY_BLOCK_RETURN;
26   outPath.Truncate();
28   nsAutoreleasePool localPool;
30   // note that the result of this expression might be nil, meaning no matching
31   // app was found.
32   NSString* temp = [[NSWorkspace sharedWorkspace]
33       absolutePathForAppBundleWithIdentifier:
34           [NSString
35               stringWithCharacters:reinterpret_cast<const unichar*>(
36                                        ((nsString)bundleIdentifier).get())
37                             length:((nsString)bundleIdentifier).Length()]];
39   if (temp) {
40     // Copy out the resultant absolute path into outPath if non-nil.
41     nsCocoaUtils::GetStringForNSString(temp, outPath);
42   }
44   return NS_OK;
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
56   // reason.
57   BOOL success = [[NSWorkspace sharedWorkspace]
58        launchAppWithBundleIdentifier:
59            [NSString
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;
78   }
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]
88             recycleURLs:[NSArray
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);
93       }];
95   return NS_OK;
97   NS_OBJC_END_TRY_BLOCK_RETURN(NS_ERROR_FAILURE);