Bug 1627075 - Init StartupCache before Omnijar r=froydnj

We need to be able to init StartupCache before the Omnijar in order to cache
all of the Omnijar contents we access. This patch implements that.

Depends on D77632

Differential Revision: https://phabricator.services.mozilla.com/D77633
This commit is contained in:
Doug Thayer 2020-07-07 04:34:07 +00:00
Родитель 5b755bb7f3
Коммит 7dcd89a08d
4 изменённых файлов: 63 добавлений и 35 удалений

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

@ -114,31 +114,35 @@ StartupCache* StartupCache::GetSingletonNoInit() {
}
StartupCache* StartupCache::GetSingleton() {
if (!gStartupCache) {
if (!XRE_IsParentProcess()) {
return nullptr;
}
#ifdef MOZ_DISABLE_STARTUPCACHE
return nullptr;
#else
StartupCache::InitSingleton();
#endif
}
return StartupCache::gStartupCache;
}
void StartupCache::DeleteSingleton() { StartupCache::gStartupCache = nullptr; }
nsresult StartupCache::InitSingleton() {
nsresult StartupCache::PartialInitSingleton(nsIFile* aProfileLocalDir) {
#ifdef MOZ_DISABLE_STARTUPCACHE
return NS_OK;
#else
if (!XRE_IsParentProcess()) {
return NS_OK;
}
nsresult rv;
StartupCache::gStartupCache = new StartupCache();
rv = StartupCache::gStartupCache->Init();
rv = StartupCache::gStartupCache->PartialInit(aProfileLocalDir);
if (NS_FAILED(rv)) {
StartupCache::gStartupCache = nullptr;
}
return rv;
#endif
}
nsresult StartupCache::FullyInitSingleton() {
if (!StartupCache::gStartupCache) {
return NS_OK;
}
return StartupCache::gStartupCache->FullyInit();
}
StaticRefPtr<StartupCache> StartupCache::gStartupCache;
@ -159,7 +163,7 @@ StartupCache::StartupCache()
StartupCache::~StartupCache() { UnregisterWeakMemoryReporter(this); }
nsresult StartupCache::Init() {
nsresult StartupCache::PartialInit(nsIFile* aProfileLocalDir) {
// workaround for bug 653936
nsCOMPtr<nsIProtocolHandler> jarInitializer(
do_GetService(NS_NETWORK_PROTOCOL_CONTRACTID_PREFIX "jar"));
@ -177,9 +181,9 @@ nsresult StartupCache::Init() {
if (env && *env) {
rv = NS_NewLocalFile(NS_ConvertUTF8toUTF16(env), false,
getter_AddRefs(mFile));
} else {
} else if (aProfileLocalDir) {
nsCOMPtr<nsIFile> file;
rv = NS_GetSpecialDirectory("ProfLDS", getter_AddRefs(file));
rv = aProfileLocalDir->Clone(getter_AddRefs(file));
if (NS_FAILED(rv)) {
// return silently, this will fail in mochitests's xpcshell process.
return rv;
@ -197,25 +201,12 @@ nsresult StartupCache::Init() {
NS_ENSURE_SUCCESS(rv, rv);
mFile = file;
} else {
return NS_ERROR_INVALID_ARG;
}
NS_ENSURE_TRUE(mFile, NS_ERROR_UNEXPECTED);
mObserverService = do_GetService("@mozilla.org/observer-service;1");
if (!mObserverService) {
NS_WARNING("Could not get observerService.");
return NS_ERROR_UNEXPECTED;
}
mListener = new StartupCacheListener();
rv = mObserverService->AddObserver(mListener, NS_XPCOM_SHUTDOWN_OBSERVER_ID,
false);
NS_ENSURE_SUCCESS(rv, rv);
rv = mObserverService->AddObserver(mListener, "startupcache-invalidate",
false);
NS_ENSURE_SUCCESS(rv, rv);
auto result = LoadArchive();
rv = result.isErr() ? result.unwrapErr() : NS_OK;
@ -234,6 +225,29 @@ nsresult StartupCache::Init() {
return NS_OK;
}
nsresult StartupCache::FullyInit() {
mObserverService = do_GetService("@mozilla.org/observer-service;1");
if (!mObserverService) {
NS_WARNING("Could not get observerService.");
return NS_ERROR_UNEXPECTED;
}
mListener = new StartupCacheListener();
nsresult rv = mObserverService->AddObserver(
mListener, NS_XPCOM_SHUTDOWN_OBSERVER_ID, false);
if (NS_WARN_IF(NS_FAILED(rv))) {
return rv;
}
rv = mObserverService->AddObserver(mListener, "startupcache-invalidate",
false);
if (NS_WARN_IF(NS_FAILED(rv))) {
return rv;
}
return NS_OK;
}
void StartupCache::StartPrefetchMemoryThread() {
// XXX: It would be great for this to not create its own thread, unfortunately
// there doesn't seem to be an existing thread that makes sense for this, so

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

@ -171,6 +171,17 @@ class StartupCache : public nsIMemoryReporter {
static StartupCache* GetSingletonNoInit();
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 void DeleteSingleton();
// This measures all the heap memory used by the StartupCache, i.e. it
@ -189,7 +200,8 @@ class StartupCache : public nsIMemoryReporter {
friend class StartupCacheInfo;
Result<Ok, nsresult> LoadArchive();
nsresult Init();
nsresult PartialInit(nsIFile* aProfileLocalDir);
nsresult FullyInit();
// Returns a file pointer for the cache file with the given name in the
// current profile.
@ -204,7 +216,6 @@ class StartupCache : public nsIMemoryReporter {
void WaitOnPrefetchThread();
void StartPrefetchMemoryThread();
static nsresult InitSingleton();
static void WriteTimeout(nsITimer* aTimer, void* aClosure);
void MaybeWriteOffMainThread();
static void ThreadedPrefetch(void* aClosure);

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

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

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

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