Bug 394984: Improve initializations of NSAutoReleasePools. r=mstange
authorStephen A Pohl <spohl.mozilla.bugs@gmail.com>
Fri, 29 Apr 2016 16:43:39 -0400
changeset 491517 d154bc80a284d6cbb34a359b2b56079f7f04271f
parent 491516 4734d09a05a0c431fba09fb0c0d6e420c4d3fb54
child 491518 9291c3b0effd78f29db8c03f8a5a687ebb423ce9
push id47343
push userbmo:dothayer@mozilla.com
push dateWed, 01 Mar 2017 22:58:58 +0000
reviewersmstange
bugs394984
milestone49.0a1
Bug 394984: Improve initializations of NSAutoReleasePools. r=mstange
toolkit/mozapps/update/updater/launchchild_osx.mm
--- a/toolkit/mozapps/update/updater/launchchild_osx.mm
+++ b/toolkit/mozapps/update/updater/launchchild_osx.mm
@@ -8,67 +8,79 @@
 #include <CoreServices/CoreServices.h>
 #include <crt_externs.h>
 #include <stdlib.h>
 #include <stdio.h>
 #include <spawn.h>
 #include <SystemConfiguration/SystemConfiguration.h>
 #include "readstrings.h"
 
+class MacAutoreleasePool {
+public:
+  MacAutoreleasePool()
+  {
+    mPool = [[NSAutoreleasePool alloc] init];
+  }
+  ~MacAutoreleasePool()
+  {
+    [mPool release];
+  }
+
+private:
+  NSAutoreleasePool* mPool;
+};
+
 void LaunchChild(int argc, const char** argv)
 {
-  NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
+  MacAutoreleasePool pool;
+
   @try {
     NSString* launchPath = [NSString stringWithUTF8String:argv[0]];
     NSMutableArray* arguments = [NSMutableArray arrayWithCapacity:argc];
     for (int i = 1; i < argc; i++) {
       [arguments addObject:[NSString stringWithUTF8String:argv[i]]];
     }
     [NSTask launchedTaskWithLaunchPath:launchPath
                              arguments:arguments];
   } @catch (NSException* e) {
     // Ignore any exception.
   }
-  [pool release];
 }
 
 void
 LaunchMacPostProcess(const char* aAppBundle)
 {
+  MacAutoreleasePool pool;
+
   // Launch helper to perform post processing for the update; this is the Mac
   // analogue of LaunchWinPostProcess (PostUpdateWin).
-  NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
-
   NSString* iniPath = [NSString stringWithUTF8String:aAppBundle];
   iniPath =
     [iniPath stringByAppendingPathComponent:@"Contents/Resources/updater.ini"];
 
   NSFileManager* fileManager = [NSFileManager defaultManager];
   if (![fileManager fileExistsAtPath:iniPath]) {
     // the file does not exist; there is nothing to run
-    [pool release];
     return;
   }
 
   int readResult;
   char values[2][MAX_TEXT_LEN];
   readResult = ReadStrings([iniPath UTF8String],
                            "ExeRelPath\0ExeArg\0",
                            2,
                            values,
                            "PostUpdateMac");
   if (readResult) {
-    [pool release];
     return;
   }
 
   NSString *exeRelPath = [NSString stringWithUTF8String:values[0]];
   NSString *exeArg = [NSString stringWithUTF8String:values[1]];
   if (!exeArg || !exeRelPath) {
-    [pool release];
     return;
   }
 
   NSString* exeFullPath = [NSString stringWithUTF8String:aAppBundle];
   exeFullPath = [exeFullPath stringByAppendingPathComponent:exeRelPath];
 
   char optVals[1][MAX_TEXT_LEN];
   readResult = ReadStrings([iniPath UTF8String],
@@ -84,23 +96,21 @@ LaunchMacPostProcess(const char* aAppBun
   if (!readResult) {
     NSString *exeAsync = [NSString stringWithUTF8String:optVals[0]];
     if ([exeAsync isEqualToString:@"false"]) {
       [task waitUntilExit];
     }
   }
   // ignore the return value of the task, there's nothing we can do with it
   [task release];
-
-  [pool release];
 }
 
 void CleanupElevatedMacUpdate(bool aFailureOccurred)
 {
-  NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
+  MacAutoreleasePool pool;
 
   id updateServer = nil;
   @try {
     updateServer = (id)[NSConnection
       rootProxyForConnectionWithRegisteredName:
         @"org.mozilla.updater.server"
       host:nil
       usingNameServer:[NSSocketPortNameServer sharedInstance]];
@@ -122,23 +132,23 @@ void CleanupElevatedMacUpdate(bool aFail
   [manager removeItemAtPath:@"/Library/LaunchDaemons/org.mozilla.updater.plist"
                       error:nil];
   const char* launchctlArgs[] = {"/bin/launchctl",
                                  "remove",
                                  "org.mozilla.updater"};
   // The following call will terminate the current process due to the "remove"
   // argument in launchctlArgs.
   LaunchChild(3, launchctlArgs);
-
-  [pool release];
 }
 
 // Note: Caller is responsible for freeing argv.
 bool ObtainUpdaterArguments(int* argc, char*** argv)
 {
+  MacAutoreleasePool pool;
+
   id updateServer = nil;
   @try {
     updateServer = (id)[NSConnection
       rootProxyForConnectionWithRegisteredName:
         @"org.mozilla.updater.server"
       host:nil
       usingNameServer:[NSSocketPortNameServer sharedInstance]];
     if (!updateServer ||
@@ -247,85 +257,82 @@ bool ObtainUpdaterArguments(int* argc, c
 {
   return mShouldKeepRunning;
 }
 
 @end
 
 bool ServeElevatedUpdate(int argc, const char** argv)
 {
+  MacAutoreleasePool pool;
+
   NSMutableArray* updaterArguments = [NSMutableArray arrayWithCapacity:argc];
   for (int i = 0; i < argc; i++) {
     [updaterArguments addObject:[NSString stringWithUTF8String:argv[i]]];
   }
 
   ElevatedUpdateServer* updater =
     [[ElevatedUpdateServer alloc] initWithArgs:[updaterArguments copy]];
   bool didSucceed = [updater runServer];
 
   [updater release];
   return didSucceed;
 }
 
 bool IsOwnedByGroupAdmin(const char* aAppBundle)
 {
-  NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
+  MacAutoreleasePool pool;
 
   NSString* appDir = [NSString stringWithUTF8String:aAppBundle];
   NSFileManager* fileManager = [NSFileManager defaultManager];
 
   NSDictionary* attributes = [fileManager attributesOfItemAtPath:appDir
                                                            error:nil];
   bool isOwnedByAdmin = false;
   if (attributes &&
       [[attributes valueForKey:NSFileGroupOwnerAccountID] intValue] == 80) {
     isOwnedByAdmin = true;
   }
-
-  [pool drain];
   return isOwnedByAdmin;
 }
 
 void SetGroupOwnershipAndPermissions(const char* aAppBundle)
 {
-  NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
+  MacAutoreleasePool pool;
 
   NSString* appDir = [NSString stringWithUTF8String:aAppBundle];
   NSFileManager* fileManager = [NSFileManager defaultManager];
   NSError* error = nil;
   NSArray* paths =
     [fileManager subpathsOfDirectoryAtPath:appDir
                                      error:&error];
   if (error) {
-    [pool drain];
     return;
   }
 
   // Set group ownership of Firefox.app to 80 ("admin") and permissions to
   // 0775.
   if (![fileManager setAttributes:@{ NSFileGroupOwnerAccountID: @(80),
                                      NSFilePosixPermissions: @(0775) }
                      ofItemAtPath:appDir
                             error:&error] || error) {
-    [pool drain];
     return;
   }
 
   NSArray* permKeys = [NSArray arrayWithObjects:NSFileGroupOwnerAccountID,
                                                 NSFilePosixPermissions,
                                                 nil];
   // For all descendants of Firefox.app, set group ownership to 80 ("admin") and
   // ensure write permission for the group.
   for (NSString* currPath in paths) {
     NSString* child = [appDir stringByAppendingPathComponent:currPath];
     NSDictionary* oldAttributes =
       [fileManager attributesOfItemAtPath:child
                                     error:&error];
     if (error) {
-      [pool drain];
       return;
     }
     // Skip symlinks, since they could be pointing to files outside of the .app
     // bundle.
     if ([oldAttributes fileType] == NSFileTypeSymbolicLink) {
       continue;
     }
     NSNumber* oldPerms =
@@ -335,15 +342,12 @@ void SetGroupOwnershipAndPermissions(con
         [NSNumber numberWithUnsignedLong:80],
         [NSNumber numberWithUnsignedLong:[oldPerms shortValue] | 020],
         nil];
     NSDictionary* attributes = [NSDictionary dictionaryWithObjects:permObjects
                                                            forKeys:permKeys];
     if (![fileManager setAttributes:attributes
                        ofItemAtPath:child
                               error:&error] || error) {
-      [pool drain];
       return;
     }
   }
-
-  [pool drain];
 }