Files
palemoon27/startupcache/StartupCacheUtils.cpp
T
roytam1 6af3bad98f import changes from `dev' branch of rmottola/Arctic-Fox:
- Bug 1251090 - Shell functions should check whether an off main thread compilation is for a script or a module r=shu (41b89b1232)
- Bug 1222420 - Make AutoFinishGC into a function r=terrence (a8f535528c)
- Bug 1253094, part 4 - Stop using DebugOnly for class/struct members in js/. r=billm (2c790aa526)
- Bug 1080968 - Don't use constexpr on clang-cl when emulating MSVC 2013 (e9cc2d7c7c)
- Bug 1257496 - Add information about cycle count and GC state to our GC stats r=sfink (6c2250bfba)
- Bug 1256178 - Drop the moz prefix from the MozCSSKeyframeRule and MozCSSKeyframesRule interfaces; r=bzbarsky (894855bce1)
- Bug 1250746 - Add InitMouseEvent back to nsIDOMMouseEvent. r=khuey (cb6cd03f04)
- Bug 1249896 - Part 1: Remove unused AndOrLeft, AndOrRight, RelationalLeft, and RelationalRight. r=jwalden (c4db9dfecf)
- Bug 1249896 - Part 2: Remove unused type member from SIMD Int8x16Defnetc. r=bbouvier (7d3d7a4db1)
- Bug 1249896 - Part 3: Add JS namespace for GenericNaN. r=luke (69acc1f3ec)
- Bug 1249896 - Part 4: Avoid declaring variable between cases in switch. r=luke (28e5f94c24)
- Bug 1249896 - Part 5: Remove unused ReportInvalidTrapResult. r=efaust (caa948acac)
- Bug 1249896 - Part 7: Remove unnecessary static from my_ErrorReporter. r=sfink (7f723e079d)
- Bug 1249896 - Part 8: Remove unreachable code from GetLine. r=jwalden (108c857a1a)
- Bug 1249896 - Part 9: Include shell/jsshell.h in js.cpp. r=sfink (638bfc13c4)
- Bug 1253094, part 3 - Stop using DebugOnly for class/struct members in ipc/. r=billm (ab9e014158)
- Bug 1259737 - Make sure BackgroundImpl.cpp's mBoundThread is not used in release builds. r=froydnj (5c278ab398)
- Bug 1254515 - GonkCameraImage.cpp:16:5: error: class 'mozilla::GonkCameraImage' does not have any field named 'mThread' r=reuben (e5cac8cf5b)
- bug 1211849 - Remove canClear support from the Sanitizer. r=mak (efb1eb638c)
- Bug 1244650 - Failure to clear Forms and Search Data on exit. r=yoric (ff4197f990)
-  1245184 - nsICookieManager.remove should use the OriginAttributes, r=sicking (e508a643dd)
- Bug 1245184 - nsICookieManager.removeNative for TestCookie cppunittest, r=smaug (93c992a281)
- missing bits (48f6148087)
- Bug 1251469 - Add telemetry probes for the time needed to sanitize a loaded or unloaded Flash. r=yoric (7936c88ae5)
- Bug 1249786 - Sanitize on startup prefs are broken. r=yoric (6a1d873055)
- Bug 1252089 - Report the in-progress sanitization in shutdown abort metadata. r=yoric (34b09bcad9)
-  Bug 1253204 - Don't abort shutdown on plugins sanitization;r=mak (b68ce6ca3c)
- Bug 1213672 - Turning onWindowOpened into a variable; r=mak (67d5ea5f01)
- Bug 1250362 - don't flush prefs on startup unless it's really needed. r=yoric (c3fb052a3e)
- Bug 1250952: Create a join block in case we're in dead code after the condition; r=luke (1d6b706ae5)
- Bug 1249601 - Implement wasm i64 comparisons. r=sunfish (4927f709db)
- Bug 1250165 - Implement wasm i64 bitwise ops. r=luke (5c25322772)
- Bug 1251225 - Implement wasm i64 binary arithmetic operators. r=sunfish (f478fa8b4b)
- Bug 1251392 - Baldr: Implement PopcntI. r=sunfish (79556e947f)
- Bug 1229985 - remove nsAutoArrayPtr; r=erahm (91c49451ca)
- Bug 1251541: Replace |nsAutoArrayPtr<>| with |UniquePtr<[]>| in Bluetooth managers, r=shuang (2c1dc7ca89)
- Bug 1251541: Pass arguments to AVRCP passthrough command as uint8_t, r=shuang (73a094bd26)
- Bug 1251541: Replace |nsAutoArrayPtr<>| with |UniquePtr<[]>| in Bluetooth interfaces, r=shuang (371041f093)
- Bug 1249424 - Remove use of nsAutoArrayPtr from librlz. r=gerald (7cbcfea1b0)
- Bug 1249389 - part 0 - use getter_Transfers in TestStartupCache.cpp; r=erahm (ecb8818959)
- Bug 1249389 - part 1 - change NewObjectInputStreamFromBuffer to take a UniquePtr argument; r=erahm (0bdafa20ae)
- Bug 1249389 - part 2 - change NewBufferFromStorageStream's outparam into a UniquePtr; r=erahm (b27a70ce2d)
- Bug 1249389 - part 3 - assert the size of nsZipItemPtr's storage type; r=erahm (7c55da9ea7)
- Bug 1249389 - part 4 - make StartupCache::GetBuffer take a UniquePtr outparam; r=erahm (84b722479b)
- Bug 1249389 - part 5 - use UniquePtr instead of nsAutoArrayPtr in mozilla::scache::CacheEntry; r=erahm (0fd492ea95)
- Bug 1249389 - part 6 - provide UniquePtr overload for nsIStartupCache::GetBuffer; r=erahm (c27231d945)
- Bug 1249389 - followup - fix compilation bustage in gfxFT2FontList.cpp; r=me (a86a1b0399)
- Bug 1249389 - part 7 - clean up calls to GetBuffer in TestStartupCache; r=erahm (98939334b6)
- Bug 1249369 - use UniquePtr instead of nsAutoArrayPtr in MediaPipeline.cpp; r=jesup (7402a75a8a)
- Bug 1238842 - Add error codes to Gecko by following W3C spec. r=brsun (0dd283f6ba)
- Bug 1253217 - Change filename from BluetoothUuid to BluetoothUuidHelper. r=jocelyn (1946bcac26)
- Bug 1241382 - Revise default value of BluetoothAdapter.address and BluetoothDevice.address to empty string, f=wiwang, r=shuang (d9fdb97743)
- Bug 1223722: Transfer arrays of Bluetooth UUIDs in |BluetoothValue|, r=brsun (344af40591)
- Bug 1222956: Use |BluetoothUuid| in |BluetoothDiscoveryHandle|, r=tzimmermann (40512de386)
- Bug 1250694: Use functions to return static const BluetoothAddress and BluetoothUuid; r=tzimmermann (0c4b20066f)
- Bug 1236561 - part 1 - convert easy cases of nsAutoArrayPtr<T> to UniquePtr<T[]> in dom/bluetooth/; r=btian (31ef8b7b50)
- Bug 1236561 - part 2 - remove ObexHeaderSet::GetAuthChallenge; r=btian (f70daddd53)
- Bug 1236561 - part 3 - make BluetoothConfigurationParameter.mValue a UniquePtr; r=btian (a5a0ac988a)
- Bug 1239979: Add |BluetoothSocket::Accept| method, r=btian (ca92a0ab7c)
- Bug 1239979: Store pointer to Bluetooth socket interface in |BluetoothSocket|, r=btian (7c15e75133)
- Bug 1239979: Cleanup |BluetoothSocket|'s internals when connections close, r=btian (b6ac677d2e)
- Bug 1252841: Convert Bluetooth module to |UniquePtr<>|, r=btian (76d9759afd)
- Bug 1232687: Start bluetoothd with HAL service interface, r=shuang (379a68064f)
- Bug 1223729 - HID Connection Implementation, r=jocelyn, sr=mrbkap (0eeddf875e)
- Bug 1232670: Prepare Bluetooth PDU for sending after checking connection status, r=shuang (699ff14d9b)
- Bug 1252841: Convert Bluetooth daemon interfaces to |UniquePtr<>|, r=brsun (797260c6dc)
2024-02-07 15:22:30 +08:00

252 lines
8.3 KiB
C++

/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "nsCOMPtr.h"
#include "nsIInputStream.h"
#include "nsIStringStream.h"
#include "nsNetUtil.h"
#include "nsIFileURL.h"
#include "nsIJARURI.h"
#include "nsIResProtocolHandler.h"
#include "nsIChromeRegistry.h"
#include "nsAutoPtr.h"
#include "StartupCacheUtils.h"
#include "mozilla/scache/StartupCache.h"
#include "mozilla/Omnijar.h"
namespace mozilla {
namespace scache {
NS_EXPORT nsresult
NewObjectInputStreamFromBuffer(UniquePtr<char[]> buffer, uint32_t len,
nsIObjectInputStream** stream)
{
nsCOMPtr<nsIStringInputStream> stringStream
= do_CreateInstance("@mozilla.org/io/string-input-stream;1");
nsCOMPtr<nsIObjectInputStream> objectInput
= do_CreateInstance("@mozilla.org/binaryinputstream;1");
stringStream->AdoptData(buffer.release(), len);
objectInput->SetInputStream(stringStream);
objectInput.forget(stream);
return NS_OK;
}
NS_EXPORT nsresult
NewObjectOutputWrappedStorageStream(nsIObjectOutputStream **wrapperStream,
nsIStorageStream** stream,
bool wantDebugStream)
{
nsCOMPtr<nsIStorageStream> storageStream;
nsresult rv = NS_NewStorageStream(256, UINT32_MAX, getter_AddRefs(storageStream));
NS_ENSURE_SUCCESS(rv, rv);
nsCOMPtr<nsIObjectOutputStream> objectOutput
= do_CreateInstance("@mozilla.org/binaryoutputstream;1");
nsCOMPtr<nsIOutputStream> outputStream
= do_QueryInterface(storageStream);
objectOutput->SetOutputStream(outputStream);
#ifdef DEBUG
if (wantDebugStream) {
// Wrap in debug stream to detect unsupported writes of
// multiply-referenced non-singleton objects
StartupCache* sc = StartupCache::GetSingleton();
NS_ENSURE_TRUE(sc, NS_ERROR_UNEXPECTED);
nsCOMPtr<nsIObjectOutputStream> debugStream;
sc->GetDebugObjectOutputStream(objectOutput, getter_AddRefs(debugStream));
debugStream.forget(wrapperStream);
} else {
objectOutput.forget(wrapperStream);
}
#else
objectOutput.forget(wrapperStream);
#endif
storageStream.forget(stream);
return NS_OK;
}
NS_EXPORT nsresult
NewBufferFromStorageStream(nsIStorageStream *storageStream,
UniquePtr<char[]>* buffer, uint32_t* len)
{
nsresult rv;
nsCOMPtr<nsIInputStream> inputStream;
rv = storageStream->NewInputStream(0, getter_AddRefs(inputStream));
NS_ENSURE_SUCCESS(rv, rv);
uint64_t avail64;
rv = inputStream->Available(&avail64);
NS_ENSURE_SUCCESS(rv, rv);
NS_ENSURE_TRUE(avail64 <= UINT32_MAX, NS_ERROR_FILE_TOO_BIG);
uint32_t avail = (uint32_t)avail64;
auto temp = MakeUnique<char[]>(avail);
uint32_t read;
rv = inputStream->Read(temp.get(), avail, &read);
if (NS_SUCCEEDED(rv) && avail != read)
rv = NS_ERROR_UNEXPECTED;
if (NS_FAILED(rv)) {
return rv;
}
*len = avail;
*buffer = Move(temp);
return NS_OK;
}
static const char baseName[2][5] = { "gre/", "app/" };
static inline bool
canonicalizeBase(nsAutoCString &spec,
nsACString &out)
{
nsAutoCString greBase, appBase;
nsresult rv = mozilla::Omnijar::GetURIString(mozilla::Omnijar::GRE, greBase);
if (NS_FAILED(rv) || !greBase.Length())
return false;
rv = mozilla::Omnijar::GetURIString(mozilla::Omnijar::APP, appBase);
if (NS_FAILED(rv))
return false;
bool underGre = !greBase.Compare(spec.get(), false, greBase.Length());
bool underApp = appBase.Length() &&
!appBase.Compare(spec.get(), false, appBase.Length());
if (!underGre && !underApp)
return false;
/**
* At this point, if both underGre and underApp are true, it can be one
* of the two following cases:
* - the GRE directory points to a subdirectory of the APP directory,
* meaning spec points under GRE.
* - the APP directory points to a subdirectory of the GRE directory,
* meaning spec points under APP.
* Checking the GRE and APP path length is enough to know in which case
* we are.
*/
if (underGre && underApp && greBase.Length() < appBase.Length())
underGre = false;
out.AppendLiteral("/resource/");
out.Append(baseName[underGre ? mozilla::Omnijar::GRE : mozilla::Omnijar::APP]);
out.Append(Substring(spec, underGre ? greBase.Length() : appBase.Length()));
return true;
}
/**
* PathifyURI transforms uris into useful zip paths
* to make it easier to manipulate startup cache entries
* using standard zip tools.
* Transformations applied:
* * resource:// URIs are resolved to their corresponding file/jar URI to
* canonicalize resources URIs other than gre and app.
* * Paths under GRE or APP directory have their base path replaced with
* resource/gre or resource/app to avoid depending on install location.
* * jar:file:///path/to/file.jar!/sub/path urls are replaced with
* /path/to/file.jar/sub/path
*
* The result is appended to the string passed in. Adding a prefix before
* calling is recommended to avoid colliding with other cache users.
*
* For example, in the js loader (string is prefixed with jsloader by caller):
* resource://gre/modules/XPCOMUtils.jsm or
* file://$GRE_DIR/modules/XPCOMUtils.jsm or
* jar:file://$GRE_DIR/omni.jar!/modules/XPCOMUtils.jsm becomes
* jsloader/resource/gre/modules/XPCOMUtils.jsm
* file://$PROFILE_DIR/extensions/{uuid}/components/component.js becomes
* jsloader/$PROFILE_DIR/extensions/%7Buuid%7D/components/component.js
* jar:file://$PROFILE_DIR/extensions/some.xpi!/components/component.js becomes
* jsloader/$PROFILE_DIR/extensions/some.xpi/components/component.js
*/
NS_EXPORT nsresult
PathifyURI(nsIURI *in, nsACString &out)
{
bool equals;
nsresult rv;
nsCOMPtr<nsIURI> uri = in;
nsAutoCString spec;
// Resolve resource:// URIs. At the end of this if/else block, we
// have both spec and uri variables identifying the same URI.
if (NS_SUCCEEDED(in->SchemeIs("resource", &equals)) && equals) {
nsCOMPtr<nsIIOService> ioService = do_GetIOService(&rv);
NS_ENSURE_SUCCESS(rv, rv);
nsCOMPtr<nsIProtocolHandler> ph;
rv = ioService->GetProtocolHandler("resource", getter_AddRefs(ph));
NS_ENSURE_SUCCESS(rv, rv);
nsCOMPtr<nsIResProtocolHandler> irph(do_QueryInterface(ph, &rv));
NS_ENSURE_SUCCESS(rv, rv);
rv = irph->ResolveURI(in, spec);
NS_ENSURE_SUCCESS(rv, rv);
rv = ioService->NewURI(spec, nullptr, nullptr, getter_AddRefs(uri));
NS_ENSURE_SUCCESS(rv, rv);
} else {
if (NS_SUCCEEDED(in->SchemeIs("chrome", &equals)) && equals) {
nsCOMPtr<nsIChromeRegistry> chromeReg =
mozilla::services::GetChromeRegistryService();
if (!chromeReg)
return NS_ERROR_UNEXPECTED;
rv = chromeReg->ConvertChromeURL(in, getter_AddRefs(uri));
NS_ENSURE_SUCCESS(rv, rv);
}
rv = uri->GetSpec(spec);
NS_ENSURE_SUCCESS(rv, rv);
}
if (!canonicalizeBase(spec, out)) {
if (NS_SUCCEEDED(uri->SchemeIs("file", &equals)) && equals) {
nsCOMPtr<nsIFileURL> baseFileURL;
baseFileURL = do_QueryInterface(uri, &rv);
NS_ENSURE_SUCCESS(rv, rv);
nsAutoCString path;
rv = baseFileURL->GetPath(path);
NS_ENSURE_SUCCESS(rv, rv);
out.Append(path);
} else if (NS_SUCCEEDED(uri->SchemeIs("jar", &equals)) && equals) {
nsCOMPtr<nsIJARURI> jarURI = do_QueryInterface(uri, &rv);
NS_ENSURE_SUCCESS(rv, rv);
nsCOMPtr<nsIURI> jarFileURI;
rv = jarURI->GetJARFile(getter_AddRefs(jarFileURI));
NS_ENSURE_SUCCESS(rv, rv);
rv = PathifyURI(jarFileURI, out);
NS_ENSURE_SUCCESS(rv, rv);
nsAutoCString path;
rv = jarURI->GetJAREntry(path);
NS_ENSURE_SUCCESS(rv, rv);
out.Append('/');
out.Append(path);
} else { // Very unlikely
nsAutoCString spec;
rv = uri->GetSpec(spec);
NS_ENSURE_SUCCESS(rv, rv);
out.Append('/');
out.Append(spec);
}
}
return NS_OK;
}
} // namespace scache
} // namespace mozilla