/src/mozilla-central/dom/commandhandler/nsControllerCommandTable.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
2 | | /* vim: set ts=8 sts=2 et sw=2 tw=80: */ |
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 "nsString.h" |
8 | | #include "nsIControllerCommand.h" |
9 | | #include "nsControllerCommandTable.h" |
10 | | #include "nsGlobalWindowCommands.h" |
11 | | #include "mozilla/EditorController.h" |
12 | | #include "mozilla/HTMLEditorController.h" |
13 | | |
14 | | nsresult NS_NewControllerCommandTable(nsIControllerCommandTable** aResult); |
15 | | |
16 | | // this value is used to size the hash table. Just a sensible upper bound |
17 | | #define NUM_COMMANDS_LENGTH 32 |
18 | | |
19 | | nsControllerCommandTable::nsControllerCommandTable() |
20 | | : mCommandsTable(NUM_COMMANDS_LENGTH) |
21 | | , mMutable(true) |
22 | 0 | { |
23 | 0 | } |
24 | | |
25 | | nsControllerCommandTable::~nsControllerCommandTable() |
26 | 0 | { |
27 | 0 | } |
28 | | |
29 | | NS_IMPL_ISUPPORTS(nsControllerCommandTable, nsIControllerCommandTable, |
30 | | nsISupportsWeakReference) |
31 | | |
32 | | NS_IMETHODIMP |
33 | | nsControllerCommandTable::MakeImmutable(void) |
34 | 0 | { |
35 | 0 | mMutable = false; |
36 | 0 | return NS_OK; |
37 | 0 | } |
38 | | |
39 | | NS_IMETHODIMP |
40 | | nsControllerCommandTable::RegisterCommand(const char* aCommandName, |
41 | | nsIControllerCommand* aCommand) |
42 | 0 | { |
43 | 0 | NS_ENSURE_TRUE(mMutable, NS_ERROR_FAILURE); |
44 | 0 |
|
45 | 0 | mCommandsTable.Put(nsDependentCString(aCommandName), aCommand); |
46 | 0 |
|
47 | 0 | return NS_OK; |
48 | 0 | } |
49 | | |
50 | | NS_IMETHODIMP |
51 | | nsControllerCommandTable::UnregisterCommand(const char* aCommandName, |
52 | | nsIControllerCommand* aCommand) |
53 | 0 | { |
54 | 0 | NS_ENSURE_TRUE(mMutable, NS_ERROR_FAILURE); |
55 | 0 |
|
56 | 0 | nsDependentCString commandKey(aCommandName); |
57 | 0 | if (!mCommandsTable.Get(commandKey, nullptr)) { |
58 | 0 | return NS_ERROR_FAILURE; |
59 | 0 | } |
60 | 0 | |
61 | 0 | mCommandsTable.Remove(commandKey); |
62 | 0 | return NS_OK; |
63 | 0 | } |
64 | | |
65 | | NS_IMETHODIMP |
66 | | nsControllerCommandTable::FindCommandHandler(const char* aCommandName, |
67 | | nsIControllerCommand** aResult) |
68 | 0 | { |
69 | 0 | NS_ENSURE_ARG_POINTER(aResult); |
70 | 0 |
|
71 | 0 | *aResult = nullptr; |
72 | 0 |
|
73 | 0 | nsCOMPtr<nsIControllerCommand> foundCommand; |
74 | 0 | mCommandsTable.Get(nsDependentCString(aCommandName), |
75 | 0 | getter_AddRefs(foundCommand)); |
76 | 0 | if (!foundCommand) { |
77 | 0 | return NS_ERROR_FAILURE; |
78 | 0 | } |
79 | 0 | |
80 | 0 | foundCommand.forget(aResult); |
81 | 0 | return NS_OK; |
82 | 0 | } |
83 | | |
84 | | NS_IMETHODIMP |
85 | | nsControllerCommandTable::IsCommandEnabled(const char* aCommandName, |
86 | | nsISupports* aCommandRefCon, |
87 | | bool* aResult) |
88 | 0 | { |
89 | 0 | NS_ENSURE_ARG_POINTER(aResult); |
90 | 0 |
|
91 | 0 | *aResult = false; |
92 | 0 |
|
93 | 0 | nsCOMPtr<nsIControllerCommand> commandHandler; |
94 | 0 | FindCommandHandler(aCommandName, getter_AddRefs(commandHandler)); |
95 | 0 | if (!commandHandler) { |
96 | 0 | NS_WARNING("Controller command table asked about a command that it does " |
97 | 0 | "not handle"); |
98 | 0 | return NS_OK; |
99 | 0 | } |
100 | 0 |
|
101 | 0 | return commandHandler->IsCommandEnabled(aCommandName, aCommandRefCon, |
102 | 0 | aResult); |
103 | 0 | } |
104 | | |
105 | | NS_IMETHODIMP |
106 | | nsControllerCommandTable::UpdateCommandState(const char* aCommandName, |
107 | | nsISupports* aCommandRefCon) |
108 | 0 | { |
109 | 0 | nsCOMPtr<nsIControllerCommand> commandHandler; |
110 | 0 | FindCommandHandler(aCommandName, getter_AddRefs(commandHandler)); |
111 | 0 | if (!commandHandler) { |
112 | 0 | NS_WARNING("Controller command table asked to update the state of a " |
113 | 0 | "command that it does not handle"); |
114 | 0 | return NS_OK; |
115 | 0 | } |
116 | 0 |
|
117 | 0 | return NS_ERROR_NOT_IMPLEMENTED; |
118 | 0 | } |
119 | | |
120 | | NS_IMETHODIMP |
121 | | nsControllerCommandTable::SupportsCommand(const char* aCommandName, |
122 | | nsISupports* aCommandRefCon, |
123 | | bool* aResult) |
124 | 0 | { |
125 | 0 | NS_ENSURE_ARG_POINTER(aResult); |
126 | 0 |
|
127 | 0 | // XXX: need to check the readonly and disabled states |
128 | 0 |
|
129 | 0 | *aResult = false; |
130 | 0 |
|
131 | 0 | nsCOMPtr<nsIControllerCommand> commandHandler; |
132 | 0 | FindCommandHandler(aCommandName, getter_AddRefs(commandHandler)); |
133 | 0 |
|
134 | 0 | *aResult = (commandHandler.get() != nullptr); |
135 | 0 | return NS_OK; |
136 | 0 | } |
137 | | |
138 | | NS_IMETHODIMP |
139 | | nsControllerCommandTable::DoCommand(const char* aCommandName, |
140 | | nsISupports* aCommandRefCon) |
141 | 0 | { |
142 | 0 | nsCOMPtr<nsIControllerCommand> commandHandler; |
143 | 0 | FindCommandHandler(aCommandName, getter_AddRefs(commandHandler)); |
144 | 0 | if (!commandHandler) { |
145 | 0 | NS_WARNING("Controller command table asked to do a command that it does " |
146 | 0 | "not handle"); |
147 | 0 | return NS_OK; |
148 | 0 | } |
149 | 0 |
|
150 | 0 | return commandHandler->DoCommand(aCommandName, aCommandRefCon); |
151 | 0 | } |
152 | | |
153 | | NS_IMETHODIMP |
154 | | nsControllerCommandTable::DoCommandParams(const char* aCommandName, |
155 | | nsICommandParams* aParams, |
156 | | nsISupports* aCommandRefCon) |
157 | 0 | { |
158 | 0 | nsCOMPtr<nsIControllerCommand> commandHandler; |
159 | 0 | FindCommandHandler(aCommandName, getter_AddRefs(commandHandler)); |
160 | 0 | if (!commandHandler) { |
161 | 0 | NS_WARNING("Controller command table asked to do a command that it does " |
162 | 0 | "not handle"); |
163 | 0 | return NS_OK; |
164 | 0 | } |
165 | 0 | return commandHandler->DoCommandParams(aCommandName, aParams, aCommandRefCon); |
166 | 0 | } |
167 | | |
168 | | NS_IMETHODIMP |
169 | | nsControllerCommandTable::GetCommandState(const char* aCommandName, |
170 | | nsICommandParams* aParams, |
171 | | nsISupports* aCommandRefCon) |
172 | 0 | { |
173 | 0 | nsCOMPtr<nsIControllerCommand> commandHandler; |
174 | 0 | FindCommandHandler(aCommandName, getter_AddRefs(commandHandler)); |
175 | 0 | if (!commandHandler) { |
176 | 0 | NS_WARNING("Controller command table asked to do a command that it does " |
177 | 0 | "not handle"); |
178 | 0 | return NS_OK; |
179 | 0 | } |
180 | 0 | return commandHandler->GetCommandStateParams(aCommandName, aParams, |
181 | 0 | aCommandRefCon); |
182 | 0 | } |
183 | | |
184 | | NS_IMETHODIMP |
185 | | nsControllerCommandTable::GetSupportedCommands(uint32_t* aCount, |
186 | | char*** aCommands) |
187 | 0 | { |
188 | 0 | char** commands = |
189 | 0 | static_cast<char**>(moz_xmalloc(sizeof(char*) * mCommandsTable.Count())); |
190 | 0 | *aCount = mCommandsTable.Count(); |
191 | 0 | *aCommands = commands; |
192 | 0 |
|
193 | 0 | for (auto iter = mCommandsTable.Iter(); !iter.Done(); iter.Next()) { |
194 | 0 | *commands = ToNewCString(iter.Key()); |
195 | 0 | commands++; |
196 | 0 | } |
197 | 0 | return NS_OK; |
198 | 0 | } |
199 | | |
200 | | typedef nsresult (*CommandTableRegistrar)(nsIControllerCommandTable*); |
201 | | |
202 | | static already_AddRefed<nsIControllerCommandTable> |
203 | | CreateCommandTableWithCommands(CommandTableRegistrar aRegistrar) |
204 | 0 | { |
205 | 0 | nsCOMPtr<nsIControllerCommandTable> commandTable = |
206 | 0 | new nsControllerCommandTable(); |
207 | 0 |
|
208 | 0 | nsresult rv = aRegistrar(commandTable); |
209 | 0 | if (NS_FAILED(rv)) return nullptr; |
210 | 0 | |
211 | 0 | // we don't know here whether we're being created as an instance, |
212 | 0 | // or a service, so we can't become immutable |
213 | 0 | |
214 | 0 | return commandTable.forget(); |
215 | 0 | } |
216 | | |
217 | | // static |
218 | | already_AddRefed<nsIControllerCommandTable> |
219 | | nsControllerCommandTable::CreateEditorCommandTable() |
220 | 0 | { |
221 | 0 | return CreateCommandTableWithCommands( |
222 | 0 | EditorController::RegisterEditorCommands); |
223 | 0 | } |
224 | | |
225 | | // static |
226 | | already_AddRefed<nsIControllerCommandTable> |
227 | | nsControllerCommandTable::CreateEditingCommandTable() |
228 | 0 | { |
229 | 0 | return CreateCommandTableWithCommands( |
230 | 0 | EditorController::RegisterEditingCommands); |
231 | 0 | } |
232 | | |
233 | | // static |
234 | | already_AddRefed<nsIControllerCommandTable> |
235 | | nsControllerCommandTable::CreateHTMLEditorCommandTable() |
236 | 0 | { |
237 | 0 | return CreateCommandTableWithCommands( |
238 | 0 | HTMLEditorController::RegisterHTMLEditorCommands); |
239 | 0 | } |
240 | | |
241 | | // static |
242 | | already_AddRefed<nsIControllerCommandTable> |
243 | | nsControllerCommandTable::CreateHTMLEditorDocStateCommandTable() |
244 | 0 | { |
245 | 0 | return CreateCommandTableWithCommands( |
246 | 0 | HTMLEditorController::RegisterEditorDocStateCommands); |
247 | 0 | } |
248 | | |
249 | | // static |
250 | | already_AddRefed<nsIControllerCommandTable> |
251 | | nsControllerCommandTable::CreateWindowCommandTable() |
252 | 0 | { |
253 | 0 | return CreateCommandTableWithCommands( |
254 | 0 | nsWindowCommandRegistration::RegisterWindowCommands); |
255 | 0 | } |
256 | | |
257 | | nsresult |
258 | | NS_NewControllerCommandTable(nsIControllerCommandTable** aResult) |
259 | 0 | { |
260 | 0 | MOZ_ASSERT(aResult != nullptr, "null ptr"); |
261 | 0 | if (!aResult) { |
262 | 0 | return NS_ERROR_NULL_POINTER; |
263 | 0 | } |
264 | 0 | |
265 | 0 | nsControllerCommandTable* newCommandTable = new nsControllerCommandTable(); |
266 | 0 | NS_ADDREF(newCommandTable); |
267 | 0 | *aResult = newCommandTable; |
268 | 0 | return NS_OK; |
269 | 0 | } |