embedding/components/commandhandler/src/nsCommandGroup.cpp

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

     1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     2 /* This Source Code Form is subject to the terms of the Mozilla Public
     3  * License, v. 2.0. If a copy of the MPL was not distributed with this
     4  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     6 #include "nsString.h"
     7 #include "nsReadableUtils.h"
     8 #include "nsTArray.h"
     9 #include "nsISimpleEnumerator.h"
    10 #include "nsXPCOM.h"
    11 #include "nsSupportsPrimitives.h"
    12 #include "nsIComponentManager.h"
    14 #include "nsCommandGroup.h"
    15 #include "nsIControllerCommand.h"
    16 #include "nsCRT.h"
    19 class nsGroupsEnumerator : public nsISimpleEnumerator
    20 {
    21 public:
    22               nsGroupsEnumerator(nsHashtable& inHashTable);
    23   virtual     ~nsGroupsEnumerator();
    25   NS_DECL_ISUPPORTS
    26   NS_DECL_NSISIMPLEENUMERATOR
    28 protected:
    30   static bool HashEnum(nsHashKey *aKey, void *aData, void* aClosure);
    32   nsresult      Initialize();
    34 protected:
    36   nsHashtable&  mHashTable;
    37   int32_t       mIndex;
    38   char **       mGroupNames;        // array of pointers to char16_t* in the hash table
    39   bool          mInitted;
    41 };
    43 /* Implementation file */
    44 NS_IMPL_ISUPPORTS(nsGroupsEnumerator, nsISimpleEnumerator)
    46 nsGroupsEnumerator::nsGroupsEnumerator(nsHashtable& inHashTable)
    47 : mHashTable(inHashTable)
    48 , mIndex(-1)
    49 , mGroupNames(nullptr)
    50 , mInitted(false)
    51 {
    52   /* member initializers and constructor code */
    53 }
    55 nsGroupsEnumerator::~nsGroupsEnumerator()
    56 {
    57   delete [] mGroupNames;    // ok on null pointer
    58 }
    60 /* boolean hasMoreElements (); */
    61 NS_IMETHODIMP
    62 nsGroupsEnumerator::HasMoreElements(bool *_retval)
    63 {
    64   nsresult  rv = NS_OK;
    66   NS_ENSURE_ARG_POINTER(_retval);
    68   if (!mInitted) {
    69     rv = Initialize();
    70     if (NS_FAILED(rv)) return rv;
    71   }
    73   *_retval = (mIndex < mHashTable.Count() - 1); 
    74   return NS_OK;
    75 }
    77 /* nsISupports getNext (); */
    78 NS_IMETHODIMP
    79 nsGroupsEnumerator::GetNext(nsISupports **_retval)
    80 {
    81   nsresult  rv = NS_OK;
    83   NS_ENSURE_ARG_POINTER(_retval);
    85   if (!mInitted) {
    86     rv = Initialize();
    87     if (NS_FAILED(rv)) return rv;
    88   }
    90   mIndex ++;
    91   if (mIndex >= mHashTable.Count())
    92     return NS_ERROR_FAILURE;
    94   char *thisGroupName = mGroupNames[mIndex];
    96   nsCOMPtr<nsISupportsCString> supportsString = do_CreateInstance(NS_SUPPORTS_CSTRING_CONTRACTID, &rv);
    97   if (NS_FAILED(rv)) return rv;
    99   supportsString->SetData(nsDependentCString(thisGroupName));
   100   return CallQueryInterface(supportsString, _retval);
   101 }
   103 /* static */
   104 /* return false to stop */
   105 bool
   106 nsGroupsEnumerator::HashEnum(nsHashKey *aKey, void *aData, void* aClosure)
   107 {
   108   nsGroupsEnumerator*   groupsEnum = reinterpret_cast<nsGroupsEnumerator *>(aClosure);
   109   nsCStringKey*         stringKey = static_cast<nsCStringKey*>(aKey);
   111   groupsEnum->mGroupNames[groupsEnum->mIndex] = (char*)stringKey->GetString();
   112   groupsEnum->mIndex ++;
   113   return true;
   114 }
   116 nsresult
   117 nsGroupsEnumerator::Initialize()
   118 {
   119   if (mInitted) return NS_OK;
   121   mGroupNames = new char*[mHashTable.Count()];
   122   if (!mGroupNames) return NS_ERROR_OUT_OF_MEMORY;
   124   mIndex = 0; 
   125   mHashTable.Enumerate(HashEnum, (void*)this);
   127   mIndex = -1;
   128   mInitted = true;
   129   return NS_OK;
   130 }
   132 #if 0
   133 #pragma mark -
   134 #endif
   136 class nsNamedGroupEnumerator : public nsISimpleEnumerator
   137 {
   138 public:
   139               nsNamedGroupEnumerator(nsTArray<char*>* inArray);
   140   virtual     ~nsNamedGroupEnumerator();
   142   NS_DECL_ISUPPORTS
   143   NS_DECL_NSISIMPLEENUMERATOR
   145 protected:
   147   nsTArray<char*>* mGroupArray;
   148   int32_t          mIndex;
   150 };
   152 nsNamedGroupEnumerator::nsNamedGroupEnumerator(nsTArray<char*>* inArray)
   153 : mGroupArray(inArray)
   154 , mIndex(-1)
   155 {
   156 }
   158 nsNamedGroupEnumerator::~nsNamedGroupEnumerator()
   159 {
   160 }
   162 NS_IMPL_ISUPPORTS(nsNamedGroupEnumerator, nsISimpleEnumerator)
   164 /* boolean hasMoreElements (); */
   165 NS_IMETHODIMP
   166 nsNamedGroupEnumerator::HasMoreElements(bool *_retval)
   167 {
   168   NS_ENSURE_ARG_POINTER(_retval);
   170   int32_t arrayLen = mGroupArray ? mGroupArray->Length() : 0;
   171   *_retval = (mIndex < arrayLen - 1); 
   172   return NS_OK;
   173 }
   175 /* nsISupports getNext (); */
   176 NS_IMETHODIMP
   177 nsNamedGroupEnumerator::GetNext(nsISupports **_retval)
   178 {
   179   NS_ENSURE_ARG_POINTER(_retval);
   181   if (!mGroupArray)
   182     return NS_ERROR_FAILURE;
   184   mIndex ++;
   185   if (mIndex >= int32_t(mGroupArray->Length()))
   186     return NS_ERROR_FAILURE;
   188   char16_t   *thisGroupName = (char16_t*)mGroupArray->ElementAt(mIndex);
   189   NS_ASSERTION(thisGroupName, "Bad Element in mGroupArray");
   191   nsresult rv;
   192   nsCOMPtr<nsISupportsString> supportsString = do_CreateInstance(NS_SUPPORTS_STRING_CONTRACTID, &rv);
   193   if (NS_FAILED(rv)) return rv;
   195   supportsString->SetData(nsDependentString(thisGroupName));
   196   return CallQueryInterface(supportsString, _retval);
   197 }
   199 #if 0
   200 #pragma mark -
   201 #endif
   204 /* Implementation file */
   205 NS_IMPL_ISUPPORTS(nsControllerCommandGroup, nsIControllerCommandGroup)
   207 nsControllerCommandGroup::nsControllerCommandGroup()
   208 {
   209 }
   211 nsControllerCommandGroup::~nsControllerCommandGroup()
   212 {
   213   ClearGroupsHash();
   214 }
   216 void
   217 nsControllerCommandGroup::ClearGroupsHash()
   218 {
   219     mGroupsHash.Reset(ClearEnumerator, (void *)this);
   220 }
   222 #if 0
   223 #pragma mark -
   224 #endif
   226 /* void addCommandToGroup (in DOMString aCommand, in DOMString aGroup); */
   227 NS_IMETHODIMP
   228 nsControllerCommandGroup::AddCommandToGroup(const char * aCommand, const char *aGroup)
   229 {
   230   nsCStringKey   groupKey(aGroup);  
   231   nsTArray<char*>* commandList;
   232   if ((commandList = (nsTArray<char*> *)mGroupsHash.Get(&groupKey)) == nullptr)
   233   {
   234     // make this list
   235     commandList = new nsAutoTArray<char*, 8>;
   236     mGroupsHash.Put(&groupKey, (void *)commandList);
   237   }
   238   // add the command to the list. Note that we're not checking for duplicates here
   239   char* commandString = NS_strdup(aCommand); // we store allocated char16_t* in the array
   240   if (!commandString) return NS_ERROR_OUT_OF_MEMORY;
   242 #ifdef DEBUG
   243   char** appended =
   244 #endif
   245     commandList->AppendElement(commandString);
   246   NS_ASSERTION(appended, "Append failed");
   248   return NS_OK;
   249 }
   251 /* void removeCommandFromGroup (in DOMString aCommand, in DOMString aGroup); */
   252 NS_IMETHODIMP
   253 nsControllerCommandGroup::RemoveCommandFromGroup(const char * aCommand, const char * aGroup)
   254 {
   255   nsCStringKey   groupKey(aGroup);
   256   nsTArray<char*>* commandList = (nsTArray<char*> *)mGroupsHash.Get(&groupKey);
   257   if (!commandList) return NS_OK;     // no group
   259   uint32_t numEntries = commandList->Length();
   260   for (uint32_t i = 0; i < numEntries; i ++)
   261   {
   262     char*  commandString = commandList->ElementAt(i);
   263     if (!nsCRT::strcmp(aCommand,commandString))
   264     {
   265       commandList->RemoveElementAt(i);
   266       nsMemory::Free(commandString);
   267       break;
   268     }
   269   }
   271   return NS_OK;
   272 }
   274 /* boolean isCommandInGroup (in DOMString aCommand, in DOMString aGroup); */
   275 NS_IMETHODIMP
   276 nsControllerCommandGroup::IsCommandInGroup(const char * aCommand, const char * aGroup, bool *_retval)
   277 {
   278   NS_ENSURE_ARG_POINTER(_retval);
   279   *_retval = false;
   281   nsCStringKey   groupKey(aGroup);
   282   nsTArray<char*>* commandList = (nsTArray<char*> *)mGroupsHash.Get(&groupKey);
   283   if (!commandList) return NS_OK;     // no group
   285   uint32_t numEntries = commandList->Length();
   286   for (uint32_t i = 0; i < numEntries; i ++)
   287   {
   288     char*  commandString = commandList->ElementAt(i);
   289     if (!nsCRT::strcmp(aCommand,commandString))
   290     {
   291       *_retval = true;
   292       break;
   293     }
   294   }
   295   return NS_OK;
   296 }
   298 /* nsISimpleEnumerator getGroupsEnumerator (); */
   299 NS_IMETHODIMP
   300 nsControllerCommandGroup::GetGroupsEnumerator(nsISimpleEnumerator **_retval)
   301 {
   302   nsGroupsEnumerator*   groupsEnum = new nsGroupsEnumerator(mGroupsHash);
   303   if (!groupsEnum) return NS_ERROR_OUT_OF_MEMORY;
   305   return groupsEnum->QueryInterface(NS_GET_IID(nsISimpleEnumerator), (void **)_retval);
   306 }
   308 /* nsISimpleEnumerator getEnumeratorForGroup (in DOMString aGroup); */
   309 NS_IMETHODIMP
   310 nsControllerCommandGroup::GetEnumeratorForGroup(const char * aGroup, nsISimpleEnumerator **_retval)
   311 {
   312   nsCStringKey   groupKey(aGroup);  
   313   nsTArray<char*>* commandList = (nsTArray<char*> *)mGroupsHash.Get(&groupKey); // may be null
   315   nsNamedGroupEnumerator*   theGroupEnum = new nsNamedGroupEnumerator(commandList);
   316   if (!theGroupEnum) return NS_ERROR_OUT_OF_MEMORY;
   318   return theGroupEnum->QueryInterface(NS_GET_IID(nsISimpleEnumerator), (void **)_retval);
   319 }
   321 #if 0
   322 #pragma mark -
   323 #endif
   325 bool nsControllerCommandGroup::ClearEnumerator(nsHashKey *aKey, void *aData, void* closure)
   326 {
   327   nsTArray<char*>* commandList = (nsTArray<char*> *)aData;
   328   if (commandList)
   329   {  
   330     uint32_t numEntries = commandList->Length();
   331     for (uint32_t i = 0; i < numEntries; i ++)
   332     {
   333       char* commandString = commandList->ElementAt(i);
   334       nsMemory::Free(commandString);
   335     }
   337     delete commandList;
   338   }
   340   return true;
   341 }

mercurial