1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #ifndef mozilla_Omnijar_h
8 #define mozilla_Omnijar_h
14 #include "nsZipArchive.h"
16 #include "mozilla/StaticPtr.h"
23 * Store an nsIFile for an omni.jar. We can store two paths here, one
24 * for GRE (corresponding to resource://gre/) and one for APP
25 * (corresponding to resource:/// and resource://app/), but only
26 * store one when both point to the same location (unified).
28 static StaticRefPtr
<nsIFile
> sPath
[2];
31 * Cached nsZipArchives for the corresponding sPath
33 static StaticRefPtr
<nsZipArchive
> sReader
[2];
36 * Cached nsZipArchives for the outer jar, when using nested jars.
39 static StaticRefPtr
<nsZipArchive
> sOuterReader
[2];
42 * Has Omnijar::Init() been called?
44 static bool sInitialized
;
47 * Is using unified GRE/APP jar?
49 static bool sIsUnified
;
52 enum Type
{ GRE
= 0, APP
= 1 };
56 * Returns whether we are using nested jars.
58 static inline bool IsNested(Type aType
) {
59 MOZ_ASSERT(IsInitialized(), "Omnijar not initialized");
60 return !!sOuterReader
[aType
];
64 * Returns a nsZipArchive pointer for the outer jar file when using nested
65 * jars. Returns nullptr in the same cases GetPath() would, or if not using
68 static inline already_AddRefed
<nsZipArchive
> GetOuterReader(Type aType
) {
69 MOZ_ASSERT(IsInitialized(), "Omnijar not initialized");
70 RefPtr
<nsZipArchive
> reader
= sOuterReader
[aType
].get();
71 return reader
.forget();
76 * Returns whether SetBase has been called at least once with
79 static inline bool IsInitialized() { return sInitialized
; }
82 * Initializes the Omnijar API with the given directory or file for GRE and
83 * APP. Each of the paths given can be:
84 * - a file path, pointing to the omnijar file,
85 * - a directory path, pointing to a directory containing an "omni.jar" file,
86 * - nullptr for autodetection of an "omni.jar" file.
88 static void Init(nsIFile
* aGrePath
= nullptr, nsIFile
* aAppPath
= nullptr);
91 * Cleans up the Omnijar API
93 static void CleanUp();
96 * Returns an nsIFile pointing to the omni.jar file for GRE or APP.
97 * Returns nullptr when there is no corresponding omni.jar.
98 * Also returns nullptr for APP in the unified case.
100 static inline already_AddRefed
<nsIFile
> GetPath(Type aType
) {
101 MOZ_ASSERT(IsInitialized(), "Omnijar not initialized");
102 nsCOMPtr
<nsIFile
> path
= sPath
[aType
].get();
103 return path
.forget();
107 * Returns whether GRE or APP use an omni.jar. Returns PR_False for
108 * APP when using an omni.jar in the unified case.
110 static inline bool HasOmnijar(Type aType
) {
111 MOZ_ASSERT(IsInitialized(), "Omnijar not initialized");
112 return !!sPath
[aType
];
116 * Returns a nsZipArchive pointer for the omni.jar file for GRE or
117 * APP. Returns nullptr in the same cases GetPath() would.
119 static inline already_AddRefed
<nsZipArchive
> GetReader(Type aType
) {
120 MOZ_ASSERT(IsInitialized(), "Omnijar not initialized");
121 RefPtr
<nsZipArchive
> reader
= sReader
[aType
].get();
122 return reader
.forget();
126 * Returns a nsZipArchive pointer for the given path IAOI the given
127 * path is the omni.jar for either GRE or APP.
129 static already_AddRefed
<nsZipArchive
> GetReader(nsIFile
* aPath
);
132 * In the case of a nested omnijar, this returns the inner reader for the
133 * omnijar if aPath points to the outer archive and aEntry is the omnijar
134 * entry name. Returns null otherwise.
135 * In concrete terms: On Android the omnijar is nested inside the apk archive.
136 * GetReader("path/to.apk") returns the outer reader and GetInnerReader(
137 * "path/to.apk", "assets/omni.ja") returns the inner reader.
139 static already_AddRefed
<nsZipArchive
> GetInnerReader(
140 nsIFile
* aPath
, const nsACString
& aEntry
);
143 * Returns the URI string corresponding to the omni.jar or directory
144 * for GRE or APP. i.e. jar:/path/to/omni.jar!/ for omni.jar and
145 * /path/to/base/dir/ otherwise. Returns an empty string for APP in
147 * The returned URI is guaranteed to end with a slash.
149 static nsresult
GetURIString(Type aType
, nsACString
& aResult
);
153 * Used internally, respectively by Init() and CleanUp()
155 static void InitOne(nsIFile
* aPath
, Type aType
);
156 static void CleanUpOne(Type aType
);
157 }; /* class Omnijar */
160 * Returns whether or not the currently running build is an unpackaged
161 * developer build. This check is implemented by looking for omni.ja in the
162 * the obj/dist dir. We use this routine to detect when the build dir will
163 * use symlinks to the repo and object dir.
165 inline bool IsDevelopmentBuild() {
166 return !Omnijar::HasOmnijar(mozilla::Omnijar::GRE
);
169 } /* namespace mozilla */
171 #endif /* mozilla_Omnijar_h */