Backed out 6 changesets (bug 1627075) for bustages on startupcache/StartupCache.cpp . CLOSED TREE

Backed out changeset 21605186687e (bug 1627075)
Backed out changeset e29b15980da2 (bug 1627075)
Backed out changeset eb5265addd5e (bug 1627075)
Backed out changeset dfd71f4ecb81 (bug 1627075)
Backed out changeset 13ecd68b3c0d (bug 1627075)
Backed out changeset 333d035afe92 (bug 1627075)
This commit is contained in:
Narcis Beleuzu 2020-07-07 23:30:48 +03:00
Родитель f278cfa05f
Коммит a182c015f5
41 изменённых файлов: 430 добавлений и 2269 удалений

Просмотреть файл

@ -11,16 +11,6 @@
#include "mozilla/dom/PWebGLChild.h" #include "mozilla/dom/PWebGLChild.h"
#include "mozilla/dom/IpdlQueue.h" #include "mozilla/dom/IpdlQueue.h"
// This is a bit weird. Nothing directly in WebGLChild.h necessitates including
// WebGLParent.h, but if we don't do this, we get compiler errors in the
// generated code inside PWebGLChild.cpp. The error is due to a complex
// dependency chain involving IpdlQueue, which I won't go into here. Including
// WebGLParent.h inside WebGLChild.h is the simplest way we could think of to
// avoid this issue. Including it in any of the code more directly involved in
// the breaking dependency chain unfortunately introduces a cyclical dependency
// between WebGLParent.h and PWebGLParent.h.
#include "mozilla/dom/WebGLParent.h"
namespace mozilla { namespace mozilla {
class ClientWebGLContext; class ClientWebGLContext;

Просмотреть файл

@ -98,7 +98,6 @@
#include "mozilla/plugins/PluginInstanceParent.h" #include "mozilla/plugins/PluginInstanceParent.h"
#include "mozilla/plugins/PluginModuleParent.h" #include "mozilla/plugins/PluginModuleParent.h"
#include "mozilla/RemoteLazyInputStreamChild.h" #include "mozilla/RemoteLazyInputStreamChild.h"
#include "mozilla/scache/StartupCacheChild.h"
#include "mozilla/widget/ScreenManager.h" #include "mozilla/widget/ScreenManager.h"
#include "mozilla/widget/WidgetMessageUtils.h" #include "mozilla/widget/WidgetMessageUtils.h"
#include "nsBaseDragService.h" #include "nsBaseDragService.h"
@ -1921,23 +1920,6 @@ mozilla::ipc::IPCResult ContentChild::RecvPScriptCacheConstructor(
return IPC_OK(); return IPC_OK();
} }
scache::PStartupCacheChild* ContentChild::AllocPStartupCacheChild(
const bool& wantCacheData) {
return new scache::StartupCacheChild();
}
bool ContentChild::DeallocPStartupCacheChild(
scache::PStartupCacheChild* cache) {
delete static_cast<scache::StartupCacheChild*>(cache);
return true;
}
mozilla::ipc::IPCResult ContentChild::RecvPStartupCacheConstructor(
scache::PStartupCacheChild* actor, const bool& wantCacheData) {
static_cast<scache::StartupCacheChild*>(actor)->Init(wantCacheData);
return IPC_OK();
}
PNeckoChild* ContentChild::AllocPNeckoChild() { return new NeckoChild(); } PNeckoChild* ContentChild::AllocPNeckoChild() { return new NeckoChild(); }
mozilla::ipc::IPCResult ContentChild::RecvNetworkLinkTypeChange( mozilla::ipc::IPCResult ContentChild::RecvNetworkLinkTypeChange(

Просмотреть файл

@ -238,13 +238,6 @@ class ContentChild final : public PContentChild,
PScriptCacheChild*, const FileDescOrError& cacheFile, PScriptCacheChild*, const FileDescOrError& cacheFile,
const bool& wantCacheData) override; const bool& wantCacheData) override;
PStartupCacheChild* AllocPStartupCacheChild(const bool& wantCacheData);
bool DeallocPStartupCacheChild(PStartupCacheChild*);
virtual mozilla::ipc::IPCResult RecvPStartupCacheConstructor(
PStartupCacheChild*, const bool& wantCacheData) override;
PNeckoChild* AllocPNeckoChild(); PNeckoChild* AllocPNeckoChild();
bool DeallocPNeckoChild(PNeckoChild*); bool DeallocPNeckoChild(PNeckoChild*);

Просмотреть файл

@ -66,7 +66,6 @@
#include "mozilla/ProcessHangMonitorIPC.h" #include "mozilla/ProcessHangMonitorIPC.h"
#include "mozilla/RDDProcessManager.h" #include "mozilla/RDDProcessManager.h"
#include "mozilla/ScopeExit.h" #include "mozilla/ScopeExit.h"
#include "mozilla/scache/StartupCache.h"
#include "mozilla/ScriptPreloader.h" #include "mozilla/ScriptPreloader.h"
#include "mozilla/Services.h" #include "mozilla/Services.h"
#include "mozilla/Sprintf.h" #include "mozilla/Sprintf.h"
@ -156,7 +155,6 @@
#include "mozilla/plugins/PluginBridge.h" #include "mozilla/plugins/PluginBridge.h"
#include "mozilla/RemoteLazyInputStreamParent.h" #include "mozilla/RemoteLazyInputStreamParent.h"
#include "mozilla/widget/ScreenManager.h" #include "mozilla/widget/ScreenManager.h"
#include "mozilla/scache/StartupCacheParent.h"
#include "nsAnonymousTemporaryFile.h" #include "nsAnonymousTemporaryFile.h"
#include "nsAppRunner.h" #include "nsAppRunner.h"
#include "nsCExternalHandlerService.h" #include "nsCExternalHandlerService.h"
@ -2302,11 +2300,6 @@ bool ContentParent::BeginSubprocessLaunch(ProcessPriority aPriority) {
} }
mPrefSerializer->AddSharedPrefCmdLineArgs(*mSubprocess, extraArgs); mPrefSerializer->AddSharedPrefCmdLineArgs(*mSubprocess, extraArgs);
auto startupCache = mozilla::scache::StartupCache::GetSingleton();
if (startupCache) {
startupCache->AddStartupCacheCmdLineArgs(*mSubprocess, extraArgs);
}
// Register ContentParent as an observer for changes to any pref // Register ContentParent as an observer for changes to any pref
// whose prefix matches the empty string, i.e. all of them. The // whose prefix matches the empty string, i.e. all of them. The
// observation starts here in order to capture pref updates that // observation starts here in order to capture pref updates that
@ -2740,7 +2733,6 @@ bool ContentParent::InitInternal(ProcessPriority aInitialPriority) {
Unused << SendRemoteType(mRemoteType); Unused << SendRemoteType(mRemoteType);
ScriptPreloader::InitContentChild(*this); ScriptPreloader::InitContentChild(*this);
scache::StartupCache::InitContentChild(*this);
// Initialize the message manager (and load delayed scripts) now that we // Initialize the message manager (and load delayed scripts) now that we
// have established communications with the child. // have established communications with the child.
@ -3837,16 +3829,6 @@ bool ContentParent::DeallocPScriptCacheParent(PScriptCacheParent* cache) {
return true; return true;
} }
PStartupCacheParent* ContentParent::AllocPStartupCacheParent(
const bool& wantCacheData) {
return new scache::StartupCacheParent(wantCacheData);
}
bool ContentParent::DeallocPStartupCacheParent(PStartupCacheParent* cache) {
delete static_cast<scache::StartupCacheParent*>(cache);
return true;
}
PNeckoParent* ContentParent::AllocPNeckoParent() { return new NeckoParent(); } PNeckoParent* ContentParent::AllocPNeckoParent() { return new NeckoParent(); }
bool ContentParent::DeallocPNeckoParent(PNeckoParent* necko) { bool ContentParent::DeallocPNeckoParent(PNeckoParent* necko) {

Просмотреть файл

@ -86,7 +86,6 @@ class PreallocatedProcessManagerImpl;
class BenchmarkStorageParent; class BenchmarkStorageParent;
using mozilla::loader::PScriptCacheParent; using mozilla::loader::PScriptCacheParent;
using mozilla::scache::PStartupCacheParent;
namespace embedding { namespace embedding {
class PrintingParent; class PrintingParent;
@ -927,10 +926,6 @@ class ContentParent final
bool DeallocPScriptCacheParent(PScriptCacheParent* shell); bool DeallocPScriptCacheParent(PScriptCacheParent* shell);
PStartupCacheParent* AllocPStartupCacheParent(const bool& wantCacheData);
bool DeallocPStartupCacheParent(PStartupCacheParent* shell);
bool DeallocPNeckoParent(PNeckoParent* necko); bool DeallocPNeckoParent(PNeckoParent* necko);
already_AddRefed<PExternalHelperAppParent> AllocPExternalHelperAppParent( already_AddRefed<PExternalHelperAppParent> AllocPExternalHelperAppParent(

Просмотреть файл

@ -84,8 +84,6 @@ bool ContentProcess::Init(int aArgc, char* aArgv[]) {
char* prefMapHandle = nullptr; char* prefMapHandle = nullptr;
char* prefsLen = nullptr; char* prefsLen = nullptr;
char* prefMapSize = nullptr; char* prefMapSize = nullptr;
char* scacheHandle = nullptr;
char* scacheSize = nullptr;
#if defined(XP_MACOSX) && defined(MOZ_SANDBOX) #if defined(XP_MACOSX) && defined(MOZ_SANDBOX)
nsCOMPtr<nsIFile> profileDir; nsCOMPtr<nsIFile> profileDir;
#endif #endif
@ -129,11 +127,6 @@ bool ContentProcess::Init(int aArgc, char* aArgv[]) {
return false; return false;
} }
prefMapHandle = aArgv[i]; prefMapHandle = aArgv[i];
} else if (strcmp(aArgv[i], "-scacheHandle") == 0) {
if (++i == aArgc) {
return false;
}
scacheHandle = aArgv[i];
#endif #endif
} else if (strcmp(aArgv[i], "-prefsLen") == 0) { } else if (strcmp(aArgv[i], "-prefsLen") == 0) {
@ -146,11 +139,6 @@ bool ContentProcess::Init(int aArgc, char* aArgv[]) {
return false; return false;
} }
prefMapSize = aArgv[i]; prefMapSize = aArgv[i];
} else if (strcmp(aArgv[i], "-scacheSize") == 0) {
if (++i == aArgc) {
return false;
}
scacheSize = aArgv[i];
} else if (strcmp(aArgv[i], "-safeMode") == 0) { } else if (strcmp(aArgv[i], "-safeMode") == 0) {
gSafeMode = true; gSafeMode = true;
@ -187,9 +175,6 @@ bool ContentProcess::Init(int aArgc, char* aArgv[]) {
return false; return false;
} }
Unused << mozilla::scache::StartupCache::InitChildSingleton(scacheHandle,
scacheSize);
mContent.Init(IOThreadChild::message_loop(), ParentPid(), *parentBuildID, mContent.Init(IOThreadChild::message_loop(), ParentPid(), *parentBuildID,
IOThreadChild::TakeChannel(), *childID, *isForBrowser); IOThreadChild::TakeChannel(), *childID, *isForBrowser);

Просмотреть файл

@ -44,7 +44,6 @@ include protocol PVRManager;
include protocol PRemoteDecoderManager; include protocol PRemoteDecoderManager;
include protocol PProfiler; include protocol PProfiler;
include protocol PScriptCache; include protocol PScriptCache;
include protocol PStartupCache;
include protocol PSessionStorageObserver; include protocol PSessionStorageObserver;
include protocol PBenchmarkStorage; include protocol PBenchmarkStorage;
include DOMTypes; include DOMTypes;
@ -413,7 +412,6 @@ nested(upto inside_cpow) sync protocol PContent
manages PURLClassifier; manages PURLClassifier;
manages PURLClassifierLocal; manages PURLClassifierLocal;
manages PScriptCache; manages PScriptCache;
manages PStartupCache;
manages PLoginReputation; manages PLoginReputation;
manages PSessionStorageObserver; manages PSessionStorageObserver;
manages PBenchmarkStorage; manages PBenchmarkStorage;
@ -541,8 +539,6 @@ child:
async PScriptCache(FileDescOrError cacheFile, bool wantCacheData); async PScriptCache(FileDescOrError cacheFile, bool wantCacheData);
async PStartupCache(bool wantCacheData);
async RegisterChrome(ChromePackage[] packages, SubstitutionMapping[] substitutions, async RegisterChrome(ChromePackage[] packages, SubstitutionMapping[] substitutions,
OverrideMapping[] overrides, nsCString locale, bool reset); OverrideMapping[] overrides, nsCString locale, bool reset);
async RegisterChromeItem(ChromeRegistryItem item); async RegisterChromeItem(ChromeRegistryItem item);

Просмотреть файл

@ -95,14 +95,14 @@ already_AddRefed<SharedFTFace> FT2FontEntry::GetFTFace(bool aCommit) {
// here would be memory allocation, in which case mFace remains null. // here would be memory allocation, in which case mFace remains null.
RefPtr<SharedFTFace> face; RefPtr<SharedFTFace> face;
if (mFilename[0] != '/') { if (mFilename[0] != '/') {
RefPtr<CacheAwareZipReader> reader = Omnijar::GetReader(Omnijar::Type::GRE); RefPtr<nsZipArchive> reader = Omnijar::GetReader(Omnijar::Type::GRE);
nsZipItem* item = reader->GetItem(mFilename.get()); nsZipItem* item = reader->GetItem(mFilename.get());
NS_ASSERTION(item, "failed to find zip entry"); NS_ASSERTION(item, "failed to find zip entry");
uint32_t bufSize = item->RealSize(); uint32_t bufSize = item->RealSize();
uint8_t* fontDataBuf = static_cast<uint8_t*>(malloc(bufSize)); uint8_t* fontDataBuf = static_cast<uint8_t*>(malloc(bufSize));
if (fontDataBuf) { if (fontDataBuf) {
CacheAwareZipCursor cursor(item, reader, fontDataBuf, bufSize); nsZipCursor cursor(item, reader, fontDataBuf, bufSize);
cursor.Copy(&bufSize); cursor.Copy(&bufSize);
NS_ASSERTION(bufSize == item->RealSize(), "error reading bundled font"); NS_ASSERTION(bufSize == item->RealSize(), "error reading bundled font");
RefPtr<FTUserFontData> ufd = new FTUserFontData(fontDataBuf, bufSize); RefPtr<FTUserFontData> ufd = new FTUserFontData(fontDataBuf, bufSize);
@ -458,8 +458,7 @@ hb_blob_t* FT2FontEntry::GetFontTable(uint32_t aTableTag) {
} else { } else {
// A relative path means an omnijar resource, which we may need to // A relative path means an omnijar resource, which we may need to
// decompress to a temporary buffer. // decompress to a temporary buffer.
RefPtr<CacheAwareZipReader> reader = RefPtr<nsZipArchive> reader = Omnijar::GetReader(Omnijar::Type::GRE);
Omnijar::GetReader(Omnijar::Type::GRE);
nsZipItem* item = reader->GetItem(mFilename.get()); nsZipItem* item = reader->GetItem(mFilename.get());
MOZ_ASSERT(item, "failed to find zip entry"); MOZ_ASSERT(item, "failed to find zip entry");
if (item) { if (item) {
@ -470,7 +469,7 @@ hb_blob_t* FT2FontEntry::GetFontTable(uint32_t aTableTag) {
uint32_t length = item->RealSize(); uint32_t length = item->RealSize();
uint8_t* buffer = static_cast<uint8_t*>(malloc(length)); uint8_t* buffer = static_cast<uint8_t*>(malloc(length));
if (buffer) { if (buffer) {
CacheAwareZipCursor cursor(item, reader, buffer, length); nsZipCursor cursor(item, reader, buffer, length);
cursor.Copy(&length); cursor.Copy(&length);
MOZ_ASSERT(length == item->RealSize(), "error reading font"); MOZ_ASSERT(length == item->RealSize(), "error reading font");
if (length == item->RealSize()) { if (length == item->RealSize()) {
@ -1160,7 +1159,7 @@ void gfxFT2FontList::FindFontsInOmnijar(FontNameCache* aCache) {
static const char* sJarSearchPaths[] = { static const char* sJarSearchPaths[] = {
"res/fonts/*.ttf$", "res/fonts/*.ttf$",
}; };
RefPtr<CacheAwareZipReader> reader = Omnijar::GetReader(Omnijar::Type::GRE); RefPtr<nsZipArchive> reader = Omnijar::GetReader(Omnijar::Type::GRE);
for (unsigned i = 0; i < ArrayLength(sJarSearchPaths); i++) { for (unsigned i = 0; i < ArrayLength(sJarSearchPaths); i++) {
nsZipFind* find; nsZipFind* find;
if (NS_SUCCEEDED(reader->FindInit(sJarSearchPaths[i], &find))) { if (NS_SUCCEEDED(reader->FindInit(sJarSearchPaths[i], &find))) {
@ -1275,7 +1274,7 @@ void gfxFT2FontList::AddFaceToList(const nsCString& aEntryName, uint32_t aIndex,
} }
} }
void gfxFT2FontList::AppendFacesFromOmnijarEntry(CacheAwareZipReader* aArchive, void gfxFT2FontList::AppendFacesFromOmnijarEntry(nsZipArchive* aArchive,
const nsCString& aEntryName, const nsCString& aEntryName,
FontNameCache* aCache, FontNameCache* aCache,
bool aJarChanged) { bool aJarChanged) {
@ -1314,7 +1313,7 @@ void gfxFT2FontList::AppendFacesFromOmnijarEntry(CacheAwareZipReader* aArchive,
return; return;
} }
CacheAwareZipCursor cursor(item, aArchive, (uint8_t*)buffer, bufSize); nsZipCursor cursor(item, aArchive, (uint8_t*)buffer, bufSize);
uint8_t* data = cursor.Copy(&bufSize); uint8_t* data = cursor.Copy(&bufSize);
MOZ_ASSERT(data && bufSize == item->RealSize(), "error reading bundled font"); MOZ_ASSERT(data && bufSize == item->RealSize(), "error reading bundled font");
if (!data) { if (!data) {

Просмотреть файл

@ -11,9 +11,6 @@
#include "gfxPlatformFontList.h" #include "gfxPlatformFontList.h"
namespace mozilla { namespace mozilla {
class CacheAwareZipReader;
namespace dom { namespace dom {
class SystemFontListEntry; class SystemFontListEntry;
}; };
@ -177,7 +174,7 @@ class gfxFT2FontList final : public gfxPlatformFontList {
void AppendFacesFromFontFile(const nsCString& aFileName, void AppendFacesFromFontFile(const nsCString& aFileName,
FontNameCache* aCache, StandardFile aStdFile); FontNameCache* aCache, StandardFile aStdFile);
void AppendFacesFromOmnijarEntry(mozilla::CacheAwareZipReader* aArchive, void AppendFacesFromOmnijarEntry(nsZipArchive* aReader,
const nsCString& aEntryName, const nsCString& aEntryName,
FontNameCache* aCache, bool aJarChanged); FontNameCache* aCache, bool aJarChanged);

Просмотреть файл

@ -228,7 +228,7 @@ void nsHyphenationManager::LoadPatternListFromOmnijar(Omnijar::Type aType) {
return; return;
} }
RefPtr<CacheAwareZipReader> zip = Omnijar::GetReader(aType); RefPtr<nsZipArchive> zip = Omnijar::GetReader(aType);
if (!zip) { if (!zip) {
return; return;
} }

Просмотреть файл

@ -41,11 +41,20 @@ static const void* GetItemPtrFromJarURI(nsIJARURI* aJAR, uint32_t* aLength) {
if (!file) { if (!file) {
return nullptr; return nullptr;
} }
RefPtr<CacheAwareZipReader> archive = Omnijar::GetReader(file); RefPtr<nsZipArchive> archive = Omnijar::GetReader(file);
if (archive) { if (archive) {
nsCString path; nsCString path;
aJAR->GetJAREntry(path); aJAR->GetJAREntry(path);
return archive->GetData(path.get(), aLength); nsZipItem* item = archive->GetItem(path.get());
if (item && item->Compression() == 0 && item->Size() > 0) {
// We do NOT own this data, but it won't go away until the omnijar
// file is closed during shutdown.
const uint8_t* data = archive->GetData(item);
if (data) {
*aLength = item->Size();
return data;
}
}
} }
return nullptr; return nullptr;
} }

Просмотреть файл

@ -295,14 +295,13 @@ bool LocaleService::IsServer() { return mIsServer; }
static bool GetGREFileContents(const char* aFilePath, nsCString* aOutString) { static bool GetGREFileContents(const char* aFilePath, nsCString* aOutString) {
// Look for the requested file in omnijar. // Look for the requested file in omnijar.
RefPtr<CacheAwareZipReader> zip = Omnijar::GetReader(Omnijar::GRE); RefPtr<nsZipArchive> zip = Omnijar::GetReader(Omnijar::GRE);
if (zip) { if (zip) {
uint32_t length; nsZipItemPtr<char> item(zip, aFilePath);
const uint8_t* data = zip->GetData(aFilePath, &length); if (!item) {
if (!data) {
return false; return false;
} }
aOutString->Assign(reinterpret_cast<const char*>(data), length); aOutString->Assign(item.Buffer(), item.Length());
return true; return true;
} }

Просмотреть файл

@ -64,82 +64,6 @@ class OutputBuffer {
size_t cursor_ = 0; size_t cursor_ = 0;
}; };
// This is similar to OutputBuffer, but with a fixed-size buffer, rather than
// a dynamically growing one. This is currently used in order to share
// StartupCache data across processes.
class PreallocatedOutputBuffer {
public:
explicit PreallocatedOutputBuffer(Range<uint8_t>& buffer) : data(buffer) {}
uint8_t* write(size_t size) {
MOZ_ASSERT(checkCapacity(size));
auto buf = &data[cursor_];
cursor_ += size;
return buf;
}
bool codeUint8(const uint8_t& val) {
if (checkCapacity(sizeof val)) {
*write(sizeof val) = val;
}
return !error_;
}
template <typename T>
bool codeUint8(const EnumSet<T>& val) {
return codeUint8(val.serialize());
}
bool codeUint16(const uint16_t& val) {
if (checkCapacity(sizeof val)) {
LittleEndian::writeUint16(write(sizeof val), val);
}
return !error_;
}
bool codeUint32(const uint32_t& val) {
if (checkCapacity(sizeof val)) {
LittleEndian::writeUint32(write(sizeof val), val);
}
return !error_;
}
bool codeString(const nsCString& str) {
uint16_t len = CheckedUint16(str.Length()).value();
if (codeUint16(len)) {
if (checkCapacity(len)) {
memcpy(write(len), str.get(), len);
}
}
return !error_;
}
bool error() { return error_; }
bool finished() { return error_ || !remainingCapacity(); }
size_t remainingCapacity() { return data.length() - cursor_; }
size_t cursor() const { return cursor_; }
const uint8_t* Get() const { return data.begin().get(); }
private:
bool checkCapacity(size_t size) {
if (size > remainingCapacity()) {
error_ = true;
}
return !error_;
}
bool error_ = false;
public:
Range<uint8_t>& data;
size_t cursor_ = 0;
};
class InputBuffer { class InputBuffer {
public: public:
explicit InputBuffer(const Range<uint8_t>& buffer) : data(buffer) {} explicit InputBuffer(const Range<uint8_t>& buffer) : data(buffer) {}

Просмотреть файл

@ -322,7 +322,7 @@ void URLPreloader::BackgroundReadFiles() {
mReaderThread = nullptr; mReaderThread = nullptr;
}); });
Vector<CacheAwareZipCursor> cursors; Vector<nsZipCursor> cursors;
LinkedList<URLEntry> pendingURLs; LinkedList<URLEntry> pendingURLs;
{ {
MonitorAutoLock mal(mMonitor); MonitorAutoLock mal(mMonitor);
@ -352,14 +352,14 @@ void URLPreloader::BackgroundReadFiles() {
continue; continue;
} }
RefPtr<CacheAwareZipReader> zip = entry->Archive(); RefPtr<nsZipArchive> zip = entry->Archive();
if (!zip) { if (!zip) {
MOZ_CRASH_UNSAFE_PRINTF( MOZ_CRASH_UNSAFE_PRINTF(
"Failed to get Omnijar %s archive for entry (path: \"%s\")", "Failed to get Omnijar %s archive for entry (path: \"%s\")",
entry->TypeString(), entry->mPath.get()); entry->TypeString(), entry->mPath.get());
} }
auto* item = zip->GetItem(entry->mPath.get()); auto item = zip->GetItem(entry->mPath.get());
if (!item) { if (!item) {
entry->mResultCode = NS_ERROR_FILE_NOT_FOUND; entry->mResultCode = NS_ERROR_FILE_NOT_FOUND;
continue; continue;
@ -495,25 +495,25 @@ Result<const nsCString, nsresult> URLPreloader::ReadURIInternal(
} }
/* static */ Result<const nsCString, nsresult> URLPreloader::ReadZip( /* static */ Result<const nsCString, nsresult> URLPreloader::ReadZip(
CacheAwareZipReader* archive, const nsACString& path, ReadType readType) { nsZipArchive* zip, const nsACString& path, ReadType readType) {
// If the zip archive belongs to an Omnijar location, map it to a cache // If the zip archive belongs to an Omnijar location, map it to a cache
// entry, and cache it as normal. Otherwise, simply read the entry // entry, and cache it as normal. Otherwise, simply read the entry
// synchronously, since other JAR archives are currently unsupported by the // synchronously, since other JAR archives are currently unsupported by the
// cache. // cache.
RefPtr<CacheAwareZipReader> reader = Omnijar::GetReader(Omnijar::GRE); RefPtr<nsZipArchive> reader = Omnijar::GetReader(Omnijar::GRE);
if (reader == archive) { if (zip == reader) {
CacheKey key(CacheKey::TypeGREJar, path); CacheKey key(CacheKey::TypeGREJar, path);
return Read(key, readType); return Read(key, readType);
} }
reader = Omnijar::GetReader(Omnijar::APP); reader = Omnijar::GetReader(Omnijar::APP);
if (reader == archive) { if (zip == reader) {
CacheKey key(CacheKey::TypeAppJar, path); CacheKey key(CacheKey::TypeAppJar, path);
return Read(key, readType); return Read(key, readType);
} }
// Not an Omnijar archive, so just read it directly. // Not an Omnijar archive, so just read it directly.
FileLocation location(archive, PromiseFlatCString(path).BeginReading()); FileLocation location(zip, PromiseFlatCString(path).BeginReading());
return URLEntry::ReadLocation(location); return URLEntry::ReadLocation(location);
} }
@ -581,7 +581,7 @@ Result<FileLocation, nsresult> URLPreloader::CacheKey::ToFileLocation() {
return FileLocation(file); return FileLocation(file);
} }
RefPtr<CacheAwareZipReader> zip = Archive(); RefPtr<nsZipArchive> zip = Archive();
return FileLocation(zip, mPath.get()); return FileLocation(zip, mPath.get());
} }

Просмотреть файл

@ -26,6 +26,8 @@
#include "nsIThread.h" #include "nsIThread.h"
#include "nsReadableUtils.h" #include "nsReadableUtils.h"
class nsZipArchive;
namespace mozilla { namespace mozilla {
namespace loader { namespace loader {
class InputBuffer; class InputBuffer;
@ -75,7 +77,7 @@ class URLPreloader final : public nsIObserver, public nsIMemoryReporter {
static Result<const nsCString, nsresult> ReadFile(nsIFile* file, static Result<const nsCString, nsresult> ReadFile(nsIFile* file,
ReadType readType = Forget); ReadType readType = Forget);
static Result<const nsCString, nsresult> ReadZip(CacheAwareZipReader* archive, static Result<const nsCString, nsresult> ReadZip(nsZipArchive* archive,
const nsACString& path, const nsACString& path,
ReadType readType = Forget); ReadType readType = Forget);
@ -196,7 +198,7 @@ class URLPreloader final : public nsIObserver, public nsIMemoryReporter {
return ""; return "";
} }
already_AddRefed<CacheAwareZipReader> Archive() { already_AddRefed<nsZipArchive> Archive() {
return Omnijar::GetReader(OmnijarType()); return Omnijar::GetReader(OmnijarType());
} }

Просмотреть файл

@ -51,7 +51,6 @@
#include "mozilla/scache/StartupCacheUtils.h" #include "mozilla/scache/StartupCacheUtils.h"
#include "mozilla/MacroForEach.h" #include "mozilla/MacroForEach.h"
#include "mozilla/Preferences.h" #include "mozilla/Preferences.h"
#include "mozilla/Omnijar.h"
#include "mozilla/ResultExtensions.h" #include "mozilla/ResultExtensions.h"
#include "mozilla/ScriptPreloader.h" #include "mozilla/ScriptPreloader.h"
#include "mozilla/ScopeExit.h" #include "mozilla/ScopeExit.h"
@ -671,10 +670,6 @@ JSObject* mozJSComponentLoader::PrepareObjectForLocation(
static mozilla::Result<nsCString, nsresult> ReadScript( static mozilla::Result<nsCString, nsresult> ReadScript(
ComponentLoaderInfo& aInfo) { ComponentLoaderInfo& aInfo) {
// We're going to cache the XDR encoded script data - suspend writes via the
// CacheAwareZipReader, otherwise we'll end up redundantly caching scripts.
AutoSuspendStartupCacheWrites suspendScache;
MOZ_TRY(aInfo.EnsureScriptChannel()); MOZ_TRY(aInfo.EnsureScriptChannel());
nsCOMPtr<nsIInputStream> scriptStream; nsCOMPtr<nsIInputStream> scriptStream;
@ -736,15 +731,7 @@ nsresult mozJSComponentLoader::ObjectForLocation(
// to loading the script, since we can always slow-load. // to loading the script, since we can always slow-load.
bool writeToCache = false; bool writeToCache = false;
StartupCache* cache = StartupCache::GetSingleton();
// Since we are intending to cache these buffers in the script preloader
// already, caching them in the StartupCache tends to be redundant. This
// ought to be addressed, but as in bug 1627075 we extended the
// StartupCache to be multi-process, we just didn't want to propagate
// this problem into yet more processes, so we pretend the StartupCache
// doesn't exist if we're not the parent process.
StartupCache* cache =
XRE_IsParentProcess() ? StartupCache::GetSingleton() : nullptr;
aInfo.EnsureResolvedURI(); aInfo.EnsureResolvedURI();

Просмотреть файл

@ -24,7 +24,6 @@
#include "mozilla/ContentPrincipal.h" #include "mozilla/ContentPrincipal.h"
#include "mozilla/dom/ScriptLoader.h" #include "mozilla/dom/ScriptLoader.h"
#include "mozilla/Omnijar.h"
#include "mozilla/ScriptPreloader.h" #include "mozilla/ScriptPreloader.h"
#include "mozilla/SystemPrincipal.h" #include "mozilla/SystemPrincipal.h"
#include "mozilla/scache/StartupCache.h" #include "mozilla/scache/StartupCache.h"
@ -249,10 +248,6 @@ bool mozJSSubScriptLoader::ReadScript(JS::MutableHandle<JSScript*> script,
const char* uriStr, nsIIOService* serv, const char* uriStr, nsIIOService* serv,
bool wantReturnValue, bool wantReturnValue,
bool useCompilationScope) { bool useCompilationScope) {
// We're going to cache the XDR encoded script data - suspend writes via the
// CacheAwareZipReader, otherwise we'll end up redundantly caching scripts.
AutoSuspendStartupCacheWrites suspendScache;
// We create a channel and call SetContentType, to avoid expensive MIME type // We create a channel and call SetContentType, to avoid expensive MIME type
// lookups (bug 632490). // lookups (bug 632490).
nsCOMPtr<nsIChannel> chan; nsCOMPtr<nsIChannel> chan;
@ -455,15 +450,7 @@ nsresult mozJSSubScriptLoader::DoLoadSubScriptWithOptions(
bool ignoreCache = bool ignoreCache =
options.ignoreCache || !isSystem || scheme.EqualsLiteral("blob"); options.ignoreCache || !isSystem || scheme.EqualsLiteral("blob");
// Since we are intending to cache these buffers in the script preloader StartupCache* cache = ignoreCache ? nullptr : StartupCache::GetSingleton();
// already, caching them in the StartupCache tends to be redundant. This
// ought to be addressed, but as in bug 1627075 we extended the
// StartupCache to be multi-process, we just didn't want to propagate
// this problem into yet more processes, so we pretend the StartupCache
// doesn't exist if we're not the parent process.
StartupCache* cache = (ignoreCache || !XRE_IsParentProcess())
? nullptr
: StartupCache::GetSingleton();
nsAutoCString cachePath; nsAutoCString cachePath;
SubscriptCachePath(cx, uri, targetObj, cachePath); SubscriptCachePath(cx, uri, targetObj, cachePath);

Просмотреть файл

@ -26,7 +26,7 @@ using namespace mozilla;
// The following initialization makes a guess of 10 entries per jarfile. // The following initialization makes a guess of 10 entries per jarfile.
nsJAR::nsJAR() nsJAR::nsJAR()
: mZip(nullptr), : mZip(new nsZipArchive()),
mReleaseTime(PR_INTERVAL_NO_TIMEOUT), mReleaseTime(PR_INTERVAL_NO_TIMEOUT),
mCache(nullptr), mCache(nullptr),
mLock("nsJAR::mLock"), mLock("nsJAR::mLock"),
@ -87,13 +87,12 @@ nsJAR::Open(nsIFile* zipFile) {
// The omnijar is special, it is opened early on and closed late // The omnijar is special, it is opened early on and closed late
// this avoids reopening it // this avoids reopening it
RefPtr<CacheAwareZipReader> zip = mozilla::Omnijar::GetReader(zipFile); RefPtr<nsZipArchive> zip = mozilla::Omnijar::GetReader(zipFile);
if (zip) { if (zip) {
mZip = zip; mZip = zip;
mSkipArchiveClosing = true; mSkipArchiveClosing = true;
return NS_OK; return NS_OK;
} }
mZip = new CacheAwareZipReader();
return mZip->OpenArchive(zipFile); return mZip->OpenArchive(zipFile);
} }
@ -103,7 +102,7 @@ nsJAR::OpenInner(nsIZipReader* aZipReader, const nsACString& aZipEntry) {
if (mOpened) return NS_ERROR_FAILURE; // Already open! if (mOpened) return NS_ERROR_FAILURE; // Already open!
nsJAR* outerJAR = static_cast<nsJAR*>(aZipReader); nsJAR* outerJAR = static_cast<nsJAR*>(aZipReader);
RefPtr<CacheAwareZipReader> innerZip = RefPtr<nsZipArchive> innerZip =
mozilla::Omnijar::GetInnerReader(outerJAR->mZipFile, aZipEntry); mozilla::Omnijar::GetInnerReader(outerJAR->mZipFile, aZipEntry);
if (innerZip) { if (innerZip) {
mOpened = true; mOpened = true;
@ -125,12 +124,11 @@ nsJAR::OpenInner(nsIZipReader* aZipReader, const nsACString& aZipEntry) {
mOuterZipEntry.Assign(aZipEntry); mOuterZipEntry.Assign(aZipEntry);
RefPtr<nsZipHandle> handle; RefPtr<nsZipHandle> handle;
rv = nsZipHandle::Init(static_cast<nsJAR*>(aZipReader)->mZip->GetZipArchive(), rv = nsZipHandle::Init(static_cast<nsJAR*>(aZipReader)->mZip.get(),
PromiseFlatCString(aZipEntry).get(), PromiseFlatCString(aZipEntry).get(),
getter_AddRefs(handle)); getter_AddRefs(handle));
if (NS_FAILED(rv)) return rv; if (NS_FAILED(rv)) return rv;
mZip = new CacheAwareZipReader();
return mZip->OpenArchive(handle); return mZip->OpenArchive(handle);
} }
@ -146,7 +144,6 @@ nsJAR::OpenMemory(void* aData, uint32_t aLength) {
getter_AddRefs(handle)); getter_AddRefs(handle));
if (NS_FAILED(rv)) return rv; if (NS_FAILED(rv)) return rv;
mZip = new CacheAwareZipReader();
return mZip->OpenArchive(handle); return mZip->OpenArchive(handle);
} }
@ -168,7 +165,7 @@ nsJAR::Close() {
if (mSkipArchiveClosing) { if (mSkipArchiveClosing) {
// Reset state, but don't close the omnijar because we did not open it. // Reset state, but don't close the omnijar because we did not open it.
mSkipArchiveClosing = false; mSkipArchiveClosing = false;
mZip = nullptr; mZip = new nsZipArchive();
return NS_OK; return NS_OK;
} }
@ -304,7 +301,7 @@ nsresult nsJAR::GetNSPRFileDesc(PRFileDesc** aNSPRFileDesc) {
return NS_ERROR_FAILURE; return NS_ERROR_FAILURE;
} }
RefPtr<nsZipHandle> handle = mZip->GetZipArchive()->GetFD(); RefPtr<nsZipHandle> handle = mZip->GetFD();
if (!handle) { if (!handle) {
return NS_ERROR_FAILURE; return NS_ERROR_FAILURE;
} }

Просмотреть файл

@ -23,17 +23,13 @@
#include "nsRefPtrHashtable.h" #include "nsRefPtrHashtable.h"
#include "nsTHashtable.h" #include "nsTHashtable.h"
#include "nsIZipReader.h" #include "nsIZipReader.h"
#include "nsZipArchive.h"
#include "nsWeakReference.h" #include "nsWeakReference.h"
#include "nsIObserver.h" #include "nsIObserver.h"
#include "mozilla/Attributes.h" #include "mozilla/Attributes.h"
#include "nsZipArchive.h"
class nsZipReaderCache; class nsZipReaderCache;
namespace mozilla {
class CacheAwareZipReader;
} // namespace mozilla
/*------------------------------------------------------------------------- /*-------------------------------------------------------------------------
* Class nsJAR declaration. * Class nsJAR declaration.
* nsJAR serves as an XPCOM wrapper for nsZipArchive with the addition of * nsJAR serves as an XPCOM wrapper for nsZipArchive with the addition of
@ -76,9 +72,9 @@ class nsJAR final : public nsIZipReader {
protected: protected:
//-- Private data members //-- Private data members
nsCOMPtr<nsIFile> mZipFile; // The zip/jar file on disk nsCOMPtr<nsIFile> mZipFile; // The zip/jar file on disk
nsCString mOuterZipEntry; // The entry in the zip this zip is reading from nsCString mOuterZipEntry; // The entry in the zip this zip is reading from
RefPtr<mozilla::CacheAwareZipReader> mZip; // The underlying zip archive RefPtr<nsZipArchive> mZip; // The underlying zip archive
PRIntervalTime mReleaseTime; // used by nsZipReaderCache for flushing entries PRIntervalTime mReleaseTime; // used by nsZipReaderCache for flushing entries
nsZipReaderCache* nsZipReaderCache*
mCache; // if cached, this points to the cache it's contained in mCache; // if cached, this points to the cache it's contained in

Просмотреть файл

@ -67,15 +67,8 @@ nsresult nsJARInputStream::InitFile(nsJAR* aJar, nsZipItem* item) {
// Must keep handle to filepointer and mmap structure as long as we need // Must keep handle to filepointer and mmap structure as long as we need
// access to the mmapped data // access to the mmapped data
rv = aJar->mZip->GetPersistentHandle(item, &mItemHandle, mFd = aJar->mZip->GetFD();
CacheAwareZipReader::DeferCaching); mZs.next_in = (Bytef*)aJar->mZip->GetData(item);
if (NS_FAILED(rv)) {
return rv;
}
mZs.next_in =
(Bytef*)aJar->mZip->GetData(item, CacheAwareZipReader::DeferCaching);
if (!mZs.next_in) { if (!mZs.next_in) {
return NS_ERROR_FILE_CORRUPTED; return NS_ERROR_FILE_CORRUPTED;
} }
@ -199,7 +192,7 @@ nsJARInputStream::Read(char* aBuffer, uint32_t aCount, uint32_t* aBytesRead) {
*aBytesRead = 0; *aBytesRead = 0;
nsresult rv = NS_OK; nsresult rv = NS_OK;
MMAP_FAULT_HANDLER_BEGIN_HANDLE(mItemHandle.UnderlyingFD()) MMAP_FAULT_HANDLER_BEGIN_HANDLE(mFd)
switch (mMode) { switch (mMode) {
case MODE_NOTINITED: case MODE_NOTINITED:
return NS_OK; return NS_OK;
@ -218,15 +211,15 @@ nsJARInputStream::Read(char* aBuffer, uint32_t aCount, uint32_t* aBytesRead) {
rv = ContinueInflate(aBuffer, aCount, aBytesRead); rv = ContinueInflate(aBuffer, aCount, aBytesRead);
} }
// be aggressive about releasing the file! // be aggressive about releasing the file!
// note that sometimes, we will release mItemHandle before we've finished // note that sometimes, we will release mFd before we've finished
// deflating - this is because zlib buffers the input // deflating - this is because zlib buffers the input
if (mZs.avail_in == 0) { if (mZs.avail_in == 0) {
mItemHandle.ReleaseHandle(); mFd = nullptr;
} }
break; break;
case MODE_COPY: case MODE_COPY:
if (mItemHandle) { if (mFd) {
uint32_t count = std::min(aCount, mOutSize - uint32_t(mZs.total_out)); uint32_t count = std::min(aCount, mOutSize - uint32_t(mZs.total_out));
if (count) { if (count) {
memcpy(aBuffer, mZs.next_in + mZs.total_out, count); memcpy(aBuffer, mZs.next_in + mZs.total_out, count);
@ -235,10 +228,9 @@ nsJARInputStream::Read(char* aBuffer, uint32_t aCount, uint32_t* aBytesRead) {
*aBytesRead = count; *aBytesRead = count;
} }
// be aggressive about releasing the file! // be aggressive about releasing the file!
// note that sometimes, we will release mItemHandle before we've finished // note that sometimes, we will release mFd before we've finished copying.
// copying.
if (mZs.total_out >= mOutSize) { if (mZs.total_out >= mOutSize) {
mItemHandle.ReleaseHandle(); mFd = nullptr;
} }
break; break;
} }
@ -270,7 +262,7 @@ nsJARInputStream::Close() {
} }
#endif #endif
mMode = MODE_CLOSED; mMode = MODE_CLOSED;
mItemHandle.ReleaseHandle(); mFd = nullptr;
return NS_OK; return NS_OK;
} }

Просмотреть файл

@ -11,7 +11,6 @@
#include "nsJAR.h" #include "nsJAR.h"
#include "nsTArray.h" #include "nsTArray.h"
#include "mozilla/Attributes.h" #include "mozilla/Attributes.h"
#include "mozilla/Omnijar.h"
#ifdef MOZ_JAR_BROTLI #ifdef MOZ_JAR_BROTLI
struct BrotliDecoderStateStruct; struct BrotliDecoderStateStruct;
@ -52,11 +51,11 @@ class nsJARInputStream final : public nsIInputStream {
private: private:
~nsJARInputStream() { Close(); } ~nsJARInputStream() { Close(); }
mozilla::CacheAwareZipHandle mItemHandle; // handle for reading RefPtr<nsZipHandle> mFd; // handle for reading
uint32_t mOutSize; // inflated size uint32_t mOutSize; // inflated size
uint32_t mInCrc; // CRC as provided by the zipentry uint32_t mInCrc; // CRC as provided by the zipentry
uint32_t mOutCrc; // CRC as calculated by me uint32_t mOutCrc; // CRC as calculated by me
z_stream mZs; // zip data structure z_stream mZs; // zip data structure
#ifdef MOZ_JAR_BROTLI #ifdef MOZ_JAR_BROTLI
BrotliDecoderStateStruct* mBrotliState; // Brotli decoder state BrotliDecoderStateStruct* mBrotliState; // Brotli decoder state
#endif #endif

Просмотреть файл

@ -341,21 +341,11 @@ nsZipHandle::~nsZipHandle() {
//--------------------------------------------- //---------------------------------------------
// nsZipArchive::OpenArchive // nsZipArchive::OpenArchive
//--------------------------------------------- //---------------------------------------------
nsresult nsZipArchive::OpenArchive(nsZipHandle* aZipHandle, PRFileDesc* aFd, nsresult nsZipArchive::OpenArchive(nsZipHandle* aZipHandle, PRFileDesc* aFd) {
Span<const uint8_t> aCachedCentral) {
mFd = aZipHandle; mFd = aZipHandle;
//-- get table of contents for archive //-- get table of contents for archive
nsresult rv = NS_OK; nsresult rv = BuildFileList(aFd);
if (!mBuiltFileList) {
if (!aCachedCentral.IsEmpty()) {
auto* start = aCachedCentral.Elements();
auto* end = start + aCachedCentral.Length();
rv = BuildFileListFromBuffer(start, end);
} else {
rv = BuildFileList(aFd);
}
}
if (NS_SUCCEEDED(rv)) { if (NS_SUCCEEDED(rv)) {
if (aZipHandle->mFile && XRE_IsParentProcess()) { if (aZipHandle->mFile && XRE_IsParentProcess()) {
static char* env = PR_GetEnv("MOZ_JAR_LOG_FILE"); static char* env = PR_GetEnv("MOZ_JAR_LOG_FILE");
@ -409,8 +399,7 @@ nsresult nsZipArchive::OpenArchive(nsZipHandle* aZipHandle, PRFileDesc* aFd,
return rv; return rv;
} }
nsresult nsZipArchive::OpenArchive(nsIFile* aFile, nsresult nsZipArchive::OpenArchive(nsIFile* aFile) {
Span<const uint8_t> aCachedCentral) {
RefPtr<nsZipHandle> handle; RefPtr<nsZipHandle> handle;
#if defined(XP_WIN) #if defined(XP_WIN)
mozilla::AutoFDClose fd; mozilla::AutoFDClose fd;
@ -421,9 +410,9 @@ nsresult nsZipArchive::OpenArchive(nsIFile* aFile,
if (NS_FAILED(rv)) return rv; if (NS_FAILED(rv)) return rv;
#if defined(XP_WIN) #if defined(XP_WIN)
return OpenArchive(handle, fd.get(), aCachedCentral); return OpenArchive(handle, fd.get());
#else #else
return OpenArchive(handle, nullptr, aCachedCentral); return OpenArchive(handle);
#endif #endif
} }
@ -473,75 +462,13 @@ nsresult nsZipArchive::CloseArchive() {
// Let us also cleanup the mFiles table for re-use on the next 'open' call // Let us also cleanup the mFiles table for re-use on the next 'open' call
memset(mFiles, 0, sizeof(mFiles)); memset(mFiles, 0, sizeof(mFiles));
mBuiltSynthetics = false; mBuiltSynthetics = false;
AutoWriteLock lock(mLazyOpenLock);
mLazyOpenParams = Nothing();
return NS_OK; return NS_OK;
} }
nsresult nsZipArchive::EnsureArchiveOpenedOnDisk() {
{
AutoReadLock lock(mLazyOpenLock);
if (!mLazyOpenParams) {
return NS_OK;
}
}
AutoWriteLock lock(mLazyOpenLock);
if (!mLazyOpenParams) {
// Another thread beat us to opening the archive while we were waiting on
// the mutex.
return NS_OK;
}
nsresult rv = OpenArchive(mLazyOpenParams->mFile);
if (NS_WARN_IF(NS_FAILED(rv))) {
return NS_ERROR_UNEXPECTED;
}
mLazyOpenParams = Nothing();
return NS_OK;
}
nsresult nsZipArchive::EnsureFileListBuilt() {
{
AutoReadLock lock(mLazyOpenLock);
if (!mLazyOpenParams || mBuiltFileList) {
return NS_OK;
}
}
AutoWriteLock lock(mLazyOpenLock);
if (!mLazyOpenParams || mBuiltFileList) {
// Another thread beat us to building the file list while we were waiting
// on the mutex.
return NS_OK;
}
nsresult rv;
if (!mLazyOpenParams->mCachedCentral.IsEmpty()) {
auto* start = mLazyOpenParams->mCachedCentral.Elements();
auto* end = start + mLazyOpenParams->mCachedCentral.Length();
rv = BuildFileListFromBuffer(start, end);
} else {
rv = OpenArchive(mLazyOpenParams->mFile);
mLazyOpenParams = Nothing();
}
return rv;
}
//--------------------------------------------- //---------------------------------------------
// nsZipArchive::GetItem // nsZipArchive::GetItem
//--------------------------------------------- //---------------------------------------------
nsZipItem* nsZipArchive::GetItem(const char* aEntryName) { nsZipItem* nsZipArchive::GetItem(const char* aEntryName) {
nsresult rv = EnsureFileListBuilt();
if (NS_WARN_IF(NS_FAILED(rv))) {
return nullptr;
}
if (aEntryName) { if (aEntryName) {
uint32_t len = strlen(aEntryName); uint32_t len = strlen(aEntryName);
//-- If the request is for a directory, make sure that synthetic entries //-- If the request is for a directory, make sure that synthetic entries
@ -625,12 +552,6 @@ nsresult nsZipArchive::ExtractFile(nsZipItem* item, nsIFile* outFile,
nsresult nsZipArchive::FindInit(const char* aPattern, nsZipFind** aFind) { nsresult nsZipArchive::FindInit(const char* aPattern, nsZipFind** aFind) {
if (!aFind) return NS_ERROR_ILLEGAL_VALUE; if (!aFind) return NS_ERROR_ILLEGAL_VALUE;
nsresult rv = EnsureFileListBuilt();
if (NS_WARN_IF(NS_FAILED(rv))) {
return NS_ERROR_UNEXPECTED;
}
// null out param in case an error happens // null out param in case an error happens
*aFind = nullptr; *aFind = nullptr;
@ -638,7 +559,7 @@ nsresult nsZipArchive::FindInit(const char* aPattern, nsZipFind** aFind) {
char* pattern = 0; char* pattern = 0;
// Create synthetic directory entries on demand // Create synthetic directory entries on demand
rv = BuildSynthetics(); nsresult rv = BuildSynthetics();
if (rv != NS_OK) return rv; if (rv != NS_OK) return rv;
// validate the pattern // validate the pattern
@ -682,12 +603,7 @@ nsresult nsZipFind::FindNext(const char** aResult, uint16_t* aNameLen) {
*aResult = 0; *aResult = 0;
*aNameLen = 0; *aNameLen = 0;
MMAP_FAULT_HANDLER_BEGIN_HANDLE(mArchive->GetFD())
// NOTE: don't use GetFD here. if mFd is not null, then we need to have this
// fault handler, as we may be reading from the memory mapped file. However
// if it is null, then we can guarantee that we're reading here from a cached
// buffer, which we assume is not mapped to a file.
MMAP_FAULT_HANDLER_BEGIN_HANDLE(mArchive->mFd)
// we start from last match, look for next // we start from last match, look for next
while (mSlot < ZIP_TABSIZE) { while (mSlot < ZIP_TABSIZE) {
// move to next in current chain, or move to new slot // move to next in current chain, or move to new slot
@ -733,13 +649,10 @@ nsZipItem* nsZipArchive::CreateZipItem() {
// nsZipArchive::BuildFileList // nsZipArchive::BuildFileList
//--------------------------------------------- //---------------------------------------------
nsresult nsZipArchive::BuildFileList(PRFileDesc* aFd) { nsresult nsZipArchive::BuildFileList(PRFileDesc* aFd) {
mBuiltFileList = true;
// Get archive size using end pos // Get archive size using end pos
const uint8_t* buf; const uint8_t* buf;
const uint8_t* startp = mFd->mFileData; const uint8_t* startp = mFd->mFileData;
const uint8_t* endp = startp + mFd->mLen; const uint8_t* endp = startp + mFd->mLen;
nsresult rv;
MMAP_FAULT_HANDLER_BEGIN_HANDLE(mFd) MMAP_FAULT_HANDLER_BEGIN_HANDLE(mFd)
uint32_t centralOffset = 4; uint32_t centralOffset = 4;
// Only perform readahead in the parent process. Children processes // Only perform readahead in the parent process. Children processes
@ -764,63 +677,20 @@ nsresult nsZipArchive::BuildFileList(PRFileDesc* aFd) {
return NS_ERROR_FILE_CORRUPTED; return NS_ERROR_FILE_CORRUPTED;
} }
uintptr_t startpInt = reinterpret_cast<uintptr_t>(startp); buf = startp + centralOffset;
if (startpInt + centralOffset < startpInt || centralOffset > mFd->mLen) {
// avoid overflow of startp + centralOffset.
if (buf < startp) {
return NS_ERROR_FILE_CORRUPTED; return NS_ERROR_FILE_CORRUPTED;
} }
buf = startp + centralOffset;
mZipCentralOffset = centralOffset;
rv = BuildFileListFromBuffer(buf, endp);
MMAP_FAULT_HANDLER_CATCH(NS_ERROR_FAILURE)
return rv;
}
UniquePtr<uint8_t[]> nsZipArchive::CopyCentralDirectoryBuffer(size_t* aSize) {
*aSize = 0;
// mZipCentralOffset could in theory be 0. In practice though, we likely
// won't ever see this. If the end result is that we can't cache the buffer
// in these cases, that's fine.
if (!mZipCentralOffset || !mZipCentralSize) {
return nullptr;
}
const uint8_t* buf;
const uint8_t* startp = mFd->mFileData;
buf = startp + mZipCentralOffset;
// Just a sanity check to make sure these values haven't overflowed the
// buffer mapped to our file. Technically the pointer could overflow the max
// pointer value, but that could only happen with this check succeeding if
// mFd->mLen is incorrect, which we will here assume is impossible.
if (mZipCentralOffset + mZipCentralSize > mFd->mLen) {
return nullptr;
}
auto resultBuf = MakeUnique<uint8_t[]>(mZipCentralSize);
MMAP_FAULT_HANDLER_BEGIN_HANDLE(mFd)
memcpy(resultBuf.get(), buf, mZipCentralSize);
MMAP_FAULT_HANDLER_CATCH(nullptr)
*aSize = mZipCentralSize;
return resultBuf;
}
nsresult nsZipArchive::BuildFileListFromBuffer(const uint8_t* aBuf,
const uint8_t* aEnd) {
mBuiltFileList = true;
const uint8_t* buf = aBuf;
//-- Read the central directory headers //-- Read the central directory headers
uint32_t sig = 0; uint32_t sig = 0;
while ((buf + int32_t(sizeof(uint32_t)) > buf) && while ((buf + int32_t(sizeof(uint32_t)) > buf) &&
(buf + int32_t(sizeof(uint32_t)) <= aEnd) && (buf + int32_t(sizeof(uint32_t)) <= endp) &&
((sig = xtolong(buf)) == CENTRALSIG)) { ((sig = xtolong(buf)) == CENTRALSIG)) {
// Make sure there is enough data available. // Make sure there is enough data available.
if ((buf > aEnd) || (aEnd - buf < ZIPCENTRAL_SIZE)) { if ((buf > endp) || (endp - buf < ZIPCENTRAL_SIZE)) {
return NS_ERROR_FILE_CORRUPTED; return NS_ERROR_FILE_CORRUPTED;
} }
@ -838,7 +708,7 @@ nsresult nsZipArchive::BuildFileListFromBuffer(const uint8_t* aBuf,
return NS_ERROR_FILE_CORRUPTED; return NS_ERROR_FILE_CORRUPTED;
} }
if (buf >= buf + diff || // No overflow if (buf >= buf + diff || // No overflow
buf >= aEnd - diff) { buf >= endp - diff) {
return NS_ERROR_FILE_CORRUPTED; return NS_ERROR_FILE_CORRUPTED;
} }
@ -865,18 +735,18 @@ nsresult nsZipArchive::BuildFileListFromBuffer(const uint8_t* aBuf,
} }
// Make the comment available for consumers. // Make the comment available for consumers.
if ((aEnd >= buf) && (aEnd - buf >= ZIPEND_SIZE)) { if ((endp >= buf) && (endp - buf >= ZIPEND_SIZE)) {
ZipEnd* zipend = (ZipEnd*)buf; ZipEnd* zipend = (ZipEnd*)buf;
buf += ZIPEND_SIZE; buf += ZIPEND_SIZE;
uint16_t commentlen = xtoint(zipend->commentfield_len); uint16_t commentlen = xtoint(zipend->commentfield_len);
if (aEnd - buf >= commentlen) { if (endp - buf >= commentlen) {
mCommentPtr = (const char*)aBuf; mCommentPtr = (const char*)buf;
mCommentLen = commentlen; mCommentLen = commentlen;
} }
} }
mZipCentralSize = buf - aBuf; MMAP_FAULT_HANDLER_CATCH(NS_ERROR_FAILURE)
return NS_OK; return NS_OK;
} }
@ -946,64 +816,15 @@ nsresult nsZipArchive::BuildSynthetics() {
} }
nsZipHandle* nsZipArchive::GetFD() { nsZipHandle* nsZipArchive::GetFD() {
nsresult rv = EnsureArchiveOpenedOnDisk(); if (!mFd) return nullptr;
if (NS_WARN_IF(NS_FAILED(rv))) {
return nullptr;
}
return mFd.get(); return mFd.get();
} }
void nsZipArchive::GetURIString(nsACString& result) {
{
AutoReadLock lock(mLazyOpenLock);
if (!mLazyOpenParams) {
mFd->mFile.GetURIString(result);
return;
}
}
AutoReadLock lock(mLazyOpenLock);
if (!mLazyOpenParams) {
// Another thread consumed mLazyOpenParams while we were waiting.
mFd->mFile.GetURIString(result);
return;
}
// This is a bit tricky - typically, we could just
// NS_GetURLSpecFromActualFile from mLazyOpenParams->mFile or from
// mFd->mFile.GetBaseFile(), depending on which we currently have. However,
// this won't actually be correct if this zip archive is nested inside
// another archive. However, at present, we know that mLazyOpenParams can
// only be here if we were opened from a real underlying file, so we assume
// that we're safe to do this. Any future code that breaks this assumption
// will need to update things here.
NS_GetURLSpecFromActualFile(mLazyOpenParams->mFile, result);
}
already_AddRefed<nsIFile> nsZipArchive::GetBaseFile() {
{
AutoReadLock lock(mLazyOpenLock);
if (!mLazyOpenParams) {
return mFd->mFile.GetBaseFile();
}
}
AutoReadLock lock(mLazyOpenLock);
if (!mLazyOpenParams) {
// Another thread consumed mLazyOpenParams while we were waiting.
return mFd->mFile.GetBaseFile();
}
nsCOMPtr<nsIFile> file = mLazyOpenParams->mFile;
return file.forget();
}
//--------------------------------------------- //---------------------------------------------
// nsZipArchive::GetDataOffset // nsZipArchive::GetDataOffset
//--------------------------------------------- //---------------------------------------------
uint32_t nsZipArchive::GetDataOffset(nsZipItem* aItem) { uint32_t nsZipArchive::GetDataOffset(nsZipItem* aItem) {
MOZ_ASSERT(aItem); MOZ_ASSERT(aItem);
nsresult rv = EnsureArchiveOpenedOnDisk();
MOZ_RELEASE_ASSERT(!NS_FAILED(rv),
"Should have been able to open the zip archive");
uint32_t offset; uint32_t offset;
MMAP_FAULT_HANDLER_BEGIN_HANDLE(mFd) MMAP_FAULT_HANDLER_BEGIN_HANDLE(mFd)
@ -1034,10 +855,6 @@ uint32_t nsZipArchive::GetDataOffset(nsZipItem* aItem) {
//--------------------------------------------- //---------------------------------------------
const uint8_t* nsZipArchive::GetData(nsZipItem* aItem) { const uint8_t* nsZipArchive::GetData(nsZipItem* aItem) {
MOZ_ASSERT(aItem); MOZ_ASSERT(aItem);
nsresult rv = EnsureArchiveOpenedOnDisk();
if (NS_WARN_IF(NS_FAILED(rv))) {
return nullptr;
}
uint32_t offset = GetDataOffset(aItem); uint32_t offset = GetDataOffset(aItem);
MMAP_FAULT_HANDLER_BEGIN_HANDLE(mFd) MMAP_FAULT_HANDLER_BEGIN_HANDLE(mFd)
@ -1072,12 +889,8 @@ int64_t nsZipArchive::SizeOfMapping() { return mFd ? mFd->SizeOfMapping() : 0; }
nsZipArchive::nsZipArchive() nsZipArchive::nsZipArchive()
: mRefCnt(0), : mRefCnt(0),
mCommentPtr(nullptr), mCommentPtr(nullptr),
mZipCentralOffset(0),
mZipCentralSize(0),
mCommentLen(0), mCommentLen(0),
mBuiltSynthetics(false), mBuiltSynthetics(false),
mBuiltFileList(false),
mLazyOpenLock("nsZipArchive::mLazyOpenLock"),
mUseZipLog(false) { mUseZipLog(false) {
// initialize the table to nullptr // initialize the table to nullptr
memset(mFiles, 0, sizeof(mFiles)); memset(mFiles, 0, sizeof(mFiles));
@ -1385,14 +1198,12 @@ uint8_t* nsZipCursor::ReadOrCopy(uint32_t* aBytesRead, bool aCopy) {
nsZipItemPtr_base::nsZipItemPtr_base(nsZipArchive* aZip, const char* aEntryName, nsZipItemPtr_base::nsZipItemPtr_base(nsZipArchive* aZip, const char* aEntryName,
bool doCRC) bool doCRC)
: mReturnBuf(nullptr), mReadlen(0) { : mReturnBuf(nullptr), mReadlen(0) {
nsZipItem* item = aZip->GetItem(aEntryName);
if (!item) {
return;
}
// make sure the ziparchive hangs around // make sure the ziparchive hangs around
mZipHandle = aZip->GetFD(); mZipHandle = aZip->GetFD();
nsZipItem* item = aZip->GetItem(aEntryName);
if (!item) return;
uint32_t size = 0; uint32_t size = 0;
bool compressed = (item->Compression() == DEFLATED); bool compressed = (item->Compression() == DEFLATED);
#ifdef MOZ_JAR_BROTLI #ifdef MOZ_JAR_BROTLI

Просмотреть файл

@ -17,11 +17,9 @@
#include "nsIFile.h" #include "nsIFile.h"
#include "nsISupportsImpl.h" // For mozilla::ThreadSafeAutoRefCnt #include "nsISupportsImpl.h" // For mozilla::ThreadSafeAutoRefCnt
#include "mozilla/ArenaAllocator.h" #include "mozilla/ArenaAllocator.h"
#include "mozilla/Atomics.h"
#include "mozilla/FileUtils.h" #include "mozilla/FileUtils.h"
#include "mozilla/FileLocation.h" #include "mozilla/FileLocation.h"
#include "mozilla/UniquePtr.h" #include "mozilla/UniquePtr.h"
#include "mozilla/RWLock.h"
class nsZipFind; class nsZipFind;
struct PRFileDesc; struct PRFileDesc;
@ -86,22 +84,6 @@ class nsZipArchive final {
/** destructing the object closes the archive */ /** destructing the object closes the archive */
~nsZipArchive(); ~nsZipArchive();
/**
* LazyOpenArchiveParams is a class which is used to store cached
* contents of omnijars.
*
*/
struct LazyOpenArchiveParams {
nsCOMPtr<nsIFile> mFile;
mozilla::Span<const uint8_t> mCachedCentral;
LazyOpenArchiveParams(nsIFile* aFile,
mozilla::Span<const uint8_t> aCachedCentral)
: mFile(nullptr), mCachedCentral(aCachedCentral) {
aFile->Clone(getter_AddRefs(mFile));
}
};
public: public:
static const char* sFileCorruptedReason; static const char* sFileCorruptedReason;
@ -115,16 +97,11 @@ class nsZipArchive final {
* object. If we were allowed to use exceptions this would have been * object. If we were allowed to use exceptions this would have been
* part of the constructor * part of the constructor
* *
* @param aZipHandle The nsZipHandle used to access the zip * @param aZipHandle The nsZipHandle used to access the zip
* @param aFd Optional PRFileDesc for Windows readahead * @param aFd Optional PRFileDesc for Windows readahead optimization
optimization
* @param aCachedCentral Optional cached buffer containing the zip central
for this zip.
* @return status code * @return status code
*/ */
nsresult OpenArchive(nsZipHandle* aZipHandle, PRFileDesc* aFd = nullptr, nsresult OpenArchive(nsZipHandle* aZipHandle, PRFileDesc* aFd = nullptr);
mozilla::Span<const uint8_t> aCachedCentral =
mozilla::Span<const uint8_t>());
/** /**
* OpenArchive * OpenArchive
@ -132,46 +109,9 @@ class nsZipArchive final {
* Convenience function that generates nsZipHandle * Convenience function that generates nsZipHandle
* *
* @param aFile The file used to access the zip * @param aFile The file used to access the zip
* @param aCachedCentral Optional cached buffer containing the zip central
for this zip.
* @return status code * @return status code
*/ */
nsresult OpenArchive(nsIFile* aFile, nsresult OpenArchive(nsIFile* aFile);
mozilla::Span<const uint8_t> aCachedCentral =
mozilla::Span<const uint8_t>());
/**
* Ensures underlying archive is opened, if it was opened with
* LazyOpenArchive.
*
* Convenience function that generates nsZipHandle
*
* @param aFile The file used to access the zip
* @return status code
*/
nsresult EnsureArchiveOpenedOnDisk();
/**
* OpenArchive
*
* Lazily opens the zip archive on the first request to get data from it.
* NOTE: The buffer provided for aCachedCentral must outlive this
* nsZipArchive. This is presently true for the StartupCache, as it ensures
* that even past cache invalidation, all accessed buffers persist for the
* lifetime of the application, but we will need to ensure that this remains
* true.
*
* @param aFile The file used to access the zip
* @param aCachedCentral Cached buffer containing the zip central
for this zip.
* @return status code
*/
nsresult LazyOpenArchive(nsIFile* aFile,
mozilla::Span<const uint8_t> aCachedCentral) {
mozilla::AutoWriteLock lock(mLazyOpenLock);
mLazyOpenParams.emplace(aFile, aCachedCentral);
return NS_OK;
}
/** /**
* Test the integrity of items in this archive by running * Test the integrity of items in this archive by running
@ -227,21 +167,6 @@ class nsZipArchive final {
*/ */
nsZipHandle* GetFD(); nsZipHandle* GetFD();
/*
* Gets the URI string to the mapped file. One could get this URI string
* in a roundabout way using GetFD, but GetFD requires opening the file for
* read access, which can be expensive.
*/
void GetURIString(nsACString& result);
/*
* Gets the underlying nsIFile pointer. Like GetURIString, this is to be
* preferred over GetFD where possible, because it does not require opening
* the file for read access, which can be expensive, and is to be avoided
* when possible during application startup.
*/
already_AddRefed<nsIFile> GetBaseFile();
/** /**
* Gets the data offset. * Gets the data offset.
* @param aItem Pointer to nsZipItem * @param aItem Pointer to nsZipItem
@ -256,17 +181,6 @@ class nsZipArchive final {
*/ */
const uint8_t* GetData(nsZipItem* aItem); const uint8_t* GetData(nsZipItem* aItem);
/**
* Copies the contents of the zip central directory, and returns it to the
* caller to take ownership. This is useful for caching the contents of the
* central directory, which can be compressed and stored elsewhere, and
* passed back into OpenArchive when this archive is opened in the future.
*
* @param aSize size_t pointer to be filled with the size of the
returned buffer.
*/
mozilla::UniquePtr<uint8_t[]> CopyCentralDirectoryBuffer(size_t* aSize);
bool GetComment(nsACString& aComment); bool GetComment(nsACString& aComment);
/** /**
@ -290,20 +204,14 @@ class nsZipArchive final {
mozilla::ArenaAllocator<1024, sizeof(void*)> mArena; mozilla::ArenaAllocator<1024, sizeof(void*)> mArena;
const char* mCommentPtr; const char* mCommentPtr;
size_t mZipCentralOffset;
size_t mZipCentralSize;
uint16_t mCommentLen; uint16_t mCommentLen;
// Whether we synthesized the directory entries // Whether we synthesized the directory entries
bool mBuiltSynthetics; bool mBuiltSynthetics;
bool mBuiltFileList;
// file handle // file handle
RefPtr<nsZipHandle> mFd; RefPtr<nsZipHandle> mFd;
mozilla::Maybe<LazyOpenArchiveParams> mLazyOpenParams;
mozilla::RWLock mLazyOpenLock;
// file URI, for logging // file URI, for logging
nsCString mURI; nsCString mURI;
@ -315,9 +223,7 @@ class nsZipArchive final {
//--- private methods --- //--- private methods ---
nsZipItem* CreateZipItem(); nsZipItem* CreateZipItem();
nsresult BuildFileList(PRFileDesc* aFd = nullptr); nsresult BuildFileList(PRFileDesc* aFd = nullptr);
nsresult BuildFileListFromBuffer(const uint8_t* aBuf, const uint8_t* aEnd);
nsresult BuildSynthetics(); nsresult BuildSynthetics();
nsresult EnsureFileListBuilt();
nsZipArchive& operator=(const nsZipArchive& rhs) = delete; nsZipArchive& operator=(const nsZipArchive& rhs) = delete;
nsZipArchive(const nsZipArchive& rhs) = delete; nsZipArchive(const nsZipArchive& rhs) = delete;

Просмотреть файл

@ -82,6 +82,7 @@
#include "nsXPCOMCID.h" #include "nsXPCOMCID.h"
#include "nsXPCOM.h" #include "nsXPCOM.h"
#include "nsXULAppAPI.h" #include "nsXULAppAPI.h"
#include "nsZipArchive.h"
#include "plbase64.h" #include "plbase64.h"
#include "PLDHashTable.h" #include "PLDHashTable.h"
#include "plstr.h" #include "plstr.h"
@ -4311,7 +4312,7 @@ static nsresult pref_LoadPrefsInDir(nsIFile* aDir,
return rv; return rv;
} }
static nsresult pref_ReadPrefFromJar(CacheAwareZipReader* aJarReader, static nsresult pref_ReadPrefFromJar(nsZipArchive* aJarReader,
const char* aName) { const char* aName) {
TimeStamp startTime = TimeStamp::Now(); TimeStamp startTime = TimeStamp::Now();
@ -4328,8 +4329,8 @@ static nsresult pref_ReadPrefFromJar(CacheAwareZipReader* aJarReader,
return NS_OK; return NS_OK;
} }
static nsresult pref_ReadDefaultPrefs( static nsresult pref_ReadDefaultPrefs(const RefPtr<nsZipArchive> jarReader,
const RefPtr<CacheAwareZipReader>& jarReader, const char* path) { const char* path) {
UniquePtr<nsZipFind> find; UniquePtr<nsZipFind> find;
nsTArray<nsCString> prefEntries; nsTArray<nsCString> prefEntries;
const char* entryName; const char* entryName;
@ -4508,7 +4509,7 @@ nsresult Preferences::InitInitialObjects(bool aIsStartup) {
const char* entryName; const char* entryName;
uint16_t entryNameLen; uint16_t entryNameLen;
RefPtr<CacheAwareZipReader> jarReader = Omnijar::GetReader(Omnijar::GRE); RefPtr<nsZipArchive> jarReader = Omnijar::GetReader(Omnijar::GRE);
if (jarReader) { if (jarReader) {
#ifdef MOZ_WIDGET_ANDROID #ifdef MOZ_WIDGET_ANDROID
// Try to load an architecture-specific greprefs.js first. This will be // Try to load an architecture-specific greprefs.js first. This will be
@ -4590,7 +4591,7 @@ nsresult Preferences::InitInitialObjects(bool aIsStartup) {
// Load jar:$app/omni.jar!/defaults/preferences/*.js // Load jar:$app/omni.jar!/defaults/preferences/*.js
// or jar:$gre/omni.jar!/defaults/preferences/*.js. // or jar:$gre/omni.jar!/defaults/preferences/*.js.
RefPtr<CacheAwareZipReader> appJarReader = Omnijar::GetReader(Omnijar::APP); RefPtr<nsZipArchive> appJarReader = Omnijar::GetReader(Omnijar::APP);
// GetReader(Omnijar::APP) returns null when `$app == $gre`, in // GetReader(Omnijar::APP) returns null when `$app == $gre`, in
// which case we look for app-specific default preferences in $gre. // which case we look for app-specific default preferences in $gre.

Просмотреть файл

@ -1,31 +0,0 @@
/* -*- Mode: C++; c-basic-offset: 4; indent-tabs-mode: nil; tab-width: 8 -*- */
/* vim: set sw=4 ts=8 et tw=80 ft=cpp : */
/* 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 protocol PContent;
using class mozilla::TimeStamp from "mozilla/TimeStamp.h";
using mozilla::void_t from "ipc/IPCMessageUtils.h";
namespace mozilla {
namespace scache {
struct EntryData {
nsCString key;
// This will be an empty array if data is present in the previous
// session's cache.
uint8_t[] data;
};
protocol PStartupCache
{
manager PContent;
parent:
async __delete__(EntryData[] entries);
};
} // namespace scache
} // namespace mozilla

Разница между файлами не показана из-за своего большого размера Загрузить разницу

Просмотреть файл

@ -22,13 +22,10 @@
#include "mozilla/Attributes.h" #include "mozilla/Attributes.h"
#include "mozilla/AutoMemMap.h" #include "mozilla/AutoMemMap.h"
#include "mozilla/Compression.h" #include "mozilla/Compression.h"
#include "mozilla/EnumSet.h"
#include "mozilla/MemoryReporting.h" #include "mozilla/MemoryReporting.h"
#include "mozilla/Mutex.h" #include "mozilla/Mutex.h"
#include "mozilla/Omnijar.h"
#include "mozilla/Result.h" #include "mozilla/Result.h"
#include "mozilla/UniquePtr.h" #include "mozilla/UniquePtr.h"
#include "mozilla/UniquePtrExtensions.h"
/** /**
* The StartupCache is a persistent cache of simple key-value pairs, * The StartupCache is a persistent cache of simple key-value pairs,
@ -81,153 +78,47 @@
*/ */
namespace mozilla { namespace mozilla {
namespace dom {
class ContentParent;
}
namespace ipc {
class GeckoChildProcessHost;
} // namespace ipc
namespace scache { namespace scache {
class StartupCacheChild;
#ifdef XP_UNIX
// Please see bug 1440207 about improving the problem of random fixed FDs,
// which the addition of the below constant exacerbates.
static const int kStartupCacheFd = 11;
#endif
// We use INT_MAX here just to simplify the sorting - we want to push
// unrequested entries to the back, and have requested entries in the order
// they came in.
static const int kStartupCacheEntryNotRequested = INT_MAX;
static const int kStartupcacheEntryNotInSharedData = -1;
// StartupCache entries can be backed by a buffer which they allocate as
// soon as they are requested, into which they decompress the contents out
// of the memory mapped file, *or* they can be backed by a contiguous buffer
// which we allocate up front and decompress into, in order to share it with
// child processes. This class is a helper class to hold a buffer which the
// entry itself may or may not own.
//
// Side note: it may be appropriate for StartupCache entries to never own
// their underlying buffers. We explicitly work to ensure that anything the
// StartupCache returns to a caller survives for the lifetime of the
// application, so it may be preferable to have a set of large contiguous
// buffers which we allocate on demand, and fill up with cache entry contents,
// but at that point we're basically implementing our own hacky pseudo-malloc,
// for relatively uncertain performance gains. For the time being, we just
// keep the existing model unchanged.
class MaybeOwnedCharPtr {
public:
char* mPtr;
bool mOwned;
~MaybeOwnedCharPtr() {
if (mOwned) {
delete[] mPtr;
}
}
MaybeOwnedCharPtr(const MaybeOwnedCharPtr& other);
MaybeOwnedCharPtr& operator=(const MaybeOwnedCharPtr& other);
MaybeOwnedCharPtr(MaybeOwnedCharPtr&& other)
: mPtr(std::exchange(other.mPtr, nullptr)),
mOwned(std::exchange(other.mOwned, false)) {}
MaybeOwnedCharPtr& operator=(MaybeOwnedCharPtr&& other) {
std::swap(mPtr, other.mPtr);
std::swap(mOwned, other.mOwned);
return *this;
}
MaybeOwnedCharPtr& operator=(decltype(nullptr)) {
mPtr = nullptr;
mOwned = false;
return *this;
}
explicit operator bool() const { return !!mPtr; }
char* get() { return mPtr; }
explicit MaybeOwnedCharPtr(char* aBytes) : mPtr(aBytes), mOwned(false) {}
explicit MaybeOwnedCharPtr(UniquePtr<char[]>&& aBytes)
: mPtr(aBytes.release()), mOwned(true) {}
explicit MaybeOwnedCharPtr(size_t size)
: mPtr(new char[size]), mOwned(true) {}
};
enum class StartupCacheEntryFlags {
Shared,
RequestedByChild,
AddedThisSession,
};
struct StartupCacheEntry { struct StartupCacheEntry {
MaybeOwnedCharPtr mData; UniquePtr<char[]> mData;
uint32_t mOffset; uint32_t mOffset;
uint32_t mCompressedSize; uint32_t mCompressedSize;
uint32_t mUncompressedSize; uint32_t mUncompressedSize;
int32_t mSharedDataOffset;
int32_t mHeaderOffsetInFile; int32_t mHeaderOffsetInFile;
int32_t mRequestedOrder; int32_t mRequestedOrder;
EnumSet<StartupCacheEntryFlags> mFlags; bool mRequested;
MOZ_IMPLICIT StartupCacheEntry(uint32_t aOffset, uint32_t aCompressedSize, MOZ_IMPLICIT StartupCacheEntry(uint32_t aOffset, uint32_t aCompressedSize,
uint32_t aUncompressedSize, uint32_t aUncompressedSize)
EnumSet<StartupCacheEntryFlags> aFlags)
: mData(nullptr), : mData(nullptr),
mOffset(aOffset), mOffset(aOffset),
mCompressedSize(aCompressedSize), mCompressedSize(aCompressedSize),
mUncompressedSize(aUncompressedSize), mUncompressedSize(aUncompressedSize),
mSharedDataOffset(kStartupcacheEntryNotInSharedData),
mHeaderOffsetInFile(0), mHeaderOffsetInFile(0),
mRequestedOrder(kStartupCacheEntryNotRequested), mRequestedOrder(0),
mFlags(aFlags) {} mRequested(false) {}
StartupCacheEntry(UniquePtr<char[]> aData, size_t aLength, StartupCacheEntry(UniquePtr<char[]> aData, size_t aLength,
int32_t aRequestedOrder, int32_t aRequestedOrder)
EnumSet<StartupCacheEntryFlags> aFlags)
: mData(std::move(aData)), : mData(std::move(aData)),
mOffset(0), mOffset(0),
mCompressedSize(0), mCompressedSize(0),
mUncompressedSize(aLength), mUncompressedSize(aLength),
mSharedDataOffset(kStartupcacheEntryNotInSharedData),
mHeaderOffsetInFile(0), mHeaderOffsetInFile(0),
mRequestedOrder(aRequestedOrder), mRequestedOrder(0),
mFlags(aFlags) {} mRequested(true) {}
struct Comparator { struct Comparator {
using Value = std::pair<const nsCString*, StartupCacheEntry*>; using Value = std::pair<const nsCString*, StartupCacheEntry*>;
bool Equals(const Value& a, const Value& b) const { bool Equals(const Value& a, const Value& b) const {
// This is a bit ugly. Here and below, just note that we want entries return a.second->mRequestedOrder == b.second->mRequestedOrder;
// with the RequestedByChild flag to be sorted before any other entries,
// because we're going to want to decompress them and send them down to
// child processes pretty early during startup.
return a.second->mFlags.contains(
StartupCacheEntryFlags::RequestedByChild) ==
b.second->mFlags.contains(
StartupCacheEntryFlags::RequestedByChild) &&
a.second->mRequestedOrder == b.second->mRequestedOrder;
} }
bool LessThan(const Value& a, const Value& b) const { bool LessThan(const Value& a, const Value& b) const {
bool requestedByChildA = return a.second->mRequestedOrder < b.second->mRequestedOrder;
a.second->mFlags.contains(StartupCacheEntryFlags::RequestedByChild);
bool requestedByChildB =
b.second->mFlags.contains(StartupCacheEntryFlags::RequestedByChild);
if (requestedByChildA == requestedByChildB) {
return a.second->mRequestedOrder < b.second->mRequestedOrder;
} else {
return requestedByChildA;
}
} }
}; };
}; };
@ -240,26 +131,10 @@ class StartupCacheListener final : public nsIObserver {
NS_DECL_NSIOBSERVER NS_DECL_NSIOBSERVER
}; };
// This mirrors a bit of logic in the script preloader. Basically, there's
// certainly some overhead in child processes sending us lists of requested
// startup cache items, so we want to limit that. Accordingly, we only
// request to be notified of requested cache items for the first occurrence
// of each process type, enumerated below.
enum class ProcessType : uint8_t {
Uninitialized,
Parent,
Web,
Extension,
PrivilegedAbout,
};
class StartupCache : public nsIMemoryReporter { class StartupCache : public nsIMemoryReporter {
friend class StartupCacheListener; friend class StartupCacheListener;
friend class StartupCacheChild;
public: public:
using Table = HashMap<nsCString, StartupCacheEntry>;
NS_DECL_THREADSAFE_ISUPPORTS NS_DECL_THREADSAFE_ISUPPORTS
NS_DECL_NSIMEMORYREPORTER NS_DECL_NSIMEMORYREPORTER
@ -272,10 +147,11 @@ class StartupCache : public nsIMemoryReporter {
nsresult GetBuffer(const char* id, const char** outbuf, uint32_t* length); nsresult GetBuffer(const char* id, const char** outbuf, uint32_t* length);
// Stores a buffer. Caller yields ownership. // Stores a buffer. Caller yields ownership.
nsresult PutBuffer(const char* id, UniquePtr<char[]>&& inbuf, uint32_t length, nsresult PutBuffer(const char* id, UniquePtr<char[]>&& inbuf,
bool isFromChildProcess = false); uint32_t length);
void InvalidateCache(); // Removes the cache file.
void InvalidateCache(bool memoryOnly = false);
// For use during shutdown - this will write the startupcache's data // For use during shutdown - this will write the startupcache's data
// to disk if the timer hasn't already gone off. // to disk if the timer hasn't already gone off.
@ -293,35 +169,16 @@ class StartupCache : public nsIMemoryReporter {
nsresult GetDebugObjectOutputStream(nsIObjectOutputStream* aStream, nsresult GetDebugObjectOutputStream(nsIObjectOutputStream* aStream,
nsIObjectOutputStream** outStream); nsIObjectOutputStream** outStream);
static ProcessType GetChildProcessType(const nsAString& remoteType); static StartupCache* GetSingletonNoInit();
static StartupCache* GetSingleton(); static StartupCache* GetSingleton();
// This will get the StartupCache up and running to get cached entries, but
// it won't init some of the deferred things which require later services
// to be up and running.
static nsresult PartialInitSingleton(nsIFile* aProfileLocalDir);
// If the startup cache singleton exists (initialized via
// PartialInitSingleton), this will ensure that all of the ancillary
// requirements of the startup cache are met.
static nsresult FullyInitSingleton();
static nsresult InitChildSingleton(char* aScacheHandleStr,
char* aScacheSizeStr);
static void DeleteSingleton(); static void DeleteSingleton();
static void InitContentChild(dom::ContentParent& parent);
void AddStartupCacheCmdLineArgs(ipc::GeckoChildProcessHost& procHost,
std::vector<std::string>& aExtraOpts);
nsresult ParseStartupCacheCmdLineArgs(char* aScacheHandleStr,
char* aScacheSizeStr);
// This measures all the heap memory used by the StartupCache, i.e. it // This measures all the heap memory used by the StartupCache, i.e. it
// excludes the mapping. // excludes the mapping.
size_t HeapSizeOfIncludingThis(mozilla::MallocSizeOf mallocSizeOf) const; size_t HeapSizeOfIncludingThis(mozilla::MallocSizeOf mallocSizeOf) const;
bool ShouldCompactCache(); bool ShouldCompactCache();
nsresult ResetStartupWriteTimerCheckingReadCount();
nsresult ResetStartupWriteTimer(); nsresult ResetStartupWriteTimer();
bool StartupWriteComplete(); bool StartupWriteComplete();
@ -332,15 +189,7 @@ class StartupCache : public nsIMemoryReporter {
friend class StartupCacheInfo; friend class StartupCacheInfo;
Result<Ok, nsresult> LoadArchive(); Result<Ok, nsresult> LoadArchive();
nsresult PartialInit(nsIFile* aProfileLocalDir); nsresult Init();
nsresult FullyInit();
nsresult InitChild(StartupCacheChild* cacheChild);
// Removes the cache file.
void InvalidateCacheImpl(bool memoryOnly = false);
nsresult ResetStartupWriteTimerCheckingReadCount();
nsresult ResetStartupWriteTimerImpl();
// Returns a file pointer for the cache file with the given name in the // Returns a file pointer for the cache file with the given name in the
// current profile. // current profile.
@ -352,69 +201,37 @@ class StartupCache : public nsIMemoryReporter {
// Writes the cache to disk // Writes the cache to disk
Result<Ok, nsresult> WriteToDisk(); Result<Ok, nsresult> WriteToDisk();
Result<Ok, nsresult> DecompressEntry(StartupCacheEntry& aEntry);
Result<Ok, nsresult> LoadEntriesOffDisk();
Result<Ok, nsresult> LoadEntriesFromSharedMemory();
void WaitOnPrefetchThread(); void WaitOnPrefetchThread();
void StartPrefetchMemoryThread(); void StartPrefetchMemoryThread();
static nsresult InitSingleton();
static void WriteTimeout(nsITimer* aTimer, void* aClosure); static void WriteTimeout(nsITimer* aTimer, void* aClosure);
static void SendEntriesTimeout(nsITimer* aTimer, void* aClosure);
void MaybeWriteOffMainThread(); void MaybeWriteOffMainThread();
static void ThreadedPrefetch(void* aClosure); static void ThreadedPrefetch(void* aClosure);
EnumSet<ProcessType> mInitializedProcesses{}; HashMap<nsCString, StartupCacheEntry> mTable;
nsCString mContentStartupFinishedTopic;
Table mTable;
// owns references to the contents of tables which have been invalidated. // owns references to the contents of tables which have been invalidated.
// In theory grows forever if the cache is continually filled and then // In theory grows forever if the cache is continually filled and then
// invalidated, but this should not happen in practice. // invalidated, but this should not happen in practice.
nsTArray<decltype(mTable)> mOldTables; nsTArray<decltype(mTable)> mOldTables;
nsCOMPtr<nsIFile> mFile; nsCOMPtr<nsIFile> mFile;
loader::AutoMemMap mCacheData; loader::AutoMemMap mCacheData;
loader::AutoMemMap mSharedData; Mutex mTableLock;
UniqueFileHandle mSharedDataHandle;
// This lock must protect a few members of the StartupCache. Essentially,
// we want to protect everything accessed by GetBuffer and PutBuffer. This
// includes:
// - mTable
// - mCacheData
// - mDecompressionContext
// - mCurTableReferenced
// - mOldTables
// - mWrittenOnce
// - gIgnoreDiskCache
// - mFile
// - mWriteTimer
// - mStartupWriteInitiated
mutable Mutex mLock;
nsCOMPtr<nsIObserverService> mObserverService; nsCOMPtr<nsIObserverService> mObserverService;
RefPtr<StartupCacheListener> mListener; RefPtr<StartupCacheListener> mListener;
nsCOMPtr<nsITimer> mWriteTimer; nsCOMPtr<nsITimer> mTimer;
nsCOMPtr<nsITimer> mSendEntriesTimer;
Atomic<bool> mDirty; Atomic<bool> mDirty;
Atomic<bool> mWrittenOnce; Atomic<bool> mWrittenOnce;
bool mCurTableReferenced; bool mCurTableReferenced;
bool mLoaded;
bool mFullyInitialized;
uint32_t mRequestedCount; uint32_t mRequestedCount;
uint32_t mPrefetchSize;
uint32_t mSharedDataSize;
size_t mCacheEntriesBaseOffset; size_t mCacheEntriesBaseOffset;
static StaticRefPtr<StartupCache> gStartupCache; static StaticRefPtr<StartupCache> gStartupCache;
static bool gShutdownInitiated; static bool gShutdownInitiated;
static bool gIgnoreDiskCache; static bool gIgnoreDiskCache;
static bool gFoundDiskCacheOnInit; static bool gFoundDiskCacheOnInit;
Atomic<StartupCacheChild*> mChildActor;
PRThread* mPrefetchThread; PRThread* mPrefetchThread;
UniquePtr<Compression::LZ4FrameDecompressionContext> mDecompressionContext; UniquePtr<Compression::LZ4FrameDecompressionContext> mDecompressionContext;
#ifdef DEBUG #ifdef DEBUG

Просмотреть файл

@ -1,64 +0,0 @@
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* 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 "mozilla/scache/StartupCacheChild.h"
#include "mozilla/scache/StartupCache.h"
#include "mozilla/dom/ContentParent.h"
namespace mozilla {
namespace scache {
void StartupCacheChild::Init(bool wantCacheData) {
mWantCacheData = wantCacheData;
auto* cache = StartupCache::GetSingleton();
if (cache) {
Unused << cache->InitChild(wantCacheData ? this : nullptr);
}
if (!wantCacheData) {
// If the parent process isn't expecting any cache data from us, we're
// done.
Send__delete__(this, AutoTArray<EntryData, 0>());
}
}
void StartupCacheChild::SendEntriesAndFinalize(StartupCache::Table& entries) {
MOZ_RELEASE_ASSERT(mWantCacheData);
nsTArray<EntryData> dataArray;
for (auto iter = entries.iter(); !iter.done(); iter.next()) {
const auto& key = iter.get().key();
auto& value = iter.get().value();
if (!value.mData ||
value.mRequestedOrder == kStartupCacheEntryNotRequested) {
continue;
}
auto data = dataArray.AppendElement();
data->key() = key;
if (value.mFlags.contains(StartupCacheEntryFlags::AddedThisSession)) {
data->data().AppendElements(
reinterpret_cast<const uint8_t*>(value.mData.get()),
value.mUncompressedSize);
}
}
mWantCacheData = false;
Send__delete__(this, dataArray);
}
void StartupCacheChild::ActorDestroy(ActorDestroyReason aWhy) {
auto* cache = StartupCache::GetSingleton();
if (cache) {
cache->mChildActor = nullptr;
}
}
} // namespace scache
} // namespace mozilla

Просмотреть файл

@ -1,43 +0,0 @@
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2; -*- */
/* 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/. */
#ifndef StartupCacheChild_h
#define StartupCacheChild_h
#include "mozilla/scache/StartupCache.h"
#include "mozilla/scache/PStartupCacheChild.h"
#include "mozilla/scache/PStartupCacheParent.h"
namespace mozilla {
namespace ipc {
class FileDescriptor;
}
namespace scache {
using mozilla::ipc::FileDescriptor;
class StartupCacheChild final : public PStartupCacheChild {
friend class mozilla::scache::StartupCache;
friend class mozilla::scache::StartupCacheListener;
public:
StartupCacheChild() = default;
void Init(bool wantCacheData);
protected:
virtual void ActorDestroy(ActorDestroyReason aWhy) override;
void SendEntriesAndFinalize(StartupCache::Table& entries);
private:
bool mWantCacheData = false;
};
} // namespace scache
} // namespace mozilla
#endif // StartupCacheChild_h

Просмотреть файл

@ -1,38 +0,0 @@
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* 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 "mozilla/scache/StartupCacheParent.h"
#include "mozilla/scache/StartupCache.h"
#include "mozilla/dom/ContentParent.h"
namespace mozilla {
namespace scache {
IPCResult StartupCacheParent::Recv__delete__(nsTArray<EntryData>&& entries) {
if (!mWantCacheData && entries.Length()) {
return IPC_FAIL(this, "UnexpectedScriptData");
}
mWantCacheData = false;
if (entries.Length()) {
auto* cache = StartupCache::GetSingleton();
for (auto& entry : entries) {
auto buffer = MakeUnique<char[]>(entry.data().Length());
memcpy(buffer.get(), entry.data().Elements(), entry.data().Length());
cache->PutBuffer(entry.key().get(), std::move(buffer),
entry.data().Length(), /* isFromChildProcess:*/ true);
}
}
return IPC_OK();
}
void StartupCacheParent::ActorDestroy(ActorDestroyReason aWhy) {}
} // namespace scache
} // namespace mozilla

Просмотреть файл

@ -1,37 +0,0 @@
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2; -*- */
/* 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/. */
#ifndef StartupCacheParent_h
#define StartupCacheParent_h
#include "mozilla/scache/StartupCache.h"
#include "mozilla/scache/PStartupCacheParent.h"
namespace mozilla {
namespace scache {
using mozilla::ipc::IPCResult;
class StartupCacheParent final : public PStartupCacheParent {
friend class PStartupCacheParent;
public:
explicit StartupCacheParent(bool wantCacheData)
: mWantCacheData(wantCacheData) {}
protected:
IPCResult Recv__delete__(nsTArray<EntryData>&& entries);
virtual void ActorDestroy(ActorDestroyReason aWhy) override;
private:
bool mWantCacheData;
};
} // namespace scache
} // namespace mozilla
#endif // StartupCacheParent_h

Просмотреть файл

@ -12,16 +12,12 @@ BROWSER_CHROME_MANIFESTS += ['test/browser/browser.ini']
EXPORTS.mozilla.scache += [ EXPORTS.mozilla.scache += [
'StartupCache.h', 'StartupCache.h',
'StartupCacheChild.h',
'StartupCacheParent.h',
'StartupCacheUtils.h', 'StartupCacheUtils.h',
] ]
UNIFIED_SOURCES += [ UNIFIED_SOURCES += [
'StartupCache.cpp', 'StartupCache.cpp',
'StartupCacheChild.cpp',
'StartupCacheInfo.cpp', 'StartupCacheInfo.cpp',
'StartupCacheParent.cpp',
'StartupCacheUtils.cpp', 'StartupCacheUtils.cpp',
] ]
@ -35,10 +31,4 @@ XPIDL_SOURCES += [
'nsIStartupCacheInfo.idl', 'nsIStartupCacheInfo.idl',
] ]
IPDL_SOURCES += [
'PStartupCache.ipdl',
]
include('/ipc/chromium/chromium-config.mozbuild')
FINAL_LIBRARY = 'xul' FINAL_LIBRARY = 'xul'

Просмотреть файл

@ -66,11 +66,6 @@ TestStartupCache::TestStartupCache() {
// We intentionally leak `env` here because it is required by PR_SetEnv // We intentionally leak `env` here because it is required by PR_SetEnv
MOZ_LSAN_INTENTIONALLY_LEAK_OBJECT(env); MOZ_LSAN_INTENTIONALLY_LEAK_OBJECT(env);
#endif #endif
if (!StartupCache::GetSingleton()) {
StartupCache::PartialInitSingleton(nullptr);
StartupCache::FullyInitSingleton();
}
StartupCache::GetSingleton()->InvalidateCache(); StartupCache::GetSingleton()->InvalidateCache();
} }
TestStartupCache::~TestStartupCache() { TestStartupCache::~TestStartupCache() {

Просмотреть файл

@ -4234,10 +4234,7 @@ int XREMain::XRE_mainStartup(bool* aExitFlag) {
mAppData->directory, gSafeMode || !startupCacheValid); mAppData->directory, gSafeMode || !startupCacheValid);
} }
if (!startupCacheValid) { if (!startupCacheValid) StartupCache::IgnoreDiskCache();
StartupCache::IgnoreDiskCache();
}
StartupCache::PartialInitSingleton(mProfLD);
if (flagFile) { if (flagFile) {
flagFile->Remove(true); flagFile->Remove(true);

Просмотреть файл

@ -142,7 +142,7 @@ void AppShutdown::Init(AppShutdownMode aMode) {
// Very early shutdowns can happen before the startup cache is even // Very early shutdowns can happen before the startup cache is even
// initialized; don't bother initializing it during shutdown. // initialized; don't bother initializing it during shutdown.
if (auto* cache = scache::StartupCache::GetSingleton()) { if (auto* cache = scache::StartupCache::GetSingletonNoInit()) {
cache->MaybeInitShutdownWrite(); cache->MaybeInitShutdownWrite();
} }
} }
@ -152,7 +152,7 @@ void AppShutdown::MaybeFastShutdown(ShutdownPhase aPhase) {
// the late write checking code. Anything that writes to disk and which // the late write checking code. Anything that writes to disk and which
// we don't want to skip should be listed out explicitly in this section. // we don't want to skip should be listed out explicitly in this section.
if (aPhase == sFastShutdownPhase || aPhase == sLateWriteChecksPhase) { if (aPhase == sFastShutdownPhase || aPhase == sLateWriteChecksPhase) {
if (auto* cache = scache::StartupCache::GetSingleton()) { if (auto* cache = scache::StartupCache::GetSingletonNoInit()) {
cache->EnsureShutdownWriteComplete(); cache->EnsureShutdownWriteComplete();
} }

Просмотреть файл

@ -7,7 +7,6 @@
#include "FileLocation.h" #include "FileLocation.h"
#include "nsZipArchive.h" #include "nsZipArchive.h"
#include "nsURLHelper.h" #include "nsURLHelper.h"
#include "mozilla/Omnijar.h"
namespace mozilla { namespace mozilla {
@ -17,12 +16,12 @@ FileLocation::~FileLocation() = default;
FileLocation::FileLocation(nsIFile* aFile) { Init(aFile); } FileLocation::FileLocation(nsIFile* aFile) { Init(aFile); }
FileLocation::FileLocation(CacheAwareZipReader* aZip, const char* aPath) { FileLocation::FileLocation(nsIFile* aFile, const char* aPath) {
Init(aZip, aPath); Init(aFile, aPath);
} }
FileLocation::FileLocation(nsZipArchive* aZip, const char* aPath) { FileLocation::FileLocation(nsZipArchive* aZip, const char* aPath) {
Init(new CacheAwareZipReader(aZip, nullptr), aPath); Init(aZip, aPath);
} }
FileLocation::FileLocation(const FileLocation& aOther) FileLocation::FileLocation(const FileLocation& aOther)
@ -89,14 +88,8 @@ void FileLocation::Init(nsIFile* aFile, const char* aPath) {
mPath = aPath; mPath = aPath;
} }
void FileLocation::Init(CacheAwareZipReader* aZip, const char* aPath) {
mBaseZip = aZip;
mBaseFile = nullptr;
mPath = aPath;
}
void FileLocation::Init(nsZipArchive* aZip, const char* aPath) { void FileLocation::Init(nsZipArchive* aZip, const char* aPath) {
mBaseZip = new CacheAwareZipReader(aZip, nullptr); mBaseZip = aZip;
mBaseFile = nullptr; mBaseFile = nullptr;
mPath = aPath; mPath = aPath;
} }
@ -105,7 +98,8 @@ void FileLocation::GetURIString(nsACString& aResult) const {
if (mBaseFile) { if (mBaseFile) {
net_GetURLSpecFromActualFile(mBaseFile, aResult); net_GetURLSpecFromActualFile(mBaseFile, aResult);
} else if (mBaseZip) { } else if (mBaseZip) {
mBaseZip->GetURIString(aResult); RefPtr<nsZipHandle> handler = mBaseZip->GetFD();
handler->mFile.GetURIString(aResult);
} }
if (IsZip()) { if (IsZip()) {
aResult.InsertLiteral("jar:", 0); aResult.InsertLiteral("jar:", 0);
@ -116,7 +110,11 @@ void FileLocation::GetURIString(nsACString& aResult) const {
already_AddRefed<nsIFile> FileLocation::GetBaseFile() { already_AddRefed<nsIFile> FileLocation::GetBaseFile() {
if (IsZip() && mBaseZip) { if (IsZip() && mBaseZip) {
return mBaseZip->GetBaseFile(); RefPtr<nsZipHandle> handler = mBaseZip->GetFD();
if (handler) {
return handler->mFile.GetBaseFile();
}
return nullptr;
} }
nsCOMPtr<nsIFile> file = mBaseFile; nsCOMPtr<nsIFile> file = mBaseFile;
@ -128,17 +126,23 @@ bool FileLocation::Equals(const FileLocation& aFile) const {
return false; return false;
} }
nsCOMPtr<nsIFile> a = mBaseFile; if (mBaseFile && aFile.mBaseFile) {
nsCOMPtr<nsIFile> b = aFile.mBaseFile; bool eq;
if (!mBaseFile && mBaseZip) { return NS_SUCCEEDED(mBaseFile->Equals(aFile.mBaseFile, &eq)) && eq;
a = mBaseZip->GetBaseFile();
}
if (!aFile.mBaseFile && aFile.mBaseZip) {
b = mBaseZip->GetBaseFile();
} }
bool eq; const FileLocation* a = this;
return NS_SUCCEEDED(a->Equals(b, &eq)) && eq; const FileLocation* b = &aFile;
if (a->mBaseZip) {
RefPtr<nsZipHandle> handler = a->mBaseZip->GetFD();
a = &handler->mFile;
}
if (b->mBaseZip) {
RefPtr<nsZipHandle> handler = b->mBaseZip->GetFD();
b = &handler->mFile;
}
return a->Equals(*b);
} }
nsresult FileLocation::GetData(Data& aData) { nsresult FileLocation::GetData(Data& aData) {
@ -147,7 +151,7 @@ nsresult FileLocation::GetData(Data& aData) {
} }
aData.mZip = mBaseZip; aData.mZip = mBaseZip;
if (!aData.mZip) { if (!aData.mZip) {
aData.mZip = new CacheAwareZipReader(); aData.mZip = new nsZipArchive();
aData.mZip->OpenArchive(mBaseFile); aData.mZip->OpenArchive(mBaseFile);
} }
aData.mItem = aData.mZip->GetItem(mPath.get()); aData.mItem = aData.mZip->GetItem(mPath.get());
@ -191,8 +195,8 @@ nsresult FileLocation::Data::Copy(char* aBuf, uint32_t aLen) {
return NS_OK; return NS_OK;
} }
if (mItem) { if (mItem) {
CacheAwareZipCursor cursor(mItem, mZip, reinterpret_cast<uint8_t*>(aBuf), nsZipCursor cursor(mItem, mZip, reinterpret_cast<uint8_t*>(aBuf), aLen,
aLen, true); true);
uint32_t readLen; uint32_t readLen;
cursor.Copy(&readLen); cursor.Copy(&readLen);
if (readLen != aLen) { if (readLen != aLen) {

Просмотреть файл

@ -17,8 +17,6 @@ class nsZipItem;
namespace mozilla { namespace mozilla {
class CacheAwareZipReader;
class FileLocation { class FileLocation {
public: public:
/** /**
@ -29,7 +27,7 @@ class FileLocation {
* - in archives within archives * - in archives within archives
* As such, it stores a path within an archive, as well as the archive * As such, it stores a path within an archive, as well as the archive
* path itself, or the complete file path alone when on a filesystem. * path itself, or the complete file path alone when on a filesystem.
* When the archive is in an archive, an CacheAwareZipReader is stored instead * When the archive is in an archive, an nsZipArchive is stored instead
* of a file path. * of a file path.
*/ */
FileLocation(); FileLocation();
@ -47,14 +45,9 @@ class FileLocation {
/** /**
* Constructors for path within an archive. The archive can be given either * Constructors for path within an archive. The archive can be given either
* as nsIFile or CacheAwareZipReader. * as nsIFile or nsZipArchive.
*/ */
FileLocation(nsIFile* aFile, const char* aPath); FileLocation(nsIFile* aZip, const char* aPath);
/**
* Constructors for path within a zip archive.
*/
FileLocation(CacheAwareZipReader* aZip, const char* aPath);
FileLocation(nsZipArchive* aZip, const char* aPath); FileLocation(nsZipArchive* aZip, const char* aPath);
@ -68,12 +61,10 @@ class FileLocation {
*/ */
void Init(nsIFile* aFile); void Init(nsIFile* aFile);
void Init(nsIFile* aFile, const char* aPath); void Init(nsIFile* aZip, const char* aPath);
void Init(nsZipArchive* aZip, const char* aPath); void Init(nsZipArchive* aZip, const char* aPath);
void Init(CacheAwareZipReader* aZip, const char* aPath);
/** /**
* Returns an URI string corresponding to the file location * Returns an URI string corresponding to the file location
*/ */
@ -87,7 +78,7 @@ class FileLocation {
*/ */
already_AddRefed<nsIFile> GetBaseFile(); already_AddRefed<nsIFile> GetBaseFile();
CacheAwareZipReader* GetBaseZip() { return mBaseZip; } nsZipArchive* GetBaseZip() { return mBaseZip; }
/** /**
* Returns whether the "base file" (see GetBaseFile) is an archive * Returns whether the "base file" (see GetBaseFile) is an archive
@ -128,7 +119,7 @@ class FileLocation {
protected: protected:
friend class FileLocation; friend class FileLocation;
nsZipItem* mItem; nsZipItem* mItem;
RefPtr<CacheAwareZipReader> mZip; RefPtr<nsZipArchive> mZip;
mozilla::AutoFDClose mFd; mozilla::AutoFDClose mFd;
}; };
@ -140,7 +131,7 @@ class FileLocation {
private: private:
nsCOMPtr<nsIFile> mBaseFile; nsCOMPtr<nsIFile> mBaseFile;
RefPtr<CacheAwareZipReader> mBaseZip; RefPtr<nsZipArchive> mBaseZip;
nsCString mPath; nsCString mPath;
}; /* class FileLocation */ }; /* class FileLocation */

Просмотреть файл

@ -1,5 +1,4 @@
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
*/
/* vim: set ts=8 sts=2 et sw=2 tw=80: */ /* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public /* 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 * License, v. 2.0. If a copy of the MPL was not distributed with this
@ -12,28 +11,16 @@
#include "nsIFile.h" #include "nsIFile.h"
#include "nsZipArchive.h" #include "nsZipArchive.h"
#include "nsNetUtil.h" #include "nsNetUtil.h"
#include "mozilla/scache/StartupCache.h"
#include "mozilla/MmapFaultHandler.h"
namespace mozilla { namespace mozilla {
StaticRefPtr<nsIFile> Omnijar::sPath[2]; StaticRefPtr<nsIFile> Omnijar::sPath[2];
StaticRefPtr<CacheAwareZipReader> Omnijar::sReader[2]; StaticRefPtr<nsZipArchive> Omnijar::sReader[2];
StaticRefPtr<CacheAwareZipReader> Omnijar::sOuterReader[2]; StaticRefPtr<nsZipArchive> Omnijar::sOuterReader[2];
bool Omnijar::sInitialized = false; bool Omnijar::sInitialized = false;
bool Omnijar::sIsUnified = false; bool Omnijar::sIsUnified = false;
static MOZ_THREAD_LOCAL(int) tlsSuspendStartupCacheWrites;
bool SuspendingStartupCacheWritesForCurrentThread() {
if (!tlsSuspendStartupCacheWrites.init()) {
return true;
}
return tlsSuspendStartupCacheWrites.get() > 0;
}
static const char* sProp[2] = {NS_GRE_DIR, NS_XPCOM_CURRENT_PROCESS_DIR}; static const char* sProp[2] = {NS_GRE_DIR, NS_XPCOM_CURRENT_PROCESS_DIR};
static const char* sCachePrefixes[2] = {"GreOmnijar:", "AppOmnijar:"};
#define SPROP(Type) ((Type == mozilla::Omnijar::GRE) ? sProp[GRE] : sProp[APP]) #define SPROP(Type) ((Type == mozilla::Omnijar::GRE) ? sProp[GRE] : sProp[APP])
@ -91,41 +78,8 @@ void Omnijar::InitOne(nsIFile* aPath, Type aType) {
} }
RefPtr<nsZipArchive> zipReader = new nsZipArchive(); RefPtr<nsZipArchive> zipReader = new nsZipArchive();
auto* cache = scache::StartupCache::GetSingleton(); if (NS_FAILED(zipReader->OpenArchive(file))) {
const uint8_t* centralBuf = nullptr; return;
uint32_t centralBufLength = 0;
nsCString startupCacheKey =
nsPrintfCString("::%s:OmnijarCentral", sCachePrefixes[aType]);
if (cache) {
nsresult rv = cache->GetBuffer(startupCacheKey.get(),
reinterpret_cast<const char**>(&centralBuf),
&centralBufLength);
if (NS_FAILED(rv)) {
centralBuf = nullptr;
centralBufLength = 0;
}
}
if (!centralBuf) {
if (NS_FAILED(zipReader->OpenArchive(file))) {
return;
}
if (cache) {
size_t bufSize;
// Annoyingly, nsZipArchive and the startupcache use different types to
// represent bytes (uint8_t vs char), so we have to do a little dance to
// convert the UniquePtr over.
UniquePtr<char[]> centralBuf(reinterpret_cast<char*>(
zipReader->CopyCentralDirectoryBuffer(&bufSize).release()));
if (centralBuf) {
cache->PutBuffer(startupCacheKey.get(), std::move(centralBuf), bufSize);
}
}
} else {
if (NS_FAILED(zipReader->LazyOpenArchive(
file, MakeSpan(centralBuf, centralBufLength)))) {
return;
}
} }
RefPtr<nsZipArchive> outerReader; RefPtr<nsZipArchive> outerReader;
@ -140,9 +94,8 @@ void Omnijar::InitOne(nsIFile* aPath, Type aType) {
} }
CleanUpOne(aType); CleanUpOne(aType);
sReader[aType] = new CacheAwareZipReader(zipReader, sCachePrefixes[aType]); sReader[aType] = zipReader;
sOuterReader[aType] = sOuterReader[aType] = outerReader;
outerReader ? new CacheAwareZipReader(outerReader, nullptr) : nullptr;
sPath[aType] = file; sPath[aType] = file;
} }
@ -158,7 +111,7 @@ void Omnijar::CleanUp() {
sInitialized = false; sInitialized = false;
} }
already_AddRefed<CacheAwareZipReader> Omnijar::GetReader(nsIFile* aPath) { already_AddRefed<nsZipArchive> Omnijar::GetReader(nsIFile* aPath) {
MOZ_ASSERT(IsInitialized(), "Omnijar not initialized"); MOZ_ASSERT(IsInitialized(), "Omnijar not initialized");
bool equals; bool equals;
@ -179,7 +132,7 @@ already_AddRefed<CacheAwareZipReader> Omnijar::GetReader(nsIFile* aPath) {
return nullptr; return nullptr;
} }
already_AddRefed<CacheAwareZipReader> Omnijar::GetInnerReader( already_AddRefed<nsZipArchive> Omnijar::GetInnerReader(
nsIFile* aPath, const nsACString& aEntry) { nsIFile* aPath, const nsACString& aEntry) {
MOZ_ASSERT(IsInitialized(), "Omnijar not initialized"); MOZ_ASSERT(IsInitialized(), "Omnijar not initialized");
@ -244,226 +197,4 @@ nsresult Omnijar::GetURIString(Type aType, nsACString& aResult) {
return NS_OK; return NS_OK;
} }
CacheAwareZipReader::CacheAwareZipReader(nsZipArchive* aZip,
const char* aCacheKeyPrefix)
: mZip(aZip), mCacheKeyPrefix(aCacheKeyPrefix) {}
nsresult CacheAwareZipReader::FindInit(const char* aPattern,
nsZipFind** aFind) {
return mZip->FindInit(aPattern, aFind);
}
const uint8_t* CacheAwareZipReader::GetData(
nsZipItem* aItem, CacheAwareZipReader::Caching aCaching) {
nsAutoCString cacheKey;
uint32_t size;
const uint8_t* cached =
GetCachedBuffer(aItem->Name(), aItem->nameLength, &size, cacheKey);
if (cached) {
MOZ_ASSERT(size == aItem->RealSize());
return cached;
}
const uint8_t* zipItemData = mZip->GetData(aItem);
// If the data is compressed, it is somewhat silly to store it in the startup
// cache, as the startup cache will try to double compress it.
if (aCaching == Default && aItem->Compression() == STORED &&
!cacheKey.IsEmpty()) {
MOZ_ASSERT(aItem->RealSize() == aItem->Size());
PutBufferIntoCache(cacheKey, zipItemData, aItem->Size());
}
return zipItemData;
}
const uint8_t* CacheAwareZipReader::GetData(
const char* aEntryName, uint32_t* aResultSize,
CacheAwareZipReader::Caching aCaching) {
nsAutoCString cacheKey;
const uint8_t* cached =
GetCachedBuffer(aEntryName, strlen(aEntryName), aResultSize, cacheKey);
if (cached) {
return cached;
}
nsZipItem* zipItem = mZip->GetItem(aEntryName);
if (!zipItem) {
*aResultSize = 0;
return nullptr;
}
const uint8_t* zipItemData = mZip->GetData(zipItem);
*aResultSize = zipItem->Size();
// If the data is compressed, it is somewhat silly to store it in the startup
// cache, as the startup cache will try to double compress it.
if (aCaching == Default && zipItem->Compression() == STORED &&
!cacheKey.IsEmpty()) {
MOZ_ASSERT(zipItem->RealSize() == *aResultSize);
PutBufferIntoCache(cacheKey, zipItemData, *aResultSize);
}
return zipItemData;
}
nsZipItem* CacheAwareZipReader::GetItem(const char* aEntryName) {
return mZip->GetItem(aEntryName);
}
nsresult CacheAwareZipReader::CloseArchive() { return mZip->CloseArchive(); }
CacheAwareZipCursor::CacheAwareZipCursor(nsZipItem* aItem,
CacheAwareZipReader* aReader,
uint8_t* aBuf, uint32_t aBufSize,
bool aDoCRC)
: mItem(aItem),
mReader(aReader),
mBuf(aBuf),
mBufSize(aBufSize),
mDoCRC(aDoCRC) {}
uint8_t* CacheAwareZipCursor::ReadOrCopy(uint32_t* aBytesRead, bool aCopy) {
nsCString cacheKey;
const uint8_t* cached = mReader->GetCachedBuffer(
mItem->Name(), mItem->nameLength, aBytesRead, cacheKey);
if (cached && *aBytesRead <= mBufSize) {
if (aCopy) {
memcpy(mBuf, cached, *aBytesRead);
return mBuf;
}
// The const cast is unfortunate, but it matches existing consumers'
// uses. We ought to file a bug to make Read return a const uint8_t*
return const_cast<uint8_t*>(cached);
}
nsZipCursor cursor(mItem, mReader->mZip, mBuf, mBufSize, mDoCRC);
uint8_t* buf = nullptr;
if (aCopy) {
cursor.Copy(aBytesRead);
buf = mBuf;
} else {
buf = cursor.Read(aBytesRead);
}
if (!cacheKey.IsEmpty() && *aBytesRead == mItem->RealSize()) {
CacheAwareZipReader::PutBufferIntoCache(cacheKey, buf, *aBytesRead);
}
return buf;
}
nsresult CacheAwareZipReader::GetPersistentHandle(
nsZipItem* aItem, CacheAwareZipHandle* aHandle,
CacheAwareZipReader::Caching aCaching) {
nsCString cacheKey;
if (!mCacheKeyPrefix.IsEmpty() && aItem->Compression() == STORED) {
auto* cache = scache::StartupCache::GetSingleton();
if (cache) {
cacheKey.Append(mCacheKeyPrefix);
cacheKey.Append(aItem->Name(), aItem->nameLength);
if (cache->HasEntry(cacheKey.get())) {
aHandle->mDataIsCached = true;
aHandle->mFd = nullptr;
return NS_OK;
}
if (aCaching == DeferCaching) {
aHandle->mDeferredCachingKey = std::move(cacheKey);
}
}
}
nsresult rv = mZip->EnsureArchiveOpenedOnDisk();
if (NS_FAILED(rv)) {
return rv;
}
aHandle->mDataIsCached = false;
aHandle->mFd = mZip->GetFD();
if (!aHandle->mDeferredCachingKey.IsEmpty() &&
aItem->Compression() == STORED) {
MOZ_ASSERT(aItem->RealSize() == aItem->Size());
aHandle->mDataToCache = MakeSpan(mZip->GetData(aItem), aItem->Size());
}
return NS_OK;
}
const uint8_t* CacheAwareZipReader::GetCachedBuffer(const char* aEntryName,
uint32_t aEntryNameLength,
uint32_t* aResultSize,
nsCString& aCacheKey) {
*aResultSize = 0;
if (mCacheKeyPrefix.IsEmpty()) {
return nullptr;
}
auto* cache = scache::StartupCache::GetSingleton();
if (!cache) {
return nullptr;
}
aCacheKey.Append(mCacheKeyPrefix);
aCacheKey.Append(aEntryName, aEntryNameLength);
const char* cached;
nsresult rv = cache->GetBuffer(aCacheKey.get(), &cached, aResultSize);
if (NS_FAILED(rv)) {
return nullptr;
}
return reinterpret_cast<const uint8_t*>(cached);
}
void CacheAwareZipReader::PutBufferIntoCache(const nsCString& aCacheKey,
const uint8_t* aBuffer,
uint32_t aSize) {
if (SuspendingStartupCacheWritesForCurrentThread() || aSize == 0) {
return;
}
auto* cache = scache::StartupCache::GetSingleton();
auto dataCopy = MakeUnique<char[]>(aSize);
MMAP_FAULT_HANDLER_BEGIN_BUFFER(aBuffer, aSize)
memcpy(dataCopy.get(), aBuffer, aSize);
MMAP_FAULT_HANDLER_CATCH()
Unused << cache->PutBuffer(aCacheKey.get(), std::move(dataCopy), aSize);
}
void CacheAwareZipReader::PushSuspendStartupCacheWrites() {
if (!tlsSuspendStartupCacheWrites.init()) {
return;
}
tlsSuspendStartupCacheWrites.set(tlsSuspendStartupCacheWrites.get() + 1);
}
void CacheAwareZipReader::PopSuspendStartupCacheWrites() {
if (!tlsSuspendStartupCacheWrites.init()) {
return;
}
int current = tlsSuspendStartupCacheWrites.get();
MOZ_ASSERT(current > 0);
tlsSuspendStartupCacheWrites.set(current - 1);
}
void CacheAwareZipHandle::ReleaseHandle() {
if (!mDataToCache.IsEmpty()) {
MOZ_ASSERT(mFd);
MOZ_ASSERT(!mDeferredCachingKey.IsEmpty());
MOZ_ASSERT(!mDataIsCached);
auto* cache = scache::StartupCache::GetSingleton();
MOZ_ASSERT(cache);
if (cache) {
CacheAwareZipReader::PutBufferIntoCache(
mDeferredCachingKey, mDataToCache.Elements(), mDataToCache.Length());
mDataToCache = Span<const uint8_t>();
}
}
mFd = nullptr;
}
} /* namespace mozilla */ } /* namespace mozilla */

Просмотреть файл

@ -13,14 +13,10 @@
#include "nsIFile.h" #include "nsIFile.h"
#include "nsZipArchive.h" #include "nsZipArchive.h"
#include "mozilla/Span.h"
#include "mozilla/StaticPtr.h" #include "mozilla/StaticPtr.h"
#include "mozilla/UniquePtr.h"
namespace mozilla { namespace mozilla {
class CacheAwareZipReader;
class Omnijar { class Omnijar {
private: private:
/** /**
@ -32,15 +28,15 @@ class Omnijar {
static StaticRefPtr<nsIFile> sPath[2]; static StaticRefPtr<nsIFile> sPath[2];
/** /**
* Cached CacheAwareZipReaders for the corresponding sPath * Cached nsZipArchives for the corresponding sPath
*/ */
static StaticRefPtr<CacheAwareZipReader> sReader[2]; static StaticRefPtr<nsZipArchive> sReader[2];
/** /**
* Cached CacheAwareZipReaders for the outer jar, when using nested jars. * Cached nsZipArchives for the outer jar, when using nested jars.
* Otherwise nullptr. * Otherwise nullptr.
*/ */
static StaticRefPtr<CacheAwareZipReader> sOuterReader[2]; static StaticRefPtr<nsZipArchive> sOuterReader[2];
/** /**
* Has Omnijar::Init() been called? * Has Omnijar::Init() been called?
@ -65,14 +61,13 @@ class Omnijar {
} }
/** /**
* Returns an CacheAwareZipReader pointer for the outer jar file when using * Returns a nsZipArchive pointer for the outer jar file when using nested
* nested jars. Returns nullptr in the same cases GetPath() would, or if not * jars. Returns nullptr in the same cases GetPath() would, or if not using
* using nested jars. * nested jars.
*/ */
static inline already_AddRefed<CacheAwareZipReader> GetOuterReader( static inline already_AddRefed<nsZipArchive> GetOuterReader(Type aType) {
Type aType) {
MOZ_ASSERT(IsInitialized(), "Omnijar not initialized"); MOZ_ASSERT(IsInitialized(), "Omnijar not initialized");
RefPtr<CacheAwareZipReader> reader = sOuterReader[aType].get(); RefPtr<nsZipArchive> reader = sOuterReader[aType].get();
return reader.forget(); return reader.forget();
} }
@ -118,20 +113,20 @@ class Omnijar {
} }
/** /**
* Returns an CacheAwareZipReader pointer for the omni.jar file for GRE or * Returns a nsZipArchive pointer for the omni.jar file for GRE or
* APP. Returns nullptr in the same cases GetPath() would. * APP. Returns nullptr in the same cases GetPath() would.
*/ */
static inline already_AddRefed<CacheAwareZipReader> GetReader(Type aType) { static inline already_AddRefed<nsZipArchive> GetReader(Type aType) {
MOZ_ASSERT(IsInitialized(), "Omnijar not initialized"); MOZ_ASSERT(IsInitialized(), "Omnijar not initialized");
RefPtr<CacheAwareZipReader> reader = sReader[aType].get(); RefPtr<nsZipArchive> reader = sReader[aType].get();
return reader.forget(); return reader.forget();
} }
/** /**
* Returns an CacheAwareZipReader pointer for the given path IAOI the given * Returns a nsZipArchive pointer for the given path IAOI the given
* path is the omni.jar for either GRE or APP. * path is the omni.jar for either GRE or APP.
*/ */
static already_AddRefed<CacheAwareZipReader> GetReader(nsIFile* aPath); static already_AddRefed<nsZipArchive> GetReader(nsIFile* aPath);
/** /**
* In the case of a nested omnijar, this returns the inner reader for the * In the case of a nested omnijar, this returns the inner reader for the
@ -141,7 +136,7 @@ class Omnijar {
* GetReader("path/to.apk") returns the outer reader and GetInnerReader( * GetReader("path/to.apk") returns the outer reader and GetInnerReader(
* "path/to.apk", "assets/omni.ja") returns the inner reader. * "path/to.apk", "assets/omni.ja") returns the inner reader.
*/ */
static already_AddRefed<CacheAwareZipReader> GetInnerReader( static already_AddRefed<nsZipArchive> GetInnerReader(
nsIFile* aPath, const nsACString& aEntry); nsIFile* aPath, const nsACString& aEntry);
/** /**
@ -161,132 +156,6 @@ class Omnijar {
static void CleanUpOne(Type aType); static void CleanUpOne(Type aType);
}; /* class Omnijar */ }; /* class Omnijar */
class CacheAwareZipCursor {
public:
CacheAwareZipCursor(nsZipItem* aItem, CacheAwareZipReader* aReader,
uint8_t* aBuf = nullptr, uint32_t aBufSize = 0,
bool aDoCRC = false);
uint8_t* Read(uint32_t* aBytesRead) { return ReadOrCopy(aBytesRead, false); }
uint8_t* Copy(uint32_t* aBytesRead) { return ReadOrCopy(aBytesRead, true); }
private:
/* Actual implementation for both Read and Copy above */
uint8_t* ReadOrCopy(uint32_t* aBytesRead, bool aCopy);
nsZipItem* mItem;
CacheAwareZipReader* mReader;
uint8_t* mBuf;
uint32_t mBufSize;
bool mDoCRC;
};
// This class wraps an nsZipHandle, which may be null, if the data is
// cached
class CacheAwareZipHandle {
friend class CacheAwareZipReader;
public:
CacheAwareZipHandle() : mFd(nullptr), mDataIsCached(false) {}
~CacheAwareZipHandle() { ReleaseHandle(); }
nsZipHandle* UnderlyingFD() { return mFd; }
void ReleaseHandle();
explicit operator bool() const { return mDataIsCached || mFd; }
private:
RefPtr<nsZipHandle> mFd;
nsCString mDeferredCachingKey;
Span<const uint8_t> mDataToCache;
bool mDataIsCached;
};
class CacheAwareZipReader {
friend class CacheAwareZipCursor;
friend class CacheAwareZipHandle;
public:
NS_INLINE_DECL_THREADSAFE_REFCOUNTING(CacheAwareZipReader)
enum Caching {
Default,
DeferCaching,
};
// Constructor for CacheAwareZipReader. aCacheKeyPrefix will be a prefix
// which the wrapper will prepend to any requested entries prior to
// requesting the entry from the StartupCache. However, if aCacheKeyPrefix
// is null, we will simply pass through to the underlying zip archive
// without caching.
explicit CacheAwareZipReader(nsZipArchive* aZip, const char* aCacheKeyPrefix);
// The default constructor should be used for an nsZipArchive which doesn't
// want to cache any entries. Consumers will need to call `OpenArchive`
// explicitly after constructing.
CacheAwareZipReader() : mZip(new nsZipArchive()) {}
nsresult OpenArchive(nsIFile* aFile) { return mZip->OpenArchive(aFile); }
nsresult OpenArchive(nsZipHandle* aHandle) {
return mZip->OpenArchive(aHandle);
}
const uint8_t* GetData(const char* aEntryName, uint32_t* aResultSize,
Caching aCaching = Default);
const uint8_t* GetData(nsZipItem* aItem, Caching aCaching = Default);
nsresult GetPersistentHandle(nsZipItem* aItem, CacheAwareZipHandle* aHandle,
Caching aCaching);
already_AddRefed<nsIFile> GetBaseFile() { return mZip->GetBaseFile(); }
void GetURIString(nsACString& result) { mZip->GetURIString(result); }
nsZipArchive* GetZipArchive() { return mZip; }
nsresult FindInit(const char* aPattern, nsZipFind** aFind);
bool IsForZip(nsZipArchive* aArchive) { return aArchive == mZip; }
nsZipItem* GetItem(const char* aEntryName);
nsresult CloseArchive();
nsresult Test(const char* aEntryName) { return mZip->Test(aEntryName); }
nsresult ExtractFile(nsZipItem* zipEntry, nsIFile* outFile,
PRFileDesc* outFD) {
return mZip->ExtractFile(zipEntry, outFile, outFD);
}
static void PushSuspendStartupCacheWrites();
static void PopSuspendStartupCacheWrites();
protected:
~CacheAwareZipReader() = default;
private:
const uint8_t* GetCachedBuffer(const char* aEntryName,
uint32_t aEntryNameLength,
uint32_t* aResultSize, nsCString& aCacheKey);
static void PutBufferIntoCache(const nsCString& aCacheKey,
const uint8_t* aBuffer, uint32_t aSize);
RefPtr<nsZipArchive> mZip;
nsCString mCacheKeyPrefix;
};
class MOZ_RAII AutoSuspendStartupCacheWrites {
public:
AutoSuspendStartupCacheWrites() {
CacheAwareZipReader::PushSuspendStartupCacheWrites();
}
~AutoSuspendStartupCacheWrites() {
CacheAwareZipReader::PopSuspendStartupCacheWrites();
}
};
} /* namespace mozilla */ } /* namespace mozilla */
#endif /* mozilla_Omnijar_h */ #endif /* mozilla_Omnijar_h */

Просмотреть файл

@ -462,7 +462,7 @@ NS_InitXPCOM(nsIServiceManager** aResult, nsIFile* aBinDirectory,
// Init mozilla::SharedThreadPool (which needs the service manager). // Init mozilla::SharedThreadPool (which needs the service manager).
mozilla::SharedThreadPool::InitStatics(); mozilla::SharedThreadPool::InitStatics();
mozilla::scache::StartupCache::FullyInitSingleton(); mozilla::scache::StartupCache::GetSingleton();
mozilla::AvailableMemoryTracker::Init(); mozilla::AvailableMemoryTracker::Init();
// Notify observers of xpcom autoregistration start // Notify observers of xpcom autoregistration start

Просмотреть файл

@ -491,7 +491,7 @@ nsresult nsComponentManagerImpl::Init() {
InitializeModuleLocations(); InitializeModuleLocations();
ComponentLocation* cl = sModuleLocations->AppendElement(); ComponentLocation* cl = sModuleLocations->AppendElement();
cl->type = NS_APP_LOCATION; cl->type = NS_APP_LOCATION;
RefPtr<CacheAwareZipReader> greOmnijar = RefPtr<nsZipArchive> greOmnijar =
mozilla::Omnijar::GetReader(mozilla::Omnijar::GRE); mozilla::Omnijar::GetReader(mozilla::Omnijar::GRE);
if (greOmnijar) { if (greOmnijar) {
cl->location.Init(greOmnijar, "chrome.manifest"); cl->location.Init(greOmnijar, "chrome.manifest");
@ -500,7 +500,7 @@ nsresult nsComponentManagerImpl::Init() {
cl->location.Init(lf); cl->location.Init(lf);
} }
RefPtr<CacheAwareZipReader> appOmnijar = RefPtr<nsZipArchive> appOmnijar =
mozilla::Omnijar::GetReader(mozilla::Omnijar::APP); mozilla::Omnijar::GetReader(mozilla::Omnijar::APP);
if (appOmnijar) { if (appOmnijar) {
cl = sModuleLocations->AppendElement(); cl = sModuleLocations->AppendElement();