michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "mozilla/dom/ContentChild.h" michael@0: #include "nsXULAppAPI.h" michael@0: michael@0: #include "nsPrefBranch.h" michael@0: #include "nsILocalFile.h" // nsILocalFile used for backwards compatibility michael@0: #include "nsIObserverService.h" michael@0: #include "nsXPCOM.h" michael@0: #include "nsISupportsPrimitives.h" michael@0: #include "nsIDirectoryService.h" michael@0: #include "nsString.h" michael@0: #include "nsReadableUtils.h" michael@0: #include "nsXPIDLString.h" michael@0: #include "nsPrintfCString.h" michael@0: #include "nsIStringBundle.h" michael@0: #include "prefapi.h" michael@0: #include "pldhash.h" michael@0: michael@0: #include "nsCRT.h" michael@0: #include "mozilla/Services.h" michael@0: michael@0: #include "prefapi_private_data.h" michael@0: michael@0: #ifdef MOZ_CRASHREPORTER michael@0: #include "nsICrashReporter.h" michael@0: #endif michael@0: michael@0: #include "nsIConsoleService.h" michael@0: michael@0: #ifdef DEBUG michael@0: #define ENSURE_MAIN_PROCESS(message, pref) do { \ michael@0: if (GetContentChild()) { \ michael@0: nsPrintfCString msg("ENSURE_MAIN_PROCESS failed. %s %s", message, pref); \ michael@0: NS_ERROR(msg.get()); \ michael@0: return NS_ERROR_NOT_AVAILABLE; \ michael@0: } \ michael@0: } while (0); michael@0: #else michael@0: #define ENSURE_MAIN_PROCESS(message, pref) \ michael@0: if (GetContentChild()) { \ michael@0: return NS_ERROR_NOT_AVAILABLE; \ michael@0: } michael@0: #endif michael@0: michael@0: // Definitions michael@0: struct EnumerateData { michael@0: const char *parent; michael@0: nsTArray *pref_list; michael@0: }; michael@0: michael@0: // Prototypes michael@0: static PLDHashOperator michael@0: pref_enumChild(PLDHashTable *table, PLDHashEntryHdr *heh, michael@0: uint32_t i, void *arg); michael@0: michael@0: using mozilla::dom::ContentChild; michael@0: michael@0: static ContentChild* michael@0: GetContentChild() michael@0: { michael@0: if (XRE_GetProcessType() == GeckoProcessType_Content) { michael@0: ContentChild* cpc = ContentChild::GetSingleton(); michael@0: if (!cpc) { michael@0: NS_RUNTIMEABORT("Content Protocol is NULL! We're going to crash!"); michael@0: } michael@0: return cpc; michael@0: } michael@0: return nullptr; michael@0: } michael@0: michael@0: /* michael@0: * Constructor/Destructor michael@0: */ michael@0: michael@0: nsPrefBranch::nsPrefBranch(const char *aPrefRoot, bool aDefaultBranch) michael@0: { michael@0: mPrefRoot = aPrefRoot; michael@0: mPrefRootLength = mPrefRoot.Length(); michael@0: mIsDefault = aDefaultBranch; michael@0: mFreeingObserverList = false; michael@0: michael@0: nsCOMPtr observerService = michael@0: mozilla::services::GetObserverService(); michael@0: if (observerService) { michael@0: ++mRefCnt; // Our refcnt must be > 0 when we call this, or we'll get deleted! michael@0: // add weak so we don't have to clean up at shutdown michael@0: observerService->AddObserver(this, NS_XPCOM_SHUTDOWN_OBSERVER_ID, true); michael@0: --mRefCnt; michael@0: } michael@0: } michael@0: michael@0: nsPrefBranch::~nsPrefBranch() michael@0: { michael@0: freeObserverList(); michael@0: michael@0: nsCOMPtr observerService = michael@0: mozilla::services::GetObserverService(); michael@0: if (observerService) michael@0: observerService->RemoveObserver(this, NS_XPCOM_SHUTDOWN_OBSERVER_ID); michael@0: } michael@0: michael@0: michael@0: /* michael@0: * nsISupports Implementation michael@0: */ michael@0: michael@0: NS_IMPL_ADDREF(nsPrefBranch) michael@0: NS_IMPL_RELEASE(nsPrefBranch) michael@0: michael@0: NS_INTERFACE_MAP_BEGIN(nsPrefBranch) michael@0: NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIPrefBranch) michael@0: NS_INTERFACE_MAP_ENTRY(nsIPrefBranch) michael@0: NS_INTERFACE_MAP_ENTRY_CONDITIONAL(nsIPrefBranch2, !mIsDefault) michael@0: NS_INTERFACE_MAP_ENTRY_CONDITIONAL(nsIPrefBranchInternal, !mIsDefault) michael@0: NS_INTERFACE_MAP_ENTRY(nsIObserver) michael@0: NS_INTERFACE_MAP_ENTRY(nsISupportsWeakReference) michael@0: NS_INTERFACE_MAP_END michael@0: michael@0: michael@0: /* michael@0: * nsIPrefBranch Implementation michael@0: */ michael@0: michael@0: NS_IMETHODIMP nsPrefBranch::GetRoot(char **aRoot) michael@0: { michael@0: NS_ENSURE_ARG_POINTER(aRoot); michael@0: mPrefRoot.Truncate(mPrefRootLength); michael@0: *aRoot = ToNewCString(mPrefRoot); michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP nsPrefBranch::GetPrefType(const char *aPrefName, int32_t *_retval) michael@0: { michael@0: NS_ENSURE_ARG(aPrefName); michael@0: const char *pref = getPrefName(aPrefName); michael@0: *_retval = PREF_GetPrefType(pref); michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP nsPrefBranch::GetBoolPref(const char *aPrefName, bool *_retval) michael@0: { michael@0: NS_ENSURE_ARG(aPrefName); michael@0: const char *pref = getPrefName(aPrefName); michael@0: return PREF_GetBoolPref(pref, _retval, mIsDefault); michael@0: } michael@0: michael@0: NS_IMETHODIMP nsPrefBranch::SetBoolPref(const char *aPrefName, bool aValue) michael@0: { michael@0: ENSURE_MAIN_PROCESS("Cannot SetBoolPref from content process:", aPrefName); michael@0: NS_ENSURE_ARG(aPrefName); michael@0: const char *pref = getPrefName(aPrefName); michael@0: return PREF_SetBoolPref(pref, aValue, mIsDefault); michael@0: } michael@0: michael@0: NS_IMETHODIMP nsPrefBranch::GetFloatPref(const char *aPrefName, float *_retval) michael@0: { michael@0: NS_ENSURE_ARG(aPrefName); michael@0: const char *pref = getPrefName(aPrefName); michael@0: nsAutoCString stringVal; michael@0: nsresult rv = GetCharPref(pref, getter_Copies(stringVal)); michael@0: if (NS_SUCCEEDED(rv)) { michael@0: *_retval = stringVal.ToFloat(&rv); michael@0: } michael@0: michael@0: return rv; michael@0: } michael@0: michael@0: NS_IMETHODIMP nsPrefBranch::GetCharPref(const char *aPrefName, char **_retval) michael@0: { michael@0: NS_ENSURE_ARG(aPrefName); michael@0: const char *pref = getPrefName(aPrefName); michael@0: return PREF_CopyCharPref(pref, _retval, mIsDefault); michael@0: } michael@0: michael@0: NS_IMETHODIMP nsPrefBranch::SetCharPref(const char *aPrefName, const char *aValue) michael@0: { michael@0: nsresult rv = CheckSanityOfStringLength(aPrefName, aValue); michael@0: if (NS_FAILED(rv)) { michael@0: return rv; michael@0: } michael@0: return SetCharPrefInternal(aPrefName, aValue); michael@0: } michael@0: michael@0: nsresult nsPrefBranch::SetCharPrefInternal(const char *aPrefName, const char *aValue) michael@0: michael@0: { michael@0: ENSURE_MAIN_PROCESS("Cannot SetCharPref from content process:", aPrefName); michael@0: NS_ENSURE_ARG(aPrefName); michael@0: NS_ENSURE_ARG(aValue); michael@0: const char *pref = getPrefName(aPrefName); michael@0: return PREF_SetCharPref(pref, aValue, mIsDefault); michael@0: } michael@0: michael@0: NS_IMETHODIMP nsPrefBranch::GetIntPref(const char *aPrefName, int32_t *_retval) michael@0: { michael@0: NS_ENSURE_ARG(aPrefName); michael@0: const char *pref = getPrefName(aPrefName); michael@0: return PREF_GetIntPref(pref, _retval, mIsDefault); michael@0: } michael@0: michael@0: NS_IMETHODIMP nsPrefBranch::SetIntPref(const char *aPrefName, int32_t aValue) michael@0: { michael@0: ENSURE_MAIN_PROCESS("Cannot SetIntPref from content process:", aPrefName); michael@0: NS_ENSURE_ARG(aPrefName); michael@0: const char *pref = getPrefName(aPrefName); michael@0: return PREF_SetIntPref(pref, aValue, mIsDefault); michael@0: } michael@0: michael@0: NS_IMETHODIMP nsPrefBranch::GetComplexValue(const char *aPrefName, const nsIID & aType, void **_retval) michael@0: { michael@0: NS_ENSURE_ARG(aPrefName); michael@0: michael@0: nsresult rv; michael@0: nsXPIDLCString utf8String; michael@0: michael@0: // we have to do this one first because it's different than all the rest michael@0: if (aType.Equals(NS_GET_IID(nsIPrefLocalizedString))) { michael@0: nsCOMPtr theString(do_CreateInstance(NS_PREFLOCALIZEDSTRING_CONTRACTID, &rv)); michael@0: if (NS_FAILED(rv)) return rv; michael@0: michael@0: const char *pref = getPrefName(aPrefName); michael@0: bool bNeedDefault = false; michael@0: michael@0: if (mIsDefault) { michael@0: bNeedDefault = true; michael@0: } else { michael@0: // if there is no user (or locked) value michael@0: if (!PREF_HasUserPref(pref) && !PREF_PrefIsLocked(pref)) { michael@0: bNeedDefault = true; michael@0: } michael@0: } michael@0: michael@0: // if we need to fetch the default value, do that instead, otherwise use the michael@0: // value we pulled in at the top of this function michael@0: if (bNeedDefault) { michael@0: nsXPIDLString utf16String; michael@0: rv = GetDefaultFromPropertiesFile(pref, getter_Copies(utf16String)); michael@0: if (NS_SUCCEEDED(rv)) { michael@0: theString->SetData(utf16String.get()); michael@0: } michael@0: } else { michael@0: rv = GetCharPref(aPrefName, getter_Copies(utf8String)); michael@0: if (NS_SUCCEEDED(rv)) { michael@0: theString->SetData(NS_ConvertUTF8toUTF16(utf8String).get()); michael@0: } michael@0: } michael@0: michael@0: if (NS_SUCCEEDED(rv)) { michael@0: theString.forget(reinterpret_cast(_retval)); michael@0: } michael@0: michael@0: return rv; michael@0: } michael@0: michael@0: // if we can't get the pref, there's no point in being here michael@0: rv = GetCharPref(aPrefName, getter_Copies(utf8String)); michael@0: if (NS_FAILED(rv)) { michael@0: return rv; michael@0: } michael@0: michael@0: // also check nsILocalFile, for backwards compatibility michael@0: if (aType.Equals(NS_GET_IID(nsIFile)) || aType.Equals(NS_GET_IID(nsILocalFile))) { michael@0: if (GetContentChild()) { michael@0: NS_ERROR("cannot get nsIFile pref from content process"); michael@0: return NS_ERROR_NOT_AVAILABLE; michael@0: } michael@0: michael@0: nsCOMPtr file(do_CreateInstance(NS_LOCAL_FILE_CONTRACTID, &rv)); michael@0: michael@0: if (NS_SUCCEEDED(rv)) { michael@0: rv = file->SetPersistentDescriptor(utf8String); michael@0: if (NS_SUCCEEDED(rv)) { michael@0: file.forget(reinterpret_cast(_retval)); michael@0: return NS_OK; michael@0: } michael@0: } michael@0: return rv; michael@0: } michael@0: michael@0: if (aType.Equals(NS_GET_IID(nsIRelativeFilePref))) { michael@0: if (GetContentChild()) { michael@0: NS_ERROR("cannot get nsIRelativeFilePref from content process"); michael@0: return NS_ERROR_NOT_AVAILABLE; michael@0: } michael@0: michael@0: nsACString::const_iterator keyBegin, strEnd; michael@0: utf8String.BeginReading(keyBegin); michael@0: utf8String.EndReading(strEnd); michael@0: michael@0: // The pref has the format: [fromKey]a/b/c michael@0: if (*keyBegin++ != '[') michael@0: return NS_ERROR_FAILURE; michael@0: nsACString::const_iterator keyEnd(keyBegin); michael@0: if (!FindCharInReadable(']', keyEnd, strEnd)) michael@0: return NS_ERROR_FAILURE; michael@0: nsAutoCString key(Substring(keyBegin, keyEnd)); michael@0: michael@0: nsCOMPtr fromFile; michael@0: nsCOMPtr directoryService(do_GetService(NS_DIRECTORY_SERVICE_CONTRACTID, &rv)); michael@0: if (NS_FAILED(rv)) michael@0: return rv; michael@0: rv = directoryService->Get(key.get(), NS_GET_IID(nsIFile), getter_AddRefs(fromFile)); michael@0: if (NS_FAILED(rv)) michael@0: return rv; michael@0: michael@0: nsCOMPtr theFile; michael@0: rv = NS_NewNativeLocalFile(EmptyCString(), true, getter_AddRefs(theFile)); michael@0: if (NS_FAILED(rv)) michael@0: return rv; michael@0: rv = theFile->SetRelativeDescriptor(fromFile, Substring(++keyEnd, strEnd)); michael@0: if (NS_FAILED(rv)) michael@0: return rv; michael@0: nsCOMPtr relativePref; michael@0: rv = NS_NewRelativeFilePref(theFile, key, getter_AddRefs(relativePref)); michael@0: if (NS_FAILED(rv)) michael@0: return rv; michael@0: michael@0: relativePref.forget(reinterpret_cast(_retval)); michael@0: return NS_OK; michael@0: } michael@0: michael@0: if (aType.Equals(NS_GET_IID(nsISupportsString))) { michael@0: nsCOMPtr theString(do_CreateInstance(NS_SUPPORTS_STRING_CONTRACTID, &rv)); michael@0: michael@0: if (NS_SUCCEEDED(rv)) { michael@0: // Debugging to see why we end up with very long strings here with michael@0: // some addons, see bug 836263. michael@0: nsAutoString wdata; michael@0: if (!AppendUTF8toUTF16(utf8String, wdata, mozilla::fallible_t())) { michael@0: #ifdef MOZ_CRASHREPORTER michael@0: nsCOMPtr cr = michael@0: do_GetService("@mozilla.org/toolkit/crash-reporter;1"); michael@0: if (cr) { michael@0: cr->AnnotateCrashReport(NS_LITERAL_CSTRING("bug836263-size"), michael@0: nsPrintfCString("%x", utf8String.Length())); michael@0: cr->RegisterAppMemory(uint64_t(utf8String.BeginReading()), michael@0: std::min(0x1000U, utf8String.Length())); michael@0: } michael@0: #endif michael@0: NS_RUNTIMEABORT("bug836263"); michael@0: } michael@0: theString->SetData(wdata); michael@0: theString.forget(reinterpret_cast(_retval)); michael@0: } michael@0: return rv; michael@0: } michael@0: michael@0: NS_WARNING("nsPrefBranch::GetComplexValue - Unsupported interface type"); michael@0: return NS_NOINTERFACE; michael@0: } michael@0: michael@0: nsresult nsPrefBranch::CheckSanityOfStringLength(const char* aPrefName, const char* aValue) { michael@0: if (!aValue) { michael@0: return NS_OK; michael@0: } michael@0: return CheckSanityOfStringLength(aPrefName, strlen(aValue)); michael@0: } michael@0: michael@0: nsresult nsPrefBranch::CheckSanityOfStringLength(const char* aPrefName, const nsAString& aValue) { michael@0: return CheckSanityOfStringLength(aPrefName, aValue.Length()); michael@0: } michael@0: michael@0: nsresult nsPrefBranch::CheckSanityOfStringLength(const char* aPrefName, const uint32_t aLength) { michael@0: if (aLength > MAX_PREF_LENGTH) { michael@0: return NS_ERROR_ILLEGAL_VALUE; michael@0: } michael@0: if (aLength <= MAX_ADVISABLE_PREF_LENGTH) { michael@0: return NS_OK; michael@0: } michael@0: nsresult rv; michael@0: nsCOMPtr console = do_GetService("@mozilla.org/consoleservice;1", &rv); michael@0: if (NS_FAILED(rv)) { michael@0: return rv; michael@0: } michael@0: nsAutoCString message(nsPrintfCString("Warning: attempting to write %d bytes to preference %s. This is bad for general performance and memory usage. Such an amount of data should rather be written to an external file.", michael@0: aLength, michael@0: aPrefName)); michael@0: rv = console->LogStringMessage(NS_ConvertUTF8toUTF16(message).get()); michael@0: if (NS_FAILED(rv)) { michael@0: return rv; michael@0: } michael@0: return NS_OK; michael@0: } michael@0: michael@0: michael@0: NS_IMETHODIMP nsPrefBranch::SetComplexValue(const char *aPrefName, const nsIID & aType, nsISupports *aValue) michael@0: { michael@0: ENSURE_MAIN_PROCESS("Cannot SetComplexValue from content process:", aPrefName); michael@0: NS_ENSURE_ARG(aPrefName); michael@0: michael@0: nsresult rv = NS_NOINTERFACE; michael@0: michael@0: // also check nsILocalFile, for backwards compatibility michael@0: if (aType.Equals(NS_GET_IID(nsIFile)) || aType.Equals(NS_GET_IID(nsILocalFile))) { michael@0: nsCOMPtr file = do_QueryInterface(aValue); michael@0: if (!file) michael@0: return NS_NOINTERFACE; michael@0: nsAutoCString descriptorString; michael@0: michael@0: rv = file->GetPersistentDescriptor(descriptorString); michael@0: if (NS_SUCCEEDED(rv)) { michael@0: rv = SetCharPrefInternal(aPrefName, descriptorString.get()); michael@0: } michael@0: return rv; michael@0: } michael@0: michael@0: if (aType.Equals(NS_GET_IID(nsIRelativeFilePref))) { michael@0: nsCOMPtr relFilePref = do_QueryInterface(aValue); michael@0: if (!relFilePref) michael@0: return NS_NOINTERFACE; michael@0: michael@0: nsCOMPtr file; michael@0: relFilePref->GetFile(getter_AddRefs(file)); michael@0: if (!file) michael@0: return NS_NOINTERFACE; michael@0: nsAutoCString relativeToKey; michael@0: (void) relFilePref->GetRelativeToKey(relativeToKey); michael@0: michael@0: nsCOMPtr relativeToFile; michael@0: nsCOMPtr directoryService(do_GetService(NS_DIRECTORY_SERVICE_CONTRACTID, &rv)); michael@0: if (NS_FAILED(rv)) michael@0: return rv; michael@0: rv = directoryService->Get(relativeToKey.get(), NS_GET_IID(nsIFile), getter_AddRefs(relativeToFile)); michael@0: if (NS_FAILED(rv)) michael@0: return rv; michael@0: michael@0: nsAutoCString relDescriptor; michael@0: rv = file->GetRelativeDescriptor(relativeToFile, relDescriptor); michael@0: if (NS_FAILED(rv)) michael@0: return rv; michael@0: michael@0: nsAutoCString descriptorString; michael@0: descriptorString.Append('['); michael@0: descriptorString.Append(relativeToKey); michael@0: descriptorString.Append(']'); michael@0: descriptorString.Append(relDescriptor); michael@0: return SetCharPrefInternal(aPrefName, descriptorString.get()); michael@0: } michael@0: michael@0: if (aType.Equals(NS_GET_IID(nsISupportsString))) { michael@0: nsCOMPtr theString = do_QueryInterface(aValue); michael@0: michael@0: if (theString) { michael@0: nsString wideString; michael@0: michael@0: rv = theString->GetData(wideString); michael@0: if (NS_SUCCEEDED(rv)) { michael@0: // Check sanity of string length before any lengthy conversion michael@0: rv = CheckSanityOfStringLength(aPrefName, wideString); michael@0: if (NS_FAILED(rv)) { michael@0: return rv; michael@0: } michael@0: rv = SetCharPrefInternal(aPrefName, NS_ConvertUTF16toUTF8(wideString).get()); michael@0: } michael@0: } michael@0: return rv; michael@0: } michael@0: michael@0: if (aType.Equals(NS_GET_IID(nsIPrefLocalizedString))) { michael@0: nsCOMPtr theString = do_QueryInterface(aValue); michael@0: michael@0: if (theString) { michael@0: nsXPIDLString wideString; michael@0: michael@0: rv = theString->GetData(getter_Copies(wideString)); michael@0: if (NS_SUCCEEDED(rv)) { michael@0: // Check sanity of string length before any lengthy conversion michael@0: rv = CheckSanityOfStringLength(aPrefName, wideString); michael@0: if (NS_FAILED(rv)) { michael@0: return rv; michael@0: } michael@0: rv = SetCharPrefInternal(aPrefName, NS_ConvertUTF16toUTF8(wideString).get()); michael@0: } michael@0: } michael@0: return rv; michael@0: } michael@0: michael@0: NS_WARNING("nsPrefBranch::SetComplexValue - Unsupported interface type"); michael@0: return NS_NOINTERFACE; michael@0: } michael@0: michael@0: NS_IMETHODIMP nsPrefBranch::ClearUserPref(const char *aPrefName) michael@0: { michael@0: ENSURE_MAIN_PROCESS("Cannot ClearUserPref from content process:", aPrefName); michael@0: NS_ENSURE_ARG(aPrefName); michael@0: const char *pref = getPrefName(aPrefName); michael@0: return PREF_ClearUserPref(pref); michael@0: } michael@0: michael@0: NS_IMETHODIMP nsPrefBranch::PrefHasUserValue(const char *aPrefName, bool *_retval) michael@0: { michael@0: NS_ENSURE_ARG_POINTER(_retval); michael@0: NS_ENSURE_ARG(aPrefName); michael@0: const char *pref = getPrefName(aPrefName); michael@0: *_retval = PREF_HasUserPref(pref); michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP nsPrefBranch::LockPref(const char *aPrefName) michael@0: { michael@0: ENSURE_MAIN_PROCESS("Cannot LockPref from content process:", aPrefName); michael@0: NS_ENSURE_ARG(aPrefName); michael@0: const char *pref = getPrefName(aPrefName); michael@0: return PREF_LockPref(pref, true); michael@0: } michael@0: michael@0: NS_IMETHODIMP nsPrefBranch::PrefIsLocked(const char *aPrefName, bool *_retval) michael@0: { michael@0: ENSURE_MAIN_PROCESS("Cannot check PrefIsLocked from content process:", aPrefName); michael@0: NS_ENSURE_ARG_POINTER(_retval); michael@0: NS_ENSURE_ARG(aPrefName); michael@0: const char *pref = getPrefName(aPrefName); michael@0: *_retval = PREF_PrefIsLocked(pref); michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP nsPrefBranch::UnlockPref(const char *aPrefName) michael@0: { michael@0: ENSURE_MAIN_PROCESS("Cannot UnlockPref from content process:", aPrefName); michael@0: NS_ENSURE_ARG(aPrefName); michael@0: const char *pref = getPrefName(aPrefName); michael@0: return PREF_LockPref(pref, false); michael@0: } michael@0: michael@0: /* void resetBranch (in string startingAt); */ michael@0: NS_IMETHODIMP nsPrefBranch::ResetBranch(const char *aStartingAt) michael@0: { michael@0: return NS_ERROR_NOT_IMPLEMENTED; michael@0: } michael@0: michael@0: NS_IMETHODIMP nsPrefBranch::DeleteBranch(const char *aStartingAt) michael@0: { michael@0: ENSURE_MAIN_PROCESS("Cannot DeleteBranch from content process:", aStartingAt); michael@0: NS_ENSURE_ARG(aStartingAt); michael@0: const char *pref = getPrefName(aStartingAt); michael@0: return PREF_DeleteBranch(pref); michael@0: } michael@0: michael@0: NS_IMETHODIMP nsPrefBranch::GetChildList(const char *aStartingAt, uint32_t *aCount, char ***aChildArray) michael@0: { michael@0: char **outArray; michael@0: int32_t numPrefs; michael@0: int32_t dwIndex; michael@0: EnumerateData ed; michael@0: nsAutoTArray prefArray; michael@0: michael@0: NS_ENSURE_ARG(aStartingAt); michael@0: NS_ENSURE_ARG_POINTER(aCount); michael@0: NS_ENSURE_ARG_POINTER(aChildArray); michael@0: michael@0: *aChildArray = nullptr; michael@0: *aCount = 0; michael@0: michael@0: if (!gHashTable.ops) michael@0: return NS_ERROR_NOT_INITIALIZED; michael@0: michael@0: // this will contain a list of all the pref name strings michael@0: // allocate on the stack for speed michael@0: michael@0: ed.parent = getPrefName(aStartingAt); michael@0: ed.pref_list = &prefArray; michael@0: PL_DHashTableEnumerate(&gHashTable, pref_enumChild, &ed); michael@0: michael@0: // now that we've built up the list, run the callback on michael@0: // all the matching elements michael@0: numPrefs = prefArray.Length(); michael@0: michael@0: if (numPrefs) { michael@0: outArray = (char **)nsMemory::Alloc(numPrefs * sizeof(char *)); michael@0: if (!outArray) michael@0: return NS_ERROR_OUT_OF_MEMORY; michael@0: michael@0: for (dwIndex = 0; dwIndex < numPrefs; ++dwIndex) { michael@0: // we need to lop off mPrefRoot in case the user is planning to pass this michael@0: // back to us because if they do we are going to add mPrefRoot again. michael@0: const nsCString& element = prefArray[dwIndex]; michael@0: outArray[dwIndex] = (char *)nsMemory::Clone( michael@0: element.get() + mPrefRootLength, element.Length() - mPrefRootLength + 1); michael@0: michael@0: if (!outArray[dwIndex]) { michael@0: // we ran out of memory... this is annoying michael@0: NS_FREE_XPCOM_ALLOCATED_POINTER_ARRAY(dwIndex, outArray); michael@0: return NS_ERROR_OUT_OF_MEMORY; michael@0: } michael@0: } michael@0: *aChildArray = outArray; michael@0: } michael@0: *aCount = numPrefs; michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP nsPrefBranch::AddObserver(const char *aDomain, nsIObserver *aObserver, bool aHoldWeak) michael@0: { michael@0: PrefCallback *pCallback; michael@0: const char *pref; michael@0: michael@0: NS_ENSURE_ARG(aDomain); michael@0: NS_ENSURE_ARG(aObserver); michael@0: michael@0: // hold a weak reference to the observer if so requested michael@0: if (aHoldWeak) { michael@0: nsCOMPtr weakRefFactory = do_QueryInterface(aObserver); michael@0: if (!weakRefFactory) { michael@0: // the caller didn't give us a object that supports weak reference... tell them michael@0: return NS_ERROR_INVALID_ARG; michael@0: } michael@0: michael@0: // Construct a PrefCallback with a weak reference to the observer. michael@0: pCallback = new PrefCallback(aDomain, weakRefFactory, this); michael@0: michael@0: } else { michael@0: // Construct a PrefCallback with a strong reference to the observer. michael@0: pCallback = new PrefCallback(aDomain, aObserver, this); michael@0: } michael@0: michael@0: if (mObservers.Get(pCallback)) { michael@0: NS_WARNING("Ignoring duplicate observer."); michael@0: delete pCallback; michael@0: return NS_OK; michael@0: } michael@0: michael@0: mObservers.Put(pCallback, pCallback); michael@0: michael@0: // We must pass a fully qualified preference name to the callback michael@0: // aDomain == nullptr is the only possible failure, and we trapped it with michael@0: // NS_ENSURE_ARG above. michael@0: pref = getPrefName(aDomain); michael@0: PREF_RegisterCallback(pref, NotifyObserver, pCallback); michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP nsPrefBranch::RemoveObserver(const char *aDomain, nsIObserver *aObserver) michael@0: { michael@0: NS_ENSURE_ARG(aDomain); michael@0: NS_ENSURE_ARG(aObserver); michael@0: michael@0: nsresult rv = NS_OK; michael@0: michael@0: // If we're in the middle of a call to freeObserverList, don't process this michael@0: // RemoveObserver call -- the observer in question will be removed soon, if michael@0: // it hasn't been already. michael@0: // michael@0: // It's important that we don't touch mObservers in any way -- even a Get() michael@0: // which retuns null might cause the hashtable to resize itself, which will michael@0: // break the Enumerator in freeObserverList. michael@0: if (mFreeingObserverList) michael@0: return NS_OK; michael@0: michael@0: // Remove the relevant PrefCallback from mObservers and get an owning michael@0: // pointer to it. Unregister the callback first, and then let the owning michael@0: // pointer go out of scope and destroy the callback. michael@0: PrefCallback key(aDomain, aObserver, this); michael@0: nsAutoPtr pCallback; michael@0: mObservers.RemoveAndForget(&key, pCallback); michael@0: if (pCallback) { michael@0: // aDomain == nullptr is the only possible failure, trapped above michael@0: const char *pref = getPrefName(aDomain); michael@0: rv = PREF_UnregisterCallback(pref, NotifyObserver, pCallback); michael@0: } michael@0: michael@0: return rv; michael@0: } michael@0: michael@0: NS_IMETHODIMP nsPrefBranch::Observe(nsISupports *aSubject, const char *aTopic, const char16_t *someData) michael@0: { michael@0: // watch for xpcom shutdown and free our observers to eliminate any cyclic references michael@0: if (!nsCRT::strcmp(aTopic, NS_XPCOM_SHUTDOWN_OBSERVER_ID)) { michael@0: freeObserverList(); michael@0: } michael@0: return NS_OK; michael@0: } michael@0: michael@0: /* static */ michael@0: void nsPrefBranch::NotifyObserver(const char *newpref, void *data) michael@0: { michael@0: PrefCallback *pCallback = (PrefCallback *)data; michael@0: michael@0: nsCOMPtr observer = pCallback->GetObserver(); michael@0: if (!observer) { michael@0: // The observer has expired. Let's remove this callback. michael@0: pCallback->GetPrefBranch()->RemoveExpiredCallback(pCallback); michael@0: return; michael@0: } michael@0: michael@0: // remove any root this string may contain so as to not confuse the observer michael@0: // by passing them something other than what they passed us as a topic michael@0: uint32_t len = pCallback->GetPrefBranch()->GetRootLength(); michael@0: nsAutoCString suffix(newpref + len); michael@0: michael@0: observer->Observe(static_cast(pCallback->GetPrefBranch()), michael@0: NS_PREFBRANCH_PREFCHANGE_TOPIC_ID, michael@0: NS_ConvertASCIItoUTF16(suffix).get()); michael@0: } michael@0: michael@0: PLDHashOperator michael@0: FreeObserverFunc(PrefCallback *aKey, michael@0: nsAutoPtr &aCallback, michael@0: void *aArgs) michael@0: { michael@0: // Calling NS_RELEASE below might trigger a call to michael@0: // nsPrefBranch::RemoveObserver, since some classes remove themselves from michael@0: // the pref branch on destruction. We don't need to worry about this causing michael@0: // double-frees, however, because freeObserverList sets mFreeingObserverList michael@0: // to true, which prevents RemoveObserver calls from doing anything. michael@0: michael@0: nsPrefBranch *prefBranch = aCallback->GetPrefBranch(); michael@0: const char *pref = prefBranch->getPrefName(aCallback->GetDomain().get()); michael@0: PREF_UnregisterCallback(pref, nsPrefBranch::NotifyObserver, aCallback); michael@0: michael@0: return PL_DHASH_REMOVE; michael@0: } michael@0: michael@0: void nsPrefBranch::freeObserverList(void) michael@0: { michael@0: // We need to prevent anyone from modifying mObservers while we're michael@0: // enumerating over it. In particular, some clients will call michael@0: // RemoveObserver() when they're destructed; we need to keep those calls from michael@0: // touching mObservers. michael@0: mFreeingObserverList = true; michael@0: mObservers.Enumerate(&FreeObserverFunc, nullptr); michael@0: mFreeingObserverList = false; michael@0: } michael@0: michael@0: void michael@0: nsPrefBranch::RemoveExpiredCallback(PrefCallback *aCallback) michael@0: { michael@0: NS_PRECONDITION(aCallback->IsExpired(), "Callback should be expired."); michael@0: mObservers.Remove(aCallback); michael@0: } michael@0: michael@0: nsresult nsPrefBranch::GetDefaultFromPropertiesFile(const char *aPrefName, char16_t **return_buf) michael@0: { michael@0: nsresult rv; michael@0: michael@0: // the default value contains a URL to a .properties file michael@0: michael@0: nsXPIDLCString propertyFileURL; michael@0: rv = PREF_CopyCharPref(aPrefName, getter_Copies(propertyFileURL), true); michael@0: if (NS_FAILED(rv)) michael@0: return rv; michael@0: michael@0: nsCOMPtr bundleService = michael@0: mozilla::services::GetStringBundleService(); michael@0: if (!bundleService) michael@0: return NS_ERROR_FAILURE; michael@0: michael@0: nsCOMPtr bundle; michael@0: rv = bundleService->CreateBundle(propertyFileURL, michael@0: getter_AddRefs(bundle)); michael@0: if (NS_FAILED(rv)) michael@0: return rv; michael@0: michael@0: // string names are in unicode michael@0: nsAutoString stringId; michael@0: stringId.AssignASCII(aPrefName); michael@0: michael@0: return bundle->GetStringFromName(stringId.get(), return_buf); michael@0: } michael@0: michael@0: const char *nsPrefBranch::getPrefName(const char *aPrefName) michael@0: { michael@0: NS_ASSERTION(aPrefName, "null pref name!"); michael@0: michael@0: // for speed, avoid strcpy if we can: michael@0: if (mPrefRoot.IsEmpty()) michael@0: return aPrefName; michael@0: michael@0: // isn't there a better way to do this? this is really kind of gross. michael@0: mPrefRoot.Truncate(mPrefRootLength); michael@0: mPrefRoot.Append(aPrefName); michael@0: return mPrefRoot.get(); michael@0: } michael@0: michael@0: static PLDHashOperator michael@0: pref_enumChild(PLDHashTable *table, PLDHashEntryHdr *heh, michael@0: uint32_t i, void *arg) michael@0: { michael@0: PrefHashEntry *he = static_cast(heh); michael@0: EnumerateData *d = reinterpret_cast(arg); michael@0: if (strncmp(he->key, d->parent, strlen(d->parent)) == 0) { michael@0: d->pref_list->AppendElement(he->key); michael@0: } michael@0: return PL_DHASH_NEXT; michael@0: } michael@0: michael@0: //---------------------------------------------------------------------------- michael@0: // nsPrefLocalizedString michael@0: //---------------------------------------------------------------------------- michael@0: michael@0: nsPrefLocalizedString::nsPrefLocalizedString() michael@0: { michael@0: } michael@0: michael@0: nsPrefLocalizedString::~nsPrefLocalizedString() michael@0: { michael@0: } michael@0: michael@0: michael@0: /* michael@0: * nsISupports Implementation michael@0: */ michael@0: michael@0: NS_IMPL_ADDREF(nsPrefLocalizedString) michael@0: NS_IMPL_RELEASE(nsPrefLocalizedString) michael@0: michael@0: NS_INTERFACE_MAP_BEGIN(nsPrefLocalizedString) michael@0: NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIPrefLocalizedString) michael@0: NS_INTERFACE_MAP_ENTRY(nsIPrefLocalizedString) michael@0: NS_INTERFACE_MAP_ENTRY(nsISupportsString) michael@0: NS_INTERFACE_MAP_END michael@0: michael@0: nsresult nsPrefLocalizedString::Init() michael@0: { michael@0: nsresult rv; michael@0: mUnicodeString = do_CreateInstance(NS_SUPPORTS_STRING_CONTRACTID, &rv); michael@0: michael@0: return rv; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsPrefLocalizedString::GetData(char16_t **_retval) michael@0: { michael@0: nsAutoString data; michael@0: michael@0: nsresult rv = GetData(data); michael@0: if (NS_FAILED(rv)) michael@0: return rv; michael@0: michael@0: *_retval = ToNewUnicode(data); michael@0: if (!*_retval) michael@0: return NS_ERROR_OUT_OF_MEMORY; michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsPrefLocalizedString::SetData(const char16_t *aData) michael@0: { michael@0: if (!aData) michael@0: return SetData(EmptyString()); michael@0: return SetData(nsDependentString(aData)); michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsPrefLocalizedString::SetDataWithLength(uint32_t aLength, michael@0: const char16_t *aData) michael@0: { michael@0: if (!aData) michael@0: return SetData(EmptyString()); michael@0: return SetData(Substring(aData, aLength)); michael@0: } michael@0: michael@0: //---------------------------------------------------------------------------- michael@0: // nsRelativeFilePref michael@0: //---------------------------------------------------------------------------- michael@0: michael@0: NS_IMPL_ISUPPORTS(nsRelativeFilePref, nsIRelativeFilePref) michael@0: michael@0: nsRelativeFilePref::nsRelativeFilePref() michael@0: { michael@0: } michael@0: michael@0: nsRelativeFilePref::~nsRelativeFilePref() michael@0: { michael@0: } michael@0: michael@0: NS_IMETHODIMP nsRelativeFilePref::GetFile(nsIFile **aFile) michael@0: { michael@0: NS_ENSURE_ARG_POINTER(aFile); michael@0: *aFile = mFile; michael@0: NS_IF_ADDREF(*aFile); michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP nsRelativeFilePref::SetFile(nsIFile *aFile) michael@0: { michael@0: mFile = aFile; michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP nsRelativeFilePref::GetRelativeToKey(nsACString& aRelativeToKey) michael@0: { michael@0: aRelativeToKey.Assign(mRelativeToKey); michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP nsRelativeFilePref::SetRelativeToKey(const nsACString& aRelativeToKey) michael@0: { michael@0: mRelativeToKey.Assign(aRelativeToKey); michael@0: return NS_OK; michael@0: } michael@0: michael@0: #undef ENSURE_MAIN_PROCESS