nsBaseCommandController.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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 "nsIComponentManager.h"
  7. #include "nsBaseCommandController.h"
  8. #include "nsString.h"
  9. #include "nsWeakPtr.h"
  10. NS_IMPL_ADDREF(nsBaseCommandController)
  11. NS_IMPL_RELEASE(nsBaseCommandController)
  12. NS_INTERFACE_MAP_BEGIN(nsBaseCommandController)
  13. NS_INTERFACE_MAP_ENTRY(nsIController)
  14. NS_INTERFACE_MAP_ENTRY(nsICommandController)
  15. NS_INTERFACE_MAP_ENTRY(nsIControllerContext)
  16. NS_INTERFACE_MAP_ENTRY(nsIInterfaceRequestor)
  17. NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIControllerContext)
  18. NS_INTERFACE_MAP_END
  19. nsBaseCommandController::nsBaseCommandController()
  20. : mCommandContextRawPtr(nullptr)
  21. {
  22. }
  23. nsBaseCommandController::~nsBaseCommandController()
  24. {
  25. }
  26. NS_IMETHODIMP
  27. nsBaseCommandController::Init(nsIControllerCommandTable* aCommandTable)
  28. {
  29. nsresult rv = NS_OK;
  30. if (aCommandTable) {
  31. mCommandTable = aCommandTable;
  32. } else {
  33. mCommandTable =
  34. do_CreateInstance(NS_CONTROLLERCOMMANDTABLE_CONTRACTID, &rv);
  35. }
  36. return rv;
  37. }
  38. NS_IMETHODIMP
  39. nsBaseCommandController::SetCommandContext(nsISupports* aCommandContext)
  40. {
  41. mCommandContextWeakPtr = nullptr;
  42. mCommandContextRawPtr = nullptr;
  43. if (aCommandContext) {
  44. nsCOMPtr<nsISupportsWeakReference> weak = do_QueryInterface(aCommandContext);
  45. if (weak) {
  46. nsresult rv =
  47. weak->GetWeakReference(getter_AddRefs(mCommandContextWeakPtr));
  48. NS_ENSURE_SUCCESS(rv, rv);
  49. } else {
  50. mCommandContextRawPtr = aCommandContext;
  51. }
  52. }
  53. return NS_OK;
  54. }
  55. NS_IMETHODIMP
  56. nsBaseCommandController::GetInterface(const nsIID& aIID, void** aResult)
  57. {
  58. NS_ENSURE_ARG_POINTER(aResult);
  59. if (NS_SUCCEEDED(QueryInterface(aIID, aResult))) {
  60. return NS_OK;
  61. }
  62. if (aIID.Equals(NS_GET_IID(nsIControllerCommandTable))) {
  63. if (mCommandTable) {
  64. return mCommandTable->QueryInterface(aIID, aResult);
  65. }
  66. return NS_ERROR_NOT_INITIALIZED;
  67. }
  68. return NS_NOINTERFACE;
  69. }
  70. /* =======================================================================
  71. * nsIController
  72. * ======================================================================= */
  73. NS_IMETHODIMP
  74. nsBaseCommandController::IsCommandEnabled(const char* aCommand, bool* aResult)
  75. {
  76. NS_ENSURE_ARG_POINTER(aCommand);
  77. NS_ENSURE_ARG_POINTER(aResult);
  78. NS_ENSURE_STATE(mCommandTable);
  79. nsISupports* context = mCommandContextRawPtr;
  80. nsCOMPtr<nsISupports> weak;
  81. if (!context) {
  82. weak = do_QueryReferent(mCommandContextWeakPtr);
  83. context = weak;
  84. }
  85. return mCommandTable->IsCommandEnabled(aCommand, context, aResult);
  86. }
  87. NS_IMETHODIMP
  88. nsBaseCommandController::SupportsCommand(const char* aCommand, bool* aResult)
  89. {
  90. NS_ENSURE_ARG_POINTER(aCommand);
  91. NS_ENSURE_ARG_POINTER(aResult);
  92. NS_ENSURE_STATE(mCommandTable);
  93. nsISupports* context = mCommandContextRawPtr;
  94. nsCOMPtr<nsISupports> weak;
  95. if (!context) {
  96. weak = do_QueryReferent(mCommandContextWeakPtr);
  97. context = weak;
  98. }
  99. return mCommandTable->SupportsCommand(aCommand, context, aResult);
  100. }
  101. NS_IMETHODIMP
  102. nsBaseCommandController::DoCommand(const char* aCommand)
  103. {
  104. NS_ENSURE_ARG_POINTER(aCommand);
  105. NS_ENSURE_STATE(mCommandTable);
  106. nsISupports* context = mCommandContextRawPtr;
  107. nsCOMPtr<nsISupports> weak;
  108. if (!context) {
  109. weak = do_QueryReferent(mCommandContextWeakPtr);
  110. context = weak;
  111. }
  112. return mCommandTable->DoCommand(aCommand, context);
  113. }
  114. NS_IMETHODIMP
  115. nsBaseCommandController::DoCommandWithParams(const char* aCommand,
  116. nsICommandParams* aParams)
  117. {
  118. NS_ENSURE_ARG_POINTER(aCommand);
  119. NS_ENSURE_STATE(mCommandTable);
  120. nsISupports* context = mCommandContextRawPtr;
  121. nsCOMPtr<nsISupports> weak;
  122. if (!context) {
  123. weak = do_QueryReferent(mCommandContextWeakPtr);
  124. context = weak;
  125. }
  126. return mCommandTable->DoCommandParams(aCommand, aParams, context);
  127. }
  128. NS_IMETHODIMP
  129. nsBaseCommandController::GetCommandStateWithParams(const char* aCommand,
  130. nsICommandParams* aParams)
  131. {
  132. NS_ENSURE_ARG_POINTER(aCommand);
  133. NS_ENSURE_STATE(mCommandTable);
  134. nsISupports* context = mCommandContextRawPtr;
  135. nsCOMPtr<nsISupports> weak;
  136. if (!context) {
  137. weak = do_QueryReferent(mCommandContextWeakPtr);
  138. context = weak;
  139. }
  140. return mCommandTable->GetCommandState(aCommand, aParams, context);
  141. }
  142. NS_IMETHODIMP
  143. nsBaseCommandController::OnEvent(const char* aEventName)
  144. {
  145. NS_ENSURE_ARG_POINTER(aEventName);
  146. return NS_OK;
  147. }
  148. NS_IMETHODIMP
  149. nsBaseCommandController::GetSupportedCommands(uint32_t* aCount,
  150. char*** aCommands)
  151. {
  152. NS_ENSURE_STATE(mCommandTable);
  153. return mCommandTable->GetSupportedCommands(aCount, aCommands);
  154. }