michael@0: /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- michael@0: * michael@0: * This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "nsCommandHandler.h" michael@0: #include "nsWebBrowser.h" michael@0: #include "nsDocShellTreeOwner.h" michael@0: michael@0: #include "nsMemory.h" michael@0: #include "nsPIDOMWindow.h" michael@0: michael@0: nsCommandHandler::nsCommandHandler() : michael@0: mWindow(nullptr) michael@0: { michael@0: } michael@0: michael@0: nsCommandHandler::~nsCommandHandler() michael@0: { michael@0: } michael@0: michael@0: nsresult nsCommandHandler::GetCommandHandler(nsICommandHandler **aCommandHandler) michael@0: { michael@0: NS_ENSURE_ARG_POINTER(aCommandHandler); michael@0: michael@0: *aCommandHandler = nullptr; michael@0: if (mWindow == nullptr) michael@0: { michael@0: return NS_ERROR_FAILURE; michael@0: } michael@0: michael@0: nsCOMPtr window(do_QueryInterface(mWindow)); michael@0: if (!window) michael@0: { michael@0: return NS_ERROR_FAILURE; michael@0: } michael@0: michael@0: // Get the document tree owner michael@0: michael@0: nsCOMPtr docShellAsTreeItem = michael@0: do_QueryInterface(window->GetDocShell()); michael@0: nsIDocShellTreeOwner *treeOwner = nullptr; michael@0: docShellAsTreeItem->GetTreeOwner(&treeOwner); michael@0: michael@0: // Make sure the tree owner is an an nsDocShellTreeOwner object michael@0: // by QI'ing for a hidden interface. If it doesn't have the interface michael@0: // then it's not safe to do the casting. michael@0: michael@0: nsCOMPtr realTreeOwner(do_QueryInterface(treeOwner)); michael@0: if (realTreeOwner) michael@0: { michael@0: nsDocShellTreeOwner *tree = static_cast(treeOwner); michael@0: if (tree->mTreeOwner) michael@0: { michael@0: nsresult rv; michael@0: rv = tree->mTreeOwner->QueryInterface(NS_GET_IID(nsICommandHandler), (void **)aCommandHandler); michael@0: NS_RELEASE(treeOwner); michael@0: return rv; michael@0: } michael@0: michael@0: NS_RELEASE(treeOwner); michael@0: } michael@0: michael@0: *aCommandHandler = nullptr; michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: michael@0: NS_IMPL_ADDREF(nsCommandHandler) michael@0: NS_IMPL_RELEASE(nsCommandHandler) michael@0: michael@0: NS_INTERFACE_MAP_BEGIN(nsCommandHandler) michael@0: NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsICommandHandler) michael@0: NS_INTERFACE_MAP_ENTRY(nsICommandHandlerInit) michael@0: NS_INTERFACE_MAP_ENTRY(nsICommandHandler) michael@0: NS_INTERFACE_MAP_END michael@0: michael@0: /////////////////////////////////////////////////////////////////////////////// michael@0: // nsICommandHandlerInit implementation michael@0: michael@0: /* attribute nsIDocShell docShell; */ michael@0: NS_IMETHODIMP nsCommandHandler::GetWindow(nsIDOMWindow * *aWindow) michael@0: { michael@0: *aWindow = nullptr; michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP nsCommandHandler::SetWindow(nsIDOMWindow * aWindow) michael@0: { michael@0: if (aWindow == nullptr) michael@0: { michael@0: return NS_ERROR_FAILURE; michael@0: } michael@0: mWindow = aWindow; michael@0: return NS_OK; michael@0: } michael@0: michael@0: /////////////////////////////////////////////////////////////////////////////// michael@0: // nsICommandHandler implementation michael@0: michael@0: /* string exec (in string aCommand, in string aStatus); */ michael@0: NS_IMETHODIMP nsCommandHandler::Exec(const char *aCommand, const char *aStatus, char **aResult) michael@0: { michael@0: NS_ENSURE_ARG_POINTER(aCommand); michael@0: NS_ENSURE_ARG_POINTER(aResult); michael@0: michael@0: nsCOMPtr commandHandler; michael@0: GetCommandHandler(getter_AddRefs(commandHandler)); michael@0: michael@0: // Call the client's command handler to deal with this command michael@0: if (commandHandler) michael@0: { michael@0: *aResult = nullptr; michael@0: return commandHandler->Exec(aCommand, aStatus, aResult); michael@0: } michael@0: michael@0: // Return an empty string michael@0: const char szEmpty[] = ""; michael@0: *aResult = (char *) nsMemory::Clone(szEmpty, sizeof(szEmpty)); michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: /* string query (in string aCommand, in string aStatus); */ michael@0: NS_IMETHODIMP nsCommandHandler::Query(const char *aCommand, const char *aStatus, char **aResult) michael@0: { michael@0: NS_ENSURE_ARG_POINTER(aCommand); michael@0: NS_ENSURE_ARG_POINTER(aResult); michael@0: michael@0: nsCOMPtr commandHandler; michael@0: GetCommandHandler(getter_AddRefs(commandHandler)); michael@0: michael@0: // Call the client's command handler to deal with this command michael@0: if (commandHandler) michael@0: { michael@0: *aResult = nullptr; michael@0: return commandHandler->Query(aCommand, aStatus, aResult); michael@0: } michael@0: michael@0: // Return an empty string michael@0: const char szEmpty[] = ""; michael@0: *aResult = (char *) nsMemory::Clone(szEmpty, sizeof(szEmpty)); michael@0: michael@0: return NS_OK; michael@0: }