Bug 1880216 - Migrate Fenix docs into Sphinx. r=owlish,geckoview-reviewers,android...
[gecko.git] / startupcache / StartupCacheUtils.cpp
blobc8dcdd7f11731290c0f291981d54a6f62a84cbf2
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 UniqueFreePtr<char[]>* buffer,
70 uint32_t* len) {
71 nsresult rv;
72 nsCOMPtr<nsIInputStream> inputStream;
73 rv = storageStream->NewInputStream(0, getter_AddRefs(inputStream));
74 NS_ENSURE_SUCCESS(rv, rv);
76 uint64_t avail64;
77 rv = inputStream->Available(&avail64);
78 NS_ENSURE_SUCCESS(rv, rv);
79 NS_ENSURE_TRUE(avail64 <= UINT32_MAX, NS_ERROR_FILE_TOO_BIG);
81 uint32_t avail = (uint32_t)avail64;
82 auto temp = UniqueFreePtr<char[]>(
83 reinterpret_cast<char*>(malloc(sizeof(char) * avail)));
84 uint32_t read;
85 rv = inputStream->Read(temp.get(), avail, &read);
86 if (NS_SUCCEEDED(rv) && avail != read) rv = NS_ERROR_UNEXPECTED;
88 if (NS_FAILED(rv)) {
89 return rv;
92 *len = avail;
93 *buffer = std::move(temp);
94 return NS_OK;
97 static const char baseName[2][5] = {"gre/", "app/"};
99 static inline bool canonicalizeBase(nsAutoCString& spec, nsACString& out) {
100 nsAutoCString greBase, appBase;
101 nsresult rv = mozilla::Omnijar::GetURIString(mozilla::Omnijar::GRE, greBase);
102 if (NS_FAILED(rv) || !greBase.Length()) return false;
104 rv = mozilla::Omnijar::GetURIString(mozilla::Omnijar::APP, appBase);
105 if (NS_FAILED(rv)) return false;
107 bool underGre = StringBeginsWith(spec, greBase);
108 bool underApp = appBase.Length() && StringBeginsWith(spec, appBase);
110 if (!underGre && !underApp) return false;
113 * At this point, if both underGre and underApp are true, it can be one
114 * of the two following cases:
115 * - the GRE directory points to a subdirectory of the APP directory,
116 * meaning spec points under GRE.
117 * - the APP directory points to a subdirectory of the GRE directory,
118 * meaning spec points under APP.
119 * Checking the GRE and APP path length is enough to know in which case
120 * we are.
122 if (underGre && underApp && greBase.Length() < appBase.Length())
123 underGre = false;
125 out.AppendLiteral("/resource/");
126 out.Append(
127 baseName[underGre ? mozilla::Omnijar::GRE : mozilla::Omnijar::APP]);
128 out.Append(Substring(spec, underGre ? greBase.Length() : appBase.Length()));
129 return true;
133 * ResolveURI transforms a chrome: or resource: URI into the URI for its
134 * underlying resource, or returns any other URI unchanged.
136 nsresult ResolveURI(nsIURI* in, nsIURI** out) {
137 nsresult rv;
139 // Resolve resource:// URIs. At the end of this if/else block, we
140 // have both spec and uri variables identifying the same URI.
141 if (in->SchemeIs("resource")) {
142 nsCOMPtr<nsIIOService> ioService = do_GetIOService(&rv);
143 NS_ENSURE_SUCCESS(rv, rv);
145 nsCOMPtr<nsIProtocolHandler> ph;
146 rv = ioService->GetProtocolHandler("resource", getter_AddRefs(ph));
147 NS_ENSURE_SUCCESS(rv, rv);
149 nsCOMPtr<nsIResProtocolHandler> irph(do_QueryInterface(ph, &rv));
150 NS_ENSURE_SUCCESS(rv, rv);
152 nsAutoCString spec;
153 rv = irph->ResolveURI(in, spec);
154 NS_ENSURE_SUCCESS(rv, rv);
156 return ioService->NewURI(spec, nullptr, nullptr, out);
158 if (in->SchemeIs("chrome")) {
159 nsCOMPtr<nsIChromeRegistry> chromeReg =
160 mozilla::services::GetChromeRegistry();
161 if (!chromeReg) return NS_ERROR_UNEXPECTED;
163 return chromeReg->ConvertChromeURL(in, out);
166 *out = do_AddRef(in).take();
167 return NS_OK;
170 static nsresult PathifyURIImpl(nsIURI* in, nsACString& out) {
171 nsCOMPtr<nsIURI> uri;
172 nsresult rv = ResolveURI(in, getter_AddRefs(uri));
173 NS_ENSURE_SUCCESS(rv, rv);
175 nsAutoCString spec;
176 rv = uri->GetSpec(spec);
177 NS_ENSURE_SUCCESS(rv, rv);
179 if (!canonicalizeBase(spec, out)) {
180 if (uri->SchemeIs("file")) {
181 nsCOMPtr<nsIFileURL> baseFileURL;
182 baseFileURL = do_QueryInterface(uri, &rv);
183 NS_ENSURE_SUCCESS(rv, rv);
185 nsAutoCString path;
186 rv = baseFileURL->GetPathQueryRef(path);
187 NS_ENSURE_SUCCESS(rv, rv);
189 out.Append(path);
190 } else if (uri->SchemeIs("jar")) {
191 nsCOMPtr<nsIJARURI> jarURI = do_QueryInterface(uri, &rv);
192 NS_ENSURE_SUCCESS(rv, rv);
194 nsCOMPtr<nsIURI> jarFileURI;
195 rv = jarURI->GetJARFile(getter_AddRefs(jarFileURI));
196 NS_ENSURE_SUCCESS(rv, rv);
198 rv = PathifyURIImpl(jarFileURI, out);
199 NS_ENSURE_SUCCESS(rv, rv);
201 nsAutoCString path;
202 rv = jarURI->GetJAREntry(path);
203 NS_ENSURE_SUCCESS(rv, rv);
204 out.Append('/');
205 out.Append(path);
206 } else { // Very unlikely
207 rv = uri->GetSpec(spec);
208 NS_ENSURE_SUCCESS(rv, rv);
210 out.Append('/');
211 out.Append(spec);
214 return NS_OK;
217 nsresult PathifyURI(const char* loaderType, size_t loaderTypeLength, nsIURI* in,
218 nsACString& out) {
219 out.AssignASCII(loaderType, loaderTypeLength);
221 return PathifyURIImpl(in, out);
224 } // namespace scache
225 } // namespace mozilla