[JAEGER] Merge from tracemonkey.
[mozilla-central.git] / modules / libpref / src / nsPrefBranch.cpp
blob81b90bc46d8b355017e3cdabbc0318e6175b5182
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
13 * License.
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.
22 * Contributor(s):
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 ***** */
41 #ifdef MOZ_IPC
42 #include "mozilla/dom/ContentProcessChild.h"
43 #include "nsXULAppAPI.h"
44 #endif
46 #include "nsPrefBranch.h"
47 #include "nsILocalFile.h"
48 #include "nsIObserverService.h"
49 #include "nsXPCOM.h"
50 #include "nsISupportsPrimitives.h"
51 #include "nsIDirectoryService.h"
52 #include "nsString.h"
53 #include "nsReadableUtils.h"
54 #include "nsXPIDLString.h"
55 #include "nsIStringBundle.h"
56 #include "prefapi.h"
57 #include "prmem.h"
58 #include "pldhash.h"
60 #include "plstr.h"
61 #include "nsCRT.h"
62 #include "mozilla/Services.h"
64 #include "prefapi_private_data.h"
66 // Definitions
67 struct EnumerateData {
68 const char *parent;
69 nsTArray<nsCString> *pref_list;
72 struct PrefCallbackData {
73 nsPrefBranch *pBranch;
74 nsIObserver *pObserver;
75 nsIWeakReference *pWeakRef;
76 char pDomain[1];
80 // Prototypes
81 static PLDHashOperator
82 pref_enumChild(PLDHashTable *table, PLDHashEntryHdr *heh,
83 PRUint32 i, void *arg);
84 static nsresult
85 NotifyObserver(const char *newpref, void *data);
87 #ifdef MOZ_IPC
88 using mozilla::dom::ContentProcessChild;
90 static ContentProcessChild*
91 GetContentProcessChild()
93 if (XRE_GetProcessType() == GeckoProcessType_Content) {
94 ContentProcessChild* cpc = ContentProcessChild::GetSingleton();
95 if (!cpc) {
96 NS_RUNTIMEABORT("Content Protocol is NULL! We're going to crash!");
98 return cpc;
100 return nsnull;
102 #endif // MOZ_IPC
105 * Constructor/Destructor
108 nsPrefBranch::nsPrefBranch(const char *aPrefRoot, PRBool aDefaultBranch)
109 : mObservers(nsnull)
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);
121 --mRefCnt;
125 nsPrefBranch::~nsPrefBranch()
127 freeObserverList();
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)
145 NS_INTERFACE_MAP_END
149 * nsIPrefBranch Implementation
152 NS_IMETHODIMP nsPrefBranch::GetRoot(char **aRoot)
154 NS_ENSURE_ARG_POINTER(aRoot);
156 mPrefRoot.Truncate(mPrefRootLength);
157 *aRoot = ToNewCString(mPrefRoot);
158 return NS_OK;
161 NS_IMETHODIMP nsPrefBranch::GetPrefType(const char *aPrefName, PRInt32 *_retval)
163 #ifdef MOZ_IPC
164 if (ContentProcessChild* cpc = GetContentProcessChild()) {
165 nsresult rv = NS_ERROR_NOT_AVAILABLE;
166 PRInt32 retval;
167 cpc->SendGetPrefType(nsDependentCString(getPrefName(aPrefName)), &retval, &rv);
168 if (NS_SUCCEEDED(rv))
169 *_retval = retval;
170 return rv;
172 #endif
174 const char *pref = getPrefName(aPrefName);
175 *_retval = PREF_GetPrefType(pref);
176 return NS_OK;
179 NS_IMETHODIMP nsPrefBranch::GetBoolPref(const char *aPrefName, PRBool *_retval)
181 #ifdef MOZ_IPC
182 if (ContentProcessChild* cpc = GetContentProcessChild()) {
183 nsresult rv = NS_ERROR_NOT_AVAILABLE;
184 PRBool retval;
185 cpc->SendGetBoolPref(nsDependentCString(getPrefName(aPrefName)), &retval, &rv);
186 if (NS_SUCCEEDED(rv))
187 *_retval = retval;
188 return rv;
190 #endif
192 const char *pref = getPrefName(aPrefName);
193 return PREF_GetBoolPref(pref, _retval, mIsDefault);
196 NS_IMETHODIMP nsPrefBranch::SetBoolPref(const char *aPrefName, PRInt32 aValue)
198 #ifdef MOZ_IPC
199 if (GetContentProcessChild()) {
200 NS_ERROR("cannot set pref from content process");
201 return NS_ERROR_NOT_AVAILABLE;
203 #endif
205 const char *pref = getPrefName(aPrefName);
206 return PREF_SetBoolPref(pref, aValue, mIsDefault);
209 NS_IMETHODIMP nsPrefBranch::GetCharPref(const char *aPrefName, char **_retval)
211 #ifdef MOZ_IPC
212 if (ContentProcessChild* cpc = GetContentProcessChild()) {
213 nsresult rv = NS_ERROR_NOT_AVAILABLE;
214 nsCAutoString prefValue;
215 cpc->SendGetCharPref(nsDependentCString(getPrefName(aPrefName)),
216 &prefValue, &rv);
217 if (NS_SUCCEEDED(rv)) {
218 *_retval = strdup(prefValue.get());
220 return rv;
222 #endif
224 const char *pref = getPrefName(aPrefName);
225 return PREF_CopyCharPref(pref, _retval, mIsDefault);
228 NS_IMETHODIMP nsPrefBranch::SetCharPref(const char *aPrefName, const char *aValue)
230 #ifdef MOZ_IPC
231 if (GetContentProcessChild()) {
232 NS_ERROR("cannot set pref from content process");
233 return NS_ERROR_NOT_AVAILABLE;
235 #endif
237 const char *pref = getPrefName(aPrefName);
238 return PREF_SetCharPref(pref, aValue, mIsDefault);
241 NS_IMETHODIMP nsPrefBranch::GetIntPref(const char *aPrefName, PRInt32 *_retval)
243 #ifdef MOZ_IPC
244 if (ContentProcessChild* cpc = GetContentProcessChild()) {
245 nsresult rv = NS_ERROR_NOT_AVAILABLE;
246 PRInt32 retval;
247 cpc->SendGetIntPref(nsDependentCString(getPrefName(aPrefName)), &retval, &rv);
248 if (NS_SUCCEEDED(rv))
249 *_retval = retval;
250 return rv;
252 #endif
254 const char *pref = getPrefName(aPrefName);
255 return PREF_GetIntPref(pref, _retval, mIsDefault);
258 NS_IMETHODIMP nsPrefBranch::SetIntPref(const char *aPrefName, PRInt32 aValue)
260 #ifdef MOZ_IPC
261 if (GetContentProcessChild()) {
262 NS_ERROR("cannot set pref from content process");
263 return NS_ERROR_NOT_AVAILABLE;
265 #endif
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)
273 nsresult rv;
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;
281 #ifdef MOZ_IPC
282 if (ContentProcessChild* cpc = GetContentProcessChild()) {
283 nsAutoString prefValue;
285 rv = NS_ERROR_NOT_AVAILABLE;
286 cpc->SendGetPrefLocalizedString(nsDependentCString(getPrefName(aPrefName)),
287 &prefValue, &rv);
288 if (NS_FAILED(rv)) return rv;
290 theString->SetData(prefValue.get());
291 theString.forget(reinterpret_cast<nsIPrefLocalizedString**>(_retval));
292 return rv;
294 #endif
296 const char *pref = getPrefName(aPrefName);
297 PRBool bNeedDefault = PR_FALSE;
299 if (mIsDefault) {
300 bNeedDefault = PR_TRUE;
301 } else {
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
310 if (bNeedDefault) {
311 nsXPIDLString utf16String;
312 rv = GetDefaultFromPropertiesFile(pref, getter_Copies(utf16String));
313 if (NS_SUCCEEDED(rv)) {
314 theString->SetData(utf16String.get());
316 } else {
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;
327 if (mIsDefault) {
328 bNeedDefault = PR_TRUE;
329 } else {
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
338 if (bNeedDefault) {
339 nsXPIDLString utf16String;
340 rv = GetDefaultFromPropertiesFile(pref, getter_Copies(utf16String));
341 if (NS_SUCCEEDED(rv)) {
342 rv = theString->SetData(utf16String.get());
344 } else {
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;
353 NS_ADDREF(temp);
354 *_retval = (void *)temp;
358 return rv;
361 // if we can't get the pref, there's no point in being here
362 rv = GetCharPref(aPrefName, getter_Copies(utf8String));
363 if (NS_FAILED(rv)) {
364 return rv;
367 if (aType.Equals(NS_GET_IID(nsILocalFile))) {
368 #ifdef MOZ_IPC
369 if (GetContentProcessChild()) {
370 NS_ERROR("cannot get nsILocalFile pref from content process");
371 return NS_ERROR_NOT_AVAILABLE;
373 #endif
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));
381 return NS_OK;
384 return rv;
387 if (aType.Equals(NS_GET_IID(nsIRelativeFilePref))) {
388 #ifdef MOZ_IPC
389 if (GetContentProcessChild()) {
390 NS_ERROR("cannot get nsIRelativeFilePref from content process");
391 return NS_ERROR_NOT_AVAILABLE;
393 #endif
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));
409 if (NS_FAILED(rv))
410 return rv;
411 rv = directoryService->Get(key.get(), NS_GET_IID(nsILocalFile), getter_AddRefs(fromFile));
412 if (NS_FAILED(rv))
413 return rv;
415 nsCOMPtr<nsILocalFile> theFile;
416 rv = NS_NewNativeLocalFile(EmptyCString(), PR_TRUE, getter_AddRefs(theFile));
417 if (NS_FAILED(rv))
418 return rv;
419 rv = theFile->SetRelativeDescriptor(fromFile, Substring(++keyEnd, strEnd));
420 if (NS_FAILED(rv))
421 return rv;
422 nsCOMPtr<nsIRelativeFilePref> relativePref;
423 rv = NS_NewRelativeFilePref(theFile, key, getter_AddRefs(relativePref));
424 if (NS_FAILED(rv))
425 return rv;
427 relativePref.forget(reinterpret_cast<nsIRelativeFilePref**>(_retval));
428 return NS_OK;
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));
438 return rv;
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)
447 #ifdef MOZ_IPC
448 if (GetContentProcessChild()) {
449 NS_ERROR("cannot set pref from content process");
450 return NS_ERROR_NOT_AVAILABLE;
452 #endif
454 nsresult rv = NS_NOINTERFACE;
456 if (aType.Equals(NS_GET_IID(nsILocalFile))) {
457 nsCOMPtr<nsILocalFile> file = do_QueryInterface(aValue);
458 if (!file)
459 return NS_NOINTERFACE;
460 nsCAutoString descriptorString;
462 rv = file->GetPersistentDescriptor(descriptorString);
463 if (NS_SUCCEEDED(rv)) {
464 rv = SetCharPref(aPrefName, descriptorString.get());
466 return rv;
469 if (aType.Equals(NS_GET_IID(nsIRelativeFilePref))) {
470 nsCOMPtr<nsIRelativeFilePref> relFilePref = do_QueryInterface(aValue);
471 if (!relFilePref)
472 return NS_NOINTERFACE;
474 nsCOMPtr<nsILocalFile> file;
475 relFilePref->GetFile(getter_AddRefs(file));
476 if (!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));
483 if (NS_FAILED(rv))
484 return rv;
485 rv = directoryService->Get(relativeToKey.get(), NS_GET_IID(nsILocalFile), getter_AddRefs(relativeToFile));
486 if (NS_FAILED(rv))
487 return rv;
489 nsCAutoString relDescriptor;
490 rv = file->GetRelativeDescriptor(relativeToFile, relDescriptor);
491 if (NS_FAILED(rv))
492 return rv;
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);
505 if (theString) {
506 nsAutoString wideString;
508 rv = theString->GetData(wideString);
509 if (NS_SUCCEEDED(rv)) {
510 rv = SetCharPref(aPrefName, NS_ConvertUTF16toUTF8(wideString).get());
513 return rv;
516 if (aType.Equals(NS_GET_IID(nsIPrefLocalizedString))) {
517 nsCOMPtr<nsIPrefLocalizedString> theString = do_QueryInterface(aValue);
519 if (theString) {
520 nsXPIDLString wideString;
522 rv = theString->GetData(getter_Copies(wideString));
523 if (NS_SUCCEEDED(rv)) {
524 rv = SetCharPref(aPrefName, NS_ConvertUTF16toUTF8(wideString).get());
527 return rv;
530 NS_WARNING("nsPrefBranch::SetComplexValue - Unsupported interface type");
531 return NS_NOINTERFACE;
534 NS_IMETHODIMP nsPrefBranch::ClearUserPref(const char *aPrefName)
536 #ifdef MOZ_IPC
537 if (GetContentProcessChild()) {
538 NS_ERROR("cannot set pref from content process");
539 return NS_ERROR_NOT_AVAILABLE;
541 #endif
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);
551 #ifdef MOZ_IPC
552 if (ContentProcessChild* cpc = GetContentProcessChild()) {
553 nsresult rv = NS_ERROR_NOT_AVAILABLE;
554 PRBool retval;
555 cpc->SendPrefHasUserValue(nsDependentCString(getPrefName(aPrefName)), &retval, &rv);
556 if (NS_SUCCEEDED(rv))
557 *_retval = retval;
558 return rv;
560 #endif
562 const char *pref = getPrefName(aPrefName);
563 *_retval = PREF_HasUserPref(pref);
564 return NS_OK;
567 NS_IMETHODIMP nsPrefBranch::LockPref(const char *aPrefName)
569 #ifdef MOZ_IPC
570 if (GetContentProcessChild()) {
571 NS_ERROR("cannot lock pref from content process");
572 return NS_ERROR_NOT_AVAILABLE;
574 #endif
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);
584 #ifdef MOZ_IPC
585 if (ContentProcessChild* cpc = GetContentProcessChild()) {
586 nsresult rv = NS_ERROR_NOT_AVAILABLE;
587 PRBool retval;
588 cpc->SendPrefIsLocked(nsDependentCString(getPrefName(aPrefName)), &retval, &rv);
589 if (NS_SUCCEEDED(rv))
590 *_retval = retval;
591 return rv;
593 #endif
595 const char *pref = getPrefName(aPrefName);
596 *_retval = PREF_PrefIsLocked(pref);
597 return NS_OK;
600 NS_IMETHODIMP nsPrefBranch::UnlockPref(const char *aPrefName)
602 #ifdef MOZ_IPC
603 if (GetContentProcessChild()) {
604 NS_ERROR("cannot unlock pref from content process");
605 return NS_ERROR_NOT_AVAILABLE;
607 #endif
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)
621 #ifdef MOZ_IPC
622 if (GetContentProcessChild()) {
623 NS_ERROR("cannot set pref from content process");
624 return NS_ERROR_NOT_AVAILABLE;
626 #endif
628 const char *pref = getPrefName(aStartingAt);
629 return PREF_DeleteBranch(pref);
632 NS_IMETHODIMP nsPrefBranch::GetChildList(const char *aStartingAt, PRUint32 *aCount, char ***aChildArray)
634 char **outArray;
635 PRInt32 numPrefs;
636 PRInt32 dwIndex;
637 EnumerateData ed;
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;
645 *aCount = 0;
647 #ifdef MOZ_IPC
648 if (ContentProcessChild* cpc = GetContentProcessChild()) {
649 nsresult rv = NS_ERROR_NOT_AVAILABLE;
650 cpc->SendGetChildList(nsDependentCString(getPrefName(aStartingAt)),
651 &prefArray, &rv);
652 if (NS_FAILED(rv)) return rv;
654 } else
655 #endif
657 if (!gHashTable.ops)
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();
672 if (numPrefs) {
673 outArray = (char **)nsMemory::Alloc(numPrefs * sizeof(char *));
674 if (!outArray)
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;
692 *aCount = numPrefs;
694 return NS_OK;
699 * nsIPrefBranch2 methods
702 NS_IMETHODIMP nsPrefBranch::AddObserver(const char *aDomain, nsIObserver *aObserver, PRBool aHoldWeak)
704 PrefCallbackData *pCallback;
705 const char *pref;
707 NS_ENSURE_ARG_POINTER(aDomain);
708 NS_ENSURE_ARG_POINTER(aObserver);
710 #ifdef MOZ_IPC
711 if (ContentProcessChild* cpc = GetContentProcessChild()) {
712 return cpc->AddRemotePrefObserver(nsDependentCString(aDomain), mPrefRoot, aObserver, aHoldWeak);
714 #endif
716 if (!mObservers) {
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
730 if (aHoldWeak) {
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);
739 } else {
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);
750 return NS_OK;
753 NS_IMETHODIMP nsPrefBranch::RemoveObserver(const char *aDomain, nsIObserver *aObserver)
755 const char *pref;
756 PrefCallbackData *pCallback;
757 PRInt32 count;
758 PRInt32 i;
759 nsresult rv;
761 NS_ENSURE_ARG_POINTER(aDomain);
762 NS_ENSURE_ARG_POINTER(aObserver);
764 #ifdef MOZ_IPC
765 if (XRE_GetProcessType() == GeckoProcessType_Content) {
766 nsresult rv = NS_OK;
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.
771 if (cpc)
772 rv = cpc->RemoveRemotePrefObserver(nsDependentCString(aDomain),
773 mPrefRoot,
774 aObserver);
775 return rv;
777 #endif
779 if (!mObservers)
780 return NS_OK;
782 // need to find the index of observer, so we can remove it from the domain list too
783 count = mObservers->Count();
784 if (count == 0)
785 return NS_OK;
787 for (i = 0; i < count; i++) {
788 pCallback = (PrefCallbackData *)mObservers->ElementAt(i);
789 if (pCallback &&
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);
801 } else {
802 NS_RELEASE(pCallback->pObserver);
804 NS_Free(pCallback);
806 return rv;
810 return NS_OK;
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)) {
817 freeObserverList();
819 return NS_OK;
822 static nsresult NotifyObserver(const char *newpref, void *data)
824 #ifdef MOZ_IPC
825 if (GetContentProcessChild()) {
826 // We shouldn't ever get here, since we never register NotifyObserver in the
827 // content process
828 NS_NOTREACHED("Remote prefs observation should be done from the \
829 chrome process!");
830 return NS_OK;
832 #endif
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);
844 if (!observer) {
845 // this weak referenced observer went away, remove them from the list
846 pData->pBranch->RemoveObserver(pData->pDomain, pData->pObserver);
847 return NS_OK;
849 } else {
850 observer = pData->pObserver;
853 observer->Observe(static_cast<nsIPrefBranch *>(pData->pBranch),
854 NS_PREFBRANCH_PREFCHANGE_TOPIC_ID,
855 NS_ConvertASCIItoUTF16(suffix).get());
856 return NS_OK;
860 void nsPrefBranch::freeObserverList(void)
862 const char *pref;
863 PrefCallbackData *pCallback;
865 if (mObservers) {
866 // unregister the observers
867 PRInt32 count;
869 count = mObservers->Count();
870 if (count > 0) {
871 PRInt32 i;
872 nsCAutoString domain;
873 for (i = 0; i < count; ++i) {
874 pCallback = (PrefCallbackData *)mObservers->ElementAt(i);
875 if (pCallback) {
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);
884 } else {
885 NS_RELEASE(pCallback->pObserver);
887 nsMemory::Free(pCallback);
891 delete mObservers;
892 mObservers = 0;
896 nsresult nsPrefBranch::GetDefaultFromPropertiesFile(const char *aPrefName, PRUnichar **return_buf)
898 nsresult rv;
900 // the default value contains a URL to a .properties file
902 nsXPIDLCString propertyFileURL;
903 rv = PREF_CopyCharPref(aPrefName, getter_Copies(propertyFileURL), PR_TRUE);
904 if (NS_FAILED(rv))
905 return rv;
907 nsCOMPtr<nsIStringBundleService> bundleService =
908 mozilla::services::GetStringBundleService();
909 if (!bundleService)
910 return NS_ERROR_FAILURE;
912 nsCOMPtr<nsIStringBundle> bundle;
913 rv = bundleService->CreateBundle(propertyFileURL,
914 getter_AddRefs(bundle));
915 if (NS_FAILED(rv))
916 return rv;
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())
929 return aPrefName;
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)
977 NS_INTERFACE_MAP_END
979 nsresult nsPrefLocalizedString::Init()
981 nsresult rv;
982 mUnicodeString = do_CreateInstance(NS_SUPPORTS_STRING_CONTRACTID, &rv);
984 return rv;
987 NS_IMETHODIMP
988 nsPrefLocalizedString::GetData(PRUnichar **_retval)
990 nsAutoString data;
992 nsresult rv = GetData(data);
993 if (NS_FAILED(rv))
994 return rv;
996 *_retval = ToNewUnicode(data);
997 if (!*_retval)
998 return NS_ERROR_OUT_OF_MEMORY;
1000 return NS_OK;
1003 NS_IMETHODIMP
1004 nsPrefLocalizedString::SetData(const PRUnichar *aData)
1006 if (!aData)
1007 return SetData(EmptyString());
1008 return SetData(nsDependentString(aData));
1011 NS_IMETHODIMP
1012 nsPrefLocalizedString::SetDataWithLength(PRUint32 aLength,
1013 const PRUnichar *aData)
1015 if (!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);
1037 *aFile = mFile;
1038 NS_IF_ADDREF(*aFile);
1039 return NS_OK;
1042 NS_IMETHODIMP nsRelativeFilePref::SetFile(nsILocalFile *aFile)
1044 mFile = aFile;
1045 return NS_OK;
1048 NS_IMETHODIMP nsRelativeFilePref::GetRelativeToKey(nsACString& aRelativeToKey)
1050 aRelativeToKey.Assign(mRelativeToKey);
1051 return NS_OK;
1054 NS_IMETHODIMP nsRelativeFilePref::SetRelativeToKey(const nsACString& aRelativeToKey)
1056 mRelativeToKey.Assign(aRelativeToKey);
1057 return NS_OK;