2002-08-21 23:21:36 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* ***** 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.org code.
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is
|
|
|
|
* Netscape Communications Corporation.
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 2002
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
2003-09-29 10:04:02 +04:00
|
|
|
* Brian Ryner <bryner@brianryner.com>
|
2002-08-21 23:21:36 +04:00
|
|
|
*
|
|
|
|
* 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 ***** */
|
|
|
|
|
2011-05-22 10:23:20 +04:00
|
|
|
#include "nsXPCOMGlue.h"
|
2002-08-21 23:21:36 +04:00
|
|
|
#include "nsXULAppAPI.h"
|
2011-05-22 10:24:32 +04:00
|
|
|
#if defined(XP_WIN)
|
2002-08-21 23:21:36 +04:00
|
|
|
#include <windows.h>
|
2004-06-18 01:23:51 +04:00
|
|
|
#include <stdlib.h>
|
2011-05-22 10:24:32 +04:00
|
|
|
#elif defined(XP_UNIX)
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <sys/resource.h>
|
2002-08-21 23:21:36 +04:00
|
|
|
#endif
|
2007-03-23 02:01:14 +03:00
|
|
|
|
2007-07-02 22:20:24 +04:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdarg.h>
|
2011-05-22 10:23:20 +04:00
|
|
|
#include <string.h>
|
2007-07-02 22:20:24 +04:00
|
|
|
|
2007-07-26 00:24:37 +04:00
|
|
|
#include "plstr.h"
|
|
|
|
#include "prprf.h"
|
|
|
|
#include "prenv.h"
|
|
|
|
|
2007-07-02 22:20:24 +04:00
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
#include "nsILocalFile.h"
|
|
|
|
#include "nsStringGlue.h"
|
|
|
|
|
2007-12-31 18:15:43 +03:00
|
|
|
#ifdef XP_WIN
|
|
|
|
// we want a wmain entry point
|
|
|
|
#include "nsWindowsWMain.cpp"
|
2011-05-22 10:23:20 +04:00
|
|
|
#define snprintf _snprintf
|
|
|
|
#define strcasecmp _stricmp
|
2007-12-31 18:15:43 +03:00
|
|
|
#endif
|
2011-05-22 10:23:20 +04:00
|
|
|
#include "BinaryPath.h"
|
|
|
|
|
|
|
|
#include "nsXPCOMPrivate.h" // for MAXPATHLEN and XPCOM_DLL
|
2007-12-31 18:15:43 +03:00
|
|
|
|
2011-05-22 10:24:32 +04:00
|
|
|
#include "mozilla/Telemetry.h"
|
|
|
|
|
2007-07-02 22:20:24 +04:00
|
|
|
static void Output(const char *fmt, ... )
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
va_start(ap, fmt);
|
|
|
|
|
|
|
|
#if defined(XP_WIN) && !MOZ_WINCONSOLE
|
2008-08-28 01:44:54 +04:00
|
|
|
PRUnichar msg[2048];
|
|
|
|
_vsnwprintf(msg, sizeof(msg)/sizeof(msg[0]), NS_ConvertUTF8toUTF16(fmt).get(), ap);
|
|
|
|
MessageBoxW(NULL, msg, L"XULRunner", MB_OK | MB_ICONERROR);
|
2007-07-02 22:20:24 +04:00
|
|
|
#else
|
|
|
|
vfprintf(stderr, fmt, ap);
|
2007-05-26 02:09:29 +04:00
|
|
|
#endif
|
2007-07-02 22:20:24 +04:00
|
|
|
|
|
|
|
va_end(ap);
|
|
|
|
}
|
2007-07-26 00:24:37 +04:00
|
|
|
|
2007-07-26 00:24:37 +04:00
|
|
|
/**
|
|
|
|
* Return true if |arg| matches the given argument name.
|
|
|
|
*/
|
2011-09-29 10:19:26 +04:00
|
|
|
static bool IsArg(const char* arg, const char* s)
|
2007-07-26 00:24:37 +04:00
|
|
|
{
|
|
|
|
if (*arg == '-')
|
|
|
|
{
|
|
|
|
if (*++arg == '-')
|
|
|
|
++arg;
|
2011-05-22 10:23:20 +04:00
|
|
|
return !strcasecmp(arg, s);
|
2007-07-26 00:24:37 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
#if defined(XP_WIN) || defined(XP_OS2)
|
|
|
|
if (*arg == '/')
|
2011-05-22 10:23:20 +04:00
|
|
|
return !strcasecmp(++arg, s);
|
2007-07-26 00:24:37 +04:00
|
|
|
#endif
|
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2007-07-26 00:24:37 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2007-08-06 03:48:25 +04:00
|
|
|
* A helper class which calls NS_LogInit/NS_LogTerm in its scope.
|
2007-07-26 00:24:37 +04:00
|
|
|
*/
|
|
|
|
class ScopedLogging
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
ScopedLogging() { NS_LogInit(); }
|
|
|
|
~ScopedLogging() { NS_LogTerm(); }
|
|
|
|
};
|
|
|
|
|
2011-05-22 10:23:20 +04: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-22 10:24:32 +04:00
|
|
|
XRE_TelemetryAccumulateType XRE_TelemetryAccumulate;
|
2011-05-22 10:23:20 +04:00
|
|
|
XRE_mainType XRE_main;
|
2011-06-15 20:00:28 +04:00
|
|
|
|
2011-05-22 10:23:20 +04:00
|
|
|
static const nsDynamicFunctionLoad kXULFuncs[] = {
|
|
|
|
{ "XRE_GetFileFromPath", (NSFuncPtr*) &XRE_GetFileFromPath },
|
|
|
|
{ "XRE_CreateAppData", (NSFuncPtr*) &XRE_CreateAppData },
|
|
|
|
{ "XRE_FreeAppData", (NSFuncPtr*) &XRE_FreeAppData },
|
2011-05-22 10:22:27 +04:00
|
|
|
#ifdef XRE_HAS_DLL_BLOCKLIST
|
2011-05-22 10:23:20 +04:00
|
|
|
{ "XRE_SetupDllBlocklist", (NSFuncPtr*) &XRE_SetupDllBlocklist },
|
2011-05-22 10:22:27 +04:00
|
|
|
#endif
|
2011-05-22 10:24:32 +04:00
|
|
|
{ "XRE_TelemetryAccumulate", (NSFuncPtr*) &XRE_TelemetryAccumulate },
|
2011-05-22 10:23:20 +04:00
|
|
|
{ "XRE_main", (NSFuncPtr*) &XRE_main },
|
|
|
|
{ nsnull, nsnull }
|
|
|
|
};
|
2011-05-22 10:22:27 +04:00
|
|
|
|
2011-05-22 10:23:20 +04:00
|
|
|
static int do_main(const char *exePath, int argc, char* argv[])
|
|
|
|
{
|
2007-07-02 22:20:24 +04:00
|
|
|
nsCOMPtr<nsILocalFile> appini;
|
2011-05-22 10:23:20 +04:00
|
|
|
#ifdef XP_WIN
|
|
|
|
// exePath comes from mozilla::BinaryPath::Get, which returns a UTF-8
|
|
|
|
// encoded path, so it is safe to convert it
|
2011-10-17 18:59:28 +04:00
|
|
|
nsresult rv = NS_NewLocalFile(NS_ConvertUTF8toUTF16(exePath), false,
|
2011-05-22 10:23:20 +04:00
|
|
|
getter_AddRefs(appini));
|
|
|
|
#else
|
2011-10-17 18:59:28 +04:00
|
|
|
nsresult rv = NS_NewNativeLocalFile(nsDependentCString(exePath), false,
|
2011-05-22 10:23:20 +04:00
|
|
|
getter_AddRefs(appini));
|
|
|
|
#endif
|
2007-07-02 22:20:24 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return 255;
|
|
|
|
}
|
2011-05-22 10:23:20 +04:00
|
|
|
|
2007-07-02 22:20:24 +04:00
|
|
|
appini->SetNativeLeafName(NS_LITERAL_CSTRING("application.ini"));
|
|
|
|
|
2007-07-26 00:24:37 +04:00
|
|
|
// Allow firefox.exe to launch XULRunner apps via -app <application.ini>
|
|
|
|
// Note that -app must be the *first* argument.
|
2011-05-22 10:23:20 +04:00
|
|
|
const char *appDataFile = getenv("XUL_APP_FILE");
|
2007-10-13 09:47:04 +04: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-26 00:24:37 +04: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-22 10:23:20 +04: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-26 00:24:37 +04:00
|
|
|
argv[2] = argv[0];
|
|
|
|
argv += 2;
|
|
|
|
argc -= 2;
|
|
|
|
}
|
|
|
|
|
2007-07-02 22:20:24 +04:00
|
|
|
nsXREAppData *appData;
|
|
|
|
rv = XRE_CreateAppData(appini, &appData);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
Output("Couldn't read application.ini");
|
|
|
|
return 255;
|
|
|
|
}
|
|
|
|
|
|
|
|
int result = XRE_main(argc, argv, appData);
|
|
|
|
XRE_FreeAppData(appData);
|
2011-05-22 10:23:20 +04:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char* argv[])
|
|
|
|
{
|
|
|
|
char exePath[MAXPATHLEN];
|
|
|
|
|
|
|
|
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]);
|
|
|
|
if (!lastSlash || (lastSlash - exePath > MAXPATHLEN - sizeof(XPCOM_DLL) - 1))
|
|
|
|
return 255;
|
|
|
|
|
|
|
|
strcpy(++lastSlash, XPCOM_DLL);
|
|
|
|
|
2011-05-22 10:24:32 +04:00
|
|
|
int gotCounters;
|
|
|
|
#if defined(XP_UNIX)
|
|
|
|
struct rusage initialRUsage;
|
|
|
|
gotCounters = !getrusage(RUSAGE_SELF, &initialRUsage);
|
|
|
|
#elif defined(XP_WIN)
|
2011-05-16 12:25:05 +04:00
|
|
|
// GetProcessIoCounters().ReadOperationCount seems to have little to
|
|
|
|
// do with actual read operations. It reports 0 or 1 at this stage
|
|
|
|
// in the program. Luckily 1 coincides with when prefetch is
|
|
|
|
// enabled. If Windows prefetch didn't happen we can do our own
|
|
|
|
// faster dll preloading.
|
|
|
|
IO_COUNTERS ioCounters;
|
2011-05-22 10:24:32 +04:00
|
|
|
gotCounters = GetProcessIoCounters(GetCurrentProcess(), &ioCounters);
|
|
|
|
if (gotCounters && !ioCounters.ReadOperationCount)
|
2011-05-16 12:25:05 +04:00
|
|
|
#endif
|
|
|
|
{
|
|
|
|
XPCOMGlueEnablePreload();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-05-22 10:23:20 +04:00
|
|
|
rv = XPCOMGlueStartup(exePath);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
Output("Couldn't load XPCOM.\n");
|
|
|
|
return 255;
|
|
|
|
}
|
|
|
|
|
|
|
|
rv = XPCOMGlueLoadXULFunctions(kXULFuncs);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
Output("Couldn't load XRE functions.\n");
|
|
|
|
return 255;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef XRE_HAS_DLL_BLOCKLIST
|
|
|
|
XRE_SetupDllBlocklist();
|
|
|
|
#endif
|
|
|
|
|
2011-05-22 10:24:32 +04: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-22 10:23:20 +04:00
|
|
|
int result;
|
|
|
|
{
|
|
|
|
ScopedLogging log;
|
|
|
|
result = do_main(exePath, argc, argv);
|
|
|
|
}
|
|
|
|
|
|
|
|
XPCOMGlueShutdown();
|
2007-07-02 22:20:24 +04:00
|
|
|
return result;
|
2007-03-23 02:01:14 +03:00
|
|
|
}
|