michael@0: /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- michael@0: * This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #ifndef mozilla_Omnijar_h michael@0: #define mozilla_Omnijar_h michael@0: michael@0: #include "nscore.h" michael@0: #include "nsCOMPtr.h" michael@0: #include "nsString.h" michael@0: #include "nsIFile.h" michael@0: #include "nsZipArchive.h" michael@0: michael@0: class nsIURI; michael@0: michael@0: namespace mozilla { michael@0: michael@0: class Omnijar { michael@0: private: michael@0: /** michael@0: * Store an nsIFile for an omni.jar. We can store two paths here, one michael@0: * for GRE (corresponding to resource://gre/) and one for APP michael@0: * (corresponding to resource:/// and resource://app/), but only michael@0: * store one when both point to the same location (unified). michael@0: */ michael@0: static nsIFile *sPath[2]; michael@0: michael@0: /** michael@0: * Cached nsZipArchives for the corresponding sPath michael@0: */ michael@0: static nsZipArchive *sReader[2]; michael@0: michael@0: /** michael@0: * Has Omnijar::Init() been called? michael@0: */ michael@0: static bool sInitialized; michael@0: michael@0: public: michael@0: enum Type { michael@0: GRE = 0, michael@0: APP = 1 michael@0: }; michael@0: michael@0: /** michael@0: * Returns whether SetBase has been called at least once with michael@0: * a valid nsIFile michael@0: */ michael@0: static inline bool michael@0: IsInitialized() michael@0: { michael@0: return sInitialized; michael@0: } michael@0: michael@0: /** michael@0: * Initializes the Omnijar API with the given directory or file for GRE and michael@0: * APP. Each of the paths given can be: michael@0: * - a file path, pointing to the omnijar file, michael@0: * - a directory path, pointing to a directory containing an "omni.jar" file, michael@0: * - nullptr for autodetection of an "omni.jar" file. michael@0: */ michael@0: static void Init(nsIFile *aGrePath = nullptr, nsIFile *aAppPath = nullptr); michael@0: michael@0: /** michael@0: * Cleans up the Omnijar API michael@0: */ michael@0: static void CleanUp(); michael@0: michael@0: /** michael@0: * Returns an nsIFile pointing to the omni.jar file for GRE or APP. michael@0: * Returns nullptr when there is no corresponding omni.jar. michael@0: * Also returns nullptr for APP in the unified case. michael@0: */ michael@0: static inline already_AddRefed michael@0: GetPath(Type aType) michael@0: { michael@0: NS_ABORT_IF_FALSE(IsInitialized(), "Omnijar not initialized"); michael@0: nsCOMPtr path = sPath[aType]; michael@0: return path.forget(); michael@0: } michael@0: michael@0: /** michael@0: * Returns whether GRE or APP use an omni.jar. Returns PR_False for michael@0: * APP when using an omni.jar in the unified case. michael@0: */ michael@0: static inline bool michael@0: HasOmnijar(Type aType) michael@0: { michael@0: NS_ABORT_IF_FALSE(IsInitialized(), "Omnijar not initialized"); michael@0: return !!sPath[aType]; michael@0: } michael@0: michael@0: /** michael@0: * Returns a nsZipArchive pointer for the omni.jar file for GRE or michael@0: * APP. Returns nullptr in the same cases GetPath() would. michael@0: */ michael@0: static inline already_AddRefed michael@0: GetReader(Type aType) michael@0: { michael@0: NS_ABORT_IF_FALSE(IsInitialized(), "Omnijar not initialized"); michael@0: nsRefPtr reader = sReader[aType]; michael@0: return reader.forget(); michael@0: } michael@0: michael@0: /** michael@0: * Returns a nsZipArchive pointer for the given path IAOI the given michael@0: * path is the omni.jar for either GRE or APP. michael@0: */ michael@0: static already_AddRefed GetReader(nsIFile *aPath); michael@0: michael@0: /** michael@0: * Returns the URI string corresponding to the omni.jar or directory michael@0: * for GRE or APP. i.e. jar:/path/to/omni.jar!/ for omni.jar and michael@0: * /path/to/base/dir/ otherwise. Returns an empty string for APP in michael@0: * the unified case. michael@0: * The returned URI is guaranteed to end with a slash. michael@0: */ michael@0: static nsresult GetURIString(Type aType, nsACString &result); michael@0: michael@0: private: michael@0: /** michael@0: * Used internally, respectively by Init() and CleanUp() michael@0: */ michael@0: static void InitOne(nsIFile *aPath, Type aType); michael@0: static void CleanUpOne(Type aType); michael@0: michael@0: }; /* class Omnijar */ michael@0: michael@0: } /* namespace mozilla */ michael@0: michael@0: #endif /* mozilla_Omnijar_h */