2012-05-21 15:12:37 +04:00
|
|
|
/* 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/. */
|
2005-08-29 23:27:16 +04:00
|
|
|
|
2012-12-15 03:58:45 +04:00
|
|
|
#include "mozilla/DebugOnly.h"
|
|
|
|
|
2011-01-05 16:48:21 +03:00
|
|
|
#if defined(MOZ_WIDGET_QT)
|
|
|
|
#include "nsQAppInstance.h"
|
|
|
|
#endif
|
|
|
|
|
2009-08-29 03:16:19 +04:00
|
|
|
#include "base/basictypes.h"
|
|
|
|
|
2005-08-29 23:27:16 +04:00
|
|
|
#include "nsXULAppAPI.h"
|
2005-09-01 17:21:55 +04:00
|
|
|
|
|
|
|
#include <stdlib.h>
|
2012-06-16 10:06:10 +04:00
|
|
|
#if defined(MOZ_WIDGET_GTK)
|
2009-10-19 20:28:42 +04:00
|
|
|
#include <glib.h>
|
|
|
|
#endif
|
2005-09-01 17:21:55 +04:00
|
|
|
|
2009-06-30 23:21:25 +04:00
|
|
|
#include "prenv.h"
|
|
|
|
|
|
|
|
#include "nsIAppShell.h"
|
2005-09-01 17:21:55 +04:00
|
|
|
#include "nsIAppStartupNotifier.h"
|
|
|
|
#include "nsIDirectoryService.h"
|
2012-06-06 06:08:30 +04:00
|
|
|
#include "nsIFile.h"
|
2005-09-01 17:21:55 +04:00
|
|
|
#include "nsIToolkitChromeRegistry.h"
|
2009-06-30 23:21:25 +04:00
|
|
|
#include "nsIToolkitProfile.h"
|
2005-09-01 17:21:55 +04:00
|
|
|
|
2010-02-04 01:17:09 +03:00
|
|
|
#ifdef XP_WIN
|
|
|
|
#include <process.h>
|
2014-05-18 07:16:51 +04:00
|
|
|
#include "mozilla/ipc/WindowsMessageLoop.h"
|
2010-02-04 01:17:09 +03:00
|
|
|
#endif
|
|
|
|
|
2005-09-01 17:21:55 +04:00
|
|
|
#include "nsAppDirectoryServiceDefs.h"
|
2006-01-12 18:29:10 +03:00
|
|
|
#include "nsAppRunner.h"
|
2009-07-11 10:33:10 +04:00
|
|
|
#include "nsAutoRef.h"
|
2005-09-01 17:21:55 +04:00
|
|
|
#include "nsDirectoryServiceDefs.h"
|
2010-01-13 00:14:38 +03:00
|
|
|
#include "nsExceptionHandler.h"
|
2005-09-01 17:21:55 +04:00
|
|
|
#include "nsString.h"
|
2009-06-30 23:21:25 +04:00
|
|
|
#include "nsThreadUtils.h"
|
2010-12-03 11:24:17 +03:00
|
|
|
#include "nsJSUtils.h"
|
2009-06-30 23:21:25 +04:00
|
|
|
#include "nsWidgetsCID.h"
|
2006-01-12 18:29:10 +03:00
|
|
|
#include "nsXREDirProvider.h"
|
2009-06-30 23:21:25 +04:00
|
|
|
|
2010-08-13 08:31:59 +04:00
|
|
|
#include "mozilla/Omnijar.h"
|
2010-09-10 21:09:30 +04:00
|
|
|
#if defined(XP_MACOSX)
|
2011-10-27 06:57:26 +04:00
|
|
|
#include "nsVersionComparator.h"
|
2010-09-10 21:09:30 +04:00
|
|
|
#include "chrome/common/mach_ipc_mac.h"
|
|
|
|
#endif
|
2010-02-25 01:25:16 +03:00
|
|
|
#include "nsX11ErrorHandler.h"
|
2014-06-16 12:54:00 +04:00
|
|
|
#include "nsGDKErrorHandler.h"
|
2009-07-20 23:04:52 +04:00
|
|
|
#include "base/at_exit.h"
|
|
|
|
#include "base/command_line.h"
|
|
|
|
#include "base/message_loop.h"
|
2009-10-28 00:52:37 +03:00
|
|
|
#include "base/process_util.h"
|
2009-07-20 23:04:52 +04:00
|
|
|
#include "chrome/common/child_process.h"
|
|
|
|
|
2010-02-04 01:17:09 +03:00
|
|
|
#include "mozilla/ipc/BrowserProcessSubThread.h"
|
2010-05-11 08:18:00 +04:00
|
|
|
#include "mozilla/ipc/GeckoChildProcessHost.h"
|
|
|
|
#include "mozilla/ipc/IOThreadChild.h"
|
|
|
|
#include "mozilla/ipc/ProcessChild.h"
|
2009-06-30 23:21:25 +04:00
|
|
|
#include "ScopedXREEmbed.h"
|
|
|
|
|
2010-05-11 08:18:00 +04:00
|
|
|
#include "mozilla/plugins/PluginProcessChild.h"
|
2010-07-19 22:33:33 +04:00
|
|
|
#include "mozilla/dom/ContentProcess.h"
|
|
|
|
#include "mozilla/dom/ContentParent.h"
|
|
|
|
#include "mozilla/dom/ContentChild.h"
|
2009-06-30 23:21:25 +04:00
|
|
|
|
2009-07-11 10:33:10 +04:00
|
|
|
#include "mozilla/ipc/TestShellParent.h"
|
|
|
|
#include "mozilla/ipc/XPCShellEnvironment.h"
|
2015-02-18 04:09:32 +03:00
|
|
|
#include "mozilla/WindowsDllBlocklist.h"
|
2009-07-11 10:33:10 +04:00
|
|
|
|
2014-05-18 07:05:46 +04:00
|
|
|
#include "GMPProcessChild.h"
|
2014-11-14 11:26:24 +03:00
|
|
|
#include "GMPLoader.h"
|
2016-06-11 05:27:24 +03:00
|
|
|
#include "mozilla/gfx/GPUProcessImpl.h"
|
2014-05-18 07:05:46 +04:00
|
|
|
|
2013-03-18 18:25:50 +04:00
|
|
|
#include "GeckoProfiler.h"
|
2012-07-27 22:57:48 +04:00
|
|
|
|
2016-06-08 18:46:24 +03:00
|
|
|
#include "mozilla/Telemetry.h"
|
2015-05-27 20:59:13 +03:00
|
|
|
|
2014-11-29 20:12:18 +03:00
|
|
|
#if defined(MOZ_SANDBOX) && defined(XP_WIN)
|
2016-05-15 18:35:22 +03:00
|
|
|
#include "mozilla/sandboxTarget.h"
|
2014-11-29 20:12:18 +03:00
|
|
|
#include "mozilla/sandboxing/loggingCallbacks.h"
|
2014-06-11 18:32:37 +04:00
|
|
|
#endif
|
|
|
|
|
2009-10-06 22:02:26 +04:00
|
|
|
#ifdef MOZ_IPDL_TESTS
|
|
|
|
#include "mozilla/_ipdltest/IPDLUnitTests.h"
|
2010-05-11 08:18:00 +04:00
|
|
|
#include "mozilla/_ipdltest/IPDLUnitTestProcessChild.h"
|
2009-10-06 22:02:26 +04:00
|
|
|
|
2010-05-11 08:18:00 +04:00
|
|
|
using mozilla::_ipdltest::IPDLUnitTestProcessChild;
|
2009-10-06 22:02:26 +04:00
|
|
|
#endif // ifdef MOZ_IPDL_TESTS
|
|
|
|
|
2014-07-26 04:52:00 +04:00
|
|
|
#ifdef MOZ_B2G_LOADER
|
|
|
|
#include "nsLocalFile.h"
|
|
|
|
#include "nsXREAppData.h"
|
|
|
|
#endif
|
|
|
|
|
2016-05-21 05:57:31 +03:00
|
|
|
#ifdef MOZ_JPROF
|
|
|
|
#include "jprof.h"
|
|
|
|
#endif
|
|
|
|
|
2011-10-11 09:50:08 +04:00
|
|
|
using namespace mozilla;
|
|
|
|
|
2009-09-03 04:18:27 +04:00
|
|
|
using mozilla::ipc::BrowserProcessSubThread;
|
2010-05-11 08:18:00 +04:00
|
|
|
using mozilla::ipc::GeckoChildProcessHost;
|
|
|
|
using mozilla::ipc::IOThreadChild;
|
|
|
|
using mozilla::ipc::ProcessChild;
|
2009-06-30 23:21:25 +04:00
|
|
|
using mozilla::ipc::ScopedXREEmbed;
|
|
|
|
|
2010-05-11 08:18:00 +04:00
|
|
|
using mozilla::plugins::PluginProcessChild;
|
2010-07-19 22:33:33 +04:00
|
|
|
using mozilla::dom::ContentProcess;
|
|
|
|
using mozilla::dom::ContentParent;
|
|
|
|
using mozilla::dom::ContentChild;
|
2009-11-06 23:43:39 +03:00
|
|
|
|
2014-11-14 11:26:24 +03:00
|
|
|
using mozilla::gmp::GMPLoader;
|
|
|
|
using mozilla::gmp::CreateGMPLoader;
|
|
|
|
using mozilla::gmp::GMPProcessChild;
|
|
|
|
|
2009-07-11 10:33:10 +04:00
|
|
|
using mozilla::ipc::TestShellParent;
|
2009-08-29 03:16:19 +04:00
|
|
|
using mozilla::ipc::TestShellCommandParent;
|
2009-07-11 10:33:10 +04:00
|
|
|
using mozilla::ipc::XPCShellEnvironment;
|
2009-12-15 01:13:00 +03:00
|
|
|
|
|
|
|
using mozilla::startup::sChildProcessType;
|
2009-06-30 23:21:25 +04:00
|
|
|
|
|
|
|
static NS_DEFINE_CID(kAppShellCID, NS_APPSHELL_CID);
|
2005-08-29 23:27:16 +04:00
|
|
|
|
2010-05-15 04:25:10 +04:00
|
|
|
#ifdef XP_WIN
|
2013-12-03 19:07:22 +04:00
|
|
|
static const wchar_t kShellLibraryName[] = L"shell32.dll";
|
2010-05-15 04:25:10 +04:00
|
|
|
#endif
|
|
|
|
|
2006-01-12 18:29:10 +03:00
|
|
|
nsresult
|
2012-06-06 06:08:30 +04:00
|
|
|
XRE_LockProfileDirectory(nsIFile* aDirectory,
|
2006-01-12 18:29:10 +03:00
|
|
|
nsISupports* *aLockObject)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIProfileLock> lock;
|
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
nsresult rv = NS_LockProfilePath(aDirectory, nullptr, nullptr,
|
2006-01-12 18:29:10 +03:00
|
|
|
getter_AddRefs(lock));
|
|
|
|
if (NS_SUCCEEDED(rv))
|
|
|
|
NS_ADDREF(*aLockObject = lock);
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
static int32_t sInitCounter;
|
2005-09-01 17:21:55 +04:00
|
|
|
|
|
|
|
nsresult
|
2012-06-06 06:08:30 +04:00
|
|
|
XRE_InitEmbedding2(nsIFile *aLibXULDirectory,
|
2015-05-27 20:59:13 +03:00
|
|
|
nsIFile *aAppDirectory,
|
|
|
|
nsIDirectoryServiceProvider *aAppDirProvider)
|
2005-09-01 17:21:55 +04:00
|
|
|
{
|
2006-01-12 18:29:10 +03:00
|
|
|
// Initialize some globals to make nsXREDirProvider happy
|
2012-07-30 18:20:58 +04:00
|
|
|
static char* kNullCommandLine[] = { nullptr };
|
2006-01-12 18:29:10 +03:00
|
|
|
gArgv = kNullCommandLine;
|
|
|
|
gArgc = 0;
|
2005-09-01 17:21:55 +04:00
|
|
|
|
|
|
|
NS_ENSURE_ARG(aLibXULDirectory);
|
2005-12-23 17:51:39 +03:00
|
|
|
|
2006-01-12 18:29:10 +03:00
|
|
|
if (++sInitCounter > 1) // XXXbsmedberg is this really the right solution?
|
|
|
|
return NS_OK;
|
|
|
|
|
2005-12-23 17:51:39 +03:00
|
|
|
if (!aAppDirectory)
|
|
|
|
aAppDirectory = aLibXULDirectory;
|
2005-09-01 17:21:55 +04:00
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
|
2006-01-12 18:29:10 +03:00
|
|
|
new nsXREDirProvider; // This sets gDirServiceProvider
|
|
|
|
if (!gDirServiceProvider)
|
2005-09-01 17:21:55 +04:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
2006-01-12 18:29:10 +03:00
|
|
|
rv = gDirServiceProvider->Initialize(aAppDirectory, aLibXULDirectory,
|
|
|
|
aAppDirProvider);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
rv = NS_InitXPCOM2(nullptr, aAppDirectory, gDirServiceProvider);
|
2005-09-01 17:21:55 +04:00
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
2010-03-13 11:03:27 +03:00
|
|
|
// We do not need to autoregister components here. The CheckCompatibility()
|
|
|
|
// bits in nsAppRunner.cpp check for an invalidation flag in
|
|
|
|
// compatibility.ini.
|
|
|
|
// If the app wants to autoregister every time (for instance, if it's debug),
|
|
|
|
// it can do so after we return from this function.
|
2005-09-01 17:21:55 +04:00
|
|
|
|
|
|
|
nsCOMPtr<nsIObserver> startupNotifier
|
|
|
|
(do_CreateInstance(NS_APPSTARTUPNOTIFIER_CONTRACTID));
|
|
|
|
if (!startupNotifier)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
startupNotifier->Observe(nullptr, APPSTARTUP_TOPIC, nullptr);
|
2005-09-01 17:21:55 +04:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2006-01-12 18:29:10 +03:00
|
|
|
void
|
|
|
|
XRE_NotifyProfile()
|
|
|
|
{
|
|
|
|
NS_ASSERTION(gDirServiceProvider, "XRE_InitEmbedding was not called!");
|
|
|
|
gDirServiceProvider->DoStartup();
|
|
|
|
}
|
|
|
|
|
2005-09-01 17:21:55 +04:00
|
|
|
void
|
|
|
|
XRE_TermEmbedding()
|
|
|
|
{
|
|
|
|
if (--sInitCounter != 0)
|
|
|
|
return;
|
|
|
|
|
2006-01-12 18:29:10 +03:00
|
|
|
NS_ASSERTION(gDirServiceProvider,
|
|
|
|
"XRE_TermEmbedding without XRE_InitEmbedding");
|
|
|
|
|
|
|
|
gDirServiceProvider->DoShutdown();
|
2012-07-30 18:20:58 +04:00
|
|
|
NS_ShutdownXPCOM(nullptr);
|
2006-01-12 18:29:10 +03:00
|
|
|
delete gDirServiceProvider;
|
2005-09-01 17:21:55 +04:00
|
|
|
}
|
2009-06-30 23:21:25 +04:00
|
|
|
|
2009-07-10 23:03:09 +04:00
|
|
|
const char*
|
2009-08-19 21:09:51 +04:00
|
|
|
XRE_ChildProcessTypeToString(GeckoProcessType aProcessType)
|
2009-07-10 23:03:09 +04:00
|
|
|
{
|
2009-08-19 21:09:51 +04:00
|
|
|
return (aProcessType < GeckoProcessType_End) ?
|
2016-06-02 01:13:59 +03:00
|
|
|
kGeckoProcessTypeString[aProcessType] : "invalid";
|
2009-07-10 23:03:09 +04:00
|
|
|
}
|
|
|
|
|
2014-08-09 04:52:12 +04:00
|
|
|
namespace mozilla {
|
|
|
|
namespace startup {
|
|
|
|
GeckoProcessType sChildProcessType = GeckoProcessType_Default;
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace startup
|
|
|
|
} // namespace mozilla
|
2014-08-09 04:52:12 +04:00
|
|
|
|
|
|
|
void
|
|
|
|
XRE_SetProcessType(const char* aProcessTypeString)
|
2009-07-10 23:03:09 +04:00
|
|
|
{
|
2014-08-09 04:55:22 +04:00
|
|
|
static bool called = false;
|
|
|
|
if (called) {
|
|
|
|
MOZ_CRASH();
|
|
|
|
}
|
|
|
|
called = true;
|
|
|
|
|
2014-08-09 04:52:12 +04:00
|
|
|
sChildProcessType = GeckoProcessType_Invalid;
|
2009-07-10 23:03:09 +04:00
|
|
|
for (int i = 0;
|
2011-10-11 09:50:08 +04:00
|
|
|
i < (int) ArrayLength(kGeckoProcessTypeString);
|
2009-07-10 23:03:09 +04:00
|
|
|
++i) {
|
2009-08-19 21:09:51 +04:00
|
|
|
if (!strcmp(kGeckoProcessTypeString[i], aProcessTypeString)) {
|
2014-08-09 04:52:12 +04:00
|
|
|
sChildProcessType = static_cast<GeckoProcessType>(i);
|
|
|
|
return;
|
2009-07-10 23:03:09 +04:00
|
|
|
}
|
|
|
|
}
|
2009-12-15 01:13:00 +03:00
|
|
|
}
|
2009-08-19 21:09:51 +04:00
|
|
|
|
2010-01-13 00:14:38 +03:00
|
|
|
#if defined(MOZ_CRASHREPORTER)
|
2010-03-19 05:01:10 +03:00
|
|
|
// FIXME/bug 539522: this out-of-place function is stuck here because
|
|
|
|
// IPDL wants access to this crashreporter interface, and
|
|
|
|
// crashreporter is built in such a way to make that awkward
|
2011-09-29 10:19:26 +04:00
|
|
|
bool
|
2012-08-22 19:56:38 +04:00
|
|
|
XRE_TakeMinidumpForChild(uint32_t aChildPid, nsIFile** aDump,
|
|
|
|
uint32_t* aSequence)
|
2010-03-19 05:01:10 +03:00
|
|
|
{
|
2012-07-11 06:20:05 +04:00
|
|
|
return CrashReporter::TakeMinidumpForChild(aChildPid, aDump, aSequence);
|
2010-03-19 05:01:10 +03:00
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool
|
2010-01-13 00:14:38 +03:00
|
|
|
XRE_SetRemoteExceptionHandler(const char* aPipe/*= 0*/)
|
|
|
|
{
|
2010-08-16 23:05:09 +04:00
|
|
|
#if defined(XP_WIN) || defined(XP_MACOSX)
|
2010-01-13 00:14:38 +03:00
|
|
|
return CrashReporter::SetRemoteExceptionHandler(nsDependentCString(aPipe));
|
|
|
|
#elif defined(OS_LINUX)
|
|
|
|
return CrashReporter::SetRemoteExceptionHandler();
|
|
|
|
#else
|
|
|
|
# error "OOP crash reporter unsupported on this platform"
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
#endif // if defined(MOZ_CRASHREPORTER)
|
|
|
|
|
2010-05-15 04:25:10 +04:00
|
|
|
#if defined(XP_WIN)
|
|
|
|
void
|
|
|
|
SetTaskbarGroupId(const nsString& aId)
|
|
|
|
{
|
|
|
|
typedef HRESULT (WINAPI * SetCurrentProcessExplicitAppUserModelIDPtr)(PCWSTR AppID);
|
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
SetCurrentProcessExplicitAppUserModelIDPtr funcAppUserModelID = nullptr;
|
2010-05-15 04:25:10 +04:00
|
|
|
|
|
|
|
HMODULE hDLL = ::LoadLibraryW(kShellLibraryName);
|
|
|
|
|
|
|
|
funcAppUserModelID = (SetCurrentProcessExplicitAppUserModelIDPtr)
|
|
|
|
GetProcAddress(hDLL, "SetCurrentProcessExplicitAppUserModelID");
|
|
|
|
|
|
|
|
if (!funcAppUserModelID) {
|
|
|
|
::FreeLibrary(hDLL);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (FAILED(funcAppUserModelID(aId.get()))) {
|
|
|
|
NS_WARNING("SetCurrentProcessExplicitAppUserModelID failed for child process.");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (hDLL)
|
|
|
|
::FreeLibrary(hDLL);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2009-06-30 23:21:25 +04:00
|
|
|
nsresult
|
|
|
|
XRE_InitChildProcess(int aArgc,
|
2014-11-14 11:26:24 +03:00
|
|
|
char* aArgv[],
|
2016-05-15 18:35:22 +03:00
|
|
|
const XREChildData* aChildData)
|
2009-06-30 23:21:25 +04:00
|
|
|
{
|
2009-10-28 00:52:37 +03:00
|
|
|
NS_ENSURE_ARG_MIN(aArgc, 2);
|
2009-06-30 23:21:25 +04:00
|
|
|
NS_ENSURE_ARG_POINTER(aArgv);
|
|
|
|
NS_ENSURE_ARG_POINTER(aArgv[0]);
|
2016-05-15 18:35:22 +03:00
|
|
|
MOZ_ASSERT(aChildData);
|
2013-05-30 17:23:50 +04:00
|
|
|
|
2015-02-18 04:09:32 +03:00
|
|
|
#ifdef HAS_DLL_BLOCKLIST
|
|
|
|
DllBlocklist_Initialize();
|
|
|
|
#endif
|
|
|
|
|
2016-05-21 05:57:31 +03:00
|
|
|
#ifdef MOZ_JPROF
|
|
|
|
// Call the code to install our handler
|
|
|
|
setupProfilingStuff();
|
|
|
|
#endif
|
|
|
|
|
2015-05-27 20:59:13 +03:00
|
|
|
// This is needed by Telemetry to initialize histogram collection.
|
2016-06-08 18:46:24 +03:00
|
|
|
Telemetry::CreateStatisticsRecorder();
|
2015-05-27 20:59:13 +03:00
|
|
|
|
2014-11-14 11:26:24 +03:00
|
|
|
#if !defined(MOZ_WIDGET_ANDROID) && !defined(MOZ_WIDGET_GONK)
|
|
|
|
// On non-Fennec Gecko, the GMPLoader code resides in plugin-container,
|
|
|
|
// and we must forward it through to the GMP code here.
|
2016-05-15 18:35:22 +03:00
|
|
|
GMPProcessChild::SetGMPLoader(aChildData->gmpLoader.get());
|
2014-11-14 11:26:24 +03:00
|
|
|
#else
|
|
|
|
// On Fennec, the GMPLoader's code resides inside XUL (because for the time
|
|
|
|
// being GMPLoader relies upon NSPR, which we can't use in plugin-container
|
|
|
|
// on Android), so we create it here inside XUL and pass it to the GMP code.
|
2016-05-15 18:35:22 +03:00
|
|
|
UniquePtr<GMPLoader> loader = CreateGMPLoader(nullptr);
|
|
|
|
GMPProcessChild::SetGMPLoader(loader.get());
|
2014-11-14 11:26:24 +03:00
|
|
|
#endif
|
|
|
|
|
2013-05-30 17:23:50 +04:00
|
|
|
#if defined(XP_WIN)
|
|
|
|
// From the --attach-console support in nsNativeAppSupportWin.cpp, but
|
|
|
|
// here we are a content child process, so we always attempt to attach
|
|
|
|
// to the parent's (ie, the browser's) console.
|
|
|
|
// Try to attach console to the parent process.
|
|
|
|
// It will succeed when the parent process is a command line,
|
|
|
|
// so that stdio will be displayed in it.
|
|
|
|
if (AttachConsole(ATTACH_PARENT_PROCESS)) {
|
|
|
|
// Change std handles to refer to new console handles.
|
|
|
|
// Before doing so, ensure that stdout/stderr haven't been
|
|
|
|
// redirected to a valid file
|
|
|
|
if (_fileno(stdout) == -1 ||
|
|
|
|
_get_osfhandle(fileno(stdout)) == -1)
|
|
|
|
freopen("CONOUT$", "w", stdout);
|
|
|
|
// Merge stderr into CONOUT$ since there isn't any `CONERR$`.
|
|
|
|
// http://msdn.microsoft.com/en-us/library/windows/desktop/ms683231%28v=vs.85%29.aspx
|
|
|
|
if (_fileno(stderr) == -1 ||
|
|
|
|
_get_osfhandle(fileno(stderr)) == -1)
|
|
|
|
freopen("CONOUT$", "w", stderr);
|
|
|
|
if (_fileno(stdin) == -1 || _get_osfhandle(fileno(stdin)) == -1)
|
|
|
|
freopen("CONIN$", "r", stdin);
|
|
|
|
}
|
2016-05-15 18:35:22 +03:00
|
|
|
|
|
|
|
#if defined(MOZ_SANDBOX)
|
|
|
|
if (aChildData->sandboxTargetServices) {
|
|
|
|
SandboxTarget::Instance()->SetTargetServices(aChildData->sandboxTargetServices);
|
|
|
|
}
|
|
|
|
#endif
|
2013-05-30 17:23:50 +04:00
|
|
|
#endif
|
|
|
|
|
2015-06-10 03:44:46 +03:00
|
|
|
// NB: This must be called before profiler_init
|
|
|
|
NS_LogInit();
|
|
|
|
|
2015-10-19 22:22:11 +03:00
|
|
|
mozilla::LogModule::Init();
|
|
|
|
|
2013-05-28 16:03:38 +04:00
|
|
|
char aLocal;
|
|
|
|
profiler_init(&aLocal);
|
2014-05-24 01:12:29 +04:00
|
|
|
|
|
|
|
PROFILER_LABEL("Startup", "XRE_InitChildProcess",
|
|
|
|
js::ProfileEntry::Category::OTHER);
|
2009-06-30 23:21:25 +04:00
|
|
|
|
2010-09-10 21:09:30 +04:00
|
|
|
// Complete 'task_t' exchange for Mac OS X. This structure has the same size
|
|
|
|
// regardless of architecture so we don't have any cross-arch issues here.
|
|
|
|
#ifdef XP_MACOSX
|
|
|
|
if (aArgc < 1)
|
2012-07-30 13:24:36 +04:00
|
|
|
return NS_ERROR_FAILURE;
|
2010-09-10 21:09:30 +04:00
|
|
|
const char* const mach_port_name = aArgv[--aArgc];
|
|
|
|
|
|
|
|
const int kTimeoutMs = 1000;
|
|
|
|
|
|
|
|
MachSendMessage child_message(0);
|
2014-09-01 05:12:55 +04:00
|
|
|
if (!child_message.AddDescriptor(MachMsgPortDescriptor(mach_task_self()))) {
|
2010-09-10 21:09:30 +04:00
|
|
|
NS_WARNING("child AddDescriptor(mach_task_self()) failed.");
|
2012-07-30 13:24:36 +04:00
|
|
|
return NS_ERROR_FAILURE;
|
2010-09-10 21:09:30 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
ReceivePort child_recv_port;
|
|
|
|
mach_port_t raw_child_recv_port = child_recv_port.GetPort();
|
2014-09-01 05:12:55 +04:00
|
|
|
if (!child_message.AddDescriptor(MachMsgPortDescriptor(raw_child_recv_port))) {
|
2010-09-10 21:09:30 +04:00
|
|
|
NS_WARNING("Adding descriptor to message failed");
|
2012-07-30 13:24:36 +04:00
|
|
|
return NS_ERROR_FAILURE;
|
2010-09-10 21:09:30 +04:00
|
|
|
}
|
|
|
|
|
2015-06-17 07:38:38 +03:00
|
|
|
ReceivePort* ports_out_receiver = new ReceivePort();
|
|
|
|
if (!child_message.AddDescriptor(MachMsgPortDescriptor(ports_out_receiver->GetPort()))) {
|
|
|
|
NS_WARNING("Adding descriptor to message failed");
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
ReceivePort* ports_in_receiver = new ReceivePort();
|
|
|
|
if (!child_message.AddDescriptor(MachMsgPortDescriptor(ports_in_receiver->GetPort()))) {
|
|
|
|
NS_WARNING("Adding descriptor to message failed");
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2010-09-10 21:09:30 +04:00
|
|
|
MachPortSender child_sender(mach_port_name);
|
|
|
|
kern_return_t err = child_sender.SendMessage(child_message, kTimeoutMs);
|
|
|
|
if (err != KERN_SUCCESS) {
|
|
|
|
NS_WARNING("child SendMessage() failed");
|
2012-07-30 13:24:36 +04:00
|
|
|
return NS_ERROR_FAILURE;
|
2010-09-10 21:09:30 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
MachReceiveMessage parent_message;
|
|
|
|
err = child_recv_port.WaitForMessage(&parent_message, kTimeoutMs);
|
|
|
|
if (err != KERN_SUCCESS) {
|
|
|
|
NS_WARNING("child WaitForMessage() failed");
|
2012-07-30 13:24:36 +04:00
|
|
|
return NS_ERROR_FAILURE;
|
2010-09-10 21:09:30 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (parent_message.GetTranslatedPort(0) == MACH_PORT_NULL) {
|
|
|
|
NS_WARNING("child GetTranslatedPort(0) failed");
|
2012-07-30 13:24:36 +04:00
|
|
|
return NS_ERROR_FAILURE;
|
2010-09-10 21:09:30 +04:00
|
|
|
}
|
2015-06-17 07:38:38 +03:00
|
|
|
|
2010-09-10 21:09:30 +04:00
|
|
|
err = task_set_bootstrap_port(mach_task_self(),
|
|
|
|
parent_message.GetTranslatedPort(0));
|
2015-06-17 07:38:38 +03:00
|
|
|
|
|
|
|
if (parent_message.GetTranslatedPort(1) == MACH_PORT_NULL) {
|
|
|
|
NS_WARNING("child GetTranslatedPort(1) failed");
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
MachPortSender* ports_out_sender = new MachPortSender(parent_message.GetTranslatedPort(1));
|
|
|
|
|
|
|
|
if (parent_message.GetTranslatedPort(2) == MACH_PORT_NULL) {
|
|
|
|
NS_WARNING("child GetTranslatedPort(2) failed");
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
MachPortSender* ports_in_sender = new MachPortSender(parent_message.GetTranslatedPort(2));
|
|
|
|
|
2010-09-10 21:09:30 +04:00
|
|
|
if (err != KERN_SUCCESS) {
|
|
|
|
NS_WARNING("child task_set_bootstrap_port() failed");
|
2012-07-30 13:24:36 +04:00
|
|
|
return NS_ERROR_FAILURE;
|
2010-09-10 21:09:30 +04:00
|
|
|
}
|
2015-06-17 07:38:38 +03:00
|
|
|
|
2010-09-10 21:09:30 +04:00
|
|
|
#endif
|
2011-06-24 03:30:31 +04:00
|
|
|
|
|
|
|
SetupErrorHandling(aArgv[0]);
|
|
|
|
|
2010-09-10 21:09:30 +04:00
|
|
|
#if defined(MOZ_CRASHREPORTER)
|
|
|
|
if (aArgc < 1)
|
2012-07-30 13:24:36 +04:00
|
|
|
return NS_ERROR_FAILURE;
|
2010-09-10 21:09:30 +04:00
|
|
|
const char* const crashReporterArg = aArgv[--aArgc];
|
|
|
|
|
|
|
|
# if defined(XP_WIN) || defined(XP_MACOSX)
|
|
|
|
// on windows and mac, |crashReporterArg| is the named pipe on which the
|
|
|
|
// server is listening for requests, or "-" if crash reporting is
|
|
|
|
// disabled.
|
2011-09-08 20:11:52 +04:00
|
|
|
if (0 != strcmp("-", crashReporterArg) &&
|
|
|
|
!XRE_SetRemoteExceptionHandler(crashReporterArg)) {
|
|
|
|
// Bug 684322 will add better visibility into this condition
|
|
|
|
NS_WARNING("Could not setup crash reporting\n");
|
|
|
|
}
|
2010-09-10 21:09:30 +04:00
|
|
|
# elif defined(OS_LINUX)
|
|
|
|
// on POSIX, |crashReporterArg| is "true" if crash reporting is
|
|
|
|
// enabled, false otherwise
|
2011-09-08 20:11:52 +04:00
|
|
|
if (0 != strcmp("false", crashReporterArg) &&
|
2013-10-11 00:36:42 +04:00
|
|
|
!XRE_SetRemoteExceptionHandler(nullptr)) {
|
2011-09-08 20:11:52 +04:00
|
|
|
// Bug 684322 will add better visibility into this condition
|
|
|
|
NS_WARNING("Could not setup crash reporting\n");
|
|
|
|
}
|
2010-09-10 21:09:30 +04:00
|
|
|
# else
|
|
|
|
# error "OOP crash reporting unsupported on this platform"
|
|
|
|
# endif
|
|
|
|
#endif // if defined(MOZ_CRASHREPORTER)
|
|
|
|
|
2010-01-29 09:50:44 +03:00
|
|
|
gArgv = aArgv;
|
|
|
|
gArgc = aArgc;
|
|
|
|
|
2015-03-30 14:30:36 +03:00
|
|
|
#ifdef MOZ_X11
|
|
|
|
XInitThreads();
|
|
|
|
#endif
|
2015-06-26 20:59:52 +03:00
|
|
|
#if MOZ_WIDGET_GTK == 2
|
|
|
|
XRE_GlibInit();
|
2009-10-19 20:28:42 +04:00
|
|
|
#endif
|
2016-05-27 02:50:26 +03:00
|
|
|
#ifdef MOZ_WIDGET_GTK
|
|
|
|
// Setting the name here avoids the need to pass this through to gtk_init().
|
|
|
|
g_set_prgname(aArgv[0]);
|
|
|
|
#endif
|
2009-10-19 20:28:42 +04:00
|
|
|
|
2011-01-05 16:48:21 +03:00
|
|
|
#if defined(MOZ_WIDGET_QT)
|
|
|
|
nsQAppInstance::AddRef();
|
|
|
|
#endif
|
|
|
|
|
2016-03-07 20:08:15 +03:00
|
|
|
#ifdef OS_POSIX
|
|
|
|
if (PR_GetEnv("MOZ_DEBUG_CHILD_PROCESS") ||
|
|
|
|
PR_GetEnv("MOZ_DEBUG_CHILD_PAUSE")) {
|
2016-03-03 14:09:25 +03:00
|
|
|
printf_stderr("\n\nCHILDCHILDCHILDCHILD\n debug me @ %d\n\n",
|
|
|
|
base::GetCurrentProcId());
|
|
|
|
sleep(30);
|
2016-03-07 20:08:15 +03:00
|
|
|
}
|
2009-07-07 20:26:49 +04:00
|
|
|
#elif defined(OS_WIN)
|
2016-03-07 20:08:15 +03:00
|
|
|
if (PR_GetEnv("MOZ_DEBUG_CHILD_PROCESS")) {
|
|
|
|
NS_DebugBreak(NS_DEBUG_BREAK,
|
|
|
|
"Invoking NS_DebugBreak() to debug child process",
|
|
|
|
nullptr, __FILE__, __LINE__);
|
|
|
|
} else if (PR_GetEnv("MOZ_DEBUG_CHILD_PAUSE")) {
|
|
|
|
printf_stderr("\n\nCHILDCHILDCHILDCHILD\n debug me @ %d\n\n",
|
|
|
|
base::GetCurrentProcId());
|
2016-03-03 14:09:25 +03:00
|
|
|
::Sleep(10000);
|
2009-06-30 23:21:25 +04:00
|
|
|
}
|
2016-03-07 20:08:15 +03:00
|
|
|
#endif
|
2009-06-30 23:21:25 +04:00
|
|
|
|
2009-10-28 00:52:37 +03:00
|
|
|
// child processes launched by GeckoChildProcessHost get this magic
|
|
|
|
// argument appended to their command lines
|
|
|
|
const char* const parentPIDString = aArgv[aArgc-1];
|
2015-02-10 01:34:50 +03:00
|
|
|
MOZ_ASSERT(parentPIDString, "NULL parent PID");
|
2009-10-28 00:52:37 +03:00
|
|
|
--aArgc;
|
|
|
|
|
|
|
|
char* end = 0;
|
|
|
|
base::ProcessId parentPID = strtol(parentPIDString, &end, 10);
|
2015-02-10 01:34:50 +03:00
|
|
|
MOZ_ASSERT(!*end, "invalid parent PID");
|
2009-11-18 01:18:06 +03:00
|
|
|
|
2015-06-17 07:38:38 +03:00
|
|
|
#ifdef XP_MACOSX
|
|
|
|
mozilla::ipc::SharedMemoryBasic::SetupMachMemory(parentPID, ports_in_receiver, ports_in_sender,
|
|
|
|
ports_out_sender, ports_out_receiver, true);
|
|
|
|
#endif
|
|
|
|
|
2010-05-15 04:25:10 +04:00
|
|
|
#if defined(XP_WIN)
|
|
|
|
// On Win7+, register the application user model id passed in by
|
|
|
|
// parent. This insures windows created by the container properly
|
|
|
|
// group with the parent app on the Win7 taskbar.
|
2012-12-18 20:24:42 +04:00
|
|
|
const char* const appModelUserId = aArgv[--aArgc];
|
2010-05-15 04:25:10 +04:00
|
|
|
if (appModelUserId) {
|
|
|
|
// '-' implies no support
|
|
|
|
if (*appModelUserId != '-') {
|
|
|
|
nsString appId;
|
|
|
|
appId.AssignWithConversion(nsDependentCString(appModelUserId));
|
|
|
|
// The version string is encased in quotes
|
|
|
|
appId.Trim(NS_LITERAL_CSTRING("\"").get());
|
|
|
|
// Set the id
|
|
|
|
SetTaskbarGroupId(appId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2009-06-30 23:21:25 +04:00
|
|
|
base::AtExitManager exitManager;
|
2009-11-04 08:12:18 +03:00
|
|
|
|
2012-08-06 16:02:31 +04:00
|
|
|
nsresult rv = XRE_InitCommandLine(aArgc, aArgv);
|
2009-11-20 01:57:56 +03:00
|
|
|
if (NS_FAILED(rv)) {
|
2013-04-18 19:34:49 +04:00
|
|
|
profiler_shutdown();
|
2009-11-20 01:57:56 +03:00
|
|
|
NS_LogTerm();
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2009-11-04 08:12:18 +03:00
|
|
|
|
2010-05-14 10:41:14 +04:00
|
|
|
MessageLoop::Type uiLoopType;
|
2014-08-09 04:52:12 +04:00
|
|
|
switch (XRE_GetProcessType()) {
|
2010-05-14 10:41:14 +04:00
|
|
|
case GeckoProcessType_Content:
|
|
|
|
// Content processes need the XPCOM/chromium frankenventloop
|
|
|
|
uiLoopType = MessageLoop::TYPE_MOZILLA_CHILD;
|
|
|
|
break;
|
2014-12-16 01:04:21 +03:00
|
|
|
case GeckoProcessType_GMPlugin:
|
|
|
|
uiLoopType = MessageLoop::TYPE_DEFAULT;
|
|
|
|
break;
|
2016-06-11 05:27:24 +03:00
|
|
|
case GeckoProcessType_GPU:
|
|
|
|
uiLoopType = MessageLoop::TYPE_UI;
|
|
|
|
break;
|
2010-05-14 10:41:14 +04:00
|
|
|
default:
|
|
|
|
uiLoopType = MessageLoop::TYPE_UI;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2009-06-30 23:21:25 +04:00
|
|
|
{
|
2010-08-06 02:11:23 +04:00
|
|
|
// This is a lexical scope for the MessageLoop below. We want it
|
|
|
|
// to go out of scope before NS_LogTerm() so that we don't get
|
|
|
|
// spurious warnings about XPCOM objects being destroyed from a
|
|
|
|
// static context.
|
|
|
|
|
|
|
|
// Associate this thread with a UI MessageLoop
|
|
|
|
MessageLoop uiMessageLoop(uiLoopType);
|
|
|
|
{
|
|
|
|
nsAutoPtr<ProcessChild> process;
|
|
|
|
|
2014-05-18 07:16:51 +04:00
|
|
|
#ifdef XP_WIN
|
|
|
|
mozilla::ipc::windows::InitUIThread();
|
|
|
|
#endif
|
|
|
|
|
2014-08-09 04:52:12 +04:00
|
|
|
switch (XRE_GetProcessType()) {
|
2010-08-06 02:11:23 +04:00
|
|
|
case GeckoProcessType_Default:
|
|
|
|
NS_RUNTIMEABORT("This makes no sense");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case GeckoProcessType_Plugin:
|
2015-04-01 11:40:35 +03:00
|
|
|
process = new PluginProcessChild(parentPID);
|
2010-08-06 02:11:23 +04:00
|
|
|
break;
|
|
|
|
|
2012-12-18 20:24:42 +04:00
|
|
|
case GeckoProcessType_Content: {
|
2015-04-01 11:40:35 +03:00
|
|
|
process = new ContentProcess(parentPID);
|
2012-12-18 20:24:42 +04:00
|
|
|
// If passed in grab the application path for xpcom init
|
|
|
|
nsCString appDir;
|
|
|
|
for (int idx = aArgc; idx > 0; idx--) {
|
|
|
|
if (aArgv[idx] && !strcmp(aArgv[idx], "-appdir")) {
|
|
|
|
appDir.Assign(nsDependentCString(aArgv[idx+1]));
|
|
|
|
static_cast<ContentProcess*>(process.get())->SetAppDir(appDir);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-08-06 02:11:23 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case GeckoProcessType_IPDLUnitTest:
|
2009-10-06 22:02:26 +04:00
|
|
|
#ifdef MOZ_IPDL_TESTS
|
2015-04-01 11:40:35 +03:00
|
|
|
process = new IPDLUnitTestProcessChild(parentPID);
|
2010-05-11 08:18:00 +04:00
|
|
|
#else
|
2010-08-06 02:11:23 +04:00
|
|
|
NS_RUNTIMEABORT("rebuild with --enable-ipdl-tests");
|
2009-10-06 22:02:26 +04:00
|
|
|
#endif
|
2010-08-06 02:11:23 +04:00
|
|
|
break;
|
2009-10-06 22:02:26 +04:00
|
|
|
|
2014-05-18 07:05:46 +04:00
|
|
|
case GeckoProcessType_GMPlugin:
|
2015-04-01 11:40:35 +03:00
|
|
|
process = new gmp::GMPProcessChild(parentPID);
|
2014-05-18 07:05:46 +04:00
|
|
|
break;
|
|
|
|
|
2016-06-11 05:27:24 +03:00
|
|
|
case GeckoProcessType_GPU:
|
|
|
|
process = new gfx::GPUProcessImpl(parentPID);
|
|
|
|
break;
|
|
|
|
|
2010-08-06 02:11:23 +04:00
|
|
|
default:
|
|
|
|
NS_RUNTIMEABORT("Unknown main thread class");
|
|
|
|
}
|
2009-06-30 23:21:25 +04:00
|
|
|
|
2010-08-06 02:11:23 +04:00
|
|
|
if (!process->Init()) {
|
2013-04-18 19:34:49 +04:00
|
|
|
profiler_shutdown();
|
2010-08-06 02:11:23 +04:00
|
|
|
NS_LogTerm();
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2009-09-03 04:18:27 +04:00
|
|
|
|
2016-03-25 19:51:00 +03:00
|
|
|
#ifdef MOZ_CRASHREPORTER
|
2016-03-16 21:35:50 +03:00
|
|
|
#if defined(XP_WIN) || defined(XP_MACOSX)
|
|
|
|
CrashReporter::InitChildProcessTmpDir();
|
|
|
|
#endif
|
2016-03-25 19:51:00 +03:00
|
|
|
#endif
|
2016-03-16 21:35:50 +03:00
|
|
|
|
2015-07-16 14:24:34 +03:00
|
|
|
#if defined(XP_WIN)
|
|
|
|
// Set child processes up such that they will get killed after the
|
|
|
|
// chrome process is killed in cases where the user shuts the system
|
|
|
|
// down or logs off.
|
|
|
|
::SetProcessShutdownParameters(0x280 - 1, SHUTDOWN_NORETRY);
|
|
|
|
#endif
|
|
|
|
|
2014-11-29 20:12:18 +03:00
|
|
|
#if defined(MOZ_SANDBOX) && defined(XP_WIN)
|
2014-06-11 18:32:37 +04:00
|
|
|
// We need to do this after the process has been initialised, as
|
2014-11-29 20:12:18 +03:00
|
|
|
// InitLoggingIfRequired may need access to prefs.
|
2016-05-15 18:35:22 +03:00
|
|
|
mozilla::sandboxing::InitLoggingIfRequired(aChildData->ProvideLogFunction);
|
2014-06-11 18:32:37 +04:00
|
|
|
#endif
|
|
|
|
|
2015-12-29 15:56:00 +03:00
|
|
|
OverrideDefaultLocaleIfNeeded();
|
|
|
|
|
2010-08-06 02:11:23 +04:00
|
|
|
// Run the UI event loop on the main thread.
|
|
|
|
uiMessageLoop.MessageLoop::Run();
|
2009-09-03 04:18:27 +04:00
|
|
|
|
2010-08-06 02:11:23 +04:00
|
|
|
// Allow ProcessChild to clean up after itself before going out of
|
|
|
|
// scope and being deleted
|
|
|
|
process->CleanUp();
|
2011-02-25 14:53:36 +03:00
|
|
|
mozilla::Omnijar::CleanUp();
|
2016-05-27 01:07:24 +03:00
|
|
|
|
|
|
|
#if defined(XP_MACOSX)
|
|
|
|
// Everybody should be done using shared memory by now.
|
|
|
|
mozilla::ipc::SharedMemoryBasic::Shutdown();
|
|
|
|
#endif
|
2010-08-06 02:11:23 +04:00
|
|
|
}
|
2009-06-30 23:21:25 +04:00
|
|
|
}
|
|
|
|
|
2016-06-08 18:46:24 +03:00
|
|
|
Telemetry::DestroyStatisticsRecorder();
|
2013-04-18 19:34:49 +04:00
|
|
|
profiler_shutdown();
|
2009-11-20 01:57:56 +03:00
|
|
|
NS_LogTerm();
|
2009-12-05 03:15:44 +03:00
|
|
|
return XRE_DeinitCommandLine();
|
2009-06-30 23:21:25 +04:00
|
|
|
}
|
|
|
|
|
2009-09-03 04:18:27 +04:00
|
|
|
MessageLoop*
|
|
|
|
XRE_GetIOMessageLoop()
|
|
|
|
{
|
|
|
|
if (sChildProcessType == GeckoProcessType_Default) {
|
|
|
|
return BrowserProcessSubThread::GetMessageLoop(BrowserProcessSubThread::IO);
|
|
|
|
}
|
2010-05-11 08:18:00 +04:00
|
|
|
return IOThreadChild::message_loop();
|
2009-09-03 04:18:27 +04:00
|
|
|
}
|
|
|
|
|
2009-06-30 23:21:25 +04:00
|
|
|
namespace {
|
|
|
|
|
2016-04-26 03:23:21 +03:00
|
|
|
class MainFunctionRunnable : public Runnable
|
2009-06-30 23:21:25 +04:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
NS_DECL_NSIRUNNABLE
|
|
|
|
|
|
|
|
MainFunctionRunnable(MainFunction aFunction,
|
|
|
|
void* aData)
|
|
|
|
: mFunction(aFunction),
|
|
|
|
mData(aData)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(aFunction, "Don't give me a null pointer!");
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
MainFunction mFunction;
|
|
|
|
void* mData;
|
|
|
|
};
|
|
|
|
|
|
|
|
} /* anonymous namespace */
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
MainFunctionRunnable::Run()
|
|
|
|
{
|
|
|
|
mFunction(mData);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
XRE_InitParentProcess(int aArgc,
|
|
|
|
char* aArgv[],
|
|
|
|
MainFunction aMainFunction,
|
|
|
|
void* aMainFunctionData)
|
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_MIN(aArgc, 1);
|
|
|
|
NS_ENSURE_ARG_POINTER(aArgv);
|
|
|
|
NS_ENSURE_ARG_POINTER(aArgv[0]);
|
|
|
|
|
2011-06-03 22:33:55 +04:00
|
|
|
ScopedXREEmbed embed;
|
|
|
|
|
2010-08-19 23:50:17 +04:00
|
|
|
gArgc = aArgc;
|
|
|
|
gArgv = aArgv;
|
2012-08-06 16:02:31 +04:00
|
|
|
nsresult rv = XRE_InitCommandLine(gArgc, gArgv);
|
2009-11-04 08:12:18 +03:00
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return NS_ERROR_FAILURE;
|
2009-08-28 23:46:21 +04:00
|
|
|
|
2009-06-30 23:21:25 +04:00
|
|
|
{
|
|
|
|
embed.Start();
|
|
|
|
|
|
|
|
nsCOMPtr<nsIAppShell> appShell(do_GetService(kAppShellCID));
|
|
|
|
NS_ENSURE_TRUE(appShell, NS_ERROR_FAILURE);
|
|
|
|
|
|
|
|
if (aMainFunction) {
|
|
|
|
nsCOMPtr<nsIRunnable> runnable =
|
|
|
|
new MainFunctionRunnable(aMainFunction, aMainFunctionData);
|
|
|
|
NS_ENSURE_TRUE(runnable, NS_ERROR_OUT_OF_MEMORY);
|
|
|
|
|
|
|
|
nsresult rv = NS_DispatchToCurrentThread(runnable);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Do event loop
|
|
|
|
if (NS_FAILED(appShell->Run())) {
|
|
|
|
NS_WARNING("Failed to run appshell");
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-12-05 03:15:44 +03:00
|
|
|
return XRE_DeinitCommandLine();
|
2009-06-30 23:21:25 +04:00
|
|
|
}
|
|
|
|
|
2009-10-06 22:02:26 +04:00
|
|
|
#ifdef MOZ_IPDL_TESTS
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// IPDL unit test
|
|
|
|
|
|
|
|
int
|
|
|
|
XRE_RunIPDLTest(int aArgc, char** aArgv)
|
|
|
|
{
|
|
|
|
if (aArgc < 2) {
|
|
|
|
fprintf(stderr, "TEST-UNEXPECTED-FAIL | <---> | insufficient #args, need at least 2\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void* data = reinterpret_cast<void*>(aArgv[aArgc-1]);
|
|
|
|
|
|
|
|
nsresult rv =
|
|
|
|
XRE_InitParentProcess(
|
|
|
|
--aArgc, aArgv, mozilla::_ipdltest::IPDLUnitTestMain, data);
|
|
|
|
NS_ENSURE_SUCCESS(rv, 1);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif // ifdef MOZ_IPDL_TESTS
|
|
|
|
|
2009-08-28 23:46:21 +04:00
|
|
|
nsresult
|
|
|
|
XRE_RunAppShell()
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIAppShell> appShell(do_GetService(kAppShellCID));
|
2012-12-10 18:14:52 +04:00
|
|
|
NS_ENSURE_TRUE(appShell, NS_ERROR_FAILURE);
|
2011-02-01 22:53:53 +03:00
|
|
|
#if defined(XP_MACOSX)
|
|
|
|
{
|
|
|
|
// In content processes that want XPCOM (and hence want
|
|
|
|
// AppShell), we usually run our hybrid event loop through
|
|
|
|
// MessagePump::Run(), by way of nsBaseAppShell::Run(). The
|
|
|
|
// Cocoa nsAppShell impl, however, implements its own Run()
|
|
|
|
// that's unaware of MessagePump. That's all rather suboptimal,
|
|
|
|
// but oddly enough not a problem... usually.
|
|
|
|
//
|
|
|
|
// The problem with this setup comes during startup.
|
|
|
|
// XPCOM-in-subprocesses depends on IPC, e.g. to init the pref
|
|
|
|
// service, so we have to init IPC first. But, IPC also
|
|
|
|
// indirectly kinda-depends on XPCOM, because MessagePump
|
|
|
|
// schedules work from off-main threads (e.g. IO thread) by
|
|
|
|
// using NS_DispatchToMainThread(). If the IO thread receives a
|
|
|
|
// Message from the parent before nsThreadManager is
|
|
|
|
// initialized, then DispatchToMainThread() will fail, although
|
|
|
|
// MessagePump will remember the task. This race condition
|
|
|
|
// isn't a problem when appShell->Run() ends up in
|
|
|
|
// MessagePump::Run(), because MessagePump will immediate see it
|
|
|
|
// has work to do. It *is* a problem when we end up in [NSApp
|
|
|
|
// run], because it's not aware that MessagePump has work that
|
|
|
|
// needs to be processed; that was supposed to be signaled by
|
|
|
|
// nsIRunnable(s).
|
|
|
|
//
|
|
|
|
// So instead of hacking Cocoa nsAppShell or rewriting the
|
|
|
|
// event-loop system, we compromise here by processing any tasks
|
|
|
|
// that might have been enqueued on MessagePump, *before*
|
|
|
|
// MessagePump::ScheduleWork was able to successfully
|
|
|
|
// DispatchToMainThread().
|
|
|
|
MessageLoop* loop = MessageLoop::current();
|
|
|
|
bool couldNest = loop->NestableTasksAllowed();
|
|
|
|
|
|
|
|
loop->SetNestableTasksAllowed(true);
|
2016-04-28 03:06:05 +03:00
|
|
|
RefPtr<Runnable> task = new MessageLoop::QuitTask();
|
|
|
|
loop->PostTask(task.forget());
|
2011-02-01 22:53:53 +03:00
|
|
|
loop->Run();
|
|
|
|
|
|
|
|
loop->SetNestableTasksAllowed(couldNest);
|
|
|
|
}
|
|
|
|
#endif // XP_MACOSX
|
2009-08-28 23:46:21 +04:00
|
|
|
return appShell->Run();
|
|
|
|
}
|
|
|
|
|
2009-08-26 03:39:30 +04:00
|
|
|
void
|
2009-12-03 11:16:28 +03:00
|
|
|
XRE_ShutdownChildProcess()
|
2009-08-28 23:46:21 +04:00
|
|
|
{
|
2015-02-10 01:34:50 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread(), "Wrong thread!");
|
2009-12-03 11:16:28 +03:00
|
|
|
|
2011-07-19 14:04:54 +04:00
|
|
|
mozilla::DebugOnly<MessageLoop*> ioLoop = XRE_GetIOMessageLoop();
|
2015-02-10 01:34:50 +03:00
|
|
|
MOZ_ASSERT(!!ioLoop, "Bad shutdown order");
|
2009-12-03 11:16:28 +03:00
|
|
|
|
2010-05-11 08:18:00 +04:00
|
|
|
// Quit() sets off the following chain of events
|
|
|
|
// (1) UI loop starts quitting
|
|
|
|
// (2) UI loop returns from Run() in XRE_InitChildProcess()
|
|
|
|
// (3) ProcessChild goes out of scope and terminates the IO thread
|
|
|
|
// (4) ProcessChild joins the IO thread
|
|
|
|
// (5) exit()
|
2011-02-01 22:53:53 +03:00
|
|
|
MessageLoop::current()->Quit();
|
|
|
|
#if defined(XP_MACOSX)
|
|
|
|
nsCOMPtr<nsIAppShell> appShell(do_GetService(kAppShellCID));
|
|
|
|
if (appShell) {
|
|
|
|
// On Mac, we might be only above nsAppShell::Run(), not
|
|
|
|
// MessagePump::Run(). See XRE_RunAppShell(). To account for
|
|
|
|
// that case, we fire off an Exit() here. If we were indeed
|
|
|
|
// above MessagePump::Run(), this Exit() is just superfluous.
|
|
|
|
appShell->Exit();
|
|
|
|
}
|
|
|
|
#endif // XP_MACOSX
|
2009-08-29 03:16:19 +04:00
|
|
|
}
|
2009-08-28 23:46:21 +04:00
|
|
|
|
2009-08-29 03:16:19 +04:00
|
|
|
namespace {
|
2011-06-24 03:31:58 +04:00
|
|
|
ContentParent* gContentParent; //long-lived, manually refcounted
|
2009-11-06 23:43:39 +03:00
|
|
|
TestShellParent* GetOrCreateTestShellParent()
|
|
|
|
{
|
2011-06-24 03:31:58 +04:00
|
|
|
if (!gContentParent) {
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<ContentParent> parent = ContentParent::GetNewOrUsedBrowserProcess();
|
2013-04-03 03:52:51 +04:00
|
|
|
parent.forget(&gContentParent);
|
2011-06-24 03:31:58 +04:00
|
|
|
} else if (!gContentParent->IsAlive()) {
|
2012-07-30 18:20:58 +04:00
|
|
|
return nullptr;
|
2009-11-06 23:43:39 +03:00
|
|
|
}
|
2011-06-24 03:31:58 +04:00
|
|
|
TestShellParent* tsp = gContentParent->GetTestShellSingleton();
|
|
|
|
if (!tsp) {
|
|
|
|
tsp = gContentParent->CreateTestShell();
|
|
|
|
}
|
|
|
|
return tsp;
|
2009-11-06 23:43:39 +03:00
|
|
|
}
|
2015-07-13 18:25:42 +03:00
|
|
|
|
|
|
|
} // namespace
|
2009-08-28 23:46:21 +04:00
|
|
|
|
2009-08-29 03:16:19 +04:00
|
|
|
bool
|
|
|
|
XRE_SendTestShellCommand(JSContext* aCx,
|
|
|
|
JSString* aCommand,
|
|
|
|
void* aCallback)
|
2009-08-28 23:46:21 +04:00
|
|
|
{
|
2013-05-13 00:58:02 +04:00
|
|
|
JS::RootedString cmd(aCx, aCommand);
|
2009-11-06 23:43:39 +03:00
|
|
|
TestShellParent* tsp = GetOrCreateTestShellParent();
|
|
|
|
NS_ENSURE_TRUE(tsp, false);
|
2009-08-28 23:46:21 +04:00
|
|
|
|
2014-07-05 19:30:54 +04:00
|
|
|
nsAutoJSString command;
|
2013-05-13 00:58:02 +04:00
|
|
|
NS_ENSURE_TRUE(command.init(aCx, cmd), false);
|
2010-12-03 11:24:17 +03:00
|
|
|
|
2009-08-29 03:16:19 +04:00
|
|
|
if (!aCallback) {
|
2009-11-06 23:43:39 +03:00
|
|
|
return tsp->SendExecuteCommand(command);
|
2009-08-26 03:39:30 +04:00
|
|
|
}
|
2009-08-29 03:16:19 +04:00
|
|
|
|
|
|
|
TestShellCommandParent* callback = static_cast<TestShellCommandParent*>(
|
2009-11-06 23:43:39 +03:00
|
|
|
tsp->SendPTestShellCommandConstructor(command));
|
2009-08-29 03:16:19 +04:00
|
|
|
NS_ENSURE_TRUE(callback, false);
|
|
|
|
|
2013-03-07 01:16:46 +04:00
|
|
|
JS::Value callbackVal = *reinterpret_cast<JS::Value*>(aCallback);
|
2009-08-29 03:16:19 +04:00
|
|
|
NS_ENSURE_TRUE(callback->SetCallback(aCx, callbackVal), false);
|
|
|
|
|
|
|
|
return true;
|
2009-08-26 03:39:30 +04:00
|
|
|
}
|
|
|
|
|
2009-11-11 11:34:08 +03:00
|
|
|
bool
|
|
|
|
XRE_ShutdownTestShell()
|
|
|
|
{
|
2011-06-24 03:31:58 +04:00
|
|
|
if (!gContentParent) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
bool ret = true;
|
|
|
|
if (gContentParent->IsAlive()) {
|
|
|
|
ret = gContentParent->DestroyTestShell(
|
|
|
|
gContentParent->GetTestShellSingleton());
|
|
|
|
}
|
|
|
|
NS_RELEASE(gContentParent);
|
|
|
|
return ret;
|
2009-11-11 11:34:08 +03:00
|
|
|
}
|
|
|
|
|
2010-02-25 01:25:16 +03:00
|
|
|
#ifdef MOZ_X11
|
|
|
|
void
|
|
|
|
XRE_InstallX11ErrorHandler()
|
|
|
|
{
|
2014-06-16 12:54:00 +04:00
|
|
|
#if (MOZ_WIDGET_GTK == 3)
|
|
|
|
InstallGdkErrorHandler();
|
|
|
|
#else
|
2010-02-25 01:25:16 +03:00
|
|
|
InstallX11ErrorHandler();
|
2014-06-16 12:54:00 +04:00
|
|
|
#endif
|
2010-02-25 01:25:16 +03:00
|
|
|
}
|
|
|
|
#endif
|
2012-05-25 14:01:29 +04:00
|
|
|
|
2014-07-26 04:52:00 +04:00
|
|
|
#ifdef MOZ_B2G_LOADER
|
|
|
|
extern const nsXREAppData* gAppData;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Preload static data of Gecko for B2G loader.
|
|
|
|
*
|
|
|
|
* This function is supposed to be called before XPCOM is initialized.
|
|
|
|
* For now, this function preloads
|
|
|
|
* - XPT interface Information
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
XRE_ProcLoaderPreload(const char* aProgramDir, const nsXREAppData* aAppData)
|
|
|
|
{
|
|
|
|
void PreloadXPT(nsIFile *);
|
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
nsCOMPtr<nsIFile> omnijarFile;
|
|
|
|
rv = NS_NewNativeLocalFile(nsCString(aProgramDir),
|
|
|
|
true,
|
|
|
|
getter_AddRefs(omnijarFile));
|
2015-01-08 06:35:19 +03:00
|
|
|
MOZ_RELEASE_ASSERT(NS_SUCCEEDED(rv));
|
|
|
|
|
2014-07-26 04:52:00 +04:00
|
|
|
rv = omnijarFile->AppendNative(NS_LITERAL_CSTRING(NS_STRINGIFY(OMNIJAR_NAME)));
|
2015-01-08 06:35:19 +03:00
|
|
|
MOZ_RELEASE_ASSERT(NS_SUCCEEDED(rv));
|
2014-07-26 04:52:00 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* gAppData is required by nsXULAppInfo. The manifest parser
|
|
|
|
* evaluate flags with the information from nsXULAppInfo.
|
|
|
|
*/
|
|
|
|
gAppData = aAppData;
|
|
|
|
|
|
|
|
PreloadXPT(omnijarFile);
|
|
|
|
|
|
|
|
gAppData = nullptr;
|
|
|
|
}
|
|
|
|
#endif /* MOZ_B2G_LOADER */
|