Skip to content

Commit 4899bba

Browse files
Bug 660768 modules should use mozilla::Preferences r=roc+joe+tglek
1 parent ef124e5 commit 4899bba

File tree

5 files changed

+46
-80
lines changed

5 files changed

+46
-80
lines changed

modules/libjar/nsJARChannel.cpp

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@
5252
#include "nsIPrincipal.h"
5353
#include "nsIFileURL.h"
5454

55+
#include "mozilla/Preferences.h"
56+
57+
using namespace mozilla;
58+
5559
static NS_DEFINE_CID(kZipReaderCID, NS_ZIPREADER_CID);
5660

5761
// the entry for a directory will either be empty (in the case of the
@@ -839,18 +843,9 @@ nsJARChannel::OnDownloadComplete(nsIDownloader *downloader,
839843
}
840844
}
841845

842-
if (NS_SUCCEEDED(status) && mIsUnsafe) {
843-
PRBool allowUnpack = PR_FALSE;
844-
845-
nsCOMPtr<nsIPrefBranch> prefs =
846-
do_GetService(NS_PREFSERVICE_CONTRACTID);
847-
if (prefs) {
848-
prefs->GetBoolPref("network.jar.open-unsafe-types", &allowUnpack);
849-
}
850-
851-
if (!allowUnpack) {
852-
status = NS_ERROR_UNSAFE_CONTENT_TYPE;
853-
}
846+
if (NS_SUCCEEDED(status) && mIsUnsafe &&
847+
!Preferences::GetBool("network.jar.open-unsafe-types", PR_FALSE)) {
848+
status = NS_ERROR_UNSAFE_CONTENT_TYPE;
854849
}
855850

856851
if (NS_SUCCEEDED(status)) {

modules/libpr0n/src/DiscardTracker.cpp

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,11 @@
3535
*
3636
* ***** END LICENSE BLOCK ***** */
3737

38-
#include "nsIServiceManager.h"
39-
#include "nsIPrefService.h"
40-
#include "nsIPrefBranch2.h"
41-
4238
#include "nsComponentManagerUtils.h"
4339
#include "nsITimer.h"
4440
#include "RasterImage.h"
4541
#include "DiscardTracker.h"
42+
#include "mozilla/Preferences.h"
4643

4744
namespace mozilla {
4845
namespace imagelib {
@@ -174,12 +171,7 @@ DiscardTracker::ReloadTimeout()
174171

175172
// read the timeout pref
176173
PRInt32 discardTimeout;
177-
nsCOMPtr<nsIPrefBranch2> branch = do_GetService(NS_PREFSERVICE_CONTRACTID);
178-
if (!branch) {
179-
NS_WARNING("nsIPrefBranch2 is not available!");
180-
return;
181-
}
182-
rv = branch->GetIntPref(DISCARD_TIMEOUT_PREF, &discardTimeout);
174+
rv = Preferences::GetInt(DISCARD_TIMEOUT_PREF, &discardTimeout);
183175

184176
// If we got something bogus, return
185177
if (!NS_SUCCEEDED(rv) || discardTimeout <= 0)

modules/libpr0n/src/imgLoader.cpp

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,6 @@
5555
#include "nsIHttpChannel.h"
5656
#include "nsICachingChannel.h"
5757
#include "nsIInterfaceRequestor.h"
58-
#include "nsIPrefBranch2.h"
59-
#include "nsIPrefService.h"
6058
#include "nsIProgressEventSink.h"
6159
#include "nsIChannelEventSink.h"
6260
#include "nsIAsyncVerifyRedirectCallback.h"
@@ -90,7 +88,9 @@
9088
#include "nsIChannelPolicy.h"
9189

9290
#include "mozilla/FunctionTimer.h"
91+
#include "mozilla/Preferences.h"
9392

93+
using namespace mozilla;
9494
using namespace mozilla::imagelib;
9595

9696
#if defined(DEBUG_pavlov) || defined(DEBUG_timeless)
@@ -851,19 +851,15 @@ nsresult imgLoader::InitCache()
851851
if (!sChromeCache.Init())
852852
return NS_ERROR_OUT_OF_MEMORY;
853853

854-
nsCOMPtr<nsIPrefBranch> prefs = do_GetService(NS_PREFSERVICE_CONTRACTID, &rv);
855-
if (NS_FAILED(rv))
856-
return rv;
857-
858854
PRInt32 timeweight;
859-
rv = prefs->GetIntPref("image.cache.timeweight", &timeweight);
855+
rv = Preferences::GetInt("image.cache.timeweight", &timeweight);
860856
if (NS_SUCCEEDED(rv))
861857
sCacheTimeWeight = timeweight / 1000.0;
862858
else
863859
sCacheTimeWeight = 0.5;
864860

865861
PRInt32 cachesize;
866-
rv = prefs->GetIntPref("image.cache.size", &cachesize);
862+
rv = Preferences::GetInt("image.cache.size", &cachesize);
867863
if (NS_SUCCEEDED(rv))
868864
sCacheMaxSize = cachesize;
869865
else
@@ -884,13 +880,9 @@ nsresult imgLoader::InitCache()
884880
nsresult imgLoader::Init()
885881
{
886882
nsresult rv;
887-
nsCOMPtr<nsIPrefBranch2> prefs = do_GetService(NS_PREFSERVICE_CONTRACTID, &rv);
888-
if (NS_FAILED(rv))
889-
return rv;
890-
891-
ReadAcceptHeaderPref(prefs);
883+
ReadAcceptHeaderPref();
892884

893-
prefs->AddObserver("image.http.accept", this, PR_TRUE);
885+
Preferences::AddWeakObserver(this, "image.http.accept");
894886

895887
// Listen for when we leave private browsing mode
896888
nsCOMPtr<nsIObserverService> obService = mozilla::services::GetObserverService();
@@ -906,8 +898,7 @@ imgLoader::Observe(nsISupports* aSubject, const char* aTopic, const PRUnichar* a
906898
// We listen for pref change notifications...
907899
if (!strcmp(aTopic, NS_PREFBRANCH_PREFCHANGE_TOPIC_ID)) {
908900
if (!strcmp(NS_ConvertUTF16toUTF8(aData).get(), "image.http.accept")) {
909-
nsCOMPtr<nsIPrefBranch> prefs = do_QueryInterface(aSubject);
910-
ReadAcceptHeaderPref(prefs);
901+
ReadAcceptHeaderPref();
911902
}
912903
}
913904

@@ -925,13 +916,10 @@ imgLoader::Observe(nsISupports* aSubject, const char* aTopic, const PRUnichar* a
925916
return NS_OK;
926917
}
927918

928-
void imgLoader::ReadAcceptHeaderPref(nsIPrefBranch *aBranch)
919+
void imgLoader::ReadAcceptHeaderPref()
929920
{
930-
NS_ASSERTION(aBranch, "Pref branch is null");
931-
932-
nsXPIDLCString accept;
933-
nsresult rv = aBranch->GetCharPref("image.http.accept", getter_Copies(accept));
934-
if (NS_SUCCEEDED(rv))
921+
nsAdoptingCString accept = Preferences::GetCString("image.http.accept");
922+
if (accept)
935923
mAcceptHeader = accept;
936924
else
937925
mAcceptHeader = "image/png,image/*;q=0.8,*/*;q=0.5";

modules/libpr0n/src/imgLoader.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@ class imgRequestProxy;
6262
class imgIRequest;
6363
class imgIDecoderObserver;
6464
class nsILoadGroup;
65-
class nsIPrefBranch;
6665

6766
class imgCacheEntry
6867
{
@@ -323,7 +322,7 @@ class imgLoader : public imgILoader,
323322
nsLoadFlags aLoadFlags, imgIRequest *aRequestProxy,
324323
imgIRequest **_retval);
325324

326-
void ReadAcceptHeaderPref(nsIPrefBranch *aBranch);
325+
void ReadAcceptHeaderPref();
327326

328327

329328
typedef nsRefPtrHashtable<nsCStringHashKey, imgCacheEntry> imgCacheTable;

modules/libpr0n/src/imgRequest.cpp

Lines changed: 26 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,7 @@
8080
#include "nsNetUtil.h"
8181
#include "nsIProtocolHandler.h"
8282

83-
#include "nsIPrefService.h"
84-
#include "nsIPrefBranch2.h"
83+
#include "mozilla/Preferences.h"
8584

8685
#include "DiscardTracker.h"
8786
#include "nsAsyncRedirectVerifyHelper.h"
@@ -93,12 +92,20 @@
9392
#define MAXBYTESFORSYNC_PREF "image.mem.max_bytes_for_sync_decode"
9493
#define SVG_MIMETYPE "image/svg+xml"
9594

95+
using namespace mozilla;
9696
using namespace mozilla::imagelib;
9797

9898
/* Kept up to date by a pref observer. */
9999
static PRBool gDecodeOnDraw = PR_FALSE;
100100
static PRBool gDiscardable = PR_FALSE;
101101

102+
static const char* kObservedPrefs[] = {
103+
DISCARD_PREF,
104+
DECODEONDRAW_PREF,
105+
DISCARD_TIMEOUT_PREF,
106+
nsnull
107+
};
108+
102109
/*
103110
* Pref observer goop. Yuck.
104111
*/
@@ -108,31 +115,28 @@ static PRBool gRegisteredPrefObserver = PR_FALSE;
108115

109116
// Reloader
110117
static void
111-
ReloadPrefs(nsIPrefBranch *aBranch)
118+
ReloadPrefs()
112119
{
113120
// Discardable
114-
PRBool discardable;
115-
nsresult rv = aBranch->GetBoolPref(DISCARD_PREF, &discardable);
116-
if (NS_SUCCEEDED(rv))
117-
gDiscardable = discardable;
121+
gDiscardable = Preferences::GetBool(DISCARD_PREF, gDiscardable);
118122

119123
// Decode-on-draw
120-
PRBool decodeondraw;
121-
rv = aBranch->GetBoolPref(DECODEONDRAW_PREF, &decodeondraw);
122-
if (NS_SUCCEEDED(rv))
123-
gDecodeOnDraw = decodeondraw;
124+
gDecodeOnDraw = Preferences::GetBool(DECODEONDRAW_PREF, gDecodeOnDraw);
124125

125126
// Progressive decoding knobs
126127
PRInt32 bytesAtATime, maxMS, maxBytesForSync;
127-
rv = aBranch->GetIntPref(BYTESATATIME_PREF, &bytesAtATime);
128-
if (NS_SUCCEEDED(rv))
128+
if (NS_SUCCEEDED(Preferences::GetInt(BYTESATATIME_PREF, &bytesAtATime))) {
129129
RasterImage::SetDecodeBytesAtATime(bytesAtATime);
130-
rv = aBranch->GetIntPref(MAXMS_PREF, &maxMS);
131-
if (NS_SUCCEEDED(rv))
130+
}
131+
132+
if (NS_SUCCEEDED(Preferences::GetInt(MAXMS_PREF, &maxMS))) {
132133
RasterImage::SetMaxMSBeforeYield(maxMS);
133-
rv = aBranch->GetIntPref(MAXBYTESFORSYNC_PREF, &maxBytesForSync);
134-
if (NS_SUCCEEDED(rv))
134+
}
135+
136+
if (NS_SUCCEEDED(Preferences::GetInt(MAXBYTESFORSYNC_PREF,
137+
&maxBytesForSync))) {
135138
RasterImage::SetMaxBytesForSyncDecode(maxBytesForSync);
139+
}
136140

137141
// Discard timeout
138142
mozilla::imagelib::DiscardTracker::ReloadTimeout();
@@ -161,15 +165,8 @@ imgRequestPrefObserver::Observe(nsISupports *aSubject,
161165
strcmp(NS_LossyConvertUTF16toASCII(aData).get(), DISCARD_TIMEOUT_PREF))
162166
return NS_OK;
163167

164-
// Get the pref branch
165-
nsCOMPtr<nsIPrefBranch> branch = do_QueryInterface(aSubject);
166-
if (!branch) {
167-
NS_WARNING("Couldn't get pref branch within imgRequestPrefObserver::Observe!");
168-
return NS_OK;
169-
}
170-
171168
// Process the change
172-
ReloadPrefs(branch);
169+
ReloadPrefs();
173170

174171
return NS_OK;
175172
}
@@ -243,15 +240,10 @@ nsresult imgRequest::Init(nsIURI *aURI,
243240

244241
// Register our pref observer if it hasn't been done yet.
245242
if (NS_UNLIKELY(!gRegisteredPrefObserver)) {
246-
nsCOMPtr<nsIPrefBranch2> branch = do_GetService(NS_PREFSERVICE_CONTRACTID);
247-
if (branch) {
248-
nsCOMPtr<nsIObserver> observer(new imgRequestPrefObserver());
249-
branch->AddObserver(DISCARD_PREF, observer, PR_FALSE);
250-
branch->AddObserver(DECODEONDRAW_PREF, observer, PR_FALSE);
251-
branch->AddObserver(DISCARD_TIMEOUT_PREF, observer, PR_FALSE);
252-
ReloadPrefs(branch);
253-
gRegisteredPrefObserver = PR_TRUE;
254-
}
243+
nsCOMPtr<nsIObserver> observer(new imgRequestPrefObserver());
244+
Preferences::AddStrongObservers(observer, kObservedPrefs);
245+
ReloadPrefs();
246+
gRegisteredPrefObserver = PR_TRUE;
255247
}
256248

257249
return NS_OK;

0 commit comments

Comments
 (0)