Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
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 "xpcom-config.h" |
michael@0 | 7 | #include <new> // for placement new |
michael@0 | 8 | #include "nscore.h" |
michael@0 | 9 | #include "nsCRT.h" |
michael@0 | 10 | |
michael@0 | 11 | #include "nsCommandParams.h" |
michael@0 | 12 | #include "mozilla/HashFunctions.h" |
michael@0 | 13 | |
michael@0 | 14 | using namespace mozilla; |
michael@0 | 15 | |
michael@0 | 16 | const PLDHashTableOps nsCommandParams::sHashOps = |
michael@0 | 17 | { |
michael@0 | 18 | PL_DHashAllocTable, |
michael@0 | 19 | PL_DHashFreeTable, |
michael@0 | 20 | HashKey, |
michael@0 | 21 | HashMatchEntry, |
michael@0 | 22 | HashMoveEntry, |
michael@0 | 23 | HashClearEntry, |
michael@0 | 24 | PL_DHashFinalizeStub |
michael@0 | 25 | }; |
michael@0 | 26 | |
michael@0 | 27 | |
michael@0 | 28 | NS_IMPL_ISUPPORTS(nsCommandParams, nsICommandParams) |
michael@0 | 29 | |
michael@0 | 30 | nsCommandParams::nsCommandParams() |
michael@0 | 31 | : mCurEntry(0) |
michael@0 | 32 | , mNumEntries(eNumEntriesUnknown) |
michael@0 | 33 | { |
michael@0 | 34 | // init the hash table later |
michael@0 | 35 | } |
michael@0 | 36 | |
michael@0 | 37 | nsCommandParams::~nsCommandParams() |
michael@0 | 38 | { |
michael@0 | 39 | PL_DHashTableFinish(&mValuesHash); |
michael@0 | 40 | } |
michael@0 | 41 | |
michael@0 | 42 | nsresult |
michael@0 | 43 | nsCommandParams::Init() |
michael@0 | 44 | { |
michael@0 | 45 | PL_DHashTableInit(&mValuesHash, &sHashOps, (void *)this, sizeof(HashEntry), 4); |
michael@0 | 46 | |
michael@0 | 47 | return NS_OK; |
michael@0 | 48 | } |
michael@0 | 49 | |
michael@0 | 50 | #if 0 |
michael@0 | 51 | #pragma mark - |
michael@0 | 52 | #endif |
michael@0 | 53 | |
michael@0 | 54 | /* short getValueType (in string name); */ |
michael@0 | 55 | NS_IMETHODIMP nsCommandParams::GetValueType(const char * name, int16_t *_retval) |
michael@0 | 56 | { |
michael@0 | 57 | NS_ENSURE_ARG_POINTER(_retval); |
michael@0 | 58 | *_retval = eNoType; |
michael@0 | 59 | HashEntry* foundEntry = GetNamedEntry(name); |
michael@0 | 60 | if (foundEntry) |
michael@0 | 61 | { |
michael@0 | 62 | *_retval = foundEntry->mEntryType; |
michael@0 | 63 | return NS_OK; |
michael@0 | 64 | } |
michael@0 | 65 | |
michael@0 | 66 | return NS_ERROR_FAILURE; |
michael@0 | 67 | } |
michael@0 | 68 | |
michael@0 | 69 | /* boolean getBooleanValue (in AString name); */ |
michael@0 | 70 | NS_IMETHODIMP nsCommandParams::GetBooleanValue(const char * name, bool *_retval) |
michael@0 | 71 | { |
michael@0 | 72 | NS_ENSURE_ARG_POINTER(_retval); |
michael@0 | 73 | *_retval = false; |
michael@0 | 74 | |
michael@0 | 75 | HashEntry* foundEntry = GetNamedEntry(name); |
michael@0 | 76 | if (foundEntry && foundEntry->mEntryType == eBooleanType) |
michael@0 | 77 | { |
michael@0 | 78 | *_retval = foundEntry->mData.mBoolean; |
michael@0 | 79 | return NS_OK; |
michael@0 | 80 | } |
michael@0 | 81 | |
michael@0 | 82 | return NS_ERROR_FAILURE; |
michael@0 | 83 | } |
michael@0 | 84 | |
michael@0 | 85 | /* long getLongValue (in AString name); */ |
michael@0 | 86 | NS_IMETHODIMP nsCommandParams::GetLongValue(const char * name, int32_t *_retval) |
michael@0 | 87 | { |
michael@0 | 88 | NS_ENSURE_ARG_POINTER(_retval); |
michael@0 | 89 | *_retval = false; |
michael@0 | 90 | |
michael@0 | 91 | HashEntry* foundEntry = GetNamedEntry(name); |
michael@0 | 92 | if (foundEntry && foundEntry->mEntryType == eLongType) |
michael@0 | 93 | { |
michael@0 | 94 | *_retval = foundEntry->mData.mLong; |
michael@0 | 95 | return NS_OK; |
michael@0 | 96 | } |
michael@0 | 97 | |
michael@0 | 98 | return NS_ERROR_FAILURE; |
michael@0 | 99 | } |
michael@0 | 100 | |
michael@0 | 101 | /* double getDoubleValue (in AString name); */ |
michael@0 | 102 | NS_IMETHODIMP nsCommandParams::GetDoubleValue(const char * name, double *_retval) |
michael@0 | 103 | { |
michael@0 | 104 | NS_ENSURE_ARG_POINTER(_retval); |
michael@0 | 105 | *_retval = 0.0; |
michael@0 | 106 | |
michael@0 | 107 | HashEntry* foundEntry = GetNamedEntry(name); |
michael@0 | 108 | if (foundEntry && foundEntry->mEntryType == eDoubleType) |
michael@0 | 109 | { |
michael@0 | 110 | *_retval = foundEntry->mData.mDouble; |
michael@0 | 111 | return NS_OK; |
michael@0 | 112 | } |
michael@0 | 113 | |
michael@0 | 114 | return NS_ERROR_FAILURE; |
michael@0 | 115 | } |
michael@0 | 116 | |
michael@0 | 117 | /* AString getStringValue (in AString name); */ |
michael@0 | 118 | NS_IMETHODIMP nsCommandParams::GetStringValue(const char *name, nsAString & _retval) |
michael@0 | 119 | { |
michael@0 | 120 | _retval.Truncate(); |
michael@0 | 121 | HashEntry* foundEntry = GetNamedEntry(name); |
michael@0 | 122 | if (foundEntry && foundEntry->mEntryType == eWStringType) |
michael@0 | 123 | { |
michael@0 | 124 | NS_ASSERTION(foundEntry->mData.mString, "Null string"); |
michael@0 | 125 | _retval.Assign(*foundEntry->mData.mString); |
michael@0 | 126 | return NS_OK; |
michael@0 | 127 | } |
michael@0 | 128 | |
michael@0 | 129 | return NS_ERROR_FAILURE; |
michael@0 | 130 | } |
michael@0 | 131 | |
michael@0 | 132 | /* AString getStringValue (in AString name); */ |
michael@0 | 133 | NS_IMETHODIMP nsCommandParams::GetCStringValue(const char * name, char **_retval) |
michael@0 | 134 | { |
michael@0 | 135 | HashEntry* foundEntry = GetNamedEntry(name); |
michael@0 | 136 | if (foundEntry && foundEntry->mEntryType == eStringType) |
michael@0 | 137 | { |
michael@0 | 138 | NS_ASSERTION(foundEntry->mData.mCString, "Null string"); |
michael@0 | 139 | *_retval = ToNewCString(*foundEntry->mData.mCString); |
michael@0 | 140 | return NS_OK; |
michael@0 | 141 | } |
michael@0 | 142 | |
michael@0 | 143 | return NS_ERROR_FAILURE; |
michael@0 | 144 | } |
michael@0 | 145 | |
michael@0 | 146 | /* nsISupports getISupportsValue (in AString name); */ |
michael@0 | 147 | NS_IMETHODIMP nsCommandParams::GetISupportsValue(const char * name, nsISupports **_retval) |
michael@0 | 148 | { |
michael@0 | 149 | NS_ENSURE_ARG_POINTER(_retval); |
michael@0 | 150 | *_retval = nullptr; |
michael@0 | 151 | |
michael@0 | 152 | HashEntry* foundEntry = GetNamedEntry(name); |
michael@0 | 153 | if (foundEntry && foundEntry->mEntryType == eISupportsType) |
michael@0 | 154 | { |
michael@0 | 155 | NS_IF_ADDREF(*_retval = foundEntry->mISupports.get()); |
michael@0 | 156 | return NS_OK; |
michael@0 | 157 | } |
michael@0 | 158 | |
michael@0 | 159 | return NS_ERROR_FAILURE; |
michael@0 | 160 | } |
michael@0 | 161 | |
michael@0 | 162 | #if 0 |
michael@0 | 163 | #pragma mark - |
michael@0 | 164 | #endif |
michael@0 | 165 | |
michael@0 | 166 | /* void setBooleanValue (in AString name, in boolean value); */ |
michael@0 | 167 | NS_IMETHODIMP nsCommandParams::SetBooleanValue(const char * name, bool value) |
michael@0 | 168 | { |
michael@0 | 169 | HashEntry* foundEntry; |
michael@0 | 170 | GetOrMakeEntry(name, eBooleanType, foundEntry); |
michael@0 | 171 | if (!foundEntry) return NS_ERROR_OUT_OF_MEMORY; |
michael@0 | 172 | |
michael@0 | 173 | foundEntry->mData.mBoolean = value; |
michael@0 | 174 | |
michael@0 | 175 | return NS_OK; |
michael@0 | 176 | } |
michael@0 | 177 | |
michael@0 | 178 | /* void setLongValue (in AString name, in long value); */ |
michael@0 | 179 | NS_IMETHODIMP nsCommandParams::SetLongValue(const char * name, int32_t value) |
michael@0 | 180 | { |
michael@0 | 181 | HashEntry* foundEntry; |
michael@0 | 182 | GetOrMakeEntry(name, eLongType, foundEntry); |
michael@0 | 183 | if (!foundEntry) return NS_ERROR_OUT_OF_MEMORY; |
michael@0 | 184 | |
michael@0 | 185 | foundEntry->mData.mLong = value; |
michael@0 | 186 | return NS_OK; |
michael@0 | 187 | } |
michael@0 | 188 | |
michael@0 | 189 | /* void setDoubleValue (in AString name, in double value); */ |
michael@0 | 190 | NS_IMETHODIMP nsCommandParams::SetDoubleValue(const char * name, double value) |
michael@0 | 191 | { |
michael@0 | 192 | HashEntry* foundEntry; |
michael@0 | 193 | GetOrMakeEntry(name, eDoubleType, foundEntry); |
michael@0 | 194 | if (!foundEntry) return NS_ERROR_OUT_OF_MEMORY; |
michael@0 | 195 | |
michael@0 | 196 | foundEntry->mData.mDouble = value; |
michael@0 | 197 | return NS_OK; |
michael@0 | 198 | } |
michael@0 | 199 | |
michael@0 | 200 | /* void setStringValue (in AString name, in AString value); */ |
michael@0 | 201 | NS_IMETHODIMP nsCommandParams::SetStringValue(const char * name, const nsAString & value) |
michael@0 | 202 | { |
michael@0 | 203 | HashEntry* foundEntry; |
michael@0 | 204 | GetOrMakeEntry(name, eWStringType, foundEntry); |
michael@0 | 205 | if (!foundEntry) return NS_ERROR_OUT_OF_MEMORY; |
michael@0 | 206 | |
michael@0 | 207 | foundEntry->mData.mString = new nsString(value); |
michael@0 | 208 | return NS_OK; |
michael@0 | 209 | } |
michael@0 | 210 | |
michael@0 | 211 | /* void setCStringValue (in string name, in string value); */ |
michael@0 | 212 | NS_IMETHODIMP nsCommandParams::SetCStringValue(const char * name, const char * value) |
michael@0 | 213 | { |
michael@0 | 214 | HashEntry* foundEntry; |
michael@0 | 215 | GetOrMakeEntry(name, eStringType, foundEntry); |
michael@0 | 216 | if (!foundEntry) |
michael@0 | 217 | return NS_ERROR_OUT_OF_MEMORY; |
michael@0 | 218 | foundEntry->mData.mCString = new nsCString(value); |
michael@0 | 219 | return NS_OK; |
michael@0 | 220 | } |
michael@0 | 221 | |
michael@0 | 222 | /* void setISupportsValue (in AString name, in nsISupports value); */ |
michael@0 | 223 | NS_IMETHODIMP nsCommandParams::SetISupportsValue(const char * name, nsISupports *value) |
michael@0 | 224 | { |
michael@0 | 225 | HashEntry* foundEntry; |
michael@0 | 226 | GetOrMakeEntry(name, eISupportsType, foundEntry); |
michael@0 | 227 | if (!foundEntry) return NS_ERROR_OUT_OF_MEMORY; |
michael@0 | 228 | |
michael@0 | 229 | foundEntry->mISupports = value; // addrefs |
michael@0 | 230 | return NS_OK; |
michael@0 | 231 | } |
michael@0 | 232 | |
michael@0 | 233 | /* void removeValue (in AString name); */ |
michael@0 | 234 | NS_IMETHODIMP |
michael@0 | 235 | nsCommandParams::RemoveValue(const char * name) |
michael@0 | 236 | { |
michael@0 | 237 | // PL_DHASH_REMOVE doesn't tell us if the entry was really removed, so we return |
michael@0 | 238 | // NS_OK unconditionally. |
michael@0 | 239 | (void)PL_DHashTableOperate(&mValuesHash, (void *)name, PL_DHASH_REMOVE); |
michael@0 | 240 | |
michael@0 | 241 | // inval the number of entries |
michael@0 | 242 | mNumEntries = eNumEntriesUnknown; |
michael@0 | 243 | return NS_OK; |
michael@0 | 244 | } |
michael@0 | 245 | |
michael@0 | 246 | #if 0 |
michael@0 | 247 | #pragma mark - |
michael@0 | 248 | #endif |
michael@0 | 249 | |
michael@0 | 250 | nsCommandParams::HashEntry* |
michael@0 | 251 | nsCommandParams::GetNamedEntry(const char * name) |
michael@0 | 252 | { |
michael@0 | 253 | HashEntry *foundEntry = (HashEntry *)PL_DHashTableOperate(&mValuesHash, (void *)name, PL_DHASH_LOOKUP); |
michael@0 | 254 | |
michael@0 | 255 | if (PL_DHASH_ENTRY_IS_BUSY(foundEntry)) |
michael@0 | 256 | return foundEntry; |
michael@0 | 257 | |
michael@0 | 258 | return nullptr; |
michael@0 | 259 | } |
michael@0 | 260 | |
michael@0 | 261 | |
michael@0 | 262 | nsCommandParams::HashEntry* |
michael@0 | 263 | nsCommandParams::GetIndexedEntry(int32_t index) |
michael@0 | 264 | { |
michael@0 | 265 | HashEntry* entry = reinterpret_cast<HashEntry*>(mValuesHash.entryStore); |
michael@0 | 266 | HashEntry* limit = entry + PL_DHASH_TABLE_SIZE(&mValuesHash); |
michael@0 | 267 | uint32_t entryCount = 0; |
michael@0 | 268 | |
michael@0 | 269 | do |
michael@0 | 270 | { |
michael@0 | 271 | if (!PL_DHASH_ENTRY_IS_LIVE(entry)) |
michael@0 | 272 | continue; |
michael@0 | 273 | |
michael@0 | 274 | if ((int32_t)entryCount == index) |
michael@0 | 275 | return entry; |
michael@0 | 276 | |
michael@0 | 277 | entryCount ++; |
michael@0 | 278 | } while (++entry < limit); |
michael@0 | 279 | |
michael@0 | 280 | return nullptr; |
michael@0 | 281 | } |
michael@0 | 282 | |
michael@0 | 283 | |
michael@0 | 284 | uint32_t |
michael@0 | 285 | nsCommandParams::GetNumEntries() |
michael@0 | 286 | { |
michael@0 | 287 | HashEntry* entry = reinterpret_cast<HashEntry*>(mValuesHash.entryStore); |
michael@0 | 288 | HashEntry* limit = entry + PL_DHASH_TABLE_SIZE(&mValuesHash); |
michael@0 | 289 | uint32_t entryCount = 0; |
michael@0 | 290 | |
michael@0 | 291 | do |
michael@0 | 292 | { |
michael@0 | 293 | if (PL_DHASH_ENTRY_IS_LIVE(entry)) |
michael@0 | 294 | entryCount ++; |
michael@0 | 295 | } while (++entry < limit); |
michael@0 | 296 | |
michael@0 | 297 | return entryCount; |
michael@0 | 298 | } |
michael@0 | 299 | |
michael@0 | 300 | nsresult |
michael@0 | 301 | nsCommandParams::GetOrMakeEntry(const char * name, uint8_t entryType, HashEntry*& outEntry) |
michael@0 | 302 | { |
michael@0 | 303 | |
michael@0 | 304 | HashEntry *foundEntry = (HashEntry *)PL_DHashTableOperate(&mValuesHash, (void *)name, PL_DHASH_LOOKUP); |
michael@0 | 305 | if (PL_DHASH_ENTRY_IS_BUSY(foundEntry)) // reuse existing entry |
michael@0 | 306 | { |
michael@0 | 307 | foundEntry->Reset(entryType); |
michael@0 | 308 | foundEntry->mEntryName.Assign(name); |
michael@0 | 309 | outEntry = foundEntry; |
michael@0 | 310 | return NS_OK; |
michael@0 | 311 | } |
michael@0 | 312 | |
michael@0 | 313 | foundEntry = (HashEntry *)PL_DHashTableOperate(&mValuesHash, (void *)name, PL_DHASH_ADD); |
michael@0 | 314 | if (!foundEntry) return NS_ERROR_OUT_OF_MEMORY; |
michael@0 | 315 | |
michael@0 | 316 | // placement new that sucker. Our ctor does not clobber keyHash, which is important. |
michael@0 | 317 | outEntry = new (foundEntry) HashEntry(entryType, name); |
michael@0 | 318 | return NS_OK; |
michael@0 | 319 | } |
michael@0 | 320 | |
michael@0 | 321 | #if 0 |
michael@0 | 322 | #pragma mark - |
michael@0 | 323 | #endif |
michael@0 | 324 | |
michael@0 | 325 | PLDHashNumber |
michael@0 | 326 | nsCommandParams::HashKey(PLDHashTable *table, const void *key) |
michael@0 | 327 | { |
michael@0 | 328 | return HashString((const char *)key); |
michael@0 | 329 | } |
michael@0 | 330 | |
michael@0 | 331 | bool |
michael@0 | 332 | nsCommandParams::HashMatchEntry(PLDHashTable *table, |
michael@0 | 333 | const PLDHashEntryHdr *entry, const void *key) |
michael@0 | 334 | { |
michael@0 | 335 | const char* keyString = (const char*)key; |
michael@0 | 336 | const HashEntry* thisEntry = static_cast<const HashEntry*>(entry); |
michael@0 | 337 | |
michael@0 | 338 | return thisEntry->mEntryName.Equals(keyString); |
michael@0 | 339 | } |
michael@0 | 340 | |
michael@0 | 341 | void |
michael@0 | 342 | nsCommandParams::HashMoveEntry(PLDHashTable *table, const PLDHashEntryHdr *from, |
michael@0 | 343 | PLDHashEntryHdr *to) |
michael@0 | 344 | { |
michael@0 | 345 | const HashEntry* fromEntry = static_cast<const HashEntry*>(from); |
michael@0 | 346 | HashEntry* toEntry = static_cast<HashEntry*>(to); |
michael@0 | 347 | |
michael@0 | 348 | *toEntry = *fromEntry; |
michael@0 | 349 | // we leave from dirty, but that's OK |
michael@0 | 350 | } |
michael@0 | 351 | |
michael@0 | 352 | void |
michael@0 | 353 | nsCommandParams::HashClearEntry(PLDHashTable *table, PLDHashEntryHdr *entry) |
michael@0 | 354 | { |
michael@0 | 355 | HashEntry* thisEntry = static_cast<HashEntry*>(entry); |
michael@0 | 356 | thisEntry->~HashEntry(); // call dtor explicitly |
michael@0 | 357 | memset(thisEntry, 0, sizeof(HashEntry)); // and clear out |
michael@0 | 358 | } |
michael@0 | 359 | |
michael@0 | 360 | #if 0 |
michael@0 | 361 | #pragma mark - |
michael@0 | 362 | #endif |
michael@0 | 363 | |
michael@0 | 364 | /* boolean hasMoreElements (); */ |
michael@0 | 365 | NS_IMETHODIMP |
michael@0 | 366 | nsCommandParams::HasMoreElements(bool *_retval) |
michael@0 | 367 | { |
michael@0 | 368 | NS_ENSURE_ARG_POINTER(_retval); |
michael@0 | 369 | |
michael@0 | 370 | if (mNumEntries == eNumEntriesUnknown) |
michael@0 | 371 | mNumEntries = GetNumEntries(); |
michael@0 | 372 | |
michael@0 | 373 | *_retval = mCurEntry < mNumEntries; |
michael@0 | 374 | return NS_OK; |
michael@0 | 375 | } |
michael@0 | 376 | |
michael@0 | 377 | /* void first (); */ |
michael@0 | 378 | NS_IMETHODIMP |
michael@0 | 379 | nsCommandParams::First() |
michael@0 | 380 | { |
michael@0 | 381 | mCurEntry = 0; |
michael@0 | 382 | return NS_OK; |
michael@0 | 383 | } |
michael@0 | 384 | |
michael@0 | 385 | /* AString getNext (); */ |
michael@0 | 386 | NS_IMETHODIMP |
michael@0 | 387 | nsCommandParams::GetNext(char **_retval) |
michael@0 | 388 | { |
michael@0 | 389 | HashEntry* thisEntry = GetIndexedEntry(mCurEntry); |
michael@0 | 390 | if (!thisEntry) |
michael@0 | 391 | return NS_ERROR_FAILURE; |
michael@0 | 392 | |
michael@0 | 393 | *_retval = ToNewCString(thisEntry->mEntryName); |
michael@0 | 394 | mCurEntry++; |
michael@0 | 395 | return NS_OK; |
michael@0 | 396 | } |