Files
palemoon27/startupcache/StartupCache.cpp
T
roytam1 8a0a002cf2 import changes from `dev' branch of rmottola/Arctic-Fox:
- Bug 1148708: Add missing 'override' annotations in DocAccessibleChild.h. rs=ehsan (d606358545)
- Bug 1210408 - make nsMaiInterfaceAction work with proxies, r=tbsaunde (f7c819c6ae)
- Bug 1210407 - teach nsMaiInterfaceTable to use proxies, r=tbsaunde (4ca4f10b5f)
- bug 1185157 make sure we don't send an event to a destroyed ipc document r=billm (23acf53f75)
- bug 1214864 - make SetCarretOffset() async r=davidb (e3079e9b2d)
- missing of Bug 1139972 - IPC Proxy for charAt, r=tbsaunde (e9593ed752)
- bug 1191598 - Pass MOZ_CURRENT_PROJECT in environment when running post-build automation steps for universal mac builds. r=gps (fc342c6ced)
- Bug 1164596 - Add mach android-emulator command; r=ahal (afeb9b27d1)
- Bug 1223149 - Add basic usage documentation for mach build; r=glandium (bfb802d175)
- Bug 1182301 - Improve 'mach build' notifications. r=gps (2c65a122d1)
- Bug 1184696 - Add clobber targets to |mach clobber|; Ability to clobber compiled python files, r=gps (35d8be292e)
- Bug 1117958 - Allow any debugging options to the run or gtest mach subcommands to automatically enable debugging. r=gps (32f986af4b)
- Bug 1180081 - Properly rebuild gtest/libxul before running gtests. r=gps (80db9a3d49)
- Bug 1171647, part 1 - Define a new function to convert the mode to a string. r=njn (61ad16f5ba)
- Bug 1171647, part 2 - Remove redundant assertion for dark matter mode. r=njn (b5ac9519f3)
- Bug 1058178, part 1 - Implement DMD heap scanning mode. r=njn (60e1079536)
- Bug 1058178, part 2 - Implement address clamping analysis for DMD scan logs. r=njn (45c0326b93)
- Bug 1102388 - Fix DMD static constructor ordering dependency. r=mccr8 (59b87897a1)
- Bug 1128705 - Don't redefine PAGE_SIZE in DMD if it's already defined. r=erahm (49216348ee)
- Bug 1179042 - Add a script for analyzing memory blocks using a heap scan DMD log. r=njn DONTBUILD (1c08d2d66e)
- Bug 1207245 - part 6 - rename nsRefPtr<T> to RefPtr<T>; r=ehsan; a=Tomcat (1c999d139b)
- Bug 1158772 - fix non-idiomatic memset call in nsDeque.cpp; r=erahm (de6b555245)
- Bug 1199400 - Part 1: Use CheckedInt when growing nsDeque capacity. r=froydnj (dfdf6814a3)
- Bug 1199400 - Part 0: Remove unused nsDequeIterator. r=froydnj (38d69d7f47)
- Bug 1199400 - Part 2: Add tests for possible nsDeque corner cases. r=froydnj (931baff195)
- Bug 1201997 - Part 1 - Converted compiled test to gtest for nsDeque class. Added tests to test untested methods. r=froydn (e893916651)
- Bug 1201997 - Part 2 - Removing unused methods from the nsDeque class. r=froydn (41595a90ac)
- Bug 1201997 - Part 3 - Make internally used methods private. r=froydn (8cd3afd96f)
- Bug 1201997 - Part 4 - Change size and offset variables to size_t.r=froydn (73eabc8d60)
- Bug 1215140 P1 Add an nsIConsoleReportCollector interface to support navigation channel logging. r=bz (8a41535e2b)
- Bug 1215140 P2 Make HttpBaseChannel implement nsIConsoleReportCollector. r=bz (75fca301f2)
- Bug 1197679 - If nsUnknownDecoder is involved in e10s DivertToParent can break. r=jduell (5d94a12504)
- Bug 1178991 - smartptr for http converter r=hurley (8e7fbc8443)
- bug 366559 - patch 2, fix nsHTTPCompressConv indentation r=bagder (ba762da587)
- bug 366559 - patch 3, fix nsHTTPCompressConv bracing style r=bagder (54195ab451)
- bits of  bug 366559 - patch 7, content-encoding brotli for http (f0b4051022)
- Bug 1205112 - Make PushEvent.data nullable. r=mt,smaug (775db32856)
- Bug 1193414 - SharedWorkers thread should be kept alive also when the SharedWorker object is CCed, r=khuey (b77ea8125c)
- Bug 1206520: Add about:config prefs to enable throwing on asm.js validation failures; r=bz (c42126665d)
- Bug 1193414 - Telemetry for SharedWorker spawning. r=bkelly (77984b7bcc)
- Bug 1205676 - Enable WPT service-worker/unregister-then-register-new-script.https.html in e10s, r=nsm (ec24939cf6)
- Bug 1193133 - Throw when calling postMessage from a Service Worker dom object with no global. r=bkelly (526dcacfab)
- Bug 1181871 P1 Only enforce Cache Context shared data destruction on target thread after init. r=ehsan (cdbf3ed3a8)
- Bug 1181871 P2 Fix ServiceWorkerManager usage of stack-based ErrorResult. r=ehsan (c449195d90)
- minor cleanup and missing bit of 1198230 (02f459db05)
- Bug 1143717 - Implement the ServiceWorkerMessageEvent interface. r=baku (027b3465f2)
- fix misspatch (708eee4e84)
- Bug 1188545 - Disentangle service workers from shared workers and refactor event dispatching code into a separate class. r=nsm,mrbkap (fb5b5341c9)
- Bug 1205228 - Change PackagedAppVerifier to notify the verification result asynchronously. r=valentin. (9edda0fa00)
- Bug 1178518 - Packaged App Utils. r=valentin (f60f3b7a93)
- Bug 1213150 - Part 1: Add a nsContentUtils::IsNonSubresourceRequest helper; r=jdm (b509cc3cc9)
- Bug 1213150 - Part 2: Rework ShouldPrepareForIntercept() in terms of subresource requests; r=jdm (2e92fe8780)
- Bug 1213150 - Part 3: Remove nsIInterceptedChannel.isNavigation; r=jdm (becf1cc12f)
- Bug 1213150 follow-up: fix build bustage (8d73d6ca73)
- Bug 1198394 - Part 1: Allow interception of HSTS upgraded connections in non-e10s mode; r=mcmanus (f504c5be08)
- Bug 1198394 - Part 2: Add a test for interception of HSTS upgraded connections; r=jdm (054e984eef)
- Bug 1187011 - Don't allow response body with null body status. r=bkelly (b1860741d1)
- missing bit of 1140788 (29d319712e)
- Bug 1213436 - Reject core dumps with node IDs that don't fit in an IEEE 754 double; r=sfink (3c1f6fdda0)
- Bug 1211006 - Add Debugger.Source.prototype.canonicalId; r=ejpbruel (eef7b79fce)
- Bug 1199218 - Implement JS::ubi::Node::size for js::LazyScript referents; r=sfink (098a48d240)
- Bug 1220031 - Add JS::ubi::Node::scriptFilename; r=sfink (6b824ae680)
- Bug 1143575. Remove unused MediaQueue::Empty. r=cpearce (de737f3433)
- Bug 1209933 - Make sure all parent runtime pointers are the topmost parent, r=billm. (fe824d967d)
- Bug 1197012 - Fix ThrowTypeError in Notification. r=mccr8 (0b1a097526)
- Bug 1197893 - Check the number of arguments for ThrowTypeError() and ThrowRangeError() at compile time. r=peterv (d98c7d78a0)
- Bug 1142083 - Add test for IDN Unicode domain redirect. r=mcmanus (0c8961fe17)
- Bug 1187159 - Add mochitest for loading packaged apps (iframe+fetch+mozapp) r=jduell (ce90ea561b)
- Bug 1186290 - Notify TabParent to switch process when loading a signed package. r=honzab, r=kanru. (c58a14554a)
- fix (15e2df75eb)
- Bug 1206124 P1 Fix "same-origin" CORS credentials in FetchDriver. r=ehsan (fae1bb6ab3)
- Bug 1206124 P2 Test fetch() with credentials and redirects. r=ehsan (ffc6254112)
- Bug 1211751: Remove nsIChannelEventSink-forwarding from EventSource and FetchDriver. It's never needed. r=smaug (adafe5737a)
- Bug 1212433 Fail fetch() calls that require preflight and also redirect. r=sicking a=abillings (c0d6742b9e)
- Bug 1193128 - Fix base64 decoding when fetching data URIs. r=baku (80bafa291a)
- Bug 1195167 part 1: Let necko handle all protocols. r=bkelly (bb932b0ada)
- Bug 1195167 part 2: Remove redundant aCORSFlag argument and instead use mCORSFlagEverSet. r=bkelly (beadafcad0)
- Bug 1195167 part 3: Remove more scheme-specific handling from FetchDriver. r=bkelly (d00b38db9e)
- Bug 1195167 part 4: Remove FetchDriver::BasicFetch since it is empty. r=bkelly (c5ed097267)
- Bug 1210413 P2 Test CORS credentials on cross-origin redirects. r=sicking a=dveditz (b4eeb8aac0)
- Bug 1210413 P1 Propagate new channel load flags from child to parent on redirect. r=jduell a=dveditz (8b329af4fa)
- Bug 1195167 part 5: Make FetchDriver use AsyncOpen2. r=bkelly (cc217c4cc1)
- Bug 1195167 part 6: Some code simplification since necko handles fetch recursion. r=bkelly (f3b6da2262)
- Bug 1195167: Followup to fix test which I forgot to change (81e7439a2e)
- Bug 1215746: Remove RequestMode::Cors_with_forced_preflight. r=bkelly (0336e812b6)
- Bug 1211000: Move CORS preflight logic from nsCORSListenerProxy to nsCORSPreflightListener. r=ehsan (bf2f71cf22)
- missing bit of Bug 1211443 - Drop scheduled update if decoder initialization isn't done yet. r=jya (f6bc074e33)
- Bug 1182571: Fix nsILoadInfo->GetContentPolicyType API to be less ambigious. Audit and fix all users of it. r=ckerschb (5af6fa7442)
- fix (e40c8e7625)
- Bug 1173811 - Part 1: Propagate the response URL to intercepted channels when necessary (non-e10s). r=mayhemer,bkelly (26f4f13c28)
- Bug 1173811 - Part 2: Propagate the response URL to intercepted channels when necessary (e10s). r=mayhemer,bkelly (a603fe1df2)
- Bug 1154309 - Add New Resource Timing Fields r=bz,hurley (1d14eb6bef)
- Bug 1175685 - add OriginAttribute to LoadInfo. r=jonas, r=ckerschb, r=michal (a5d18bb637)
- Bug 1175685 - add OriginAttribute to LoadInfo. r=jonas, r=ckerschb, r=michal (fb07d2c8aa)
- Bug 1212904 P1 Add a LoadTainting enumeration. r=jduell (a1db8a3e99)
- Bug 1212904 P2 Add LoadTainting information to nsILoadInfo. r=jduell (2482e5e334)
- Bug 1221151 - use [infallible] in nsILoadInfo.idl instead of manual %{C++ blocks; r=jduell (aae73129b6)
- Bug 1045891 - CSP 2 child-src implementation r=ckerschb (792920aeb9)
- Bug 1219931 - CSP: Don't allow removing a policy (r=sicking) (9daaab4186)
- Bug 1208661 - Dump client-side layer textures. r=BenWa (1f2d17d515)
2022-10-17 11:08:37 +08:00

839 lines
24 KiB
C++

/* -*- 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 "prio.h"
#include "PLDHashTable.h"
#include "nsXPCOMStrings.h"
#include "mozilla/IOInterposer.h"
#include "mozilla/MemoryReporting.h"
#include "mozilla/scache/StartupCache.h"
#include "nsAutoPtr.h"
#include "nsClassHashtable.h"
#include "nsComponentManagerUtils.h"
#include "nsDirectoryServiceUtils.h"
#include "nsIClassInfo.h"
#include "nsIFile.h"
#include "nsIObserver.h"
#include "nsIObserverService.h"
#include "nsIOutputStream.h"
#include "nsIStartupCache.h"
#include "nsIStorageStream.h"
#include "nsIStreamBufferAccess.h"
#include "nsIStringStream.h"
#include "nsISupports.h"
#include "nsITimer.h"
#include "nsIZipWriter.h"
#include "nsIZipReader.h"
#include "nsWeakReference.h"
#include "nsZipArchive.h"
#include "mozilla/Omnijar.h"
#include "prenv.h"
#include "mozilla/Telemetry.h"
#include "nsThreadUtils.h"
#include "nsXULAppAPI.h"
#include "nsIProtocolHandler.h"
#ifdef IS_BIG_ENDIAN
#define SC_ENDIAN "big"
#else
#define SC_ENDIAN "little"
#endif
#if PR_BYTES_PER_WORD == 4
#define SC_WORDSIZE "4"
#else
#define SC_WORDSIZE "8"
#endif
namespace mozilla {
namespace scache {
MOZ_DEFINE_MALLOC_SIZE_OF(StartupCacheMallocSizeOf)
NS_IMETHODIMP
StartupCache::CollectReports(nsIHandleReportCallback* aHandleReport,
nsISupports* aData, bool aAnonymize)
{
#define REPORT(_path, _kind, _amount, _desc) \
do { \
nsresult rv = \
aHandleReport->Callback(EmptyCString(), \
NS_LITERAL_CSTRING(_path), \
_kind, UNITS_BYTES, _amount, \
NS_LITERAL_CSTRING(_desc), aData); \
NS_ENSURE_SUCCESS(rv, rv); \
} while (0)
REPORT("explicit/startup-cache/mapping", KIND_NONHEAP,
SizeOfMapping(),
"Memory used to hold the mapping of the startup cache from file. "
"This memory is likely to be swapped out shortly after start-up.");
REPORT("explicit/startup-cache/data", KIND_HEAP,
HeapSizeOfIncludingThis(StartupCacheMallocSizeOf),
"Memory used by the startup cache for things other than the file "
"mapping.");
return NS_OK;
}
#define STARTUP_CACHE_NAME "startupCache." SC_WORDSIZE "." SC_ENDIAN
StartupCache*
StartupCache::GetSingleton()
{
if (!gStartupCache) {
if (!XRE_IsParentProcess()) {
return nullptr;
}
#ifdef MOZ_DISABLE_STARTUPCACHE
return nullptr;
#endif
StartupCache::InitSingleton();
}
return StartupCache::gStartupCache;
}
void
StartupCache::DeleteSingleton()
{
StartupCache::gStartupCache = nullptr;
}
nsresult
StartupCache::InitSingleton()
{
nsresult rv;
StartupCache::gStartupCache = new StartupCache();
rv = StartupCache::gStartupCache->Init();
if (NS_FAILED(rv)) {
StartupCache::gStartupCache = nullptr;
}
return rv;
}
StaticRefPtr<StartupCache> StartupCache::gStartupCache;
bool StartupCache::gShutdownInitiated;
bool StartupCache::gIgnoreDiskCache;
enum StartupCache::TelemetrifyAge StartupCache::gPostFlushAgeAction = StartupCache::IGNORE_AGE;
NS_IMPL_ISUPPORTS(StartupCache, nsIMemoryReporter)
StartupCache::StartupCache()
: mArchive(nullptr), mStartupWriteInitiated(false), mWriteThread(nullptr)
{ }
StartupCache::~StartupCache()
{
if (mTimer) {
mTimer->Cancel();
}
// Generally, the in-memory table should be empty here,
// but an early shutdown means either mTimer didn't run
// or the write thread is still running.
WaitOnWriteThread();
// If we shutdown quickly timer wont have fired. Instead of writing
// it on the main thread and block the shutdown we simply wont update
// the startup cache. Always do this if the file doesn't exist since
// we use it part of the package step.
if (!mArchive) {
WriteToDisk();
}
UnregisterWeakMemoryReporter(this);
}
nsresult
StartupCache::Init()
{
// workaround for bug 653936
nsCOMPtr<nsIProtocolHandler> jarInitializer(do_GetService(NS_NETWORK_PROTOCOL_CONTRACTID_PREFIX "jar"));
nsresult rv;
// This allows to override the startup cache filename
// which is useful from xpcshell, when there is no ProfLDS directory to keep cache in.
char *env = PR_GetEnv("MOZ_STARTUP_CACHE");
if (env) {
rv = NS_NewLocalFile(NS_ConvertUTF8toUTF16(env), false, getter_AddRefs(mFile));
} else {
nsCOMPtr<nsIFile> file;
rv = NS_GetSpecialDirectory("ProfLDS",
getter_AddRefs(file));
if (NS_FAILED(rv)) {
// return silently, this will fail in mochitests's xpcshell process.
return rv;
}
nsCOMPtr<nsIFile> profDir;
NS_GetSpecialDirectory("ProfDS", getter_AddRefs(profDir));
if (profDir) {
bool same;
if (NS_SUCCEEDED(profDir->Equals(file, &same)) && !same) {
// We no longer store the startup cache in the main profile
// directory, so we should cleanup the old one.
if (NS_SUCCEEDED(
profDir->AppendNative(NS_LITERAL_CSTRING("startupCache")))) {
profDir->Remove(true);
}
}
}
rv = file->AppendNative(NS_LITERAL_CSTRING("startupCache"));
NS_ENSURE_SUCCESS(rv, rv);
// Try to create the directory if it's not there yet
rv = file->Create(nsIFile::DIRECTORY_TYPE, 0777);
if (NS_FAILED(rv) && rv != NS_ERROR_FILE_ALREADY_EXISTS)
return rv;
rv = file->AppendNative(NS_LITERAL_CSTRING(STARTUP_CACHE_NAME));
NS_ENSURE_SUCCESS(rv, rv);
mFile = do_QueryInterface(file);
}
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);
rv = LoadArchive(RECORD_AGE);
// Sometimes we don't have a cache yet, that's ok.
// If it's corrupted, just remove it and start over.
if (gIgnoreDiskCache || (NS_FAILED(rv) && rv != NS_ERROR_FILE_NOT_FOUND)) {
NS_WARNING("Failed to load startupcache file correctly, removing!");
InvalidateCache();
}
RegisterWeakMemoryReporter(this);
return NS_OK;
}
/**
* LoadArchive can be called from the main thread or while reloading cache on write thread.
*/
nsresult
StartupCache::LoadArchive(enum TelemetrifyAge flag)
{
if (gIgnoreDiskCache)
return NS_ERROR_FAILURE;
bool exists;
mArchive = nullptr;
nsresult rv = mFile->Exists(&exists);
if (NS_FAILED(rv) || !exists)
return NS_ERROR_FILE_NOT_FOUND;
mArchive = new nsZipArchive();
rv = mArchive->OpenArchive(mFile);
if (NS_FAILED(rv) || flag == IGNORE_AGE)
return rv;
nsCString comment;
if (!mArchive->GetComment(comment)) {
return rv;
}
const char *data;
size_t len = NS_CStringGetData(comment, &data);
PRTime creationStamp;
// We might not have a comment if the startup cache file was created
// before we started recording creation times in the comment.
if (len == sizeof(creationStamp)) {
memcpy(&creationStamp, data, len);
PRTime current = PR_Now();
int64_t diff = current - creationStamp;
// We can't use AccumulateTimeDelta here because we have no way of
// reifying a TimeStamp from creationStamp.
int64_t usec_per_hour = PR_USEC_PER_SEC * int64_t(3600);
int64_t hour_diff = (diff + usec_per_hour - 1) / usec_per_hour;
mozilla::Telemetry::Accumulate(Telemetry::STARTUP_CACHE_AGE_HOURS,
hour_diff);
}
return rv;
}
namespace {
nsresult
GetBufferFromZipArchive(nsZipArchive *zip, bool doCRC, const char* id,
char** outbuf, uint32_t* length)
{
if (!zip)
return NS_ERROR_NOT_AVAILABLE;
nsZipItemPtr<char> zipItem(zip, id, doCRC);
if (!zipItem)
return NS_ERROR_NOT_AVAILABLE;
*outbuf = zipItem.Forget();
*length = zipItem.Length();
return NS_OK;
}
} /* anonymous namespace */
// NOTE: this will not find a new entry until it has been written to disk!
// Consumer should take ownership of the resulting buffer.
nsresult
StartupCache::GetBuffer(const char* id, char** outbuf, uint32_t* length)
{
NS_ASSERTION(NS_IsMainThread(), "Startup cache only available on main thread");
WaitOnWriteThread();
if (!mStartupWriteInitiated) {
CacheEntry* entry;
nsDependentCString idStr(id);
mTable.Get(idStr, &entry);
if (entry) {
*outbuf = new char[entry->size];
memcpy(*outbuf, entry->data, entry->size);
*length = entry->size;
return NS_OK;
}
}
nsresult rv = GetBufferFromZipArchive(mArchive, true, id, outbuf, length);
if (NS_SUCCEEDED(rv))
return rv;
RefPtr<nsZipArchive> omnijar = mozilla::Omnijar::GetReader(mozilla::Omnijar::APP);
// no need to checksum omnijarred entries
rv = GetBufferFromZipArchive(omnijar, false, id, outbuf, length);
if (NS_SUCCEEDED(rv))
return rv;
omnijar = mozilla::Omnijar::GetReader(mozilla::Omnijar::GRE);
// no need to checksum omnijarred entries
return GetBufferFromZipArchive(omnijar, false, id, outbuf, length);
}
// Makes a copy of the buffer, client retains ownership of inbuf.
nsresult
StartupCache::PutBuffer(const char* id, const char* inbuf, uint32_t len)
{
NS_ASSERTION(NS_IsMainThread(), "Startup cache only available on main thread");
WaitOnWriteThread();
if (StartupCache::gShutdownInitiated) {
return NS_ERROR_NOT_AVAILABLE;
}
nsAutoArrayPtr<char> data(new char[len]);
memcpy(data, inbuf, len);
nsCString idStr(id);
// Cache it for now, we'll write all together later.
CacheEntry* entry;
if (mTable.Get(idStr)) {
NS_ASSERTION(false, "Existing entry in StartupCache.");
// Double-caching is undesirable but not an error.
return NS_OK;
}
#ifdef DEBUG
if (mArchive) {
nsZipItem* zipItem = mArchive->GetItem(id);
NS_ASSERTION(zipItem == nullptr, "Existing entry in disk StartupCache.");
}
#endif
entry = new CacheEntry(data.forget(), len);
mTable.Put(idStr, entry);
mPendingWrites.AppendElement(idStr);
return ResetStartupWriteTimer();
}
size_t
StartupCache::SizeOfMapping()
{
return mArchive ? mArchive->SizeOfMapping() : 0;
}
size_t
StartupCache::HeapSizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf) const
{
// This function could measure more members, but they haven't been found by
// DMD to be significant. They can be added later if necessary.
size_t n = aMallocSizeOf(this);
n += mTable.ShallowSizeOfExcludingThis(aMallocSizeOf);
for (auto iter = mTable.ConstIter(); !iter.Done(); iter.Next()) {
n += iter.Data()->SizeOfIncludingThis(aMallocSizeOf);
}
n += mPendingWrites.ShallowSizeOfExcludingThis(aMallocSizeOf);
return n;
}
struct CacheWriteHolder
{
nsCOMPtr<nsIZipWriter> writer;
nsCOMPtr<nsIStringInputStream> stream;
PRTime time;
};
static void
CacheCloseHelper(const nsACString& key, const CacheEntry* data,
const CacheWriteHolder* holder)
{
MOZ_ASSERT(data); // assert key was found in mTable.
nsresult rv;
nsIStringInputStream* stream = holder->stream;
nsIZipWriter* writer = holder->writer;
stream->ShareData(data->data, data->size);
#ifdef DEBUG
bool hasEntry;
rv = writer->HasEntry(key, &hasEntry);
NS_ASSERTION(NS_SUCCEEDED(rv) && hasEntry == false,
"Existing entry in disk StartupCache.");
#endif
rv = writer->AddEntryStream(key, holder->time, true, stream, false);
if (NS_FAILED(rv)) {
NS_WARNING("cache entry deleted but not written to disk.");
}
}
/**
* WriteToDisk writes the cache out to disk. Callers of WriteToDisk need to call WaitOnWriteThread
* to make sure there isn't a write happening on another thread
*/
void
StartupCache::WriteToDisk()
{
nsresult rv;
mStartupWriteInitiated = true;
if (mTable.Count() == 0)
return;
nsCOMPtr<nsIZipWriter> zipW = do_CreateInstance("@mozilla.org/zipwriter;1");
if (!zipW)
return;
rv = zipW->Open(mFile, PR_RDWR | PR_CREATE_FILE);
if (NS_FAILED(rv)) {
NS_WARNING("could not open zipfile for write");
return;
}
// If we didn't have an mArchive member, that means that we failed to
// open the startup cache for reading. Therefore, we need to record
// the time of creation in a zipfile comment; this will be useful for
// Telemetry statistics.
PRTime now = PR_Now();
if (!mArchive) {
nsCString comment;
comment.Assign((char *)&now, sizeof(now));
zipW->SetComment(comment);
}
nsCOMPtr<nsIStringInputStream> stream
= do_CreateInstance("@mozilla.org/io/string-input-stream;1", &rv);
if (NS_FAILED(rv)) {
NS_WARNING("Couldn't create string input stream.");
return;
}
CacheWriteHolder holder;
holder.stream = stream;
holder.writer = zipW;
holder.time = now;
for (auto key = mPendingWrites.begin(); key != mPendingWrites.end(); key++) {
CacheCloseHelper(*key, mTable.Get(*key), &holder);
}
mPendingWrites.Clear();
mTable.Clear();
// Close the archive so Windows doesn't choke.
mArchive = nullptr;
zipW->Close();
// We succesfully wrote the archive to disk; mark the disk file as trusted
gIgnoreDiskCache = false;
// Our reader's view of the archive is outdated now, reload it.
LoadArchive(gPostFlushAgeAction);
return;
}
void
StartupCache::InvalidateCache()
{
WaitOnWriteThread();
mPendingWrites.Clear();
mTable.Clear();
mArchive = nullptr;
nsresult rv = mFile->Remove(false);
if (NS_FAILED(rv) && rv != NS_ERROR_FILE_TARGET_DOES_NOT_EXIST &&
rv != NS_ERROR_FILE_NOT_FOUND) {
gIgnoreDiskCache = true;
mozilla::Telemetry::Accumulate(Telemetry::STARTUP_CACHE_INVALID, true);
return;
}
gIgnoreDiskCache = false;
LoadArchive(gPostFlushAgeAction);
}
void
StartupCache::IgnoreDiskCache()
{
gIgnoreDiskCache = true;
if (gStartupCache)
gStartupCache->InvalidateCache();
}
/*
* WaitOnWriteThread() is called from a main thread to wait for the worker
* thread to finish. However since the same code is used in the worker thread and
* main thread, the worker thread can also call WaitOnWriteThread() which is a no-op.
*/
void
StartupCache::WaitOnWriteThread()
{
NS_ASSERTION(NS_IsMainThread(), "Startup cache should only wait for io thread on main thread");
if (!mWriteThread || mWriteThread == PR_GetCurrentThread())
return;
PR_JoinThread(mWriteThread);
mWriteThread = nullptr;
}
void
StartupCache::ThreadedWrite(void *aClosure)
{
PR_SetCurrentThreadName("StartupCache");
mozilla::IOInterposer::RegisterCurrentThread();
/*
* It is safe to use the pointer passed in aClosure to reference the
* StartupCache object because the thread's lifetime is tightly coupled to
* the lifetime of the StartupCache object; this thread is joined in the
* StartupCache destructor, guaranteeing that this function runs if and only
* if the StartupCache object is valid.
*/
StartupCache* startupCacheObj = static_cast<StartupCache*>(aClosure);
startupCacheObj->WriteToDisk();
mozilla::IOInterposer::UnregisterCurrentThread();
}
/*
* The write-thread is spawned on a timeout(which is reset with every write). This
* can avoid a slow shutdown. After writing out the cache, the zipreader is
* reloaded on the worker thread.
*/
void
StartupCache::WriteTimeout(nsITimer *aTimer, void *aClosure)
{
/*
* It is safe to use the pointer passed in aClosure to reference the
* StartupCache object because the timer's lifetime is tightly coupled to
* the lifetime of the StartupCache object; this timer is canceled in the
* StartupCache destructor, guaranteeing that this function runs if and only
* if the StartupCache object is valid.
*/
StartupCache* startupCacheObj = static_cast<StartupCache*>(aClosure);
startupCacheObj->mWriteThread = PR_CreateThread(PR_USER_THREAD,
StartupCache::ThreadedWrite,
startupCacheObj,
PR_PRIORITY_NORMAL,
PR_GLOBAL_THREAD,
PR_JOINABLE_THREAD,
0);
}
// We don't want to refcount StartupCache, so we'll just
// hold a ref to this and pass it to observerService instead.
NS_IMPL_ISUPPORTS(StartupCacheListener, nsIObserver)
nsresult
StartupCacheListener::Observe(nsISupports *subject, const char* topic, const char16_t* data)
{
StartupCache* sc = StartupCache::GetSingleton();
if (!sc)
return NS_OK;
if (strcmp(topic, NS_XPCOM_SHUTDOWN_OBSERVER_ID) == 0) {
// Do not leave the thread running past xpcom shutdown
sc->WaitOnWriteThread();
StartupCache::gShutdownInitiated = true;
} else if (strcmp(topic, "startupcache-invalidate") == 0) {
sc->InvalidateCache();
}
return NS_OK;
}
nsresult
StartupCache::GetDebugObjectOutputStream(nsIObjectOutputStream* aStream,
nsIObjectOutputStream** aOutStream)
{
NS_ENSURE_ARG_POINTER(aStream);
#ifdef DEBUG
StartupCacheDebugOutputStream* stream
= new StartupCacheDebugOutputStream(aStream, &mWriteObjectMap);
NS_ADDREF(*aOutStream = stream);
#else
NS_ADDREF(*aOutStream = aStream);
#endif
return NS_OK;
}
nsresult
StartupCache::ResetStartupWriteTimer()
{
mStartupWriteInitiated = false;
nsresult rv;
if (!mTimer)
mTimer = do_CreateInstance("@mozilla.org/timer;1", &rv);
else
rv = mTimer->Cancel();
NS_ENSURE_SUCCESS(rv, rv);
// Wait for 10 seconds, then write out the cache.
mTimer->InitWithFuncCallback(StartupCache::WriteTimeout, this, 60000,
nsITimer::TYPE_ONE_SHOT);
return NS_OK;
}
nsresult
StartupCache::RecordAgesAlways()
{
gPostFlushAgeAction = RECORD_AGE;
return NS_OK;
}
// StartupCacheDebugOutputStream implementation
#ifdef DEBUG
NS_IMPL_ISUPPORTS(StartupCacheDebugOutputStream, nsIObjectOutputStream,
nsIBinaryOutputStream, nsIOutputStream)
bool
StartupCacheDebugOutputStream::CheckReferences(nsISupports* aObject)
{
nsresult rv;
nsCOMPtr<nsIClassInfo> classInfo = do_QueryInterface(aObject);
if (!classInfo) {
NS_ERROR("aObject must implement nsIClassInfo");
return false;
}
uint32_t flags;
rv = classInfo->GetFlags(&flags);
NS_ENSURE_SUCCESS(rv, false);
if (flags & nsIClassInfo::SINGLETON)
return true;
nsISupportsHashKey* key = mObjectMap->GetEntry(aObject);
if (key) {
NS_ERROR("non-singleton aObject is referenced multiple times in this"
"serialization, we don't support that.");
return false;
}
mObjectMap->PutEntry(aObject);
return true;
}
// nsIObjectOutputStream implementation
nsresult
StartupCacheDebugOutputStream::WriteObject(nsISupports* aObject, bool aIsStrongRef)
{
nsCOMPtr<nsISupports> rootObject(do_QueryInterface(aObject));
NS_ASSERTION(rootObject.get() == aObject,
"bad call to WriteObject -- call WriteCompoundObject!");
bool check = CheckReferences(aObject);
NS_ENSURE_TRUE(check, NS_ERROR_FAILURE);
return mBinaryStream->WriteObject(aObject, aIsStrongRef);
}
nsresult
StartupCacheDebugOutputStream::WriteSingleRefObject(nsISupports* aObject)
{
nsCOMPtr<nsISupports> rootObject(do_QueryInterface(aObject));
NS_ASSERTION(rootObject.get() == aObject,
"bad call to WriteSingleRefObject -- call WriteCompoundObject!");
bool check = CheckReferences(aObject);
NS_ENSURE_TRUE(check, NS_ERROR_FAILURE);
return mBinaryStream->WriteSingleRefObject(aObject);
}
nsresult
StartupCacheDebugOutputStream::WriteCompoundObject(nsISupports* aObject,
const nsIID& aIID,
bool aIsStrongRef)
{
nsCOMPtr<nsISupports> rootObject(do_QueryInterface(aObject));
nsCOMPtr<nsISupports> roundtrip;
rootObject->QueryInterface(aIID, getter_AddRefs(roundtrip));
NS_ASSERTION(roundtrip.get() == aObject,
"bad aggregation or multiple inheritance detected by call to "
"WriteCompoundObject!");
bool check = CheckReferences(aObject);
NS_ENSURE_TRUE(check, NS_ERROR_FAILURE);
return mBinaryStream->WriteCompoundObject(aObject, aIID, aIsStrongRef);
}
nsresult
StartupCacheDebugOutputStream::WriteID(nsID const& aID)
{
return mBinaryStream->WriteID(aID);
}
char*
StartupCacheDebugOutputStream::GetBuffer(uint32_t aLength, uint32_t aAlignMask)
{
return mBinaryStream->GetBuffer(aLength, aAlignMask);
}
void
StartupCacheDebugOutputStream::PutBuffer(char* aBuffer, uint32_t aLength)
{
mBinaryStream->PutBuffer(aBuffer, aLength);
}
#endif //DEBUG
StartupCacheWrapper* StartupCacheWrapper::gStartupCacheWrapper = nullptr;
NS_IMPL_ISUPPORTS(StartupCacheWrapper, nsIStartupCache)
StartupCacheWrapper::~StartupCacheWrapper()
{
MOZ_ASSERT(gStartupCacheWrapper == this);
gStartupCacheWrapper = nullptr;
}
StartupCacheWrapper* StartupCacheWrapper::GetSingleton()
{
if (!gStartupCacheWrapper)
gStartupCacheWrapper = new StartupCacheWrapper();
NS_ADDREF(gStartupCacheWrapper);
return gStartupCacheWrapper;
}
nsresult
StartupCacheWrapper::GetBuffer(const char* id, char** outbuf, uint32_t* length)
{
StartupCache* sc = StartupCache::GetSingleton();
if (!sc) {
return NS_ERROR_NOT_INITIALIZED;
}
return sc->GetBuffer(id, outbuf, length);
}
nsresult
StartupCacheWrapper::PutBuffer(const char* id, const char* inbuf, uint32_t length)
{
StartupCache* sc = StartupCache::GetSingleton();
if (!sc) {
return NS_ERROR_NOT_INITIALIZED;
}
return sc->PutBuffer(id, inbuf, length);
}
nsresult
StartupCacheWrapper::InvalidateCache()
{
StartupCache* sc = StartupCache::GetSingleton();
if (!sc) {
return NS_ERROR_NOT_INITIALIZED;
}
sc->InvalidateCache();
return NS_OK;
}
nsresult
StartupCacheWrapper::IgnoreDiskCache()
{
StartupCache::IgnoreDiskCache();
return NS_OK;
}
nsresult
StartupCacheWrapper::GetDebugObjectOutputStream(nsIObjectOutputStream* stream,
nsIObjectOutputStream** outStream)
{
StartupCache* sc = StartupCache::GetSingleton();
if (!sc) {
return NS_ERROR_NOT_INITIALIZED;
}
return sc->GetDebugObjectOutputStream(stream, outStream);
}
nsresult
StartupCacheWrapper::StartupWriteComplete(bool *complete)
{
StartupCache* sc = StartupCache::GetSingleton();
if (!sc) {
return NS_ERROR_NOT_INITIALIZED;
}
sc->WaitOnWriteThread();
*complete = sc->mStartupWriteInitiated && sc->mTable.Count() == 0;
return NS_OK;
}
nsresult
StartupCacheWrapper::ResetStartupWriteTimer()
{
StartupCache* sc = StartupCache::GetSingleton();
return sc ? sc->ResetStartupWriteTimer() : NS_ERROR_NOT_INITIALIZED;
}
nsresult
StartupCacheWrapper::GetObserver(nsIObserver** obv) {
StartupCache* sc = StartupCache::GetSingleton();
if (!sc) {
return NS_ERROR_NOT_INITIALIZED;
}
NS_ADDREF(*obv = sc->mListener);
return NS_OK;
}
nsresult
StartupCacheWrapper::RecordAgesAlways() {
StartupCache *sc = StartupCache::GetSingleton();
return sc ? sc->RecordAgesAlways() : NS_ERROR_NOT_INITIALIZED;
}
} // namespace scache
} // namespace mozilla