1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #include "nsIInputStream.h"
7 #include "nsIStringStream.h"
9 #include "nsIFileURL.h"
10 #include "nsIJARURI.h"
11 #include "nsIResProtocolHandler.h"
12 #include "nsIChromeRegistry.h"
13 #include "nsAutoPtr.h"
14 #include "nsStringStream.h"
15 #include "StartupCacheUtils.h"
16 #include "mozilla/scache/StartupCache.h"
17 #include "mozilla/Omnijar.h"
23 NewObjectInputStreamFromBuffer(UniquePtr
<char[]> buffer
, uint32_t len
,
24 nsIObjectInputStream
** stream
)
26 nsCOMPtr
<nsIInputStream
> stringStream
;
27 nsresult rv
= NS_NewByteInputStream(getter_AddRefs(stringStream
),
28 buffer
.release(), len
,
30 MOZ_ALWAYS_SUCCEEDS(rv
);
32 nsCOMPtr
<nsIObjectInputStream
> objectInput
=
33 NS_NewObjectInputStream(stringStream
);
35 objectInput
.forget(stream
);
40 NewObjectOutputWrappedStorageStream(nsIObjectOutputStream
**wrapperStream
,
41 nsIStorageStream
** stream
,
44 nsCOMPtr
<nsIStorageStream
> storageStream
;
46 nsresult rv
= NS_NewStorageStream(256, UINT32_MAX
, getter_AddRefs(storageStream
));
47 NS_ENSURE_SUCCESS(rv
, rv
);
49 nsCOMPtr
<nsIOutputStream
> outputStream
50 = do_QueryInterface(storageStream
);
52 nsCOMPtr
<nsIObjectOutputStream
> objectOutput
53 = NS_NewObjectOutputStream(outputStream
);
56 if (wantDebugStream
) {
57 // Wrap in debug stream to detect unsupported writes of
58 // multiply-referenced non-singleton objects
59 StartupCache
* sc
= StartupCache::GetSingleton();
60 NS_ENSURE_TRUE(sc
, NS_ERROR_UNEXPECTED
);
61 nsCOMPtr
<nsIObjectOutputStream
> debugStream
;
62 sc
->GetDebugObjectOutputStream(objectOutput
, getter_AddRefs(debugStream
));
63 debugStream
.forget(wrapperStream
);
65 objectOutput
.forget(wrapperStream
);
68 objectOutput
.forget(wrapperStream
);
71 storageStream
.forget(stream
);
76 NewBufferFromStorageStream(nsIStorageStream
*storageStream
,
77 UniquePtr
<char[]>* buffer
, uint32_t* len
)
80 nsCOMPtr
<nsIInputStream
> inputStream
;
81 rv
= storageStream
->NewInputStream(0, getter_AddRefs(inputStream
));
82 NS_ENSURE_SUCCESS(rv
, rv
);
85 rv
= inputStream
->Available(&avail64
);
86 NS_ENSURE_SUCCESS(rv
, rv
);
87 NS_ENSURE_TRUE(avail64
<= UINT32_MAX
, NS_ERROR_FILE_TOO_BIG
);
89 uint32_t avail
= (uint32_t)avail64
;
90 auto temp
= MakeUnique
<char[]>(avail
);
92 rv
= inputStream
->Read(temp
.get(), avail
, &read
);
93 if (NS_SUCCEEDED(rv
) && avail
!= read
)
94 rv
= NS_ERROR_UNEXPECTED
;
101 *buffer
= std::move(temp
);
105 static const char baseName
[2][5] = { "gre/", "app/" };
108 canonicalizeBase(nsAutoCString
&spec
,
111 nsAutoCString greBase
, appBase
;
112 nsresult rv
= mozilla::Omnijar::GetURIString(mozilla::Omnijar::GRE
, greBase
);
113 if (NS_FAILED(rv
) || !greBase
.Length())
116 rv
= mozilla::Omnijar::GetURIString(mozilla::Omnijar::APP
, appBase
);
120 bool underGre
= !greBase
.Compare(spec
.get(), false, greBase
.Length());
121 bool underApp
= appBase
.Length() &&
122 !appBase
.Compare(spec
.get(), false, appBase
.Length());
124 if (!underGre
&& !underApp
)
128 * At this point, if both underGre and underApp are true, it can be one
129 * of the two following cases:
130 * - the GRE directory points to a subdirectory of the APP directory,
131 * meaning spec points under GRE.
132 * - the APP directory points to a subdirectory of the GRE directory,
133 * meaning spec points under APP.
134 * Checking the GRE and APP path length is enough to know in which case
137 if (underGre
&& underApp
&& greBase
.Length() < appBase
.Length())
140 out
.AppendLiteral("/resource/");
141 out
.Append(baseName
[underGre
? mozilla::Omnijar::GRE
: mozilla::Omnijar::APP
]);
142 out
.Append(Substring(spec
, underGre
? greBase
.Length() : appBase
.Length()));
147 * ResolveURI transforms a chrome: or resource: URI into the URI for its
148 * underlying resource, or returns any other URI unchanged.
151 ResolveURI(nsIURI
*in
, nsIURI
**out
)
156 // Resolve resource:// URIs. At the end of this if/else block, we
157 // have both spec and uri variables identifying the same URI.
158 if (NS_SUCCEEDED(in
->SchemeIs("resource", &equals
)) && equals
) {
159 nsCOMPtr
<nsIIOService
> ioService
= do_GetIOService(&rv
);
160 NS_ENSURE_SUCCESS(rv
, rv
);
162 nsCOMPtr
<nsIProtocolHandler
> ph
;
163 rv
= ioService
->GetProtocolHandler("resource", getter_AddRefs(ph
));
164 NS_ENSURE_SUCCESS(rv
, rv
);
166 nsCOMPtr
<nsIResProtocolHandler
> irph(do_QueryInterface(ph
, &rv
));
167 NS_ENSURE_SUCCESS(rv
, rv
);
170 rv
= irph
->ResolveURI(in
, spec
);
171 NS_ENSURE_SUCCESS(rv
, rv
);
173 return ioService
->NewURI(spec
, nullptr, nullptr, out
);
174 } else if (NS_SUCCEEDED(in
->SchemeIs("chrome", &equals
)) && equals
) {
175 nsCOMPtr
<nsIChromeRegistry
> chromeReg
=
176 mozilla::services::GetChromeRegistryService();
178 return NS_ERROR_UNEXPECTED
;
180 return chromeReg
->ConvertChromeURL(in
, out
);
183 *out
= do_AddRef(in
).take();
188 * PathifyURI transforms uris into useful zip paths
189 * to make it easier to manipulate startup cache entries
190 * using standard zip tools.
191 * Transformations applied:
192 * * resource:// URIs are resolved to their corresponding file/jar URI to
193 * canonicalize resources URIs other than gre and app.
194 * * Paths under GRE or APP directory have their base path replaced with
195 * resource/gre or resource/app to avoid depending on install location.
196 * * jar:file:///path/to/file.jar!/sub/path urls are replaced with
197 * /path/to/file.jar/sub/path
199 * The result is appended to the string passed in. Adding a prefix before
200 * calling is recommended to avoid colliding with other cache users.
202 * For example, in the js loader (string is prefixed with jsloader by caller):
203 * resource://gre/modules/XPCOMUtils.jsm or
204 * file://$GRE_DIR/modules/XPCOMUtils.jsm or
205 * jar:file://$GRE_DIR/omni.jar!/modules/XPCOMUtils.jsm becomes
206 * jsloader/resource/gre/modules/XPCOMUtils.jsm
207 * file://$PROFILE_DIR/extensions/{uuid}/components/component.js becomes
208 * jsloader/$PROFILE_DIR/extensions/%7Buuid%7D/components/component.js
209 * jar:file://$PROFILE_DIR/extensions/some.xpi!/components/component.js becomes
210 * jsloader/$PROFILE_DIR/extensions/some.xpi/components/component.js
213 PathifyURI(nsIURI
*in
, nsACString
&out
)
218 nsCOMPtr
<nsIURI
> uri
;
219 rv
= ResolveURI(in
, getter_AddRefs(uri
));
220 NS_ENSURE_SUCCESS(rv
, rv
);
223 rv
= uri
->GetSpec(spec
);
224 NS_ENSURE_SUCCESS(rv
, rv
);
226 if (!canonicalizeBase(spec
, out
)) {
227 if (NS_SUCCEEDED(uri
->SchemeIs("file", &equals
)) && equals
) {
228 nsCOMPtr
<nsIFileURL
> baseFileURL
;
229 baseFileURL
= do_QueryInterface(uri
, &rv
);
230 NS_ENSURE_SUCCESS(rv
, rv
);
233 rv
= baseFileURL
->GetPathQueryRef(path
);
234 NS_ENSURE_SUCCESS(rv
, rv
);
237 } else if (NS_SUCCEEDED(uri
->SchemeIs("jar", &equals
)) && equals
) {
238 nsCOMPtr
<nsIJARURI
> jarURI
= do_QueryInterface(uri
, &rv
);
239 NS_ENSURE_SUCCESS(rv
, rv
);
241 nsCOMPtr
<nsIURI
> jarFileURI
;
242 rv
= jarURI
->GetJARFile(getter_AddRefs(jarFileURI
));
243 NS_ENSURE_SUCCESS(rv
, rv
);
245 rv
= PathifyURI(jarFileURI
, out
);
246 NS_ENSURE_SUCCESS(rv
, rv
);
249 rv
= jarURI
->GetJAREntry(path
);
250 NS_ENSURE_SUCCESS(rv
, rv
);
253 } else { // Very unlikely
254 rv
= uri
->GetSpec(spec
);
255 NS_ENSURE_SUCCESS(rv
, rv
);
264 } // namespace scache
265 } // namespace mozilla