1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "nsCommandHandler.h"
8 #include "nsWebBrowser.h"
9 #include "nsDocShellTreeOwner.h"
12 #include "nsPIDOMWindow.h"
14 nsCommandHandler::nsCommandHandler() :
19 nsCommandHandler::~nsCommandHandler()
23 nsresult
nsCommandHandler::GetCommandHandler(nsICommandHandler
**aCommandHandler
)
25 NS_ENSURE_ARG_POINTER(aCommandHandler
);
27 *aCommandHandler
= nullptr;
28 if (mWindow
== nullptr)
30 return NS_ERROR_FAILURE
;
33 nsCOMPtr
<nsPIDOMWindow
> window(do_QueryInterface(mWindow
));
36 return NS_ERROR_FAILURE
;
39 // Get the document tree owner
41 nsCOMPtr
<nsIDocShellTreeItem
> docShellAsTreeItem
=
42 do_QueryInterface(window
->GetDocShell());
43 nsIDocShellTreeOwner
*treeOwner
= nullptr;
44 docShellAsTreeItem
->GetTreeOwner(&treeOwner
);
46 // Make sure the tree owner is an an nsDocShellTreeOwner object
47 // by QI'ing for a hidden interface. If it doesn't have the interface
48 // then it's not safe to do the casting.
50 nsCOMPtr
<nsICDocShellTreeOwner
> realTreeOwner(do_QueryInterface(treeOwner
));
53 nsDocShellTreeOwner
*tree
= static_cast<nsDocShellTreeOwner
*>(treeOwner
);
57 rv
= tree
->mTreeOwner
->QueryInterface(NS_GET_IID(nsICommandHandler
), (void **)aCommandHandler
);
58 NS_RELEASE(treeOwner
);
62 NS_RELEASE(treeOwner
);
65 *aCommandHandler
= nullptr;
71 NS_IMPL_ADDREF(nsCommandHandler
)
72 NS_IMPL_RELEASE(nsCommandHandler
)
74 NS_INTERFACE_MAP_BEGIN(nsCommandHandler
)
75 NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports
, nsICommandHandler
)
76 NS_INTERFACE_MAP_ENTRY(nsICommandHandlerInit
)
77 NS_INTERFACE_MAP_ENTRY(nsICommandHandler
)
80 ///////////////////////////////////////////////////////////////////////////////
81 // nsICommandHandlerInit implementation
83 /* attribute nsIDocShell docShell; */
84 NS_IMETHODIMP
nsCommandHandler::GetWindow(nsIDOMWindow
* *aWindow
)
90 NS_IMETHODIMP
nsCommandHandler::SetWindow(nsIDOMWindow
* aWindow
)
92 if (aWindow
== nullptr)
94 return NS_ERROR_FAILURE
;
100 ///////////////////////////////////////////////////////////////////////////////
101 // nsICommandHandler implementation
103 /* string exec (in string aCommand, in string aStatus); */
104 NS_IMETHODIMP
nsCommandHandler::Exec(const char *aCommand
, const char *aStatus
, char **aResult
)
106 NS_ENSURE_ARG_POINTER(aCommand
);
107 NS_ENSURE_ARG_POINTER(aResult
);
109 nsCOMPtr
<nsICommandHandler
> commandHandler
;
110 GetCommandHandler(getter_AddRefs(commandHandler
));
112 // Call the client's command handler to deal with this command
116 return commandHandler
->Exec(aCommand
, aStatus
, aResult
);
119 // Return an empty string
120 const char szEmpty
[] = "";
121 *aResult
= (char *) nsMemory::Clone(szEmpty
, sizeof(szEmpty
));
126 /* string query (in string aCommand, in string aStatus); */
127 NS_IMETHODIMP
nsCommandHandler::Query(const char *aCommand
, const char *aStatus
, char **aResult
)
129 NS_ENSURE_ARG_POINTER(aCommand
);
130 NS_ENSURE_ARG_POINTER(aResult
);
132 nsCOMPtr
<nsICommandHandler
> commandHandler
;
133 GetCommandHandler(getter_AddRefs(commandHandler
));
135 // Call the client's command handler to deal with this command
139 return commandHandler
->Query(aCommand
, aStatus
, aResult
);
142 // Return an empty string
143 const char szEmpty
[] = "";
144 *aResult
= (char *) nsMemory::Clone(szEmpty
, sizeof(szEmpty
));