|
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- |
|
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/. */ |
|
6 |
|
7 #include "nsCommandHandler.h" |
|
8 #include "nsWebBrowser.h" |
|
9 #include "nsDocShellTreeOwner.h" |
|
10 |
|
11 #include "nsMemory.h" |
|
12 #include "nsPIDOMWindow.h" |
|
13 |
|
14 nsCommandHandler::nsCommandHandler() : |
|
15 mWindow(nullptr) |
|
16 { |
|
17 } |
|
18 |
|
19 nsCommandHandler::~nsCommandHandler() |
|
20 { |
|
21 } |
|
22 |
|
23 nsresult nsCommandHandler::GetCommandHandler(nsICommandHandler **aCommandHandler) |
|
24 { |
|
25 NS_ENSURE_ARG_POINTER(aCommandHandler); |
|
26 |
|
27 *aCommandHandler = nullptr; |
|
28 if (mWindow == nullptr) |
|
29 { |
|
30 return NS_ERROR_FAILURE; |
|
31 } |
|
32 |
|
33 nsCOMPtr<nsPIDOMWindow> window(do_QueryInterface(mWindow)); |
|
34 if (!window) |
|
35 { |
|
36 return NS_ERROR_FAILURE; |
|
37 } |
|
38 |
|
39 // Get the document tree owner |
|
40 |
|
41 nsCOMPtr<nsIDocShellTreeItem> docShellAsTreeItem = |
|
42 do_QueryInterface(window->GetDocShell()); |
|
43 nsIDocShellTreeOwner *treeOwner = nullptr; |
|
44 docShellAsTreeItem->GetTreeOwner(&treeOwner); |
|
45 |
|
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. |
|
49 |
|
50 nsCOMPtr<nsICDocShellTreeOwner> realTreeOwner(do_QueryInterface(treeOwner)); |
|
51 if (realTreeOwner) |
|
52 { |
|
53 nsDocShellTreeOwner *tree = static_cast<nsDocShellTreeOwner *>(treeOwner); |
|
54 if (tree->mTreeOwner) |
|
55 { |
|
56 nsresult rv; |
|
57 rv = tree->mTreeOwner->QueryInterface(NS_GET_IID(nsICommandHandler), (void **)aCommandHandler); |
|
58 NS_RELEASE(treeOwner); |
|
59 return rv; |
|
60 } |
|
61 |
|
62 NS_RELEASE(treeOwner); |
|
63 } |
|
64 |
|
65 *aCommandHandler = nullptr; |
|
66 |
|
67 return NS_OK; |
|
68 } |
|
69 |
|
70 |
|
71 NS_IMPL_ADDREF(nsCommandHandler) |
|
72 NS_IMPL_RELEASE(nsCommandHandler) |
|
73 |
|
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) |
|
78 NS_INTERFACE_MAP_END |
|
79 |
|
80 /////////////////////////////////////////////////////////////////////////////// |
|
81 // nsICommandHandlerInit implementation |
|
82 |
|
83 /* attribute nsIDocShell docShell; */ |
|
84 NS_IMETHODIMP nsCommandHandler::GetWindow(nsIDOMWindow * *aWindow) |
|
85 { |
|
86 *aWindow = nullptr; |
|
87 return NS_OK; |
|
88 } |
|
89 |
|
90 NS_IMETHODIMP nsCommandHandler::SetWindow(nsIDOMWindow * aWindow) |
|
91 { |
|
92 if (aWindow == nullptr) |
|
93 { |
|
94 return NS_ERROR_FAILURE; |
|
95 } |
|
96 mWindow = aWindow; |
|
97 return NS_OK; |
|
98 } |
|
99 |
|
100 /////////////////////////////////////////////////////////////////////////////// |
|
101 // nsICommandHandler implementation |
|
102 |
|
103 /* string exec (in string aCommand, in string aStatus); */ |
|
104 NS_IMETHODIMP nsCommandHandler::Exec(const char *aCommand, const char *aStatus, char **aResult) |
|
105 { |
|
106 NS_ENSURE_ARG_POINTER(aCommand); |
|
107 NS_ENSURE_ARG_POINTER(aResult); |
|
108 |
|
109 nsCOMPtr<nsICommandHandler> commandHandler; |
|
110 GetCommandHandler(getter_AddRefs(commandHandler)); |
|
111 |
|
112 // Call the client's command handler to deal with this command |
|
113 if (commandHandler) |
|
114 { |
|
115 *aResult = nullptr; |
|
116 return commandHandler->Exec(aCommand, aStatus, aResult); |
|
117 } |
|
118 |
|
119 // Return an empty string |
|
120 const char szEmpty[] = ""; |
|
121 *aResult = (char *) nsMemory::Clone(szEmpty, sizeof(szEmpty)); |
|
122 |
|
123 return NS_OK; |
|
124 } |
|
125 |
|
126 /* string query (in string aCommand, in string aStatus); */ |
|
127 NS_IMETHODIMP nsCommandHandler::Query(const char *aCommand, const char *aStatus, char **aResult) |
|
128 { |
|
129 NS_ENSURE_ARG_POINTER(aCommand); |
|
130 NS_ENSURE_ARG_POINTER(aResult); |
|
131 |
|
132 nsCOMPtr<nsICommandHandler> commandHandler; |
|
133 GetCommandHandler(getter_AddRefs(commandHandler)); |
|
134 |
|
135 // Call the client's command handler to deal with this command |
|
136 if (commandHandler) |
|
137 { |
|
138 *aResult = nullptr; |
|
139 return commandHandler->Query(aCommand, aStatus, aResult); |
|
140 } |
|
141 |
|
142 // Return an empty string |
|
143 const char szEmpty[] = ""; |
|
144 *aResult = (char *) nsMemory::Clone(szEmpty, sizeof(szEmpty)); |
|
145 |
|
146 return NS_OK; |
|
147 } |