Sat, 03 Jan 2015 20:18:00 +0100
Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.
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 "nsCOMPtr.h" |
michael@0 | 7 | #include "nsXBLPrototypeHandler.h" |
michael@0 | 8 | #include "nsXBLWindowKeyHandler.h" |
michael@0 | 9 | #include "nsIContent.h" |
michael@0 | 10 | #include "nsIAtom.h" |
michael@0 | 11 | #include "nsIDOMKeyEvent.h" |
michael@0 | 12 | #include "nsXBLService.h" |
michael@0 | 13 | #include "nsIServiceManager.h" |
michael@0 | 14 | #include "nsGkAtoms.h" |
michael@0 | 15 | #include "nsXBLDocumentInfo.h" |
michael@0 | 16 | #include "nsIDOMElement.h" |
michael@0 | 17 | #include "nsFocusManager.h" |
michael@0 | 18 | #include "nsIURI.h" |
michael@0 | 19 | #include "nsNetUtil.h" |
michael@0 | 20 | #include "nsContentUtils.h" |
michael@0 | 21 | #include "nsXBLPrototypeBinding.h" |
michael@0 | 22 | #include "nsPIDOMWindow.h" |
michael@0 | 23 | #include "nsIDocShell.h" |
michael@0 | 24 | #include "nsIPresShell.h" |
michael@0 | 25 | #include "mozilla/EventStateManager.h" |
michael@0 | 26 | #include "nsISelectionController.h" |
michael@0 | 27 | #include "mozilla/Preferences.h" |
michael@0 | 28 | #include "mozilla/TextEvents.h" |
michael@0 | 29 | #include "mozilla/dom/Element.h" |
michael@0 | 30 | #include "mozilla/dom/Event.h" |
michael@0 | 31 | #include "nsIEditor.h" |
michael@0 | 32 | #include "nsIHTMLEditor.h" |
michael@0 | 33 | #include "nsIDOMDocument.h" |
michael@0 | 34 | |
michael@0 | 35 | using namespace mozilla; |
michael@0 | 36 | using namespace mozilla::dom; |
michael@0 | 37 | |
michael@0 | 38 | class nsXBLSpecialDocInfo : public nsIObserver |
michael@0 | 39 | { |
michael@0 | 40 | public: |
michael@0 | 41 | nsRefPtr<nsXBLDocumentInfo> mHTMLBindings; |
michael@0 | 42 | nsRefPtr<nsXBLDocumentInfo> mUserHTMLBindings; |
michael@0 | 43 | |
michael@0 | 44 | static const char sHTMLBindingStr[]; |
michael@0 | 45 | static const char sUserHTMLBindingStr[]; |
michael@0 | 46 | |
michael@0 | 47 | bool mInitialized; |
michael@0 | 48 | |
michael@0 | 49 | public: |
michael@0 | 50 | NS_DECL_ISUPPORTS |
michael@0 | 51 | NS_DECL_NSIOBSERVER |
michael@0 | 52 | |
michael@0 | 53 | void LoadDocInfo(); |
michael@0 | 54 | void GetAllHandlers(const char* aType, |
michael@0 | 55 | nsXBLPrototypeHandler** handler, |
michael@0 | 56 | nsXBLPrototypeHandler** userHandler); |
michael@0 | 57 | void GetHandlers(nsXBLDocumentInfo* aInfo, |
michael@0 | 58 | const nsACString& aRef, |
michael@0 | 59 | nsXBLPrototypeHandler** aResult); |
michael@0 | 60 | |
michael@0 | 61 | nsXBLSpecialDocInfo() : mInitialized(false) {} |
michael@0 | 62 | |
michael@0 | 63 | virtual ~nsXBLSpecialDocInfo() {} |
michael@0 | 64 | |
michael@0 | 65 | }; |
michael@0 | 66 | |
michael@0 | 67 | const char nsXBLSpecialDocInfo::sHTMLBindingStr[] = |
michael@0 | 68 | "chrome://global/content/platformHTMLBindings.xml"; |
michael@0 | 69 | |
michael@0 | 70 | NS_IMPL_ISUPPORTS(nsXBLSpecialDocInfo, nsIObserver) |
michael@0 | 71 | |
michael@0 | 72 | NS_IMETHODIMP |
michael@0 | 73 | nsXBLSpecialDocInfo::Observe(nsISupports* aSubject, |
michael@0 | 74 | const char* aTopic, |
michael@0 | 75 | const char16_t* aData) |
michael@0 | 76 | { |
michael@0 | 77 | MOZ_ASSERT(!strcmp(aTopic, "xpcom-shutdown"), "wrong topic"); |
michael@0 | 78 | |
michael@0 | 79 | // On shutdown, clear our fields to avoid an extra cycle collection. |
michael@0 | 80 | mHTMLBindings = nullptr; |
michael@0 | 81 | mUserHTMLBindings = nullptr; |
michael@0 | 82 | mInitialized = false; |
michael@0 | 83 | nsContentUtils::UnregisterShutdownObserver(this); |
michael@0 | 84 | |
michael@0 | 85 | return NS_OK; |
michael@0 | 86 | } |
michael@0 | 87 | |
michael@0 | 88 | void nsXBLSpecialDocInfo::LoadDocInfo() |
michael@0 | 89 | { |
michael@0 | 90 | if (mInitialized) |
michael@0 | 91 | return; |
michael@0 | 92 | mInitialized = true; |
michael@0 | 93 | nsContentUtils::RegisterShutdownObserver(this); |
michael@0 | 94 | |
michael@0 | 95 | nsXBLService* xblService = nsXBLService::GetInstance(); |
michael@0 | 96 | if (!xblService) |
michael@0 | 97 | return; |
michael@0 | 98 | |
michael@0 | 99 | // Obtain the platform doc info |
michael@0 | 100 | nsCOMPtr<nsIURI> bindingURI; |
michael@0 | 101 | NS_NewURI(getter_AddRefs(bindingURI), sHTMLBindingStr); |
michael@0 | 102 | if (!bindingURI) { |
michael@0 | 103 | return; |
michael@0 | 104 | } |
michael@0 | 105 | xblService->LoadBindingDocumentInfo(nullptr, nullptr, |
michael@0 | 106 | bindingURI, |
michael@0 | 107 | nullptr, |
michael@0 | 108 | true, |
michael@0 | 109 | getter_AddRefs(mHTMLBindings)); |
michael@0 | 110 | |
michael@0 | 111 | const nsAdoptingCString& userHTMLBindingStr = |
michael@0 | 112 | Preferences::GetCString("dom.userHTMLBindings.uri"); |
michael@0 | 113 | if (!userHTMLBindingStr.IsEmpty()) { |
michael@0 | 114 | NS_NewURI(getter_AddRefs(bindingURI), userHTMLBindingStr); |
michael@0 | 115 | if (!bindingURI) { |
michael@0 | 116 | return; |
michael@0 | 117 | } |
michael@0 | 118 | |
michael@0 | 119 | xblService->LoadBindingDocumentInfo(nullptr, nullptr, |
michael@0 | 120 | bindingURI, |
michael@0 | 121 | nullptr, |
michael@0 | 122 | true, |
michael@0 | 123 | getter_AddRefs(mUserHTMLBindings)); |
michael@0 | 124 | } |
michael@0 | 125 | } |
michael@0 | 126 | |
michael@0 | 127 | // |
michael@0 | 128 | // GetHandlers |
michael@0 | 129 | // |
michael@0 | 130 | // |
michael@0 | 131 | void |
michael@0 | 132 | nsXBLSpecialDocInfo::GetHandlers(nsXBLDocumentInfo* aInfo, |
michael@0 | 133 | const nsACString& aRef, |
michael@0 | 134 | nsXBLPrototypeHandler** aResult) |
michael@0 | 135 | { |
michael@0 | 136 | nsXBLPrototypeBinding* binding = aInfo->GetPrototypeBinding(aRef); |
michael@0 | 137 | |
michael@0 | 138 | NS_ASSERTION(binding, "No binding found for the XBL window key handler."); |
michael@0 | 139 | if (!binding) |
michael@0 | 140 | return; |
michael@0 | 141 | |
michael@0 | 142 | *aResult = binding->GetPrototypeHandlers(); |
michael@0 | 143 | } |
michael@0 | 144 | |
michael@0 | 145 | void |
michael@0 | 146 | nsXBLSpecialDocInfo::GetAllHandlers(const char* aType, |
michael@0 | 147 | nsXBLPrototypeHandler** aHandler, |
michael@0 | 148 | nsXBLPrototypeHandler** aUserHandler) |
michael@0 | 149 | { |
michael@0 | 150 | if (mUserHTMLBindings) { |
michael@0 | 151 | nsAutoCString type(aType); |
michael@0 | 152 | type.Append("User"); |
michael@0 | 153 | GetHandlers(mUserHTMLBindings, type, aUserHandler); |
michael@0 | 154 | } |
michael@0 | 155 | if (mHTMLBindings) { |
michael@0 | 156 | GetHandlers(mHTMLBindings, nsDependentCString(aType), aHandler); |
michael@0 | 157 | } |
michael@0 | 158 | } |
michael@0 | 159 | |
michael@0 | 160 | // Init statics |
michael@0 | 161 | nsXBLSpecialDocInfo* nsXBLWindowKeyHandler::sXBLSpecialDocInfo = nullptr; |
michael@0 | 162 | uint32_t nsXBLWindowKeyHandler::sRefCnt = 0; |
michael@0 | 163 | |
michael@0 | 164 | nsXBLWindowKeyHandler::nsXBLWindowKeyHandler(nsIDOMElement* aElement, |
michael@0 | 165 | EventTarget* aTarget) |
michael@0 | 166 | : mTarget(aTarget), |
michael@0 | 167 | mHandler(nullptr), |
michael@0 | 168 | mUserHandler(nullptr) |
michael@0 | 169 | { |
michael@0 | 170 | mWeakPtrForElement = do_GetWeakReference(aElement); |
michael@0 | 171 | ++sRefCnt; |
michael@0 | 172 | } |
michael@0 | 173 | |
michael@0 | 174 | nsXBLWindowKeyHandler::~nsXBLWindowKeyHandler() |
michael@0 | 175 | { |
michael@0 | 176 | // If mWeakPtrForElement is non-null, we created a prototype handler. |
michael@0 | 177 | if (mWeakPtrForElement) |
michael@0 | 178 | delete mHandler; |
michael@0 | 179 | |
michael@0 | 180 | --sRefCnt; |
michael@0 | 181 | if (!sRefCnt) { |
michael@0 | 182 | NS_IF_RELEASE(sXBLSpecialDocInfo); |
michael@0 | 183 | } |
michael@0 | 184 | } |
michael@0 | 185 | |
michael@0 | 186 | NS_IMPL_ISUPPORTS(nsXBLWindowKeyHandler, |
michael@0 | 187 | nsIDOMEventListener) |
michael@0 | 188 | |
michael@0 | 189 | static void |
michael@0 | 190 | BuildHandlerChain(nsIContent* aContent, nsXBLPrototypeHandler** aResult) |
michael@0 | 191 | { |
michael@0 | 192 | *aResult = nullptr; |
michael@0 | 193 | |
michael@0 | 194 | // Since we chain each handler onto the next handler, |
michael@0 | 195 | // we'll enumerate them here in reverse so that when we |
michael@0 | 196 | // walk the chain they'll come out in the original order |
michael@0 | 197 | for (nsIContent* key = aContent->GetLastChild(); |
michael@0 | 198 | key; |
michael@0 | 199 | key = key->GetPreviousSibling()) { |
michael@0 | 200 | |
michael@0 | 201 | if (key->NodeInfo()->Equals(nsGkAtoms::key, kNameSpaceID_XUL)) { |
michael@0 | 202 | // Check whether the key element has empty value at key/char attribute. |
michael@0 | 203 | // Such element is used by localizers for alternative shortcut key |
michael@0 | 204 | // definition on the locale. See bug 426501. |
michael@0 | 205 | nsAutoString valKey, valCharCode, valKeyCode; |
michael@0 | 206 | bool attrExists = |
michael@0 | 207 | key->GetAttr(kNameSpaceID_None, nsGkAtoms::key, valKey) || |
michael@0 | 208 | key->GetAttr(kNameSpaceID_None, nsGkAtoms::charcode, valCharCode) || |
michael@0 | 209 | key->GetAttr(kNameSpaceID_None, nsGkAtoms::keycode, valKeyCode); |
michael@0 | 210 | if (attrExists && |
michael@0 | 211 | valKey.IsEmpty() && valCharCode.IsEmpty() && valKeyCode.IsEmpty()) |
michael@0 | 212 | continue; |
michael@0 | 213 | |
michael@0 | 214 | nsXBLPrototypeHandler* handler = new nsXBLPrototypeHandler(key); |
michael@0 | 215 | |
michael@0 | 216 | if (!handler) |
michael@0 | 217 | return; |
michael@0 | 218 | |
michael@0 | 219 | handler->SetNextHandler(*aResult); |
michael@0 | 220 | *aResult = handler; |
michael@0 | 221 | } |
michael@0 | 222 | } |
michael@0 | 223 | } |
michael@0 | 224 | |
michael@0 | 225 | // |
michael@0 | 226 | // EnsureHandlers |
michael@0 | 227 | // |
michael@0 | 228 | // Lazily load the XBL handlers. Overridden to handle being attached |
michael@0 | 229 | // to a particular element rather than the document |
michael@0 | 230 | // |
michael@0 | 231 | nsresult |
michael@0 | 232 | nsXBLWindowKeyHandler::EnsureHandlers() |
michael@0 | 233 | { |
michael@0 | 234 | nsCOMPtr<Element> el = GetElement(); |
michael@0 | 235 | NS_ENSURE_STATE(!mWeakPtrForElement || el); |
michael@0 | 236 | if (el) { |
michael@0 | 237 | // We are actually a XUL <keyset>. |
michael@0 | 238 | if (mHandler) |
michael@0 | 239 | return NS_OK; |
michael@0 | 240 | |
michael@0 | 241 | nsCOMPtr<nsIContent> content(do_QueryInterface(el)); |
michael@0 | 242 | BuildHandlerChain(content, &mHandler); |
michael@0 | 243 | } else { // We are an XBL file of handlers. |
michael@0 | 244 | if (!sXBLSpecialDocInfo) { |
michael@0 | 245 | sXBLSpecialDocInfo = new nsXBLSpecialDocInfo(); |
michael@0 | 246 | NS_ADDREF(sXBLSpecialDocInfo); |
michael@0 | 247 | } |
michael@0 | 248 | sXBLSpecialDocInfo->LoadDocInfo(); |
michael@0 | 249 | |
michael@0 | 250 | // Now determine which handlers we should be using. |
michael@0 | 251 | if (IsHTMLEditableFieldFocused()) { |
michael@0 | 252 | sXBLSpecialDocInfo->GetAllHandlers("editor", &mHandler, &mUserHandler); |
michael@0 | 253 | } |
michael@0 | 254 | else { |
michael@0 | 255 | sXBLSpecialDocInfo->GetAllHandlers("browser", &mHandler, &mUserHandler); |
michael@0 | 256 | } |
michael@0 | 257 | } |
michael@0 | 258 | |
michael@0 | 259 | return NS_OK; |
michael@0 | 260 | } |
michael@0 | 261 | |
michael@0 | 262 | nsresult |
michael@0 | 263 | nsXBLWindowKeyHandler::WalkHandlers(nsIDOMKeyEvent* aKeyEvent, nsIAtom* aEventType) |
michael@0 | 264 | { |
michael@0 | 265 | bool prevent; |
michael@0 | 266 | aKeyEvent->GetDefaultPrevented(&prevent); |
michael@0 | 267 | if (prevent) |
michael@0 | 268 | return NS_OK; |
michael@0 | 269 | |
michael@0 | 270 | bool trustedEvent = false; |
michael@0 | 271 | // Don't process the event if it was not dispatched from a trusted source |
michael@0 | 272 | aKeyEvent->GetIsTrusted(&trustedEvent); |
michael@0 | 273 | |
michael@0 | 274 | if (!trustedEvent) |
michael@0 | 275 | return NS_OK; |
michael@0 | 276 | |
michael@0 | 277 | nsresult rv = EnsureHandlers(); |
michael@0 | 278 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 279 | |
michael@0 | 280 | bool isDisabled; |
michael@0 | 281 | nsCOMPtr<Element> el = GetElement(&isDisabled); |
michael@0 | 282 | if (!el) { |
michael@0 | 283 | if (mUserHandler) { |
michael@0 | 284 | WalkHandlersInternal(aKeyEvent, aEventType, mUserHandler, true); |
michael@0 | 285 | aKeyEvent->GetDefaultPrevented(&prevent); |
michael@0 | 286 | if (prevent) |
michael@0 | 287 | return NS_OK; // Handled by the user bindings. Our work here is done. |
michael@0 | 288 | } |
michael@0 | 289 | } |
michael@0 | 290 | |
michael@0 | 291 | // skip keysets that are disabled |
michael@0 | 292 | if (el && isDisabled) { |
michael@0 | 293 | return NS_OK; |
michael@0 | 294 | } |
michael@0 | 295 | |
michael@0 | 296 | WalkHandlersInternal(aKeyEvent, aEventType, mHandler, true); |
michael@0 | 297 | |
michael@0 | 298 | return NS_OK; |
michael@0 | 299 | } |
michael@0 | 300 | |
michael@0 | 301 | NS_IMETHODIMP |
michael@0 | 302 | nsXBLWindowKeyHandler::HandleEvent(nsIDOMEvent* aEvent) |
michael@0 | 303 | { |
michael@0 | 304 | nsCOMPtr<nsIDOMKeyEvent> keyEvent(do_QueryInterface(aEvent)); |
michael@0 | 305 | NS_ENSURE_TRUE(keyEvent, NS_ERROR_INVALID_ARG); |
michael@0 | 306 | |
michael@0 | 307 | uint16_t eventPhase; |
michael@0 | 308 | aEvent->GetEventPhase(&eventPhase); |
michael@0 | 309 | if (eventPhase == nsIDOMEvent::CAPTURING_PHASE) { |
michael@0 | 310 | HandleEventOnCapture(keyEvent); |
michael@0 | 311 | return NS_OK; |
michael@0 | 312 | } |
michael@0 | 313 | |
michael@0 | 314 | nsAutoString eventType; |
michael@0 | 315 | aEvent->GetType(eventType); |
michael@0 | 316 | nsCOMPtr<nsIAtom> eventTypeAtom = do_GetAtom(eventType); |
michael@0 | 317 | NS_ENSURE_TRUE(eventTypeAtom, NS_ERROR_OUT_OF_MEMORY); |
michael@0 | 318 | |
michael@0 | 319 | return WalkHandlers(keyEvent, eventTypeAtom); |
michael@0 | 320 | } |
michael@0 | 321 | |
michael@0 | 322 | void |
michael@0 | 323 | nsXBLWindowKeyHandler::HandleEventOnCapture(nsIDOMKeyEvent* aEvent) |
michael@0 | 324 | { |
michael@0 | 325 | WidgetKeyboardEvent* widgetEvent = |
michael@0 | 326 | aEvent->GetInternalNSEvent()->AsKeyboardEvent(); |
michael@0 | 327 | |
michael@0 | 328 | if (widgetEvent->mFlags.mNoCrossProcessBoundaryForwarding) { |
michael@0 | 329 | return; |
michael@0 | 330 | } |
michael@0 | 331 | |
michael@0 | 332 | nsCOMPtr<mozilla::dom::Element> originalTarget = |
michael@0 | 333 | do_QueryInterface(aEvent->GetInternalNSEvent()->originalTarget); |
michael@0 | 334 | if (!EventStateManager::IsRemoteTarget(originalTarget)) { |
michael@0 | 335 | return; |
michael@0 | 336 | } |
michael@0 | 337 | |
michael@0 | 338 | if (!HasHandlerForEvent(aEvent)) { |
michael@0 | 339 | return; |
michael@0 | 340 | } |
michael@0 | 341 | |
michael@0 | 342 | // If this event hasn't been marked as mNoCrossProcessBoundaryForwarding |
michael@0 | 343 | // yet, it means it wasn't processed by content. We'll not call any |
michael@0 | 344 | // of the handlers at this moment, and will wait for the event to be |
michael@0 | 345 | // redispatched with mNoCrossProcessBoundaryForwarding = 1 to process it. |
michael@0 | 346 | |
michael@0 | 347 | // Inform the child process that this is a event that we want a reply |
michael@0 | 348 | // from. |
michael@0 | 349 | widgetEvent->mFlags.mWantReplyFromContentProcess = 1; |
michael@0 | 350 | aEvent->StopPropagation(); |
michael@0 | 351 | } |
michael@0 | 352 | |
michael@0 | 353 | // |
michael@0 | 354 | // EventMatched |
michael@0 | 355 | // |
michael@0 | 356 | // See if the given handler cares about this particular key event |
michael@0 | 357 | // |
michael@0 | 358 | bool |
michael@0 | 359 | nsXBLWindowKeyHandler::EventMatched(nsXBLPrototypeHandler* inHandler, |
michael@0 | 360 | nsIAtom* inEventType, |
michael@0 | 361 | nsIDOMKeyEvent* inEvent, |
michael@0 | 362 | uint32_t aCharCode, bool aIgnoreShiftKey) |
michael@0 | 363 | { |
michael@0 | 364 | return inHandler->KeyEventMatched(inEventType, inEvent, aCharCode, |
michael@0 | 365 | aIgnoreShiftKey); |
michael@0 | 366 | } |
michael@0 | 367 | |
michael@0 | 368 | bool |
michael@0 | 369 | nsXBLWindowKeyHandler::IsHTMLEditableFieldFocused() |
michael@0 | 370 | { |
michael@0 | 371 | nsIFocusManager* fm = nsFocusManager::GetFocusManager(); |
michael@0 | 372 | if (!fm) |
michael@0 | 373 | return false; |
michael@0 | 374 | |
michael@0 | 375 | nsCOMPtr<nsIDOMWindow> focusedWindow; |
michael@0 | 376 | fm->GetFocusedWindow(getter_AddRefs(focusedWindow)); |
michael@0 | 377 | if (!focusedWindow) |
michael@0 | 378 | return false; |
michael@0 | 379 | |
michael@0 | 380 | nsCOMPtr<nsPIDOMWindow> piwin(do_QueryInterface(focusedWindow)); |
michael@0 | 381 | nsIDocShell *docShell = piwin->GetDocShell(); |
michael@0 | 382 | if (!docShell) { |
michael@0 | 383 | return false; |
michael@0 | 384 | } |
michael@0 | 385 | |
michael@0 | 386 | nsCOMPtr<nsIEditor> editor; |
michael@0 | 387 | docShell->GetEditor(getter_AddRefs(editor)); |
michael@0 | 388 | nsCOMPtr<nsIHTMLEditor> htmlEditor = do_QueryInterface(editor); |
michael@0 | 389 | if (!htmlEditor) { |
michael@0 | 390 | return false; |
michael@0 | 391 | } |
michael@0 | 392 | |
michael@0 | 393 | nsCOMPtr<nsIDOMDocument> domDocument; |
michael@0 | 394 | editor->GetDocument(getter_AddRefs(domDocument)); |
michael@0 | 395 | nsCOMPtr<nsIDocument> doc = do_QueryInterface(domDocument); |
michael@0 | 396 | if (doc->HasFlag(NODE_IS_EDITABLE)) { |
michael@0 | 397 | // Don't need to perform any checks in designMode documents. |
michael@0 | 398 | return true; |
michael@0 | 399 | } |
michael@0 | 400 | |
michael@0 | 401 | nsCOMPtr<nsIDOMElement> focusedElement; |
michael@0 | 402 | fm->GetFocusedElement(getter_AddRefs(focusedElement)); |
michael@0 | 403 | nsCOMPtr<nsINode> focusedNode = do_QueryInterface(focusedElement); |
michael@0 | 404 | if (focusedNode) { |
michael@0 | 405 | // If there is a focused element, make sure it's in the active editing host. |
michael@0 | 406 | // Note that GetActiveEditingHost finds the current editing host based on |
michael@0 | 407 | // the document's selection. Even though the document selection is usually |
michael@0 | 408 | // collapsed to where the focus is, but the page may modify the selection |
michael@0 | 409 | // without our knowledge, in which case this check will do something useful. |
michael@0 | 410 | nsCOMPtr<Element> activeEditingHost = htmlEditor->GetActiveEditingHost(); |
michael@0 | 411 | if (!activeEditingHost) { |
michael@0 | 412 | return false; |
michael@0 | 413 | } |
michael@0 | 414 | return nsContentUtils::ContentIsDescendantOf(focusedNode, activeEditingHost); |
michael@0 | 415 | } |
michael@0 | 416 | |
michael@0 | 417 | return false; |
michael@0 | 418 | } |
michael@0 | 419 | |
michael@0 | 420 | // |
michael@0 | 421 | // WalkHandlersInternal and WalkHandlersAndExecute |
michael@0 | 422 | // |
michael@0 | 423 | // Given a particular DOM event and a pointer to the first handler in the list, |
michael@0 | 424 | // scan through the list to find something to handle the event. If aExecute = true, |
michael@0 | 425 | // the handler will be executed; otherwise just return an answer telling if a handler |
michael@0 | 426 | // for that event was found. |
michael@0 | 427 | // |
michael@0 | 428 | bool |
michael@0 | 429 | nsXBLWindowKeyHandler::WalkHandlersInternal(nsIDOMKeyEvent* aKeyEvent, |
michael@0 | 430 | nsIAtom* aEventType, |
michael@0 | 431 | nsXBLPrototypeHandler* aHandler, |
michael@0 | 432 | bool aExecute) |
michael@0 | 433 | { |
michael@0 | 434 | nsAutoTArray<nsShortcutCandidate, 10> accessKeys; |
michael@0 | 435 | nsContentUtils::GetAccelKeyCandidates(aKeyEvent, accessKeys); |
michael@0 | 436 | |
michael@0 | 437 | if (accessKeys.IsEmpty()) { |
michael@0 | 438 | return WalkHandlersAndExecute(aKeyEvent, aEventType, aHandler, |
michael@0 | 439 | 0, false, aExecute); |
michael@0 | 440 | } |
michael@0 | 441 | |
michael@0 | 442 | for (uint32_t i = 0; i < accessKeys.Length(); ++i) { |
michael@0 | 443 | nsShortcutCandidate &key = accessKeys[i]; |
michael@0 | 444 | if (WalkHandlersAndExecute(aKeyEvent, aEventType, aHandler, |
michael@0 | 445 | key.mCharCode, key.mIgnoreShift, aExecute)) |
michael@0 | 446 | return true; |
michael@0 | 447 | } |
michael@0 | 448 | return false; |
michael@0 | 449 | } |
michael@0 | 450 | |
michael@0 | 451 | bool |
michael@0 | 452 | nsXBLWindowKeyHandler::WalkHandlersAndExecute(nsIDOMKeyEvent* aKeyEvent, |
michael@0 | 453 | nsIAtom* aEventType, |
michael@0 | 454 | nsXBLPrototypeHandler* aHandler, |
michael@0 | 455 | uint32_t aCharCode, |
michael@0 | 456 | bool aIgnoreShiftKey, |
michael@0 | 457 | bool aExecute) |
michael@0 | 458 | { |
michael@0 | 459 | nsresult rv; |
michael@0 | 460 | |
michael@0 | 461 | // Try all of the handlers until we find one that matches the event. |
michael@0 | 462 | for (nsXBLPrototypeHandler *currHandler = aHandler; currHandler; |
michael@0 | 463 | currHandler = currHandler->GetNextHandler()) { |
michael@0 | 464 | bool stopped = aKeyEvent->IsDispatchStopped(); |
michael@0 | 465 | if (stopped) { |
michael@0 | 466 | // The event is finished, don't execute any more handlers |
michael@0 | 467 | return false; |
michael@0 | 468 | } |
michael@0 | 469 | |
michael@0 | 470 | if (!EventMatched(currHandler, aEventType, aKeyEvent, |
michael@0 | 471 | aCharCode, aIgnoreShiftKey)) |
michael@0 | 472 | continue; // try the next one |
michael@0 | 473 | |
michael@0 | 474 | // Before executing this handler, check that it's not disabled, |
michael@0 | 475 | // and that it has something to do (oncommand of the <key> or its |
michael@0 | 476 | // <command> is non-empty). |
michael@0 | 477 | nsCOMPtr<nsIContent> elt = currHandler->GetHandlerElement(); |
michael@0 | 478 | nsCOMPtr<Element> commandElt; |
michael@0 | 479 | |
michael@0 | 480 | // See if we're in a XUL doc. |
michael@0 | 481 | nsCOMPtr<Element> el = GetElement(); |
michael@0 | 482 | if (el && elt) { |
michael@0 | 483 | // We are. Obtain our command attribute. |
michael@0 | 484 | nsAutoString command; |
michael@0 | 485 | elt->GetAttr(kNameSpaceID_None, nsGkAtoms::command, command); |
michael@0 | 486 | if (!command.IsEmpty()) { |
michael@0 | 487 | // Locate the command element in question. Note that we |
michael@0 | 488 | // know "elt" is in a doc if we're dealing with it here. |
michael@0 | 489 | NS_ASSERTION(elt->IsInDoc(), "elt must be in document"); |
michael@0 | 490 | nsIDocument *doc = elt->GetCurrentDoc(); |
michael@0 | 491 | if (doc) |
michael@0 | 492 | commandElt = do_QueryInterface(doc->GetElementById(command)); |
michael@0 | 493 | |
michael@0 | 494 | if (!commandElt) { |
michael@0 | 495 | NS_ERROR("A XUL <key> is observing a command that doesn't exist. Unable to execute key binding!"); |
michael@0 | 496 | continue; |
michael@0 | 497 | } |
michael@0 | 498 | } |
michael@0 | 499 | } |
michael@0 | 500 | |
michael@0 | 501 | if (!commandElt) { |
michael@0 | 502 | commandElt = do_QueryInterface(elt); |
michael@0 | 503 | } |
michael@0 | 504 | |
michael@0 | 505 | if (commandElt) { |
michael@0 | 506 | nsAutoString value; |
michael@0 | 507 | commandElt->GetAttribute(NS_LITERAL_STRING("disabled"), value); |
michael@0 | 508 | if (value.EqualsLiteral("true")) { |
michael@0 | 509 | continue; // this handler is disabled, try the next one |
michael@0 | 510 | } |
michael@0 | 511 | |
michael@0 | 512 | // Check that there is an oncommand handler |
michael@0 | 513 | commandElt->GetAttribute(NS_LITERAL_STRING("oncommand"), value); |
michael@0 | 514 | if (value.IsEmpty()) { |
michael@0 | 515 | continue; // nothing to do |
michael@0 | 516 | } |
michael@0 | 517 | } |
michael@0 | 518 | |
michael@0 | 519 | nsCOMPtr<EventTarget> piTarget; |
michael@0 | 520 | nsCOMPtr<Element> element = GetElement(); |
michael@0 | 521 | if (element) { |
michael@0 | 522 | piTarget = commandElt; |
michael@0 | 523 | } else { |
michael@0 | 524 | piTarget = mTarget; |
michael@0 | 525 | } |
michael@0 | 526 | |
michael@0 | 527 | if (!aExecute) { |
michael@0 | 528 | return true; |
michael@0 | 529 | } |
michael@0 | 530 | |
michael@0 | 531 | rv = currHandler->ExecuteHandler(piTarget, aKeyEvent); |
michael@0 | 532 | if (NS_SUCCEEDED(rv)) { |
michael@0 | 533 | return true; |
michael@0 | 534 | } |
michael@0 | 535 | } |
michael@0 | 536 | |
michael@0 | 537 | return false; |
michael@0 | 538 | } |
michael@0 | 539 | |
michael@0 | 540 | bool |
michael@0 | 541 | nsXBLWindowKeyHandler::HasHandlerForEvent(nsIDOMKeyEvent* aEvent) |
michael@0 | 542 | { |
michael@0 | 543 | if (!aEvent->InternalDOMEvent()->IsTrusted()) { |
michael@0 | 544 | return false; |
michael@0 | 545 | } |
michael@0 | 546 | |
michael@0 | 547 | nsresult rv = EnsureHandlers(); |
michael@0 | 548 | NS_ENSURE_SUCCESS(rv, false); |
michael@0 | 549 | |
michael@0 | 550 | bool isDisabled; |
michael@0 | 551 | nsCOMPtr<Element> el = GetElement(&isDisabled); |
michael@0 | 552 | if (el && isDisabled) { |
michael@0 | 553 | return false; |
michael@0 | 554 | } |
michael@0 | 555 | |
michael@0 | 556 | nsAutoString eventType; |
michael@0 | 557 | aEvent->GetType(eventType); |
michael@0 | 558 | nsCOMPtr<nsIAtom> eventTypeAtom = do_GetAtom(eventType); |
michael@0 | 559 | NS_ENSURE_TRUE(eventTypeAtom, false); |
michael@0 | 560 | |
michael@0 | 561 | return WalkHandlersInternal(aEvent, eventTypeAtom, mHandler, false); |
michael@0 | 562 | } |
michael@0 | 563 | |
michael@0 | 564 | already_AddRefed<Element> |
michael@0 | 565 | nsXBLWindowKeyHandler::GetElement(bool* aIsDisabled) |
michael@0 | 566 | { |
michael@0 | 567 | nsCOMPtr<Element> element = do_QueryReferent(mWeakPtrForElement); |
michael@0 | 568 | if (element && aIsDisabled) { |
michael@0 | 569 | *aIsDisabled = element->AttrValueIs(kNameSpaceID_None, nsGkAtoms::disabled, |
michael@0 | 570 | nsGkAtoms::_true, eCaseMatters); |
michael@0 | 571 | } |
michael@0 | 572 | return element.forget(); |
michael@0 | 573 | } |
michael@0 | 574 | |
michael@0 | 575 | /////////////////////////////////////////////////////////////////////////////////// |
michael@0 | 576 | |
michael@0 | 577 | already_AddRefed<nsXBLWindowKeyHandler> |
michael@0 | 578 | NS_NewXBLWindowKeyHandler(nsIDOMElement* aElement, EventTarget* aTarget) |
michael@0 | 579 | { |
michael@0 | 580 | nsRefPtr<nsXBLWindowKeyHandler> result = |
michael@0 | 581 | new nsXBLWindowKeyHandler(aElement, aTarget); |
michael@0 | 582 | return result.forget(); |
michael@0 | 583 | } |