Bug 1306327 - Move reading application.ini to XRE_main. r=bsmedberg

Reading application.ini involves using nsCOMPtr<nsIFile>, and that can
only happen through the XPCOM glue, which we eventually want to get rid
of.

So, while keeping the command line argument/environment variable
handling in nsBrowserApp, we move the actually parsing of the file to
XRE_main, where things can be handled without the XPCOM glue.

--HG--
extra : rebase_source : 487960a671476d4edae4f568c37efa6563ef4dff
This commit is contained in:
Mike Hommey 2017-01-10 16:43:23 +09:00
parent bebd095c4c
commit 2330dcf840
7 changed files with 110 additions and 96 deletions

View File

@ -176,30 +176,16 @@ void libFuzzerGetFuncs(const char* moduleName, LibFuzzerInitFunc* initFunc,
static int do_main(int argc, char* argv[], char* envp[])
{
nsCOMPtr<nsIFile> appini;
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 = gBootstrap->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 ((!appDataFile || !*appDataFile) &&
(argc > 1 && IsArg(argv[1], "app"))) {
if (argc == 2) {
Output("Incorrect number of arguments passed to -app");
return 255;
}
rv = gBootstrap->XRE_GetFileFromPath(argv[2], getter_AddRefs(appini));
if (NS_FAILED(rv)) {
Output("application.ini path not recognized: '%s'", argv[2]);
return 255;
}
appDataFile = argv[2];
char appEnv[MAXPATHLEN];
SprintfLiteral(appEnv, "XUL_APP_FILE=%s", argv[2]);
@ -224,36 +210,15 @@ static int do_main(int argc, char* argv[], char* envp[])
return gBootstrap->XRE_XPCShellMain(--argc, argv, envp, &shellData);
}
XREAppData appData;
BootstrapConfig config;
if (appini) {
rv = gBootstrap->XRE_ParseAppData(appini, appData);
if (NS_FAILED(rv)) {
Output("Couldn't read application.ini");
return 255;
}
appini->GetParent(getter_AddRefs(appData.directory));
if (appDataFile && *appDataFile) {
config.appData = nullptr;
config.appDataPath = appDataFile;
} else {
// no -app flag so we use the compiled-in app data
appData = sAppData;
nsCOMPtr<nsIFile> exeFile;
rv = mozilla::BinaryPath::GetFile(argv[0], getter_AddRefs(exeFile));
if (NS_FAILED(rv)) {
Output("Couldn't find the application directory.\n");
return 255;
}
nsCOMPtr<nsIFile> greDir;
exeFile->GetParent(getter_AddRefs(greDir));
#ifdef XP_MACOSX
greDir->SetNativeLeafName(NS_LITERAL_CSTRING(kOSXResourcesFolder));
#endif
nsCOMPtr<nsIFile> appSubdir;
greDir->Clone(getter_AddRefs(appSubdir));
appSubdir->Append(NS_LITERAL_STRING(kDesktopFolder));
appData.directory = appSubdir;
config.appData = &sAppData;
config.appDataPath = kDesktopFolder;
}
#if defined(XP_WIN) && defined(MOZ_SANDBOX)
@ -265,7 +230,7 @@ static int do_main(int argc, char* argv[], char* envp[])
return 255;
}
#endif
appData.sandboxBrokerServices = brokerServices;
config.sandboxBrokerServices = brokerServices;
#endif
#ifdef LIBFUZZER
@ -273,7 +238,7 @@ static int do_main(int argc, char* argv[], char* envp[])
gBootstrap->XRE_LibFuzzerSetMain(argc, argv, libfuzzer_main);
#endif
return gBootstrap->XRE_main(argc, argv, appData);
return gBootstrap->XRE_main(argc, argv, config);
}
static bool

View File

@ -33,14 +33,6 @@ public:
::NS_LogTerm();
}
virtual nsresult XRE_GetFileFromPath(const char* aPath, nsIFile** aResult) override {
return ::XRE_GetFileFromPath(aPath, aResult);
}
virtual nsresult XRE_ParseAppData(nsIFile* aINIFile, mozilla::XREAppData& aAppData) override {
return ::XRE_ParseAppData(aINIFile, aAppData);
}
virtual void XRE_TelemetryAccumulate(int aID, uint32_t aSample) override {
::XRE_TelemetryAccumulate(aID, aSample);
}
@ -49,8 +41,8 @@ public:
::XRE_StartupTimelineRecord(aEvent, aWhen);
}
virtual int XRE_main(int argc, char* argv[], const mozilla::XREAppData& aAppData) override {
return ::XRE_main(argc, argv, aAppData);
virtual int XRE_main(int argc, char* argv[], const BootstrapConfig& aConfig) override {
return ::XRE_main(argc, argv, aConfig);
}
virtual void XRE_StopLateWriteChecks() override {

View File

@ -23,8 +23,29 @@ extern "C" NS_EXPORT
void GeckoStart(JNIEnv* aEnv, char** argv, int argc, const mozilla::StaticXREAppData& aAppData);
#endif
#if defined(XP_WIN) && defined(MOZ_SANDBOX)
namespace sandbox {
class BrokerServices;
}
#endif
namespace mozilla {
struct BootstrapConfig
{
#if defined(XP_WIN) && defined(MOZ_SANDBOX)
/* Chromium sandbox BrokerServices. */
sandbox::BrokerServices* sandboxBrokerServices;
#endif
/* Pointer to static XRE AppData from application.ini.h */
const StaticXREAppData* appData;
/* When the pointer above is null, points to the (string) path of an
* application.ini file to open and parse.
* When the pointer above is non-null, may indicate the directory where
* application files are, relative to the XRE. */
const char* appDataPath;
};
/**
* This class is virtual abstract so that using it does not require linking
* any symbols. The singleton instance of this class is obtained from the
@ -64,15 +85,11 @@ public:
virtual void NS_LogTerm() = 0;
virtual nsresult XRE_GetFileFromPath(const char* aPath, nsIFile** aResult) = 0;
virtual nsresult XRE_ParseAppData(nsIFile* aINIFile, mozilla::XREAppData& aAppData) = 0;
virtual void XRE_TelemetryAccumulate(int aID, uint32_t aSample) = 0;
virtual void XRE_StartupTimelineRecord(int aEvent, mozilla::TimeStamp aWhen) = 0;
virtual int XRE_main(int argc, char* argv[], const mozilla::XREAppData& aAppData) = 0;
virtual int XRE_main(int argc, char* argv[], const BootstrapConfig& aConfig) = 0;
virtual void XRE_StopLateWriteChecks() = 0;

View File

@ -18,6 +18,7 @@
#include "nsAppRunner.h"
#include "APKOpen.h"
#include "nsExceptionHandler.h"
#include "mozilla/Bootstrap.h"
#define LOG(args...) __android_log_print(ANDROID_LOG_INFO, MOZ_APP_NAME, args)
@ -42,10 +43,11 @@ GeckoStart(JNIEnv* env, char** argv, int argc, const StaticXREAppData& aAppData)
return;
}
XREAppData appData;
appData = aAppData;
BootstrapConfig config;
config.appData = &aAppData;
config.appDataPath = nullptr;
int result = XRE_main(argc, argv, appData);
int result = XRE_main(argc, argv, config);
if (result)
LOG("XRE_main returned %d", result);

View File

@ -21,6 +21,7 @@
#include "nsAppRunner.h"
#include "mozilla/XREAppData.h"
#include "mozilla/Bootstrap.h"
#if defined(MOZ_UPDATER) && !defined(MOZ_WIDGET_ANDROID)
#include "nsUpdateDriver.h"
#endif
@ -3024,7 +3025,7 @@ public:
mAppData = nullptr;
}
int XRE_main(int argc, char* argv[], const XREAppData& aAppData);
int XRE_main(int argc, char* argv[], const BootstrapConfig& aConfig);
int XRE_mainInit(bool* aExitFlag);
int XRE_mainStartup(bool* aExitFlag);
nsresult XRE_mainRun();
@ -3184,31 +3185,6 @@ XREMain::XRE_mainInit(bool* aExitFlag)
// XXX Originally ScopedLogging was here? Now it's in XRE_main above
// XRE_mainInit.
if (!mAppData->xreDirectory) {
nsCOMPtr<nsIFile> lf;
rv = XRE_GetBinaryPath(gArgv[0], getter_AddRefs(lf));
if (NS_FAILED(rv))
return 2;
nsCOMPtr<nsIFile> greDir;
rv = lf->GetParent(getter_AddRefs(greDir));
if (NS_FAILED(rv))
return 2;
#ifdef XP_MACOSX
nsCOMPtr<nsIFile> parent;
greDir->GetParent(getter_AddRefs(parent));
greDir = parent.forget();
greDir->AppendNative(NS_LITERAL_CSTRING("Resources"));
#endif
mAppData->xreDirectory = greDir;
}
if (!mAppData->directory) {
mAppData->directory = mAppData->xreDirectory;
}
if (!mAppData->minVersion) {
Output(true, "Error: Gecko:MinVersion not specified in application.ini\n");
return 1;
@ -4526,7 +4502,7 @@ XRE_CreateStatsObject()
* .app/Contents/Resources.
*/
int
XREMain::XRE_main(int argc, char* argv[], const XREAppData& aAppData)
XREMain::XRE_main(int argc, char* argv[], const BootstrapConfig& aConfig)
{
ScopedLogging log;
@ -4555,7 +4531,27 @@ XREMain::XRE_main(int argc, char* argv[], const XREAppData& aAppData)
gArgc = argc;
gArgv = argv;
mAppData = MakeUnique<XREAppData>(aAppData);
if (aConfig.appData) {
mAppData = MakeUnique<XREAppData>(*aConfig.appData);
} else {
MOZ_RELEASE_ASSERT(aConfig.appDataPath);
nsCOMPtr<nsIFile> appini;
rv = XRE_GetFileFromPath(aConfig.appDataPath, getter_AddRefs(appini));
if (NS_FAILED(rv)) {
Output(true, "Error: unrecognized path: %s\n", aConfig.appDataPath);
return 1;
}
mAppData = MakeUnique<XREAppData>();
rv = XRE_ParseAppData(appini, *mAppData);
if (NS_FAILED(rv)) {
Output(true, "Couldn't read application.ini");
return 1;
}
appini->GetParent(getter_AddRefs(mAppData->directory));
}
if (!mAppData->remotingName) {
mAppData->remotingName = mAppData->name;
}
@ -4569,6 +4565,40 @@ XREMain::XRE_main(int argc, char* argv[], const XREAppData& aAppData)
rv = binFile->GetPath(gAbsoluteArgv0Path);
NS_ENSURE_SUCCESS(rv, 1);
if (!mAppData->xreDirectory) {
nsCOMPtr<nsIFile> lf;
rv = XRE_GetBinaryPath(gArgv[0], getter_AddRefs(lf));
if (NS_FAILED(rv))
return 2;
nsCOMPtr<nsIFile> greDir;
rv = lf->GetParent(getter_AddRefs(greDir));
if (NS_FAILED(rv))
return 2;
#ifdef XP_MACOSX
nsCOMPtr<nsIFile> parent;
greDir->GetParent(getter_AddRefs(parent));
greDir = parent.forget();
greDir->AppendNative(NS_LITERAL_CSTRING("Resources"));
#endif
mAppData->xreDirectory = greDir;
}
if (aConfig.appData && aConfig.appDataPath) {
mAppData->xreDirectory->Clone(getter_AddRefs(mAppData->directory));
mAppData->directory->AppendNative(nsDependentCString(aConfig.appDataPath));
}
if (!mAppData->directory) {
mAppData->directory = mAppData->xreDirectory;
}
#if defined(XP_WIN) && defined(MOZ_SANDBOX)
mAppData->sandboxBrokerServices = aConfig.sandboxBrokerServices;
#endif
mozilla::IOInterposerInit ioInterposerGuard;
#if defined(XP_WIN)
@ -4692,11 +4722,11 @@ XRE_StopLateWriteChecks(void) {
}
int
XRE_main(int argc, char* argv[], const XREAppData& aAppData)
XRE_main(int argc, char* argv[], const BootstrapConfig& aConfig)
{
XREMain main;
int result = main.XRE_main(argc, argv, aAppData);
int result = main.XRE_main(argc, argv, aConfig);
mozilla::RecordShutdownEndTimeStamp();
return result;
}

View File

@ -36,6 +36,11 @@ public:
*this = aOther;
}
explicit XREAppData(const StaticXREAppData& aOther)
{
*this = aOther;
}
XREAppData& operator=(const StaticXREAppData& aOther);
XREAppData& operator=(const XREAppData& aOther);
XREAppData& operator=(XREAppData&& aOther) = default;

View File

@ -194,15 +194,18 @@
* Windows, these should be in UTF8. On unix-like platforms
* these are in the "native" character set.
*
* @param aAppData Information about the application to be run.
* @param aConfig Information about the application to be run.
*
* @return A native result code suitable for returning from main().
*
* @note If the binary is linked against the standalone XPCOM glue,
* XPCOMGlueStartup() should be called before this method.
*/
namespace mozilla {
struct BootstrapConfig;
}
XRE_API(int,
XRE_main, (int argc, char* argv[], const mozilla::XREAppData& aAppData))
XRE_main, (int argc, char* argv[], const mozilla::BootstrapConfig& aConfig))
/**
* Given a path relative to the current working directory (or an absolute