michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #import michael@0: michael@0: #include "nsMacWebAppUtils.h" michael@0: #include "nsCOMPtr.h" michael@0: #include "nsCocoaUtils.h" michael@0: #include "nsString.h" michael@0: michael@0: // This must be included last: michael@0: #include "nsObjCExceptions.h" michael@0: michael@0: // Find the path to the app with the given bundleIdentifier, if any. michael@0: // Note that the OS will return the path to the newest binary, if there is more than one. michael@0: // The determination of 'newest' is complex and beyond the scope of this comment. michael@0: michael@0: NS_IMPL_ISUPPORTS(nsMacWebAppUtils, nsIMacWebAppUtils) michael@0: michael@0: NS_IMETHODIMP nsMacWebAppUtils::PathForAppWithIdentifier(const nsAString& bundleIdentifier, nsAString& outPath) { michael@0: NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT; michael@0: michael@0: outPath.Truncate(); michael@0: michael@0: nsAutoreleasePool localPool; michael@0: michael@0: //note that the result of this expression might be nil, meaning no matching app was found. michael@0: NSString* temp = [[NSWorkspace sharedWorkspace] absolutePathForAppBundleWithIdentifier: michael@0: [NSString stringWithCharacters:reinterpret_cast(((nsString)bundleIdentifier).get()) michael@0: length:((nsString)bundleIdentifier).Length()]]; michael@0: michael@0: if (temp) { michael@0: // Copy out the resultant absolute path into outPath if non-nil. michael@0: nsCocoaUtils::GetStringForNSString(temp, outPath); michael@0: } michael@0: michael@0: return NS_OK; michael@0: michael@0: NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT; michael@0: } michael@0: michael@0: NS_IMETHODIMP nsMacWebAppUtils::LaunchAppWithIdentifier(const nsAString& bundleIdentifier) { michael@0: NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT; michael@0: michael@0: nsAutoreleasePool localPool; michael@0: michael@0: // Note this might return false, meaning the app wasnt launched for some reason. michael@0: BOOL success = [[NSWorkspace sharedWorkspace] launchAppWithBundleIdentifier: michael@0: [NSString stringWithCharacters:reinterpret_cast(((nsString)bundleIdentifier).get()) michael@0: length:((nsString)bundleIdentifier).Length()] michael@0: options: (NSWorkspaceLaunchOptions)0 michael@0: additionalEventParamDescriptor: nil michael@0: launchIdentifier: NULL]; michael@0: michael@0: return success ? NS_OK : NS_ERROR_FAILURE; michael@0: michael@0: NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT; michael@0: } michael@0: michael@0: NS_IMETHODIMP nsMacWebAppUtils::TrashApp(const nsAString& path, nsITrashAppCallback* aCallback) { michael@0: NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT; michael@0: michael@0: if (NS_WARN_IF(!aCallback)) { michael@0: return NS_ERROR_INVALID_ARG; michael@0: } michael@0: michael@0: nsCOMPtr callback = aCallback; michael@0: michael@0: NSString* tempString = [NSString stringWithCharacters:reinterpret_cast(((nsString)path).get()) michael@0: length:path.Length()]; michael@0: michael@0: [[NSWorkspace sharedWorkspace] recycleURLs: [NSArray arrayWithObject:[NSURL fileURLWithPath:tempString]] michael@0: completionHandler: ^(NSDictionary *newURLs, NSError *error) { michael@0: nsresult rv = (error == nil) ? NS_OK : NS_ERROR_FAILURE; michael@0: callback->TrashAppFinished(rv); michael@0: }]; michael@0: michael@0: return NS_OK; michael@0: michael@0: NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT; michael@0: }