2011-12-06 15:52:26 -08:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2012-05-21 04:12:37 -07: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/. */
|
2011-12-06 15:52:26 -08:00
|
|
|
|
2012-02-21 23:12:15 -08:00
|
|
|
#include "nsXULAppAPI.h"
|
2011-12-06 15:52:26 -08:00
|
|
|
#include "application.ini.h"
|
|
|
|
#include "nsXPCOMGlue.h"
|
|
|
|
#if defined(XP_WIN)
|
|
|
|
#include <windows.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#elif defined(XP_UNIX)
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <sys/resource.h>
|
2012-09-28 16:11:03 -07:00
|
|
|
#include <unistd.h>
|
2011-12-06 15:52:26 -08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdarg.h>
|
2014-07-30 00:24:00 -07:00
|
|
|
#include <string.h>
|
2011-12-06 15:52:26 -08:00
|
|
|
|
|
|
|
#include "nsCOMPtr.h"
|
2012-06-05 19:08:30 -07:00
|
|
|
#include "nsIFile.h"
|
2011-12-06 15:52:26 -08:00
|
|
|
#include "nsStringGlue.h"
|
|
|
|
|
|
|
|
#ifdef XP_WIN
|
|
|
|
// we want a wmain entry point
|
2014-08-27 19:53:38 -07:00
|
|
|
#define XRE_DONT_SUPPORT_XPSP2 // See https://bugzil.la/1023941#c32
|
2011-12-06 15:52:26 -08:00
|
|
|
#include "nsWindowsWMain.cpp"
|
|
|
|
#define snprintf _snprintf
|
|
|
|
#define strcasecmp _stricmp
|
|
|
|
#endif
|
2012-11-28 10:43:19 -08:00
|
|
|
|
|
|
|
#ifdef MOZ_WIDGET_GONK
|
2013-05-07 20:58:22 -07:00
|
|
|
#include "GonkDisplay.h"
|
2012-11-28 10:43:19 -08:00
|
|
|
#endif
|
|
|
|
|
2011-12-06 15:52:26 -08:00
|
|
|
#include "BinaryPath.h"
|
|
|
|
|
|
|
|
#include "nsXPCOMPrivate.h" // for MAXPATHLEN and XPCOM_DLL
|
|
|
|
|
2013-03-08 11:43:32 -08:00
|
|
|
#ifdef MOZ_WIDGET_GONK
|
|
|
|
# include <binder/ProcessState.h>
|
|
|
|
#endif
|
|
|
|
|
2011-12-06 15:52:26 -08:00
|
|
|
#include "mozilla/Telemetry.h"
|
2013-11-12 05:31:32 -08:00
|
|
|
#include "mozilla/WindowsDllBlocklist.h"
|
2011-12-06 15:52:26 -08:00
|
|
|
|
|
|
|
static void Output(const char *fmt, ... )
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
va_start(ap, fmt);
|
|
|
|
|
|
|
|
#if defined(XP_WIN) && !MOZ_WINCONSOLE
|
2014-01-04 07:02:17 -08:00
|
|
|
char16_t msg[2048];
|
2011-12-06 15:52:26 -08:00
|
|
|
_vsnwprintf(msg, sizeof(msg)/sizeof(msg[0]), NS_ConvertUTF8toUTF16(fmt).get(), ap);
|
2013-04-02 17:20:15 -07:00
|
|
|
MessageBoxW(nullptr, msg, L"XULRunner", MB_OK | MB_ICONERROR);
|
2011-12-06 15:52:26 -08:00
|
|
|
#else
|
|
|
|
vfprintf(stderr, fmt, ap);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
va_end(ap);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return true if |arg| matches the given argument name.
|
|
|
|
*/
|
|
|
|
static bool IsArg(const char* arg, const char* s)
|
|
|
|
{
|
|
|
|
if (*arg == '-')
|
|
|
|
{
|
|
|
|
if (*++arg == '-')
|
|
|
|
++arg;
|
|
|
|
return !strcasecmp(arg, s);
|
|
|
|
}
|
|
|
|
|
2014-02-10 14:57:01 -08:00
|
|
|
#if defined(XP_WIN)
|
2011-12-06 15:52:26 -08:00
|
|
|
if (*arg == '/')
|
|
|
|
return !strcasecmp(++arg, s);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A helper class which calls NS_LogInit/NS_LogTerm in its scope.
|
|
|
|
*/
|
|
|
|
class ScopedLogging
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
ScopedLogging() { NS_LogInit(); }
|
|
|
|
~ScopedLogging() { NS_LogTerm(); }
|
|
|
|
};
|
|
|
|
|
|
|
|
XRE_GetFileFromPathType XRE_GetFileFromPath;
|
|
|
|
XRE_CreateAppDataType XRE_CreateAppData;
|
|
|
|
XRE_FreeAppDataType XRE_FreeAppData;
|
|
|
|
XRE_TelemetryAccumulateType XRE_TelemetryAccumulate;
|
|
|
|
XRE_mainType XRE_main;
|
|
|
|
|
|
|
|
static const nsDynamicFunctionLoad kXULFuncs[] = {
|
|
|
|
{ "XRE_GetFileFromPath", (NSFuncPtr*) &XRE_GetFileFromPath },
|
|
|
|
{ "XRE_CreateAppData", (NSFuncPtr*) &XRE_CreateAppData },
|
|
|
|
{ "XRE_FreeAppData", (NSFuncPtr*) &XRE_FreeAppData },
|
|
|
|
{ "XRE_TelemetryAccumulate", (NSFuncPtr*) &XRE_TelemetryAccumulate },
|
|
|
|
{ "XRE_main", (NSFuncPtr*) &XRE_main },
|
2012-07-30 07:20:58 -07:00
|
|
|
{ nullptr, nullptr }
|
2011-12-06 15:52:26 -08:00
|
|
|
};
|
|
|
|
|
2012-02-14 10:03:31 -08:00
|
|
|
static int do_main(int argc, char* argv[])
|
2011-12-06 15:52:26 -08:00
|
|
|
{
|
2012-06-05 19:08:30 -07:00
|
|
|
nsCOMPtr<nsIFile> appini;
|
2011-12-06 15:52:26 -08:00
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
// Allow firefox.exe to launch XULRunner apps via -app <application.ini>
|
|
|
|
// Note that -app must be the *first* argument.
|
|
|
|
const char *appDataFile = getenv("XUL_APP_FILE");
|
|
|
|
if (appDataFile && *appDataFile) {
|
|
|
|
rv = XRE_GetFileFromPath(appDataFile, getter_AddRefs(appini));
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
Output("Invalid path found: '%s'", appDataFile);
|
|
|
|
return 255;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (argc > 1 && IsArg(argv[1], "app")) {
|
|
|
|
if (argc == 2) {
|
|
|
|
Output("Incorrect number of arguments passed to -app");
|
|
|
|
return 255;
|
|
|
|
}
|
|
|
|
|
|
|
|
rv = XRE_GetFileFromPath(argv[2], getter_AddRefs(appini));
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
Output("application.ini path not recognized: '%s'", argv[2]);
|
|
|
|
return 255;
|
|
|
|
}
|
|
|
|
|
|
|
|
char appEnv[MAXPATHLEN];
|
|
|
|
snprintf(appEnv, MAXPATHLEN, "XUL_APP_FILE=%s", argv[2]);
|
|
|
|
if (putenv(appEnv)) {
|
|
|
|
Output("Couldn't set %s.\n", appEnv);
|
|
|
|
return 255;
|
|
|
|
}
|
|
|
|
argv[2] = argv[0];
|
|
|
|
argv += 2;
|
|
|
|
argc -= 2;
|
|
|
|
}
|
|
|
|
|
2012-11-28 10:43:19 -08:00
|
|
|
#ifdef MOZ_WIDGET_GONK
|
|
|
|
/* Called to start the boot animation */
|
2013-05-07 20:58:22 -07:00
|
|
|
(void) mozilla::GetGonkDisplay();
|
2012-11-28 10:43:19 -08:00
|
|
|
#endif
|
|
|
|
|
2011-12-06 15:52:26 -08:00
|
|
|
if (appini) {
|
|
|
|
nsXREAppData *appData;
|
|
|
|
rv = XRE_CreateAppData(appini, &appData);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
Output("Couldn't read application.ini");
|
|
|
|
return 255;
|
|
|
|
}
|
2012-05-25 03:01:29 -07:00
|
|
|
int result = XRE_main(argc, argv, appData, 0);
|
2011-12-06 15:52:26 -08:00
|
|
|
XRE_FreeAppData(appData);
|
2012-02-14 10:03:31 -08:00
|
|
|
return result;
|
2011-12-06 15:52:26 -08:00
|
|
|
}
|
|
|
|
|
2012-05-25 03:01:29 -07:00
|
|
|
return XRE_main(argc, argv, &sAppData, 0);
|
2011-12-06 15:52:26 -08:00
|
|
|
}
|
|
|
|
|
2014-07-30 00:24:00 -07:00
|
|
|
#ifdef MOZ_B2G_LOADER
|
|
|
|
/*
|
|
|
|
* The main() in B2GLoader.cpp is the new main function instead of the
|
|
|
|
* main() here if it is enabled. So, rename it to b2g_man().
|
|
|
|
*/
|
|
|
|
#define main b2g_main
|
|
|
|
#define _CONST const
|
|
|
|
#else
|
|
|
|
#define _CONST
|
|
|
|
#endif
|
|
|
|
|
|
|
|
int main(int argc, _CONST char* argv[])
|
2011-12-06 15:52:26 -08:00
|
|
|
{
|
2014-07-30 00:24:00 -07:00
|
|
|
#ifndef MOZ_B2G_LOADER
|
2011-12-06 15:52:26 -08:00
|
|
|
char exePath[MAXPATHLEN];
|
2014-07-30 00:24:00 -07:00
|
|
|
#endif
|
2011-12-06 15:52:26 -08:00
|
|
|
|
2013-03-08 11:43:32 -08:00
|
|
|
#ifdef MOZ_WIDGET_GONK
|
|
|
|
// This creates a ThreadPool for binder ipc. A ThreadPool is necessary to
|
|
|
|
// receive binder calls, though not necessary to send binder calls.
|
|
|
|
// ProcessState::Self() also needs to be called once on the main thread to
|
|
|
|
// register the main thread with the binder driver.
|
|
|
|
android::ProcessState::self()->startThreadPool();
|
|
|
|
#endif
|
|
|
|
|
2014-07-30 00:24:00 -07:00
|
|
|
nsresult rv;
|
|
|
|
#ifndef MOZ_B2G_LOADER
|
|
|
|
rv = mozilla::BinaryPath::Get(argv[0], exePath);
|
2011-12-06 15:52:26 -08:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
Output("Couldn't calculate the application directory.\n");
|
|
|
|
return 255;
|
|
|
|
}
|
|
|
|
|
|
|
|
char *lastSlash = strrchr(exePath, XPCOM_FILE_PATH_SEPARATOR[0]);
|
2012-09-05 16:52:54 -07:00
|
|
|
if (!lastSlash || ((lastSlash - exePath) + sizeof(XPCOM_DLL) + 1 > MAXPATHLEN))
|
2011-12-06 15:52:26 -08:00
|
|
|
return 255;
|
|
|
|
|
|
|
|
strcpy(++lastSlash, XPCOM_DLL);
|
2014-07-30 00:24:00 -07:00
|
|
|
#endif // MOZ_B2G_LOADER
|
2011-12-06 15:52:26 -08:00
|
|
|
|
2012-09-28 16:11:03 -07:00
|
|
|
#if defined(XP_UNIX)
|
|
|
|
// If the b2g app is launched from adb shell, then the shell will wind
|
|
|
|
// up being the process group controller. This means that we can't send
|
|
|
|
// signals to the process group (useful for profiling).
|
|
|
|
// We ignore the return value since setsid() fails if we're already the
|
|
|
|
// process group controller (the normal situation).
|
|
|
|
(void)setsid();
|
|
|
|
#endif
|
|
|
|
|
2011-12-06 15:52:26 -08:00
|
|
|
int gotCounters;
|
|
|
|
#if defined(XP_UNIX)
|
|
|
|
struct rusage initialRUsage;
|
|
|
|
gotCounters = !getrusage(RUSAGE_SELF, &initialRUsage);
|
|
|
|
#elif defined(XP_WIN)
|
|
|
|
IO_COUNTERS ioCounters;
|
|
|
|
gotCounters = GetProcessIoCounters(GetCurrentProcess(), &ioCounters);
|
|
|
|
#endif
|
|
|
|
|
2013-11-12 05:31:32 -08:00
|
|
|
#ifdef HAS_DLL_BLOCKLIST
|
|
|
|
DllBlocklist_Initialize();
|
|
|
|
#endif
|
|
|
|
|
2014-07-30 00:24:00 -07:00
|
|
|
// B2G loader has already initialized Gecko so we can't initialize
|
|
|
|
// it again here.
|
|
|
|
#ifndef MOZ_B2G_LOADER
|
2012-08-02 07:50:29 -07:00
|
|
|
// We do this because of data in bug 771745
|
|
|
|
XPCOMGlueEnablePreload();
|
2011-12-06 15:52:26 -08:00
|
|
|
|
|
|
|
rv = XPCOMGlueStartup(exePath);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
Output("Couldn't load XPCOM.\n");
|
|
|
|
return 255;
|
|
|
|
}
|
|
|
|
// Reset exePath so that it is the directory name and not the xpcom dll name
|
|
|
|
*lastSlash = 0;
|
2014-07-30 00:24:00 -07:00
|
|
|
#endif // MOZ_B2G_LOADER
|
2011-12-06 15:52:26 -08:00
|
|
|
|
|
|
|
rv = XPCOMGlueLoadXULFunctions(kXULFuncs);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
Output("Couldn't load XRE functions.\n");
|
|
|
|
return 255;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (gotCounters) {
|
|
|
|
#if defined(XP_WIN)
|
|
|
|
XRE_TelemetryAccumulate(mozilla::Telemetry::EARLY_GLUESTARTUP_READ_OPS,
|
|
|
|
int(ioCounters.ReadOperationCount));
|
|
|
|
XRE_TelemetryAccumulate(mozilla::Telemetry::EARLY_GLUESTARTUP_READ_TRANSFER,
|
|
|
|
int(ioCounters.ReadTransferCount / 1024));
|
|
|
|
IO_COUNTERS newIoCounters;
|
|
|
|
if (GetProcessIoCounters(GetCurrentProcess(), &newIoCounters)) {
|
|
|
|
XRE_TelemetryAccumulate(mozilla::Telemetry::GLUESTARTUP_READ_OPS,
|
|
|
|
int(newIoCounters.ReadOperationCount - ioCounters.ReadOperationCount));
|
|
|
|
XRE_TelemetryAccumulate(mozilla::Telemetry::GLUESTARTUP_READ_TRANSFER,
|
|
|
|
int((newIoCounters.ReadTransferCount - ioCounters.ReadTransferCount) / 1024));
|
|
|
|
}
|
|
|
|
#elif defined(XP_UNIX)
|
|
|
|
XRE_TelemetryAccumulate(mozilla::Telemetry::EARLY_GLUESTARTUP_HARD_FAULTS,
|
|
|
|
int(initialRUsage.ru_majflt));
|
|
|
|
struct rusage newRUsage;
|
|
|
|
if (!getrusage(RUSAGE_SELF, &newRUsage)) {
|
|
|
|
XRE_TelemetryAccumulate(mozilla::Telemetry::GLUESTARTUP_HARD_FAULTS,
|
|
|
|
int(newRUsage.ru_majflt - initialRUsage.ru_majflt));
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
int result;
|
|
|
|
{
|
|
|
|
ScopedLogging log;
|
2014-07-30 00:24:00 -07:00
|
|
|
char **_argv;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Duplicate argument vector to conform non-const argv of
|
|
|
|
* do_main() since XRE_main() is very stupid with non-const argv.
|
|
|
|
*/
|
|
|
|
_argv = new char *[argc + 1];
|
|
|
|
for (int i = 0; i < argc; i++) {
|
2014-09-04 20:21:24 -07:00
|
|
|
size_t len = strlen(argv[i]) + 1;
|
|
|
|
_argv[i] = new char[len];
|
2014-07-30 00:24:00 -07:00
|
|
|
MOZ_ASSERT(_argv[i] != nullptr);
|
2014-09-04 20:21:24 -07:00
|
|
|
memcpy(_argv[i], argv[i], len);
|
2014-07-30 00:24:00 -07:00
|
|
|
}
|
|
|
|
_argv[argc] = nullptr;
|
|
|
|
|
|
|
|
result = do_main(argc, _argv);
|
|
|
|
|
|
|
|
for (int i = 0; i < argc; i++) {
|
2014-09-04 20:21:24 -07:00
|
|
|
delete[] _argv[i];
|
2014-07-30 00:24:00 -07:00
|
|
|
}
|
|
|
|
delete[] _argv;
|
2011-12-06 15:52:26 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|