|
1 /* -*- Mode: C++; tab-width: 2; 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 |
|
6 #include "nsString.h" |
|
7 #include "nsIControllerCommand.h" |
|
8 #include "nsControllerCommandTable.h" |
|
9 |
|
10 // prototype; |
|
11 nsresult |
|
12 NS_NewControllerCommandTable(nsIControllerCommandTable** aResult); |
|
13 |
|
14 |
|
15 // this value is used to size the hash table. Just a sensible upper bound |
|
16 #define NUM_COMMANDS_BOUNDS 64 |
|
17 |
|
18 |
|
19 nsControllerCommandTable::nsControllerCommandTable() |
|
20 : mCommandsTable(NUM_COMMANDS_BOUNDS) |
|
21 , mMutable(true) |
|
22 { |
|
23 } |
|
24 |
|
25 |
|
26 nsControllerCommandTable::~nsControllerCommandTable() |
|
27 { |
|
28 } |
|
29 |
|
30 NS_IMPL_ISUPPORTS(nsControllerCommandTable, nsIControllerCommandTable, nsISupportsWeakReference) |
|
31 |
|
32 NS_IMETHODIMP |
|
33 nsControllerCommandTable::MakeImmutable(void) |
|
34 { |
|
35 mMutable = false; |
|
36 return NS_OK; |
|
37 } |
|
38 |
|
39 NS_IMETHODIMP |
|
40 nsControllerCommandTable::RegisterCommand(const char * aCommandName, nsIControllerCommand *aCommand) |
|
41 { |
|
42 NS_ENSURE_TRUE(mMutable, NS_ERROR_FAILURE); |
|
43 |
|
44 mCommandsTable.Put(nsDependentCString(aCommandName), aCommand); |
|
45 |
|
46 return NS_OK; |
|
47 } |
|
48 |
|
49 |
|
50 NS_IMETHODIMP |
|
51 nsControllerCommandTable::UnregisterCommand(const char * aCommandName, nsIControllerCommand *aCommand) |
|
52 { |
|
53 NS_ENSURE_TRUE(mMutable, NS_ERROR_FAILURE); |
|
54 |
|
55 nsDependentCString commandKey(aCommandName); |
|
56 |
|
57 if (!mCommandsTable.Get(commandKey, nullptr)) { |
|
58 return NS_ERROR_FAILURE; |
|
59 } |
|
60 |
|
61 mCommandsTable.Remove(commandKey); |
|
62 return NS_OK; |
|
63 } |
|
64 |
|
65 |
|
66 NS_IMETHODIMP |
|
67 nsControllerCommandTable::FindCommandHandler(const char * aCommandName, nsIControllerCommand **outCommand) |
|
68 { |
|
69 NS_ENSURE_ARG_POINTER(outCommand); |
|
70 |
|
71 *outCommand = nullptr; |
|
72 |
|
73 nsCOMPtr<nsIControllerCommand> foundCommand; |
|
74 mCommandsTable.Get(nsDependentCString(aCommandName), getter_AddRefs(foundCommand)); |
|
75 if (!foundCommand) return NS_ERROR_FAILURE; |
|
76 |
|
77 foundCommand.forget(outCommand); |
|
78 return NS_OK; |
|
79 } |
|
80 |
|
81 |
|
82 |
|
83 /* boolean isCommandEnabled (in wstring command); */ |
|
84 NS_IMETHODIMP |
|
85 nsControllerCommandTable::IsCommandEnabled(const char * aCommandName, nsISupports *aCommandRefCon, bool *aResult) |
|
86 { |
|
87 NS_ENSURE_ARG_POINTER(aResult); |
|
88 |
|
89 *aResult = false; |
|
90 |
|
91 // find the command |
|
92 nsCOMPtr<nsIControllerCommand> commandHandler; |
|
93 FindCommandHandler(aCommandName, getter_AddRefs(commandHandler)); |
|
94 if (!commandHandler) |
|
95 { |
|
96 #if DEBUG |
|
97 NS_WARNING("Controller command table asked about a command that it does not handle -- "); |
|
98 #endif |
|
99 return NS_OK; // we don't handle this command |
|
100 } |
|
101 |
|
102 return commandHandler->IsCommandEnabled(aCommandName, aCommandRefCon, aResult); |
|
103 } |
|
104 |
|
105 |
|
106 NS_IMETHODIMP |
|
107 nsControllerCommandTable::UpdateCommandState(const char * aCommandName, nsISupports *aCommandRefCon) |
|
108 { |
|
109 // find the command |
|
110 nsCOMPtr<nsIControllerCommand> commandHandler; |
|
111 FindCommandHandler(aCommandName, getter_AddRefs(commandHandler)); |
|
112 if (!commandHandler) |
|
113 { |
|
114 #if DEBUG |
|
115 NS_WARNING("Controller command table asked to update the state of a command that it does not handle -- "); |
|
116 #endif |
|
117 return NS_OK; // we don't handle this command |
|
118 } |
|
119 |
|
120 return NS_ERROR_NOT_IMPLEMENTED; |
|
121 } |
|
122 |
|
123 NS_IMETHODIMP |
|
124 nsControllerCommandTable::SupportsCommand(const char * aCommandName, nsISupports *aCommandRefCon, bool *aResult) |
|
125 { |
|
126 NS_ENSURE_ARG_POINTER(aResult); |
|
127 |
|
128 // XXX: need to check the readonly and disabled states |
|
129 |
|
130 *aResult = false; |
|
131 |
|
132 // find the command |
|
133 nsCOMPtr<nsIControllerCommand> commandHandler; |
|
134 FindCommandHandler(aCommandName, getter_AddRefs(commandHandler)); |
|
135 |
|
136 *aResult = (commandHandler.get() != nullptr); |
|
137 return NS_OK; |
|
138 } |
|
139 |
|
140 /* void doCommand (in wstring command); */ |
|
141 NS_IMETHODIMP |
|
142 nsControllerCommandTable::DoCommand(const char * aCommandName, nsISupports *aCommandRefCon) |
|
143 { |
|
144 // find the command |
|
145 nsCOMPtr<nsIControllerCommand> commandHandler; |
|
146 FindCommandHandler(aCommandName, getter_AddRefs(commandHandler)); |
|
147 if (!commandHandler) |
|
148 { |
|
149 #if DEBUG |
|
150 NS_WARNING("Controller command table asked to do a command that it does not handle -- "); |
|
151 #endif |
|
152 return NS_OK; // we don't handle this command |
|
153 } |
|
154 |
|
155 return commandHandler->DoCommand(aCommandName, aCommandRefCon); |
|
156 } |
|
157 |
|
158 NS_IMETHODIMP |
|
159 nsControllerCommandTable::DoCommandParams(const char *aCommandName, nsICommandParams *aParams, nsISupports *aCommandRefCon) |
|
160 { |
|
161 // find the command |
|
162 nsCOMPtr<nsIControllerCommand> commandHandler; |
|
163 FindCommandHandler(aCommandName, getter_AddRefs(commandHandler)); |
|
164 if (!commandHandler) |
|
165 { |
|
166 #if DEBUG |
|
167 NS_WARNING("Controller command table asked to do a command that it does not handle -- "); |
|
168 #endif |
|
169 return NS_OK; // we don't handle this command |
|
170 } |
|
171 return commandHandler->DoCommandParams(aCommandName, aParams, aCommandRefCon); |
|
172 } |
|
173 |
|
174 |
|
175 NS_IMETHODIMP |
|
176 nsControllerCommandTable::GetCommandState(const char *aCommandName, nsICommandParams *aParams, nsISupports *aCommandRefCon) |
|
177 { |
|
178 // find the command |
|
179 nsCOMPtr<nsIControllerCommand> commandHandler; |
|
180 FindCommandHandler(aCommandName, getter_AddRefs(commandHandler)); |
|
181 if (!commandHandler) |
|
182 { |
|
183 #if DEBUG |
|
184 NS_WARNING("Controller command table asked to do a command that it does not handle -- "); |
|
185 #endif |
|
186 return NS_OK; // we don't handle this command |
|
187 } |
|
188 return commandHandler->GetCommandStateParams(aCommandName, aParams, aCommandRefCon); |
|
189 } |
|
190 |
|
191 |
|
192 nsresult |
|
193 NS_NewControllerCommandTable(nsIControllerCommandTable** aResult) |
|
194 { |
|
195 NS_PRECONDITION(aResult != nullptr, "null ptr"); |
|
196 if (! aResult) |
|
197 return NS_ERROR_NULL_POINTER; |
|
198 |
|
199 nsControllerCommandTable* newCommandTable = new nsControllerCommandTable(); |
|
200 if (! newCommandTable) |
|
201 return NS_ERROR_OUT_OF_MEMORY; |
|
202 |
|
203 NS_ADDREF(newCommandTable); |
|
204 *aResult = newCommandTable; |
|
205 return NS_OK; |
|
206 } |