2007-03-22 10:30:00 -07: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/. */
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-02-21 23:12:15 -08:00
|
|
|
#include "nsXULAppAPI.h"
|
2011-11-21 23:05:59 -08:00
|
|
|
#include "application.ini.h"
|
2011-05-21 23:23:20 -07:00
|
|
|
#include "nsXPCOMGlue.h"
|
2011-05-21 23:24:32 -07:00
|
|
|
#if defined(XP_WIN)
|
2007-03-22 10:30:00 -07:00
|
|
|
#include <windows.h>
|
|
|
|
#include <stdlib.h>
|
2011-05-21 23:24:32 -07:00
|
|
|
#elif defined(XP_UNIX)
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <sys/resource.h>
|
2007-03-22 10:30:00 -07:00
|
|
|
#endif
|
|
|
|
|
2011-12-19 09:47:09 -08:00
|
|
|
#ifdef XP_MACOSX
|
|
|
|
#include "MacQuirks.h"
|
|
|
|
#endif
|
|
|
|
|
2007-07-02 11:20:24 -07:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdarg.h>
|
2012-06-16 10:44:07 -07:00
|
|
|
#include <time.h>
|
2007-07-25 13:24:37 -07:00
|
|
|
|
2007-07-02 11:20:24 -07:00
|
|
|
#include "nsCOMPtr.h"
|
2012-06-05 19:08:30 -07:00
|
|
|
#include "nsIFile.h"
|
2007-07-02 11:20:24 -07:00
|
|
|
#include "nsStringGlue.h"
|
|
|
|
|
2007-12-31 07:15:43 -08:00
|
|
|
#ifdef XP_WIN
|
|
|
|
// we want a wmain entry point
|
|
|
|
#include "nsWindowsWMain.cpp"
|
2011-05-21 23:23:20 -07:00
|
|
|
#define snprintf _snprintf
|
|
|
|
#define strcasecmp _stricmp
|
2007-12-31 07:15:43 -08:00
|
|
|
#endif
|
2011-05-21 23:23:20 -07:00
|
|
|
#include "BinaryPath.h"
|
|
|
|
|
|
|
|
#include "nsXPCOMPrivate.h" // for MAXPATHLEN and XPCOM_DLL
|
2007-12-31 07:15:43 -08:00
|
|
|
|
2011-05-21 23:24:32 -07:00
|
|
|
#include "mozilla/Telemetry.h"
|
|
|
|
|
2007-07-02 11:20:24 -07:00
|
|
|
static void Output(const char *fmt, ... )
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
va_start(ap, fmt);
|
|
|
|
|
2012-07-24 10:04:03 -07:00
|
|
|
#ifndef XP_WIN
|
2007-07-02 11:20:24 -07:00
|
|
|
vfprintf(stderr, fmt, ap);
|
2012-07-24 10:04:03 -07:00
|
|
|
#else
|
|
|
|
char msg[2048];
|
|
|
|
vsnprintf_s(msg, _countof(msg), _TRUNCATE, fmt, ap);
|
|
|
|
|
|
|
|
wchar_t wide_msg[2048];
|
|
|
|
MultiByteToWideChar(CP_UTF8,
|
|
|
|
0,
|
|
|
|
msg,
|
|
|
|
-1,
|
|
|
|
wide_msg,
|
|
|
|
_countof(wide_msg));
|
|
|
|
#if MOZ_WINCONSOLE
|
|
|
|
fwprintf_s(stderr, wide_msg);
|
|
|
|
#else
|
|
|
|
MessageBoxW(NULL, wide_msg, L"Firefox", MB_OK
|
|
|
|
| MB_ICONERROR
|
|
|
|
| MB_SETFOREGROUND);
|
|
|
|
#endif
|
2007-05-25 15:09:29 -07:00
|
|
|
#endif
|
2007-07-02 11:20:24 -07:00
|
|
|
|
|
|
|
va_end(ap);
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2007-07-25 13:24:37 -07:00
|
|
|
/**
|
|
|
|
* Return true if |arg| matches the given argument name.
|
|
|
|
*/
|
2011-09-28 23:19:26 -07:00
|
|
|
static bool IsArg(const char* arg, const char* s)
|
2007-07-25 13:24:37 -07:00
|
|
|
{
|
|
|
|
if (*arg == '-')
|
|
|
|
{
|
|
|
|
if (*++arg == '-')
|
|
|
|
++arg;
|
2011-05-21 23:23:20 -07:00
|
|
|
return !strcasecmp(arg, s);
|
2007-07-25 13:24:37 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#if defined(XP_WIN) || defined(XP_OS2)
|
|
|
|
if (*arg == '/')
|
2011-05-21 23:23:20 -07:00
|
|
|
return !strcasecmp(++arg, s);
|
2007-07-25 13:24:37 -07:00
|
|
|
#endif
|
|
|
|
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2007-07-25 13:24:37 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2007-08-05 16:48:25 -07:00
|
|
|
* A helper class which calls NS_LogInit/NS_LogTerm in its scope.
|
2007-07-25 13:24:37 -07:00
|
|
|
*/
|
|
|
|
class ScopedLogging
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
ScopedLogging() { NS_LogInit(); }
|
|
|
|
~ScopedLogging() { NS_LogTerm(); }
|
|
|
|
};
|
|
|
|
|
2011-05-21 23:23:20 -07:00
|
|
|
XRE_GetFileFromPathType XRE_GetFileFromPath;
|
|
|
|
XRE_CreateAppDataType XRE_CreateAppData;
|
|
|
|
XRE_FreeAppDataType XRE_FreeAppData;
|
|
|
|
#ifdef XRE_HAS_DLL_BLOCKLIST
|
|
|
|
XRE_SetupDllBlocklistType XRE_SetupDllBlocklist;
|
|
|
|
#endif
|
2011-05-21 23:24:32 -07:00
|
|
|
XRE_TelemetryAccumulateType XRE_TelemetryAccumulate;
|
2012-06-14 23:23:08 -07:00
|
|
|
XRE_StartupTimelineRecordType XRE_StartupTimelineRecord;
|
2011-05-21 23:23:20 -07:00
|
|
|
XRE_mainType XRE_main;
|
2011-06-15 09:00:28 -07:00
|
|
|
|
2011-05-21 23:23:20 -07:00
|
|
|
static const nsDynamicFunctionLoad kXULFuncs[] = {
|
|
|
|
{ "XRE_GetFileFromPath", (NSFuncPtr*) &XRE_GetFileFromPath },
|
|
|
|
{ "XRE_CreateAppData", (NSFuncPtr*) &XRE_CreateAppData },
|
|
|
|
{ "XRE_FreeAppData", (NSFuncPtr*) &XRE_FreeAppData },
|
2011-05-21 23:22:27 -07:00
|
|
|
#ifdef XRE_HAS_DLL_BLOCKLIST
|
2011-05-21 23:23:20 -07:00
|
|
|
{ "XRE_SetupDllBlocklist", (NSFuncPtr*) &XRE_SetupDllBlocklist },
|
2011-05-21 23:22:27 -07:00
|
|
|
#endif
|
2011-05-21 23:24:32 -07:00
|
|
|
{ "XRE_TelemetryAccumulate", (NSFuncPtr*) &XRE_TelemetryAccumulate },
|
2012-06-14 23:23:08 -07:00
|
|
|
{ "XRE_StartupTimelineRecord", (NSFuncPtr*) &XRE_StartupTimelineRecord },
|
2011-05-21 23:23:20 -07:00
|
|
|
{ "XRE_main", (NSFuncPtr*) &XRE_main },
|
2012-07-30 07:20:58 -07:00
|
|
|
{ nullptr, nullptr }
|
2011-05-21 23:23:20 -07:00
|
|
|
};
|
2011-05-21 23:22:27 -07:00
|
|
|
|
2012-02-14 10:03:31 -08:00
|
|
|
static int do_main(int argc, char* argv[])
|
2011-05-21 23:23:20 -07:00
|
|
|
{
|
2012-06-05 19:08:30 -07:00
|
|
|
nsCOMPtr<nsIFile> appini;
|
2011-11-21 23:05:59 -08:00
|
|
|
nsresult rv;
|
2007-07-02 11:20:24 -07:00
|
|
|
|
2007-07-25 13:24:37 -07:00
|
|
|
// Allow firefox.exe to launch XULRunner apps via -app <application.ini>
|
|
|
|
// Note that -app must be the *first* argument.
|
2011-05-21 23:23:20 -07:00
|
|
|
const char *appDataFile = getenv("XUL_APP_FILE");
|
2007-10-12 22:47:04 -07:00
|
|
|
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")) {
|
2007-07-25 13:24:37 -07:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2011-05-21 23:23:20 -07:00
|
|
|
char appEnv[MAXPATHLEN];
|
|
|
|
snprintf(appEnv, MAXPATHLEN, "XUL_APP_FILE=%s", argv[2]);
|
|
|
|
if (putenv(appEnv)) {
|
|
|
|
Output("Couldn't set %s.\n", appEnv);
|
|
|
|
return 255;
|
|
|
|
}
|
2007-07-25 13:24:37 -07:00
|
|
|
argv[2] = argv[0];
|
|
|
|
argv += 2;
|
|
|
|
argc -= 2;
|
|
|
|
}
|
|
|
|
|
2011-11-21 23:05:59 -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-11-21 23:05:59 -08:00
|
|
|
XRE_FreeAppData(appData);
|
2012-02-14 10:03:31 -08:00
|
|
|
return result;
|
2007-07-02 11:20:24 -07:00
|
|
|
}
|
|
|
|
|
2012-05-25 03:01:29 -07:00
|
|
|
return XRE_main(argc, argv, &sAppData, 0);
|
2011-05-21 23:23:20 -07:00
|
|
|
}
|
|
|
|
|
2012-05-27 19:40:48 -07:00
|
|
|
#ifdef XP_WIN
|
|
|
|
/**
|
|
|
|
* Determines if the registry is disabled via the service or not.
|
|
|
|
*
|
|
|
|
* @return true if prefetch is disabled
|
|
|
|
* false if prefetch is not disabled or an error occurred.
|
|
|
|
*/
|
|
|
|
bool IsPrefetchDisabledViaService()
|
|
|
|
{
|
|
|
|
// We don't need to return false when we don't have MOZ_MAINTENANCE_SERVICE
|
|
|
|
// defined. The reason is because another product installed that has it
|
|
|
|
// defined may have cleared our prefetch for us. There is no known way
|
|
|
|
// to figure out which prefetch files are associated with which apps
|
|
|
|
// because of the prefetch hash. So we disable all of them that start
|
|
|
|
// with FIREFOX.
|
|
|
|
HKEY baseKey;
|
|
|
|
LONG retCode = RegOpenKeyExW(HKEY_LOCAL_MACHINE,
|
|
|
|
L"SOFTWARE\\Mozilla\\MaintenanceService", 0,
|
|
|
|
KEY_READ | KEY_WOW64_64KEY, &baseKey);
|
|
|
|
if (retCode != ERROR_SUCCESS) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
DWORD disabledValue = 0;
|
|
|
|
DWORD disabledValueSize = sizeof(DWORD);
|
|
|
|
RegQueryValueExW(baseKey, L"FFPrefetchDisabled", 0, NULL,
|
|
|
|
reinterpret_cast<LPBYTE>(&disabledValue),
|
|
|
|
&disabledValueSize);
|
|
|
|
RegCloseKey(baseKey);
|
|
|
|
return disabledValue == 1;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2012-06-14 23:23:08 -07:00
|
|
|
/* Local implementation of PR_Now, since the executable can't depend on NSPR */
|
|
|
|
static PRTime _PR_Now()
|
|
|
|
{
|
|
|
|
#ifdef XP_WIN
|
|
|
|
MOZ_STATIC_ASSERT(sizeof(PRTime) == sizeof(FILETIME), "PRTime must have the same size as FILETIME");
|
|
|
|
FILETIME ft;
|
|
|
|
GetSystemTimeAsFileTime(&ft);
|
|
|
|
PRTime now;
|
|
|
|
CopyMemory(&now, &ft, sizeof(PRTime));
|
|
|
|
#ifdef __GNUC__
|
|
|
|
return (now - 116444736000000000LL) / 10LL;
|
|
|
|
#else
|
|
|
|
return (now - 116444736000000000i64) / 10i64;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#else
|
|
|
|
struct timeval tm;
|
|
|
|
gettimeofday(&tm, 0);
|
|
|
|
return (((PRTime)tm.tv_sec * 1000000LL) + (PRTime)tm.tv_usec);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2011-05-21 23:23:20 -07:00
|
|
|
int main(int argc, char* argv[])
|
|
|
|
{
|
2012-06-14 23:23:08 -07:00
|
|
|
PRTime start = _PR_Now();
|
2011-05-21 23:23:20 -07:00
|
|
|
char exePath[MAXPATHLEN];
|
|
|
|
|
2011-12-19 09:47:09 -08:00
|
|
|
#ifdef XP_MACOSX
|
|
|
|
TriggerQuirks();
|
|
|
|
#endif
|
|
|
|
|
2011-05-21 23:23:20 -07:00
|
|
|
nsresult rv = mozilla::BinaryPath::Get(argv[0], exePath);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
Output("Couldn't calculate the application directory.\n");
|
|
|
|
return 255;
|
|
|
|
}
|
|
|
|
|
|
|
|
char *lastSlash = strrchr(exePath, XPCOM_FILE_PATH_SEPARATOR[0]);
|
2012-03-26 08:53:15 -07:00
|
|
|
if (!lastSlash || (size_t(lastSlash - exePath) > MAXPATHLEN - sizeof(XPCOM_DLL) - 1))
|
2011-05-21 23:23:20 -07:00
|
|
|
return 255;
|
|
|
|
|
|
|
|
strcpy(++lastSlash, XPCOM_DLL);
|
|
|
|
|
2011-05-21 23:24:32 -07:00
|
|
|
int gotCounters;
|
|
|
|
#if defined(XP_UNIX)
|
|
|
|
struct rusage initialRUsage;
|
|
|
|
gotCounters = !getrusage(RUSAGE_SELF, &initialRUsage);
|
|
|
|
#elif defined(XP_WIN)
|
2011-05-16 01:25:05 -07:00
|
|
|
IO_COUNTERS ioCounters;
|
2011-05-21 23:24:32 -07:00
|
|
|
gotCounters = GetProcessIoCounters(GetCurrentProcess(), &ioCounters);
|
2012-07-08 18:54:10 -07:00
|
|
|
#endif
|
2012-05-27 19:40:49 -07:00
|
|
|
|
2012-07-08 18:54:10 -07:00
|
|
|
#if !defined(XP_WIN)
|
|
|
|
XPCOMGlueEnablePreload();
|
2011-05-16 01:25:05 -07:00
|
|
|
#endif
|
|
|
|
|
2011-05-21 23:23:20 -07:00
|
|
|
rv = XPCOMGlueStartup(exePath);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
Output("Couldn't load XPCOM.\n");
|
|
|
|
return 255;
|
|
|
|
}
|
2011-11-21 23:05:59 -08:00
|
|
|
// Reset exePath so that it is the directory name and not the xpcom dll name
|
|
|
|
*lastSlash = 0;
|
2011-05-21 23:23:20 -07:00
|
|
|
|
|
|
|
rv = XPCOMGlueLoadXULFunctions(kXULFuncs);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
Output("Couldn't load XRE functions.\n");
|
|
|
|
return 255;
|
|
|
|
}
|
|
|
|
|
2012-06-14 23:23:08 -07:00
|
|
|
XRE_StartupTimelineRecord(mozilla::StartupTimeline::START, start);
|
|
|
|
|
2011-05-21 23:23:20 -07:00
|
|
|
#ifdef XRE_HAS_DLL_BLOCKLIST
|
|
|
|
XRE_SetupDllBlocklist();
|
|
|
|
#endif
|
|
|
|
|
2011-05-21 23:24:32 -07:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2011-05-21 23:23:20 -07:00
|
|
|
int result;
|
|
|
|
{
|
|
|
|
ScopedLogging log;
|
2012-02-14 10:03:31 -08:00
|
|
|
result = do_main(argc, argv);
|
2011-05-21 23:23:20 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
XPCOMGlueShutdown();
|
2007-07-02 11:20:24 -07:00
|
|
|
return result;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|