diff --git a/browser/app/nsBrowserApp.cpp b/browser/app/nsBrowserApp.cpp index da3b24e90f6..d04b4093056 100644 --- a/browser/app/nsBrowserApp.cpp +++ b/browser/app/nsBrowserApp.cpp @@ -45,6 +45,10 @@ #include #include +#include "plstr.h" +#include "prprf.h" +#include "prenv.h" + #include "nsCOMPtr.h" #include "nsILocalFile.h" #include "nsStringGlue.h" @@ -67,8 +71,40 @@ static void Output(const char *fmt, ... ) va_end(ap); } +/** + * Return true if |arg| matches the given argument name. + */ +static PRBool IsArg(const char* arg, const char* s) +{ + if (*arg == '-') + { + if (*++arg == '-') + ++arg; + return !PL_strcasecmp(arg, s); + } + +#if defined(XP_WIN) || defined(XP_OS2) + if (*arg == '/') + return !PL_strcasecmp(++arg, s); +#endif + + return PR_FALSE; +} + +/** + * A helper class which calls NS_LogTerm/NS_LogTerm in its scope. + */ +class ScopedLogging +{ +public: + ScopedLogging() { NS_LogInit(); } + ~ScopedLogging() { NS_LogTerm(); } +}; + int main(int argc, char* argv[]) { + ScopedLogging log; + nsCOMPtr appini; nsresult rv = XRE_GetBinaryPath(argv[0], getter_AddRefs(appini)); if (NS_FAILED(rv)) { @@ -77,6 +113,36 @@ int main(int argc, char* argv[]) } appini->SetNativeLeafName(NS_LITERAL_CSTRING("application.ini")); + // Allow firefox.exe to launch XULRunner apps via -app + // Note that -app must be the *first* argument. + char *appEnv = nsnull; + const char *appDataFile = PR_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; + } + + appEnv = PR_smprintf("XUL_APP_FILE=%s", argv[2]); + PR_SetEnv(appEnv); + argv[2] = argv[0]; + argv += 2; + argc -= 2; + } + nsXREAppData *appData; rv = XRE_CreateAppData(appini, &appData); if (NS_FAILED(rv)) { @@ -86,6 +152,8 @@ int main(int argc, char* argv[]) int result = XRE_main(argc, argv, appData); XRE_FreeAppData(appData); + if (appEnv) + PR_smprintf_free(appEnv); return result; } diff --git a/toolkit/xre/nsAppData.cpp b/toolkit/xre/nsAppData.cpp index 492b705ae42..2c33516e8dc 100644 --- a/toolkit/xre/nsAppData.cpp +++ b/toolkit/xre/nsAppData.cpp @@ -40,6 +40,7 @@ #include "nsILocalFile.h" #include "nsAppRunner.h" #include "nsCRTGlue.h" +#include "nsAutoPtr.h" void SetAllocatedString(const char *&str, const char *newvalue) @@ -114,19 +115,27 @@ XRE_CreateAppData(nsILocalFile* aINIFile, nsXREAppData **aAppData) { NS_ENSURE_ARG(aINIFile && aAppData); - nsXREAppData *data = new ScopedAppData(); + nsAutoPtr data = new ScopedAppData(); if (!data) return NS_ERROR_OUT_OF_MEMORY; nsresult rv = XRE_ParseAppData(aINIFile, data); - if (NS_FAILED(rv)) { - delete data; - } - else { - *aAppData = data; + if (NS_FAILED(rv)) + return rv; + + if (!data->directory) { + nsCOMPtr appDir; + rv = aINIFile->GetParent(getter_AddRefs(appDir)); + if (NS_FAILED(rv)) + return rv; + + rv = CallQueryInterface(appDir, &data->directory); + if (NS_FAILED(rv)) + return rv; } - return rv; + *aAppData = data.forget(); + return NS_OK; } struct ReadString { diff --git a/xulrunner/app/nsXULRunnerApp.cpp b/xulrunner/app/nsXULRunnerApp.cpp index 9669793d9c5..f4eb4aab9b8 100644 --- a/xulrunner/app/nsXULRunnerApp.cpp +++ b/xulrunner/app/nsXULRunnerApp.cpp @@ -443,17 +443,6 @@ int main(int argc, char* argv[]) return 2; } - if (!appData->directory) { - nsCOMPtr appDir; - rv = appDataLF->GetParent(getter_AddRefs(appDir)); - if (NS_FAILED(rv)) { - Output(PR_TRUE, "Error: could not get application directory.\n"); - return 2; - } - - CallQueryInterface(appDir, &appData->directory); - } - return XRE_main(argc, argv, appData); }