Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
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/. */
6 #include "nsString.h"
7 #include "nsIControllerCommand.h"
8 #include "nsControllerCommandTable.h"
10 // prototype;
11 nsresult
12 NS_NewControllerCommandTable(nsIControllerCommandTable** aResult);
15 // this value is used to size the hash table. Just a sensible upper bound
16 #define NUM_COMMANDS_BOUNDS 64
19 nsControllerCommandTable::nsControllerCommandTable()
20 : mCommandsTable(NUM_COMMANDS_BOUNDS)
21 , mMutable(true)
22 {
23 }
26 nsControllerCommandTable::~nsControllerCommandTable()
27 {
28 }
30 NS_IMPL_ISUPPORTS(nsControllerCommandTable, nsIControllerCommandTable, nsISupportsWeakReference)
32 NS_IMETHODIMP
33 nsControllerCommandTable::MakeImmutable(void)
34 {
35 mMutable = false;
36 return NS_OK;
37 }
39 NS_IMETHODIMP
40 nsControllerCommandTable::RegisterCommand(const char * aCommandName, nsIControllerCommand *aCommand)
41 {
42 NS_ENSURE_TRUE(mMutable, NS_ERROR_FAILURE);
44 mCommandsTable.Put(nsDependentCString(aCommandName), aCommand);
46 return NS_OK;
47 }
50 NS_IMETHODIMP
51 nsControllerCommandTable::UnregisterCommand(const char * aCommandName, nsIControllerCommand *aCommand)
52 {
53 NS_ENSURE_TRUE(mMutable, NS_ERROR_FAILURE);
55 nsDependentCString commandKey(aCommandName);
57 if (!mCommandsTable.Get(commandKey, nullptr)) {
58 return NS_ERROR_FAILURE;
59 }
61 mCommandsTable.Remove(commandKey);
62 return NS_OK;
63 }
66 NS_IMETHODIMP
67 nsControllerCommandTable::FindCommandHandler(const char * aCommandName, nsIControllerCommand **outCommand)
68 {
69 NS_ENSURE_ARG_POINTER(outCommand);
71 *outCommand = nullptr;
73 nsCOMPtr<nsIControllerCommand> foundCommand;
74 mCommandsTable.Get(nsDependentCString(aCommandName), getter_AddRefs(foundCommand));
75 if (!foundCommand) return NS_ERROR_FAILURE;
77 foundCommand.forget(outCommand);
78 return NS_OK;
79 }
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);
89 *aResult = false;
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 }
102 return commandHandler->IsCommandEnabled(aCommandName, aCommandRefCon, aResult);
103 }
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 }
120 return NS_ERROR_NOT_IMPLEMENTED;
121 }
123 NS_IMETHODIMP
124 nsControllerCommandTable::SupportsCommand(const char * aCommandName, nsISupports *aCommandRefCon, bool *aResult)
125 {
126 NS_ENSURE_ARG_POINTER(aResult);
128 // XXX: need to check the readonly and disabled states
130 *aResult = false;
132 // find the command
133 nsCOMPtr<nsIControllerCommand> commandHandler;
134 FindCommandHandler(aCommandName, getter_AddRefs(commandHandler));
136 *aResult = (commandHandler.get() != nullptr);
137 return NS_OK;
138 }
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 }
155 return commandHandler->DoCommand(aCommandName, aCommandRefCon);
156 }
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 }
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 }
192 nsresult
193 NS_NewControllerCommandTable(nsIControllerCommandTable** aResult)
194 {
195 NS_PRECONDITION(aResult != nullptr, "null ptr");
196 if (! aResult)
197 return NS_ERROR_NULL_POINTER;
199 nsControllerCommandTable* newCommandTable = new nsControllerCommandTable();
200 if (! newCommandTable)
201 return NS_ERROR_OUT_OF_MEMORY;
203 NS_ADDREF(newCommandTable);
204 *aResult = newCommandTable;
205 return NS_OK;
206 }