Bug 1477919 [wpt PR 12154] - url: DecodeURLEscapeSequences() should not apply UTF...
[gecko.git] / startupcache / StartupCacheUtils.cpp
blob92f30f0ab34dc027e1a3bcb15cadab00620563a9
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/. */
5 #include "nsCOMPtr.h"
6 #include "nsIInputStream.h"
7 #include "nsIStringStream.h"
8 #include "nsNetUtil.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"
19 namespace mozilla {
20 namespace scache {
22 nsresult
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,
29 NS_ASSIGNMENT_ADOPT);
30 MOZ_ALWAYS_SUCCEEDS(rv);
32 nsCOMPtr<nsIObjectInputStream> objectInput =
33 NS_NewObjectInputStream(stringStream);
35 objectInput.forget(stream);
36 return NS_OK;
39 nsresult
40 NewObjectOutputWrappedStorageStream(nsIObjectOutputStream **wrapperStream,
41 nsIStorageStream** stream,
42 bool wantDebugStream)
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);
55 #ifdef DEBUG
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);
64 } else {
65 objectOutput.forget(wrapperStream);
67 #else
68 objectOutput.forget(wrapperStream);
69 #endif
71 storageStream.forget(stream);
72 return NS_OK;
75 nsresult
76 NewBufferFromStorageStream(nsIStorageStream *storageStream,
77 UniquePtr<char[]>* buffer, uint32_t* len)
79 nsresult rv;
80 nsCOMPtr<nsIInputStream> inputStream;
81 rv = storageStream->NewInputStream(0, getter_AddRefs(inputStream));
82 NS_ENSURE_SUCCESS(rv, rv);
84 uint64_t avail64;
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);
91 uint32_t read;
92 rv = inputStream->Read(temp.get(), avail, &read);
93 if (NS_SUCCEEDED(rv) && avail != read)
94 rv = NS_ERROR_UNEXPECTED;
96 if (NS_FAILED(rv)) {
97 return rv;
100 *len = avail;
101 *buffer = std::move(temp);
102 return NS_OK;
105 static const char baseName[2][5] = { "gre/", "app/" };
107 static inline bool
108 canonicalizeBase(nsAutoCString &spec,
109 nsACString &out)
111 nsAutoCString greBase, appBase;
112 nsresult rv = mozilla::Omnijar::GetURIString(mozilla::Omnijar::GRE, greBase);
113 if (NS_FAILED(rv) || !greBase.Length())
114 return false;
116 rv = mozilla::Omnijar::GetURIString(mozilla::Omnijar::APP, appBase);
117 if (NS_FAILED(rv))
118 return false;
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)
125 return false;
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
135 * we are.
137 if (underGre && underApp && greBase.Length() < appBase.Length())
138 underGre = false;
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()));
143 return true;
147 * ResolveURI transforms a chrome: or resource: URI into the URI for its
148 * underlying resource, or returns any other URI unchanged.
150 nsresult
151 ResolveURI(nsIURI *in, nsIURI **out)
153 bool equals;
154 nsresult rv;
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);
169 nsAutoCString spec;
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();
177 if (!chromeReg)
178 return NS_ERROR_UNEXPECTED;
180 return chromeReg->ConvertChromeURL(in, out);
183 *out = do_AddRef(in).take();
184 return NS_OK;
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
212 nsresult
213 PathifyURI(nsIURI *in, nsACString &out)
215 bool equals;
216 nsresult rv;
218 nsCOMPtr<nsIURI> uri;
219 rv = ResolveURI(in, getter_AddRefs(uri));
220 NS_ENSURE_SUCCESS(rv, rv);
222 nsAutoCString spec;
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);
232 nsAutoCString path;
233 rv = baseFileURL->GetPathQueryRef(path);
234 NS_ENSURE_SUCCESS(rv, rv);
236 out.Append(path);
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);
248 nsAutoCString path;
249 rv = jarURI->GetJAREntry(path);
250 NS_ENSURE_SUCCESS(rv, rv);
251 out.Append('/');
252 out.Append(path);
253 } else { // Very unlikely
254 rv = uri->GetSpec(spec);
255 NS_ENSURE_SUCCESS(rv, rv);
257 out.Append('/');
258 out.Append(spec);
261 return NS_OK;
264 } // namespace scache
265 } // namespace mozilla