sbPrefBranch.h
Go to the documentation of this file.
1 /*
2 //
3 // BEGIN SONGBIRD GPL
4 //
5 // This file is part of the Songbird web player.
6 //
7 // Copyright(c) 2005-2008 POTI, Inc.
8 // http://songbirdnest.com
9 //
10 // This file may be licensed under the terms of of the
11 // GNU General Public License Version 2 (the "GPL").
12 //
13 // Software distributed under the License is distributed
14 // on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either
15 // express or implied. See the GPL for the specific language
16 // governing rights and limitations.
17 //
18 // You should have received a copy of the GPL along with this
19 // program. If not, go to http://www.gnu.org/licenses/gpl.html
20 // or write to the Free Software Foundation, Inc.,
21 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 //
23 // END SONGBIRD GPL
24 //
25 */
26 
27 #ifndef SBPREFBRANCH_H_
28 #define SBPREFBRANCH_H_
29 
30 #include <nsCOMPtr.h>
31 #include <nsThreadUtils.h>
32 #include <nsComponentManagerUtils.h>
33 
34 #include <nsIPrefBranch.h>
35 #include <nsIPrefService.h>
36 #include <nsIVariant.h>
37 
39 
46 {
47 public:
52  sbPrefBranch(const char* aRoot, nsresult* aResult) : mCreatingThread(PR_GetCurrentThread()) {
53  NS_ASSERTION(aResult != nsnull, "null nsresult* passed into sbPrefBranch constructor");
54  *aResult = NS_OK;
55 
56  // special bonus macro for passing our errors outwards
57 #define __ENSURE_SUCCESS(rv) \
58  PR_BEGIN_MACRO \
59  if (NS_FAILED(rv)) { \
60  *aResult = rv; \
61  NS_ENSURE_SUCCESS(rv, /*void */); \
62  } \
63  PR_END_MACRO
64 
65  nsresult rv;
66 
67  // get the prefs service
68  nsCOMPtr<nsIPrefService> prefService;
69  prefService = do_GetService(NS_PREFSERVICE_CONTRACTID, &rv);
70  __ENSURE_SUCCESS(rv);
71 
72  // If we're not on the main thread proxy the service
73  PRBool const isMainThread = NS_IsMainThread();
74  if (!isMainThread) {
75  nsCOMPtr<nsIPrefService> proxy;
76  rv = do_GetProxyForObject(NS_PROXY_TO_MAIN_THREAD,
77  NS_GET_IID(nsIPrefService),
78  prefService,
79  nsIProxyObjectManager::INVOKE_SYNC,
80  getter_AddRefs(proxy));
81  __ENSURE_SUCCESS(rv);
82  prefService.swap(proxy);
83  }
84 
85  if (aRoot) {
86  rv = prefService->GetBranch(aRoot, getter_AddRefs(mPrefBranch));
87  __ENSURE_SUCCESS(rv);
88  } else {
89  mPrefBranch = do_QueryInterface(prefService, &rv);
90  __ENSURE_SUCCESS(rv);
91  }
92 
93  // If we're not on the main thread, and we're not using the root
94  // then we need a proxy to the prefBranch too
95  if (!isMainThread && aRoot) {
96  nsCOMPtr<nsIPrefBranch> proxy;
97  rv = do_GetProxyForObject(NS_PROXY_TO_MAIN_THREAD,
98  NS_GET_IID(nsIPrefBranch),
99  mPrefBranch,
100  nsIProxyObjectManager::INVOKE_SYNC,
101  getter_AddRefs(proxy));
102  __ENSURE_SUCCESS(rv);
103  mPrefBranch.swap(proxy);
104  }
105 
106 #undef __ENSURE_SUCCESS
107  }
108 
109  PRBool GetBoolPref(const char* aKey, PRBool aDefault) {
110  NS_ASSERTION(PR_GetCurrentThread() == mCreatingThread, "sbPrefBranch used on multiple threads");
111  NS_ASSERTION(mPrefBranch, "mPrefBranch is null in sbPrefBranch");
112 
113  PRBool result;
114  nsresult rv = mPrefBranch->GetBoolPref(aKey, &result);
115  if (NS_FAILED(rv)) {
116  return aDefault;
117  }
118  return result;
119  }
120 
121  nsresult SetBoolPref(const char* aKey, PRBool aValue) {
122  NS_ASSERTION(PR_GetCurrentThread() == mCreatingThread, "sbPrefBranch used on multiple threads");
123  NS_ASSERTION(mPrefBranch, "mPrefBranch is null in sbPrefBranch");
124 
125  return mPrefBranch->SetBoolPref(aKey, aValue);
126  }
127 
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");
131 
132  PRInt32 result;
133  nsresult rv = mPrefBranch->GetIntPref(aKey, &result);
134  if (NS_FAILED(rv)) {
135  return aDefault;
136  }
137  return result;
138  }
139 
140  nsresult SetIntPref(const char* aKey, const PRInt32 aValue) {
141  NS_ASSERTION(PR_GetCurrentThread() == mCreatingThread, "sbPrefBranch used on multiple threads");
142  NS_ASSERTION(mPrefBranch, "mPrefBranch is null in sbPrefBranch");
143 
144  return mPrefBranch->SetIntPref(aKey, aValue);
145  }
146 
147 
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");
151 
152  char* result;
153  nsresult rv = mPrefBranch->GetCharPref(aKey, &result);
154  if (NS_FAILED(rv) || result == nsnull) {
155  return aDefault;
156  }
157  nsCString res;
158  res.Adopt(result);
159  return res;
160  }
161 
162  nsresult SetCharPref(const char* aKey, const nsCString& aValue) {
163  NS_ASSERTION(PR_GetCurrentThread() == mCreatingThread, "sbPrefBranch used on multiple threads");
164  NS_ASSERTION(mPrefBranch, "mPrefBranch is null in sbPrefBranch");
165 
166  return mPrefBranch->SetCharPref(aKey, aValue.get());
167  }
168 
169  /* Get a preference as an nsIVariant */
170  nsresult GetPreference(const nsAString & aPrefName, nsIVariant **_retval)
171  {
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);
177  nsresult rv;
178 
179  NS_LossyConvertUTF16toASCII prefNameASCII(aPrefName);
180 
181  // get tht type of the pref
182  PRInt32 prefType;
183  rv = mPrefBranch->GetPrefType(prefNameASCII.get(), &prefType);
184  NS_ENSURE_SUCCESS(rv, rv);
185 
186  // create a writable variant
187  nsCOMPtr<nsIWritableVariant> writableVariant =
188  do_CreateInstance("@songbirdnest.com/Songbird/Variant;1", &rv);
189  NS_ENSURE_SUCCESS(rv, rv);
190 
191  // get the value of our pref
192  switch (prefType) {
193  case nsIPrefBranch::PREF_INVALID: {
194  rv = writableVariant->SetAsEmpty();
195  NS_ENSURE_SUCCESS(rv, rv);
196  break;
197  }
198  case nsIPrefBranch::PREF_STRING: {
199  char* _value = NULL;
200  rv = mPrefBranch->GetCharPref(prefNameASCII.get(), &_value);
201  NS_ENSURE_SUCCESS(rv, rv);
202 
203  nsCString value;
204  value.Adopt(_value);
205 
206  // set the value of the variant to the value of the pref
207  rv = writableVariant->SetAsACString(value);
208  NS_ENSURE_SUCCESS(rv, rv);
209  break;
210  }
211  case nsIPrefBranch::PREF_INT: {
212  PRInt32 value;
213  rv = mPrefBranch->GetIntPref(prefNameASCII.get(), &value);
214  NS_ENSURE_SUCCESS(rv, rv);
215 
216  rv = writableVariant->SetAsInt32(value);
217  NS_ENSURE_SUCCESS(rv, rv);
218  break;
219  }
220  case nsIPrefBranch::PREF_BOOL: {
221  PRBool value;
222  rv = mPrefBranch->GetBoolPref(prefNameASCII.get(), &value);
223  NS_ENSURE_SUCCESS(rv, rv);
224 
225  rv = writableVariant->SetAsBool(value);
226  NS_ENSURE_SUCCESS(rv, rv);
227  break;
228  }
229  default: {
230  NS_NOTREACHED("Impossible pref type");
231  // And for release builds, do something sane.
232  rv = writableVariant->SetAsEmpty();
233  NS_ENSURE_SUCCESS(rv, rv);
234  break;
235  }
236  }
237  return CallQueryInterface(writableVariant, _retval);
238  }
239 
240 private:
241  nsCOMPtr<nsIPrefBranch> mPrefBranch;
242  PRThread * mCreatingThread;
243 };
244 
245 #endif /*SBPREFBRANCH_H_*/
return NS_OK
#define __ENSURE_SUCCESS(rv)
nsCString GetCharPref(const char *aKey, const nsCString &aDefault)
Definition: sbPrefBranch.h:148
onPageChanged aValue
Definition: FeedWriter.js:1395
const NS_PREFSERVICE_CONTRACTID
nsresult SetBoolPref(const char *aKey, PRBool aValue)
Definition: sbPrefBranch.h:121
const nsIPrefBranch
nsresult do_GetProxyForObject(nsIEventTarget *aTarget, REFNSIID aIID, nsISupports *aObj, PRInt32 aProxyType, void **aProxyObject)
nsresult GetPreference(const nsAString &aPrefName, nsIVariant **_retval)
Definition: sbPrefBranch.h:170
sbPrefBranch(const char *aRoot, nsresult *aResult)
Definition: sbPrefBranch.h:52
countRef value
Definition: FeedWriter.js:1423
nsresult SetCharPref(const char *aKey, const nsCString &aValue)
Definition: sbPrefBranch.h:162
nsresult SetIntPref(const char *aKey, const PRInt32 aValue)
Definition: sbPrefBranch.h:140
PRInt32 GetIntPref(const char *aKey, const PRInt32 aDefault)
Definition: sbPrefBranch.h:128
PRBool GetBoolPref(const char *aKey, PRBool aDefault)
Definition: sbPrefBranch.h:109