nsCommandHandler.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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 "nsCommandHandler.h"
  6. #include "nsWebBrowser.h"
  7. #include "nsDocShellTreeOwner.h"
  8. #include "nsMemory.h"
  9. #include "nsPIDOMWindow.h"
  10. nsCommandHandler::nsCommandHandler()
  11. : mWindow(nullptr)
  12. {
  13. }
  14. nsCommandHandler::~nsCommandHandler()
  15. {
  16. }
  17. nsresult
  18. nsCommandHandler::GetCommandHandler(nsICommandHandler** aCommandHandler)
  19. {
  20. NS_ENSURE_ARG_POINTER(aCommandHandler);
  21. *aCommandHandler = nullptr;
  22. if (!mWindow) {
  23. return NS_ERROR_FAILURE;
  24. }
  25. // Get the document tree owner
  26. nsCOMPtr<nsIDocShellTreeItem> docShellAsTreeItem =
  27. do_QueryInterface(mWindow->GetDocShell());
  28. nsIDocShellTreeOwner* treeOwner = nullptr;
  29. docShellAsTreeItem->GetTreeOwner(&treeOwner);
  30. // Make sure the tree owner is an an nsDocShellTreeOwner object
  31. // by QI'ing for a hidden interface. If it doesn't have the interface
  32. // then it's not safe to do the casting.
  33. nsCOMPtr<nsICDocShellTreeOwner> realTreeOwner(do_QueryInterface(treeOwner));
  34. if (realTreeOwner) {
  35. nsDocShellTreeOwner* tree = static_cast<nsDocShellTreeOwner*>(treeOwner);
  36. if (tree->mTreeOwner) {
  37. nsresult rv;
  38. rv = tree->mTreeOwner->QueryInterface(NS_GET_IID(nsICommandHandler),
  39. (void**)aCommandHandler);
  40. NS_RELEASE(treeOwner);
  41. return rv;
  42. }
  43. NS_RELEASE(treeOwner);
  44. }
  45. *aCommandHandler = nullptr;
  46. return NS_OK;
  47. }
  48. NS_IMPL_ADDREF(nsCommandHandler)
  49. NS_IMPL_RELEASE(nsCommandHandler)
  50. NS_INTERFACE_MAP_BEGIN(nsCommandHandler)
  51. NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsICommandHandler)
  52. NS_INTERFACE_MAP_ENTRY(nsICommandHandlerInit)
  53. NS_INTERFACE_MAP_ENTRY(nsICommandHandler)
  54. NS_INTERFACE_MAP_END
  55. ///////////////////////////////////////////////////////////////////////////////
  56. // nsICommandHandlerInit implementation
  57. NS_IMETHODIMP
  58. nsCommandHandler::GetWindow(mozIDOMWindowProxy** aWindow)
  59. {
  60. *aWindow = nullptr;
  61. return NS_OK;
  62. }
  63. NS_IMETHODIMP
  64. nsCommandHandler::SetWindow(mozIDOMWindowProxy* aWindow)
  65. {
  66. if (!aWindow) {
  67. return NS_ERROR_FAILURE;
  68. }
  69. mWindow = nsPIDOMWindowOuter::From(aWindow);
  70. return NS_OK;
  71. }
  72. ///////////////////////////////////////////////////////////////////////////////
  73. // nsICommandHandler implementation
  74. NS_IMETHODIMP
  75. nsCommandHandler::Exec(const char* aCommand, const char* aStatus,
  76. char** aResult)
  77. {
  78. NS_ENSURE_ARG_POINTER(aCommand);
  79. NS_ENSURE_ARG_POINTER(aResult);
  80. nsCOMPtr<nsICommandHandler> commandHandler;
  81. GetCommandHandler(getter_AddRefs(commandHandler));
  82. // Call the client's command handler to deal with this command
  83. if (commandHandler) {
  84. *aResult = nullptr;
  85. return commandHandler->Exec(aCommand, aStatus, aResult);
  86. }
  87. // Return an empty string
  88. const char szEmpty[] = "";
  89. *aResult = (char*)nsMemory::Clone(szEmpty, sizeof(szEmpty));
  90. return NS_OK;
  91. }
  92. NS_IMETHODIMP
  93. nsCommandHandler::Query(const char* aCommand, const char* aStatus,
  94. char** aResult)
  95. {
  96. NS_ENSURE_ARG_POINTER(aCommand);
  97. NS_ENSURE_ARG_POINTER(aResult);
  98. nsCOMPtr<nsICommandHandler> commandHandler;
  99. GetCommandHandler(getter_AddRefs(commandHandler));
  100. // Call the client's command handler to deal with this command
  101. if (commandHandler) {
  102. *aResult = nullptr;
  103. return commandHandler->Query(aCommand, aStatus, aResult);
  104. }
  105. // Return an empty string
  106. const char szEmpty[] = "";
  107. *aResult = (char*)nsMemory::Clone(szEmpty, sizeof(szEmpty));
  108. return NS_OK;
  109. }