1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/embedding/components/commandhandler/src/nsControllerCommandTable.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,206 @@ 1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +#include "nsString.h" 1.10 +#include "nsIControllerCommand.h" 1.11 +#include "nsControllerCommandTable.h" 1.12 + 1.13 +// prototype; 1.14 +nsresult 1.15 +NS_NewControllerCommandTable(nsIControllerCommandTable** aResult); 1.16 + 1.17 + 1.18 +// this value is used to size the hash table. Just a sensible upper bound 1.19 +#define NUM_COMMANDS_BOUNDS 64 1.20 + 1.21 + 1.22 +nsControllerCommandTable::nsControllerCommandTable() 1.23 +: mCommandsTable(NUM_COMMANDS_BOUNDS) 1.24 +, mMutable(true) 1.25 +{ 1.26 +} 1.27 + 1.28 + 1.29 +nsControllerCommandTable::~nsControllerCommandTable() 1.30 +{ 1.31 +} 1.32 + 1.33 +NS_IMPL_ISUPPORTS(nsControllerCommandTable, nsIControllerCommandTable, nsISupportsWeakReference) 1.34 + 1.35 +NS_IMETHODIMP 1.36 +nsControllerCommandTable::MakeImmutable(void) 1.37 +{ 1.38 + mMutable = false; 1.39 + return NS_OK; 1.40 +} 1.41 + 1.42 +NS_IMETHODIMP 1.43 +nsControllerCommandTable::RegisterCommand(const char * aCommandName, nsIControllerCommand *aCommand) 1.44 +{ 1.45 + NS_ENSURE_TRUE(mMutable, NS_ERROR_FAILURE); 1.46 + 1.47 + mCommandsTable.Put(nsDependentCString(aCommandName), aCommand); 1.48 + 1.49 + return NS_OK; 1.50 +} 1.51 + 1.52 + 1.53 +NS_IMETHODIMP 1.54 +nsControllerCommandTable::UnregisterCommand(const char * aCommandName, nsIControllerCommand *aCommand) 1.55 +{ 1.56 + NS_ENSURE_TRUE(mMutable, NS_ERROR_FAILURE); 1.57 + 1.58 + nsDependentCString commandKey(aCommandName); 1.59 + 1.60 + if (!mCommandsTable.Get(commandKey, nullptr)) { 1.61 + return NS_ERROR_FAILURE; 1.62 + } 1.63 + 1.64 + mCommandsTable.Remove(commandKey); 1.65 + return NS_OK; 1.66 +} 1.67 + 1.68 + 1.69 +NS_IMETHODIMP 1.70 +nsControllerCommandTable::FindCommandHandler(const char * aCommandName, nsIControllerCommand **outCommand) 1.71 +{ 1.72 + NS_ENSURE_ARG_POINTER(outCommand); 1.73 + 1.74 + *outCommand = nullptr; 1.75 + 1.76 + nsCOMPtr<nsIControllerCommand> foundCommand; 1.77 + mCommandsTable.Get(nsDependentCString(aCommandName), getter_AddRefs(foundCommand)); 1.78 + if (!foundCommand) return NS_ERROR_FAILURE; 1.79 + 1.80 + foundCommand.forget(outCommand); 1.81 + return NS_OK; 1.82 +} 1.83 + 1.84 + 1.85 + 1.86 +/* boolean isCommandEnabled (in wstring command); */ 1.87 +NS_IMETHODIMP 1.88 +nsControllerCommandTable::IsCommandEnabled(const char * aCommandName, nsISupports *aCommandRefCon, bool *aResult) 1.89 +{ 1.90 + NS_ENSURE_ARG_POINTER(aResult); 1.91 + 1.92 + *aResult = false; 1.93 + 1.94 + // find the command 1.95 + nsCOMPtr<nsIControllerCommand> commandHandler; 1.96 + FindCommandHandler(aCommandName, getter_AddRefs(commandHandler)); 1.97 + if (!commandHandler) 1.98 + { 1.99 +#if DEBUG 1.100 + NS_WARNING("Controller command table asked about a command that it does not handle -- "); 1.101 +#endif 1.102 + return NS_OK; // we don't handle this command 1.103 + } 1.104 + 1.105 + return commandHandler->IsCommandEnabled(aCommandName, aCommandRefCon, aResult); 1.106 +} 1.107 + 1.108 + 1.109 +NS_IMETHODIMP 1.110 +nsControllerCommandTable::UpdateCommandState(const char * aCommandName, nsISupports *aCommandRefCon) 1.111 +{ 1.112 + // find the command 1.113 + nsCOMPtr<nsIControllerCommand> commandHandler; 1.114 + FindCommandHandler(aCommandName, getter_AddRefs(commandHandler)); 1.115 + if (!commandHandler) 1.116 + { 1.117 +#if DEBUG 1.118 + NS_WARNING("Controller command table asked to update the state of a command that it does not handle -- "); 1.119 +#endif 1.120 + return NS_OK; // we don't handle this command 1.121 + } 1.122 + 1.123 + return NS_ERROR_NOT_IMPLEMENTED; 1.124 +} 1.125 + 1.126 +NS_IMETHODIMP 1.127 +nsControllerCommandTable::SupportsCommand(const char * aCommandName, nsISupports *aCommandRefCon, bool *aResult) 1.128 +{ 1.129 + NS_ENSURE_ARG_POINTER(aResult); 1.130 + 1.131 + // XXX: need to check the readonly and disabled states 1.132 + 1.133 + *aResult = false; 1.134 + 1.135 + // find the command 1.136 + nsCOMPtr<nsIControllerCommand> commandHandler; 1.137 + FindCommandHandler(aCommandName, getter_AddRefs(commandHandler)); 1.138 + 1.139 + *aResult = (commandHandler.get() != nullptr); 1.140 + return NS_OK; 1.141 +} 1.142 + 1.143 +/* void doCommand (in wstring command); */ 1.144 +NS_IMETHODIMP 1.145 +nsControllerCommandTable::DoCommand(const char * aCommandName, nsISupports *aCommandRefCon) 1.146 +{ 1.147 + // find the command 1.148 + nsCOMPtr<nsIControllerCommand> commandHandler; 1.149 + FindCommandHandler(aCommandName, getter_AddRefs(commandHandler)); 1.150 + if (!commandHandler) 1.151 + { 1.152 +#if DEBUG 1.153 + NS_WARNING("Controller command table asked to do a command that it does not handle -- "); 1.154 +#endif 1.155 + return NS_OK; // we don't handle this command 1.156 + } 1.157 + 1.158 + return commandHandler->DoCommand(aCommandName, aCommandRefCon); 1.159 +} 1.160 + 1.161 +NS_IMETHODIMP 1.162 +nsControllerCommandTable::DoCommandParams(const char *aCommandName, nsICommandParams *aParams, nsISupports *aCommandRefCon) 1.163 +{ 1.164 + // find the command 1.165 + nsCOMPtr<nsIControllerCommand> commandHandler; 1.166 + FindCommandHandler(aCommandName, getter_AddRefs(commandHandler)); 1.167 + if (!commandHandler) 1.168 + { 1.169 +#if DEBUG 1.170 + NS_WARNING("Controller command table asked to do a command that it does not handle -- "); 1.171 +#endif 1.172 + return NS_OK; // we don't handle this command 1.173 + } 1.174 + return commandHandler->DoCommandParams(aCommandName, aParams, aCommandRefCon); 1.175 +} 1.176 + 1.177 + 1.178 +NS_IMETHODIMP 1.179 +nsControllerCommandTable::GetCommandState(const char *aCommandName, nsICommandParams *aParams, nsISupports *aCommandRefCon) 1.180 +{ 1.181 + // find the command 1.182 + nsCOMPtr<nsIControllerCommand> commandHandler; 1.183 + FindCommandHandler(aCommandName, getter_AddRefs(commandHandler)); 1.184 + if (!commandHandler) 1.185 + { 1.186 +#if DEBUG 1.187 + NS_WARNING("Controller command table asked to do a command that it does not handle -- "); 1.188 +#endif 1.189 + return NS_OK; // we don't handle this command 1.190 + } 1.191 + return commandHandler->GetCommandStateParams(aCommandName, aParams, aCommandRefCon); 1.192 +} 1.193 + 1.194 + 1.195 +nsresult 1.196 +NS_NewControllerCommandTable(nsIControllerCommandTable** aResult) 1.197 +{ 1.198 + NS_PRECONDITION(aResult != nullptr, "null ptr"); 1.199 + if (! aResult) 1.200 + return NS_ERROR_NULL_POINTER; 1.201 + 1.202 + nsControllerCommandTable* newCommandTable = new nsControllerCommandTable(); 1.203 + if (! newCommandTable) 1.204 + return NS_ERROR_OUT_OF_MEMORY; 1.205 + 1.206 + NS_ADDREF(newCommandTable); 1.207 + *aResult = newCommandTable; 1.208 + return NS_OK; 1.209 +}