Bug 1518639: Move command line handing to nsRemoteService. r=jimm
authorDave Townsend <dtownsend@oxymoronical.com>
Thu, 31 Jan 2019 12:13:34 -0800
changeset 520640 03c9381974486d342d41f010a60eb436b546ec7a
parent 520639 b9d78d81a381350aa548ce7d6b640b93d57ebb7c
child 520641 30cd61daa60513ca4c31cd466ad6868fb7859317
push id10862
push userffxbld-merge
push dateMon, 11 Mar 2019 13:01:11 +0000
treeherdermozilla-beta@a2e7f5c935da [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersjimm
bugs1518639
milestone67.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1518639: Move command line handing to nsRemoteService. r=jimm Makes nsRemoteService handle the command line parsing, though this will end up being removed in a later patch. Differential Revision: https://phabricator.services.mozilla.com/D19071
toolkit/components/remote/nsRemoteService.cpp
toolkit/components/remote/nsRemoteService.h
toolkit/xre/nsAppRunner.cpp
--- a/toolkit/components/remote/nsRemoteService.cpp
+++ b/toolkit/components/remote/nsRemoteService.cpp
@@ -1,51 +1,59 @@
 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
 /* vim:expandtab:shiftwidth=2:tabstop=8:
  */
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
+#ifdef XP_UNIX
+#  include <sys/types.h>
+#  include <pwd.h>
+#endif
+
 #ifdef MOZ_WIDGET_GTK
 #  include "nsGTKRemoteServer.h"
 #  include "nsXRemoteClient.h"
 #  ifdef MOZ_ENABLE_DBUS
 #    include "nsDBusRemoteServer.h"
 #    include "nsDBusRemoteClient.h"
 #  endif
 #endif
 #include "nsRemoteService.h"
 
 #include "nsString.h"
 #include "nsServiceManagerUtils.h"
 #include "mozilla/ModuleUtils.h"
 #include "SpecialSystemDirectory.h"
+#include "mozilla/CmdLineAndEnvUtils.h"
 
 // Time to wait for the remoting service to start
 #define START_TIMEOUT_SEC 5
 #define START_SLEEP_MSEC 100
 
 using namespace mozilla;
 
 extern int gArgc;
 extern char** gArgv;
 
+using namespace mozilla;
+
 NS_IMPL_ISUPPORTS(nsRemoteService, nsIObserver)
 
-void nsRemoteService::LockStartup(nsCString& aProgram, const char* aProfile) {
+nsRemoteService::nsRemoteService(const char* aProgram) : mProgram(aProgram) {
+  ToLowerCase(mProgram);
+}
+
+void nsRemoteService::LockStartup() {
   nsCOMPtr<nsIFile> mutexDir;
   nsresult rv = GetSpecialSystemDirectory(OS_TemporaryDirectory,
                                           getter_AddRefs(mutexDir));
   if (NS_SUCCEEDED(rv)) {
-    nsAutoCString mutexPath(aProgram);
-    if (aProfile) {
-      mutexPath.Append(NS_LITERAL_CSTRING("_") + nsDependentCString(aProfile));
-    }
-    mutexDir->AppendNative(mutexPath);
+    mutexDir->AppendNative(mProgram);
 
     rv = mutexDir->Create(nsIFile::DIRECTORY_TYPE, 0700);
     if (NS_SUCCEEDED(rv) || rv == NS_ERROR_FILE_ALREADY_EXISTS) {
       mRemoteLockDir = mutexDir;
     }
   }
 
   if (mRemoteLockDir) {
@@ -67,19 +75,20 @@ void nsRemoteService::UnlockStartup() {
   mRemoteLock.Cleanup();
 
   if (mRemoteLockDir) {
     mRemoteLockDir->Remove(false);
     mRemoteLockDir = nullptr;
   }
 }
 
-RemoteResult nsRemoteService::StartClient(const char* aDesktopStartupID,
-                                          nsCString& program,
-                                          const char* profile) {
+RemoteResult nsRemoteService::StartClient(const char* aDesktopStartupID) {
+  const char* profile;
+  ArgResult ar = CheckArg(gArgc, gArgv, "p", &profile, CheckArgFlag::None);
+
   nsAutoPtr<nsRemoteClient> client;
 
 #ifdef MOZ_WIDGET_GTK
   bool useX11Remote = GDK_IS_X11_DISPLAY(gdk_display_get_default());
 
 #  if defined(MOZ_ENABLE_DBUS)
   if (!useX11Remote) {
     client = new nsDBusRemoteClient();
@@ -89,17 +98,17 @@ RemoteResult nsRemoteService::StartClien
     client = new nsXRemoteClient();
   }
 
   nsresult rv = client ? client->Init() : NS_ERROR_FAILURE;
   if (NS_FAILED(rv)) return REMOTE_NOT_FOUND;
 
   nsCString response;
   bool success = false;
-  rv = client->SendCommandLine(program.get(), profile, gArgc, gArgv,
+  rv = client->SendCommandLine(mProgram.get(), profile, gArgc, gArgv,
                                aDesktopStartupID, getter_Copies(response),
                                &success);
   // did the command fail?
   if (!success) return REMOTE_NOT_FOUND;
 
   // The "command not parseable" error is returned when the
   // nsICommandLineHandler throws a NS_ERROR_ABORT.
   if (response.EqualsLiteral("500 command not parseable"))
@@ -108,18 +117,17 @@ RemoteResult nsRemoteService::StartClien
   if (NS_FAILED(rv)) return REMOTE_NOT_FOUND;
 
   return REMOTE_FOUND;
 #else
   return REMOTE_NOT_FOUND;
 #endif
 }
 
-void nsRemoteService::StartupServer(const char* aAppName,
-                                    const char* aProfileName) {
+void nsRemoteService::StartupServer(const char* aProfile) {
   if (mRemoteServer) {
     return;
   }
 
 #ifdef MOZ_WIDGET_GTK
   bool useX11Remote = GDK_IS_X11_DISPLAY(gdk_display_get_default());
 
 #  if defined(MOZ_ENABLE_DBUS)
@@ -127,17 +135,17 @@ void nsRemoteService::StartupServer(cons
     mRemoteServer = MakeUnique<nsDBusRemoteServer>();
   }
 #  endif
   if (useX11Remote) {
     mRemoteServer = MakeUnique<nsGTKRemoteServer>();
   }
 #endif
 
-  nsresult rv = mRemoteServer->Startup(aAppName, aProfileName);
+  nsresult rv = mRemoteServer->Startup(mProgram.get(), aProfile);
 
   if (NS_FAILED(rv)) {
     mRemoteServer = nullptr;
     return;
   }
 
   nsCOMPtr<nsIObserverService> obs(
       do_GetService("@mozilla.org/observer-service;1"));
--- a/toolkit/components/remote/nsRemoteService.h
+++ b/toolkit/components/remote/nsRemoteService.h
@@ -23,27 +23,27 @@ enum RemoteResult {
 };
 
 class nsRemoteService final : public nsIObserver {
  public:
   // We will be a static singleton, so don't use the ordinary methods.
   NS_DECL_ISUPPORTS
   NS_DECL_NSIOBSERVER
 
-  nsRemoteService() = default;
+  explicit nsRemoteService(const char* aProgram);
 
-  void LockStartup(nsCString& aProgram, const char* aProfile);
+  void LockStartup();
   void UnlockStartup();
 
-  RemoteResult StartClient(const char* aDesktopStartupID, nsCString& program,
-                           const char* profile);
-  void StartupServer(const char* aAppName, const char* aProfileName);
+  RemoteResult StartClient(const char* aDesktopStartupID);
+  void StartupServer(const char* aProfile);
   void ShutdownServer();
 
  private:
   ~nsRemoteService();
 
   mozilla::UniquePtr<nsRemoteServer> mRemoteServer;
   nsProfileLock mRemoteLock;
   nsCOMPtr<nsIFile> mRemoteLockDir;
+  nsCString mProgram;
 };
 
 #endif  // __nsRemoteService_h__
--- a/toolkit/xre/nsAppRunner.cpp
+++ b/toolkit/xre/nsAppRunner.cpp
@@ -3402,29 +3402,36 @@ int XREMain::XRE_mainInit(bool* aExitFla
   if (ar == ARG_BAD) {
     PR_fprintf(PR_STDERR,
                "Error: argument --no-remote is invalid when argument --osint "
                "is specified\n");
     return 1;
   }
   if (ar == ARG_FOUND) {
     SaveToEnv("MOZ_NO_REMOTE=1");
-  }
-
+#ifdef MOZ_WIDGET_GTK
+    mDisableRemote = true;
+  } else if (EnvHasValue("MOZ_NO_REMOTE")) {
+    mDisableRemote = true;
+#endif
+  }
+
+#ifdef MOZ_WIDGET_GTK
   ar = CheckArg("new-instance", nullptr,
                 CheckArgFlag::CheckOSInt | CheckArgFlag::RemoveArg);
   if (ar == ARG_BAD) {
     PR_fprintf(PR_STDERR,
                "Error: argument --new-instance is invalid when argument "
                "--osint is specified\n");
     return 1;
   }
-  if (ar == ARG_FOUND) {
-    SaveToEnv("MOZ_NEW_INSTANCE=1");
-  }
+  if (ar == ARG_FOUND || EnvHasValue("MOZ_NEW_INSTANCE")) {
+    mDisableRemote = true;
+  }
+#endif
 
   ar = CheckArg("offline", nullptr,
                 CheckArgFlag::CheckOSInt | CheckArgFlag::RemoveArg);
   if (ar == ARG_BAD) {
     PR_fprintf(PR_STDERR,
                "Error: argument --offline is invalid when argument --osint is "
                "specified\n");
     return 1;
@@ -3887,48 +3894,27 @@ int XREMain::XRE_mainStartup(bool* aExit
     }
 #  endif
   } else {
     mDisableRemote = true;
   }
 #endif
 #if defined(MOZ_WIDGET_GTK)
   // handle --remote now that xpcom is fired up
-  bool newInstance;
-  {
-    char* e = PR_GetEnv("MOZ_NO_REMOTE");
-    mDisableRemote = (mDisableRemote || (e && *e));
-    if (mDisableRemote) {
-      newInstance = true;
-    } else {
-      e = PR_GetEnv("MOZ_NEW_INSTANCE");
-      newInstance = (e && *e);
-    }
-  }
-
-  if (!newInstance) {
-    mRemoteService = new nsRemoteService();
+  if (!mDisableRemote) {
+    mRemoteService = new nsRemoteService(gAppData->remotingName);
     if (mRemoteService) {
-      nsAutoCString program(gAppData->remotingName);
-      ToLowerCase(program);
-
-      const char* profile = nullptr;
-      // It doesn't matter if this succeeds or fails. A failure will be handled
-      // later.
-      CheckArg("p", &profile, CheckArgFlag::None);
-
-      mRemoteService->LockStartup(program, profile);
+      mRemoteService->LockStartup();
 
       // Try to remote the entire command line. If this fails, start up
       // normally.
       const char* desktopStartupIDPtr =
           mDesktopStartupID.IsEmpty() ? nullptr : mDesktopStartupID.get();
 
-      RemoteResult rr =
-          mRemoteService->StartClient(desktopStartupIDPtr, program, profile);
+      RemoteResult rr = mRemoteService->StartClient(desktopStartupIDPtr);
       if (rr == REMOTE_FOUND) {
         *aExitFlag = true;
         return 0;
       }
       if (rr == REMOTE_ARG_BAD) {
         return 1;
       }
     }
@@ -4554,17 +4540,17 @@ nsresult XREMain::XRE_mainRun() {
     appStartup->GetShuttingDown(&mShuttingDown);
   }
 
   if (!mShuttingDown) {
 #if defined(MOZ_WIDGET_GTK)
     // if we have X remote support, start listening for requests on the
     // proxy window.
     if (mRemoteService) {
-      mRemoteService->StartupServer(mAppData->remotingName, mProfileName.get());
+      mRemoteService->StartupServer(mProfileName.get());
       mRemoteService->UnlockStartup();
     }
 #endif /* MOZ_WIDGET_GTK */
 
     mNativeApp->Enable();
   }
 
 #ifdef MOZ_INSTRUMENT_EVENT_LOOP