embedding/components/commandhandler/src/nsCommandManager.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
michael@0 8 #include "nsIController.h"
michael@0 9 #include "nsIControllers.h"
michael@0 10 #include "nsIObserver.h"
michael@0 11
michael@0 12 #include "nsIComponentManager.h"
michael@0 13
michael@0 14 #include "nsServiceManagerUtils.h"
michael@0 15 #include "nsIScriptSecurityManager.h"
michael@0 16
michael@0 17 #include "nsIDOMWindow.h"
michael@0 18 #include "nsPIDOMWindow.h"
michael@0 19 #include "nsPIWindowRoot.h"
michael@0 20 #include "nsIFocusManager.h"
michael@0 21
michael@0 22 #include "nsCOMArray.h"
michael@0 23
michael@0 24 #include "nsCommandManager.h"
michael@0 25
michael@0 26
michael@0 27 nsCommandManager::nsCommandManager()
michael@0 28 : mWindow(nullptr)
michael@0 29 {
michael@0 30 /* member initializers and constructor code */
michael@0 31 }
michael@0 32
michael@0 33 nsCommandManager::~nsCommandManager()
michael@0 34 {
michael@0 35 /* destructor code */
michael@0 36 }
michael@0 37
michael@0 38
michael@0 39 static PLDHashOperator
michael@0 40 TraverseCommandObservers(const char* aKey,
michael@0 41 nsCommandManager::ObserverList* aObservers,
michael@0 42 void* aClosure)
michael@0 43 {
michael@0 44 nsCycleCollectionTraversalCallback *cb =
michael@0 45 static_cast<nsCycleCollectionTraversalCallback*>(aClosure);
michael@0 46
michael@0 47 int32_t i, numItems = aObservers->Length();
michael@0 48 for (i = 0; i < numItems; ++i) {
michael@0 49 cb->NoteXPCOMChild(aObservers->ElementAt(i));
michael@0 50 }
michael@0 51
michael@0 52 return PL_DHASH_NEXT;
michael@0 53 }
michael@0 54
michael@0 55 NS_IMPL_CYCLE_COLLECTION_CLASS(nsCommandManager)
michael@0 56
michael@0 57 NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(nsCommandManager)
michael@0 58 tmp->mObserversTable.Clear();
michael@0 59 NS_IMPL_CYCLE_COLLECTION_UNLINK_END
michael@0 60 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(nsCommandManager)
michael@0 61 tmp->mObserversTable.EnumerateRead(TraverseCommandObservers, &cb);
michael@0 62 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
michael@0 63
michael@0 64 NS_IMPL_CYCLE_COLLECTING_ADDREF(nsCommandManager)
michael@0 65 NS_IMPL_CYCLE_COLLECTING_RELEASE(nsCommandManager)
michael@0 66
michael@0 67 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(nsCommandManager)
michael@0 68 NS_INTERFACE_MAP_ENTRY(nsICommandManager)
michael@0 69 NS_INTERFACE_MAP_ENTRY(nsPICommandUpdater)
michael@0 70 NS_INTERFACE_MAP_ENTRY(nsISupportsWeakReference)
michael@0 71 NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsICommandManager)
michael@0 72 NS_INTERFACE_MAP_END
michael@0 73
michael@0 74 #if 0
michael@0 75 #pragma mark -
michael@0 76 #endif
michael@0 77
michael@0 78 /* void init (in nsIDOMWindow aWindow); */
michael@0 79 NS_IMETHODIMP
michael@0 80 nsCommandManager::Init(nsIDOMWindow *aWindow)
michael@0 81 {
michael@0 82 NS_ENSURE_ARG_POINTER(aWindow);
michael@0 83
michael@0 84 NS_ASSERTION(aWindow, "Need non-null window here");
michael@0 85 mWindow = aWindow; // weak ptr
michael@0 86 return NS_OK;
michael@0 87 }
michael@0 88
michael@0 89 /* void commandStatusChanged (in DOMString aCommandName, in long aChangeFlags); */
michael@0 90 NS_IMETHODIMP
michael@0 91 nsCommandManager::CommandStatusChanged(const char * aCommandName)
michael@0 92 {
michael@0 93 ObserverList* commandObservers;
michael@0 94 mObserversTable.Get(aCommandName, &commandObservers);
michael@0 95
michael@0 96 if (commandObservers)
michael@0 97 {
michael@0 98 // XXX Should we worry about observers removing themselves from Observe()?
michael@0 99 int32_t i, numItems = commandObservers->Length();
michael@0 100 for (i = 0; i < numItems; ++i)
michael@0 101 {
michael@0 102 nsCOMPtr<nsIObserver> observer = commandObservers->ElementAt(i);
michael@0 103 // should we get the command state to pass here? This might be expensive.
michael@0 104 observer->Observe(NS_ISUPPORTS_CAST(nsICommandManager*, this),
michael@0 105 aCommandName,
michael@0 106 MOZ_UTF16("command_status_changed"));
michael@0 107 }
michael@0 108 }
michael@0 109
michael@0 110 return NS_OK;
michael@0 111 }
michael@0 112
michael@0 113 #if 0
michael@0 114 #pragma mark -
michael@0 115 #endif
michael@0 116
michael@0 117 /* void addCommandObserver (in nsIObserver aCommandObserver, in wstring aCommandToObserve); */
michael@0 118 NS_IMETHODIMP
michael@0 119 nsCommandManager::AddCommandObserver(nsIObserver *aCommandObserver, const char *aCommandToObserve)
michael@0 120 {
michael@0 121 NS_ENSURE_ARG(aCommandObserver);
michael@0 122
michael@0 123 // XXX todo: handle special cases of aCommandToObserve being null, or empty
michael@0 124
michael@0 125 // for each command in the table, we make a list of observers for that command
michael@0 126 ObserverList* commandObservers;
michael@0 127 if (!mObserversTable.Get(aCommandToObserve, &commandObservers))
michael@0 128 {
michael@0 129 commandObservers = new ObserverList;
michael@0 130 mObserversTable.Put(aCommandToObserve, commandObservers);
michael@0 131 }
michael@0 132
michael@0 133 // need to check that this command observer hasn't already been registered
michael@0 134 int32_t existingIndex = commandObservers->IndexOf(aCommandObserver);
michael@0 135 if (existingIndex == -1)
michael@0 136 commandObservers->AppendElement(aCommandObserver);
michael@0 137 else
michael@0 138 NS_WARNING("Registering command observer twice on the same command");
michael@0 139
michael@0 140 return NS_OK;
michael@0 141 }
michael@0 142
michael@0 143 /* void removeCommandObserver (in nsIObserver aCommandObserver, in wstring aCommandObserved); */
michael@0 144 NS_IMETHODIMP
michael@0 145 nsCommandManager::RemoveCommandObserver(nsIObserver *aCommandObserver, const char *aCommandObserved)
michael@0 146 {
michael@0 147 NS_ENSURE_ARG(aCommandObserver);
michael@0 148
michael@0 149 // XXX todo: handle special cases of aCommandToObserve being null, or empty
michael@0 150
michael@0 151 ObserverList* commandObservers;
michael@0 152 if (!mObserversTable.Get(aCommandObserved, &commandObservers))
michael@0 153 return NS_ERROR_UNEXPECTED;
michael@0 154
michael@0 155 commandObservers->RemoveElement(aCommandObserver);
michael@0 156
michael@0 157 return NS_OK;
michael@0 158 }
michael@0 159
michael@0 160 /* boolean isCommandSupported(in string aCommandName,
michael@0 161 in nsIDOMWindow aTargetWindow); */
michael@0 162 NS_IMETHODIMP
michael@0 163 nsCommandManager::IsCommandSupported(const char *aCommandName,
michael@0 164 nsIDOMWindow *aTargetWindow,
michael@0 165 bool *outCommandSupported)
michael@0 166 {
michael@0 167 NS_ENSURE_ARG_POINTER(outCommandSupported);
michael@0 168
michael@0 169 nsCOMPtr<nsIController> controller;
michael@0 170 GetControllerForCommand(aCommandName, aTargetWindow, getter_AddRefs(controller));
michael@0 171 *outCommandSupported = (controller.get() != nullptr);
michael@0 172 return NS_OK;
michael@0 173 }
michael@0 174
michael@0 175 /* boolean isCommandEnabled(in string aCommandName,
michael@0 176 in nsIDOMWindow aTargetWindow); */
michael@0 177 NS_IMETHODIMP
michael@0 178 nsCommandManager::IsCommandEnabled(const char *aCommandName,
michael@0 179 nsIDOMWindow *aTargetWindow,
michael@0 180 bool *outCommandEnabled)
michael@0 181 {
michael@0 182 NS_ENSURE_ARG_POINTER(outCommandEnabled);
michael@0 183
michael@0 184 bool commandEnabled = false;
michael@0 185
michael@0 186 nsCOMPtr<nsIController> controller;
michael@0 187 GetControllerForCommand(aCommandName, aTargetWindow, getter_AddRefs(controller));
michael@0 188 if (controller)
michael@0 189 {
michael@0 190 controller->IsCommandEnabled(aCommandName, &commandEnabled);
michael@0 191 }
michael@0 192 *outCommandEnabled = commandEnabled;
michael@0 193 return NS_OK;
michael@0 194 }
michael@0 195
michael@0 196 /* void getCommandState (in DOMString aCommandName,
michael@0 197 in nsIDOMWindow aTargetWindow,
michael@0 198 inout nsICommandParams aCommandParams); */
michael@0 199 NS_IMETHODIMP
michael@0 200 nsCommandManager::GetCommandState(const char *aCommandName,
michael@0 201 nsIDOMWindow *aTargetWindow,
michael@0 202 nsICommandParams *aCommandParams)
michael@0 203 {
michael@0 204 nsCOMPtr<nsIController> controller;
michael@0 205 nsAutoString tValue;
michael@0 206 nsresult rv = GetControllerForCommand(aCommandName, aTargetWindow, getter_AddRefs(controller));
michael@0 207 if (!controller)
michael@0 208 return NS_ERROR_FAILURE;
michael@0 209
michael@0 210 nsCOMPtr<nsICommandController> commandController = do_QueryInterface(controller);
michael@0 211 if (commandController)
michael@0 212 rv = commandController->GetCommandStateWithParams(aCommandName, aCommandParams);
michael@0 213 else
michael@0 214 rv = NS_ERROR_NOT_IMPLEMENTED;
michael@0 215 return rv;
michael@0 216 }
michael@0 217
michael@0 218 /* void doCommand(in string aCommandName,
michael@0 219 in nsICommandParams aCommandParams,
michael@0 220 in nsIDOMWindow aTargetWindow); */
michael@0 221 NS_IMETHODIMP
michael@0 222 nsCommandManager::DoCommand(const char *aCommandName,
michael@0 223 nsICommandParams *aCommandParams,
michael@0 224 nsIDOMWindow *aTargetWindow)
michael@0 225 {
michael@0 226 nsCOMPtr<nsIController> controller;
michael@0 227 nsresult rv = GetControllerForCommand(aCommandName, aTargetWindow, getter_AddRefs(controller));
michael@0 228 if (!controller)
michael@0 229 return NS_ERROR_FAILURE;
michael@0 230
michael@0 231 nsCOMPtr<nsICommandController> commandController = do_QueryInterface(controller);
michael@0 232 if (commandController && aCommandParams)
michael@0 233 rv = commandController->DoCommandWithParams(aCommandName, aCommandParams);
michael@0 234 else
michael@0 235 rv = controller->DoCommand(aCommandName);
michael@0 236 return rv;
michael@0 237 }
michael@0 238
michael@0 239 nsresult
michael@0 240 nsCommandManager::IsCallerChrome(bool *is_caller_chrome)
michael@0 241 {
michael@0 242 *is_caller_chrome = false;
michael@0 243 nsresult rv = NS_OK;
michael@0 244 nsCOMPtr<nsIScriptSecurityManager> secMan =
michael@0 245 do_GetService(NS_SCRIPTSECURITYMANAGER_CONTRACTID, &rv);
michael@0 246 if (NS_FAILED(rv))
michael@0 247 return rv;
michael@0 248 if (!secMan)
michael@0 249 return NS_ERROR_FAILURE;
michael@0 250
michael@0 251 rv = secMan->SubjectPrincipalIsSystem(is_caller_chrome);
michael@0 252 return rv;
michael@0 253 }
michael@0 254
michael@0 255 nsresult
michael@0 256 nsCommandManager::GetControllerForCommand(const char *aCommand,
michael@0 257 nsIDOMWindow *aTargetWindow,
michael@0 258 nsIController** outController)
michael@0 259 {
michael@0 260 nsresult rv = NS_ERROR_FAILURE;
michael@0 261 *outController = nullptr;
michael@0 262
michael@0 263 // check if we're in content or chrome
michael@0 264 // if we're not chrome we must have a target window or we bail
michael@0 265 bool isChrome = false;
michael@0 266 rv = IsCallerChrome(&isChrome);
michael@0 267 if (NS_FAILED(rv))
michael@0 268 return rv;
michael@0 269
michael@0 270 if (!isChrome) {
michael@0 271 if (!aTargetWindow)
michael@0 272 return rv;
michael@0 273
michael@0 274 // if a target window is specified, it must be the window we expect
michael@0 275 if (aTargetWindow != mWindow)
michael@0 276 return NS_ERROR_FAILURE;
michael@0 277 }
michael@0 278
michael@0 279 if (aTargetWindow) {
michael@0 280 // get the controller for this particular window
michael@0 281 nsCOMPtr<nsIControllers> controllers;
michael@0 282 rv = aTargetWindow->GetControllers(getter_AddRefs(controllers));
michael@0 283 if (NS_FAILED(rv))
michael@0 284 return rv;
michael@0 285 if (!controllers)
michael@0 286 return NS_ERROR_FAILURE;
michael@0 287
michael@0 288 // dispatch the command
michael@0 289 return controllers->GetControllerForCommand(aCommand, outController);
michael@0 290 }
michael@0 291
michael@0 292 nsCOMPtr<nsPIDOMWindow> window(do_QueryInterface(mWindow));
michael@0 293 NS_ENSURE_TRUE(window, NS_ERROR_FAILURE);
michael@0 294 nsCOMPtr<nsPIWindowRoot> root = window->GetTopWindowRoot();
michael@0 295 NS_ENSURE_TRUE(root, NS_ERROR_FAILURE);
michael@0 296
michael@0 297 // no target window; send command to focus controller
michael@0 298 return root->GetControllerForCommand(aCommand, outController);
michael@0 299 }
michael@0 300

mercurial