widget/cocoa/nsMacWebAppUtils.mm

Thu, 15 Jan 2015 15:59:08 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 15 Jan 2015 15:59:08 +0100
branch
TOR_BUG_9701
changeset 10
ac0c01689b40
permissions
-rw-r--r--

Implement a real Private Browsing Mode condition by changing the API/ABI;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4
michael@0 5 #import <Cocoa/Cocoa.h>
michael@0 6
michael@0 7 #include "nsMacWebAppUtils.h"
michael@0 8 #include "nsCOMPtr.h"
michael@0 9 #include "nsCocoaUtils.h"
michael@0 10 #include "nsString.h"
michael@0 11
michael@0 12 // This must be included last:
michael@0 13 #include "nsObjCExceptions.h"
michael@0 14
michael@0 15 // Find the path to the app with the given bundleIdentifier, if any.
michael@0 16 // Note that the OS will return the path to the newest binary, if there is more than one.
michael@0 17 // The determination of 'newest' is complex and beyond the scope of this comment.
michael@0 18
michael@0 19 NS_IMPL_ISUPPORTS(nsMacWebAppUtils, nsIMacWebAppUtils)
michael@0 20
michael@0 21 NS_IMETHODIMP nsMacWebAppUtils::PathForAppWithIdentifier(const nsAString& bundleIdentifier, nsAString& outPath) {
michael@0 22 NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
michael@0 23
michael@0 24 outPath.Truncate();
michael@0 25
michael@0 26 nsAutoreleasePool localPool;
michael@0 27
michael@0 28 //note that the result of this expression might be nil, meaning no matching app was found.
michael@0 29 NSString* temp = [[NSWorkspace sharedWorkspace] absolutePathForAppBundleWithIdentifier:
michael@0 30 [NSString stringWithCharacters:reinterpret_cast<const unichar*>(((nsString)bundleIdentifier).get())
michael@0 31 length:((nsString)bundleIdentifier).Length()]];
michael@0 32
michael@0 33 if (temp) {
michael@0 34 // Copy out the resultant absolute path into outPath if non-nil.
michael@0 35 nsCocoaUtils::GetStringForNSString(temp, outPath);
michael@0 36 }
michael@0 37
michael@0 38 return NS_OK;
michael@0 39
michael@0 40 NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
michael@0 41 }
michael@0 42
michael@0 43 NS_IMETHODIMP nsMacWebAppUtils::LaunchAppWithIdentifier(const nsAString& bundleIdentifier) {
michael@0 44 NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
michael@0 45
michael@0 46 nsAutoreleasePool localPool;
michael@0 47
michael@0 48 // Note this might return false, meaning the app wasnt launched for some reason.
michael@0 49 BOOL success = [[NSWorkspace sharedWorkspace] launchAppWithBundleIdentifier:
michael@0 50 [NSString stringWithCharacters:reinterpret_cast<const unichar*>(((nsString)bundleIdentifier).get())
michael@0 51 length:((nsString)bundleIdentifier).Length()]
michael@0 52 options: (NSWorkspaceLaunchOptions)0
michael@0 53 additionalEventParamDescriptor: nil
michael@0 54 launchIdentifier: NULL];
michael@0 55
michael@0 56 return success ? NS_OK : NS_ERROR_FAILURE;
michael@0 57
michael@0 58 NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
michael@0 59 }
michael@0 60
michael@0 61 NS_IMETHODIMP nsMacWebAppUtils::TrashApp(const nsAString& path, nsITrashAppCallback* aCallback) {
michael@0 62 NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
michael@0 63
michael@0 64 if (NS_WARN_IF(!aCallback)) {
michael@0 65 return NS_ERROR_INVALID_ARG;
michael@0 66 }
michael@0 67
michael@0 68 nsCOMPtr<nsITrashAppCallback> callback = aCallback;
michael@0 69
michael@0 70 NSString* tempString = [NSString stringWithCharacters:reinterpret_cast<const unichar*>(((nsString)path).get())
michael@0 71 length:path.Length()];
michael@0 72
michael@0 73 [[NSWorkspace sharedWorkspace] recycleURLs: [NSArray arrayWithObject:[NSURL fileURLWithPath:tempString]]
michael@0 74 completionHandler: ^(NSDictionary *newURLs, NSError *error) {
michael@0 75 nsresult rv = (error == nil) ? NS_OK : NS_ERROR_FAILURE;
michael@0 76 callback->TrashAppFinished(rv);
michael@0 77 }];
michael@0 78
michael@0 79 return NS_OK;
michael@0 80
michael@0 81 NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
michael@0 82 }

mercurial