diff --git a/embedding/components/commandhandler/src/nsCommandGroup.cpp b/embedding/components/commandhandler/src/nsCommandGroup.cpp index 82c42d4729d9..a481f53d5dae 100644 --- a/embedding/components/commandhandler/src/nsCommandGroup.cpp +++ b/embedding/components/commandhandler/src/nsCommandGroup.cpp @@ -25,7 +25,7 @@ public: NS_DECL_NSISIMPLEENUMERATOR protected: - static PLDHashOperator HashEnum(const nsACString &aKey, nsTArray *aData, void *aClosure); + static PLDHashOperator HashEnum(const nsACString &aKey, nsTArray *aData, void *aClosure); nsresult Initialize(); protected: @@ -100,7 +100,7 @@ nsGroupsEnumerator::GetNext(nsISupports **_retval) /* static */ /* return false to stop */ PLDHashOperator -nsGroupsEnumerator::HashEnum(const nsACString &aKey, nsTArray *aData, void *aClosure) +nsGroupsEnumerator::HashEnum(const nsACString &aKey, nsTArray *aData, void *aClosure) { nsGroupsEnumerator *groupsEnum = static_cast(aClosure); groupsEnum->mGroupNames[groupsEnum->mIndex] = (char*)aKey.Data(); @@ -131,18 +131,18 @@ nsGroupsEnumerator::Initialize() class nsNamedGroupEnumerator : public nsISimpleEnumerator { public: - nsNamedGroupEnumerator(nsTArray *inArray); + nsNamedGroupEnumerator(nsTArray *inArray); virtual ~nsNamedGroupEnumerator(); NS_DECL_ISUPPORTS NS_DECL_NSISIMPLEENUMERATOR protected: - nsTArray *mGroupArray; + nsTArray *mGroupArray; int32_t mIndex; }; -nsNamedGroupEnumerator::nsNamedGroupEnumerator(nsTArray *inArray) +nsNamedGroupEnumerator::nsNamedGroupEnumerator(nsTArray *inArray) : mGroupArray(inArray) , mIndex(-1) { @@ -178,13 +178,14 @@ nsNamedGroupEnumerator::GetNext(nsISupports **_retval) if (mIndex >= int32_t(mGroupArray->Length())) return NS_ERROR_FAILURE; - const nsCString& thisGroupName = mGroupArray->ElementAt(mIndex); + char16_t *thisGroupName = (char16_t*)mGroupArray->ElementAt(mIndex); + NS_ASSERTION(thisGroupName, "Bad Element in mGroupArray"); nsresult rv; - nsCOMPtr supportsString = do_CreateInstance(NS_SUPPORTS_CSTRING_CONTRACTID, &rv); + nsCOMPtr supportsString = do_CreateInstance(NS_SUPPORTS_STRING_CONTRACTID, &rv); if (NS_FAILED(rv)) return rv; - supportsString->SetData(thisGroupName); + supportsString->SetData(nsDependentString(thisGroupName)); return CallQueryInterface(supportsString, _retval); } @@ -208,6 +209,7 @@ nsControllerCommandGroup::~nsControllerCommandGroup() void nsControllerCommandGroup::ClearGroupsHash() { + mGroupsHash.EnumerateRead(ClearEnumerator, nullptr); mGroupsHash.Clear(); } @@ -220,18 +222,21 @@ NS_IMETHODIMP nsControllerCommandGroup::AddCommandToGroup(const char *aCommand, const char *aGroup) { nsDependentCString groupKey(aGroup); - nsTArray *commandList; + nsTArray *commandList; if ((commandList = mGroupsHash.Get(groupKey)) == nullptr) { // make this list - commandList = new nsAutoTArray; + commandList = new nsAutoTArray; mGroupsHash.Put(groupKey, commandList); } - + // add the command to the list. Note that we're not checking for duplicates here + char *commandString = NS_strdup(aCommand); // we store allocated char16_t* in the array + if (!commandString) return NS_ERROR_OUT_OF_MEMORY; + #ifdef DEBUG - nsCString *appended = + char **appended = #endif - commandList->AppendElement(aCommand); + commandList->AppendElement(commandString); NS_ASSERTION(appended, "Append failed"); return NS_OK; @@ -242,19 +247,21 @@ NS_IMETHODIMP nsControllerCommandGroup::RemoveCommandFromGroup(const char *aCommand, const char *aGroup) { nsDependentCString groupKey(aGroup); - nsTArray *commandList = mGroupsHash.Get(groupKey); + nsTArray *commandList = mGroupsHash.Get(groupKey); if (!commandList) return NS_OK; // no group uint32_t numEntries = commandList->Length(); for (uint32_t i = 0; i < numEntries; i++) { - nsCString commandString = commandList->ElementAt(i); - if (nsDependentCString(aCommand) != commandString) + char *commandString = commandList->ElementAt(i); + if (!nsCRT::strcmp(aCommand,commandString)) { commandList->RemoveElementAt(i); + nsMemory::Free(commandString); break; } } + return NS_OK; } @@ -266,14 +273,14 @@ nsControllerCommandGroup::IsCommandInGroup(const char *aCommand, const char *aGr *_retval = false; nsDependentCString groupKey(aGroup); - nsTArray *commandList = mGroupsHash.Get(groupKey); + nsTArray *commandList = mGroupsHash.Get(groupKey); if (!commandList) return NS_OK; // no group uint32_t numEntries = commandList->Length(); for (uint32_t i = 0; i < numEntries; i++) { - nsCString commandString = commandList->ElementAt(i); - if (nsDependentCString(aCommand) != commandString) + char *commandString = commandList->ElementAt(i); + if (!nsCRT::strcmp(aCommand,commandString)) { *_retval = true; break; @@ -297,9 +304,9 @@ NS_IMETHODIMP nsControllerCommandGroup::GetEnumeratorForGroup(const char *aGroup, nsISimpleEnumerator **_retval) { nsDependentCString groupKey(aGroup); - nsTArray *commandList = mGroupsHash.Get(groupKey); // may be null + nsTArray *commandList = mGroupsHash.Get(groupKey); // may be null - nsNamedGroupEnumerator *theGroupEnum = new nsNamedGroupEnumerator(commandList); + nsNamedGroupEnumerator* theGroupEnum = new nsNamedGroupEnumerator(commandList); if (!theGroupEnum) return NS_ERROR_OUT_OF_MEMORY; return theGroupEnum->QueryInterface(NS_GET_IID(nsISimpleEnumerator), (void **)_retval); @@ -308,3 +315,19 @@ nsControllerCommandGroup::GetEnumeratorForGroup(const char *aGroup, nsISimpleEnu #if 0 #pragma mark - #endif + +PLDHashOperator +nsControllerCommandGroup::ClearEnumerator(const nsACString &aKey, nsTArray *aData, void *closure) +{ + nsTArray *commandList = aData; + if (commandList) + { + uint32_t numEntries = commandList->Length(); + for (uint32_t i = 0; i < numEntries; i++) + { + char *commandString = commandList->ElementAt(i); + nsMemory::Free(commandString); + } + } + return PL_DHASH_NEXT; +} diff --git a/embedding/components/commandhandler/src/nsCommandGroup.h b/embedding/components/commandhandler/src/nsCommandGroup.h index 30f9a4c584bc..bdab0204cdc6 100644 --- a/embedding/components/commandhandler/src/nsCommandGroup.h +++ b/embedding/components/commandhandler/src/nsCommandGroup.h @@ -3,13 +3,17 @@ * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + + #ifndef nsCommandGroup_h__ #define nsCommandGroup_h__ #include "nsIController.h" +#include "nsHashtable.h" #include "nsClassHashtable.h" #include "nsHashKeys.h" + // {ecd55a01-2780-11d5-a73c-ca641a6813bc} #define NS_CONTROLLER_COMMAND_GROUP_CID \ { 0xecd55a01, 0x2780, 0x11d5, { 0xa7, 0x3c, 0xca, 0x64, 0x1a, 0x68, 0x13, 0xbc } } @@ -17,6 +21,7 @@ #define NS_CONTROLLER_COMMAND_GROUP_CONTRACTID \ "@mozilla.org/embedcomp/controller-command-group;1" + class nsControllerCommandGroup : public nsIControllerCommandGroup { public: @@ -27,14 +32,16 @@ public: NS_DECL_NSICONTROLLERCOMMANDGROUP public: - typedef nsClassHashtable> GroupsHashtable; + typedef nsClassHashtable> GroupsHashtable; protected: void ClearGroupsHash(); + static PLDHashOperator ClearEnumerator(const nsACString &aKey, nsTArray *aData, void *closure); protected: - GroupsHashtable mGroupsHash; // hash keyed on command group. - // This could be made more space-efficient, maybe with atoms + nsClassHashtable> mGroupsHash; // hash keyed on command group. + // This could be made more space-efficient, maybe with atoms + }; #endif // nsCommandGroup_h__