Bug 556644 - 1. Move omnijar setup to NS_InitXPCOM and use omni.jar by default, r=bsmedberg a=blocking2.0
authorMichael Wu <mwu@mozilla.com>
Tue, 10 Aug 2010 15:08:06 -0700
changeset 49360 f80832cefeb5ae5743147b2175a690ed4816a8e0
parent 49359 7d3a8767a03be6de7488a7ce7b8b7585d39eab37
child 49361 f2bffbc6fee799b48b9900cdc96116432da1e110
push idunknown
push userunknown
push dateunknown
reviewersbsmedberg, blocking2.0
bugs556644
milestone2.0b4pre
Bug 556644 - 1. Move omnijar setup to NS_InitXPCOM and use omni.jar by default, r=bsmedberg a=blocking2.0
ipc/glue/GeckoChildProcessHost.cpp
toolkit/xre/nsAppRunner.cpp
xpcom/build/Omnijar.cpp
xpcom/build/nsXPComInit.cpp
--- a/ipc/glue/GeckoChildProcessHost.cpp
+++ b/ipc/glue/GeckoChildProcessHost.cpp
@@ -233,24 +233,16 @@ GeckoChildProcessHost::PerformAsyncLaunc
 #ifdef OS_LINUX
 #ifdef ANDROID
     path += "/lib";
 #endif
     newEnvVars["LD_LIBRARY_PATH"] = path.get();
 #elif OS_MACOSX
     newEnvVars["DYLD_LIBRARY_PATH"] = path.get();
 #endif
-#ifdef MOZ_OMNIJAR
-    // Make sure the child process can find the omnijar
-    // See ScopedXPCOMStartup::Initialize in nsAppRunner.cpp
-    nsCAutoString omnijarPath;
-    if (mozilla::OmnijarPath())
-      mozilla::OmnijarPath()->GetNativePath(omnijarPath);
-    newEnvVars["OMNIJAR_PATH"] = omnijarPath.get();
-#endif
   }
   else {
     exePath = FilePath(CommandLine::ForCurrentProcess()->argv()[0]);
     exePath = exePath.DirName();
   }
 
 #ifdef OS_MACOSX
   // We need to use an App Bundle on OS X so that we can hide
@@ -275,16 +267,27 @@ GeckoChildProcessHost::PerformAsyncLaunc
   // other end of the socketpair() from us
 
   std::vector<std::string> childArgv;
 
   childArgv.push_back(exePath.value());
 
   childArgv.insert(childArgv.end(), aExtraOpts.begin(), aExtraOpts.end());
 
+#ifdef MOZ_OMNIJAR
+  // Make sure the child process can find the omnijar
+  // See XRE_InitCommandLine in nsAppRunner.cpp
+  nsCAutoString omnijarPath;
+  if (mozilla::OmnijarPath()) {
+    mozilla::OmnijarPath()->GetNativePath(omnijarPath);
+    childArgv.push_back("-omnijar");
+    childArgv.push_back(omnijarPath.get());
+  }
+#endif
+
   childArgv.push_back(pidstring);
   childArgv.push_back(childProcessType);
 
 #if defined(MOZ_CRASHREPORTER)
 #  if defined(OS_LINUX)
   int childCrashFd, childCrashRemapFd;
   if (!CrashReporter::CreateNotificationPipeForChild(
         &childCrashFd, &childCrashRemapFd))
@@ -325,16 +328,28 @@ GeckoChildProcessHost::PerformAsyncLaunc
 
   for (std::vector<std::string>::iterator it = aExtraOpts.begin();
        it != aExtraOpts.end();
        ++it) {
       cmdLine.AppendLooseValue(UTF8ToWide(*it));
   }
 
   cmdLine.AppendLooseValue(std::wstring(mGroupId.get()));
+
+#ifdef MOZ_OMNIJAR
+  // Make sure the child process can find the omnijar
+  // See XRE_InitCommandLine in nsAppRunner.cpp
+  nsAutoString omnijarPath;
+  if (mozilla::OmnijarPath()) {
+    mozilla::OmnijarPath()->GetPath(omnijarPath);
+    cmdLine.AppendLooseValue(UTF8ToWide("-omnijar"));
+    cmdLine.AppendLooseValue(omnijarPath.get());
+  }
+#endif
+
   cmdLine.AppendLooseValue(UTF8ToWide(pidstring));
   cmdLine.AppendLooseValue(UTF8ToWide(childProcessType));
 #if defined(MOZ_CRASHREPORTER)
   cmdLine.AppendLooseValue(
     UTF8ToWide(CrashReporter::GetChildNotificationPipe()));
 #endif
 
   base::LaunchApp(cmdLine, false, false, &process);
--- a/toolkit/xre/nsAppRunner.cpp
+++ b/toolkit/xre/nsAppRunner.cpp
@@ -1109,20 +1109,16 @@ ScopedXPCOMStartup::~ScopedXPCOMStartup(
       appStartup->DestroyHiddenWindow();
 
     gDirServiceProvider->DoShutdown();
 
     WriteConsoleLog();
 
     NS_ShutdownXPCOM(mServiceManager);
     mServiceManager = nsnull;
-
-#ifdef MOZ_OMNIJAR
-    mozilla::SetOmnijar(nsnull);
-#endif
   }
 }
 
 // {95d89e3e-a169-41a3-8e56-719978e15b12}
 #define APPINFO_CID \
   { 0x95d89e3e, 0xa169, 0x41a3, { 0x8e, 0x56, 0x71, 0x99, 0x78, 0xe1, 0x5b, 0x12 } }
 
 // {0C4A446C-EE82-41f2-8D04-D366D2C7A7D4}
@@ -1170,26 +1166,16 @@ static const mozilla::Module kXREModule 
 NSMODULE_DEFN(Apprunner) = &kXREModule;
 
 nsresult
 ScopedXPCOMStartup::Initialize()
 {
   NS_ASSERTION(gDirServiceProvider, "Should not get here!");
 
   nsresult rv;
-#ifdef MOZ_OMNIJAR
-  nsCOMPtr<nsILocalFile> lf;
-  char *omnijarPath = getenv("OMNIJAR_PATH");
-  if (omnijarPath)
-    rv = NS_NewNativeLocalFile(nsDependentCString(omnijarPath), PR_TRUE, getter_AddRefs(lf));
-  else
-    rv = XRE_GetBinaryPath(gArgv[0], getter_AddRefs(lf));
-  if (NS_SUCCEEDED(rv))
-    mozilla::SetOmnijar(lf);
-#endif
 
 #ifndef MOZ_ENABLE_LIBXUL
 #ifndef _BUILD_STATIC_BIN
   XRE_AddStaticComponent(&kXREModule);
 #else
   for (const mozilla::Module *const *staticModules = kPStaticModules;
        *staticModules; ++staticModules)
       XRE_AddStaticComponent(*staticModules);
@@ -3772,26 +3758,16 @@ XRE_InitCommandLine(int aArgc, char* aAr
 {
   nsresult rv = NS_OK;
 
 #if defined(MOZ_IPC)
 
 #if defined(OS_WIN)
   CommandLine::Init(aArgc, aArgv);
 #else
-#ifdef MOZ_OMNIJAR
-  nsCOMPtr<nsILocalFile> lf;
-  char *omnijarPath = getenv("OMNIJAR_PATH");
-  if (omnijarPath)
-    rv = NS_NewNativeLocalFile(nsDependentCString(omnijarPath), PR_TRUE, getter_AddRefs(lf));
-  else
-    rv = XRE_GetBinaryPath(gArgv[0], getter_AddRefs(lf));
-  if (NS_SUCCEEDED(rv))
-    mozilla::SetOmnijar(lf);
-#endif
 
   // these leak on error, but that's OK: we'll just exit()
   char** canonArgs = new char*[aArgc];
 
   // get the canonical version of the binary's path
   nsCOMPtr<nsILocalFile> binFile;
   rv = XRE_GetBinaryPath(aArgv[0], getter_AddRefs(binFile));
   if (NS_FAILED(rv))
@@ -3813,16 +3789,35 @@ XRE_InitCommandLine(int aArgc, char* aAr
   NS_ASSERTION(!CommandLine::IsInitialized(), "Bad news!");
   CommandLine::Init(aArgc, canonArgs);
 
   for (int i = 0; i < aArgc; ++i)
       free(canonArgs[i]);
   delete[] canonArgs;
 #endif
 #endif
+
+#ifdef MOZ_OMNIJAR
+  const char *omnijarPath = nsnull;
+  ArgResult ar = CheckArg("omnijar", PR_FALSE, &omnijarPath);
+  if (ar == ARG_BAD) {
+    PR_fprintf(PR_STDERR, "Error: argument -omnijar requires an omnijar path\n");
+    return NS_ERROR_FAILURE;
+  }
+
+  if (!omnijarPath)
+    return rv;
+
+  nsCOMPtr<nsILocalFile> omnijar;
+  rv = NS_NewNativeLocalFile(nsDependentCString(omnijarPath), PR_TRUE,
+                             getter_AddRefs(omnijar));
+  if (NS_SUCCEEDED(rv))
+    mozilla::SetOmnijar(omnijar);
+#endif
+
   return rv;
 }
 
 nsresult
 XRE_DeinitCommandLine()
 {
   nsresult rv = NS_OK;
 
--- a/xpcom/build/Omnijar.cpp
+++ b/xpcom/build/Omnijar.cpp
@@ -40,49 +40,62 @@
 
 #include "nsILocalFile.h"
 #include "nsXULAppAPI.h"
 #include "nsZipArchive.h"
 
 static nsILocalFile* sOmnijarPath = nsnull;
 static nsZipArchive* sOmnijarReader = nsnull;
 
+static void
+SetupReader()
+{
+    if (!sOmnijarPath) {
+        return;
+    }
+
+    nsZipArchive* zipReader = new nsZipArchive();
+    if (!zipReader) {
+        NS_IF_RELEASE(sOmnijarPath);
+        return;
+    }
+
+    if (NS_FAILED(zipReader->OpenArchive(sOmnijarPath))) {
+        delete zipReader;
+        NS_IF_RELEASE(sOmnijarPath);
+        return;
+    }
+
+    sOmnijarReader = zipReader;
+}
+
 nsILocalFile*
 mozilla::OmnijarPath()
 {
+    if (!sOmnijarReader)
+        SetupReader();
+
     return sOmnijarPath;
 }
 
 nsZipArchive*
 mozilla::OmnijarReader()
 {
+    if (!sOmnijarReader)
+        SetupReader();
+
     return sOmnijarReader;
 }
 
 void
 mozilla::SetOmnijar(nsILocalFile* aPath)
 {
     NS_IF_RELEASE(sOmnijarPath);
     if (sOmnijarReader) {
         sOmnijarReader->CloseArchive();
         delete sOmnijarReader;
         sOmnijarReader = nsnull;
     }
 
-    if (!aPath) {
-        return;
-    }
-
-    nsZipArchive* zipReader = new nsZipArchive();
-    if (!zipReader) {
-        return;
-    }
-
-    if (NS_FAILED(zipReader->OpenArchive(aPath))) {
-        delete zipReader;
-        return;
-    }
-
-    sOmnijarReader = zipReader;
     sOmnijarPath = aPath;
-    NS_ADDREF(sOmnijarPath);
+    NS_IF_ADDREF(sOmnijarPath);
 }
 
--- a/xpcom/build/nsXPComInit.cpp
+++ b/xpcom/build/nsXPComInit.cpp
@@ -137,16 +137,17 @@ extern nsresult nsStringInputStreamConst
 #endif
 
 #include "nsSystemInfo.h"
 #include "nsMemoryReporterManager.h"
 
 #include <locale.h>
 #include "mozilla/Services.h"
 #include "mozilla/FunctionTimer.h"
+#include "mozilla/Omnijar.h"
 
 #include "nsChromeRegistry.h"
 #include "nsChromeProtocolHandler.h"
 
 #ifdef MOZ_IPC
 #include "base/at_exit.h"
 #include "base/command_line.h"
 #include "base/message_loop.h"
@@ -414,16 +415,17 @@ NS_InitXPCOM2(nsIServiceManager* *result
         setlocale(LC_ALL, "");
 #endif
 
 #if defined(XP_UNIX) || defined(XP_OS2)
     NS_TIME_FUNCTION_MARK("Next: startup native charset utils");
 
     NS_StartupNativeCharsetUtils();
 #endif
+
     NS_TIME_FUNCTION_MARK("Next: startup local file");
 
     NS_StartupLocalFile();
 
     StartupSpecialSystemDirectory();
 
     rv = nsDirectoryService::RealInit();
     if (NS_FAILED(rv))
@@ -452,16 +454,36 @@ NS_InitXPCOM2(nsIServiceManager* *result
         nsDirectoryService::gService->Set(NS_XPCOM_LIBRARY_FILE, xpcomLib);
     }
     
     if (appFileLocationProvider) {
         rv = nsDirectoryService::gService->RegisterProvider(appFileLocationProvider);
         if (NS_FAILED(rv)) return rv;
     }
 
+#ifdef MOZ_OMNIJAR
+    NS_TIME_FUNCTION_MARK("Next: Omnijar init");
+
+    if (!mozilla::OmnijarPath()) {
+        nsCOMPtr<nsILocalFile> omnijar;
+        nsCOMPtr<nsIFile> file;
+
+        rv = NS_ERROR_FAILURE;
+        nsDirectoryService::gService->Get(NS_GRE_DIR,
+                                          NS_GET_IID(nsIFile),
+                                          getter_AddRefs(file));
+        if (file)
+            rv = file->Append(NS_LITERAL_STRING("omni.jar"));
+        if (NS_SUCCEEDED(rv))
+            omnijar = do_QueryInterface(file);
+        if (NS_SUCCEEDED(rv))
+            mozilla::SetOmnijar(omnijar);
+    }
+#endif
+
 #ifdef MOZ_IPC
     if ((sCommandLineWasInitialized = !CommandLine::IsInitialized())) {
         NS_TIME_FUNCTION_MARK("Next: IPC command line init");
 
 #ifdef OS_WIN
         CommandLine::Init(0, nsnull);
 #else
         nsCOMPtr<nsIFile> binaryFile;
@@ -729,14 +751,18 @@ ShutdownXPCOM(nsIServiceManager* servMgr
         sCommandLineWasInitialized = false;
     }
     if (sExitManager) {
         delete sExitManager;
         sExitManager = nsnull;
     }
 #endif
 
+#ifdef MOZ_OMNIJAR
+    mozilla::SetOmnijar(nsnull);
+#endif
+
     NS_LogTerm();
 
     return NS_OK;
 }
 
 } // namespace mozilla