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 * Like `Init`, but returns a failed nsresult instead of crashing on failure.
93 static nsresult
FallibleInit(nsIFile
* aGrePath
= nullptr,
94 nsIFile
* aAppPath
= nullptr);
97 * Initializes the Omnijar API for a child process, given its argument
98 * list, if the `-greomni` flag and optionally also the `-appomni` flag
99 * is present. (`-appomni` is absent in the case of a unified jar.) If
100 * neither flag is present, the Omnijar API is not initialized. The
101 * flags, if present, will be removed from the argument list.
103 static void ChildProcessInit(int& aArgc
, char** aArgv
);
106 * Cleans up the Omnijar API
108 static void CleanUp();
111 * Returns an nsIFile pointing to the omni.jar file for GRE or APP.
112 * Returns nullptr when there is no corresponding omni.jar.
113 * Also returns nullptr for APP in the unified case.
115 static inline already_AddRefed
<nsIFile
> GetPath(Type aType
) {
116 MOZ_ASSERT(IsInitialized(), "Omnijar not initialized");
117 nsCOMPtr
<nsIFile
> path
= sPath
[aType
].get();
118 return path
.forget();
122 * Returns whether GRE or APP use an omni.jar. Returns PR_False for
123 * APP when using an omni.jar in the unified case.
125 static inline bool HasOmnijar(Type aType
) {
126 MOZ_ASSERT(IsInitialized(), "Omnijar not initialized");
127 return !!sPath
[aType
];
131 * Returns a nsZipArchive pointer for the omni.jar file for GRE or
132 * APP. Returns nullptr in the same cases GetPath() would.
134 static inline already_AddRefed
<nsZipArchive
> GetReader(Type aType
) {
135 MOZ_ASSERT(IsInitialized(), "Omnijar not initialized");
136 RefPtr
<nsZipArchive
> reader
= sReader
[aType
].get();
137 return reader
.forget();
141 * Returns a nsZipArchive pointer for the given path IAOI the given
142 * path is the omni.jar for either GRE or APP.
144 static already_AddRefed
<nsZipArchive
> GetReader(nsIFile
* aPath
);
147 * In the case of a nested omnijar, this returns the inner reader for the
148 * omnijar if aPath points to the outer archive and aEntry is the omnijar
149 * entry name. Returns null otherwise.
150 * In concrete terms: On Android the omnijar is nested inside the apk archive.
151 * GetReader("path/to.apk") returns the outer reader and GetInnerReader(
152 * "path/to.apk", "assets/omni.ja") returns the inner reader.
154 static already_AddRefed
<nsZipArchive
> GetInnerReader(
155 nsIFile
* aPath
, const nsACString
& aEntry
);
158 * Returns the URI string corresponding to the omni.jar or directory
159 * for GRE or APP. i.e. jar:/path/to/omni.jar!/ for omni.jar and
160 * /path/to/base/dir/ otherwise. Returns an empty string for APP in
162 * The returned URI is guaranteed to end with a slash.
164 static nsresult
GetURIString(Type aType
, nsACString
& aResult
);
168 * Used internally, respectively by Init() and CleanUp()
170 static nsresult
InitOne(nsIFile
* aPath
, Type aType
);
171 static void CleanUpOne(Type aType
);
172 }; /* class Omnijar */
175 * Returns whether or not the currently running build is an unpackaged
176 * developer build. This check is implemented by looking for omni.ja in the
177 * the obj/dist dir. We use this routine to detect when the build dir will
178 * use symlinks to the repo and object dir.
180 inline bool IsPackagedBuild() {
181 return Omnijar::HasOmnijar(mozilla::Omnijar::GRE
);
184 } /* namespace mozilla */
186 #endif /* mozilla_Omnijar_h */