2007-03-22 10:30:00 -07:00
|
|
|
/* ***** BEGIN LICENSE BLOCK *****
|
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
|
|
|
*
|
|
|
|
* The contents of this file are subject to the Mozilla Public License Version
|
|
|
|
* 1.1 (the "License"); you may not use this file except in compliance with
|
|
|
|
* the License. You may obtain a copy of the License at
|
|
|
|
* http://www.mozilla.org/MPL/
|
|
|
|
*
|
|
|
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* License.
|
|
|
|
*
|
|
|
|
* The Original Code is Mozilla libXUL embedding.
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is
|
|
|
|
* Benjamin Smedberg <benjamin@smedbergs.us>
|
|
|
|
*
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 2005
|
|
|
|
* the Mozilla Foundation. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
|
|
|
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
|
|
|
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
|
|
|
* in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
* of those above. If you wish to allow use of your version of this file only
|
|
|
|
* under the terms of either the GPL or the LGPL, and not to allow others to
|
|
|
|
* use your version of this file under the terms of the MPL, indicate your
|
|
|
|
* decision by deleting the provisions above and replace them with the notice
|
|
|
|
* and other provisions required by the GPL or the LGPL. If you do not delete
|
|
|
|
* the provisions above, a recipient may use your version of this file under
|
|
|
|
* the terms of any one of the MPL, the GPL or the LGPL.
|
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
|
|
|
|
2009-10-19 06:55:03 -07:00
|
|
|
#ifdef MOZ_IPC
|
2009-08-28 16:16:19 -07:00
|
|
|
#include "base/basictypes.h"
|
2009-10-19 06:55:03 -07:00
|
|
|
#endif
|
2009-08-28 16:16:19 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsXULAppAPI.h"
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
2009-10-19 09:28:42 -07:00
|
|
|
#if defined(MOZ_WIDGET_GTK2)
|
|
|
|
#include <glib.h>
|
|
|
|
#endif
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-06-30 12:21:25 -07:00
|
|
|
#include "prenv.h"
|
|
|
|
|
|
|
|
#include "nsIAppShell.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsIAppStartupNotifier.h"
|
|
|
|
#include "nsIDirectoryService.h"
|
|
|
|
#include "nsILocalFile.h"
|
|
|
|
#include "nsIToolkitChromeRegistry.h"
|
2009-06-30 12:21:25 -07:00
|
|
|
#include "nsIToolkitProfile.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-01-12 13:14:38 -08:00
|
|
|
#if defined(OS_LINUX)
|
|
|
|
# define XP_LINUX
|
|
|
|
#endif
|
|
|
|
|
2010-02-03 14:17:09 -08:00
|
|
|
#ifdef XP_WIN
|
|
|
|
#include <process.h>
|
|
|
|
#endif
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsAppDirectoryServiceDefs.h"
|
|
|
|
#include "nsAppRunner.h"
|
2009-07-10 23:33:10 -07:00
|
|
|
#include "nsAutoRef.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsDirectoryServiceDefs.h"
|
2010-01-12 13:14:38 -08:00
|
|
|
#include "nsExceptionHandler.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsString.h"
|
2009-06-30 12:21:25 -07:00
|
|
|
#include "nsThreadUtils.h"
|
|
|
|
#include "nsWidgetsCID.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsXREDirProvider.h"
|
2009-06-30 12:21:25 -07:00
|
|
|
|
2009-07-20 12:04:52 -07:00
|
|
|
#ifdef MOZ_IPC
|
2010-02-24 14:25:16 -08:00
|
|
|
#include "nsX11ErrorHandler.h"
|
2009-07-20 12:04:52 -07:00
|
|
|
#include "base/at_exit.h"
|
|
|
|
#include "base/command_line.h"
|
|
|
|
#include "base/message_loop.h"
|
2009-10-27 14:52:37 -07:00
|
|
|
#include "base/process_util.h"
|
2009-07-20 12:04:52 -07:00
|
|
|
#include "chrome/common/child_process.h"
|
2010-04-20 11:43:51 -07:00
|
|
|
#include "chrome/common/notification_service.h"
|
2009-07-20 12:04:52 -07:00
|
|
|
|
2010-02-03 14:17:09 -08:00
|
|
|
#include "mozilla/ipc/BrowserProcessSubThread.h"
|
2010-05-10 21:18:00 -07:00
|
|
|
#include "mozilla/ipc/GeckoChildProcessHost.h"
|
|
|
|
#include "mozilla/ipc/IOThreadChild.h"
|
|
|
|
#include "mozilla/ipc/ProcessChild.h"
|
2009-06-30 12:21:25 -07:00
|
|
|
#include "ScopedXREEmbed.h"
|
|
|
|
|
2010-06-25 08:00:35 -07:00
|
|
|
#include "mozilla/jetpack/JetpackProcessChild.h"
|
2010-05-10 21:18:00 -07:00
|
|
|
#include "mozilla/plugins/PluginProcessChild.h"
|
2010-07-19 11:33:33 -07:00
|
|
|
#include "mozilla/dom/ContentProcess.h"
|
|
|
|
#include "mozilla/dom/ContentParent.h"
|
|
|
|
#include "mozilla/dom/ContentChild.h"
|
2009-06-30 12:21:25 -07:00
|
|
|
|
2009-11-06 12:43:39 -08:00
|
|
|
#include "mozilla/jsipc/ContextWrapperParent.h"
|
|
|
|
|
2009-07-10 23:33:10 -07:00
|
|
|
#include "mozilla/ipc/TestShellParent.h"
|
|
|
|
#include "mozilla/ipc/XPCShellEnvironment.h"
|
|
|
|
|
2009-10-06 11:02:26 -07:00
|
|
|
#ifdef MOZ_IPDL_TESTS
|
|
|
|
#include "mozilla/_ipdltest/IPDLUnitTests.h"
|
2010-05-10 21:18:00 -07:00
|
|
|
#include "mozilla/_ipdltest/IPDLUnitTestProcessChild.h"
|
2009-10-06 11:02:26 -07:00
|
|
|
|
2010-05-10 21:18:00 -07:00
|
|
|
using mozilla::_ipdltest::IPDLUnitTestProcessChild;
|
2009-10-06 11:02:26 -07:00
|
|
|
#endif // ifdef MOZ_IPDL_TESTS
|
|
|
|
|
2009-09-02 17:18:27 -07:00
|
|
|
using mozilla::ipc::BrowserProcessSubThread;
|
2010-05-10 21:18:00 -07:00
|
|
|
using mozilla::ipc::GeckoChildProcessHost;
|
|
|
|
using mozilla::ipc::IOThreadChild;
|
|
|
|
using mozilla::ipc::ProcessChild;
|
2009-06-30 12:21:25 -07:00
|
|
|
using mozilla::ipc::ScopedXREEmbed;
|
|
|
|
|
2010-06-25 08:00:35 -07:00
|
|
|
using mozilla::jetpack::JetpackProcessChild;
|
2010-05-10 21:18:00 -07:00
|
|
|
using mozilla::plugins::PluginProcessChild;
|
2010-07-19 11:33:33 -07:00
|
|
|
using mozilla::dom::ContentProcess;
|
|
|
|
using mozilla::dom::ContentParent;
|
|
|
|
using mozilla::dom::ContentChild;
|
2009-11-06 12:43:39 -08:00
|
|
|
|
|
|
|
using mozilla::jsipc::PContextWrapperParent;
|
|
|
|
using mozilla::jsipc::ContextWrapperParent;
|
|
|
|
|
2009-07-10 23:33:10 -07:00
|
|
|
using mozilla::ipc::TestShellParent;
|
2009-08-28 16:16:19 -07:00
|
|
|
using mozilla::ipc::TestShellCommandParent;
|
2009-07-10 23:33:10 -07:00
|
|
|
using mozilla::ipc::XPCShellEnvironment;
|
2009-12-14 14:13:00 -08:00
|
|
|
|
|
|
|
using mozilla::startup::sChildProcessType;
|
2009-07-20 12:04:52 -07:00
|
|
|
#endif
|
2009-06-30 12:21:25 -07:00
|
|
|
|
|
|
|
static NS_DEFINE_CID(kAppShellCID, NS_APPSHELL_CID);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-05-14 17:25:10 -07:00
|
|
|
#ifdef XP_WIN
|
|
|
|
static const PRUnichar kShellLibraryName[] = L"shell32.dll";
|
|
|
|
#endif
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
nsresult
|
|
|
|
XRE_LockProfileDirectory(nsILocalFile* aDirectory,
|
|
|
|
nsISupports* *aLockObject)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIProfileLock> lock;
|
|
|
|
|
|
|
|
nsresult rv = NS_LockProfilePath(aDirectory, nsnull, nsnull,
|
|
|
|
getter_AddRefs(lock));
|
|
|
|
if (NS_SUCCEEDED(rv))
|
|
|
|
NS_ADDREF(*aLockObject = lock);
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
static PRInt32 sInitCounter;
|
|
|
|
|
|
|
|
nsresult
|
2010-06-10 11:11:11 -07:00
|
|
|
XRE_InitEmbedding2(nsILocalFile *aLibXULDirectory,
|
|
|
|
nsILocalFile *aAppDirectory,
|
|
|
|
nsIDirectoryServiceProvider *aAppDirProvider)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
// Initialize some globals to make nsXREDirProvider happy
|
|
|
|
static char* kNullCommandLine[] = { nsnull };
|
|
|
|
gArgv = kNullCommandLine;
|
|
|
|
gArgc = 0;
|
|
|
|
|
|
|
|
NS_ENSURE_ARG(aLibXULDirectory);
|
|
|
|
|
|
|
|
if (++sInitCounter > 1) // XXXbsmedberg is this really the right solution?
|
|
|
|
return NS_OK;
|
|
|
|
|
|
|
|
if (!aAppDirectory)
|
|
|
|
aAppDirectory = aLibXULDirectory;
|
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
new nsXREDirProvider; // This sets gDirServiceProvider
|
|
|
|
if (!gDirServiceProvider)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
|
|
|
rv = gDirServiceProvider->Initialize(aAppDirectory, aLibXULDirectory,
|
|
|
|
aAppDirProvider);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
2010-06-10 11:11:11 -07:00
|
|
|
rv = NS_InitXPCOM2(nsnull, aAppDirectory, gDirServiceProvider);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
2010-03-13 00:03:27 -08: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.
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
nsCOMPtr<nsIObserver> startupNotifier
|
|
|
|
(do_CreateInstance(NS_APPSTARTUPNOTIFIER_CONTRACTID));
|
|
|
|
if (!startupNotifier)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
startupNotifier->Observe(nsnull, APPSTARTUP_TOPIC, nsnull);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
XRE_NotifyProfile()
|
|
|
|
{
|
|
|
|
NS_ASSERTION(gDirServiceProvider, "XRE_InitEmbedding was not called!");
|
|
|
|
gDirServiceProvider->DoStartup();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
XRE_TermEmbedding()
|
|
|
|
{
|
|
|
|
if (--sInitCounter != 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
NS_ASSERTION(gDirServiceProvider,
|
|
|
|
"XRE_TermEmbedding without XRE_InitEmbedding");
|
|
|
|
|
|
|
|
gDirServiceProvider->DoShutdown();
|
|
|
|
NS_ShutdownXPCOM(nsnull);
|
|
|
|
delete gDirServiceProvider;
|
|
|
|
}
|
2009-06-30 12:21:25 -07:00
|
|
|
|
2009-07-10 12:03:09 -07:00
|
|
|
const char*
|
2009-08-19 10:09:51 -07:00
|
|
|
XRE_ChildProcessTypeToString(GeckoProcessType aProcessType)
|
2009-07-10 12:03:09 -07:00
|
|
|
{
|
2009-08-19 10:09:51 -07:00
|
|
|
return (aProcessType < GeckoProcessType_End) ?
|
|
|
|
kGeckoProcessTypeString[aProcessType] : nsnull;
|
2009-07-10 12:03:09 -07:00
|
|
|
}
|
|
|
|
|
2009-08-19 10:09:51 -07:00
|
|
|
GeckoProcessType
|
2009-07-10 12:03:09 -07:00
|
|
|
XRE_StringToChildProcessType(const char* aProcessTypeString)
|
|
|
|
{
|
|
|
|
for (int i = 0;
|
2009-08-19 10:09:51 -07:00
|
|
|
i < (int) NS_ARRAY_LENGTH(kGeckoProcessTypeString);
|
2009-07-10 12:03:09 -07:00
|
|
|
++i) {
|
2009-08-19 10:09:51 -07:00
|
|
|
if (!strcmp(kGeckoProcessTypeString[i], aProcessTypeString)) {
|
|
|
|
return static_cast<GeckoProcessType>(i);
|
2009-07-10 12:03:09 -07:00
|
|
|
}
|
|
|
|
}
|
2009-08-19 10:09:51 -07:00
|
|
|
return GeckoProcessType_Invalid;
|
2009-07-10 12:03:09 -07:00
|
|
|
}
|
|
|
|
|
2009-07-20 12:04:52 -07:00
|
|
|
#ifdef MOZ_IPC
|
2009-12-14 14:13:00 -08:00
|
|
|
namespace mozilla {
|
|
|
|
namespace startup {
|
|
|
|
GeckoProcessType sChildProcessType = GeckoProcessType_Default;
|
|
|
|
}
|
|
|
|
}
|
2009-08-19 10:09:51 -07:00
|
|
|
|
2010-01-12 13:14:38 -08:00
|
|
|
#if defined(MOZ_CRASHREPORTER)
|
2010-03-18 19:01:10 -07: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
|
|
|
|
PRBool
|
2010-03-24 14:22:01 -07:00
|
|
|
XRE_TakeMinidumpForChild(PRUint32 aChildPid, nsILocalFile** aDump)
|
2010-03-18 19:01:10 -07:00
|
|
|
{
|
|
|
|
return CrashReporter::TakeMinidumpForChild(aChildPid, aDump);
|
|
|
|
}
|
|
|
|
|
2010-03-30 10:32:53 -07:00
|
|
|
#if !defined(XP_MACOSX)
|
2010-01-12 13:14:38 -08:00
|
|
|
PRBool
|
|
|
|
XRE_SetRemoteExceptionHandler(const char* aPipe/*= 0*/)
|
|
|
|
{
|
|
|
|
#if defined(XP_WIN)
|
|
|
|
return CrashReporter::SetRemoteExceptionHandler(nsDependentCString(aPipe));
|
|
|
|
#elif defined(OS_LINUX)
|
|
|
|
return CrashReporter::SetRemoteExceptionHandler();
|
|
|
|
#else
|
|
|
|
# error "OOP crash reporter unsupported on this platform"
|
|
|
|
#endif
|
|
|
|
}
|
2010-03-30 10:32:53 -07:00
|
|
|
#endif // !XP_MACOSX
|
2010-01-12 13:14:38 -08:00
|
|
|
#endif // if defined(MOZ_CRASHREPORTER)
|
|
|
|
|
2010-05-14 17:25:10 -07:00
|
|
|
#if defined(XP_WIN)
|
|
|
|
void
|
|
|
|
SetTaskbarGroupId(const nsString& aId)
|
|
|
|
{
|
|
|
|
typedef HRESULT (WINAPI * SetCurrentProcessExplicitAppUserModelIDPtr)(PCWSTR AppID);
|
|
|
|
|
|
|
|
SetCurrentProcessExplicitAppUserModelIDPtr funcAppUserModelID = nsnull;
|
|
|
|
|
|
|
|
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 12:21:25 -07:00
|
|
|
nsresult
|
|
|
|
XRE_InitChildProcess(int aArgc,
|
|
|
|
char* aArgv[],
|
2009-08-19 10:09:51 -07:00
|
|
|
GeckoProcessType aProcess)
|
2009-06-30 12:21:25 -07:00
|
|
|
{
|
2009-10-27 14:52:37 -07:00
|
|
|
NS_ENSURE_ARG_MIN(aArgc, 2);
|
2009-06-30 12:21:25 -07:00
|
|
|
NS_ENSURE_ARG_POINTER(aArgv);
|
|
|
|
NS_ENSURE_ARG_POINTER(aArgv[0]);
|
|
|
|
|
2009-11-10 11:16:12 -08:00
|
|
|
sChildProcessType = aProcess;
|
2010-01-27 20:23:16 -08:00
|
|
|
|
2010-01-28 22:50:44 -08:00
|
|
|
gArgv = aArgv;
|
|
|
|
gArgc = aArgc;
|
|
|
|
|
2010-01-27 20:23:16 -08:00
|
|
|
SetupErrorHandling(aArgv[0]);
|
2009-11-10 11:16:12 -08:00
|
|
|
|
2009-10-19 09:28:42 -07:00
|
|
|
#if defined(MOZ_WIDGET_GTK2)
|
|
|
|
g_thread_init(NULL);
|
|
|
|
#endif
|
|
|
|
|
2009-06-30 12:21:25 -07:00
|
|
|
if (PR_GetEnv("MOZ_DEBUG_CHILD_PROCESS")) {
|
2009-07-07 09:26:49 -07:00
|
|
|
#ifdef OS_POSIX
|
2009-06-30 12:21:25 -07:00
|
|
|
printf("\n\nCHILDCHILDCHILDCHILD\n debug me @%d\n\n", getpid());
|
|
|
|
sleep(30);
|
2009-07-07 09:26:49 -07:00
|
|
|
#elif defined(OS_WIN)
|
2010-02-03 14:17:09 -08:00
|
|
|
printf("\n\nCHILDCHILDCHILDCHILD\n debug me @%d\n\n", _getpid());
|
2009-07-07 09:26:49 -07:00
|
|
|
Sleep(30000);
|
|
|
|
#endif
|
2009-06-30 12:21:25 -07:00
|
|
|
}
|
|
|
|
|
2009-10-27 14:52:37 -07:00
|
|
|
// child processes launched by GeckoChildProcessHost get this magic
|
|
|
|
// argument appended to their command lines
|
|
|
|
const char* const parentPIDString = aArgv[aArgc-1];
|
|
|
|
NS_ABORT_IF_FALSE(parentPIDString, "NULL parent PID");
|
|
|
|
--aArgc;
|
|
|
|
|
|
|
|
char* end = 0;
|
|
|
|
base::ProcessId parentPID = strtol(parentPIDString, &end, 10);
|
|
|
|
NS_ABORT_IF_FALSE(!*end, "invalid parent PID");
|
2009-11-17 14:18:06 -08:00
|
|
|
|
2009-10-27 14:52:37 -07:00
|
|
|
base::ProcessHandle parentHandle;
|
2009-11-17 14:18:06 -08:00
|
|
|
bool ok = base::OpenProcessHandle(parentPID, &parentHandle);
|
|
|
|
NS_ABORT_IF_FALSE(ok, "can't open handle to parent");
|
2009-10-27 14:52:37 -07:00
|
|
|
|
2010-05-14 17:25:10 -07: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.
|
|
|
|
const char* const appModelUserId = aArgv[aArgc-1];
|
|
|
|
--aArgc;
|
|
|
|
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 12:21:25 -07:00
|
|
|
base::AtExitManager exitManager;
|
2010-04-20 11:43:51 -07:00
|
|
|
NotificationService notificationService;
|
2009-11-03 21:12:18 -08:00
|
|
|
|
2009-11-19 14:57:56 -08:00
|
|
|
NS_LogInit();
|
|
|
|
|
2009-11-03 21:12:18 -08:00
|
|
|
int rv = XRE_InitCommandLine(aArgc, aArgv);
|
2009-11-19 14:57:56 -08:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_LogTerm();
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2009-11-03 21:12:18 -08:00
|
|
|
|
2010-05-13 23:41:14 -07:00
|
|
|
MessageLoop::Type uiLoopType;
|
|
|
|
switch (aProcess) {
|
|
|
|
case GeckoProcessType_Content:
|
|
|
|
// Content processes need the XPCOM/chromium frankenventloop
|
|
|
|
uiLoopType = MessageLoop::TYPE_MOZILLA_CHILD;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
uiLoopType = MessageLoop::TYPE_UI;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2009-06-30 12:21:25 -07:00
|
|
|
{
|
2010-08-05 15:11:23 -07: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;
|
|
|
|
|
|
|
|
switch (aProcess) {
|
|
|
|
case GeckoProcessType_Default:
|
|
|
|
NS_RUNTIMEABORT("This makes no sense");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case GeckoProcessType_Plugin:
|
|
|
|
process = new PluginProcessChild(parentHandle);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case GeckoProcessType_Content:
|
|
|
|
process = new ContentProcess(parentHandle);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case GeckoProcessType_Jetpack:
|
|
|
|
process = new JetpackProcessChild(parentHandle);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case GeckoProcessType_IPDLUnitTest:
|
2009-10-06 11:02:26 -07:00
|
|
|
#ifdef MOZ_IPDL_TESTS
|
2010-08-05 15:11:23 -07:00
|
|
|
process = new IPDLUnitTestProcessChild(parentHandle);
|
2010-05-10 21:18:00 -07:00
|
|
|
#else
|
2010-08-05 15:11:23 -07:00
|
|
|
NS_RUNTIMEABORT("rebuild with --enable-ipdl-tests");
|
2009-10-06 11:02:26 -07:00
|
|
|
#endif
|
2010-08-05 15:11:23 -07:00
|
|
|
break;
|
2009-10-06 11:02:26 -07:00
|
|
|
|
2010-08-05 15:11:23 -07:00
|
|
|
default:
|
|
|
|
NS_RUNTIMEABORT("Unknown main thread class");
|
|
|
|
}
|
2009-06-30 12:21:25 -07:00
|
|
|
|
2010-08-05 15:11:23 -07:00
|
|
|
if (!process->Init()) {
|
|
|
|
NS_LogTerm();
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2009-09-02 17:18:27 -07:00
|
|
|
|
2010-08-05 15:11:23 -07:00
|
|
|
// Run the UI event loop on the main thread.
|
|
|
|
uiMessageLoop.MessageLoop::Run();
|
2009-09-02 17:18:27 -07:00
|
|
|
|
2010-08-05 15:11:23 -07:00
|
|
|
// Allow ProcessChild to clean up after itself before going out of
|
|
|
|
// scope and being deleted
|
|
|
|
process->CleanUp();
|
|
|
|
}
|
2009-06-30 12:21:25 -07:00
|
|
|
}
|
|
|
|
|
2009-11-19 14:57:56 -08:00
|
|
|
NS_LogTerm();
|
2009-12-04 16:15:44 -08:00
|
|
|
return XRE_DeinitCommandLine();
|
2009-06-30 12:21:25 -07:00
|
|
|
}
|
|
|
|
|
2009-09-02 17:18:27 -07:00
|
|
|
MessageLoop*
|
|
|
|
XRE_GetIOMessageLoop()
|
|
|
|
{
|
|
|
|
if (sChildProcessType == GeckoProcessType_Default) {
|
|
|
|
return BrowserProcessSubThread::GetMessageLoop(BrowserProcessSubThread::IO);
|
|
|
|
}
|
2010-05-10 21:18:00 -07:00
|
|
|
return IOThreadChild::message_loop();
|
2009-09-02 17:18:27 -07:00
|
|
|
}
|
|
|
|
|
2009-06-30 12:21:25 -07:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
class MainFunctionRunnable : public nsRunnable
|
|
|
|
{
|
|
|
|
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]);
|
|
|
|
|
2009-11-03 21:12:18 -08:00
|
|
|
int rv = XRE_InitCommandLine(aArgc, aArgv);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return NS_ERROR_FAILURE;
|
2009-08-28 12:46:21 -07:00
|
|
|
|
2009-08-25 16:07:22 -07:00
|
|
|
ScopedXREEmbed embed;
|
2009-06-30 12:21:25 -07: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-04 16:15:44 -08:00
|
|
|
return XRE_DeinitCommandLine();
|
2009-06-30 12:21:25 -07:00
|
|
|
}
|
|
|
|
|
2009-10-06 11:02:26 -07: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 12:46:21 -07:00
|
|
|
nsresult
|
|
|
|
XRE_RunAppShell()
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIAppShell> appShell(do_GetService(kAppShellCID));
|
|
|
|
NS_ENSURE_TRUE(appShell, NS_ERROR_FAILURE);
|
|
|
|
|
|
|
|
return appShell->Run();
|
|
|
|
}
|
|
|
|
|
|
|
|
template<>
|
2010-07-19 11:33:33 -07:00
|
|
|
struct RunnableMethodTraits<ContentChild>
|
2009-08-28 12:46:21 -07:00
|
|
|
{
|
2010-07-19 11:33:33 -07:00
|
|
|
static void RetainCallee(ContentChild* obj) { }
|
|
|
|
static void ReleaseCallee(ContentChild* obj) { }
|
2009-08-28 12:46:21 -07:00
|
|
|
};
|
|
|
|
|
2009-08-25 16:39:30 -07:00
|
|
|
void
|
2009-12-03 00:16:28 -08:00
|
|
|
XRE_ShutdownChildProcess()
|
2009-08-28 12:46:21 -07:00
|
|
|
{
|
2010-02-03 14:17:09 -08:00
|
|
|
NS_ABORT_IF_FALSE(MessageLoopForUI::current(), "Wrong thread!");
|
2009-12-03 00:16:28 -08:00
|
|
|
|
2010-02-03 14:17:09 -08:00
|
|
|
MessageLoop* ioLoop = XRE_GetIOMessageLoop();
|
|
|
|
NS_ABORT_IF_FALSE(!!ioLoop, "Bad shutdown order");
|
2009-12-03 00:16:28 -08:00
|
|
|
|
2010-05-10 21:18:00 -07: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()
|
|
|
|
MessageLoop::current()->Quit();
|
2009-08-28 16:16:19 -07:00
|
|
|
}
|
2009-08-28 12:46:21 -07:00
|
|
|
|
2009-08-28 16:16:19 -07:00
|
|
|
namespace {
|
|
|
|
TestShellParent* gTestShellParent = nsnull;
|
2009-11-06 12:43:39 -08:00
|
|
|
TestShellParent* GetOrCreateTestShellParent()
|
|
|
|
{
|
|
|
|
if (!gTestShellParent) {
|
2010-07-19 11:33:33 -07:00
|
|
|
ContentParent* parent = ContentParent::GetSingleton();
|
2009-11-06 12:43:39 -08:00
|
|
|
NS_ENSURE_TRUE(parent, nsnull);
|
|
|
|
gTestShellParent = parent->CreateTestShell();
|
|
|
|
NS_ENSURE_TRUE(gTestShellParent, nsnull);
|
|
|
|
}
|
|
|
|
return gTestShellParent;
|
|
|
|
}
|
2009-08-28 12:46:21 -07:00
|
|
|
}
|
|
|
|
|
2009-08-28 16:16:19 -07:00
|
|
|
bool
|
|
|
|
XRE_SendTestShellCommand(JSContext* aCx,
|
|
|
|
JSString* aCommand,
|
|
|
|
void* aCallback)
|
2009-08-28 12:46:21 -07:00
|
|
|
{
|
2009-11-06 12:43:39 -08:00
|
|
|
TestShellParent* tsp = GetOrCreateTestShellParent();
|
|
|
|
NS_ENSURE_TRUE(tsp, false);
|
2009-08-28 12:46:21 -07:00
|
|
|
|
2009-08-28 16:16:19 -07:00
|
|
|
nsDependentString command((PRUnichar*)JS_GetStringChars(aCommand),
|
|
|
|
JS_GetStringLength(aCommand));
|
|
|
|
if (!aCallback) {
|
2009-11-06 12:43:39 -08:00
|
|
|
return tsp->SendExecuteCommand(command);
|
2009-08-25 16:39:30 -07:00
|
|
|
}
|
2009-08-28 16:16:19 -07:00
|
|
|
|
|
|
|
TestShellCommandParent* callback = static_cast<TestShellCommandParent*>(
|
2009-11-06 12:43:39 -08:00
|
|
|
tsp->SendPTestShellCommandConstructor(command));
|
2009-08-28 16:16:19 -07:00
|
|
|
NS_ENSURE_TRUE(callback, false);
|
|
|
|
|
|
|
|
jsval callbackVal = *reinterpret_cast<jsval*>(aCallback);
|
|
|
|
NS_ENSURE_TRUE(callback->SetCallback(aCx, callbackVal), false);
|
|
|
|
|
|
|
|
return true;
|
2009-08-25 16:39:30 -07:00
|
|
|
}
|
|
|
|
|
2009-11-06 12:43:39 -08:00
|
|
|
bool
|
|
|
|
XRE_GetChildGlobalObject(JSContext* aCx, JSObject** aGlobalP)
|
|
|
|
{
|
|
|
|
TestShellParent* tsp = GetOrCreateTestShellParent();
|
|
|
|
return tsp && tsp->GetGlobalJSObject(aCx, aGlobalP);
|
|
|
|
}
|
|
|
|
|
2009-11-11 00:34:08 -08:00
|
|
|
bool
|
|
|
|
XRE_ShutdownTestShell()
|
|
|
|
{
|
|
|
|
if (!gTestShellParent)
|
|
|
|
return true;
|
2010-07-19 11:33:33 -07:00
|
|
|
return ContentParent::GetSingleton()->DestroyTestShell(gTestShellParent);
|
2009-11-11 00:34:08 -08:00
|
|
|
}
|
|
|
|
|
2010-02-24 14:25:16 -08:00
|
|
|
#ifdef MOZ_X11
|
|
|
|
void
|
|
|
|
XRE_InstallX11ErrorHandler()
|
|
|
|
{
|
|
|
|
InstallX11ErrorHandler();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2009-10-19 06:55:03 -07:00
|
|
|
#endif // MOZ_IPC
|
|
|
|
|