1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
15 * The Original Code is Mozilla Communicator client code.
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 1998
20 * the Initial Developer. All Rights Reserved.
23 * Alec Flett <alecf@netscape.com>
24 * Brian Nesse <bnesse@netscape.com>
25 * Frederic Plourde <frederic.plourde@collabora.co.uk>
27 * Alternatively, the contents of this file may be used under the terms of
28 * either the GNU General Public License Version 2 or later (the "GPL"), or
29 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
30 * in which case the provisions of the GPL or the LGPL are applicable instead
31 * of those above. If you wish to allow use of your version of this file only
32 * under the terms of either the GPL or the LGPL, and not to allow others to
33 * use your version of this file under the terms of the MPL, indicate your
34 * decision by deleting the provisions above and replace them with the notice
35 * and other provisions required by the GPL or the LGPL. If you do not delete
36 * the provisions above, a recipient may use your version of this file under
37 * the terms of any one of the MPL, the GPL or the LGPL.
39 * ***** END LICENSE BLOCK ***** */
42 #include "mozilla/dom/ContentProcessChild.h"
43 #include "nsXULAppAPI.h"
46 #include "nsPrefBranch.h"
47 #include "nsILocalFile.h"
48 #include "nsIObserverService.h"
50 #include "nsISupportsPrimitives.h"
51 #include "nsIDirectoryService.h"
53 #include "nsReadableUtils.h"
54 #include "nsXPIDLString.h"
55 #include "nsIStringBundle.h"
62 #include "mozilla/Services.h"
64 #include "prefapi_private_data.h"
67 struct EnumerateData
{
69 nsTArray
<nsCString
> *pref_list
;
72 struct PrefCallbackData
{
73 nsPrefBranch
*pBranch
;
74 nsIObserver
*pObserver
;
75 nsIWeakReference
*pWeakRef
;
81 static PLDHashOperator
82 pref_enumChild(PLDHashTable
*table
, PLDHashEntryHdr
*heh
,
83 PRUint32 i
, void *arg
);
85 NotifyObserver(const char *newpref
, void *data
);
88 using mozilla::dom::ContentProcessChild
;
90 static ContentProcessChild
*
91 GetContentProcessChild()
93 if (XRE_GetProcessType() == GeckoProcessType_Content
) {
94 ContentProcessChild
* cpc
= ContentProcessChild::GetSingleton();
96 NS_RUNTIMEABORT("Content Protocol is NULL! We're going to crash!");
105 * Constructor/Destructor
108 nsPrefBranch::nsPrefBranch(const char *aPrefRoot
, PRBool aDefaultBranch
)
111 mPrefRoot
= aPrefRoot
;
112 mPrefRootLength
= mPrefRoot
.Length();
113 mIsDefault
= aDefaultBranch
;
115 nsCOMPtr
<nsIObserverService
> observerService
=
116 mozilla::services::GetObserverService();
117 if (observerService
) {
118 ++mRefCnt
; // Our refcnt must be > 0 when we call this, or we'll get deleted!
119 // add weak so we don't have to clean up at shutdown
120 observerService
->AddObserver(this, NS_XPCOM_SHUTDOWN_OBSERVER_ID
, PR_TRUE
);
125 nsPrefBranch::~nsPrefBranch()
132 * nsISupports Implementation
135 NS_IMPL_THREADSAFE_ADDREF(nsPrefBranch
)
136 NS_IMPL_THREADSAFE_RELEASE(nsPrefBranch
)
138 NS_INTERFACE_MAP_BEGIN(nsPrefBranch
)
139 NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports
, nsIPrefBranch
)
140 NS_INTERFACE_MAP_ENTRY(nsIPrefBranch
)
141 NS_INTERFACE_MAP_ENTRY_CONDITIONAL(nsIPrefBranch2
, !mIsDefault
)
142 NS_INTERFACE_MAP_ENTRY_CONDITIONAL(nsIPrefBranchInternal
, !mIsDefault
)
143 NS_INTERFACE_MAP_ENTRY(nsIObserver
)
144 NS_INTERFACE_MAP_ENTRY(nsISupportsWeakReference
)
149 * nsIPrefBranch Implementation
152 NS_IMETHODIMP
nsPrefBranch::GetRoot(char **aRoot
)
154 NS_ENSURE_ARG_POINTER(aRoot
);
156 mPrefRoot
.Truncate(mPrefRootLength
);
157 *aRoot
= ToNewCString(mPrefRoot
);
161 NS_IMETHODIMP
nsPrefBranch::GetPrefType(const char *aPrefName
, PRInt32
*_retval
)
164 if (ContentProcessChild
* cpc
= GetContentProcessChild()) {
165 nsresult rv
= NS_ERROR_NOT_AVAILABLE
;
167 cpc
->SendGetPrefType(nsDependentCString(getPrefName(aPrefName
)), &retval
, &rv
);
168 if (NS_SUCCEEDED(rv
))
174 const char *pref
= getPrefName(aPrefName
);
175 *_retval
= PREF_GetPrefType(pref
);
179 NS_IMETHODIMP
nsPrefBranch::GetBoolPref(const char *aPrefName
, PRBool
*_retval
)
182 if (ContentProcessChild
* cpc
= GetContentProcessChild()) {
183 nsresult rv
= NS_ERROR_NOT_AVAILABLE
;
185 cpc
->SendGetBoolPref(nsDependentCString(getPrefName(aPrefName
)), &retval
, &rv
);
186 if (NS_SUCCEEDED(rv
))
192 const char *pref
= getPrefName(aPrefName
);
193 return PREF_GetBoolPref(pref
, _retval
, mIsDefault
);
196 NS_IMETHODIMP
nsPrefBranch::SetBoolPref(const char *aPrefName
, PRInt32 aValue
)
199 if (GetContentProcessChild()) {
200 NS_ERROR("cannot set pref from content process");
201 return NS_ERROR_NOT_AVAILABLE
;
205 const char *pref
= getPrefName(aPrefName
);
206 return PREF_SetBoolPref(pref
, aValue
, mIsDefault
);
209 NS_IMETHODIMP
nsPrefBranch::GetCharPref(const char *aPrefName
, char **_retval
)
212 if (ContentProcessChild
* cpc
= GetContentProcessChild()) {
213 nsresult rv
= NS_ERROR_NOT_AVAILABLE
;
214 nsCAutoString prefValue
;
215 cpc
->SendGetCharPref(nsDependentCString(getPrefName(aPrefName
)),
217 if (NS_SUCCEEDED(rv
)) {
218 *_retval
= strdup(prefValue
.get());
224 const char *pref
= getPrefName(aPrefName
);
225 return PREF_CopyCharPref(pref
, _retval
, mIsDefault
);
228 NS_IMETHODIMP
nsPrefBranch::SetCharPref(const char *aPrefName
, const char *aValue
)
231 if (GetContentProcessChild()) {
232 NS_ERROR("cannot set pref from content process");
233 return NS_ERROR_NOT_AVAILABLE
;
237 const char *pref
= getPrefName(aPrefName
);
238 return PREF_SetCharPref(pref
, aValue
, mIsDefault
);
241 NS_IMETHODIMP
nsPrefBranch::GetIntPref(const char *aPrefName
, PRInt32
*_retval
)
244 if (ContentProcessChild
* cpc
= GetContentProcessChild()) {
245 nsresult rv
= NS_ERROR_NOT_AVAILABLE
;
247 cpc
->SendGetIntPref(nsDependentCString(getPrefName(aPrefName
)), &retval
, &rv
);
248 if (NS_SUCCEEDED(rv
))
254 const char *pref
= getPrefName(aPrefName
);
255 return PREF_GetIntPref(pref
, _retval
, mIsDefault
);
258 NS_IMETHODIMP
nsPrefBranch::SetIntPref(const char *aPrefName
, PRInt32 aValue
)
261 if (GetContentProcessChild()) {
262 NS_ERROR("cannot set pref from content process");
263 return NS_ERROR_NOT_AVAILABLE
;
267 const char *pref
= getPrefName(aPrefName
);
268 return PREF_SetIntPref(pref
, aValue
, mIsDefault
);
271 NS_IMETHODIMP
nsPrefBranch::GetComplexValue(const char *aPrefName
, const nsIID
& aType
, void **_retval
)
274 nsXPIDLCString utf8String
;
276 // we have to do this one first because it's different than all the rest
277 if (aType
.Equals(NS_GET_IID(nsIPrefLocalizedString
))) {
278 nsCOMPtr
<nsIPrefLocalizedString
> theString(do_CreateInstance(NS_PREFLOCALIZEDSTRING_CONTRACTID
, &rv
));
279 if (NS_FAILED(rv
)) return rv
;
282 if (ContentProcessChild
* cpc
= GetContentProcessChild()) {
283 nsAutoString prefValue
;
285 rv
= NS_ERROR_NOT_AVAILABLE
;
286 cpc
->SendGetPrefLocalizedString(nsDependentCString(getPrefName(aPrefName
)),
288 if (NS_FAILED(rv
)) return rv
;
290 theString
->SetData(prefValue
.get());
291 theString
.forget(reinterpret_cast<nsIPrefLocalizedString
**>(_retval
));
296 const char *pref
= getPrefName(aPrefName
);
297 PRBool bNeedDefault
= PR_FALSE
;
300 bNeedDefault
= PR_TRUE
;
302 // if there is no user (or locked) value
303 if (!PREF_HasUserPref(pref
) && !PREF_PrefIsLocked(pref
)) {
304 bNeedDefault
= PR_TRUE
;
308 // if we need to fetch the default value, do that instead, otherwise use the
309 // value we pulled in at the top of this function
311 nsXPIDLString utf16String
;
312 rv
= GetDefaultFromPropertiesFile(pref
, getter_Copies(utf16String
));
313 if (NS_SUCCEEDED(rv
)) {
314 theString
->SetData(utf16String
.get());
317 rv
= GetCharPref(aPrefName
, getter_Copies(utf8String
));
318 if (NS_SUCCEEDED(rv
)) {
319 theString
->SetData(NS_ConvertUTF8toUTF16(utf8String
).get());
323 if (NS_SUCCEEDED(rv
)) {
324 const char *pref
= getPrefName(aPrefName
);
325 PRBool bNeedDefault
= PR_FALSE
;
328 bNeedDefault
= PR_TRUE
;
330 // if there is no user (or locked) value
331 if (!PREF_HasUserPref(pref
) && !PREF_PrefIsLocked(pref
)) {
332 bNeedDefault
= PR_TRUE
;
336 // if we need to fetch the default value, do that instead, otherwise use the
337 // value we pulled in at the top of this function
339 nsXPIDLString utf16String
;
340 rv
= GetDefaultFromPropertiesFile(pref
, getter_Copies(utf16String
));
341 if (NS_SUCCEEDED(rv
)) {
342 rv
= theString
->SetData(utf16String
.get());
345 rv
= GetCharPref(aPrefName
, getter_Copies(utf8String
));
346 if (NS_SUCCEEDED(rv
)) {
347 rv
= theString
->SetData(NS_ConvertUTF8toUTF16(utf8String
).get());
350 if (NS_SUCCEEDED(rv
)) {
351 nsIPrefLocalizedString
*temp
= theString
;
354 *_retval
= (void *)temp
;
361 // if we can't get the pref, there's no point in being here
362 rv
= GetCharPref(aPrefName
, getter_Copies(utf8String
));
367 if (aType
.Equals(NS_GET_IID(nsILocalFile
))) {
369 if (GetContentProcessChild()) {
370 NS_ERROR("cannot get nsILocalFile pref from content process");
371 return NS_ERROR_NOT_AVAILABLE
;
375 nsCOMPtr
<nsILocalFile
> file(do_CreateInstance(NS_LOCAL_FILE_CONTRACTID
, &rv
));
377 if (NS_SUCCEEDED(rv
)) {
378 rv
= file
->SetPersistentDescriptor(utf8String
);
379 if (NS_SUCCEEDED(rv
)) {
380 file
.forget(reinterpret_cast<nsILocalFile
**>(_retval
));
387 if (aType
.Equals(NS_GET_IID(nsIRelativeFilePref
))) {
389 if (GetContentProcessChild()) {
390 NS_ERROR("cannot get nsIRelativeFilePref from content process");
391 return NS_ERROR_NOT_AVAILABLE
;
395 nsACString::const_iterator keyBegin
, strEnd
;
396 utf8String
.BeginReading(keyBegin
);
397 utf8String
.EndReading(strEnd
);
399 // The pref has the format: [fromKey]a/b/c
400 if (*keyBegin
++ != '[')
401 return NS_ERROR_FAILURE
;
402 nsACString::const_iterator
keyEnd(keyBegin
);
403 if (!FindCharInReadable(']', keyEnd
, strEnd
))
404 return NS_ERROR_FAILURE
;
405 nsCAutoString
key(Substring(keyBegin
, keyEnd
));
407 nsCOMPtr
<nsILocalFile
> fromFile
;
408 nsCOMPtr
<nsIProperties
> directoryService(do_GetService(NS_DIRECTORY_SERVICE_CONTRACTID
, &rv
));
411 rv
= directoryService
->Get(key
.get(), NS_GET_IID(nsILocalFile
), getter_AddRefs(fromFile
));
415 nsCOMPtr
<nsILocalFile
> theFile
;
416 rv
= NS_NewNativeLocalFile(EmptyCString(), PR_TRUE
, getter_AddRefs(theFile
));
419 rv
= theFile
->SetRelativeDescriptor(fromFile
, Substring(++keyEnd
, strEnd
));
422 nsCOMPtr
<nsIRelativeFilePref
> relativePref
;
423 rv
= NS_NewRelativeFilePref(theFile
, key
, getter_AddRefs(relativePref
));
427 relativePref
.forget(reinterpret_cast<nsIRelativeFilePref
**>(_retval
));
431 if (aType
.Equals(NS_GET_IID(nsISupportsString
))) {
432 nsCOMPtr
<nsISupportsString
> theString(do_CreateInstance(NS_SUPPORTS_STRING_CONTRACTID
, &rv
));
434 if (NS_SUCCEEDED(rv
)) {
435 theString
->SetData(NS_ConvertUTF8toUTF16(utf8String
));
436 theString
.forget(reinterpret_cast<nsISupportsString
**>(_retval
));
441 NS_WARNING("nsPrefBranch::GetComplexValue - Unsupported interface type");
442 return NS_NOINTERFACE
;
445 NS_IMETHODIMP
nsPrefBranch::SetComplexValue(const char *aPrefName
, const nsIID
& aType
, nsISupports
*aValue
)
448 if (GetContentProcessChild()) {
449 NS_ERROR("cannot set pref from content process");
450 return NS_ERROR_NOT_AVAILABLE
;
454 nsresult rv
= NS_NOINTERFACE
;
456 if (aType
.Equals(NS_GET_IID(nsILocalFile
))) {
457 nsCOMPtr
<nsILocalFile
> file
= do_QueryInterface(aValue
);
459 return NS_NOINTERFACE
;
460 nsCAutoString descriptorString
;
462 rv
= file
->GetPersistentDescriptor(descriptorString
);
463 if (NS_SUCCEEDED(rv
)) {
464 rv
= SetCharPref(aPrefName
, descriptorString
.get());
469 if (aType
.Equals(NS_GET_IID(nsIRelativeFilePref
))) {
470 nsCOMPtr
<nsIRelativeFilePref
> relFilePref
= do_QueryInterface(aValue
);
472 return NS_NOINTERFACE
;
474 nsCOMPtr
<nsILocalFile
> file
;
475 relFilePref
->GetFile(getter_AddRefs(file
));
477 return NS_NOINTERFACE
;
478 nsCAutoString relativeToKey
;
479 (void) relFilePref
->GetRelativeToKey(relativeToKey
);
481 nsCOMPtr
<nsILocalFile
> relativeToFile
;
482 nsCOMPtr
<nsIProperties
> directoryService(do_GetService(NS_DIRECTORY_SERVICE_CONTRACTID
, &rv
));
485 rv
= directoryService
->Get(relativeToKey
.get(), NS_GET_IID(nsILocalFile
), getter_AddRefs(relativeToFile
));
489 nsCAutoString relDescriptor
;
490 rv
= file
->GetRelativeDescriptor(relativeToFile
, relDescriptor
);
494 nsCAutoString descriptorString
;
495 descriptorString
.Append('[');
496 descriptorString
.Append(relativeToKey
);
497 descriptorString
.Append(']');
498 descriptorString
.Append(relDescriptor
);
499 return SetCharPref(aPrefName
, descriptorString
.get());
502 if (aType
.Equals(NS_GET_IID(nsISupportsString
))) {
503 nsCOMPtr
<nsISupportsString
> theString
= do_QueryInterface(aValue
);
506 nsAutoString wideString
;
508 rv
= theString
->GetData(wideString
);
509 if (NS_SUCCEEDED(rv
)) {
510 rv
= SetCharPref(aPrefName
, NS_ConvertUTF16toUTF8(wideString
).get());
516 if (aType
.Equals(NS_GET_IID(nsIPrefLocalizedString
))) {
517 nsCOMPtr
<nsIPrefLocalizedString
> theString
= do_QueryInterface(aValue
);
520 nsXPIDLString wideString
;
522 rv
= theString
->GetData(getter_Copies(wideString
));
523 if (NS_SUCCEEDED(rv
)) {
524 rv
= SetCharPref(aPrefName
, NS_ConvertUTF16toUTF8(wideString
).get());
530 NS_WARNING("nsPrefBranch::SetComplexValue - Unsupported interface type");
531 return NS_NOINTERFACE
;
534 NS_IMETHODIMP
nsPrefBranch::ClearUserPref(const char *aPrefName
)
537 if (GetContentProcessChild()) {
538 NS_ERROR("cannot set pref from content process");
539 return NS_ERROR_NOT_AVAILABLE
;
543 const char *pref
= getPrefName(aPrefName
);
544 return PREF_ClearUserPref(pref
);
547 NS_IMETHODIMP
nsPrefBranch::PrefHasUserValue(const char *aPrefName
, PRBool
*_retval
)
549 NS_ENSURE_ARG_POINTER(_retval
);
552 if (ContentProcessChild
* cpc
= GetContentProcessChild()) {
553 nsresult rv
= NS_ERROR_NOT_AVAILABLE
;
555 cpc
->SendPrefHasUserValue(nsDependentCString(getPrefName(aPrefName
)), &retval
, &rv
);
556 if (NS_SUCCEEDED(rv
))
562 const char *pref
= getPrefName(aPrefName
);
563 *_retval
= PREF_HasUserPref(pref
);
567 NS_IMETHODIMP
nsPrefBranch::LockPref(const char *aPrefName
)
570 if (GetContentProcessChild()) {
571 NS_ERROR("cannot lock pref from content process");
572 return NS_ERROR_NOT_AVAILABLE
;
576 const char *pref
= getPrefName(aPrefName
);
577 return PREF_LockPref(pref
, PR_TRUE
);
580 NS_IMETHODIMP
nsPrefBranch::PrefIsLocked(const char *aPrefName
, PRBool
*_retval
)
582 NS_ENSURE_ARG_POINTER(_retval
);
585 if (ContentProcessChild
* cpc
= GetContentProcessChild()) {
586 nsresult rv
= NS_ERROR_NOT_AVAILABLE
;
588 cpc
->SendPrefIsLocked(nsDependentCString(getPrefName(aPrefName
)), &retval
, &rv
);
589 if (NS_SUCCEEDED(rv
))
595 const char *pref
= getPrefName(aPrefName
);
596 *_retval
= PREF_PrefIsLocked(pref
);
600 NS_IMETHODIMP
nsPrefBranch::UnlockPref(const char *aPrefName
)
603 if (GetContentProcessChild()) {
604 NS_ERROR("cannot unlock pref from content process");
605 return NS_ERROR_NOT_AVAILABLE
;
609 const char *pref
= getPrefName(aPrefName
);
610 return PREF_LockPref(pref
, PR_FALSE
);
613 /* void resetBranch (in string startingAt); */
614 NS_IMETHODIMP
nsPrefBranch::ResetBranch(const char *aStartingAt
)
616 return NS_ERROR_NOT_IMPLEMENTED
;
619 NS_IMETHODIMP
nsPrefBranch::DeleteBranch(const char *aStartingAt
)
622 if (GetContentProcessChild()) {
623 NS_ERROR("cannot set pref from content process");
624 return NS_ERROR_NOT_AVAILABLE
;
628 const char *pref
= getPrefName(aStartingAt
);
629 return PREF_DeleteBranch(pref
);
632 NS_IMETHODIMP
nsPrefBranch::GetChildList(const char *aStartingAt
, PRUint32
*aCount
, char ***aChildArray
)
638 nsAutoTArray
<nsCString
, 32> prefArray
;
640 NS_ENSURE_ARG_POINTER(aStartingAt
);
641 NS_ENSURE_ARG_POINTER(aCount
);
642 NS_ENSURE_ARG_POINTER(aChildArray
);
644 *aChildArray
= nsnull
;
648 if (ContentProcessChild
* cpc
= GetContentProcessChild()) {
649 nsresult rv
= NS_ERROR_NOT_AVAILABLE
;
650 cpc
->SendGetChildList(nsDependentCString(getPrefName(aStartingAt
)),
652 if (NS_FAILED(rv
)) return rv
;
658 return NS_ERROR_NOT_INITIALIZED
;
660 // this will contain a list of all the pref name strings
661 // allocate on the stack for speed
663 ed
.parent
= getPrefName(aStartingAt
);
664 ed
.pref_list
= &prefArray
;
665 PL_DHashTableEnumerate(&gHashTable
, pref_enumChild
, &ed
);
668 // now that we've built up the list, run the callback on
669 // all the matching elements
670 numPrefs
= prefArray
.Length();
673 outArray
= (char **)nsMemory::Alloc(numPrefs
* sizeof(char *));
675 return NS_ERROR_OUT_OF_MEMORY
;
677 for (dwIndex
= 0; dwIndex
< numPrefs
; ++dwIndex
) {
678 // we need to lop off mPrefRoot in case the user is planning to pass this
679 // back to us because if they do we are going to add mPrefRoot again.
680 const nsCString
& element
= prefArray
[dwIndex
];
681 outArray
[dwIndex
] = (char *)nsMemory::Clone(
682 element
.get() + mPrefRootLength
, element
.Length() - mPrefRootLength
+ 1);
684 if (!outArray
[dwIndex
]) {
685 // we ran out of memory... this is annoying
686 NS_FREE_XPCOM_ALLOCATED_POINTER_ARRAY(dwIndex
, outArray
);
687 return NS_ERROR_OUT_OF_MEMORY
;
690 *aChildArray
= outArray
;
699 * nsIPrefBranch2 methods
702 NS_IMETHODIMP
nsPrefBranch::AddObserver(const char *aDomain
, nsIObserver
*aObserver
, PRBool aHoldWeak
)
704 PrefCallbackData
*pCallback
;
707 NS_ENSURE_ARG_POINTER(aDomain
);
708 NS_ENSURE_ARG_POINTER(aObserver
);
711 if (ContentProcessChild
* cpc
= GetContentProcessChild()) {
712 return cpc
->AddRemotePrefObserver(nsDependentCString(aDomain
), mPrefRoot
, aObserver
, aHoldWeak
);
717 mObservers
= new nsAutoVoidArray();
718 if (nsnull
== mObservers
)
719 return NS_ERROR_OUT_OF_MEMORY
;
722 pCallback
= (PrefCallbackData
*)NS_Alloc(sizeof(PrefCallbackData
) + strlen(aDomain
));
723 if (nsnull
== pCallback
)
724 return NS_ERROR_OUT_OF_MEMORY
;
726 pCallback
->pBranch
= this;
727 pCallback
->pObserver
= aObserver
;
729 // hold a weak reference to the observer if so requested
731 nsCOMPtr
<nsISupportsWeakReference
> weakRefFactory
= do_QueryInterface(aObserver
);
732 if (!weakRefFactory
) {
733 // the caller didn't give us a object that supports weak reference... tell them
734 nsMemory::Free(pCallback
);
735 return NS_ERROR_INVALID_ARG
;
737 nsCOMPtr
<nsIWeakReference
> tmp
= do_GetWeakReference(weakRefFactory
);
738 NS_ADDREF(pCallback
->pWeakRef
= tmp
);
740 pCallback
->pWeakRef
= nsnull
;
741 NS_ADDREF(pCallback
->pObserver
);
744 strcpy(pCallback
->pDomain
, aDomain
);
745 mObservers
->AppendElement(pCallback
);
747 // We must pass a fully qualified preference name to the callback
748 pref
= getPrefName(aDomain
); // aDomain == nsnull only possible failure, trapped above
749 PREF_RegisterCallback(pref
, NotifyObserver
, pCallback
);
753 NS_IMETHODIMP
nsPrefBranch::RemoveObserver(const char *aDomain
, nsIObserver
*aObserver
)
756 PrefCallbackData
*pCallback
;
761 NS_ENSURE_ARG_POINTER(aDomain
);
762 NS_ENSURE_ARG_POINTER(aObserver
);
765 if (XRE_GetProcessType() == GeckoProcessType_Content
) {
767 ContentProcessChild
*cpc
= ContentProcessChild::GetSingleton();
768 // In case cpc doesn't exist here, we're silently returning (instead of
769 // asserting), because the child process is likely to be null
770 // when this is called during xpcom-shutdown.
772 rv
= cpc
->RemoveRemotePrefObserver(nsDependentCString(aDomain
),
782 // need to find the index of observer, so we can remove it from the domain list too
783 count
= mObservers
->Count();
787 for (i
= 0; i
< count
; i
++) {
788 pCallback
= (PrefCallbackData
*)mObservers
->ElementAt(i
);
790 pCallback
->pObserver
== aObserver
&&
791 !strcmp(pCallback
->pDomain
, aDomain
)) {
792 // We must pass a fully qualified preference name to remove the callback
793 pref
= getPrefName(aDomain
); // aDomain == nsnull only possible failure, trapped above
794 rv
= PREF_UnregisterCallback(pref
, NotifyObserver
, pCallback
);
795 if (NS_SUCCEEDED(rv
)) {
796 // Remove this observer from our array so that nobody else can remove
797 // what we're trying to remove ourselves right now.
798 mObservers
->RemoveElementAt(i
);
799 if (pCallback
->pWeakRef
) {
800 NS_RELEASE(pCallback
->pWeakRef
);
802 NS_RELEASE(pCallback
->pObserver
);
813 NS_IMETHODIMP
nsPrefBranch::Observe(nsISupports
*aSubject
, const char *aTopic
, const PRUnichar
*someData
)
815 // watch for xpcom shutdown and free our observers to eliminate any cyclic references
816 if (!nsCRT::strcmp(aTopic
, NS_XPCOM_SHUTDOWN_OBSERVER_ID
)) {
822 static nsresult
NotifyObserver(const char *newpref
, void *data
)
825 if (GetContentProcessChild()) {
826 // We shouldn't ever get here, since we never register NotifyObserver in the
828 NS_NOTREACHED("Remote prefs observation should be done from the \
834 PrefCallbackData
*pData
= (PrefCallbackData
*)data
;
836 // remove any root this string may contain so as to not confuse the observer
837 // by passing them something other than what they passed us as a topic
838 PRUint32 len
= pData
->pBranch
->GetRootLength();
839 nsCAutoString
suffix(newpref
+ len
);
841 nsCOMPtr
<nsIObserver
> observer
;
842 if (pData
->pWeakRef
) {
843 observer
= do_QueryReferent(pData
->pWeakRef
);
845 // this weak referenced observer went away, remove them from the list
846 pData
->pBranch
->RemoveObserver(pData
->pDomain
, pData
->pObserver
);
850 observer
= pData
->pObserver
;
853 observer
->Observe(static_cast<nsIPrefBranch
*>(pData
->pBranch
),
854 NS_PREFBRANCH_PREFCHANGE_TOPIC_ID
,
855 NS_ConvertASCIItoUTF16(suffix
).get());
860 void nsPrefBranch::freeObserverList(void)
863 PrefCallbackData
*pCallback
;
866 // unregister the observers
869 count
= mObservers
->Count();
872 nsCAutoString domain
;
873 for (i
= 0; i
< count
; ++i
) {
874 pCallback
= (PrefCallbackData
*)mObservers
->ElementAt(i
);
876 // We must pass a fully qualified preference name to remove the callback
877 pref
= getPrefName(pCallback
->pDomain
);
878 // Remove this observer from our array so that nobody else can remove
879 // what we're trying to remove right now.
880 mObservers
->ReplaceElementAt(nsnull
, i
);
881 PREF_UnregisterCallback(pref
, NotifyObserver
, pCallback
);
882 if (pCallback
->pWeakRef
) {
883 NS_RELEASE(pCallback
->pWeakRef
);
885 NS_RELEASE(pCallback
->pObserver
);
887 nsMemory::Free(pCallback
);
896 nsresult
nsPrefBranch::GetDefaultFromPropertiesFile(const char *aPrefName
, PRUnichar
**return_buf
)
900 // the default value contains a URL to a .properties file
902 nsXPIDLCString propertyFileURL
;
903 rv
= PREF_CopyCharPref(aPrefName
, getter_Copies(propertyFileURL
), PR_TRUE
);
907 nsCOMPtr
<nsIStringBundleService
> bundleService
=
908 mozilla::services::GetStringBundleService();
910 return NS_ERROR_FAILURE
;
912 nsCOMPtr
<nsIStringBundle
> bundle
;
913 rv
= bundleService
->CreateBundle(propertyFileURL
,
914 getter_AddRefs(bundle
));
918 // string names are in unicode
919 nsAutoString stringId
;
920 stringId
.AssignASCII(aPrefName
);
922 return bundle
->GetStringFromName(stringId
.get(), return_buf
);
925 const char *nsPrefBranch::getPrefName(const char *aPrefName
)
927 // for speed, avoid strcpy if we can:
928 if (mPrefRoot
.IsEmpty())
931 // isn't there a better way to do this? this is really kind of gross.
932 mPrefRoot
.Truncate(mPrefRootLength
);
934 // only append if anything to append
935 if ((nsnull
!= aPrefName
) && (*aPrefName
!= '\0'))
936 mPrefRoot
.Append(aPrefName
);
938 return mPrefRoot
.get();
941 static PLDHashOperator
942 pref_enumChild(PLDHashTable
*table
, PLDHashEntryHdr
*heh
,
943 PRUint32 i
, void *arg
)
945 PrefHashEntry
*he
= static_cast<PrefHashEntry
*>(heh
);
946 EnumerateData
*d
= reinterpret_cast<EnumerateData
*>(arg
);
947 if (strncmp(he
->key
, d
->parent
, strlen(d
->parent
)) == 0) {
948 d
->pref_list
->AppendElement(he
->key
);
950 return PL_DHASH_NEXT
;
953 //----------------------------------------------------------------------------
954 // nsPrefLocalizedString
955 //----------------------------------------------------------------------------
957 nsPrefLocalizedString::nsPrefLocalizedString()
961 nsPrefLocalizedString::~nsPrefLocalizedString()
967 * nsISupports Implementation
970 NS_IMPL_THREADSAFE_ADDREF(nsPrefLocalizedString
)
971 NS_IMPL_THREADSAFE_RELEASE(nsPrefLocalizedString
)
973 NS_INTERFACE_MAP_BEGIN(nsPrefLocalizedString
)
974 NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports
, nsIPrefLocalizedString
)
975 NS_INTERFACE_MAP_ENTRY(nsIPrefLocalizedString
)
976 NS_INTERFACE_MAP_ENTRY(nsISupportsString
)
979 nsresult
nsPrefLocalizedString::Init()
982 mUnicodeString
= do_CreateInstance(NS_SUPPORTS_STRING_CONTRACTID
, &rv
);
988 nsPrefLocalizedString::GetData(PRUnichar
**_retval
)
992 nsresult rv
= GetData(data
);
996 *_retval
= ToNewUnicode(data
);
998 return NS_ERROR_OUT_OF_MEMORY
;
1004 nsPrefLocalizedString::SetData(const PRUnichar
*aData
)
1007 return SetData(EmptyString());
1008 return SetData(nsDependentString(aData
));
1012 nsPrefLocalizedString::SetDataWithLength(PRUint32 aLength
,
1013 const PRUnichar
*aData
)
1016 return SetData(EmptyString());
1017 return SetData(Substring(aData
, aData
+ aLength
));
1020 //----------------------------------------------------------------------------
1021 // nsRelativeFilePref
1022 //----------------------------------------------------------------------------
1024 NS_IMPL_THREADSAFE_ISUPPORTS1(nsRelativeFilePref
, nsIRelativeFilePref
)
1026 nsRelativeFilePref::nsRelativeFilePref()
1030 nsRelativeFilePref::~nsRelativeFilePref()
1034 NS_IMETHODIMP
nsRelativeFilePref::GetFile(nsILocalFile
**aFile
)
1036 NS_ENSURE_ARG_POINTER(aFile
);
1038 NS_IF_ADDREF(*aFile
);
1042 NS_IMETHODIMP
nsRelativeFilePref::SetFile(nsILocalFile
*aFile
)
1048 NS_IMETHODIMP
nsRelativeFilePref::GetRelativeToKey(nsACString
& aRelativeToKey
)
1050 aRelativeToKey
.Assign(mRelativeToKey
);
1054 NS_IMETHODIMP
nsRelativeFilePref::SetRelativeToKey(const nsACString
& aRelativeToKey
)
1056 mRelativeToKey
.Assign(aRelativeToKey
);