embedding/components/commandhandler/src/nsControllerCommandTable.cpp

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

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

mercurial