From 26a029d407be480d791972afb5975cf62c9360a6 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Fri, 19 Apr 2024 02:47:55 +0200 Subject: Adding upstream version 124.0.1. Signed-off-by: Daniel Baumann --- widget/cocoa/nsMacWebAppUtils.mm | 98 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 98 insertions(+) create mode 100644 widget/cocoa/nsMacWebAppUtils.mm (limited to 'widget/cocoa/nsMacWebAppUtils.mm') diff --git a/widget/cocoa/nsMacWebAppUtils.mm b/widget/cocoa/nsMacWebAppUtils.mm new file mode 100644 index 0000000000..e1a06f873d --- /dev/null +++ b/widget/cocoa/nsMacWebAppUtils.mm @@ -0,0 +1,98 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#import + +#include "nsMacWebAppUtils.h" +#include "nsCOMPtr.h" +#include "nsCocoaUtils.h" +#include "nsString.h" + +// This must be included last: +#include "nsObjCExceptions.h" + +// Find the path to the app with the given bundleIdentifier, if any. +// Note that the OS will return the path to the newest binary, if there is more +// than one. The determination of 'newest' is complex and beyond the scope of +// this comment. + +NS_IMPL_ISUPPORTS(nsMacWebAppUtils, nsIMacWebAppUtils) + +NS_IMETHODIMP nsMacWebAppUtils::PathForAppWithIdentifier( + const nsAString& bundleIdentifier, nsAString& outPath) { + NS_OBJC_BEGIN_TRY_BLOCK_RETURN; + + outPath.Truncate(); + + nsAutoreleasePool localPool; + + // note that the result of this expression might be nil, meaning no matching + // app was found. + NSString* temp = [[NSWorkspace sharedWorkspace] + absolutePathForAppBundleWithIdentifier: + [NSString + stringWithCharacters:reinterpret_cast( + ((nsString)bundleIdentifier).get()) + length:((nsString)bundleIdentifier).Length()]]; + + if (temp) { + // Copy out the resultant absolute path into outPath if non-nil. + nsCocoaUtils::GetStringForNSString(temp, outPath); + } + + return NS_OK; + + NS_OBJC_END_TRY_BLOCK_RETURN(NS_ERROR_FAILURE); +} + +NS_IMETHODIMP nsMacWebAppUtils::LaunchAppWithIdentifier( + const nsAString& bundleIdentifier) { + NS_OBJC_BEGIN_TRY_BLOCK_RETURN; + + nsAutoreleasePool localPool; + + // Note this might return false, meaning the app wasnt launched for some + // reason. + BOOL success = [[NSWorkspace sharedWorkspace] + launchAppWithBundleIdentifier: + [NSString + stringWithCharacters:reinterpret_cast( + ((nsString)bundleIdentifier).get()) + length:((nsString)bundleIdentifier).Length()] + options:(NSWorkspaceLaunchOptions)0 + additionalEventParamDescriptor:nil + launchIdentifier:NULL]; + + return success ? NS_OK : NS_ERROR_FAILURE; + + NS_OBJC_END_TRY_BLOCK_RETURN(NS_ERROR_FAILURE); +} + +NS_IMETHODIMP nsMacWebAppUtils::TrashApp(const nsAString& path, + nsITrashAppCallback* aCallback) { + NS_OBJC_BEGIN_TRY_BLOCK_RETURN; + + if (NS_WARN_IF(!aCallback)) { + return NS_ERROR_INVALID_ARG; + } + + nsCOMPtr callback = aCallback; + + NSString* tempString = + [NSString stringWithCharacters:reinterpret_cast( + ((nsString)path).get()) + length:path.Length()]; + + [[NSWorkspace sharedWorkspace] + recycleURLs:[NSArray + arrayWithObject:[NSURL fileURLWithPath:tempString]] + completionHandler:^(NSDictionary* newURLs, NSError* error) { + nsresult rv = (error == nil) ? NS_OK : NS_ERROR_FAILURE; + callback->TrashAppFinished(rv); + }]; + + return NS_OK; + + NS_OBJC_END_TRY_BLOCK_RETURN(NS_ERROR_FAILURE); +} -- cgit v1.2.3