michael@0: /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- michael@0: * vim: sw=4 ts=4 et : michael@0: * This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "nsXPCOM.h" michael@0: #include "nsXULAppAPI.h" michael@0: michael@0: // FIXME/cjones testing michael@0: #if !defined(OS_WIN) michael@0: #include michael@0: #endif michael@0: michael@0: #ifdef XP_WIN michael@0: #include michael@0: // we want a wmain entry point michael@0: // but we don't want its DLL load protection, because we'll handle it here michael@0: #define XRE_DONT_PROTECT_DLL_LOAD michael@0: #include "nsWindowsWMain.cpp" michael@0: #include "nsSetDllDirectory.h" michael@0: #endif michael@0: michael@0: #if defined(XP_WIN) && defined(MOZ_CONTENT_SANDBOX) michael@0: #include "sandbox/chromium/base/basictypes.h" michael@0: #include "sandbox/win/src/sandbox.h" michael@0: #include "sandbox/win/src/sandbox_factory.h" michael@0: #include "mozilla/sandboxTarget.h" michael@0: #endif michael@0: michael@0: #ifdef MOZ_WIDGET_GONK michael@0: # include michael@0: # include michael@0: michael@0: # include michael@0: michael@0: # ifdef LOGE_IF michael@0: # undef LOGE_IF michael@0: # endif michael@0: michael@0: # include michael@0: # define LOGE_IF(cond, ...) \ michael@0: ( (CONDITION(cond)) \ michael@0: ? ((void)__android_log_print(ANDROID_LOG_ERROR, \ michael@0: "Gecko:MozillaRntimeMain", __VA_ARGS__)) \ michael@0: : (void)0 ) michael@0: michael@0: #endif michael@0: michael@0: #ifdef MOZ_NUWA_PROCESS michael@0: #include michael@0: #include "ipc/Nuwa.h" michael@0: #endif michael@0: michael@0: #ifdef MOZ_WIDGET_GONK michael@0: static void michael@0: InitializeBinder(void *aDummy) { michael@0: // Change thread priority to 0 only during calling ProcessState::self(). michael@0: // The priority is registered to binder driver and used for default Binder michael@0: // Thread's priority. michael@0: // To change the process's priority to small value need's root permission. michael@0: int curPrio = getpriority(PRIO_PROCESS, 0); michael@0: int err = setpriority(PRIO_PROCESS, 0, 0); michael@0: MOZ_ASSERT(!err); michael@0: LOGE_IF(err, "setpriority failed. Current process needs root permission."); michael@0: android::ProcessState::self()->startThreadPool(); michael@0: setpriority(PRIO_PROCESS, 0, curPrio); michael@0: } michael@0: #endif michael@0: michael@0: #if defined(XP_WIN) && defined(MOZ_CONTENT_SANDBOX) michael@0: static bool gIsSandboxEnabled = false; michael@0: void StartSandboxCallback() michael@0: { michael@0: if (gIsSandboxEnabled) { michael@0: sandbox::TargetServices* target_service = michael@0: sandbox::SandboxFactory::GetTargetServices(); michael@0: target_service->LowerToken(); michael@0: } michael@0: } michael@0: #endif michael@0: michael@0: int michael@0: main(int argc, char* argv[]) michael@0: { michael@0: bool isNuwa = false; michael@0: for (int i = 1; i < argc; i++) { michael@0: isNuwa |= strcmp(argv[i], "-nuwa") == 0; michael@0: #if defined(XP_WIN) && defined(MOZ_CONTENT_SANDBOX) michael@0: gIsSandboxEnabled |= strcmp(argv[i], "-sandbox") == 0; michael@0: #endif michael@0: } michael@0: michael@0: #ifdef MOZ_NUWA_PROCESS michael@0: if (isNuwa) { michael@0: PrepareNuwaProcess(); michael@0: } michael@0: #endif michael@0: michael@0: #ifdef MOZ_WIDGET_GONK michael@0: // This creates a ThreadPool for binder ipc. A ThreadPool is necessary to michael@0: // receive binder calls, though not necessary to send binder calls. michael@0: // ProcessState::Self() also needs to be called once on the main thread to michael@0: // register the main thread with the binder driver. michael@0: michael@0: #ifdef MOZ_NUWA_PROCESS michael@0: if (!isNuwa) { michael@0: InitializeBinder(nullptr); michael@0: } else { michael@0: NuwaAddFinalConstructor(&InitializeBinder, nullptr); michael@0: } michael@0: #else michael@0: InitializeBinder(nullptr); michael@0: #endif michael@0: #endif michael@0: michael@0: // Check for the absolute minimum number of args we need to move michael@0: // forward here. We expect the last arg to be the child process type. michael@0: if (argc < 1) michael@0: return 3; michael@0: GeckoProcessType proctype = XRE_StringToChildProcessType(argv[--argc]); michael@0: michael@0: #ifdef XP_WIN michael@0: // For plugins, this is done in PluginProcessChild::Init, as we need to michael@0: // avoid it for unsupported plugins. See PluginProcessChild::Init for michael@0: // the details. michael@0: if (proctype != GeckoProcessType_Plugin) { michael@0: mozilla::SanitizeEnvironmentVariables(); michael@0: SetDllDirectory(L""); michael@0: } michael@0: michael@0: #ifdef MOZ_CONTENT_SANDBOX michael@0: if (gIsSandboxEnabled) { michael@0: sandbox::TargetServices* target_service = michael@0: sandbox::SandboxFactory::GetTargetServices(); michael@0: if (!target_service) { michael@0: return 1; michael@0: } michael@0: michael@0: sandbox::ResultCode result = target_service->Init(); michael@0: if (result != sandbox::SBOX_ALL_OK) { michael@0: return 2; michael@0: } michael@0: mozilla::SandboxTarget::Instance()->SetStartSandboxCallback(StartSandboxCallback); michael@0: } michael@0: #endif michael@0: #endif michael@0: michael@0: nsresult rv = XRE_InitChildProcess(argc, argv, proctype); michael@0: NS_ENSURE_SUCCESS(rv, 1); michael@0: michael@0: return 0; michael@0: }