From 4053b81c11f26850111bb55a46cbb446c95c1c8b Mon Sep 17 00:00:00 2001 From: "suresh%netscape.com" Date: Mon, 27 Jan 2003 22:23:28 +0000 Subject: [PATCH] bug 166648 - allow link prefetching to be disabled via user prefs. r-dougt, sr-alecf, a-asa. --- uriloader/prefetch/nsPrefetchService.cpp | 68 ++++++++++++--- uriloader/prefetch/nsPrefetchService.h | 2 + .../resources/content/pref-cache.js | 8 +- .../resources/content/pref-cache.xul | 87 +++++++++---------- .../resources/locale/en-US/pref-cache.dtd | 20 +++-- 5 files changed, 108 insertions(+), 77 deletions(-) diff --git a/uriloader/prefetch/nsPrefetchService.cpp b/uriloader/prefetch/nsPrefetchService.cpp index 2e7ea59ecb1..7cf08c62b54 100644 --- a/uriloader/prefetch/nsPrefetchService.cpp +++ b/uriloader/prefetch/nsPrefetchService.cpp @@ -40,7 +40,7 @@ #include "nsICategoryManager.h" #include "nsIObserverService.h" #include "nsIPrefService.h" -#include "nsIPrefBranch.h" +#include "nsIPrefBranchInternal.h" #include "nsIDocCharset.h" #include "nsIWebProgress.h" #include "nsCURILoader.h" @@ -74,6 +74,8 @@ static PRLogModuleInfo *gPrefetchLog; static NS_DEFINE_IID(kDocLoaderServiceCID, NS_DOCUMENTLOADER_SERVICE_CID); static NS_DEFINE_IID(kPrefServiceCID, NS_PREFSERVICE_CID); +#define PREFETCH_PREF "network.prefetch-next" + //----------------------------------------------------------------------------- // helpers //----------------------------------------------------------------------------- @@ -203,7 +205,7 @@ nsPrefetchService::nsPrefetchService() : mQueueHead(nsnull) , mQueueTail(nsnull) , mStopCount(0) - , mDisabled(PR_FALSE) + , mDisabled(PR_TRUE) { } @@ -224,19 +226,20 @@ nsPrefetchService::Init() nsresult rv; - // Verify that "network.prefetch-next" preference is set to true. Skip - // this step if we encounter any errors. + // read prefs and hook up pref observer nsCOMPtr prefServ(do_GetService(kPrefServiceCID, &rv)); if (NS_SUCCEEDED(rv)) { nsCOMPtr prefs; rv = prefServ->GetBranch(nsnull, getter_AddRefs(prefs)); if (NS_SUCCEEDED(rv)) { PRBool enabled; - rv = prefs->GetBoolPref("network.prefetch-next", &enabled); - if (NS_SUCCEEDED(rv) && !enabled) { - LOG(("nsPrefetchService disabled via preferences\n")); - return NS_ERROR_ABORT; - } + rv = prefs->GetBoolPref(PREFETCH_PREF, &enabled); + if (NS_SUCCEEDED(rv) && enabled) + mDisabled = PR_FALSE; + + nsCOMPtr prefsInt(do_QueryInterface(prefs)); + if (prefsInt) + prefsInt->AddObserver(PREFETCH_PREF, this, PR_TRUE); } } @@ -247,12 +250,11 @@ nsPrefetchService::Init() rv = observerServ->AddObserver(this, NS_XPCOM_SHUTDOWN_OBSERVER_ID, PR_TRUE); if (NS_FAILED(rv)) return rv; - - // Register as an observer for the document loader - nsCOMPtr progress(do_GetService(kDocLoaderServiceCID, &rv)); - if (NS_FAILED(rv)) return rv; - return progress->AddProgressListener(this, nsIWebProgress::NOTIFY_STATE_DOCUMENT); + if (!mDisabled) + AddProgressListener(); + + return NS_OK; } void @@ -305,6 +307,24 @@ nsPrefetchService::ProcessNextURI() // nsPrefetchService //----------------------------------------------------------------------------- +void +nsPrefetchService::AddProgressListener() +{ + // Register as an observer for the document loader + nsCOMPtr progress(do_GetService(kDocLoaderServiceCID)); + if (progress) + progress->AddProgressListener(this, nsIWebProgress::NOTIFY_STATE_DOCUMENT); +} + +void +nsPrefetchService::RemoveProgressListener() +{ + // Register as an observer for the document loader + nsCOMPtr progress(do_GetService(kDocLoaderServiceCID)); + if (progress) + progress->RemoveProgressListener(this); +} + nsresult nsPrefetchService::EnqueueURI(nsIURI *aURI, nsIURI *aReferrerURI) { @@ -576,6 +596,26 @@ nsPrefetchService::Observe(nsISupports *aSubject, StopPrefetching(); mDisabled = PR_TRUE; } + else if (!strcmp(aTopic, NS_PREFBRANCH_PREFCHANGE_TOPIC_ID)) { + nsCOMPtr prefs(do_QueryInterface(aSubject)); + PRBool enabled; + nsresult rv = prefs->GetBoolPref(PREFETCH_PREF, &enabled); + if (NS_SUCCEEDED(rv) && enabled) { + if (mDisabled) { + LOG(("enabling prefetching\n")); + mDisabled = PR_FALSE; + AddProgressListener(); + } + } + else { + if (!mDisabled) { + LOG(("disabling prefetching\n")); + StopPrefetching(); + mDisabled = PR_TRUE; + RemoveProgressListener(); + } + } + } return NS_OK; } diff --git a/uriloader/prefetch/nsPrefetchService.h b/uriloader/prefetch/nsPrefetchService.h index acdba2b7d21..7476eeb03a2 100644 --- a/uriloader/prefetch/nsPrefetchService.h +++ b/uriloader/prefetch/nsPrefetchService.h @@ -75,6 +75,8 @@ public: private: + void AddProgressListener(); + void RemoveProgressListener(); nsresult EnqueueURI(nsIURI *aURI, nsIURI *aReferrerURI); nsresult DequeueURI(nsIURI **aURI, nsIURI **aReferrerURI); void EmptyQueue(); diff --git a/xpfe/components/prefwindow/resources/content/pref-cache.js b/xpfe/components/prefwindow/resources/content/pref-cache.js index 4c922c65b5c..aee42e9f9a3 100644 --- a/xpfe/components/prefwindow/resources/content/pref-cache.js +++ b/xpfe/components/prefwindow/resources/content/pref-cache.js @@ -89,12 +89,8 @@ function prefClearCache(aType) cacheService.evictEntries(aType); } -function prefClearMemCache() -{ - prefClearCache(Components.interfaces.nsICache.STORE_IN_MEMORY); -} - -function prefClearDiskCache() +function prefClearDiskAndMemCache() { prefClearCache(Components.interfaces.nsICache.STORE_ON_DISK); + prefClearCache(Components.interfaces.nsICache.STORE_IN_MEMORY); } diff --git a/xpfe/components/prefwindow/resources/content/pref-cache.xul b/xpfe/components/prefwindow/resources/content/pref-cache.xul index afffaa6c3d6..e1b6f0cc2db 100644 --- a/xpfe/components/prefwindow/resources/content/pref-cache.xul +++ b/xpfe/components/prefwindow/resources/content/pref-cache.xul @@ -37,7 +37,7 @@ @@ -50,62 +50,53 @@ &cachePara; - - - - - - - - - -