Bug 1492664 - generate portable URLs for Android mach commands; r=nalexander
[gecko.git] / xpcom / build / Omnijar.h
bloba00f40d603ec38684973fd1a77d463562870f49d
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 * Cleans up the Omnijar API
93 static void CleanUp();
95 /**
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 * Returns the URI string corresponding to the omni.jar or directory
133 * for GRE or APP. i.e. jar:/path/to/omni.jar!/ for omni.jar and
134 * /path/to/base/dir/ otherwise. Returns an empty string for APP in
135 * the unified case.
136 * The returned URI is guaranteed to end with a slash.
138 static nsresult GetURIString(Type aType, nsACString& aResult);
140 private:
142 * Used internally, respectively by Init() and CleanUp()
144 static void InitOne(nsIFile* aPath, Type aType);
145 static void CleanUpOne(Type aType);
146 }; /* class Omnijar */
148 } /* namespace mozilla */
150 #endif /* mozilla_Omnijar_h */