modules/libpref/src/nsPrefBranch.cpp

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.

michael@0 1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 2 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5
michael@0 6 #include "mozilla/dom/ContentChild.h"
michael@0 7 #include "nsXULAppAPI.h"
michael@0 8
michael@0 9 #include "nsPrefBranch.h"
michael@0 10 #include "nsILocalFile.h" // nsILocalFile used for backwards compatibility
michael@0 11 #include "nsIObserverService.h"
michael@0 12 #include "nsXPCOM.h"
michael@0 13 #include "nsISupportsPrimitives.h"
michael@0 14 #include "nsIDirectoryService.h"
michael@0 15 #include "nsString.h"
michael@0 16 #include "nsReadableUtils.h"
michael@0 17 #include "nsXPIDLString.h"
michael@0 18 #include "nsPrintfCString.h"
michael@0 19 #include "nsIStringBundle.h"
michael@0 20 #include "prefapi.h"
michael@0 21 #include "pldhash.h"
michael@0 22
michael@0 23 #include "nsCRT.h"
michael@0 24 #include "mozilla/Services.h"
michael@0 25
michael@0 26 #include "prefapi_private_data.h"
michael@0 27
michael@0 28 #ifdef MOZ_CRASHREPORTER
michael@0 29 #include "nsICrashReporter.h"
michael@0 30 #endif
michael@0 31
michael@0 32 #include "nsIConsoleService.h"
michael@0 33
michael@0 34 #ifdef DEBUG
michael@0 35 #define ENSURE_MAIN_PROCESS(message, pref) do { \
michael@0 36 if (GetContentChild()) { \
michael@0 37 nsPrintfCString msg("ENSURE_MAIN_PROCESS failed. %s %s", message, pref); \
michael@0 38 NS_ERROR(msg.get()); \
michael@0 39 return NS_ERROR_NOT_AVAILABLE; \
michael@0 40 } \
michael@0 41 } while (0);
michael@0 42 #else
michael@0 43 #define ENSURE_MAIN_PROCESS(message, pref) \
michael@0 44 if (GetContentChild()) { \
michael@0 45 return NS_ERROR_NOT_AVAILABLE; \
michael@0 46 }
michael@0 47 #endif
michael@0 48
michael@0 49 // Definitions
michael@0 50 struct EnumerateData {
michael@0 51 const char *parent;
michael@0 52 nsTArray<nsCString> *pref_list;
michael@0 53 };
michael@0 54
michael@0 55 // Prototypes
michael@0 56 static PLDHashOperator
michael@0 57 pref_enumChild(PLDHashTable *table, PLDHashEntryHdr *heh,
michael@0 58 uint32_t i, void *arg);
michael@0 59
michael@0 60 using mozilla::dom::ContentChild;
michael@0 61
michael@0 62 static ContentChild*
michael@0 63 GetContentChild()
michael@0 64 {
michael@0 65 if (XRE_GetProcessType() == GeckoProcessType_Content) {
michael@0 66 ContentChild* cpc = ContentChild::GetSingleton();
michael@0 67 if (!cpc) {
michael@0 68 NS_RUNTIMEABORT("Content Protocol is NULL! We're going to crash!");
michael@0 69 }
michael@0 70 return cpc;
michael@0 71 }
michael@0 72 return nullptr;
michael@0 73 }
michael@0 74
michael@0 75 /*
michael@0 76 * Constructor/Destructor
michael@0 77 */
michael@0 78
michael@0 79 nsPrefBranch::nsPrefBranch(const char *aPrefRoot, bool aDefaultBranch)
michael@0 80 {
michael@0 81 mPrefRoot = aPrefRoot;
michael@0 82 mPrefRootLength = mPrefRoot.Length();
michael@0 83 mIsDefault = aDefaultBranch;
michael@0 84 mFreeingObserverList = false;
michael@0 85
michael@0 86 nsCOMPtr<nsIObserverService> observerService =
michael@0 87 mozilla::services::GetObserverService();
michael@0 88 if (observerService) {
michael@0 89 ++mRefCnt; // Our refcnt must be > 0 when we call this, or we'll get deleted!
michael@0 90 // add weak so we don't have to clean up at shutdown
michael@0 91 observerService->AddObserver(this, NS_XPCOM_SHUTDOWN_OBSERVER_ID, true);
michael@0 92 --mRefCnt;
michael@0 93 }
michael@0 94 }
michael@0 95
michael@0 96 nsPrefBranch::~nsPrefBranch()
michael@0 97 {
michael@0 98 freeObserverList();
michael@0 99
michael@0 100 nsCOMPtr<nsIObserverService> observerService =
michael@0 101 mozilla::services::GetObserverService();
michael@0 102 if (observerService)
michael@0 103 observerService->RemoveObserver(this, NS_XPCOM_SHUTDOWN_OBSERVER_ID);
michael@0 104 }
michael@0 105
michael@0 106
michael@0 107 /*
michael@0 108 * nsISupports Implementation
michael@0 109 */
michael@0 110
michael@0 111 NS_IMPL_ADDREF(nsPrefBranch)
michael@0 112 NS_IMPL_RELEASE(nsPrefBranch)
michael@0 113
michael@0 114 NS_INTERFACE_MAP_BEGIN(nsPrefBranch)
michael@0 115 NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIPrefBranch)
michael@0 116 NS_INTERFACE_MAP_ENTRY(nsIPrefBranch)
michael@0 117 NS_INTERFACE_MAP_ENTRY_CONDITIONAL(nsIPrefBranch2, !mIsDefault)
michael@0 118 NS_INTERFACE_MAP_ENTRY_CONDITIONAL(nsIPrefBranchInternal, !mIsDefault)
michael@0 119 NS_INTERFACE_MAP_ENTRY(nsIObserver)
michael@0 120 NS_INTERFACE_MAP_ENTRY(nsISupportsWeakReference)
michael@0 121 NS_INTERFACE_MAP_END
michael@0 122
michael@0 123
michael@0 124 /*
michael@0 125 * nsIPrefBranch Implementation
michael@0 126 */
michael@0 127
michael@0 128 NS_IMETHODIMP nsPrefBranch::GetRoot(char **aRoot)
michael@0 129 {
michael@0 130 NS_ENSURE_ARG_POINTER(aRoot);
michael@0 131 mPrefRoot.Truncate(mPrefRootLength);
michael@0 132 *aRoot = ToNewCString(mPrefRoot);
michael@0 133 return NS_OK;
michael@0 134 }
michael@0 135
michael@0 136 NS_IMETHODIMP nsPrefBranch::GetPrefType(const char *aPrefName, int32_t *_retval)
michael@0 137 {
michael@0 138 NS_ENSURE_ARG(aPrefName);
michael@0 139 const char *pref = getPrefName(aPrefName);
michael@0 140 *_retval = PREF_GetPrefType(pref);
michael@0 141 return NS_OK;
michael@0 142 }
michael@0 143
michael@0 144 NS_IMETHODIMP nsPrefBranch::GetBoolPref(const char *aPrefName, bool *_retval)
michael@0 145 {
michael@0 146 NS_ENSURE_ARG(aPrefName);
michael@0 147 const char *pref = getPrefName(aPrefName);
michael@0 148 return PREF_GetBoolPref(pref, _retval, mIsDefault);
michael@0 149 }
michael@0 150
michael@0 151 NS_IMETHODIMP nsPrefBranch::SetBoolPref(const char *aPrefName, bool aValue)
michael@0 152 {
michael@0 153 ENSURE_MAIN_PROCESS("Cannot SetBoolPref from content process:", aPrefName);
michael@0 154 NS_ENSURE_ARG(aPrefName);
michael@0 155 const char *pref = getPrefName(aPrefName);
michael@0 156 return PREF_SetBoolPref(pref, aValue, mIsDefault);
michael@0 157 }
michael@0 158
michael@0 159 NS_IMETHODIMP nsPrefBranch::GetFloatPref(const char *aPrefName, float *_retval)
michael@0 160 {
michael@0 161 NS_ENSURE_ARG(aPrefName);
michael@0 162 const char *pref = getPrefName(aPrefName);
michael@0 163 nsAutoCString stringVal;
michael@0 164 nsresult rv = GetCharPref(pref, getter_Copies(stringVal));
michael@0 165 if (NS_SUCCEEDED(rv)) {
michael@0 166 *_retval = stringVal.ToFloat(&rv);
michael@0 167 }
michael@0 168
michael@0 169 return rv;
michael@0 170 }
michael@0 171
michael@0 172 NS_IMETHODIMP nsPrefBranch::GetCharPref(const char *aPrefName, char **_retval)
michael@0 173 {
michael@0 174 NS_ENSURE_ARG(aPrefName);
michael@0 175 const char *pref = getPrefName(aPrefName);
michael@0 176 return PREF_CopyCharPref(pref, _retval, mIsDefault);
michael@0 177 }
michael@0 178
michael@0 179 NS_IMETHODIMP nsPrefBranch::SetCharPref(const char *aPrefName, const char *aValue)
michael@0 180 {
michael@0 181 nsresult rv = CheckSanityOfStringLength(aPrefName, aValue);
michael@0 182 if (NS_FAILED(rv)) {
michael@0 183 return rv;
michael@0 184 }
michael@0 185 return SetCharPrefInternal(aPrefName, aValue);
michael@0 186 }
michael@0 187
michael@0 188 nsresult nsPrefBranch::SetCharPrefInternal(const char *aPrefName, const char *aValue)
michael@0 189
michael@0 190 {
michael@0 191 ENSURE_MAIN_PROCESS("Cannot SetCharPref from content process:", aPrefName);
michael@0 192 NS_ENSURE_ARG(aPrefName);
michael@0 193 NS_ENSURE_ARG(aValue);
michael@0 194 const char *pref = getPrefName(aPrefName);
michael@0 195 return PREF_SetCharPref(pref, aValue, mIsDefault);
michael@0 196 }
michael@0 197
michael@0 198 NS_IMETHODIMP nsPrefBranch::GetIntPref(const char *aPrefName, int32_t *_retval)
michael@0 199 {
michael@0 200 NS_ENSURE_ARG(aPrefName);
michael@0 201 const char *pref = getPrefName(aPrefName);
michael@0 202 return PREF_GetIntPref(pref, _retval, mIsDefault);
michael@0 203 }
michael@0 204
michael@0 205 NS_IMETHODIMP nsPrefBranch::SetIntPref(const char *aPrefName, int32_t aValue)
michael@0 206 {
michael@0 207 ENSURE_MAIN_PROCESS("Cannot SetIntPref from content process:", aPrefName);
michael@0 208 NS_ENSURE_ARG(aPrefName);
michael@0 209 const char *pref = getPrefName(aPrefName);
michael@0 210 return PREF_SetIntPref(pref, aValue, mIsDefault);
michael@0 211 }
michael@0 212
michael@0 213 NS_IMETHODIMP nsPrefBranch::GetComplexValue(const char *aPrefName, const nsIID & aType, void **_retval)
michael@0 214 {
michael@0 215 NS_ENSURE_ARG(aPrefName);
michael@0 216
michael@0 217 nsresult rv;
michael@0 218 nsXPIDLCString utf8String;
michael@0 219
michael@0 220 // we have to do this one first because it's different than all the rest
michael@0 221 if (aType.Equals(NS_GET_IID(nsIPrefLocalizedString))) {
michael@0 222 nsCOMPtr<nsIPrefLocalizedString> theString(do_CreateInstance(NS_PREFLOCALIZEDSTRING_CONTRACTID, &rv));
michael@0 223 if (NS_FAILED(rv)) return rv;
michael@0 224
michael@0 225 const char *pref = getPrefName(aPrefName);
michael@0 226 bool bNeedDefault = false;
michael@0 227
michael@0 228 if (mIsDefault) {
michael@0 229 bNeedDefault = true;
michael@0 230 } else {
michael@0 231 // if there is no user (or locked) value
michael@0 232 if (!PREF_HasUserPref(pref) && !PREF_PrefIsLocked(pref)) {
michael@0 233 bNeedDefault = true;
michael@0 234 }
michael@0 235 }
michael@0 236
michael@0 237 // if we need to fetch the default value, do that instead, otherwise use the
michael@0 238 // value we pulled in at the top of this function
michael@0 239 if (bNeedDefault) {
michael@0 240 nsXPIDLString utf16String;
michael@0 241 rv = GetDefaultFromPropertiesFile(pref, getter_Copies(utf16String));
michael@0 242 if (NS_SUCCEEDED(rv)) {
michael@0 243 theString->SetData(utf16String.get());
michael@0 244 }
michael@0 245 } else {
michael@0 246 rv = GetCharPref(aPrefName, getter_Copies(utf8String));
michael@0 247 if (NS_SUCCEEDED(rv)) {
michael@0 248 theString->SetData(NS_ConvertUTF8toUTF16(utf8String).get());
michael@0 249 }
michael@0 250 }
michael@0 251
michael@0 252 if (NS_SUCCEEDED(rv)) {
michael@0 253 theString.forget(reinterpret_cast<nsIPrefLocalizedString**>(_retval));
michael@0 254 }
michael@0 255
michael@0 256 return rv;
michael@0 257 }
michael@0 258
michael@0 259 // if we can't get the pref, there's no point in being here
michael@0 260 rv = GetCharPref(aPrefName, getter_Copies(utf8String));
michael@0 261 if (NS_FAILED(rv)) {
michael@0 262 return rv;
michael@0 263 }
michael@0 264
michael@0 265 // also check nsILocalFile, for backwards compatibility
michael@0 266 if (aType.Equals(NS_GET_IID(nsIFile)) || aType.Equals(NS_GET_IID(nsILocalFile))) {
michael@0 267 if (GetContentChild()) {
michael@0 268 NS_ERROR("cannot get nsIFile pref from content process");
michael@0 269 return NS_ERROR_NOT_AVAILABLE;
michael@0 270 }
michael@0 271
michael@0 272 nsCOMPtr<nsIFile> file(do_CreateInstance(NS_LOCAL_FILE_CONTRACTID, &rv));
michael@0 273
michael@0 274 if (NS_SUCCEEDED(rv)) {
michael@0 275 rv = file->SetPersistentDescriptor(utf8String);
michael@0 276 if (NS_SUCCEEDED(rv)) {
michael@0 277 file.forget(reinterpret_cast<nsIFile**>(_retval));
michael@0 278 return NS_OK;
michael@0 279 }
michael@0 280 }
michael@0 281 return rv;
michael@0 282 }
michael@0 283
michael@0 284 if (aType.Equals(NS_GET_IID(nsIRelativeFilePref))) {
michael@0 285 if (GetContentChild()) {
michael@0 286 NS_ERROR("cannot get nsIRelativeFilePref from content process");
michael@0 287 return NS_ERROR_NOT_AVAILABLE;
michael@0 288 }
michael@0 289
michael@0 290 nsACString::const_iterator keyBegin, strEnd;
michael@0 291 utf8String.BeginReading(keyBegin);
michael@0 292 utf8String.EndReading(strEnd);
michael@0 293
michael@0 294 // The pref has the format: [fromKey]a/b/c
michael@0 295 if (*keyBegin++ != '[')
michael@0 296 return NS_ERROR_FAILURE;
michael@0 297 nsACString::const_iterator keyEnd(keyBegin);
michael@0 298 if (!FindCharInReadable(']', keyEnd, strEnd))
michael@0 299 return NS_ERROR_FAILURE;
michael@0 300 nsAutoCString key(Substring(keyBegin, keyEnd));
michael@0 301
michael@0 302 nsCOMPtr<nsIFile> fromFile;
michael@0 303 nsCOMPtr<nsIProperties> directoryService(do_GetService(NS_DIRECTORY_SERVICE_CONTRACTID, &rv));
michael@0 304 if (NS_FAILED(rv))
michael@0 305 return rv;
michael@0 306 rv = directoryService->Get(key.get(), NS_GET_IID(nsIFile), getter_AddRefs(fromFile));
michael@0 307 if (NS_FAILED(rv))
michael@0 308 return rv;
michael@0 309
michael@0 310 nsCOMPtr<nsIFile> theFile;
michael@0 311 rv = NS_NewNativeLocalFile(EmptyCString(), true, getter_AddRefs(theFile));
michael@0 312 if (NS_FAILED(rv))
michael@0 313 return rv;
michael@0 314 rv = theFile->SetRelativeDescriptor(fromFile, Substring(++keyEnd, strEnd));
michael@0 315 if (NS_FAILED(rv))
michael@0 316 return rv;
michael@0 317 nsCOMPtr<nsIRelativeFilePref> relativePref;
michael@0 318 rv = NS_NewRelativeFilePref(theFile, key, getter_AddRefs(relativePref));
michael@0 319 if (NS_FAILED(rv))
michael@0 320 return rv;
michael@0 321
michael@0 322 relativePref.forget(reinterpret_cast<nsIRelativeFilePref**>(_retval));
michael@0 323 return NS_OK;
michael@0 324 }
michael@0 325
michael@0 326 if (aType.Equals(NS_GET_IID(nsISupportsString))) {
michael@0 327 nsCOMPtr<nsISupportsString> theString(do_CreateInstance(NS_SUPPORTS_STRING_CONTRACTID, &rv));
michael@0 328
michael@0 329 if (NS_SUCCEEDED(rv)) {
michael@0 330 // Debugging to see why we end up with very long strings here with
michael@0 331 // some addons, see bug 836263.
michael@0 332 nsAutoString wdata;
michael@0 333 if (!AppendUTF8toUTF16(utf8String, wdata, mozilla::fallible_t())) {
michael@0 334 #ifdef MOZ_CRASHREPORTER
michael@0 335 nsCOMPtr<nsICrashReporter> cr =
michael@0 336 do_GetService("@mozilla.org/toolkit/crash-reporter;1");
michael@0 337 if (cr) {
michael@0 338 cr->AnnotateCrashReport(NS_LITERAL_CSTRING("bug836263-size"),
michael@0 339 nsPrintfCString("%x", utf8String.Length()));
michael@0 340 cr->RegisterAppMemory(uint64_t(utf8String.BeginReading()),
michael@0 341 std::min(0x1000U, utf8String.Length()));
michael@0 342 }
michael@0 343 #endif
michael@0 344 NS_RUNTIMEABORT("bug836263");
michael@0 345 }
michael@0 346 theString->SetData(wdata);
michael@0 347 theString.forget(reinterpret_cast<nsISupportsString**>(_retval));
michael@0 348 }
michael@0 349 return rv;
michael@0 350 }
michael@0 351
michael@0 352 NS_WARNING("nsPrefBranch::GetComplexValue - Unsupported interface type");
michael@0 353 return NS_NOINTERFACE;
michael@0 354 }
michael@0 355
michael@0 356 nsresult nsPrefBranch::CheckSanityOfStringLength(const char* aPrefName, const char* aValue) {
michael@0 357 if (!aValue) {
michael@0 358 return NS_OK;
michael@0 359 }
michael@0 360 return CheckSanityOfStringLength(aPrefName, strlen(aValue));
michael@0 361 }
michael@0 362
michael@0 363 nsresult nsPrefBranch::CheckSanityOfStringLength(const char* aPrefName, const nsAString& aValue) {
michael@0 364 return CheckSanityOfStringLength(aPrefName, aValue.Length());
michael@0 365 }
michael@0 366
michael@0 367 nsresult nsPrefBranch::CheckSanityOfStringLength(const char* aPrefName, const uint32_t aLength) {
michael@0 368 if (aLength > MAX_PREF_LENGTH) {
michael@0 369 return NS_ERROR_ILLEGAL_VALUE;
michael@0 370 }
michael@0 371 if (aLength <= MAX_ADVISABLE_PREF_LENGTH) {
michael@0 372 return NS_OK;
michael@0 373 }
michael@0 374 nsresult rv;
michael@0 375 nsCOMPtr<nsIConsoleService> console = do_GetService("@mozilla.org/consoleservice;1", &rv);
michael@0 376 if (NS_FAILED(rv)) {
michael@0 377 return rv;
michael@0 378 }
michael@0 379 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 380 aLength,
michael@0 381 aPrefName));
michael@0 382 rv = console->LogStringMessage(NS_ConvertUTF8toUTF16(message).get());
michael@0 383 if (NS_FAILED(rv)) {
michael@0 384 return rv;
michael@0 385 }
michael@0 386 return NS_OK;
michael@0 387 }
michael@0 388
michael@0 389
michael@0 390 NS_IMETHODIMP nsPrefBranch::SetComplexValue(const char *aPrefName, const nsIID & aType, nsISupports *aValue)
michael@0 391 {
michael@0 392 ENSURE_MAIN_PROCESS("Cannot SetComplexValue from content process:", aPrefName);
michael@0 393 NS_ENSURE_ARG(aPrefName);
michael@0 394
michael@0 395 nsresult rv = NS_NOINTERFACE;
michael@0 396
michael@0 397 // also check nsILocalFile, for backwards compatibility
michael@0 398 if (aType.Equals(NS_GET_IID(nsIFile)) || aType.Equals(NS_GET_IID(nsILocalFile))) {
michael@0 399 nsCOMPtr<nsIFile> file = do_QueryInterface(aValue);
michael@0 400 if (!file)
michael@0 401 return NS_NOINTERFACE;
michael@0 402 nsAutoCString descriptorString;
michael@0 403
michael@0 404 rv = file->GetPersistentDescriptor(descriptorString);
michael@0 405 if (NS_SUCCEEDED(rv)) {
michael@0 406 rv = SetCharPrefInternal(aPrefName, descriptorString.get());
michael@0 407 }
michael@0 408 return rv;
michael@0 409 }
michael@0 410
michael@0 411 if (aType.Equals(NS_GET_IID(nsIRelativeFilePref))) {
michael@0 412 nsCOMPtr<nsIRelativeFilePref> relFilePref = do_QueryInterface(aValue);
michael@0 413 if (!relFilePref)
michael@0 414 return NS_NOINTERFACE;
michael@0 415
michael@0 416 nsCOMPtr<nsIFile> file;
michael@0 417 relFilePref->GetFile(getter_AddRefs(file));
michael@0 418 if (!file)
michael@0 419 return NS_NOINTERFACE;
michael@0 420 nsAutoCString relativeToKey;
michael@0 421 (void) relFilePref->GetRelativeToKey(relativeToKey);
michael@0 422
michael@0 423 nsCOMPtr<nsIFile> relativeToFile;
michael@0 424 nsCOMPtr<nsIProperties> directoryService(do_GetService(NS_DIRECTORY_SERVICE_CONTRACTID, &rv));
michael@0 425 if (NS_FAILED(rv))
michael@0 426 return rv;
michael@0 427 rv = directoryService->Get(relativeToKey.get(), NS_GET_IID(nsIFile), getter_AddRefs(relativeToFile));
michael@0 428 if (NS_FAILED(rv))
michael@0 429 return rv;
michael@0 430
michael@0 431 nsAutoCString relDescriptor;
michael@0 432 rv = file->GetRelativeDescriptor(relativeToFile, relDescriptor);
michael@0 433 if (NS_FAILED(rv))
michael@0 434 return rv;
michael@0 435
michael@0 436 nsAutoCString descriptorString;
michael@0 437 descriptorString.Append('[');
michael@0 438 descriptorString.Append(relativeToKey);
michael@0 439 descriptorString.Append(']');
michael@0 440 descriptorString.Append(relDescriptor);
michael@0 441 return SetCharPrefInternal(aPrefName, descriptorString.get());
michael@0 442 }
michael@0 443
michael@0 444 if (aType.Equals(NS_GET_IID(nsISupportsString))) {
michael@0 445 nsCOMPtr<nsISupportsString> theString = do_QueryInterface(aValue);
michael@0 446
michael@0 447 if (theString) {
michael@0 448 nsString wideString;
michael@0 449
michael@0 450 rv = theString->GetData(wideString);
michael@0 451 if (NS_SUCCEEDED(rv)) {
michael@0 452 // Check sanity of string length before any lengthy conversion
michael@0 453 rv = CheckSanityOfStringLength(aPrefName, wideString);
michael@0 454 if (NS_FAILED(rv)) {
michael@0 455 return rv;
michael@0 456 }
michael@0 457 rv = SetCharPrefInternal(aPrefName, NS_ConvertUTF16toUTF8(wideString).get());
michael@0 458 }
michael@0 459 }
michael@0 460 return rv;
michael@0 461 }
michael@0 462
michael@0 463 if (aType.Equals(NS_GET_IID(nsIPrefLocalizedString))) {
michael@0 464 nsCOMPtr<nsIPrefLocalizedString> theString = do_QueryInterface(aValue);
michael@0 465
michael@0 466 if (theString) {
michael@0 467 nsXPIDLString wideString;
michael@0 468
michael@0 469 rv = theString->GetData(getter_Copies(wideString));
michael@0 470 if (NS_SUCCEEDED(rv)) {
michael@0 471 // Check sanity of string length before any lengthy conversion
michael@0 472 rv = CheckSanityOfStringLength(aPrefName, wideString);
michael@0 473 if (NS_FAILED(rv)) {
michael@0 474 return rv;
michael@0 475 }
michael@0 476 rv = SetCharPrefInternal(aPrefName, NS_ConvertUTF16toUTF8(wideString).get());
michael@0 477 }
michael@0 478 }
michael@0 479 return rv;
michael@0 480 }
michael@0 481
michael@0 482 NS_WARNING("nsPrefBranch::SetComplexValue - Unsupported interface type");
michael@0 483 return NS_NOINTERFACE;
michael@0 484 }
michael@0 485
michael@0 486 NS_IMETHODIMP nsPrefBranch::ClearUserPref(const char *aPrefName)
michael@0 487 {
michael@0 488 ENSURE_MAIN_PROCESS("Cannot ClearUserPref from content process:", aPrefName);
michael@0 489 NS_ENSURE_ARG(aPrefName);
michael@0 490 const char *pref = getPrefName(aPrefName);
michael@0 491 return PREF_ClearUserPref(pref);
michael@0 492 }
michael@0 493
michael@0 494 NS_IMETHODIMP nsPrefBranch::PrefHasUserValue(const char *aPrefName, bool *_retval)
michael@0 495 {
michael@0 496 NS_ENSURE_ARG_POINTER(_retval);
michael@0 497 NS_ENSURE_ARG(aPrefName);
michael@0 498 const char *pref = getPrefName(aPrefName);
michael@0 499 *_retval = PREF_HasUserPref(pref);
michael@0 500 return NS_OK;
michael@0 501 }
michael@0 502
michael@0 503 NS_IMETHODIMP nsPrefBranch::LockPref(const char *aPrefName)
michael@0 504 {
michael@0 505 ENSURE_MAIN_PROCESS("Cannot LockPref from content process:", aPrefName);
michael@0 506 NS_ENSURE_ARG(aPrefName);
michael@0 507 const char *pref = getPrefName(aPrefName);
michael@0 508 return PREF_LockPref(pref, true);
michael@0 509 }
michael@0 510
michael@0 511 NS_IMETHODIMP nsPrefBranch::PrefIsLocked(const char *aPrefName, bool *_retval)
michael@0 512 {
michael@0 513 ENSURE_MAIN_PROCESS("Cannot check PrefIsLocked from content process:", aPrefName);
michael@0 514 NS_ENSURE_ARG_POINTER(_retval);
michael@0 515 NS_ENSURE_ARG(aPrefName);
michael@0 516 const char *pref = getPrefName(aPrefName);
michael@0 517 *_retval = PREF_PrefIsLocked(pref);
michael@0 518 return NS_OK;
michael@0 519 }
michael@0 520
michael@0 521 NS_IMETHODIMP nsPrefBranch::UnlockPref(const char *aPrefName)
michael@0 522 {
michael@0 523 ENSURE_MAIN_PROCESS("Cannot UnlockPref from content process:", aPrefName);
michael@0 524 NS_ENSURE_ARG(aPrefName);
michael@0 525 const char *pref = getPrefName(aPrefName);
michael@0 526 return PREF_LockPref(pref, false);
michael@0 527 }
michael@0 528
michael@0 529 /* void resetBranch (in string startingAt); */
michael@0 530 NS_IMETHODIMP nsPrefBranch::ResetBranch(const char *aStartingAt)
michael@0 531 {
michael@0 532 return NS_ERROR_NOT_IMPLEMENTED;
michael@0 533 }
michael@0 534
michael@0 535 NS_IMETHODIMP nsPrefBranch::DeleteBranch(const char *aStartingAt)
michael@0 536 {
michael@0 537 ENSURE_MAIN_PROCESS("Cannot DeleteBranch from content process:", aStartingAt);
michael@0 538 NS_ENSURE_ARG(aStartingAt);
michael@0 539 const char *pref = getPrefName(aStartingAt);
michael@0 540 return PREF_DeleteBranch(pref);
michael@0 541 }
michael@0 542
michael@0 543 NS_IMETHODIMP nsPrefBranch::GetChildList(const char *aStartingAt, uint32_t *aCount, char ***aChildArray)
michael@0 544 {
michael@0 545 char **outArray;
michael@0 546 int32_t numPrefs;
michael@0 547 int32_t dwIndex;
michael@0 548 EnumerateData ed;
michael@0 549 nsAutoTArray<nsCString, 32> prefArray;
michael@0 550
michael@0 551 NS_ENSURE_ARG(aStartingAt);
michael@0 552 NS_ENSURE_ARG_POINTER(aCount);
michael@0 553 NS_ENSURE_ARG_POINTER(aChildArray);
michael@0 554
michael@0 555 *aChildArray = nullptr;
michael@0 556 *aCount = 0;
michael@0 557
michael@0 558 if (!gHashTable.ops)
michael@0 559 return NS_ERROR_NOT_INITIALIZED;
michael@0 560
michael@0 561 // this will contain a list of all the pref name strings
michael@0 562 // allocate on the stack for speed
michael@0 563
michael@0 564 ed.parent = getPrefName(aStartingAt);
michael@0 565 ed.pref_list = &prefArray;
michael@0 566 PL_DHashTableEnumerate(&gHashTable, pref_enumChild, &ed);
michael@0 567
michael@0 568 // now that we've built up the list, run the callback on
michael@0 569 // all the matching elements
michael@0 570 numPrefs = prefArray.Length();
michael@0 571
michael@0 572 if (numPrefs) {
michael@0 573 outArray = (char **)nsMemory::Alloc(numPrefs * sizeof(char *));
michael@0 574 if (!outArray)
michael@0 575 return NS_ERROR_OUT_OF_MEMORY;
michael@0 576
michael@0 577 for (dwIndex = 0; dwIndex < numPrefs; ++dwIndex) {
michael@0 578 // we need to lop off mPrefRoot in case the user is planning to pass this
michael@0 579 // back to us because if they do we are going to add mPrefRoot again.
michael@0 580 const nsCString& element = prefArray[dwIndex];
michael@0 581 outArray[dwIndex] = (char *)nsMemory::Clone(
michael@0 582 element.get() + mPrefRootLength, element.Length() - mPrefRootLength + 1);
michael@0 583
michael@0 584 if (!outArray[dwIndex]) {
michael@0 585 // we ran out of memory... this is annoying
michael@0 586 NS_FREE_XPCOM_ALLOCATED_POINTER_ARRAY(dwIndex, outArray);
michael@0 587 return NS_ERROR_OUT_OF_MEMORY;
michael@0 588 }
michael@0 589 }
michael@0 590 *aChildArray = outArray;
michael@0 591 }
michael@0 592 *aCount = numPrefs;
michael@0 593
michael@0 594 return NS_OK;
michael@0 595 }
michael@0 596
michael@0 597 NS_IMETHODIMP nsPrefBranch::AddObserver(const char *aDomain, nsIObserver *aObserver, bool aHoldWeak)
michael@0 598 {
michael@0 599 PrefCallback *pCallback;
michael@0 600 const char *pref;
michael@0 601
michael@0 602 NS_ENSURE_ARG(aDomain);
michael@0 603 NS_ENSURE_ARG(aObserver);
michael@0 604
michael@0 605 // hold a weak reference to the observer if so requested
michael@0 606 if (aHoldWeak) {
michael@0 607 nsCOMPtr<nsISupportsWeakReference> weakRefFactory = do_QueryInterface(aObserver);
michael@0 608 if (!weakRefFactory) {
michael@0 609 // the caller didn't give us a object that supports weak reference... tell them
michael@0 610 return NS_ERROR_INVALID_ARG;
michael@0 611 }
michael@0 612
michael@0 613 // Construct a PrefCallback with a weak reference to the observer.
michael@0 614 pCallback = new PrefCallback(aDomain, weakRefFactory, this);
michael@0 615
michael@0 616 } else {
michael@0 617 // Construct a PrefCallback with a strong reference to the observer.
michael@0 618 pCallback = new PrefCallback(aDomain, aObserver, this);
michael@0 619 }
michael@0 620
michael@0 621 if (mObservers.Get(pCallback)) {
michael@0 622 NS_WARNING("Ignoring duplicate observer.");
michael@0 623 delete pCallback;
michael@0 624 return NS_OK;
michael@0 625 }
michael@0 626
michael@0 627 mObservers.Put(pCallback, pCallback);
michael@0 628
michael@0 629 // We must pass a fully qualified preference name to the callback
michael@0 630 // aDomain == nullptr is the only possible failure, and we trapped it with
michael@0 631 // NS_ENSURE_ARG above.
michael@0 632 pref = getPrefName(aDomain);
michael@0 633 PREF_RegisterCallback(pref, NotifyObserver, pCallback);
michael@0 634 return NS_OK;
michael@0 635 }
michael@0 636
michael@0 637 NS_IMETHODIMP nsPrefBranch::RemoveObserver(const char *aDomain, nsIObserver *aObserver)
michael@0 638 {
michael@0 639 NS_ENSURE_ARG(aDomain);
michael@0 640 NS_ENSURE_ARG(aObserver);
michael@0 641
michael@0 642 nsresult rv = NS_OK;
michael@0 643
michael@0 644 // If we're in the middle of a call to freeObserverList, don't process this
michael@0 645 // RemoveObserver call -- the observer in question will be removed soon, if
michael@0 646 // it hasn't been already.
michael@0 647 //
michael@0 648 // It's important that we don't touch mObservers in any way -- even a Get()
michael@0 649 // which retuns null might cause the hashtable to resize itself, which will
michael@0 650 // break the Enumerator in freeObserverList.
michael@0 651 if (mFreeingObserverList)
michael@0 652 return NS_OK;
michael@0 653
michael@0 654 // Remove the relevant PrefCallback from mObservers and get an owning
michael@0 655 // pointer to it. Unregister the callback first, and then let the owning
michael@0 656 // pointer go out of scope and destroy the callback.
michael@0 657 PrefCallback key(aDomain, aObserver, this);
michael@0 658 nsAutoPtr<PrefCallback> pCallback;
michael@0 659 mObservers.RemoveAndForget(&key, pCallback);
michael@0 660 if (pCallback) {
michael@0 661 // aDomain == nullptr is the only possible failure, trapped above
michael@0 662 const char *pref = getPrefName(aDomain);
michael@0 663 rv = PREF_UnregisterCallback(pref, NotifyObserver, pCallback);
michael@0 664 }
michael@0 665
michael@0 666 return rv;
michael@0 667 }
michael@0 668
michael@0 669 NS_IMETHODIMP nsPrefBranch::Observe(nsISupports *aSubject, const char *aTopic, const char16_t *someData)
michael@0 670 {
michael@0 671 // watch for xpcom shutdown and free our observers to eliminate any cyclic references
michael@0 672 if (!nsCRT::strcmp(aTopic, NS_XPCOM_SHUTDOWN_OBSERVER_ID)) {
michael@0 673 freeObserverList();
michael@0 674 }
michael@0 675 return NS_OK;
michael@0 676 }
michael@0 677
michael@0 678 /* static */
michael@0 679 void nsPrefBranch::NotifyObserver(const char *newpref, void *data)
michael@0 680 {
michael@0 681 PrefCallback *pCallback = (PrefCallback *)data;
michael@0 682
michael@0 683 nsCOMPtr<nsIObserver> observer = pCallback->GetObserver();
michael@0 684 if (!observer) {
michael@0 685 // The observer has expired. Let's remove this callback.
michael@0 686 pCallback->GetPrefBranch()->RemoveExpiredCallback(pCallback);
michael@0 687 return;
michael@0 688 }
michael@0 689
michael@0 690 // remove any root this string may contain so as to not confuse the observer
michael@0 691 // by passing them something other than what they passed us as a topic
michael@0 692 uint32_t len = pCallback->GetPrefBranch()->GetRootLength();
michael@0 693 nsAutoCString suffix(newpref + len);
michael@0 694
michael@0 695 observer->Observe(static_cast<nsIPrefBranch *>(pCallback->GetPrefBranch()),
michael@0 696 NS_PREFBRANCH_PREFCHANGE_TOPIC_ID,
michael@0 697 NS_ConvertASCIItoUTF16(suffix).get());
michael@0 698 }
michael@0 699
michael@0 700 PLDHashOperator
michael@0 701 FreeObserverFunc(PrefCallback *aKey,
michael@0 702 nsAutoPtr<PrefCallback> &aCallback,
michael@0 703 void *aArgs)
michael@0 704 {
michael@0 705 // Calling NS_RELEASE below might trigger a call to
michael@0 706 // nsPrefBranch::RemoveObserver, since some classes remove themselves from
michael@0 707 // the pref branch on destruction. We don't need to worry about this causing
michael@0 708 // double-frees, however, because freeObserverList sets mFreeingObserverList
michael@0 709 // to true, which prevents RemoveObserver calls from doing anything.
michael@0 710
michael@0 711 nsPrefBranch *prefBranch = aCallback->GetPrefBranch();
michael@0 712 const char *pref = prefBranch->getPrefName(aCallback->GetDomain().get());
michael@0 713 PREF_UnregisterCallback(pref, nsPrefBranch::NotifyObserver, aCallback);
michael@0 714
michael@0 715 return PL_DHASH_REMOVE;
michael@0 716 }
michael@0 717
michael@0 718 void nsPrefBranch::freeObserverList(void)
michael@0 719 {
michael@0 720 // We need to prevent anyone from modifying mObservers while we're
michael@0 721 // enumerating over it. In particular, some clients will call
michael@0 722 // RemoveObserver() when they're destructed; we need to keep those calls from
michael@0 723 // touching mObservers.
michael@0 724 mFreeingObserverList = true;
michael@0 725 mObservers.Enumerate(&FreeObserverFunc, nullptr);
michael@0 726 mFreeingObserverList = false;
michael@0 727 }
michael@0 728
michael@0 729 void
michael@0 730 nsPrefBranch::RemoveExpiredCallback(PrefCallback *aCallback)
michael@0 731 {
michael@0 732 NS_PRECONDITION(aCallback->IsExpired(), "Callback should be expired.");
michael@0 733 mObservers.Remove(aCallback);
michael@0 734 }
michael@0 735
michael@0 736 nsresult nsPrefBranch::GetDefaultFromPropertiesFile(const char *aPrefName, char16_t **return_buf)
michael@0 737 {
michael@0 738 nsresult rv;
michael@0 739
michael@0 740 // the default value contains a URL to a .properties file
michael@0 741
michael@0 742 nsXPIDLCString propertyFileURL;
michael@0 743 rv = PREF_CopyCharPref(aPrefName, getter_Copies(propertyFileURL), true);
michael@0 744 if (NS_FAILED(rv))
michael@0 745 return rv;
michael@0 746
michael@0 747 nsCOMPtr<nsIStringBundleService> bundleService =
michael@0 748 mozilla::services::GetStringBundleService();
michael@0 749 if (!bundleService)
michael@0 750 return NS_ERROR_FAILURE;
michael@0 751
michael@0 752 nsCOMPtr<nsIStringBundle> bundle;
michael@0 753 rv = bundleService->CreateBundle(propertyFileURL,
michael@0 754 getter_AddRefs(bundle));
michael@0 755 if (NS_FAILED(rv))
michael@0 756 return rv;
michael@0 757
michael@0 758 // string names are in unicode
michael@0 759 nsAutoString stringId;
michael@0 760 stringId.AssignASCII(aPrefName);
michael@0 761
michael@0 762 return bundle->GetStringFromName(stringId.get(), return_buf);
michael@0 763 }
michael@0 764
michael@0 765 const char *nsPrefBranch::getPrefName(const char *aPrefName)
michael@0 766 {
michael@0 767 NS_ASSERTION(aPrefName, "null pref name!");
michael@0 768
michael@0 769 // for speed, avoid strcpy if we can:
michael@0 770 if (mPrefRoot.IsEmpty())
michael@0 771 return aPrefName;
michael@0 772
michael@0 773 // isn't there a better way to do this? this is really kind of gross.
michael@0 774 mPrefRoot.Truncate(mPrefRootLength);
michael@0 775 mPrefRoot.Append(aPrefName);
michael@0 776 return mPrefRoot.get();
michael@0 777 }
michael@0 778
michael@0 779 static PLDHashOperator
michael@0 780 pref_enumChild(PLDHashTable *table, PLDHashEntryHdr *heh,
michael@0 781 uint32_t i, void *arg)
michael@0 782 {
michael@0 783 PrefHashEntry *he = static_cast<PrefHashEntry*>(heh);
michael@0 784 EnumerateData *d = reinterpret_cast<EnumerateData *>(arg);
michael@0 785 if (strncmp(he->key, d->parent, strlen(d->parent)) == 0) {
michael@0 786 d->pref_list->AppendElement(he->key);
michael@0 787 }
michael@0 788 return PL_DHASH_NEXT;
michael@0 789 }
michael@0 790
michael@0 791 //----------------------------------------------------------------------------
michael@0 792 // nsPrefLocalizedString
michael@0 793 //----------------------------------------------------------------------------
michael@0 794
michael@0 795 nsPrefLocalizedString::nsPrefLocalizedString()
michael@0 796 {
michael@0 797 }
michael@0 798
michael@0 799 nsPrefLocalizedString::~nsPrefLocalizedString()
michael@0 800 {
michael@0 801 }
michael@0 802
michael@0 803
michael@0 804 /*
michael@0 805 * nsISupports Implementation
michael@0 806 */
michael@0 807
michael@0 808 NS_IMPL_ADDREF(nsPrefLocalizedString)
michael@0 809 NS_IMPL_RELEASE(nsPrefLocalizedString)
michael@0 810
michael@0 811 NS_INTERFACE_MAP_BEGIN(nsPrefLocalizedString)
michael@0 812 NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIPrefLocalizedString)
michael@0 813 NS_INTERFACE_MAP_ENTRY(nsIPrefLocalizedString)
michael@0 814 NS_INTERFACE_MAP_ENTRY(nsISupportsString)
michael@0 815 NS_INTERFACE_MAP_END
michael@0 816
michael@0 817 nsresult nsPrefLocalizedString::Init()
michael@0 818 {
michael@0 819 nsresult rv;
michael@0 820 mUnicodeString = do_CreateInstance(NS_SUPPORTS_STRING_CONTRACTID, &rv);
michael@0 821
michael@0 822 return rv;
michael@0 823 }
michael@0 824
michael@0 825 NS_IMETHODIMP
michael@0 826 nsPrefLocalizedString::GetData(char16_t **_retval)
michael@0 827 {
michael@0 828 nsAutoString data;
michael@0 829
michael@0 830 nsresult rv = GetData(data);
michael@0 831 if (NS_FAILED(rv))
michael@0 832 return rv;
michael@0 833
michael@0 834 *_retval = ToNewUnicode(data);
michael@0 835 if (!*_retval)
michael@0 836 return NS_ERROR_OUT_OF_MEMORY;
michael@0 837
michael@0 838 return NS_OK;
michael@0 839 }
michael@0 840
michael@0 841 NS_IMETHODIMP
michael@0 842 nsPrefLocalizedString::SetData(const char16_t *aData)
michael@0 843 {
michael@0 844 if (!aData)
michael@0 845 return SetData(EmptyString());
michael@0 846 return SetData(nsDependentString(aData));
michael@0 847 }
michael@0 848
michael@0 849 NS_IMETHODIMP
michael@0 850 nsPrefLocalizedString::SetDataWithLength(uint32_t aLength,
michael@0 851 const char16_t *aData)
michael@0 852 {
michael@0 853 if (!aData)
michael@0 854 return SetData(EmptyString());
michael@0 855 return SetData(Substring(aData, aLength));
michael@0 856 }
michael@0 857
michael@0 858 //----------------------------------------------------------------------------
michael@0 859 // nsRelativeFilePref
michael@0 860 //----------------------------------------------------------------------------
michael@0 861
michael@0 862 NS_IMPL_ISUPPORTS(nsRelativeFilePref, nsIRelativeFilePref)
michael@0 863
michael@0 864 nsRelativeFilePref::nsRelativeFilePref()
michael@0 865 {
michael@0 866 }
michael@0 867
michael@0 868 nsRelativeFilePref::~nsRelativeFilePref()
michael@0 869 {
michael@0 870 }
michael@0 871
michael@0 872 NS_IMETHODIMP nsRelativeFilePref::GetFile(nsIFile **aFile)
michael@0 873 {
michael@0 874 NS_ENSURE_ARG_POINTER(aFile);
michael@0 875 *aFile = mFile;
michael@0 876 NS_IF_ADDREF(*aFile);
michael@0 877 return NS_OK;
michael@0 878 }
michael@0 879
michael@0 880 NS_IMETHODIMP nsRelativeFilePref::SetFile(nsIFile *aFile)
michael@0 881 {
michael@0 882 mFile = aFile;
michael@0 883 return NS_OK;
michael@0 884 }
michael@0 885
michael@0 886 NS_IMETHODIMP nsRelativeFilePref::GetRelativeToKey(nsACString& aRelativeToKey)
michael@0 887 {
michael@0 888 aRelativeToKey.Assign(mRelativeToKey);
michael@0 889 return NS_OK;
michael@0 890 }
michael@0 891
michael@0 892 NS_IMETHODIMP nsRelativeFilePref::SetRelativeToKey(const nsACString& aRelativeToKey)
michael@0 893 {
michael@0 894 mRelativeToKey.Assign(aRelativeToKey);
michael@0 895 return NS_OK;
michael@0 896 }
michael@0 897
michael@0 898 #undef ENSURE_MAIN_PROCESS

mercurial