Commit 872bbb27 authored by gordon%netscape.com's avatar gordon%netscape.com
Browse files

For bugscape bug 6985. Enabling cache support for dynamically switching...

For bugscape bug 6985.  Enabling cache support for dynamically switching profiles.  r=ccarlen, sr=darin.
parent bdf2ce23
Loading
Loading
Loading
Loading
+46 −11
Original line number Diff line number Diff line
@@ -222,16 +222,13 @@ nsCacheProfilePrefObserver::Observe(nsISupports * subject,
        mHaveProfile = PR_FALSE;

        // XXX shutdown devices
        if (NS_LITERAL_STRING("shutdown-cleanse").Equals(data)) {
            // XXX we need to delete the disk cache
            // printf("cache observer: shutdown-cleanse\n");
        }
        nsCacheService::OnProfileShutdown(NS_LITERAL_STRING("shutdown-cleanse").Equals(data));
        
    } else if (NS_LITERAL_STRING("profile-after-change").Equals(topic)) {
        // profile after change
        mHaveProfile = PR_TRUE;
        ReadPrefs();
        nsCacheService::ProfileChanged();
        nsCacheService::OnProfileChanged();
    
    } else if (NS_LITERAL_STRING("nsPref:changed").Equals(topic)) {
        if (!mHaveProfile)  return NS_OK;
@@ -1054,21 +1051,59 @@ nsCacheService::ProxyObjectRelease(nsISupports * object, PRThread * thread)


void
nsCacheService::ProfileChanged()
nsCacheService::OnProfileShutdown(PRBool cleanse)
{
    if (!gService)  return;
    nsAutoLock lock(gService->mCacheServiceLock);

    NS_ASSERTION(!gService->mDiskDevice, "switching cache directories not supported yet.");
    if (gService->mDiskDevice) {
        if (cleanse)
            gService->mDiskDevice->EvictEntries(nsnull);
        gService->mDiskDevice->Shutdown();
        gService->mEnableDiskDevice = PR_FALSE;
    }
#if 0
    if (gService->mMemoryDevice) {
        gService->mMemoryDevice->Shutdown();
        gService->mEnableMemoryDevice = PR_FALSE;
    }
#endif
}


void
nsCacheService::OnProfileChanged()
{
    if (!gService)  return;
 
    nsresult   rv = NS_OK;
    nsAutoLock lock(gService->mCacheServiceLock);
    
    gService->mEnableDiskDevice   = gService->mObserver->DiskCacheEnabled();
    gService->mEnableMemoryDevice = gService->mObserver->MemoryCacheEnabled();

    if (gService->mDiskDevice)
    if (gService->mDiskDevice) {
        gService->mDiskDevice->SetCacheParentDirectory(gService->mObserver->DiskCacheParentDirectory());
        gService->mDiskDevice->SetCapacity(gService->mObserver->DiskCacheCapacity());

    if (gService->mMemoryDevice)
        // XXX initialization of mDiskDevice could be made lazily, if mEnableDiskDevice is false
        rv = gService->mDiskDevice->Init();
        if (NS_FAILED(rv)) {
            NS_ERROR("nsCacheService::OnProfileChanged: Re-initializing disk device failed");
            gService->mEnableDiskDevice = PR_FALSE;
            // XXX delete mDiskDevice?
        }
    }
    
    if (gService->mMemoryDevice) {
        gService->mMemoryDevice->SetCapacity(gService->mObserver->MemoryCacheCapacity());
        rv = gService->mMemoryDevice->Init();
        if (NS_FAILED(rv) && (rv != NS_ERROR_ALREADY_INITIALIZED)) {
            NS_ERROR("nsCacheService::OnProfileChanged: Re-initializing disk device failed");
            gService->mEnableMemoryDevice = PR_FALSE;
            // XXX delete mMemoryDevice?
        }
    }
}


+2 −1
Original line number Diff line number Diff line
@@ -112,7 +112,8 @@ public:
    /**
     * Methods called by nsCacheProfilePrefObserver
     */
    static void      ProfileChanged();
    static void      OnProfileShutdown(PRBool cleanse);
    static void      OnProfileChanged();

    static void      SetDiskCacheEnabled(PRBool  enabled);
    static void      SetDiskCacheCapacity(PRInt32  capacity);
+10 −2
Original line number Diff line number Diff line
@@ -155,8 +155,7 @@ nsDiskCacheBindery::nsDiskCacheBindery()

nsDiskCacheBindery::~nsDiskCacheBindery()
{
    if (initialized)
        PL_DHashTableFinish(&table);
    Reset();
}


@@ -171,6 +170,15 @@ nsDiskCacheBindery::Init()
    return rv;
}

void
nsDiskCacheBindery::Reset()
{
    if (initialized) {
        PL_DHashTableFinish(&table);
        initialized = PR_FALSE;
    }
}


nsDiskCacheBinding *
nsDiskCacheBindery::CreateBinding(nsCacheEntry *       entry,
+1 −0
Original line number Diff line number Diff line
@@ -123,6 +123,7 @@ public:
    ~nsDiskCacheBindery();

    nsresult                Init();
    void                    Reset();

    nsDiskCacheBinding *   CreateBinding(nsCacheEntry *       entry,
                                         nsDiskCacheRecord *  record);
+13 −3
Original line number Diff line number Diff line
@@ -275,6 +275,8 @@ nsDiskCacheDevice::Init()
{
    nsresult rv;

    NS_ENSURE_TRUE(!mInitialized, NS_ERROR_FAILURE);
    
    rv = mBindery.Init();
    if (NS_FAILED(rv)) return rv;
    
@@ -327,6 +329,10 @@ nsDiskCacheDevice::Shutdown()

        // write out persistent information about the cache.
        (void) mCacheMap->Close();
        delete mCacheMap;
        mCacheMap = nsnull;

        mBindery.Reset();

        // no longer initialized.
        mInitialized = PR_FALSE;
@@ -814,7 +820,11 @@ nsDiskCacheDevice::SetCacheParentDirectory(nsILocalFile * parentDir)
{
    nsresult rv;
    PRBool  exists;
    NS_ASSERTION(mCacheDirectory == nsnull, "switching cache directories not supportted.");

    if (mInitialized) {
        NS_ASSERTION(PR_FALSE, "Cannot switch cache directory when initialized");
        return;
    }

    if (!parentDir) {
        mCacheDirectory = nsnull;
Loading