diff --git a/dom/ipc/ContentParent.cpp b/dom/ipc/ContentParent.cpp index 7cc676bb53a3..cdf579754728 100644 --- a/dom/ipc/ContentParent.cpp +++ b/dom/ipc/ContentParent.cpp @@ -2056,6 +2056,7 @@ void ContentParent::LaunchSubprocessInternal( earlyReject(); return; } + prefSerializer.AddSharedPrefCmdLineArgs(*mSubprocess, extraArgs); // Register ContentParent as an observer for changes to any pref // whose prefix matches the empty string, i.e. all of them. The @@ -2063,43 +2064,6 @@ void ContentParent::LaunchSubprocessInternal( // happen during async launch. Preferences::AddStrongObserver(this, ""); - // Formats a pointer or pointer-sized-integer as a string suitable for passing - // in an arguments list. - auto formatPtrArg = [](auto arg) { - return nsPrintfCString("%zu", uintptr_t(arg)); - }; - -#if defined(XP_WIN) - // Record the handle as to-be-shared, and pass it via a command flag. This - // works because Windows handles are system-wide. - HANDLE prefsHandle = prefSerializer.GetSharedMemoryHandle(); - mSubprocess->AddHandleToShare(prefsHandle); - mSubprocess->AddHandleToShare(prefSerializer.GetPrefMapHandle().get()); - extraArgs.push_back("-prefsHandle"); - extraArgs.push_back(formatPtrArg(prefsHandle).get()); - extraArgs.push_back("-prefMapHandle"); - extraArgs.push_back( - formatPtrArg(prefSerializer.GetPrefMapHandle().get()).get()); -#else - // In contrast, Unix fds are per-process. So remap the fd to a fixed one that - // will be used in the child. - // XXX: bug 1440207 is about improving how fixed fds are used. - // - // Note: on Android, AddFdToRemap() sets up the fd to be passed via a Parcel, - // and the fixed fd isn't used. However, we still need to mark it for - // remapping so it doesn't get closed in the child. - mSubprocess->AddFdToRemap(prefSerializer.GetSharedMemoryHandle().fd, - kPrefsFileDescriptor); - mSubprocess->AddFdToRemap(prefSerializer.GetPrefMapHandle().get(), - kPrefMapFileDescriptor); -#endif - - // Pass the lengths via command line flags. - extraArgs.push_back("-prefsLen"); - extraArgs.push_back(formatPtrArg(prefSerializer.GetPrefLength()).get()); - extraArgs.push_back("-prefMapSize"); - extraArgs.push_back(formatPtrArg(prefSerializer.GetPrefMapSize()).get()); - if (gSafeMode) { extraArgs.push_back("-safeMode"); } diff --git a/ipc/glue/ProcessUtils.h b/ipc/glue/ProcessUtils.h index c05358223779..8119f9ac40dc 100644 --- a/ipc/glue/ProcessUtils.h +++ b/ipc/glue/ProcessUtils.h @@ -13,6 +13,8 @@ namespace mozilla { namespace ipc { +class GeckoChildProcessHost; + // You probably should call ContentChild::SetProcessName instead of calling // this directly. void SetThisProcessName(const char* aName); @@ -37,6 +39,9 @@ class SharedPreferenceSerializer final { size_t GetPrefMapSize() const { return mPrefMapSize; } + void AddSharedPrefCmdLineArgs(GeckoChildProcessHost& procHost, + std::vector& aExtraOpts) const; + private: DISALLOW_COPY_AND_ASSIGN(SharedPreferenceSerializer); size_t mPrefMapSize; diff --git a/ipc/glue/ProcessUtils_common.cpp b/ipc/glue/ProcessUtils_common.cpp index e2f3de396d3b..bba91288194c 100644 --- a/ipc/glue/ProcessUtils_common.cpp +++ b/ipc/glue/ProcessUtils_common.cpp @@ -7,6 +7,7 @@ #include "ProcessUtils.h" #include "mozilla/Preferences.h" +#include "mozilla/ipc/GeckoChildProcessHost.h" namespace mozilla { namespace ipc { @@ -51,6 +52,46 @@ bool SharedPreferenceSerializer::SerializeToSharedMemory() { return true; } +void SharedPreferenceSerializer::AddSharedPrefCmdLineArgs( + mozilla::ipc::GeckoChildProcessHost& procHost, + std::vector& aExtraOpts) const { + // Formats a pointer or pointer-sized-integer as a string suitable for passing + // in an arguments list. + auto formatPtrArg = [](auto arg) { + return nsPrintfCString("%zu", uintptr_t(arg)); + }; + +#if defined(XP_WIN) + // Record the handle as to-be-shared, and pass it via a command flag. This + // works because Windows handles are system-wide. + HANDLE prefsHandle = GetSharedMemoryHandle(); + procHost.AddHandleToShare(prefsHandle); + procHost.AddHandleToShare(GetPrefMapHandle().get()); + aExtraOpts.push_back("-prefsHandle"); + aExtraOpts.push_back(formatPtrArg(prefsHandle).get()); + aExtraOpts.push_back("-prefMapHandle"); + aExtraOpts.push_back( + formatPtrArg(GetPrefMapHandle().get()).get()); +#else + // In contrast, Unix fds are per-process. So remap the fd to a fixed one that + // will be used in the child. + // XXX: bug 1440207 is about improving how fixed fds are used. + // + // Note: on Android, AddFdToRemap() sets up the fd to be passed via a Parcel, + // and the fixed fd isn't used. However, we still need to mark it for + // remapping so it doesn't get closed in the child. + procHost.AddFdToRemap(GetSharedMemoryHandle().fd, kPrefsFileDescriptor); + procHost.AddFdToRemap(GetPrefMapHandle().get(), kPrefMapFileDescriptor); +#endif + + // Pass the lengths via command line flags. + aExtraOpts.push_back("-prefsLen"); + aExtraOpts.push_back(formatPtrArg(GetPrefLength()).get()); + aExtraOpts.push_back("-prefMapSize"); + aExtraOpts.push_back(formatPtrArg(GetPrefMapSize()).get()); +} + + #ifdef ANDROID static int gPrefsFd = -1; static int gPrefMapFd = -1; diff --git a/netwerk/ipc/SocketProcessHost.cpp b/netwerk/ipc/SocketProcessHost.cpp index 58fa85bc7149..448425c0fd2a 100644 --- a/netwerk/ipc/SocketProcessHost.cpp +++ b/netwerk/ipc/SocketProcessHost.cpp @@ -111,41 +111,7 @@ bool SocketProcessHost::Launch() { if (!prefSerializer.SerializeToSharedMemory()) { return false; } - - // Formats a pointer or pointer-sized-integer as a string suitable for passing - // in an arguments list. - auto formatPtrArg = [](auto arg) { - return nsPrintfCString("%zu", uintptr_t(arg)); - }; - -#if defined(XP_WIN) - // Record the handle as to-be-shared, and pass it via a command flag. This - // works because Windows handles are system-wide. - HANDLE prefsHandle = prefSerializer.GetSharedMemoryHandle(); - AddHandleToShare(prefsHandle); - AddHandleToShare(prefSerializer.GetPrefMapHandle().get()); - extraArgs.push_back("-prefsHandle"); - extraArgs.push_back(formatPtrArg(prefsHandle).get()); - extraArgs.push_back("-prefMapHandle"); - extraArgs.push_back( - formatPtrArg(prefSerializer.GetPrefMapHandle().get()).get()); -#else - // In contrast, Unix fds are per-process. So remap the fd to a fixed one that - // will be used in the child. - // XXX: bug 1440207 is about improving how fixed fds are used. - // - // Note: on Android, AddFdToRemap() sets up the fd to be passed via a Parcel, - // and the fixed fd isn't used. However, we still need to mark it for - // remapping so it doesn't get closed in the child. - AddFdToRemap(prefSerializer.GetSharedMemoryHandle().fd, kPrefsFileDescriptor); - AddFdToRemap(prefSerializer.GetPrefMapHandle().get(), kPrefMapFileDescriptor); -#endif - - // Pass the lengths via command line flags. - extraArgs.push_back("-prefsLen"); - extraArgs.push_back(formatPtrArg(prefSerializer.GetPrefLength()).get()); - extraArgs.push_back("-prefMapSize"); - extraArgs.push_back(formatPtrArg(prefSerializer.GetPrefMapSize()).get()); + prefSerializer.AddSharedPrefCmdLineArgs(*this, extraArgs); mLaunchPhase = LaunchPhase::Waiting; if (!GeckoChildProcessHost::LaunchAndWaitForProcessHandle(extraArgs)) {