b=450088 backing out (new reftest failed)
[wine-gecko.git] / modules / libpref / src / nsPref.cpp
blob938c0288babd904ec3c6f3e704cb92d501bd4150
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
3 * ***** BEGIN LICENSE BLOCK *****
4 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
6 * The contents of this file are subject to the Mozilla Public License Version
7 * 1.1 (the "License"); you may not use this file except in compliance with
8 * the License. You may obtain a copy of the License at
9 * http://www.mozilla.org/MPL/
11 * Software distributed under the License is distributed on an "AS IS" basis,
12 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 * for the specific language governing rights and limitations under the
14 * License.
16 * The Original Code is mozilla.org code.
18 * The Initial Developer of the Original Code is
19 * Netscape Communications Corporation.
20 * Portions created by the Initial Developer are Copyright (C) 1998
21 * the Initial Developer. All Rights Reserved.
23 * Contributor(s):
24 * Pierre Phaneuf <pp@ludusdesign.com>
26 * Alternatively, the contents of this file may be used under the terms of
27 * either the GNU General Public License Version 2 or later (the "GPL"), or
28 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
29 * in which case the provisions of the GPL or the LGPL are applicable instead
30 * of those above. If you wish to allow use of your version of this file only
31 * under the terms of either the GPL or the LGPL, and not to allow others to
32 * use your version of this file under the terms of the MPL, indicate your
33 * decision by deleting the provisions above and replace them with the notice
34 * and other provisions required by the GPL or the LGPL. If you do not delete
35 * the provisions above, a recipient may use your version of this file under
36 * the terms of any one of the MPL, the GPL or the LGPL.
38 * ***** END LICENSE BLOCK *****
40 * This Original Code has been modified by IBM Corporation.
41 * Modifications made by IBM described herein are
42 * Copyright (c) International Business Machines
43 * Corporation, 2000
45 * Modifications to Mozilla code or documentation
46 * identified per MPL Section 3.3
48 * Date Modified by Description of modification
49 * 03/27/2000 IBM Corp. Added PR_CALLBACK for Optlink
50 * use in OS2
53 #include "nsIPref.h"
54 #include "nsIPrefBranch.h"
55 #include "nsIPrefBranchInternal.h"
56 #include "nsIFactory.h"
57 #include "nsIComponentManager.h"
58 #include "nsIObserver.h"
59 #include "nsCOMPtr.h"
60 #include "nsMemory.h"
61 #include "prefapi.h"
63 #ifndef MOZ_NO_XPCOM_OBSOLETE
64 #include "nsIFileSpec.h"
65 #endif
67 #include "nsString.h"
68 #include "nsILocalFile.h"
69 #include "nsIPrefBranch.h"
70 #include "nsIPrefLocalizedString.h"
71 #include "nsISecurityPref.h"
72 #include "nsIPrefService.h"
73 #include "nsISupportsPrimitives.h"
74 #include "nsWeakReference.h"
76 #include "nsIModule.h"
77 #include "nsIGenericFactory.h"
79 #include "plstr.h"
80 #include "prmem.h"
81 #include "prprf.h"
84 class nsPref : public nsIPref,
85 public nsIPrefService,
86 public nsIObserver,
87 public nsIPrefBranchInternal,
88 public nsISecurityPref,
89 public nsSupportsWeakReference
91 public:
92 static nsPref *GetInstance();
94 /* Use xpidl-generated macro to declare everything required by nsIPref */
95 NS_DECL_ISUPPORTS
96 NS_DECL_NSIPREFBRANCH
97 NS_DECL_NSIPREFBRANCH2
98 NS_DECL_NSISECURITYPREF
99 NS_DECL_NSIOBSERVER
100 NS_FORWARD_NSIPREFSERVICE(mPrefService->)
102 NS_IMETHOD CopyCharPref(const char *pref, char ** return_buf);
104 NS_IMETHODIMP GetDefaultBoolPref(const char *pref, PRBool *_retval)
105 { return mDefaultBranch->GetBoolPref(pref, _retval); }
106 NS_IMETHODIMP CopyDefaultCharPref(const char *pref, char **_retval)
107 { return mDefaultBranch->GetCharPref(pref, _retval); }
108 NS_IMETHODIMP GetDefaultIntPref(const char *pref, PRInt32 *_retval)
109 { return mDefaultBranch->GetIntPref(pref, _retval); }
110 NS_IMETHODIMP SetDefaultBoolPref(const char *pref, PRBool value)
111 { return mDefaultBranch->SetBoolPref(pref, value); }
112 NS_IMETHODIMP SetDefaultCharPref(const char *pref, const char *value)
113 { return mDefaultBranch->SetCharPref(pref, value); }
114 NS_IMETHODIMP SetDefaultIntPref(const char *pref, PRInt32 value)
115 { return mDefaultBranch->SetIntPref(pref, value); }
117 NS_IMETHOD CopyUnicharPref(const char *pref, PRUnichar **_retval);
118 NS_IMETHOD CopyDefaultUnicharPref(const char *pref, PRUnichar **_retval);
119 NS_IMETHOD SetUnicharPref(const char *pref, const PRUnichar *value);
120 NS_IMETHOD SetDefaultUnicharPref(const char *pref, const PRUnichar *value);
121 NS_IMETHOD GetLocalizedUnicharPref(const char *pref, PRUnichar **_retval);
122 NS_IMETHOD GetDefaultLocalizedUnicharPref(const char *pref, PRUnichar **_retval);
124 NS_IMETHOD GetFilePref(const char *pref, nsIFileSpec **_retval);
125 NS_IMETHOD SetFilePref(const char *pref, nsIFileSpec *value, PRBool setDefault);
126 NS_IMETHOD GetFileXPref(const char *pref, nsILocalFile **_retval);
127 NS_IMETHOD SetFileXPref(const char *pref, nsILocalFile *value);
129 NS_IMETHOD RegisterCallback(const char *domain, PrefChangedFunc callback, void * closure);
130 NS_IMETHOD UnregisterCallback(const char *domain, PrefChangedFunc callback, void * closure);
131 NS_IMETHOD EnumerateChildren(const char *parent, PrefEnumerationFunc callback, void * data);
133 protected:
134 nsPref();
135 virtual ~nsPref();
137 static nsPref *gInstance;
139 private:
140 nsCOMPtr<nsIPrefService> mPrefService;
141 nsCOMPtr<nsIPrefBranch> mDefaultBranch;
144 nsPref* nsPref::gInstance = NULL;
145 static PRInt32 g_InstanceCount = 0;
148 NS_IMPL_THREADSAFE_ISUPPORTS8(nsPref,
149 nsIPref,
150 nsIPrefService,
151 nsIObserver,
152 nsIPrefBranch,
153 nsIPrefBranch2,
154 nsIPrefBranchInternal,
155 nsISecurityPref,
156 nsISupportsWeakReference)
158 //----------------------------------------------------------------------------------------
159 nsPref::nsPref()
160 //----------------------------------------------------------------------------------------
162 PR_AtomicIncrement(&g_InstanceCount);
164 mPrefService = do_GetService(NS_PREFSERVICE_CONTRACTID);
165 NS_ASSERTION(mPrefService, "Preference Service failed to start up!!");
167 if (mPrefService)
168 mPrefService->GetDefaultBranch("", getter_AddRefs(mDefaultBranch));
171 //----------------------------------------------------------------------------------------
172 nsPref::~nsPref()
173 //----------------------------------------------------------------------------------------
175 PR_AtomicDecrement(&g_InstanceCount);
176 gInstance = NULL;
180 * Implementations to pass branch calls through the PrefService.
183 NS_IMETHODIMP nsPref::GetRoot(char * *aRoot)
185 nsresult rv;
187 nsCOMPtr<nsIPrefBranch> prefBranch = do_QueryInterface(mPrefService, &rv);
188 if (NS_SUCCEEDED(rv))
189 rv = prefBranch->GetRoot(aRoot);
190 return rv;
193 NS_IMETHODIMP nsPref::GetPrefType(const char *aPrefName, PRInt32 *_retval)
195 nsresult rv;
197 nsCOMPtr<nsIPrefBranch> prefBranch = do_QueryInterface(mPrefService, &rv);
198 if (NS_SUCCEEDED(rv))
199 rv = prefBranch->GetPrefType(aPrefName, _retval);
200 return rv;
203 NS_IMETHODIMP nsPref::CopyCharPref(const char *pref, char ** return_buf)
205 nsresult rv;
207 nsCOMPtr<nsIPrefBranch> prefBranch = do_QueryInterface(mPrefService, &rv);
208 if (NS_SUCCEEDED(rv))
209 rv = prefBranch->GetCharPref(pref, return_buf);
210 return rv;
213 NS_IMETHODIMP nsPref::GetBoolPref(const char *aPrefName, PRBool *_retval)
215 nsresult rv;
217 nsCOMPtr<nsIPrefBranch> prefBranch = do_QueryInterface(mPrefService, &rv);
218 if (NS_SUCCEEDED(rv))
219 rv = prefBranch->GetBoolPref(aPrefName, _retval);
220 return rv;
223 NS_IMETHODIMP nsPref::SetBoolPref(const char *aPrefName, PRInt32 aValue)
225 nsresult rv;
227 nsCOMPtr<nsIPrefBranch> prefBranch = do_QueryInterface(mPrefService, &rv);
228 if (NS_SUCCEEDED(rv))
229 rv = prefBranch->SetBoolPref(aPrefName, aValue);
230 return rv;
233 NS_IMETHODIMP nsPref::GetCharPref(const char *aPrefName, char **_retval)
235 nsresult rv;
237 nsCOMPtr<nsIPrefBranch> prefBranch = do_QueryInterface(mPrefService, &rv);
238 if (NS_SUCCEEDED(rv))
239 rv = prefBranch->GetCharPref(aPrefName, _retval);
240 return rv;
243 NS_IMETHODIMP nsPref::SetCharPref(const char *aPrefName, const char *aValue)
245 nsresult rv;
247 nsCOMPtr<nsIPrefBranch> prefBranch = do_QueryInterface(mPrefService, &rv);
248 if (NS_SUCCEEDED(rv))
249 rv = prefBranch->SetCharPref(aPrefName, aValue);
250 return rv;
253 NS_IMETHODIMP nsPref::GetIntPref(const char *aPrefName, PRInt32 *_retval)
255 nsresult rv;
257 nsCOMPtr<nsIPrefBranch> prefBranch = do_QueryInterface(mPrefService, &rv);
258 if (NS_SUCCEEDED(rv))
259 rv = prefBranch->GetIntPref(aPrefName, _retval);
260 return rv;
263 NS_IMETHODIMP nsPref::SetIntPref(const char *aPrefName, PRInt32 aValue)
265 nsresult rv;
267 nsCOMPtr<nsIPrefBranch> prefBranch = do_QueryInterface(mPrefService, &rv);
268 if (NS_SUCCEEDED(rv))
269 rv = prefBranch->SetIntPref(aPrefName, aValue);
270 return rv;
273 NS_IMETHODIMP nsPref::GetComplexValue(const char *aPrefName, const nsIID & aType, void * *aValue)
275 nsresult rv;
277 nsCOMPtr<nsIPrefBranch> prefBranch = do_QueryInterface(mPrefService, &rv);
278 if (NS_SUCCEEDED(rv))
279 rv = prefBranch->GetComplexValue(aPrefName, aType, aValue);
280 return rv;
283 NS_IMETHODIMP nsPref::SetComplexValue(const char *aPrefName, const nsIID & aType, nsISupports *aValue)
285 nsresult rv;
287 nsCOMPtr<nsIPrefBranch> prefBranch = do_QueryInterface(mPrefService, &rv);
288 if (NS_SUCCEEDED(rv))
289 rv = prefBranch->SetComplexValue(aPrefName, aType, aValue);
290 return rv;
293 NS_IMETHODIMP nsPref::ClearUserPref(const char *aPrefName)
295 nsresult rv;
297 nsCOMPtr<nsIPrefBranch> prefBranch = do_QueryInterface(mPrefService, &rv);
298 if (NS_SUCCEEDED(rv))
299 rv = prefBranch->ClearUserPref(aPrefName);
300 return rv;
303 NS_IMETHODIMP nsPref::LockPref(const char *aPrefName)
305 nsresult rv;
307 nsCOMPtr<nsIPrefBranch> prefBranch = do_QueryInterface(mPrefService, &rv);
308 if (NS_SUCCEEDED(rv))
309 rv = prefBranch->LockPref(aPrefName);
310 return rv;
313 NS_IMETHODIMP nsPref::PrefIsLocked(const char *aPrefName, PRBool *_retval)
315 nsresult rv;
317 nsCOMPtr<nsIPrefBranch> prefBranch = do_QueryInterface(mPrefService, &rv);
318 if (NS_SUCCEEDED(rv))
319 rv = prefBranch->PrefIsLocked(aPrefName, _retval);
320 return rv;
323 NS_IMETHODIMP nsPref::PrefHasUserValue(const char *aPrefName, PRBool *_retval)
325 nsresult rv;
327 nsCOMPtr<nsIPrefBranch> prefBranch = do_QueryInterface(mPrefService, &rv);
328 if (NS_SUCCEEDED(rv))
329 rv = prefBranch->PrefHasUserValue(aPrefName, _retval);
330 return rv;
333 NS_IMETHODIMP nsPref::UnlockPref(const char *aPrefName)
335 nsresult rv;
337 nsCOMPtr<nsIPrefBranch> prefBranch = do_QueryInterface(mPrefService, &rv);
338 if (NS_SUCCEEDED(rv))
339 rv = prefBranch->UnlockPref(aPrefName);
340 return rv;
343 NS_IMETHODIMP nsPref::ResetBranch(const char *aStartingAt)
345 nsresult rv;
347 nsCOMPtr<nsIPrefBranch> prefBranch = do_QueryInterface(mPrefService, &rv);
348 if (NS_SUCCEEDED(rv))
349 rv = prefBranch->ResetBranch(aStartingAt);
350 return rv;
353 NS_IMETHODIMP nsPref::DeleteBranch(const char *aStartingAt)
355 nsresult rv;
357 nsCOMPtr<nsIPrefBranch> prefBranch = do_QueryInterface(mPrefService, &rv);
358 if (NS_SUCCEEDED(rv))
359 rv = prefBranch->DeleteBranch(aStartingAt);
360 return rv;
363 NS_IMETHODIMP nsPref::GetChildList(const char *aStartingAt, PRUint32 *aCount, char ***aChildArray)
365 nsresult rv;
367 nsCOMPtr<nsIPrefBranch> prefBranch = do_QueryInterface(mPrefService, &rv);
368 if (NS_SUCCEEDED(rv))
369 rv = prefBranch->GetChildList(aStartingAt, aCount, aChildArray);
370 return rv;
373 NS_IMETHODIMP nsPref::AddObserver(const char *aDomain, nsIObserver *aObserver, PRBool aHoldWeak)
375 nsresult rv;
377 nsCOMPtr<nsIPrefBranch2> prefBranch = do_QueryInterface(mPrefService, &rv);
378 if (NS_SUCCEEDED(rv))
379 rv = prefBranch->AddObserver(aDomain, aObserver, aHoldWeak);
380 return rv;
383 NS_IMETHODIMP nsPref::RemoveObserver(const char *aDomain, nsIObserver *aObserver)
385 nsresult rv;
387 nsCOMPtr<nsIPrefBranch2> prefBranch = do_QueryInterface(mPrefService, &rv);
388 if (NS_SUCCEEDED(rv))
389 rv = prefBranch->RemoveObserver(aDomain, aObserver);
390 return rv;
393 NS_IMETHODIMP nsPref::Observe(nsISupports *aSubject, const char *aTopic, const PRUnichar *someData)
395 nsresult rv;
397 nsCOMPtr<nsIObserver> observer = do_QueryInterface(mPrefService, &rv);
398 if (NS_SUCCEEDED(rv))
399 rv = observer->Observe(aSubject, aTopic, someData);
400 return rv;
405 * Some temporary support for deprecated functions.
409 * Items replaced by Get/SetComplexValue
412 NS_IMETHODIMP nsPref::CopyUnicharPref(const char *pref, PRUnichar **_retval)
414 nsresult rv;
416 nsCOMPtr<nsIPrefBranch> prefBranch = do_QueryInterface(mPrefService, &rv);
417 if (NS_SUCCEEDED(rv)) {
418 nsCOMPtr<nsISupportsString> theString;
419 rv = prefBranch->GetComplexValue(pref, NS_GET_IID(nsISupportsString),
420 getter_AddRefs(theString));
421 if (NS_FAILED(rv))
422 return rv;
424 return theString->ToString(_retval);
426 return rv;
429 NS_IMETHODIMP nsPref::CopyDefaultUnicharPref(const char *pref, PRUnichar **_retval)
431 nsresult rv;
432 nsCOMPtr<nsISupportsString> theString;
434 rv = mDefaultBranch->GetComplexValue(pref, NS_GET_IID(nsISupportsString),
435 getter_AddRefs(theString));
436 if (NS_FAILED(rv))
437 return rv;
439 return theString->ToString(_retval);
442 NS_IMETHODIMP nsPref::SetUnicharPref(const char *pref, const PRUnichar *value)
444 nsresult rv;
446 nsCOMPtr<nsIPrefBranch> prefBranch = do_QueryInterface(mPrefService, &rv);
447 if (NS_SUCCEEDED(rv)) {
448 nsCOMPtr<nsISupportsString> theString = do_CreateInstance(NS_SUPPORTS_STRING_CONTRACTID, &rv);
449 if (NS_SUCCEEDED(rv)) {
450 theString->SetData(nsDependentString(value));
451 rv = prefBranch->SetComplexValue(pref, NS_GET_IID(nsISupportsString), theString);
454 return rv;
457 NS_IMETHODIMP nsPref::SetDefaultUnicharPref(const char *pref, const PRUnichar *value)
459 nsresult rv;
461 nsCOMPtr<nsISupportsString> theString = do_CreateInstance(NS_SUPPORTS_STRING_CONTRACTID, &rv);
462 if (NS_SUCCEEDED(rv)) {
463 theString->SetData(nsDependentString(value));
464 rv = mDefaultBranch->SetComplexValue(pref, NS_GET_IID(nsISupportsString), theString);
466 return rv;
469 NS_IMETHODIMP nsPref::GetLocalizedUnicharPref(const char *pref, PRUnichar **_retval)
471 nsresult rv;
473 nsCOMPtr<nsIPrefBranch> prefBranch = do_QueryInterface(mPrefService, &rv);
474 if (NS_SUCCEEDED(rv)) {
475 nsCOMPtr<nsIPrefLocalizedString> theString;
476 rv = prefBranch->GetComplexValue(pref, NS_GET_IID(nsIPrefLocalizedString),
477 getter_AddRefs(theString));
478 if (NS_SUCCEEDED(rv)) {
479 rv = theString->ToString(_retval);
482 return rv;
485 NS_IMETHODIMP nsPref::GetDefaultLocalizedUnicharPref(const char *pref, PRUnichar **_retval)
487 nsresult rv;
488 nsCOMPtr<nsIPrefLocalizedString> theString;
490 rv = mDefaultBranch->GetComplexValue(pref, NS_GET_IID(nsIPrefLocalizedString),
491 getter_AddRefs(theString));
492 if (NS_SUCCEEDED(rv)) {
493 rv = theString->ToString(_retval);
496 return rv;
499 NS_IMETHODIMP nsPref::GetFilePref(const char *pref, nsIFileSpec **_retval)
501 #ifdef MOZ_NO_XPCOM_OBSOLETE
502 return NS_ERROR_NOT_IMPLEMENTED;
503 #else
504 nsresult rv;
506 nsCOMPtr<nsIPrefBranch> prefBranch = do_QueryInterface(mPrefService, &rv);
507 if (NS_SUCCEEDED(rv))
508 rv = prefBranch->GetComplexValue(pref, NS_GET_IID(nsIFileSpec), (void **)_retval);
509 return rv;
510 #endif
513 NS_IMETHODIMP nsPref::SetFilePref(const char *pref, nsIFileSpec *value, PRBool setDefault)
515 #ifdef MOZ_NO_XPCOM_OBSOLETE
516 return NS_ERROR_NOT_IMPLEMENTED;
517 #else
518 nsresult rv;
520 if (setDefault) {
521 rv = mDefaultBranch->SetComplexValue(pref, NS_GET_IID(nsIFileSpec), value);
522 } else {
523 nsCOMPtr<nsIPrefBranch> prefBranch = do_QueryInterface(mPrefService, &rv);
524 if (NS_SUCCEEDED(rv))
525 rv = prefBranch->SetComplexValue(pref, NS_GET_IID(nsIFileSpec), value);
527 return rv;
528 #endif
531 NS_IMETHODIMP nsPref::GetFileXPref(const char *pref, nsILocalFile **_retval)
533 nsresult rv;
535 nsCOMPtr<nsIPrefBranch> prefBranch = do_QueryInterface(mPrefService, &rv);
536 if (NS_SUCCEEDED(rv))
537 rv = prefBranch->GetComplexValue(pref, NS_GET_IID(nsILocalFile), (void **)_retval);
538 return rv;
541 NS_IMETHODIMP nsPref::SetFileXPref(const char *pref, nsILocalFile *value)
543 nsresult rv;
545 NS_ENSURE_ARG_POINTER(value);
546 nsCOMPtr<nsIPrefBranch> prefBranch = do_QueryInterface(mPrefService, &rv);
547 if (NS_SUCCEEDED(rv))
548 rv = prefBranch->SetComplexValue(pref, NS_GET_IID(nsILocalFile), value);
549 return rv;
554 * Callbacks
557 //----------------------------------------------------------------------------------------
558 NS_IMETHODIMP nsPref::RegisterCallback( const char* domain,
559 PrefChangedFunc callback,
560 void* instance_data )
561 //----------------------------------------------------------------------------------------
563 PREF_RegisterCallback(domain, callback, instance_data);
564 return NS_OK;
567 //----------------------------------------------------------------------------------------
568 NS_IMETHODIMP nsPref::UnregisterCallback( const char* domain,
569 PrefChangedFunc callback,
570 void* instance_data )
571 //----------------------------------------------------------------------------------------
573 return PREF_UnregisterCallback(domain, callback, instance_data);
577 * Preference enumeration
580 NS_IMETHODIMP nsPref::EnumerateChildren(const char *parent, PrefEnumerationFunc callback, void *arg)
582 PRUint32 theCount;
583 PRUint32 i;
584 nsresult rv;
585 char **childArray;
586 char *prefName;
588 rv = GetChildList(parent, &theCount, &childArray);
589 if (NS_SUCCEEDED(rv)) {
590 // now that we've built up the list, run the callback on all the matching elements
591 for (i = 0; i < theCount; i++) {
592 prefName = (char *)childArray[i];
593 (*callback)((char*)prefName, arg);
596 // now release all the memory
597 NS_FREE_XPCOM_ALLOCATED_POINTER_ARRAY(theCount, childArray);
600 return NS_OK;
604 * Pref access without security check - these are here
605 * to support nsScriptSecurityManager.
606 * These functions are part of nsISecurityPref, not nsIPref.
607 * **PLEASE** do not call these functions from elsewhere
609 NS_IMETHODIMP nsPref::SecurityGetBoolPref(const char *pref, PRBool * return_val)
611 nsresult rv;
613 nsCOMPtr<nsIPrefBranch> prefBranch = do_QueryInterface(mPrefService, &rv);
614 if (NS_FAILED(rv))
615 return rv;
617 nsCOMPtr<nsISecurityPref> securityPref = do_QueryInterface(prefBranch, &rv);
618 if (NS_SUCCEEDED(rv))
619 rv = securityPref->SecurityGetBoolPref(pref, return_val);
620 return rv;
623 NS_IMETHODIMP nsPref::SecuritySetBoolPref(const char *pref, PRBool value)
625 nsresult rv;
627 nsCOMPtr<nsIPrefBranch> prefBranch = do_QueryInterface(mPrefService, &rv);
628 if (NS_FAILED(rv))
629 return rv;
631 nsCOMPtr<nsISecurityPref> securityPref = do_QueryInterface(prefBranch, &rv);
632 if (NS_SUCCEEDED(rv))
633 rv = securityPref->SecuritySetBoolPref(pref, value);
634 return rv;
637 NS_IMETHODIMP nsPref::SecurityGetCharPref(const char *pref, char ** return_buf)
639 nsresult rv;
641 nsCOMPtr<nsIPrefBranch> prefBranch = do_QueryInterface(mPrefService, &rv);
642 if (NS_FAILED(rv))
643 return rv;
645 nsCOMPtr<nsISecurityPref> securityPref = do_QueryInterface(prefBranch, &rv);
646 if (NS_SUCCEEDED(rv))
647 rv = securityPref->SecurityGetCharPref(pref, return_buf);
648 return rv;
651 NS_IMETHODIMP nsPref::SecuritySetCharPref(const char *pref, const char* value)
653 nsresult rv;
655 nsCOMPtr<nsIPrefBranch> prefBranch = do_QueryInterface(mPrefService, &rv);
656 if (NS_FAILED(rv))
657 return rv;
659 nsCOMPtr<nsISecurityPref> securityPref = do_QueryInterface(prefBranch, &rv);
660 if (NS_SUCCEEDED(rv))
661 rv = securityPref->SecuritySetCharPref(pref, value);
662 return rv;
665 NS_IMETHODIMP nsPref::SecurityGetIntPref(const char *pref, PRInt32 * return_val)
667 nsresult rv;
669 nsCOMPtr<nsIPrefBranch> prefBranch = do_QueryInterface(mPrefService, &rv);
670 if (NS_FAILED(rv))
671 return rv;
673 nsCOMPtr<nsISecurityPref> securityPref = do_QueryInterface(prefBranch, &rv);
674 if (NS_SUCCEEDED(rv))
675 rv = securityPref->SecurityGetIntPref(pref, return_val);
676 return rv;
679 NS_IMETHODIMP nsPref::SecuritySetIntPref(const char *pref, PRInt32 value)
681 nsresult rv;
683 nsCOMPtr<nsIPrefBranch> prefBranch = do_QueryInterface(mPrefService, &rv);
684 if (NS_FAILED(rv))
685 return rv;
687 nsCOMPtr<nsISecurityPref> securityPref = do_QueryInterface(prefBranch, &rv);
688 if (NS_SUCCEEDED(rv))
689 rv = securityPref->SecuritySetIntPref(pref, value);
690 return rv;
693 NS_IMETHODIMP nsPref::SecurityClearUserPref(const char *pref_name)
695 nsresult rv;
697 nsCOMPtr<nsIPrefBranch> prefBranch = do_QueryInterface(mPrefService, &rv);
698 if (NS_FAILED(rv))
699 return rv;
701 nsCOMPtr<nsISecurityPref> securityPref = do_QueryInterface(prefBranch, &rv);
702 if (NS_SUCCEEDED(rv))
703 rv = securityPref->SecurityClearUserPref(pref_name);
704 return rv;
708 //----------------------------------------------------------------------------------------
709 nsPref* nsPref::GetInstance()
710 //----------------------------------------------------------------------------------------
712 if (!gInstance)
714 NS_NEWXPCOM(gInstance, nsPref);
716 return gInstance;
717 } // nsPref::GetInstance
720 //----------------------------------------------------------------------------------------
721 // Functions used to create new instances of a given object by the
722 // generic factory.
725 ////////////////////////////////////////////////////////////////////////
726 // Hand implement the GenericFactory constructor macro so I can make it
727 // non static. This is simply to keep us from having to make an nsPref.h
728 // file because we are trying to remove this object, not add to it.
731 NS_IMETHODIMP nsPrefConstructor(nsISupports *aOuter, REFNSIID aIID, void **aResult)
733 nsresult rv;
735 if (NULL == aResult) {
736 rv = NS_ERROR_NULL_POINTER;
737 return rv;
739 *aResult = NULL;
740 if (NULL != aOuter) {
741 rv = NS_ERROR_NO_AGGREGATION;
742 return rv;
745 nsPref *inst = nsPref::GetInstance();
747 if (NULL == inst) {
748 rv = NS_ERROR_OUT_OF_MEMORY;
749 return rv;
751 NS_ADDREF(inst);
752 rv = inst->QueryInterface(aIID, aResult);
753 NS_RELEASE(inst);
755 return rv;