27 #ifndef SBPREFBRANCH_H_
28 #define SBPREFBRANCH_H_
31 #include <nsThreadUtils.h>
32 #include <nsComponentManagerUtils.h>
34 #include <nsIPrefBranch.h>
35 #include <nsIPrefService.h>
36 #include <nsIVariant.h>
52 sbPrefBranch(
const char* aRoot, nsresult* aResult) : mCreatingThread(PR_GetCurrentThread()) {
53 NS_ASSERTION(aResult != nsnull,
"null nsresult* passed into sbPrefBranch constructor");
57 #define __ENSURE_SUCCESS(rv) \
59 if (NS_FAILED(rv)) { \
61 NS_ENSURE_SUCCESS(rv, ); \
68 nsCOMPtr<nsIPrefService> prefService;
73 PRBool
const isMainThread = NS_IsMainThread();
75 nsCOMPtr<nsIPrefService> proxy;
77 NS_GET_IID(nsIPrefService),
79 nsIProxyObjectManager::INVOKE_SYNC,
80 getter_AddRefs(proxy));
82 prefService.swap(proxy);
86 rv = prefService->GetBranch(aRoot, getter_AddRefs(mPrefBranch));
89 mPrefBranch = do_QueryInterface(prefService, &rv);
95 if (!isMainThread && aRoot) {
96 nsCOMPtr<nsIPrefBranch> proxy;
100 nsIProxyObjectManager::INVOKE_SYNC,
101 getter_AddRefs(proxy));
103 mPrefBranch.swap(proxy);
106 #undef __ENSURE_SUCCESS
110 NS_ASSERTION(PR_GetCurrentThread() == mCreatingThread,
"sbPrefBranch used on multiple threads");
111 NS_ASSERTION(mPrefBranch,
"mPrefBranch is null in sbPrefBranch");
114 nsresult rv = mPrefBranch->GetBoolPref(aKey, &result);
122 NS_ASSERTION(PR_GetCurrentThread() == mCreatingThread,
"sbPrefBranch used on multiple threads");
123 NS_ASSERTION(mPrefBranch,
"mPrefBranch is null in sbPrefBranch");
125 return mPrefBranch->SetBoolPref(aKey, aValue);
128 PRInt32
GetIntPref(
const char* aKey,
const PRInt32 aDefault) {
129 NS_ASSERTION(PR_GetCurrentThread() == mCreatingThread,
"sbPrefBranch used on multiple threads");
130 NS_ASSERTION(mPrefBranch,
"mPrefBranch is null in sbPrefBranch");
133 nsresult rv = mPrefBranch->GetIntPref(aKey, &result);
141 NS_ASSERTION(PR_GetCurrentThread() == mCreatingThread,
"sbPrefBranch used on multiple threads");
142 NS_ASSERTION(mPrefBranch,
"mPrefBranch is null in sbPrefBranch");
144 return mPrefBranch->SetIntPref(aKey, aValue);
148 nsCString
GetCharPref(
const char* aKey,
const nsCString& aDefault) {
149 NS_ASSERTION(PR_GetCurrentThread() == mCreatingThread,
"sbPrefBranch used on multiple threads");
150 NS_ASSERTION(mPrefBranch,
"mPrefBranch is null in sbPrefBranch");
153 nsresult rv = mPrefBranch->GetCharPref(aKey, &result);
154 if (NS_FAILED(rv) || result == nsnull) {
163 NS_ASSERTION(PR_GetCurrentThread() == mCreatingThread,
"sbPrefBranch used on multiple threads");
164 NS_ASSERTION(mPrefBranch,
"mPrefBranch is null in sbPrefBranch");
166 return mPrefBranch->SetCharPref(aKey, aValue.get());
172 NS_ASSERTION(PR_GetCurrentThread() == mCreatingThread,
173 "sbPrefBranch used on multiple threads");
174 NS_ENSURE_STATE(mPrefBranch);
175 NS_ENSURE_ARG_POINTER(_retval);
176 NS_ENSURE_FALSE(aPrefName.IsEmpty(), NS_ERROR_INVALID_ARG);
179 NS_LossyConvertUTF16toASCII prefNameASCII(aPrefName);
183 rv = mPrefBranch->GetPrefType(prefNameASCII.get(), &prefType);
184 NS_ENSURE_SUCCESS(rv, rv);
187 nsCOMPtr<nsIWritableVariant> writableVariant =
188 do_CreateInstance(
"@songbirdnest.com/Songbird/Variant;1", &rv);
189 NS_ENSURE_SUCCESS(rv, rv);
193 case nsIPrefBranch::PREF_INVALID: {
194 rv = writableVariant->SetAsEmpty();
195 NS_ENSURE_SUCCESS(rv, rv);
198 case nsIPrefBranch::PREF_STRING: {
200 rv = mPrefBranch->GetCharPref(prefNameASCII.get(), &_value);
201 NS_ENSURE_SUCCESS(rv, rv);
207 rv = writableVariant->SetAsACString(value);
208 NS_ENSURE_SUCCESS(rv, rv);
211 case nsIPrefBranch::PREF_INT: {
213 rv = mPrefBranch->GetIntPref(prefNameASCII.get(), &
value);
214 NS_ENSURE_SUCCESS(rv, rv);
216 rv = writableVariant->SetAsInt32(value);
217 NS_ENSURE_SUCCESS(rv, rv);
220 case nsIPrefBranch::PREF_BOOL: {
222 rv = mPrefBranch->GetBoolPref(prefNameASCII.get(), &
value);
223 NS_ENSURE_SUCCESS(rv, rv);
225 rv = writableVariant->SetAsBool(value);
226 NS_ENSURE_SUCCESS(rv, rv);
230 NS_NOTREACHED(
"Impossible pref type");
232 rv = writableVariant->SetAsEmpty();
233 NS_ENSURE_SUCCESS(rv, rv);
237 return CallQueryInterface(writableVariant, _retval);
241 nsCOMPtr<nsIPrefBranch> mPrefBranch;
242 PRThread * mCreatingThread;
#define __ENSURE_SUCCESS(rv)
nsCString GetCharPref(const char *aKey, const nsCString &aDefault)
const NS_PREFSERVICE_CONTRACTID
nsresult SetBoolPref(const char *aKey, PRBool aValue)
nsresult do_GetProxyForObject(nsIEventTarget *aTarget, REFNSIID aIID, nsISupports *aObj, PRInt32 aProxyType, void **aProxyObject)
nsresult GetPreference(const nsAString &aPrefName, nsIVariant **_retval)
sbPrefBranch(const char *aRoot, nsresult *aResult)
nsresult SetCharPref(const char *aKey, const nsCString &aValue)
nsresult SetIntPref(const char *aKey, const PRInt32 aValue)
PRInt32 GetIntPref(const char *aKey, const PRInt32 aDefault)
PRBool GetBoolPref(const char *aKey, PRBool aDefault)