1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/widget/cocoa/nsMacWebAppUtils.mm Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,82 @@ 1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.7 + 1.8 +#import <Cocoa/Cocoa.h> 1.9 + 1.10 +#include "nsMacWebAppUtils.h" 1.11 +#include "nsCOMPtr.h" 1.12 +#include "nsCocoaUtils.h" 1.13 +#include "nsString.h" 1.14 + 1.15 +// This must be included last: 1.16 +#include "nsObjCExceptions.h" 1.17 + 1.18 +// Find the path to the app with the given bundleIdentifier, if any. 1.19 +// Note that the OS will return the path to the newest binary, if there is more than one. 1.20 +// The determination of 'newest' is complex and beyond the scope of this comment. 1.21 + 1.22 +NS_IMPL_ISUPPORTS(nsMacWebAppUtils, nsIMacWebAppUtils) 1.23 + 1.24 +NS_IMETHODIMP nsMacWebAppUtils::PathForAppWithIdentifier(const nsAString& bundleIdentifier, nsAString& outPath) { 1.25 + NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT; 1.26 + 1.27 + outPath.Truncate(); 1.28 + 1.29 + nsAutoreleasePool localPool; 1.30 + 1.31 + //note that the result of this expression might be nil, meaning no matching app was found. 1.32 + NSString* temp = [[NSWorkspace sharedWorkspace] absolutePathForAppBundleWithIdentifier: 1.33 + [NSString stringWithCharacters:reinterpret_cast<const unichar*>(((nsString)bundleIdentifier).get()) 1.34 + length:((nsString)bundleIdentifier).Length()]]; 1.35 + 1.36 + if (temp) { 1.37 + // Copy out the resultant absolute path into outPath if non-nil. 1.38 + nsCocoaUtils::GetStringForNSString(temp, outPath); 1.39 + } 1.40 + 1.41 + return NS_OK; 1.42 + 1.43 + NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT; 1.44 +} 1.45 + 1.46 +NS_IMETHODIMP nsMacWebAppUtils::LaunchAppWithIdentifier(const nsAString& bundleIdentifier) { 1.47 + NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT; 1.48 + 1.49 + nsAutoreleasePool localPool; 1.50 + 1.51 + // Note this might return false, meaning the app wasnt launched for some reason. 1.52 + BOOL success = [[NSWorkspace sharedWorkspace] launchAppWithBundleIdentifier: 1.53 + [NSString stringWithCharacters:reinterpret_cast<const unichar*>(((nsString)bundleIdentifier).get()) 1.54 + length:((nsString)bundleIdentifier).Length()] 1.55 + options: (NSWorkspaceLaunchOptions)0 1.56 + additionalEventParamDescriptor: nil 1.57 + launchIdentifier: NULL]; 1.58 + 1.59 + return success ? NS_OK : NS_ERROR_FAILURE; 1.60 + 1.61 + NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT; 1.62 +} 1.63 + 1.64 +NS_IMETHODIMP nsMacWebAppUtils::TrashApp(const nsAString& path, nsITrashAppCallback* aCallback) { 1.65 + NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT; 1.66 + 1.67 + if (NS_WARN_IF(!aCallback)) { 1.68 + return NS_ERROR_INVALID_ARG; 1.69 + } 1.70 + 1.71 + nsCOMPtr<nsITrashAppCallback> callback = aCallback; 1.72 + 1.73 + NSString* tempString = [NSString stringWithCharacters:reinterpret_cast<const unichar*>(((nsString)path).get()) 1.74 + length:path.Length()]; 1.75 + 1.76 + [[NSWorkspace sharedWorkspace] recycleURLs: [NSArray arrayWithObject:[NSURL fileURLWithPath:tempString]] 1.77 + completionHandler: ^(NSDictionary *newURLs, NSError *error) { 1.78 + nsresult rv = (error == nil) ? NS_OK : NS_ERROR_FAILURE; 1.79 + callback->TrashAppFinished(rv); 1.80 + }]; 1.81 + 1.82 + return NS_OK; 1.83 + 1.84 + NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT; 1.85 +}