Bug 660768 modules should use mozilla::Preferences r=roc+joe+tglek
authorMasayuki Nakano <masayuki@d-toybox.com>
Sun, 12 Jun 2011 11:30:15 +0900
changeset 71442 3f771ec754bbc2666b59d57bad4aa05a0fd6b961
parent 71441 79394c301944bb391f8d34a7bc8bad1eceb7afee
child 71443 d5ce62d06d206d176092674829a203f0dc5fd4e1
push id159
push usereakhgari@mozilla.com
push dateTue, 16 Aug 2011 17:53:11 +0000
treeherdermozilla-beta@8786e3e49240 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersroc
bugs660768
milestone7.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 660768 modules should use mozilla::Preferences r=roc+joe+tglek
modules/libjar/nsJARChannel.cpp
modules/libpr0n/src/DiscardTracker.cpp
modules/libpr0n/src/imgLoader.cpp
modules/libpr0n/src/imgLoader.h
modules/libpr0n/src/imgRequest.cpp
--- a/modules/libjar/nsJARChannel.cpp
+++ b/modules/libjar/nsJARChannel.cpp
@@ -47,16 +47,20 @@
 #include "nsIPrefBranch.h"
 #include "nsIViewSourceChannel.h"
 #include "nsChannelProperties.h"
 
 #include "nsIScriptSecurityManager.h"
 #include "nsIPrincipal.h"
 #include "nsIFileURL.h"
 
+#include "mozilla/Preferences.h"
+
+using namespace mozilla;
+
 static NS_DEFINE_CID(kZipReaderCID, NS_ZIPREADER_CID);
 
 // the entry for a directory will either be empty (in the case of the
 // top-level directory) or will end with a slash
 #define ENTRY_IS_DIRECTORY(_entry) \
   ((_entry).IsEmpty() || '/' == (_entry).Last())
 
 //-----------------------------------------------------------------------------
@@ -834,28 +838,19 @@ nsJARChannel::OnDownloadComplete(nsIDown
             }
             // Soon-to-be common way to get Disposition: right now only nsIJARChannel
             rv = NS_GetContentDisposition(request, header);
             if (NS_SUCCEEDED(rv))
                 SetPropertyAsACString(NS_CHANNEL_PROP_CONTENT_DISPOSITION, header);
         }
     }
 
-    if (NS_SUCCEEDED(status) && mIsUnsafe) {
-        PRBool allowUnpack = PR_FALSE;
-
-        nsCOMPtr<nsIPrefBranch> prefs =
-            do_GetService(NS_PREFSERVICE_CONTRACTID);
-        if (prefs) {
-            prefs->GetBoolPref("network.jar.open-unsafe-types", &allowUnpack);
-        }
-
-        if (!allowUnpack) {
-            status = NS_ERROR_UNSAFE_CONTENT_TYPE;
-        }
+    if (NS_SUCCEEDED(status) && mIsUnsafe &&
+        !Preferences::GetBool("network.jar.open-unsafe-types", PR_FALSE)) {
+        status = NS_ERROR_UNSAFE_CONTENT_TYPE;
     }
 
     if (NS_SUCCEEDED(status)) {
         // Refuse to unpack view-source: jars even if open-unsafe-types is set.
         nsCOMPtr<nsIViewSourceChannel> viewSource = do_QueryInterface(channel);
         if (viewSource) {
             status = NS_ERROR_UNSAFE_CONTENT_TYPE;
         }
--- a/modules/libpr0n/src/DiscardTracker.cpp
+++ b/modules/libpr0n/src/DiscardTracker.cpp
@@ -30,24 +30,21 @@
  * use your version of this file under the terms of the MPL, indicate your
  * decision by deleting the provisions above and replace them with the notice
  * and other provisions required by the GPL or the LGPL. If you do not delete
  * the provisions above, a recipient may use your version of this file under
  * the terms of any one of the MPL, the GPL or the LGPL.
  *
  * ***** END LICENSE BLOCK ***** */
 
-#include "nsIServiceManager.h"
-#include "nsIPrefService.h"
-#include "nsIPrefBranch2.h"
-
 #include "nsComponentManagerUtils.h"
 #include "nsITimer.h"
 #include "RasterImage.h"
 #include "DiscardTracker.h"
+#include "mozilla/Preferences.h"
 
 namespace mozilla {
 namespace imagelib {
 
 static PRBool sInitialized = PR_FALSE;
 static PRBool sTimerOn = PR_FALSE;
 static PRUint32 sMinDiscardTimeoutMs = 10000; /* Default if pref unreadable. */
 static nsITimer *sTimer = nsnull;
@@ -169,22 +166,17 @@ DiscardTracker::Shutdown()
  */
 void
 DiscardTracker::ReloadTimeout()
 {
   nsresult rv;
 
   // read the timeout pref
   PRInt32 discardTimeout;
-  nsCOMPtr<nsIPrefBranch2> branch = do_GetService(NS_PREFSERVICE_CONTRACTID);
-  if (!branch) {
-    NS_WARNING("nsIPrefBranch2 is not available!");
-    return;
-  }
-  rv = branch->GetIntPref(DISCARD_TIMEOUT_PREF, &discardTimeout);
+  rv = Preferences::GetInt(DISCARD_TIMEOUT_PREF, &discardTimeout);
 
   // If we got something bogus, return
   if (!NS_SUCCEEDED(rv) || discardTimeout <= 0)
     return;
 
   // If the value didn't change, return
   if ((PRUint32) discardTimeout == sMinDiscardTimeoutMs)
     return;
--- a/modules/libpr0n/src/imgLoader.cpp
+++ b/modules/libpr0n/src/imgLoader.cpp
@@ -50,18 +50,16 @@
 #undef LoadImage
 
 #include "nsCOMPtr.h"
 
 #include "nsNetUtil.h"
 #include "nsIHttpChannel.h"
 #include "nsICachingChannel.h"
 #include "nsIInterfaceRequestor.h"
-#include "nsIPrefBranch2.h"
-#include "nsIPrefService.h"
 #include "nsIProgressEventSink.h"
 #include "nsIChannelEventSink.h"
 #include "nsIAsyncVerifyRedirectCallback.h"
 #include "nsIProxyObjectManager.h"
 #include "nsIServiceManager.h"
 #include "nsIFileURL.h"
 #include "nsThreadUtils.h"
 #include "nsXPIDLString.h"
@@ -85,17 +83,19 @@
 // we want to explore making the document own the load group
 // so we can associate the document URI with the load group.
 // until this point, we have an evil hack:
 #include "nsIHttpChannelInternal.h"  
 #include "nsIContentSecurityPolicy.h"
 #include "nsIChannelPolicy.h"
 
 #include "mozilla/FunctionTimer.h"
+#include "mozilla/Preferences.h"
 
+using namespace mozilla;
 using namespace mozilla::imagelib;
 
 #if defined(DEBUG_pavlov) || defined(DEBUG_timeless)
 #include "nsISimpleEnumerator.h"
 #include "nsXPCOM.h"
 #include "nsISupportsPrimitives.h"
 #include "nsXPIDLString.h"
 #include "nsComponentManagerUtils.h"
@@ -846,29 +846,25 @@ nsresult imgLoader::InitCache()
   if (!gCacheTracker)
     return NS_ERROR_OUT_OF_MEMORY;
 
   if (!sCache.Init())
       return NS_ERROR_OUT_OF_MEMORY;
   if (!sChromeCache.Init())
       return NS_ERROR_OUT_OF_MEMORY;
 
-  nsCOMPtr<nsIPrefBranch> prefs = do_GetService(NS_PREFSERVICE_CONTRACTID, &rv); 
-  if (NS_FAILED(rv))
-    return rv;
-
   PRInt32 timeweight;
-  rv = prefs->GetIntPref("image.cache.timeweight", &timeweight);
+  rv = Preferences::GetInt("image.cache.timeweight", &timeweight);
   if (NS_SUCCEEDED(rv))
     sCacheTimeWeight = timeweight / 1000.0;
   else
     sCacheTimeWeight = 0.5;
 
   PRInt32 cachesize;
-  rv = prefs->GetIntPref("image.cache.size", &cachesize);
+  rv = Preferences::GetInt("image.cache.size", &cachesize);
   if (NS_SUCCEEDED(rv))
     sCacheMaxSize = cachesize;
   else
     sCacheMaxSize = 5 * 1024 * 1024;
 
   NS_RegisterMemoryReporter(new imgMemoryReporter(imgMemoryReporter::ChromeUsedRaw));
   NS_RegisterMemoryReporter(new imgMemoryReporter(imgMemoryReporter::ChromeUsedUncompressed));
   NS_RegisterMemoryReporter(new imgMemoryReporter(imgMemoryReporter::ChromeUnusedRaw));
@@ -879,40 +875,35 @@ nsresult imgLoader::InitCache()
   NS_RegisterMemoryReporter(new imgMemoryReporter(imgMemoryReporter::ContentUnusedUncompressed));
   
   return NS_OK;
 }
 
 nsresult imgLoader::Init()
 {
   nsresult rv;
-  nsCOMPtr<nsIPrefBranch2> prefs = do_GetService(NS_PREFSERVICE_CONTRACTID, &rv); 
-  if (NS_FAILED(rv))
-    return rv;
+  ReadAcceptHeaderPref();
 
-  ReadAcceptHeaderPref(prefs);
-
-  prefs->AddObserver("image.http.accept", this, PR_TRUE);
+  Preferences::AddWeakObserver(this, "image.http.accept");
 
   // Listen for when we leave private browsing mode
   nsCOMPtr<nsIObserverService> obService = mozilla::services::GetObserverService();
   if (obService)
     obService->AddObserver(this, NS_PRIVATE_BROWSING_SWITCH_TOPIC, PR_TRUE);
 
   return NS_OK;
 }
 
 NS_IMETHODIMP
 imgLoader::Observe(nsISupports* aSubject, const char* aTopic, const PRUnichar* aData)
 {
   // We listen for pref change notifications...
   if (!strcmp(aTopic, NS_PREFBRANCH_PREFCHANGE_TOPIC_ID)) {
     if (!strcmp(NS_ConvertUTF16toUTF8(aData).get(), "image.http.accept")) {
-      nsCOMPtr<nsIPrefBranch> prefs = do_QueryInterface(aSubject);
-      ReadAcceptHeaderPref(prefs);
+      ReadAcceptHeaderPref();
     }
   }
 
   // ...and exits from private browsing.
   else if (!strcmp(aTopic, NS_PRIVATE_BROWSING_SWITCH_TOPIC)) {
     if (NS_LITERAL_STRING(NS_PRIVATE_BROWSING_LEAVE).Equals(aData))
       ClearImageCache();
   }
@@ -920,23 +911,20 @@ imgLoader::Observe(nsISupports* aSubject
   // (Nothing else should bring us here)
   else {
     NS_ABORT_IF_FALSE(0, "Invalid topic received");
   }
 
   return NS_OK;
 }
 
-void imgLoader::ReadAcceptHeaderPref(nsIPrefBranch *aBranch)
+void imgLoader::ReadAcceptHeaderPref()
 {
-  NS_ASSERTION(aBranch, "Pref branch is null");
-
-  nsXPIDLCString accept;
-  nsresult rv = aBranch->GetCharPref("image.http.accept", getter_Copies(accept));
-  if (NS_SUCCEEDED(rv))
+  nsAdoptingCString accept = Preferences::GetCString("image.http.accept");
+  if (accept)
     mAcceptHeader = accept;
   else
     mAcceptHeader = "image/png,image/*;q=0.8,*/*;q=0.5";
 }
 
 /* void clearCache (in boolean chrome); */
 NS_IMETHODIMP imgLoader::ClearCache(PRBool chrome)
 {
--- a/modules/libpr0n/src/imgLoader.h
+++ b/modules/libpr0n/src/imgLoader.h
@@ -57,17 +57,16 @@
 #include "prlock.h"
 #endif
 
 class imgRequest;
 class imgRequestProxy;
 class imgIRequest;
 class imgIDecoderObserver;
 class nsILoadGroup;
-class nsIPrefBranch;
 
 class imgCacheEntry
 {
 public:
   imgCacheEntry(imgRequest *request, PRBool mustValidateIfExpired = PR_FALSE);
   ~imgCacheEntry();
 
   nsrefcnt AddRef()
@@ -318,17 +317,17 @@ private: // methods
                                        imgIRequest **aProxyRequest,
                                        nsIChannelPolicy *aPolicy);
 
   nsresult CreateNewProxyForRequest(imgRequest *aRequest, nsILoadGroup *aLoadGroup,
                                     imgIDecoderObserver *aObserver,
                                     nsLoadFlags aLoadFlags, imgIRequest *aRequestProxy,
                                     imgIRequest **_retval);
 
-  void ReadAcceptHeaderPref(nsIPrefBranch *aBranch);
+  void ReadAcceptHeaderPref();
 
 
   typedef nsRefPtrHashtable<nsCStringHashKey, imgCacheEntry> imgCacheTable;
 
   static nsresult EvictEntries(imgCacheTable &aCacheToClear);
   static nsresult EvictEntries(imgCacheQueue &aQueueToClear);
 
   static imgCacheTable &GetCache(nsIURI *aURI);
--- a/modules/libpr0n/src/imgRequest.cpp
+++ b/modules/libpr0n/src/imgRequest.cpp
@@ -75,69 +75,73 @@
 #include "nsICacheVisitor.h"
 
 #include "nsString.h"
 #include "nsXPIDLString.h"
 #include "plstr.h" // PL_strcasestr(...)
 #include "nsNetUtil.h"
 #include "nsIProtocolHandler.h"
 
-#include "nsIPrefService.h"
-#include "nsIPrefBranch2.h"
+#include "mozilla/Preferences.h"
 
 #include "DiscardTracker.h"
 #include "nsAsyncRedirectVerifyHelper.h"
 
 #define DISCARD_PREF "image.mem.discardable"
 #define DECODEONDRAW_PREF "image.mem.decodeondraw"
 #define BYTESATATIME_PREF "image.mem.decode_bytes_at_a_time"
 #define MAXMS_PREF "image.mem.max_ms_before_yield"
 #define MAXBYTESFORSYNC_PREF "image.mem.max_bytes_for_sync_decode"
 #define SVG_MIMETYPE "image/svg+xml"
 
+using namespace mozilla;
 using namespace mozilla::imagelib;
 
 /* Kept up to date by a pref observer. */
 static PRBool gDecodeOnDraw = PR_FALSE;
 static PRBool gDiscardable = PR_FALSE;
 
+static const char* kObservedPrefs[] = {
+  DISCARD_PREF,
+  DECODEONDRAW_PREF,
+  DISCARD_TIMEOUT_PREF,
+  nsnull
+};
+
 /*
  * Pref observer goop. Yuck.
  */
 
 // Flag
 static PRBool gRegisteredPrefObserver = PR_FALSE;
 
 // Reloader
 static void
-ReloadPrefs(nsIPrefBranch *aBranch)
+ReloadPrefs()
 {
   // Discardable
-  PRBool discardable;
-  nsresult rv = aBranch->GetBoolPref(DISCARD_PREF, &discardable);
-  if (NS_SUCCEEDED(rv))
-    gDiscardable = discardable;
+  gDiscardable = Preferences::GetBool(DISCARD_PREF, gDiscardable);
 
   // Decode-on-draw
-  PRBool decodeondraw;
-  rv = aBranch->GetBoolPref(DECODEONDRAW_PREF, &decodeondraw);
-  if (NS_SUCCEEDED(rv))
-    gDecodeOnDraw = decodeondraw;
+  gDecodeOnDraw = Preferences::GetBool(DECODEONDRAW_PREF, gDecodeOnDraw);
 
   // Progressive decoding knobs
   PRInt32 bytesAtATime, maxMS, maxBytesForSync;
-  rv = aBranch->GetIntPref(BYTESATATIME_PREF, &bytesAtATime);
-  if (NS_SUCCEEDED(rv))
+  if (NS_SUCCEEDED(Preferences::GetInt(BYTESATATIME_PREF, &bytesAtATime))) {
     RasterImage::SetDecodeBytesAtATime(bytesAtATime);
-  rv = aBranch->GetIntPref(MAXMS_PREF, &maxMS);
-  if (NS_SUCCEEDED(rv))
+  }
+
+  if (NS_SUCCEEDED(Preferences::GetInt(MAXMS_PREF, &maxMS))) {
     RasterImage::SetMaxMSBeforeYield(maxMS);
-  rv = aBranch->GetIntPref(MAXBYTESFORSYNC_PREF, &maxBytesForSync);
-  if (NS_SUCCEEDED(rv))
+  }
+
+  if (NS_SUCCEEDED(Preferences::GetInt(MAXBYTESFORSYNC_PREF,
+                                       &maxBytesForSync))) {
     RasterImage::SetMaxBytesForSyncDecode(maxBytesForSync);
+  }
 
   // Discard timeout
   mozilla::imagelib::DiscardTracker::ReloadTimeout();
 }
 
 // Observer
 class imgRequestPrefObserver : public nsIObserver {
 public:
@@ -156,25 +160,18 @@ imgRequestPrefObserver::Observe(nsISuppo
   NS_ABORT_IF_FALSE(!strcmp(aTopic, NS_PREFBRANCH_PREFCHANGE_TOPIC_ID), "invalid topic");
 
   // Right pref
   if (strcmp(NS_LossyConvertUTF16toASCII(aData).get(), DISCARD_PREF) &&
       strcmp(NS_LossyConvertUTF16toASCII(aData).get(), DECODEONDRAW_PREF) &&
       strcmp(NS_LossyConvertUTF16toASCII(aData).get(), DISCARD_TIMEOUT_PREF))
     return NS_OK;
 
-  // Get the pref branch
-  nsCOMPtr<nsIPrefBranch> branch = do_QueryInterface(aSubject);
-  if (!branch) {
-    NS_WARNING("Couldn't get pref branch within imgRequestPrefObserver::Observe!");
-    return NS_OK;
-  }
-
   // Process the change
-  ReloadPrefs(branch);
+  ReloadPrefs();
 
   return NS_OK;
 }
 
 #if defined(PR_LOGGING)
 PRLogModuleInfo *gImgLog = PR_NewLogModule("imgRequest");
 #endif
 
@@ -238,25 +235,20 @@ nsresult imgRequest::Init(nsIURI *aURI,
   mCacheEntry = aCacheEntry;
 
   mCacheId = aCacheId;
 
   SetLoadId(aLoadId);
 
   // Register our pref observer if it hasn't been done yet.
   if (NS_UNLIKELY(!gRegisteredPrefObserver)) {
-    nsCOMPtr<nsIPrefBranch2> branch = do_GetService(NS_PREFSERVICE_CONTRACTID);
-    if (branch) {
-      nsCOMPtr<nsIObserver> observer(new imgRequestPrefObserver());
-      branch->AddObserver(DISCARD_PREF, observer, PR_FALSE);
-      branch->AddObserver(DECODEONDRAW_PREF, observer, PR_FALSE);
-      branch->AddObserver(DISCARD_TIMEOUT_PREF, observer, PR_FALSE);
-      ReloadPrefs(branch);
-      gRegisteredPrefObserver = PR_TRUE;
-    }
+    nsCOMPtr<nsIObserver> observer(new imgRequestPrefObserver());
+    Preferences::AddStrongObservers(observer, kObservedPrefs);
+    ReloadPrefs();
+    gRegisteredPrefObserver = PR_TRUE;
   }
 
   return NS_OK;
 }
 
 imgStatusTracker&
 imgRequest::GetStatusTracker()
 {