From 834ec83fc3e68ae0510ff16aafa4889349d21872 Mon Sep 17 00:00:00 2001 From: Benjamin Smedberg Date: Tue, 20 Jan 2009 14:56:44 -0500 Subject: [PATCH] Bug 470971 - Add ability to pass the GRE dir to xpcshell, r=ted - fixed patch: XRE_GetBinaryPath gets the file of the executable: we want to pass the parent directory to XPCOM. --- config/autoconf.mk.in | 1 + configure.in | 2 + js/src/xpconnect/shell/Makefile.in | 3 +- js/src/xpconnect/shell/xpcshell.cpp | 96 ++++++++++++++++++++++++++++- 4 files changed, 99 insertions(+), 3 deletions(-) diff --git a/config/autoconf.mk.in b/config/autoconf.mk.in index 36f4d588726..66bb4f46daf 100644 --- a/config/autoconf.mk.in +++ b/config/autoconf.mk.in @@ -186,6 +186,7 @@ MOZ_FIX_LINK_PATHS=@MOZ_FIX_LINK_PATHS@ XPCOM_FROZEN_LDOPTS=@XPCOM_FROZEN_LDOPTS@ XPCOM_LIBS=@XPCOM_LIBS@ +LIBXUL_LIBS=@LIBXUL_LIBS@ MOZ_TIMELINE=@MOZ_TIMELINE@ ENABLE_STRIP = @ENABLE_STRIP@ diff --git a/configure.in b/configure.in index 5807016bce5..b5a16167ef2 100644 --- a/configure.in +++ b/configure.in @@ -7254,6 +7254,8 @@ if test -n "$MOZ_ENABLE_LIBXUL" -a -n "$BUILD_STATIC_LIBS"; then AC_MSG_ERROR([--enable-libxul is not compatible with --enable-static]) fi +AC_SUBST(LIBXUL_LIBS) + if test -n "$MOZ_ENABLE_LIBXUL"; then XPCOM_LIBS="$LIBXUL_LIBS" AC_DEFINE(MOZ_ENABLE_LIBXUL) diff --git a/js/src/xpconnect/shell/Makefile.in b/js/src/xpconnect/shell/Makefile.in index 8d47f557b5b..b2cecdf1e75 100644 --- a/js/src/xpconnect/shell/Makefile.in +++ b/js/src/xpconnect/shell/Makefile.in @@ -50,6 +50,7 @@ REQUIRES = xpconnect \ xpcom \ js \ caps \ + xulapp \ $(NULL) CPPSRCS = xpcshell.cpp @@ -60,8 +61,8 @@ endif LIBS = \ $(DIST)/lib/$(LIB_PREFIX)xpcomglue_s.$(LIB_SUFFIX) \ + $(LIBXUL_LIBS) \ $(MOZ_JS_LIBS) \ - $(XPCOM_LIBS) \ $(NSPR_LIBS) \ $(NULL) diff --git a/js/src/xpconnect/shell/xpcshell.cpp b/js/src/xpconnect/shell/xpcshell.cpp index b48325bd73f..8169daf707d 100644 --- a/js/src/xpconnect/shell/xpcshell.cpp +++ b/js/src/xpconnect/shell/xpcshell.cpp @@ -45,8 +45,10 @@ /* XPConnect JavaScript interactive shell. */ #include +#include "nsXULAppAPI.h" #include "nsServiceManagerUtils.h" #include "nsComponentManagerUtils.h" +#include "nsStringAPI.h" #include "nsIXPConnect.h" #include "nsIXPCScriptable.h" #include "nsIInterfaceInfo.h" @@ -57,12 +59,16 @@ #include "nsIComponentRegistrar.h" #include "nsILocalFile.h" #include "nsStringAPI.h" +#include "nsIDirectoryService.h" +#include "nsILocalFile.h" +#include "nsDirectoryServiceDefs.h" #include "jsapi.h" #include "jsdbgapi.h" #include "jsprf.h" #include "nscore.h" #include "nsMemory.h" #include "nsIGenericFactory.h" +#include "nsISupportsImpl.h" #include "nsIJSRuntimeService.h" #include "nsCOMPtr.h" #include "nsAutoPtr.h" @@ -91,6 +97,22 @@ #include "nsIJSContextStack.h" +class XPCShellDirProvider : public nsIDirectoryServiceProvider +{ +public: + NS_DECL_ISUPPORTS_INHERITED + NS_DECL_NSIDIRECTORYSERVICEPROVIDER + + XPCShellDirProvider() { } + ~XPCShellDirProvider() { } + + PRBool SetGREDir(const char *dir); + void ClearGREDir() { mGREDir = nsnull; } + +private: + nsCOMPtr mGREDir; +}; + /***************************************************************************/ #ifdef JS_THREADSAFE @@ -919,7 +941,7 @@ static int usage(void) { fprintf(gErrFile, "%s\n", JS_GetImplementationVersion()); - fprintf(gErrFile, "usage: xpcshell [-PswWxCij] [-v version] [-f scriptfile] [-e script] [scriptfile] [scriptarg...]\n"); + fprintf(gErrFile, "usage: xpcshell [-g gredir] [-PswWxCij] [-v version] [-f scriptfile] [-e script] [scriptfile] [scriptarg...]\n"); return 2; } @@ -1548,9 +1570,39 @@ main(int argc, char **argv, char **envp) gErrFile = stderr; gOutFile = stdout; gInFile = stdin; + + NS_LogInit(); + + nsCOMPtr appFile; + rv = XRE_GetBinaryPath(argv[0], getter_AddRefs(appFile)); + if (NS_FAILED(rv)) { + printf("Couldn't figure application file.\n"); + return 1; + } + nsCOMPtr appDir; + rv = appFile->GetParent(getter_AddRefs(appDir)); + if (NS_FAILED(rv)) { + printf("Couldn't get application directory.\n"); + return 1; + } + + XPCShellDirProvider dirprovider; + + if (argc > 1 && !strcmp(argv[1], "-g")) { + if (argc < 3) + return usage(); + + if (!dirprovider.SetGREDir(argv[2])) { + printf("SetGREDir failed.\n"); + return 1; + } + argc -= 2; + argv += 2; + } + { nsCOMPtr servMan; - rv = NS_InitXPCOM2(getter_AddRefs(servMan), nsnull, nsnull); + rv = NS_InitXPCOM2(getter_AddRefs(servMan), appDir, &dirprovider); if (NS_FAILED(rv)) { printf("NS_InitXPCOM failed!\n"); return 1; @@ -1716,9 +1768,49 @@ main(int argc, char **argv, char **envp) bogus = nsnull; #endif + appDir = nsnull; + appFile = nsnull; + dirprovider.ClearGREDir(); + + NS_LogTerm(); + #ifdef XP_MACOSX FinishAutoreleasePool(); #endif return result; } + +PRBool +XPCShellDirProvider::SetGREDir(const char *dir) +{ + nsresult rv = XRE_GetFileFromPath(dir, getter_AddRefs(mGREDir)); + return NS_SUCCEEDED(rv); +} + +NS_IMETHODIMP_(nsrefcnt) +XPCShellDirProvider::AddRef() +{ + return 2; +} + +NS_IMETHODIMP_(nsrefcnt) +XPCShellDirProvider::Release() +{ + return 1; +} + +NS_IMPL_QUERY_INTERFACE1(XPCShellDirProvider, nsIDirectoryServiceProvider) + +NS_IMETHODIMP +XPCShellDirProvider::GetFile(const char *prop, PRBool *persistent, + nsIFile* *result) +{ + if (mGREDir && !strcmp(prop, NS_GRE_DIR)) { + *persistent = PR_TRUE; + NS_ADDREF(*result = mGREDir); + return NS_OK; + } + + return NS_ERROR_FAILURE; +}