Bug 1850713: remove duplicated setting of early hint preloader id in `ScriptLoader...
[gecko.git] / xpcom / build / Omnijar.h
blobe334c5547823ddd59746584639ef269ccf620d4c
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
10 #include "nscore.h"
11 #include "nsCOMPtr.h"
12 #include "nsString.h"
13 #include "nsIFile.h"
14 #include "nsZipArchive.h"
16 #include "mozilla/StaticPtr.h"
18 namespace mozilla {
20 class Omnijar {
21 private:
22 /**
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];
30 /**
31 * Cached nsZipArchives for the corresponding sPath
33 static StaticRefPtr<nsZipArchive> sReader[2];
35 /**
36 * Cached nsZipArchives for the outer jar, when using nested jars.
37 * Otherwise nullptr.
39 static StaticRefPtr<nsZipArchive> sOuterReader[2];
41 /**
42 * Has Omnijar::Init() been called?
44 static bool sInitialized;
46 /**
47 * Is using unified GRE/APP jar?
49 static bool sIsUnified;
51 public:
52 enum Type { GRE = 0, APP = 1 };
54 private:
55 /**
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];
63 /**
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
66 * nested jars.
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();
74 public:
75 /**
76 * Returns whether SetBase has been called at least once with
77 * a valid nsIFile
79 static inline bool IsInitialized() { return sInitialized; }
81 /**
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);
90 /**
91 * Initializes the Omnijar API for a child process, given its argument
92 * list, if the `-greomni` flag and optionally also the `-appomni` flag
93 * is present. (`-appomni` is absent in the case of a unified jar.) If
94 * neither flag is present, the Omnijar API is not initialized. The
95 * flags, if present, will be removed from the argument list.
97 static void ChildProcessInit(int& aArgc, char** aArgv);
99 /**
100 * Cleans up the Omnijar API
102 static void CleanUp();
105 * Returns an nsIFile pointing to the omni.jar file for GRE or APP.
106 * Returns nullptr when there is no corresponding omni.jar.
107 * Also returns nullptr for APP in the unified case.
109 static inline already_AddRefed<nsIFile> GetPath(Type aType) {
110 MOZ_ASSERT(IsInitialized(), "Omnijar not initialized");
111 nsCOMPtr<nsIFile> path = sPath[aType].get();
112 return path.forget();
116 * Returns whether GRE or APP use an omni.jar. Returns PR_False for
117 * APP when using an omni.jar in the unified case.
119 static inline bool HasOmnijar(Type aType) {
120 MOZ_ASSERT(IsInitialized(), "Omnijar not initialized");
121 return !!sPath[aType];
125 * Returns a nsZipArchive pointer for the omni.jar file for GRE or
126 * APP. Returns nullptr in the same cases GetPath() would.
128 static inline already_AddRefed<nsZipArchive> GetReader(Type aType) {
129 MOZ_ASSERT(IsInitialized(), "Omnijar not initialized");
130 RefPtr<nsZipArchive> reader = sReader[aType].get();
131 return reader.forget();
135 * Returns a nsZipArchive pointer for the given path IAOI the given
136 * path is the omni.jar for either GRE or APP.
138 static already_AddRefed<nsZipArchive> GetReader(nsIFile* aPath);
141 * In the case of a nested omnijar, this returns the inner reader for the
142 * omnijar if aPath points to the outer archive and aEntry is the omnijar
143 * entry name. Returns null otherwise.
144 * In concrete terms: On Android the omnijar is nested inside the apk archive.
145 * GetReader("path/to.apk") returns the outer reader and GetInnerReader(
146 * "path/to.apk", "assets/omni.ja") returns the inner reader.
148 static already_AddRefed<nsZipArchive> GetInnerReader(
149 nsIFile* aPath, const nsACString& aEntry);
152 * Returns the URI string corresponding to the omni.jar or directory
153 * for GRE or APP. i.e. jar:/path/to/omni.jar!/ for omni.jar and
154 * /path/to/base/dir/ otherwise. Returns an empty string for APP in
155 * the unified case.
156 * The returned URI is guaranteed to end with a slash.
158 static nsresult GetURIString(Type aType, nsACString& aResult);
160 private:
162 * Used internally, respectively by Init() and CleanUp()
164 static void InitOne(nsIFile* aPath, Type aType);
165 static void CleanUpOne(Type aType);
166 }; /* class Omnijar */
169 * Returns whether or not the currently running build is an unpackaged
170 * developer build. This check is implemented by looking for omni.ja in the
171 * the obj/dist dir. We use this routine to detect when the build dir will
172 * use symlinks to the repo and object dir.
174 inline bool IsPackagedBuild() {
175 return Omnijar::HasOmnijar(mozilla::Omnijar::GRE);
178 } /* namespace mozilla */
180 #endif /* mozilla_Omnijar_h */