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 "nsString.h" |
michael@0 | 7 | #include "nsReadableUtils.h" |
michael@0 | 8 | #include "nsTArray.h" |
michael@0 | 9 | #include "nsISimpleEnumerator.h" |
michael@0 | 10 | #include "nsXPCOM.h" |
michael@0 | 11 | #include "nsSupportsPrimitives.h" |
michael@0 | 12 | #include "nsIComponentManager.h" |
michael@0 | 13 | |
michael@0 | 14 | #include "nsCommandGroup.h" |
michael@0 | 15 | #include "nsIControllerCommand.h" |
michael@0 | 16 | #include "nsCRT.h" |
michael@0 | 17 | |
michael@0 | 18 | |
michael@0 | 19 | class nsGroupsEnumerator : public nsISimpleEnumerator |
michael@0 | 20 | { |
michael@0 | 21 | public: |
michael@0 | 22 | nsGroupsEnumerator(nsHashtable& inHashTable); |
michael@0 | 23 | virtual ~nsGroupsEnumerator(); |
michael@0 | 24 | |
michael@0 | 25 | NS_DECL_ISUPPORTS |
michael@0 | 26 | NS_DECL_NSISIMPLEENUMERATOR |
michael@0 | 27 | |
michael@0 | 28 | protected: |
michael@0 | 29 | |
michael@0 | 30 | static bool HashEnum(nsHashKey *aKey, void *aData, void* aClosure); |
michael@0 | 31 | |
michael@0 | 32 | nsresult Initialize(); |
michael@0 | 33 | |
michael@0 | 34 | protected: |
michael@0 | 35 | |
michael@0 | 36 | nsHashtable& mHashTable; |
michael@0 | 37 | int32_t mIndex; |
michael@0 | 38 | char ** mGroupNames; // array of pointers to char16_t* in the hash table |
michael@0 | 39 | bool mInitted; |
michael@0 | 40 | |
michael@0 | 41 | }; |
michael@0 | 42 | |
michael@0 | 43 | /* Implementation file */ |
michael@0 | 44 | NS_IMPL_ISUPPORTS(nsGroupsEnumerator, nsISimpleEnumerator) |
michael@0 | 45 | |
michael@0 | 46 | nsGroupsEnumerator::nsGroupsEnumerator(nsHashtable& inHashTable) |
michael@0 | 47 | : mHashTable(inHashTable) |
michael@0 | 48 | , mIndex(-1) |
michael@0 | 49 | , mGroupNames(nullptr) |
michael@0 | 50 | , mInitted(false) |
michael@0 | 51 | { |
michael@0 | 52 | /* member initializers and constructor code */ |
michael@0 | 53 | } |
michael@0 | 54 | |
michael@0 | 55 | nsGroupsEnumerator::~nsGroupsEnumerator() |
michael@0 | 56 | { |
michael@0 | 57 | delete [] mGroupNames; // ok on null pointer |
michael@0 | 58 | } |
michael@0 | 59 | |
michael@0 | 60 | /* boolean hasMoreElements (); */ |
michael@0 | 61 | NS_IMETHODIMP |
michael@0 | 62 | nsGroupsEnumerator::HasMoreElements(bool *_retval) |
michael@0 | 63 | { |
michael@0 | 64 | nsresult rv = NS_OK; |
michael@0 | 65 | |
michael@0 | 66 | NS_ENSURE_ARG_POINTER(_retval); |
michael@0 | 67 | |
michael@0 | 68 | if (!mInitted) { |
michael@0 | 69 | rv = Initialize(); |
michael@0 | 70 | if (NS_FAILED(rv)) return rv; |
michael@0 | 71 | } |
michael@0 | 72 | |
michael@0 | 73 | *_retval = (mIndex < mHashTable.Count() - 1); |
michael@0 | 74 | return NS_OK; |
michael@0 | 75 | } |
michael@0 | 76 | |
michael@0 | 77 | /* nsISupports getNext (); */ |
michael@0 | 78 | NS_IMETHODIMP |
michael@0 | 79 | nsGroupsEnumerator::GetNext(nsISupports **_retval) |
michael@0 | 80 | { |
michael@0 | 81 | nsresult rv = NS_OK; |
michael@0 | 82 | |
michael@0 | 83 | NS_ENSURE_ARG_POINTER(_retval); |
michael@0 | 84 | |
michael@0 | 85 | if (!mInitted) { |
michael@0 | 86 | rv = Initialize(); |
michael@0 | 87 | if (NS_FAILED(rv)) return rv; |
michael@0 | 88 | } |
michael@0 | 89 | |
michael@0 | 90 | mIndex ++; |
michael@0 | 91 | if (mIndex >= mHashTable.Count()) |
michael@0 | 92 | return NS_ERROR_FAILURE; |
michael@0 | 93 | |
michael@0 | 94 | char *thisGroupName = mGroupNames[mIndex]; |
michael@0 | 95 | |
michael@0 | 96 | nsCOMPtr<nsISupportsCString> supportsString = do_CreateInstance(NS_SUPPORTS_CSTRING_CONTRACTID, &rv); |
michael@0 | 97 | if (NS_FAILED(rv)) return rv; |
michael@0 | 98 | |
michael@0 | 99 | supportsString->SetData(nsDependentCString(thisGroupName)); |
michael@0 | 100 | return CallQueryInterface(supportsString, _retval); |
michael@0 | 101 | } |
michael@0 | 102 | |
michael@0 | 103 | /* static */ |
michael@0 | 104 | /* return false to stop */ |
michael@0 | 105 | bool |
michael@0 | 106 | nsGroupsEnumerator::HashEnum(nsHashKey *aKey, void *aData, void* aClosure) |
michael@0 | 107 | { |
michael@0 | 108 | nsGroupsEnumerator* groupsEnum = reinterpret_cast<nsGroupsEnumerator *>(aClosure); |
michael@0 | 109 | nsCStringKey* stringKey = static_cast<nsCStringKey*>(aKey); |
michael@0 | 110 | |
michael@0 | 111 | groupsEnum->mGroupNames[groupsEnum->mIndex] = (char*)stringKey->GetString(); |
michael@0 | 112 | groupsEnum->mIndex ++; |
michael@0 | 113 | return true; |
michael@0 | 114 | } |
michael@0 | 115 | |
michael@0 | 116 | nsresult |
michael@0 | 117 | nsGroupsEnumerator::Initialize() |
michael@0 | 118 | { |
michael@0 | 119 | if (mInitted) return NS_OK; |
michael@0 | 120 | |
michael@0 | 121 | mGroupNames = new char*[mHashTable.Count()]; |
michael@0 | 122 | if (!mGroupNames) return NS_ERROR_OUT_OF_MEMORY; |
michael@0 | 123 | |
michael@0 | 124 | mIndex = 0; |
michael@0 | 125 | mHashTable.Enumerate(HashEnum, (void*)this); |
michael@0 | 126 | |
michael@0 | 127 | mIndex = -1; |
michael@0 | 128 | mInitted = true; |
michael@0 | 129 | return NS_OK; |
michael@0 | 130 | } |
michael@0 | 131 | |
michael@0 | 132 | #if 0 |
michael@0 | 133 | #pragma mark - |
michael@0 | 134 | #endif |
michael@0 | 135 | |
michael@0 | 136 | class nsNamedGroupEnumerator : public nsISimpleEnumerator |
michael@0 | 137 | { |
michael@0 | 138 | public: |
michael@0 | 139 | nsNamedGroupEnumerator(nsTArray<char*>* inArray); |
michael@0 | 140 | virtual ~nsNamedGroupEnumerator(); |
michael@0 | 141 | |
michael@0 | 142 | NS_DECL_ISUPPORTS |
michael@0 | 143 | NS_DECL_NSISIMPLEENUMERATOR |
michael@0 | 144 | |
michael@0 | 145 | protected: |
michael@0 | 146 | |
michael@0 | 147 | nsTArray<char*>* mGroupArray; |
michael@0 | 148 | int32_t mIndex; |
michael@0 | 149 | |
michael@0 | 150 | }; |
michael@0 | 151 | |
michael@0 | 152 | nsNamedGroupEnumerator::nsNamedGroupEnumerator(nsTArray<char*>* inArray) |
michael@0 | 153 | : mGroupArray(inArray) |
michael@0 | 154 | , mIndex(-1) |
michael@0 | 155 | { |
michael@0 | 156 | } |
michael@0 | 157 | |
michael@0 | 158 | nsNamedGroupEnumerator::~nsNamedGroupEnumerator() |
michael@0 | 159 | { |
michael@0 | 160 | } |
michael@0 | 161 | |
michael@0 | 162 | NS_IMPL_ISUPPORTS(nsNamedGroupEnumerator, nsISimpleEnumerator) |
michael@0 | 163 | |
michael@0 | 164 | /* boolean hasMoreElements (); */ |
michael@0 | 165 | NS_IMETHODIMP |
michael@0 | 166 | nsNamedGroupEnumerator::HasMoreElements(bool *_retval) |
michael@0 | 167 | { |
michael@0 | 168 | NS_ENSURE_ARG_POINTER(_retval); |
michael@0 | 169 | |
michael@0 | 170 | int32_t arrayLen = mGroupArray ? mGroupArray->Length() : 0; |
michael@0 | 171 | *_retval = (mIndex < arrayLen - 1); |
michael@0 | 172 | return NS_OK; |
michael@0 | 173 | } |
michael@0 | 174 | |
michael@0 | 175 | /* nsISupports getNext (); */ |
michael@0 | 176 | NS_IMETHODIMP |
michael@0 | 177 | nsNamedGroupEnumerator::GetNext(nsISupports **_retval) |
michael@0 | 178 | { |
michael@0 | 179 | NS_ENSURE_ARG_POINTER(_retval); |
michael@0 | 180 | |
michael@0 | 181 | if (!mGroupArray) |
michael@0 | 182 | return NS_ERROR_FAILURE; |
michael@0 | 183 | |
michael@0 | 184 | mIndex ++; |
michael@0 | 185 | if (mIndex >= int32_t(mGroupArray->Length())) |
michael@0 | 186 | return NS_ERROR_FAILURE; |
michael@0 | 187 | |
michael@0 | 188 | char16_t *thisGroupName = (char16_t*)mGroupArray->ElementAt(mIndex); |
michael@0 | 189 | NS_ASSERTION(thisGroupName, "Bad Element in mGroupArray"); |
michael@0 | 190 | |
michael@0 | 191 | nsresult rv; |
michael@0 | 192 | nsCOMPtr<nsISupportsString> supportsString = do_CreateInstance(NS_SUPPORTS_STRING_CONTRACTID, &rv); |
michael@0 | 193 | if (NS_FAILED(rv)) return rv; |
michael@0 | 194 | |
michael@0 | 195 | supportsString->SetData(nsDependentString(thisGroupName)); |
michael@0 | 196 | return CallQueryInterface(supportsString, _retval); |
michael@0 | 197 | } |
michael@0 | 198 | |
michael@0 | 199 | #if 0 |
michael@0 | 200 | #pragma mark - |
michael@0 | 201 | #endif |
michael@0 | 202 | |
michael@0 | 203 | |
michael@0 | 204 | /* Implementation file */ |
michael@0 | 205 | NS_IMPL_ISUPPORTS(nsControllerCommandGroup, nsIControllerCommandGroup) |
michael@0 | 206 | |
michael@0 | 207 | nsControllerCommandGroup::nsControllerCommandGroup() |
michael@0 | 208 | { |
michael@0 | 209 | } |
michael@0 | 210 | |
michael@0 | 211 | nsControllerCommandGroup::~nsControllerCommandGroup() |
michael@0 | 212 | { |
michael@0 | 213 | ClearGroupsHash(); |
michael@0 | 214 | } |
michael@0 | 215 | |
michael@0 | 216 | void |
michael@0 | 217 | nsControllerCommandGroup::ClearGroupsHash() |
michael@0 | 218 | { |
michael@0 | 219 | mGroupsHash.Reset(ClearEnumerator, (void *)this); |
michael@0 | 220 | } |
michael@0 | 221 | |
michael@0 | 222 | #if 0 |
michael@0 | 223 | #pragma mark - |
michael@0 | 224 | #endif |
michael@0 | 225 | |
michael@0 | 226 | /* void addCommandToGroup (in DOMString aCommand, in DOMString aGroup); */ |
michael@0 | 227 | NS_IMETHODIMP |
michael@0 | 228 | nsControllerCommandGroup::AddCommandToGroup(const char * aCommand, const char *aGroup) |
michael@0 | 229 | { |
michael@0 | 230 | nsCStringKey groupKey(aGroup); |
michael@0 | 231 | nsTArray<char*>* commandList; |
michael@0 | 232 | if ((commandList = (nsTArray<char*> *)mGroupsHash.Get(&groupKey)) == nullptr) |
michael@0 | 233 | { |
michael@0 | 234 | // make this list |
michael@0 | 235 | commandList = new nsAutoTArray<char*, 8>; |
michael@0 | 236 | mGroupsHash.Put(&groupKey, (void *)commandList); |
michael@0 | 237 | } |
michael@0 | 238 | // add the command to the list. Note that we're not checking for duplicates here |
michael@0 | 239 | char* commandString = NS_strdup(aCommand); // we store allocated char16_t* in the array |
michael@0 | 240 | if (!commandString) return NS_ERROR_OUT_OF_MEMORY; |
michael@0 | 241 | |
michael@0 | 242 | #ifdef DEBUG |
michael@0 | 243 | char** appended = |
michael@0 | 244 | #endif |
michael@0 | 245 | commandList->AppendElement(commandString); |
michael@0 | 246 | NS_ASSERTION(appended, "Append failed"); |
michael@0 | 247 | |
michael@0 | 248 | return NS_OK; |
michael@0 | 249 | } |
michael@0 | 250 | |
michael@0 | 251 | /* void removeCommandFromGroup (in DOMString aCommand, in DOMString aGroup); */ |
michael@0 | 252 | NS_IMETHODIMP |
michael@0 | 253 | nsControllerCommandGroup::RemoveCommandFromGroup(const char * aCommand, const char * aGroup) |
michael@0 | 254 | { |
michael@0 | 255 | nsCStringKey groupKey(aGroup); |
michael@0 | 256 | nsTArray<char*>* commandList = (nsTArray<char*> *)mGroupsHash.Get(&groupKey); |
michael@0 | 257 | if (!commandList) return NS_OK; // no group |
michael@0 | 258 | |
michael@0 | 259 | uint32_t numEntries = commandList->Length(); |
michael@0 | 260 | for (uint32_t i = 0; i < numEntries; i ++) |
michael@0 | 261 | { |
michael@0 | 262 | char* commandString = commandList->ElementAt(i); |
michael@0 | 263 | if (!nsCRT::strcmp(aCommand,commandString)) |
michael@0 | 264 | { |
michael@0 | 265 | commandList->RemoveElementAt(i); |
michael@0 | 266 | nsMemory::Free(commandString); |
michael@0 | 267 | break; |
michael@0 | 268 | } |
michael@0 | 269 | } |
michael@0 | 270 | |
michael@0 | 271 | return NS_OK; |
michael@0 | 272 | } |
michael@0 | 273 | |
michael@0 | 274 | /* boolean isCommandInGroup (in DOMString aCommand, in DOMString aGroup); */ |
michael@0 | 275 | NS_IMETHODIMP |
michael@0 | 276 | nsControllerCommandGroup::IsCommandInGroup(const char * aCommand, const char * aGroup, bool *_retval) |
michael@0 | 277 | { |
michael@0 | 278 | NS_ENSURE_ARG_POINTER(_retval); |
michael@0 | 279 | *_retval = false; |
michael@0 | 280 | |
michael@0 | 281 | nsCStringKey groupKey(aGroup); |
michael@0 | 282 | nsTArray<char*>* commandList = (nsTArray<char*> *)mGroupsHash.Get(&groupKey); |
michael@0 | 283 | if (!commandList) return NS_OK; // no group |
michael@0 | 284 | |
michael@0 | 285 | uint32_t numEntries = commandList->Length(); |
michael@0 | 286 | for (uint32_t i = 0; i < numEntries; i ++) |
michael@0 | 287 | { |
michael@0 | 288 | char* commandString = commandList->ElementAt(i); |
michael@0 | 289 | if (!nsCRT::strcmp(aCommand,commandString)) |
michael@0 | 290 | { |
michael@0 | 291 | *_retval = true; |
michael@0 | 292 | break; |
michael@0 | 293 | } |
michael@0 | 294 | } |
michael@0 | 295 | return NS_OK; |
michael@0 | 296 | } |
michael@0 | 297 | |
michael@0 | 298 | /* nsISimpleEnumerator getGroupsEnumerator (); */ |
michael@0 | 299 | NS_IMETHODIMP |
michael@0 | 300 | nsControllerCommandGroup::GetGroupsEnumerator(nsISimpleEnumerator **_retval) |
michael@0 | 301 | { |
michael@0 | 302 | nsGroupsEnumerator* groupsEnum = new nsGroupsEnumerator(mGroupsHash); |
michael@0 | 303 | if (!groupsEnum) return NS_ERROR_OUT_OF_MEMORY; |
michael@0 | 304 | |
michael@0 | 305 | return groupsEnum->QueryInterface(NS_GET_IID(nsISimpleEnumerator), (void **)_retval); |
michael@0 | 306 | } |
michael@0 | 307 | |
michael@0 | 308 | /* nsISimpleEnumerator getEnumeratorForGroup (in DOMString aGroup); */ |
michael@0 | 309 | NS_IMETHODIMP |
michael@0 | 310 | nsControllerCommandGroup::GetEnumeratorForGroup(const char * aGroup, nsISimpleEnumerator **_retval) |
michael@0 | 311 | { |
michael@0 | 312 | nsCStringKey groupKey(aGroup); |
michael@0 | 313 | nsTArray<char*>* commandList = (nsTArray<char*> *)mGroupsHash.Get(&groupKey); // may be null |
michael@0 | 314 | |
michael@0 | 315 | nsNamedGroupEnumerator* theGroupEnum = new nsNamedGroupEnumerator(commandList); |
michael@0 | 316 | if (!theGroupEnum) return NS_ERROR_OUT_OF_MEMORY; |
michael@0 | 317 | |
michael@0 | 318 | return theGroupEnum->QueryInterface(NS_GET_IID(nsISimpleEnumerator), (void **)_retval); |
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 | bool nsControllerCommandGroup::ClearEnumerator(nsHashKey *aKey, void *aData, void* closure) |
michael@0 | 326 | { |
michael@0 | 327 | nsTArray<char*>* commandList = (nsTArray<char*> *)aData; |
michael@0 | 328 | if (commandList) |
michael@0 | 329 | { |
michael@0 | 330 | uint32_t numEntries = commandList->Length(); |
michael@0 | 331 | for (uint32_t i = 0; i < numEntries; i ++) |
michael@0 | 332 | { |
michael@0 | 333 | char* commandString = commandList->ElementAt(i); |
michael@0 | 334 | nsMemory::Free(commandString); |
michael@0 | 335 | } |
michael@0 | 336 | |
michael@0 | 337 | delete commandList; |
michael@0 | 338 | } |
michael@0 | 339 | |
michael@0 | 340 | return true; |
michael@0 | 341 | } |