Browse Source

Make sure the new instance inherite cwd on mac

Cheng Zhao 9 years ago
parent
commit
0d066de53e

+ 5 - 17
atom/browser/relauncher.cc

@@ -34,8 +34,7 @@ const CharType* kRelauncherArgSeparator = FILE_PATH_LITERAL("---");
 
 }  // namespace internal
 
-bool RelaunchApp(const base::FilePath& app,
-                 const StringVector& args) {
+bool RelaunchApp(const base::FilePath& app, const StringVector& args) {
   // Use the currently-running application's helper process. The automatic
   // update feature is careful to leave the currently-running version alone,
   // so this is safe even if the relaunch is the result of an update having
@@ -160,10 +159,9 @@ int RelauncherMain(const content::MainFunctionParams& main_parameters) {
   // Figure out what to execute, what arguments to pass it, and whether to
   // start it in the background.
   bool in_relaunch_args = false;
-  bool seen_relaunch_executable = false;
   StringType relaunch_executable;
   StringVector relauncher_args;
-  StringVector launch_args;
+  StringVector launch_argv;
   for (size_t argv_index = 2; argv_index < argv.size(); ++argv_index) {
     const StringType& arg(argv[argv_index]);
     if (!in_relaunch_args) {
@@ -173,26 +171,16 @@ int RelauncherMain(const content::MainFunctionParams& main_parameters) {
         relauncher_args.push_back(arg);
       }
     } else {
-      if (!seen_relaunch_executable) {
-        // The first argument after kRelauncherBackgroundArg is the path to
-        // the executable file or .app bundle directory. The Launch Services
-        // interface wants this separate from the rest of the arguments. In
-        // the relaunched process, this path will still be visible at argv[0].
-        relaunch_executable.assign(arg);
-        seen_relaunch_executable = true;
-      } else {
-        launch_args.push_back(arg);
-      }
+      launch_argv.push_back(arg);
     }
   }
 
-  if (!seen_relaunch_executable) {
+  if (launch_argv.empty()) {
     LOG(ERROR) << "nothing to relaunch";
     return 1;
   }
 
-  if (internal::LaunchProgram(relauncher_args, relaunch_executable,
-                              launch_args) != 0) {
+  if (internal::LaunchProgram(relauncher_args, launch_argv) != 0) {
     LOG(ERROR) << "failed to launch program";
     return 1;
   }

+ 1 - 3
atom/browser/relauncher.h

@@ -59,8 +59,7 @@ using StringVector = base::CommandLine::StringVector;
 // successfully. Returns true on success, although some failures can occur
 // after this function returns true if, for example, they occur within the
 // relauncher process. Returns false when the relaunch definitely failed.
-bool RelaunchApp(const base::FilePath& app,
-                 const StringVector& args);
+bool RelaunchApp(const base::FilePath& app, const StringVector& args);
 
 // Identical to RelaunchApp, but uses |helper| as the path to the relauncher
 // process, and allows additional arguments to be supplied to the relauncher
@@ -113,7 +112,6 @@ StringType GetWaitEventName(base::ProcessId pid);
 void RelauncherSynchronizeWithParent();
 
 int LaunchProgram(const StringVector& relauncher_args,
-                  const StringType& program,
                   const StringVector& argv);
 
 }  // namespace internal

+ 2 - 8
atom/browser/relauncher_linux.cc

@@ -53,14 +53,8 @@ void RelauncherSynchronizeWithParent() {
   HANDLE_EINTR(read(usr2_fd, &si, sizeof(si)));
 }
 
-int LaunchProgram(const std::vector<std::string>& relauncher_args,
-                  const std::string& program,
-                  const std::vector<std::string>& args) {
-  std::vector<std::string> argv;
-  argv.reserve(1 + args.size());
-  argv.push_back(program);
-  argv.insert(argv.end(), args.begin(), args.end());
-
+int LaunchProgram(const StringVector& relauncher_args,
+                  const StringVector& argv) {
   base::LaunchOptions options;
   options.allow_new_privs = true;
   options.new_process_group = true;  // detach

+ 7 - 64
atom/browser/relauncher_mac.cc

@@ -4,8 +4,6 @@
 
 #include "atom/browser/relauncher.h"
 
-#include <ApplicationServices/ApplicationServices.h>
-
 #include <sys/event.h>
 #include <sys/time.h>
 #include <sys/types.h>
@@ -13,9 +11,8 @@
 
 #include "base/files/file_util.h"
 #include "base/logging.h"
+#include "base/process/launch.h"
 #include "base/mac/mac_logging.h"
-#include "base/mac/mac_util.h"
-#include "base/mac/scoped_cftyperef.h"
 #include "base/posix/eintr_wrapper.h"
 #include "base/strings/sys_string_conversions.h"
 
@@ -23,16 +20,6 @@ namespace relauncher {
 
 namespace internal {
 
-namespace {
-
-// The beginning of the "process serial number" argument that Launch Services
-// sometimes inserts into command lines. A process serial number is only valid
-// for a single process, so any PSN arguments will be stripped from command
-// lines during relaunch to avoid confusion.
-const char kPSNArg[] = "-psn_";
-
-}  // namespace
-
 void RelauncherSynchronizeWithParent() {
   base::ScopedFD relauncher_sync_fd(kRelauncherSyncFD);
 
@@ -90,56 +77,12 @@ void RelauncherSynchronizeWithParent() {
   }
 }
 
-int LaunchProgram(const std::vector<std::string>& relauncher_args,
-                  const std::string& program,
-                  const std::vector<std::string>& argv) {
-  // The capacity for relaunch_args is 4 less than argc, because it
-  // won't contain the argv[0] of the relauncher process, the
-  // RelauncherTypeArg() at argv[1], kRelauncherArgSeparator, or the
-  // executable path of the process to be launched.
-  base::ScopedCFTypeRef<CFMutableArrayRef> relaunch_args(
-      CFArrayCreateMutable(NULL, argv.size() - 4, &kCFTypeArrayCallBacks));
-  if (!relaunch_args) {
-    LOG(ERROR) << "CFArrayCreateMutable";
-    return 1;
-  }
-
-  for (const std::string& arg : argv) {
-    // Strip any -psn_ arguments, as they apply to a specific process.
-    if (arg.compare(0, strlen(kPSNArg), kPSNArg) == 0)
-      continue;
-
-    base::ScopedCFTypeRef<CFStringRef> arg_cf(base::SysUTF8ToCFStringRef(arg));
-    if (!arg_cf) {
-      LOG(ERROR) << "base::SysUTF8ToCFStringRef failed for " << arg;
-      return 1;
-    }
-    CFArrayAppendValue(relaunch_args, arg_cf);
-  }
-
-  FSRef app_fsref;
-  if (!base::mac::FSRefFromPath(program, &app_fsref)) {
-    LOG(ERROR) << "base::mac::FSRefFromPath failed for " << program;
-    return 1;
-  }
-
-  LSApplicationParameters ls_parameters = {
-    0,  // version
-    kLSLaunchDefaults | kLSLaunchAndDisplayErrors | kLSLaunchNewInstance,
-    &app_fsref,
-    NULL,  // asyncLaunchRefCon
-    NULL,  // environment
-    relaunch_args,
-    NULL   // initialEvent
-  };
-
-  OSStatus status = LSOpenApplication(&ls_parameters, NULL);
-  if (status != noErr) {
-    OSSTATUS_LOG(ERROR, status) << "LSOpenApplication";
-    return 1;
-  }
-
-  return 0;
+int LaunchProgram(const StringVector& relauncher_args,
+                  const StringVector& argv) {
+  base::LaunchOptions options;
+  options.new_process_group = true;  // detach
+  base::Process process = base::LaunchProcess(argv, options);
+  return process.IsValid() ? 0 : 1;
 }
 
 }  // namespace internal

+ 3 - 4
atom/browser/relauncher_win.cc

@@ -65,11 +65,10 @@ void RelauncherSynchronizeWithParent() {
 }
 
 int LaunchProgram(const StringVector& relauncher_args,
-                  const StringType& program,
-                  const StringVector& args) {
+                  const StringVector& argv) {
   base::LaunchOptions options;
-  base::Process process = base::LaunchProcess(
-      program + L" " + base::JoinString(args, L" "), options);
+  base::Process process =
+      base::LaunchProcess(base::JoinString(args, L" "), options);
   return process.IsValid() ? 0 : 1;
 }