Bug 1767106 [wpt PR 33883] - [block-in-inline] Add some test cases for block-in-inlin...
[gecko.git] / startupcache / StartupCacheUtils.cpp
blob34ca602045503e31050065a33d119b1878dc9ba9
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 "nsNetUtil.h"
8 #include "nsIFileURL.h"
9 #include "nsIJARURI.h"
10 #include "nsIResProtocolHandler.h"
11 #include "nsIChromeRegistry.h"
12 #include "nsStringStream.h"
13 #include "StartupCacheUtils.h"
14 #include "mozilla/scache/StartupCache.h"
15 #include "mozilla/Omnijar.h"
17 namespace mozilla {
18 namespace scache {
20 nsresult NewObjectInputStreamFromBuffer(const char* buffer, uint32_t len,
21 nsIObjectInputStream** stream) {
22 nsCOMPtr<nsIInputStream> stringStream;
23 nsresult rv = NS_NewByteInputStream(getter_AddRefs(stringStream),
24 Span(buffer, len), NS_ASSIGNMENT_DEPEND);
25 MOZ_ALWAYS_SUCCEEDS(rv);
27 nsCOMPtr<nsIObjectInputStream> objectInput =
28 NS_NewObjectInputStream(stringStream);
30 objectInput.forget(stream);
31 return NS_OK;
34 nsresult NewObjectOutputWrappedStorageStream(
35 nsIObjectOutputStream** wrapperStream, nsIStorageStream** stream,
36 bool wantDebugStream) {
37 nsCOMPtr<nsIStorageStream> storageStream;
39 nsresult rv =
40 NS_NewStorageStream(256, UINT32_MAX, getter_AddRefs(storageStream));
41 NS_ENSURE_SUCCESS(rv, rv);
43 nsCOMPtr<nsIOutputStream> outputStream = do_QueryInterface(storageStream);
45 nsCOMPtr<nsIObjectOutputStream> objectOutput =
46 NS_NewObjectOutputStream(outputStream);
48 #ifdef DEBUG
49 if (wantDebugStream) {
50 // Wrap in debug stream to detect unsupported writes of
51 // multiply-referenced non-singleton objects
52 StartupCache* sc = StartupCache::GetSingleton();
53 NS_ENSURE_TRUE(sc, NS_ERROR_UNEXPECTED);
54 nsCOMPtr<nsIObjectOutputStream> debugStream;
55 sc->GetDebugObjectOutputStream(objectOutput, getter_AddRefs(debugStream));
56 debugStream.forget(wrapperStream);
57 } else {
58 objectOutput.forget(wrapperStream);
60 #else
61 objectOutput.forget(wrapperStream);
62 #endif
64 storageStream.forget(stream);
65 return NS_OK;
68 nsresult NewBufferFromStorageStream(nsIStorageStream* storageStream,
69 UniquePtr<char[]>* buffer, uint32_t* len) {
70 nsresult rv;
71 nsCOMPtr<nsIInputStream> inputStream;
72 rv = storageStream->NewInputStream(0, getter_AddRefs(inputStream));
73 NS_ENSURE_SUCCESS(rv, rv);
75 uint64_t avail64;
76 rv = inputStream->Available(&avail64);
77 NS_ENSURE_SUCCESS(rv, rv);
78 NS_ENSURE_TRUE(avail64 <= UINT32_MAX, NS_ERROR_FILE_TOO_BIG);
80 uint32_t avail = (uint32_t)avail64;
81 auto temp = MakeUnique<char[]>(avail);
82 uint32_t read;
83 rv = inputStream->Read(temp.get(), avail, &read);
84 if (NS_SUCCEEDED(rv) && avail != read) rv = NS_ERROR_UNEXPECTED;
86 if (NS_FAILED(rv)) {
87 return rv;
90 *len = avail;
91 *buffer = std::move(temp);
92 return NS_OK;
95 static const char baseName[2][5] = {"gre/", "app/"};
97 static inline bool canonicalizeBase(nsAutoCString& spec, nsACString& out) {
98 nsAutoCString greBase, appBase;
99 nsresult rv = mozilla::Omnijar::GetURIString(mozilla::Omnijar::GRE, greBase);
100 if (NS_FAILED(rv) || !greBase.Length()) return false;
102 rv = mozilla::Omnijar::GetURIString(mozilla::Omnijar::APP, appBase);
103 if (NS_FAILED(rv)) return false;
105 bool underGre = !greBase.Compare(spec.get(), false, greBase.Length());
106 bool underApp =
107 appBase.Length() && !appBase.Compare(spec.get(), false, appBase.Length());
109 if (!underGre && !underApp) return false;
112 * At this point, if both underGre and underApp are true, it can be one
113 * of the two following cases:
114 * - the GRE directory points to a subdirectory of the APP directory,
115 * meaning spec points under GRE.
116 * - the APP directory points to a subdirectory of the GRE directory,
117 * meaning spec points under APP.
118 * Checking the GRE and APP path length is enough to know in which case
119 * we are.
121 if (underGre && underApp && greBase.Length() < appBase.Length())
122 underGre = false;
124 out.AppendLiteral("/resource/");
125 out.Append(
126 baseName[underGre ? mozilla::Omnijar::GRE : mozilla::Omnijar::APP]);
127 out.Append(Substring(spec, underGre ? greBase.Length() : appBase.Length()));
128 return true;
132 * ResolveURI transforms a chrome: or resource: URI into the URI for its
133 * underlying resource, or returns any other URI unchanged.
135 nsresult ResolveURI(nsIURI* in, nsIURI** out) {
136 nsresult rv;
138 // Resolve resource:// URIs. At the end of this if/else block, we
139 // have both spec and uri variables identifying the same URI.
140 if (in->SchemeIs("resource")) {
141 nsCOMPtr<nsIIOService> ioService = do_GetIOService(&rv);
142 NS_ENSURE_SUCCESS(rv, rv);
144 nsCOMPtr<nsIProtocolHandler> ph;
145 rv = ioService->GetProtocolHandler("resource", getter_AddRefs(ph));
146 NS_ENSURE_SUCCESS(rv, rv);
148 nsCOMPtr<nsIResProtocolHandler> irph(do_QueryInterface(ph, &rv));
149 NS_ENSURE_SUCCESS(rv, rv);
151 nsAutoCString spec;
152 rv = irph->ResolveURI(in, spec);
153 NS_ENSURE_SUCCESS(rv, rv);
155 return ioService->NewURI(spec, nullptr, nullptr, out);
157 if (in->SchemeIs("chrome")) {
158 nsCOMPtr<nsIChromeRegistry> chromeReg =
159 mozilla::services::GetChromeRegistry();
160 if (!chromeReg) return NS_ERROR_UNEXPECTED;
162 return chromeReg->ConvertChromeURL(in, out);
165 *out = do_AddRef(in).take();
166 return NS_OK;
169 static nsresult PathifyURIImpl(nsIURI* in, nsACString& out) {
170 nsCOMPtr<nsIURI> uri;
171 nsresult rv = ResolveURI(in, getter_AddRefs(uri));
172 NS_ENSURE_SUCCESS(rv, rv);
174 nsAutoCString spec;
175 rv = uri->GetSpec(spec);
176 NS_ENSURE_SUCCESS(rv, rv);
178 if (!canonicalizeBase(spec, out)) {
179 if (uri->SchemeIs("file")) {
180 nsCOMPtr<nsIFileURL> baseFileURL;
181 baseFileURL = do_QueryInterface(uri, &rv);
182 NS_ENSURE_SUCCESS(rv, rv);
184 nsAutoCString path;
185 rv = baseFileURL->GetPathQueryRef(path);
186 NS_ENSURE_SUCCESS(rv, rv);
188 out.Append(path);
189 } else if (uri->SchemeIs("jar")) {
190 nsCOMPtr<nsIJARURI> jarURI = do_QueryInterface(uri, &rv);
191 NS_ENSURE_SUCCESS(rv, rv);
193 nsCOMPtr<nsIURI> jarFileURI;
194 rv = jarURI->GetJARFile(getter_AddRefs(jarFileURI));
195 NS_ENSURE_SUCCESS(rv, rv);
197 rv = PathifyURIImpl(jarFileURI, out);
198 NS_ENSURE_SUCCESS(rv, rv);
200 nsAutoCString path;
201 rv = jarURI->GetJAREntry(path);
202 NS_ENSURE_SUCCESS(rv, rv);
203 out.Append('/');
204 out.Append(path);
205 } else { // Very unlikely
206 rv = uri->GetSpec(spec);
207 NS_ENSURE_SUCCESS(rv, rv);
209 out.Append('/');
210 out.Append(spec);
213 return NS_OK;
216 nsresult PathifyURI(const char* loaderType, size_t loaderTypeLength, nsIURI* in,
217 nsACString& out) {
218 out.AssignASCII(loaderType, loaderTypeLength);
220 return PathifyURIImpl(in, out);
223 } // namespace scache
224 } // namespace mozilla