Bug 584238 - Add an API to clear the media cache; r=kinetik a=blocking-betaN+

This commit is contained in:
Robert O'Callahan 2010-10-13 10:14:08 -04:00
parent 4c6c291f65
commit e6023b9a3d

View File

@ -119,7 +119,9 @@ public:
MOZ_COUNT_DTOR(nsMediaCache);
}
// Main thread only. Creates the backing cache file.
// Main thread only. Creates the backing cache file. If this fails,
// then the cache is still in a semi-valid state; mFD will be null,
// so all I/O on the cache file will fail.
nsresult Init();
// Shut down the global cache if it's no longer needed. We shut down
// the cache as soon as there are no streams. This means that during
@ -128,6 +130,10 @@ public:
// shutting it down cleans things up and releases disk space.
static void MaybeShutdown();
// Brutally flush the cache contents. Main thread only.
static void Flush();
void FlushInternal();
// Cache-file access methods. These are the lowest-level cache methods.
// mMonitor must be held; these can be called on any thread.
// This can return partial reads.
@ -494,6 +500,7 @@ nsresult
nsMediaCache::Init()
{
NS_ASSERTION(NS_IsMainThread(), "Only call on main thread");
NS_ASSERTION(!mFD, "Cache file already open?");
if (!mMonitor) {
// the constructor failed
@ -527,6 +534,36 @@ nsMediaCache::Init()
return NS_OK;
}
void
nsMediaCache::Flush()
{
NS_ASSERTION(NS_IsMainThread(), "Only call on main thread");
if (!gMediaCache)
return;
gMediaCache->FlushInternal();
}
void
nsMediaCache::FlushInternal()
{
nsAutoMonitor mon(mMonitor);
for (PRInt32 blockIndex = 0; blockIndex < mIndex.Length(); ++blockIndex) {
FreeBlock(blockIndex);
}
// Truncate file, close it, and reopen
Truncate();
NS_ASSERTION(mIndex.Length() == 0, "Blocks leaked?");
if (mFD) {
PR_Close(mFD);
mFD = nsnull;
}
Init();
}
void
nsMediaCache::MaybeShutdown()
{