Bug 552121 - Add omnijar API [2/7], r=bsmedberg
authorMichael Wu <mwu@mozilla.com>
Fri, 21 May 2010 10:58:32 -0700
changeset 42540 171c1b972669bc6f3ef6b1fb7f3b1fdc9f049843
parent 42539 11b2042dee1bb9a60900b4d35a97a90379b39aa4
child 42541 43f6e1ce1122282deea0a50ffff97a36f99616c9
push idunknown
push userunknown
push dateunknown
reviewersbsmedberg
bugs552121
milestone1.9.3a5pre
Bug 552121 - Add omnijar API [2/7], r=bsmedberg
modules/libjar/objs.mk
toolkit/xre/nsAppRunner.cpp
toolkit/xre/nsXREDirProvider.cpp
xpcom/build/Makefile.in
xpcom/build/Omnijar.cpp
xpcom/build/Omnijar.h
--- a/modules/libjar/objs.mk
+++ b/modules/libjar/objs.mk
@@ -43,16 +43,17 @@ MODULES_LIBJAR_LCPPSRCS = \
 		nsXPTZipLoader.cpp \
 		nsJARProtocolHandler.cpp \
 		nsJARChannel.cpp  \
 		nsJARURI.cpp  \
 		$(NULL)
 
 MODULES_LIBJAR_LEXPORTS = \
 		zipstruct.h \
+		nsZipArchive.h \
 		$(NULL)
 
 MODULES_LIBJAR_LXPIDLSRCS = \
 		nsIZipReader.idl \
 		nsIJARChannel.idl \
 		nsIJARURI.idl \
 		nsIJARProtocolHandler.idl \
 		$(NULL)
--- a/toolkit/xre/nsAppRunner.cpp
+++ b/toolkit/xre/nsAppRunner.cpp
@@ -135,16 +135,17 @@
 #include "nsVersionComparator.h"
 
 #include "nsAppDirectoryServiceDefs.h"
 #include "nsXULAppAPI.h"
 #include "nsXREDirProvider.h"
 #include "nsToolkitCompsCID.h"
 
 #include "nsINIParser.h"
+#include "mozilla/Omnijar.h"
 
 #include <stdlib.h>
 
 #if defined(MOZ_SPLASHSCREEN)
 #include "nsSplashScreen.h"
 #endif
 
 #ifdef XP_UNIX
@@ -1066,16 +1067,20 @@ 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 } }
 
 static const nsModuleComponentInfo kComponents[] =
@@ -1119,16 +1124,23 @@ PRUint32 const kStaticModuleCount = NS_A
 #endif
 
 nsresult
 ScopedXPCOMStartup::Initialize()
 {
   NS_ASSERTION(gDirServiceProvider, "Should not get here!");
 
   nsresult rv;
+#ifdef MOZ_OMNIJAR
+  nsCOMPtr<nsILocalFile> lf;
+  rv = XRE_GetBinaryPath(gArgv[0], getter_AddRefs(lf));
+  if (NS_SUCCEEDED(rv))
+    mozilla::SetOmnijar(lf);
+#endif
+
   rv = NS_InitXPCOM3(&mServiceManager, gDirServiceProvider->GetAppDir(),
                      gDirServiceProvider,
                      kPStaticModules, kStaticModuleCount);
   if (NS_FAILED(rv)) {
     NS_ERROR("Couldn't start xpcom!");
     mServiceManager = nsnull;
   }
   else {
@@ -1284,21 +1296,16 @@ public:
 
 static void DumpArbitraryHelp()
 {
   nsresult rv;
 
   ScopedLogging log;
 
   {
-    nsXREDirProvider dirProvider;
-    rv = dirProvider.Initialize(gAppData->directory, gAppData->xreDirectory);
-    if (NS_FAILED(rv))
-      return;
-
     ScopedXPCOMStartup xpcom;
     xpcom.Initialize();
     xpcom.DoAutoreg();
 
     nsCOMPtr<nsICommandLineRunner> cmdline
       (do_CreateInstance("@mozilla.org/toolkit/command-line;1"));
     if (!cmdline)
       return;
@@ -2906,16 +2913,21 @@ XRE_main(int argc, char* argv[], const n
       Output(PR_TRUE, "Error: Platform version '%s' is not compatible with\n"
              "minVersion >= %s\nmaxVersion <= %s\n",
              gToolkitVersion,
              appData.minVersion, appData.maxVersion);
       return 1;
     }
   }
 
+  nsXREDirProvider dirProvider;
+  rv = dirProvider.Initialize(gAppData->directory, gAppData->xreDirectory);
+  if (NS_FAILED(rv))
+    return 1;
+
 #ifdef MOZ_CRASHREPORTER
   const char* crashreporterEnv = PR_GetEnv("MOZ_CRASHREPORTER");
   if (crashreporterEnv && *crashreporterEnv) {
     appData.flags |= NS_XRE_ENABLE_CRASH_REPORTER;
   }
 
   if ((appData.flags & NS_XRE_ENABLE_CRASH_REPORTER) &&
       NS_SUCCEEDED(
@@ -2934,21 +2946,18 @@ XRE_main(int argc, char* argv[], const n
       CrashReporter::AnnotateCrashReport(NS_LITERAL_CSTRING("Version"),
                                          nsDependentCString(appData.version));
     if (appData.buildID)
       CrashReporter::AnnotateCrashReport(NS_LITERAL_CSTRING("BuildID"),
                                          nsDependentCString(appData.buildID));
     CrashReporter::SetRestartArgs(argc, argv);
 
     // annotate other data (user id etc)
-    nsXREDirProvider dirProvider;
     nsCOMPtr<nsILocalFile> userAppDataDir;
-    rv = dirProvider.Initialize(gAppData->directory, gAppData->xreDirectory);
-    if (NS_SUCCEEDED(rv) &&
-        NS_SUCCEEDED(dirProvider.GetUserAppDataDirectory(
+    if (NS_SUCCEEDED(dirProvider.GetUserAppDataDirectory(
                                                          getter_AddRefs(userAppDataDir)))) {
       CrashReporter::SetupExtraData(userAppDataDir,
                                     nsDependentCString(appData.buildID));
 
       // see if we have a crashreporter-override.ini in the application directory
       nsCOMPtr<nsIFile> overrideini;
       PRBool exists;
       static char overrideEnv[MAXPATHLEN];
@@ -3069,21 +3078,16 @@ XRE_main(int argc, char* argv[], const n
 #endif
 
   MOZ_SPLASHSCREEN_UPDATE(20);
 
   rv = XRE_InitCommandLine(gArgc, gArgv);
   NS_ENSURE_SUCCESS(rv, 1);
 
   {
-    nsXREDirProvider dirProvider;
-    rv = dirProvider.Initialize(gAppData->directory, gAppData->xreDirectory);
-    if (NS_FAILED(rv))
-      return 1;
-
     // Check for -register, which registers chrome and then exits immediately.
     ar = CheckArg("register", PR_TRUE);
     if (ar == ARG_BAD) {
       PR_fprintf(PR_STDERR, "Error: argument -register is invalid when argument -osint is specified\n");
       return 1;
     } else if (ar == ARG_FOUND) {
       ScopedXPCOMStartup xpcom;
       rv = xpcom.Initialize();
--- a/toolkit/xre/nsXREDirProvider.cpp
+++ b/toolkit/xre/nsXREDirProvider.cpp
@@ -60,16 +60,17 @@
 
 #include "nsINIParser.h"
 #include "nsDependentString.h"
 #include "nsCOMArray.h"
 #include "nsArrayEnumerator.h"
 #include "nsEnumeratorUtils.h"
 #include "nsReadableUtils.h"
 #include "mozilla/Services.h"
+#include "mozilla/Omnijar.h"
 
 #include <stdlib.h>
 
 #ifdef XP_WIN
 #include <windows.h>
 #include <shlobj.h>
 // This is not defined by VC6.
 #ifndef CSIDL_LOCAL_APPDATA
@@ -698,20 +699,26 @@ nsXREDirProvider::GetFilesInternal(const
         directories.AppendObject(overrideFile);
     }
 
     rv = NS_NewArrayEnumerator(aResult, directories);
   }
   else if (!strcmp(aProperty, NS_CHROME_MANIFESTS_FILE_LIST)) {
     nsCOMArray<nsIFile> manifests;
 
-    nsCOMPtr<nsIFile> manifest;
-    mGREDir->Clone(getter_AddRefs(manifest));
-    manifest->AppendNative(NS_LITERAL_CSTRING("chrome"));
-    manifests.AppendObject(manifest);
+#ifdef MOZ_OMNIJAR
+    if (!mozilla::OmnijarPath()) {
+#endif
+        nsCOMPtr<nsIFile> manifest;
+        mGREDir->Clone(getter_AddRefs(manifest));
+        manifest->AppendNative(NS_LITERAL_CSTRING("chrome"));
+        manifests.AppendObject(manifest);
+#ifdef MOZ_OMNIJAR
+    }
+#endif
 
     PRBool eq;
     if (NS_SUCCEEDED(mXULAppDir->Equals(mGREDir, &eq)) && !eq) {
       nsCOMPtr<nsIFile> file;
       mXULAppDir->Clone(getter_AddRefs(file));
       file->AppendNative(NS_LITERAL_CSTRING("chrome"));
       PRBool exists;
       if (NS_SUCCEEDED(file->Exists(&exists)) && exists)
--- a/xpcom/build/Makefile.in
+++ b/xpcom/build/Makefile.in
@@ -72,16 +72,20 @@ CPPSRCS		= \
 		$(NULL)
 
 ifndef MOZ_ENABLE_LIBXUL
 ifeq (,$(filter-out WINNT WINCE OS2,$(OS_ARCH)))
 CPPSRCS += dlldeps.cpp
 endif
 endif
 
+ifdef MOZ_OMNIJAR
+CPPSRCS += Omnijar.cpp
+endif
+
 SHARED_LIBRARY_LIBS = \
 		../ds/$(LIB_PREFIX)xpcomds_s.$(LIB_SUFFIX) \
 		../io/$(LIB_PREFIX)xpcomio_s.$(LIB_SUFFIX) \
 		../components/$(LIB_PREFIX)xpcomcomponents_s.$(LIB_SUFFIX) \
 		../threads/$(LIB_PREFIX)xpcomthreads_s.$(LIB_SUFFIX) \
 		../proxy/src/$(LIB_PREFIX)xpcomproxy_s.$(LIB_SUFFIX) \
 		../base/$(LIB_PREFIX)xpcombase_s.$(LIB_SUFFIX) \
 		../reflect/xptcall/src/$(LIB_PREFIX)xptcall.$(LIB_SUFFIX) \
@@ -123,16 +127,17 @@ EXPORTS	= \
   xrecore.h \
   nsXULAppAPI.h \
   $(NULL)
 
 EXPORTS_mozilla = \
   XPCOM.h \
   Services.h \
   ServiceList.h \
+  Omnijar.h \
   $(NULL)
 
 # Force use of PIC
 FORCE_USE_PIC	= 1 
 
 ifndef MOZ_ENABLE_LIBXUL
 FORCE_SHARED_LIB = 1
 endif
new file mode 100644
--- /dev/null
+++ b/xpcom/build/Omnijar.cpp
@@ -0,0 +1,88 @@
+/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
+ * ***** 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 code.
+ *
+ * The Initial Developer of the Original Code is
+ * Mozilla Foundation.
+ * Portions created by the Initial Developer are Copyright (C) 2010
+ * the Initial Developer. All Rights Reserved.
+ *
+ * Contributor(s):
+ *   Michael Wu <mwu@mozilla.com>
+ *
+ * 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 ***** */
+
+#include "Omnijar.h"
+
+#include "nsILocalFile.h"
+#include "nsXULAppAPI.h"
+#include "nsZipArchive.h"
+
+static nsILocalFile* sOmnijarPath = nsnull;
+static nsZipArchive* sOmnijarReader = nsnull;
+
+nsILocalFile*
+mozilla::OmnijarPath()
+{
+    return sOmnijarPath;
+}
+
+nsZipArchive*
+mozilla::OmnijarReader()
+{
+    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);
+}
+
new file mode 100644
--- /dev/null
+++ b/xpcom/build/Omnijar.h
@@ -0,0 +1,62 @@
+/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
+ * ***** 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 code.
+ *
+ * The Initial Developer of the Original Code is
+ * Mozilla Foundation.
+ * Portions created by the Initial Developer are Copyright (C) 2010
+ * the Initial Developer. All Rights Reserved.
+ *
+ * Contributor(s):
+ *   Michael Wu <mwu@mozilla.com>
+ *
+ * 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 ***** */
+
+#ifndef mozilla_Omnijar_h
+#define mozilla_Omnijar_h
+
+class nsILocalFile;
+class nsZipArchive;
+
+#ifdef MOZ_OMNIJAR
+
+namespace mozilla {
+
+/**
+ * This returns the path to the omnijar.
+ * If the omnijar isn't available, this function will return null.
+ * Callers should fallback to flat packaging if null.
+ */
+nsILocalFile *OmnijarPath();
+nsZipArchive *OmnijarReader();
+void SetOmnijar(nsILocalFile* aPath);
+
+} /* namespace mozilla */
+
+#endif /* MOZ_OMNIJAR */
+
+#endif /* mozilla_Omnijar_h */