nsControllerCommandTable.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /* -*- Mode: C++; tab-width: 8; 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/. */
  5. #include "nsString.h"
  6. #include "nsIControllerCommand.h"
  7. #include "nsControllerCommandTable.h"
  8. nsresult NS_NewControllerCommandTable(nsIControllerCommandTable** aResult);
  9. // this value is used to size the hash table. Just a sensible upper bound
  10. #define NUM_COMMANDS_LENGTH 32
  11. nsControllerCommandTable::nsControllerCommandTable()
  12. : mCommandsTable(NUM_COMMANDS_LENGTH)
  13. , mMutable(true)
  14. {
  15. }
  16. nsControllerCommandTable::~nsControllerCommandTable()
  17. {
  18. }
  19. NS_IMPL_ISUPPORTS(nsControllerCommandTable, nsIControllerCommandTable,
  20. nsISupportsWeakReference)
  21. NS_IMETHODIMP
  22. nsControllerCommandTable::MakeImmutable(void)
  23. {
  24. mMutable = false;
  25. return NS_OK;
  26. }
  27. NS_IMETHODIMP
  28. nsControllerCommandTable::RegisterCommand(const char* aCommandName,
  29. nsIControllerCommand* aCommand)
  30. {
  31. NS_ENSURE_TRUE(mMutable, NS_ERROR_FAILURE);
  32. mCommandsTable.Put(nsDependentCString(aCommandName), aCommand);
  33. return NS_OK;
  34. }
  35. NS_IMETHODIMP
  36. nsControllerCommandTable::UnregisterCommand(const char* aCommandName,
  37. nsIControllerCommand* aCommand)
  38. {
  39. NS_ENSURE_TRUE(mMutable, NS_ERROR_FAILURE);
  40. nsDependentCString commandKey(aCommandName);
  41. if (!mCommandsTable.Get(commandKey, nullptr)) {
  42. return NS_ERROR_FAILURE;
  43. }
  44. mCommandsTable.Remove(commandKey);
  45. return NS_OK;
  46. }
  47. NS_IMETHODIMP
  48. nsControllerCommandTable::FindCommandHandler(const char* aCommandName,
  49. nsIControllerCommand** aResult)
  50. {
  51. NS_ENSURE_ARG_POINTER(aResult);
  52. *aResult = nullptr;
  53. nsCOMPtr<nsIControllerCommand> foundCommand;
  54. mCommandsTable.Get(nsDependentCString(aCommandName),
  55. getter_AddRefs(foundCommand));
  56. if (!foundCommand) {
  57. return NS_ERROR_FAILURE;
  58. }
  59. foundCommand.forget(aResult);
  60. return NS_OK;
  61. }
  62. NS_IMETHODIMP
  63. nsControllerCommandTable::IsCommandEnabled(const char* aCommandName,
  64. nsISupports* aCommandRefCon,
  65. bool* aResult)
  66. {
  67. NS_ENSURE_ARG_POINTER(aResult);
  68. *aResult = false;
  69. nsCOMPtr<nsIControllerCommand> commandHandler;
  70. FindCommandHandler(aCommandName, getter_AddRefs(commandHandler));
  71. if (!commandHandler) {
  72. NS_WARNING("Controller command table asked about a command that it does "
  73. "not handle");
  74. return NS_OK;
  75. }
  76. return commandHandler->IsCommandEnabled(aCommandName, aCommandRefCon,
  77. aResult);
  78. }
  79. NS_IMETHODIMP
  80. nsControllerCommandTable::UpdateCommandState(const char* aCommandName,
  81. nsISupports* aCommandRefCon)
  82. {
  83. nsCOMPtr<nsIControllerCommand> commandHandler;
  84. FindCommandHandler(aCommandName, getter_AddRefs(commandHandler));
  85. if (!commandHandler) {
  86. NS_WARNING("Controller command table asked to update the state of a "
  87. "command that it does not handle");
  88. return NS_OK;
  89. }
  90. return NS_ERROR_NOT_IMPLEMENTED;
  91. }
  92. NS_IMETHODIMP
  93. nsControllerCommandTable::SupportsCommand(const char* aCommandName,
  94. nsISupports* aCommandRefCon,
  95. bool* aResult)
  96. {
  97. NS_ENSURE_ARG_POINTER(aResult);
  98. // XXX: need to check the readonly and disabled states
  99. *aResult = false;
  100. nsCOMPtr<nsIControllerCommand> commandHandler;
  101. FindCommandHandler(aCommandName, getter_AddRefs(commandHandler));
  102. *aResult = (commandHandler.get() != nullptr);
  103. return NS_OK;
  104. }
  105. NS_IMETHODIMP
  106. nsControllerCommandTable::DoCommand(const char* aCommandName,
  107. nsISupports* aCommandRefCon)
  108. {
  109. nsCOMPtr<nsIControllerCommand> commandHandler;
  110. FindCommandHandler(aCommandName, getter_AddRefs(commandHandler));
  111. if (!commandHandler) {
  112. NS_WARNING("Controller command table asked to do a command that it does "
  113. "not handle");
  114. return NS_OK;
  115. }
  116. return commandHandler->DoCommand(aCommandName, aCommandRefCon);
  117. }
  118. NS_IMETHODIMP
  119. nsControllerCommandTable::DoCommandParams(const char* aCommandName,
  120. nsICommandParams* aParams,
  121. nsISupports* aCommandRefCon)
  122. {
  123. nsCOMPtr<nsIControllerCommand> commandHandler;
  124. FindCommandHandler(aCommandName, getter_AddRefs(commandHandler));
  125. if (!commandHandler) {
  126. NS_WARNING("Controller command table asked to do a command that it does "
  127. "not handle");
  128. return NS_OK;
  129. }
  130. return commandHandler->DoCommandParams(aCommandName, aParams, aCommandRefCon);
  131. }
  132. NS_IMETHODIMP
  133. nsControllerCommandTable::GetCommandState(const char* aCommandName,
  134. nsICommandParams* aParams,
  135. nsISupports* aCommandRefCon)
  136. {
  137. nsCOMPtr<nsIControllerCommand> commandHandler;
  138. FindCommandHandler(aCommandName, getter_AddRefs(commandHandler));
  139. if (!commandHandler) {
  140. NS_WARNING("Controller command table asked to do a command that it does "
  141. "not handle");
  142. return NS_OK;
  143. }
  144. return commandHandler->GetCommandStateParams(aCommandName, aParams,
  145. aCommandRefCon);
  146. }
  147. NS_IMETHODIMP
  148. nsControllerCommandTable::GetSupportedCommands(uint32_t* aCount,
  149. char*** aCommands)
  150. {
  151. char** commands =
  152. static_cast<char**>(moz_xmalloc(sizeof(char*) * mCommandsTable.Count()));
  153. *aCount = mCommandsTable.Count();
  154. *aCommands = commands;
  155. for (auto iter = mCommandsTable.Iter(); !iter.Done(); iter.Next()) {
  156. *commands = ToNewCString(iter.Key());
  157. commands++;
  158. }
  159. return NS_OK;
  160. }
  161. nsresult
  162. NS_NewControllerCommandTable(nsIControllerCommandTable** aResult)
  163. {
  164. NS_PRECONDITION(aResult != nullptr, "null ptr");
  165. if (!aResult) {
  166. return NS_ERROR_NULL_POINTER;
  167. }
  168. nsControllerCommandTable* newCommandTable = new nsControllerCommandTable();
  169. NS_ADDREF(newCommandTable);
  170. *aResult = newCommandTable;
  171. return NS_OK;
  172. }