Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 * This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #ifndef mozilla_Omnijar_h
7 #define mozilla_Omnijar_h
9 #include "nscore.h"
10 #include "nsCOMPtr.h"
11 #include "nsString.h"
12 #include "nsIFile.h"
13 #include "nsZipArchive.h"
15 class nsIURI;
17 namespace mozilla {
19 class Omnijar {
20 private:
21 /**
22 * Store an nsIFile for an omni.jar. We can store two paths here, one
23 * for GRE (corresponding to resource://gre/) and one for APP
24 * (corresponding to resource:/// and resource://app/), but only
25 * store one when both point to the same location (unified).
26 */
27 static nsIFile *sPath[2];
29 /**
30 * Cached nsZipArchives for the corresponding sPath
31 */
32 static nsZipArchive *sReader[2];
34 /**
35 * Has Omnijar::Init() been called?
36 */
37 static bool sInitialized;
39 public:
40 enum Type {
41 GRE = 0,
42 APP = 1
43 };
45 /**
46 * Returns whether SetBase has been called at least once with
47 * a valid nsIFile
48 */
49 static inline bool
50 IsInitialized()
51 {
52 return sInitialized;
53 }
55 /**
56 * Initializes the Omnijar API with the given directory or file for GRE and
57 * APP. Each of the paths given can be:
58 * - a file path, pointing to the omnijar file,
59 * - a directory path, pointing to a directory containing an "omni.jar" file,
60 * - nullptr for autodetection of an "omni.jar" file.
61 */
62 static void Init(nsIFile *aGrePath = nullptr, nsIFile *aAppPath = nullptr);
64 /**
65 * Cleans up the Omnijar API
66 */
67 static void CleanUp();
69 /**
70 * Returns an nsIFile pointing to the omni.jar file for GRE or APP.
71 * Returns nullptr when there is no corresponding omni.jar.
72 * Also returns nullptr for APP in the unified case.
73 */
74 static inline already_AddRefed<nsIFile>
75 GetPath(Type aType)
76 {
77 NS_ABORT_IF_FALSE(IsInitialized(), "Omnijar not initialized");
78 nsCOMPtr<nsIFile> path = sPath[aType];
79 return path.forget();
80 }
82 /**
83 * Returns whether GRE or APP use an omni.jar. Returns PR_False for
84 * APP when using an omni.jar in the unified case.
85 */
86 static inline bool
87 HasOmnijar(Type aType)
88 {
89 NS_ABORT_IF_FALSE(IsInitialized(), "Omnijar not initialized");
90 return !!sPath[aType];
91 }
93 /**
94 * Returns a nsZipArchive pointer for the omni.jar file for GRE or
95 * APP. Returns nullptr in the same cases GetPath() would.
96 */
97 static inline already_AddRefed<nsZipArchive>
98 GetReader(Type aType)
99 {
100 NS_ABORT_IF_FALSE(IsInitialized(), "Omnijar not initialized");
101 nsRefPtr<nsZipArchive> reader = sReader[aType];
102 return reader.forget();
103 }
105 /**
106 * Returns a nsZipArchive pointer for the given path IAOI the given
107 * path is the omni.jar for either GRE or APP.
108 */
109 static already_AddRefed<nsZipArchive> GetReader(nsIFile *aPath);
111 /**
112 * Returns the URI string corresponding to the omni.jar or directory
113 * for GRE or APP. i.e. jar:/path/to/omni.jar!/ for omni.jar and
114 * /path/to/base/dir/ otherwise. Returns an empty string for APP in
115 * the unified case.
116 * The returned URI is guaranteed to end with a slash.
117 */
118 static nsresult GetURIString(Type aType, nsACString &result);
120 private:
121 /**
122 * Used internally, respectively by Init() and CleanUp()
123 */
124 static void InitOne(nsIFile *aPath, Type aType);
125 static void CleanUpOne(Type aType);
127 }; /* class Omnijar */
129 } /* namespace mozilla */
131 #endif /* mozilla_Omnijar_h */