Line data Source code
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/. */
6 :
7 : #ifndef mozilla_Omnijar_h
8 : #define mozilla_Omnijar_h
9 :
10 : #include "nscore.h"
11 : #include "nsCOMPtr.h"
12 : #include "nsString.h"
13 : #include "nsIFile.h"
14 : #include "nsZipArchive.h"
15 :
16 : #include "mozilla/StaticPtr.h"
17 :
18 : namespace mozilla {
19 :
20 : class Omnijar
21 : {
22 : private:
23 : /**
24 : * Store an nsIFile for an omni.jar. We can store two paths here, one
25 : * for GRE (corresponding to resource://gre/) and one for APP
26 : * (corresponding to resource:/// and resource://app/), but only
27 : * store one when both point to the same location (unified).
28 : */
29 : static StaticRefPtr<nsIFile> sPath[2];
30 :
31 : /**
32 : * Cached nsZipArchives for the corresponding sPath
33 : */
34 : static StaticRefPtr<nsZipArchive> sReader[2];
35 :
36 : /**
37 : * Cached nsZipArchives for the outer jar, when using nested jars.
38 : * Otherwise nullptr.
39 : */
40 : static StaticRefPtr<nsZipArchive> sOuterReader[2];
41 :
42 : /**
43 : * Has Omnijar::Init() been called?
44 : */
45 : static bool sInitialized;
46 :
47 : /**
48 : * Is using unified GRE/APP jar?
49 : */
50 : static bool sIsUnified;
51 :
52 : public:
53 : enum Type
54 : {
55 : GRE = 0,
56 : APP = 1
57 : };
58 :
59 : private:
60 : /**
61 : * Returns whether we are using nested jars.
62 : */
63 0 : static inline bool IsNested(Type aType)
64 : {
65 0 : MOZ_ASSERT(IsInitialized(), "Omnijar not initialized");
66 0 : return !!sOuterReader[aType];
67 : }
68 :
69 : /**
70 : * Returns a nsZipArchive pointer for the outer jar file when using nested
71 : * jars. Returns nullptr in the same cases GetPath() would, or if not using
72 : * nested jars.
73 : */
74 0 : static inline already_AddRefed<nsZipArchive> GetOuterReader(Type aType)
75 : {
76 0 : MOZ_ASSERT(IsInitialized(), "Omnijar not initialized");
77 0 : RefPtr<nsZipArchive> reader = sOuterReader[aType].get();
78 0 : return reader.forget();
79 : }
80 :
81 : public:
82 : /**
83 : * Returns whether SetBase has been called at least once with
84 : * a valid nsIFile
85 : */
86 1320 : static inline bool IsInitialized() { return sInitialized; }
87 :
88 : /**
89 : * Initializes the Omnijar API with the given directory or file for GRE and
90 : * APP. Each of the paths given can be:
91 : * - a file path, pointing to the omnijar file,
92 : * - a directory path, pointing to a directory containing an "omni.jar" file,
93 : * - nullptr for autodetection of an "omni.jar" file.
94 : */
95 : static void Init(nsIFile* aGrePath = nullptr, nsIFile* aAppPath = nullptr);
96 :
97 : /**
98 : * Cleans up the Omnijar API
99 : */
100 : static void CleanUp();
101 :
102 : /**
103 : * Returns an nsIFile pointing to the omni.jar file for GRE or APP.
104 : * Returns nullptr when there is no corresponding omni.jar.
105 : * Also returns nullptr for APP in the unified case.
106 : */
107 378 : static inline already_AddRefed<nsIFile> GetPath(Type aType)
108 : {
109 378 : MOZ_ASSERT(IsInitialized(), "Omnijar not initialized");
110 756 : nsCOMPtr<nsIFile> path = sPath[aType].get();
111 756 : return path.forget();
112 : }
113 :
114 : /**
115 : * Returns whether GRE or APP use an omni.jar. Returns PR_False for
116 : * APP when using an omni.jar in the unified case.
117 : */
118 : static inline bool HasOmnijar(Type aType)
119 : {
120 : MOZ_ASSERT(IsInitialized(), "Omnijar not initialized");
121 : return !!sPath[aType];
122 : }
123 :
124 : /**
125 : * Returns a nsZipArchive pointer for the omni.jar file for GRE or
126 : * APP. Returns nullptr in the same cases GetPath() would.
127 : */
128 147 : static inline already_AddRefed<nsZipArchive> GetReader(Type aType)
129 : {
130 147 : MOZ_ASSERT(IsInitialized(), "Omnijar not initialized");
131 294 : RefPtr<nsZipArchive> reader = sReader[aType].get();
132 294 : return reader.forget();
133 : }
134 :
135 : /**
136 : * Returns a nsZipArchive pointer for the given path IAOI the given
137 : * path is the omni.jar for either GRE or APP.
138 : */
139 : static already_AddRefed<nsZipArchive> GetReader(nsIFile* aPath);
140 :
141 : /**
142 : * Returns the URI string corresponding to the omni.jar or directory
143 : * for GRE or APP. i.e. jar:/path/to/omni.jar!/ for omni.jar and
144 : * /path/to/base/dir/ otherwise. Returns an empty string for APP in
145 : * the unified case.
146 : * The returned URI is guaranteed to end with a slash.
147 : */
148 : static nsresult GetURIString(Type aType, nsACString& aResult);
149 :
150 : private:
151 : /**
152 : * Used internally, respectively by Init() and CleanUp()
153 : */
154 : static void InitOne(nsIFile* aPath, Type aType);
155 : static void CleanUpOne(Type aType);
156 : }; /* class Omnijar */
157 :
158 : } /* namespace mozilla */
159 :
160 : #endif /* mozilla_Omnijar_h */
|