Bug 724513 - Part 1 - Add StartupCache method for disregarding disk file. r=mwu

This commit is contained in:
Graeme McCutcheon 2012-10-11 09:17:15 +01:00
Родитель 87a6dd92fb
Коммит 75b6269d53
4 изменённых файлов: 152 добавлений и 8 удалений

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

@ -122,6 +122,7 @@ StartupCache::InitSingleton()
StartupCache* StartupCache::gStartupCache;
bool StartupCache::gShutdownInitiated;
bool StartupCache::gIgnoreDiskCache;
enum StartupCache::TelemetrifyAge StartupCache::gPostFlushAgeAction = StartupCache::IGNORE_AGE;
StartupCache::StartupCache()
@ -203,12 +204,12 @@ StartupCache::Init()
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 (NS_FAILED(rv) && rv != NS_ERROR_FILE_NOT_FOUND) {
if (gIgnoreDiskCache || (NS_FAILED(rv) && rv != NS_ERROR_FILE_NOT_FOUND)) {
NS_WARNING("Failed to load startupcache file correctly, removing!");
InvalidateCache();
}
@ -227,6 +228,9 @@ StartupCache::Init()
nsresult
StartupCache::LoadArchive(enum TelemetrifyAge flag)
{
if (gIgnoreDiskCache)
return NS_ERROR_FAILURE;
bool exists;
mArchive = NULL;
nsresult rv = mFile->Exists(&exists);
@ -458,6 +462,9 @@ StartupCache::WriteToDisk()
mArchive = NULL;
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);
@ -470,10 +477,24 @@ StartupCache::InvalidateCache()
WaitOnWriteThread();
mTable.Clear();
mArchive = NULL;
mFile->Remove(false);
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;
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
@ -713,6 +734,13 @@ StartupCacheWrapper::InvalidateCache()
return NS_OK;
}
nsresult
StartupCacheWrapper::IgnoreDiskCache()
{
StartupCache::IgnoreDiskCache();
return NS_OK;
}
nsresult
StartupCacheWrapper::GetDebugObjectOutputStream(nsIObjectOutputStream* stream,
nsIObjectOutputStream** outStream)

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

@ -44,6 +44,12 @@
*
* InvalidateCache() may be called if a client suspects data corruption
* or wishes to invalidate for any other reason. This will remove all existing cache data.
* Additionally, the static method IgnoreDiskCache() can be called if it is
* believed that the on-disk cache file is itself corrupt. This call implicitly
* calls InvalidateCache (if the singleton has been initialized) to ensure any
* data already read from disk is discarded. The cache will not load data from
* the disk file until a successful write occurs.
*
* Finally, getDebugObjectOutputStream() allows debug code to wrap an objectstream
* with a debug objectstream, to check for multiply-referenced objects. These will
* generally fail to deserialize correctly, unless they are stateless singletons or the
@ -114,6 +120,9 @@ public:
// Removes the cache file.
void InvalidateCache();
// Signal that data should not be loaded from the cache file
static void IgnoreDiskCache();
// In DEBUG builds, returns a stream that will attempt to check for
// and disallow multiple writes of the same object.
nsresult GetDebugObjectOutputStream(nsIObjectOutputStream* aStream,
@ -167,6 +176,7 @@ private:
static StartupCache *gStartupCache;
static bool gShutdownInitiated;
static bool gIgnoreDiskCache;
PRThread *mWriteThread;
#ifdef DEBUG
nsTHashtable<nsISupportsHashKey> mWriteObjectMap;

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

@ -9,7 +9,7 @@
#include "nsIObserver.idl"
#include "nsIObjectOutputStream.idl"
[uuid(c1b3796b-33af-4ff0-b83d-8eb0ca2c080f)]
[uuid(25957820-90a1-428c-8739-b0845d3cc534)]
interface nsIStartupCache : nsISupports
{
@ -24,6 +24,8 @@ interface nsIStartupCache : nsISupports
void invalidateCache();
void ignoreDiskCache();
/** In debug builds, wraps this object output stream with a stream that will
* detect and prevent the write of a multiply-referenced non-singleton object
* during serialization. In non-debug, returns an add-ref'd pointer to

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

@ -22,6 +22,7 @@
#include "nsIPrefService.h"
#include "nsITelemetry.h"
#include "jsapi.h"
#include "prio.h"
namespace mozilla {
namespace scache {
@ -56,7 +57,7 @@ PR_END_MACRO
nsresult
WaitForStartupTimer() {
nsresult rv;
nsCOMPtr<nsIStartupCache> sc
nsCOMPtr<nsIStartupCache> sc
= do_GetService("@mozilla.org/startupcache/cache;1");
PR_Sleep(10 * PR_TicksPerSecond());
@ -75,7 +76,7 @@ WaitForStartupTimer() {
nsresult
TestStartupWriteRead() {
nsresult rv;
nsCOMPtr<nsIStartupCache> sc
nsCOMPtr<nsIStartupCache> sc
= do_GetService("@mozilla.org/startupcache/cache;1", &rv);
if (!sc) {
fail("didn't get a pointer...");
@ -118,7 +119,7 @@ TestWriteInvalidateRead() {
const char* id = "id";
char* outbuf = NULL;
uint32_t len;
nsCOMPtr<nsIStartupCache> sc
nsCOMPtr<nsIStartupCache> sc
= do_GetService("@mozilla.org/startupcache/cache;1", &rv);
sc->InvalidateCache();
@ -247,10 +248,110 @@ TestWriteObject() {
return NS_OK;
}
nsresult
LockCacheFile(bool protect, nsIFile* profileDir) {
NS_ENSURE_ARG(profileDir);
nsCOMPtr<nsIFile> startupCache;
profileDir->Clone(getter_AddRefs(startupCache));
NS_ENSURE_STATE(startupCache);
startupCache->AppendNative(NS_LITERAL_CSTRING("startupCache"));
nsresult rv;
#ifndef XP_WIN
static uint32_t oldPermissions;
#else
static PRFileDesc* fd = nullptr;
#endif
// To prevent deletion of the startupcache file, we change the containing
// directory's permissions on Linux/Mac, and hold the file open on Windows
if (protect) {
#ifndef XP_WIN
rv = startupCache->GetPermissions(&oldPermissions);
NS_ENSURE_SUCCESS(rv, rv);
rv = startupCache->SetPermissions(0555);
NS_ENSURE_SUCCESS(rv, rv);
#else
// Filename logic from StartupCache.cpp
#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
char sStartupCacheName[] = "startupCache." SC_WORDSIZE "." SC_ENDIAN;
startupCache->AppendNative(NS_LITERAL_CSTRING(sStartupCacheName));
rv = startupCache->OpenNSPRFileDesc(PR_RDONLY, 0, &fd);
NS_ENSURE_SUCCESS(rv, rv);
#endif
} else {
#ifndef XP_WIN
rv = startupCache->SetPermissions(oldPermissions);
NS_ENSURE_SUCCESS(rv, rv);
#else
PR_Close(fd);
#endif
}
return NS_OK;
}
nsresult
TestIgnoreDiskCache(nsIFile* profileDir) {
nsresult rv;
nsCOMPtr<nsIStartupCache> sc
= do_GetService("@mozilla.org/startupcache/cache;1", &rv);
sc->InvalidateCache();
const char* buf = "Get a Beardbook app for your smartphone";
const char* id = "id";
char* outbuf = NULL;
PRUint32 len;
rv = sc->PutBuffer(id, buf, strlen(buf) + 1);
NS_ENSURE_SUCCESS(rv, rv);
rv = sc->ResetStartupWriteTimer();
rv = WaitForStartupTimer();
NS_ENSURE_SUCCESS(rv, rv);
// Prevent StartupCache::InvalidateCache from deleting the disk file
rv = LockCacheFile(true, profileDir);
NS_ENSURE_SUCCESS(rv, rv);
sc->IgnoreDiskCache();
rv = sc->GetBuffer(id, &outbuf, &len);
nsresult r = LockCacheFile(false, profileDir);
NS_ENSURE_SUCCESS(r, r);
delete[] outbuf;
if (rv == NS_ERROR_NOT_AVAILABLE) {
passed("buffer not available after ignoring disk cache");
} else if (NS_SUCCEEDED(rv)) {
fail("GetBuffer succeeded unexpectedly after ignoring disk cache");
return NS_ERROR_UNEXPECTED;
} else {
fail("GetBuffer gave an unexpected failure, expected NOT_AVAILABLE");
return rv;
}
sc->InvalidateCache();
return NS_OK;
}
nsresult
TestEarlyShutdown() {
nsresult rv;
nsCOMPtr<nsIStartupCache> sc
nsCOMPtr<nsIStartupCache> sc
= do_GetService("@mozilla.org/startupcache/cache;1", &rv);
sc->InvalidateCache();
@ -416,6 +517,9 @@ int main(int argc, char** argv)
rv = 1;
if (NS_FAILED(TestWriteObject()))
rv = 1;
nsCOMPtr<nsIFile> profileDir = xpcom.GetProfileDirectory();
if (NS_FAILED(TestIgnoreDiskCache(profileDir)))
rv = 1;
if (NS_FAILED(TestEarlyShutdown()))
rv = 1;