michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "FocusManager.h" michael@0: michael@0: #include "Accessible-inl.h" michael@0: #include "AccIterator.h" michael@0: #include "DocAccessible-inl.h" michael@0: #include "nsAccessibilityService.h" michael@0: #include "nsAccUtils.h" michael@0: #include "nsEventShell.h" michael@0: #include "Role.h" michael@0: michael@0: #include "nsFocusManager.h" michael@0: #include "mozilla/EventStateManager.h" michael@0: #include "mozilla/dom/Element.h" michael@0: michael@0: namespace mozilla { michael@0: namespace a11y { michael@0: michael@0: FocusManager::FocusManager() michael@0: { michael@0: } michael@0: michael@0: FocusManager::~FocusManager() michael@0: { michael@0: } michael@0: michael@0: Accessible* michael@0: FocusManager::FocusedAccessible() const michael@0: { michael@0: if (mActiveItem) michael@0: return mActiveItem; michael@0: michael@0: nsINode* focusedNode = FocusedDOMNode(); michael@0: if (focusedNode) { michael@0: DocAccessible* doc = michael@0: GetAccService()->GetDocAccessible(focusedNode->OwnerDoc()); michael@0: return doc ? doc->GetAccessibleEvenIfNotInMapOrContainer(focusedNode) : nullptr; michael@0: } michael@0: michael@0: return nullptr; michael@0: } michael@0: michael@0: bool michael@0: FocusManager::IsFocused(const Accessible* aAccessible) const michael@0: { michael@0: if (mActiveItem) michael@0: return mActiveItem == aAccessible; michael@0: michael@0: nsINode* focusedNode = FocusedDOMNode(); michael@0: if (focusedNode) { michael@0: // XXX: Before getting an accessible for node having a DOM focus make sure michael@0: // they belong to the same document because it can trigger unwanted document michael@0: // accessible creation for temporary about:blank document. Without this michael@0: // peculiarity we would end up with plain implementation based on michael@0: // FocusedAccessible() method call. Make sure this issue is fixed in michael@0: // bug 638465. michael@0: if (focusedNode->OwnerDoc() == aAccessible->GetNode()->OwnerDoc()) { michael@0: DocAccessible* doc = michael@0: GetAccService()->GetDocAccessible(focusedNode->OwnerDoc()); michael@0: return aAccessible == michael@0: (doc ? doc->GetAccessibleEvenIfNotInMapOrContainer(focusedNode) : nullptr); michael@0: } michael@0: } michael@0: return false; michael@0: } michael@0: michael@0: bool michael@0: FocusManager::IsFocusWithin(const Accessible* aContainer) const michael@0: { michael@0: Accessible* child = FocusedAccessible(); michael@0: while (child) { michael@0: if (child == aContainer) michael@0: return true; michael@0: michael@0: child = child->Parent(); michael@0: } michael@0: return false; michael@0: } michael@0: michael@0: FocusManager::FocusDisposition michael@0: FocusManager::IsInOrContainsFocus(const Accessible* aAccessible) const michael@0: { michael@0: Accessible* focus = FocusedAccessible(); michael@0: if (!focus) michael@0: return eNone; michael@0: michael@0: // If focused. michael@0: if (focus == aAccessible) michael@0: return eFocused; michael@0: michael@0: // If contains the focus. michael@0: Accessible* child = focus->Parent(); michael@0: while (child) { michael@0: if (child == aAccessible) michael@0: return eContainsFocus; michael@0: michael@0: child = child->Parent(); michael@0: } michael@0: michael@0: // If contained by focus. michael@0: child = aAccessible->Parent(); michael@0: while (child) { michael@0: if (child == focus) michael@0: return eContainedByFocus; michael@0: michael@0: child = child->Parent(); michael@0: } michael@0: michael@0: return eNone; michael@0: } michael@0: michael@0: void michael@0: FocusManager::NotifyOfDOMFocus(nsISupports* aTarget) michael@0: { michael@0: #ifdef A11Y_LOG michael@0: if (logging::IsEnabled(logging::eFocus)) michael@0: logging::FocusNotificationTarget("DOM focus", "Target", aTarget); michael@0: #endif michael@0: michael@0: mActiveItem = nullptr; michael@0: michael@0: nsCOMPtr targetNode(do_QueryInterface(aTarget)); michael@0: if (targetNode) { michael@0: DocAccessible* document = michael@0: GetAccService()->GetDocAccessible(targetNode->OwnerDoc()); michael@0: if (document) { michael@0: // Set selection listener for focused element. michael@0: if (targetNode->IsElement()) michael@0: SelectionMgr()->SetControlSelectionListener(targetNode->AsElement()); michael@0: michael@0: document->HandleNotification michael@0: (this, &FocusManager::ProcessDOMFocus, targetNode); michael@0: } michael@0: } michael@0: } michael@0: michael@0: void michael@0: FocusManager::NotifyOfDOMBlur(nsISupports* aTarget) michael@0: { michael@0: #ifdef A11Y_LOG michael@0: if (logging::IsEnabled(logging::eFocus)) michael@0: logging::FocusNotificationTarget("DOM blur", "Target", aTarget); michael@0: #endif michael@0: michael@0: mActiveItem = nullptr; michael@0: michael@0: // If DOM document stays focused then fire accessible focus event to process michael@0: // the case when no element within this DOM document will be focused. michael@0: nsCOMPtr targetNode(do_QueryInterface(aTarget)); michael@0: if (targetNode && targetNode->OwnerDoc() == FocusedDOMDocument()) { michael@0: nsIDocument* DOMDoc = targetNode->OwnerDoc(); michael@0: DocAccessible* document = michael@0: GetAccService()->GetDocAccessible(DOMDoc); michael@0: if (document) { michael@0: // Clear selection listener for previously focused element. michael@0: if (targetNode->IsElement()) michael@0: SelectionMgr()->ClearControlSelectionListener(); michael@0: michael@0: document->HandleNotification michael@0: (this, &FocusManager::ProcessDOMFocus, DOMDoc); michael@0: } michael@0: } michael@0: } michael@0: michael@0: void michael@0: FocusManager::ActiveItemChanged(Accessible* aItem, bool aCheckIfActive) michael@0: { michael@0: #ifdef A11Y_LOG michael@0: if (logging::IsEnabled(logging::eFocus)) michael@0: logging::FocusNotificationTarget("active item changed", "Item", aItem); michael@0: #endif michael@0: michael@0: // Nothing changed, happens for XUL trees and HTML selects. michael@0: if (aItem && aItem == mActiveItem) michael@0: return; michael@0: michael@0: mActiveItem = nullptr; michael@0: michael@0: if (aItem && aCheckIfActive) { michael@0: Accessible* widget = aItem->ContainerWidget(); michael@0: #ifdef A11Y_LOG michael@0: if (logging::IsEnabled(logging::eFocus)) michael@0: logging::ActiveWidget(widget); michael@0: #endif michael@0: if (!widget || !widget->IsActiveWidget() || !widget->AreItemsOperable()) michael@0: return; michael@0: } michael@0: mActiveItem = aItem; michael@0: michael@0: // If active item is changed then fire accessible focus event on it, otherwise michael@0: // if there's no an active item then fire focus event to accessible having michael@0: // DOM focus. michael@0: Accessible* target = FocusedAccessible(); michael@0: if (target) michael@0: DispatchFocusEvent(target->Document(), target); michael@0: } michael@0: michael@0: void michael@0: FocusManager::ForceFocusEvent() michael@0: { michael@0: nsINode* focusedNode = FocusedDOMNode(); michael@0: if (focusedNode) { michael@0: DocAccessible* document = michael@0: GetAccService()->GetDocAccessible(focusedNode->OwnerDoc()); michael@0: if (document) { michael@0: document->HandleNotification michael@0: (this, &FocusManager::ProcessDOMFocus, focusedNode); michael@0: } michael@0: } michael@0: } michael@0: michael@0: void michael@0: FocusManager::DispatchFocusEvent(DocAccessible* aDocument, michael@0: Accessible* aTarget) michael@0: { michael@0: NS_PRECONDITION(aDocument, "No document for focused accessible!"); michael@0: if (aDocument) { michael@0: nsRefPtr event = michael@0: new AccEvent(nsIAccessibleEvent::EVENT_FOCUS, aTarget, michael@0: eAutoDetect, AccEvent::eCoalesceOfSameType); michael@0: aDocument->FireDelayedEvent(event); michael@0: michael@0: #ifdef A11Y_LOG michael@0: if (logging::IsEnabled(logging::eFocus)) michael@0: logging::FocusDispatched(aTarget); michael@0: #endif michael@0: } michael@0: } michael@0: michael@0: void michael@0: FocusManager::ProcessDOMFocus(nsINode* aTarget) michael@0: { michael@0: #ifdef A11Y_LOG michael@0: if (logging::IsEnabled(logging::eFocus)) michael@0: logging::FocusNotificationTarget("process DOM focus", "Target", aTarget); michael@0: #endif michael@0: michael@0: DocAccessible* document = michael@0: GetAccService()->GetDocAccessible(aTarget->OwnerDoc()); michael@0: if (!document) michael@0: return; michael@0: michael@0: Accessible* target = document->GetAccessibleEvenIfNotInMapOrContainer(aTarget); michael@0: if (target) { michael@0: // Check if still focused. Otherwise we can end up with storing the active michael@0: // item for control that isn't focused anymore. michael@0: nsINode* focusedNode = FocusedDOMNode(); michael@0: if (!focusedNode) michael@0: return; michael@0: michael@0: Accessible* DOMFocus = michael@0: document->GetAccessibleEvenIfNotInMapOrContainer(focusedNode); michael@0: if (target != DOMFocus) michael@0: return; michael@0: michael@0: Accessible* activeItem = target->CurrentItem(); michael@0: if (activeItem) { michael@0: mActiveItem = activeItem; michael@0: target = activeItem; michael@0: } michael@0: michael@0: DispatchFocusEvent(document, target); michael@0: } michael@0: } michael@0: michael@0: void michael@0: FocusManager::ProcessFocusEvent(AccEvent* aEvent) michael@0: { michael@0: NS_PRECONDITION(aEvent->GetEventType() == nsIAccessibleEvent::EVENT_FOCUS, michael@0: "Focus event is expected!"); michael@0: michael@0: // Emit focus event if event target is the active item. Otherwise then check michael@0: // if it's still focused and then update active item and emit focus event. michael@0: Accessible* target = aEvent->GetAccessible(); michael@0: if (target != mActiveItem) { michael@0: michael@0: // Check if still focused. Otherwise we can end up with storing the active michael@0: // item for control that isn't focused anymore. michael@0: DocAccessible* document = aEvent->GetDocAccessible(); michael@0: nsINode* focusedNode = FocusedDOMNode(); michael@0: if (!focusedNode) michael@0: return; michael@0: michael@0: Accessible* DOMFocus = michael@0: document->GetAccessibleEvenIfNotInMapOrContainer(focusedNode); michael@0: if (target != DOMFocus) michael@0: return; michael@0: michael@0: Accessible* activeItem = target->CurrentItem(); michael@0: if (activeItem) { michael@0: mActiveItem = activeItem; michael@0: target = activeItem; michael@0: } michael@0: } michael@0: michael@0: // Fire menu start/end events for ARIA menus. michael@0: if (target->IsARIARole(nsGkAtoms::menuitem)) { michael@0: // The focus was moved into menu. michael@0: bool tryOwnsParent = true; michael@0: Accessible* ARIAMenubar = nullptr; michael@0: Accessible* child = target; michael@0: Accessible* parent = child->Parent(); michael@0: while (parent) { michael@0: nsRoleMapEntry* roleMap = parent->ARIARoleMap(); michael@0: if (roleMap) { michael@0: if (roleMap->Is(nsGkAtoms::menubar)) { michael@0: ARIAMenubar = parent; michael@0: break; michael@0: } michael@0: michael@0: // Go up in the parent chain of the menu hierarchy. michael@0: if (roleMap->Is(nsGkAtoms::menuitem) || roleMap->Is(nsGkAtoms::menu)) { michael@0: child = parent; michael@0: parent = child->Parent(); michael@0: tryOwnsParent = true; michael@0: continue; michael@0: } michael@0: } michael@0: michael@0: // If no required context role then check aria-owns relation. michael@0: if (!tryOwnsParent) michael@0: break; michael@0: michael@0: RelatedAccIterator iter(child->Document(), child->GetContent(), michael@0: nsGkAtoms::aria_owns); michael@0: parent = iter.Next(); michael@0: tryOwnsParent = false; michael@0: } michael@0: michael@0: if (ARIAMenubar != mActiveARIAMenubar) { michael@0: // Leaving ARIA menu. Fire menu_end event on current menubar. michael@0: if (mActiveARIAMenubar) { michael@0: nsRefPtr menuEndEvent = michael@0: new AccEvent(nsIAccessibleEvent::EVENT_MENU_END, mActiveARIAMenubar, michael@0: aEvent->FromUserInput()); michael@0: nsEventShell::FireEvent(menuEndEvent); michael@0: } michael@0: michael@0: mActiveARIAMenubar = ARIAMenubar; michael@0: michael@0: // Entering ARIA menu. Fire menu_start event. michael@0: if (mActiveARIAMenubar) { michael@0: nsRefPtr menuStartEvent = michael@0: new AccEvent(nsIAccessibleEvent::EVENT_MENU_START, michael@0: mActiveARIAMenubar, aEvent->FromUserInput()); michael@0: nsEventShell::FireEvent(menuStartEvent); michael@0: } michael@0: } michael@0: } else if (mActiveARIAMenubar) { michael@0: // Focus left a menu. Fire menu_end event. michael@0: nsRefPtr menuEndEvent = michael@0: new AccEvent(nsIAccessibleEvent::EVENT_MENU_END, mActiveARIAMenubar, michael@0: aEvent->FromUserInput()); michael@0: nsEventShell::FireEvent(menuEndEvent); michael@0: michael@0: mActiveARIAMenubar = nullptr; michael@0: } michael@0: michael@0: #ifdef A11Y_LOG michael@0: if (logging::IsEnabled(logging::eFocus)) michael@0: logging::FocusNotificationTarget("fire focus event", "Target", target); michael@0: #endif michael@0: michael@0: nsRefPtr focusEvent = michael@0: new AccEvent(nsIAccessibleEvent::EVENT_FOCUS, target, aEvent->FromUserInput()); michael@0: nsEventShell::FireEvent(focusEvent); michael@0: michael@0: // Fire scrolling_start event when the document receives the focus if it has michael@0: // an anchor jump. If an accessible within the document receive the focus michael@0: // then null out the anchor jump because it no longer applies. michael@0: DocAccessible* targetDocument = target->Document(); michael@0: Accessible* anchorJump = targetDocument->AnchorJump(); michael@0: if (anchorJump) { michael@0: if (target == targetDocument) { michael@0: // XXX: bug 625699, note in some cases the node could go away before we michael@0: // we receive focus event, for example if the node is removed from DOM. michael@0: nsEventShell::FireEvent(nsIAccessibleEvent::EVENT_SCROLLING_START, michael@0: anchorJump, aEvent->FromUserInput()); michael@0: } michael@0: targetDocument->SetAnchorJump(nullptr); michael@0: } michael@0: } michael@0: michael@0: nsINode* michael@0: FocusManager::FocusedDOMNode() const michael@0: { michael@0: nsFocusManager* DOMFocusManager = nsFocusManager::GetFocusManager(); michael@0: nsIContent* focusedElm = DOMFocusManager->GetFocusedContent(); michael@0: michael@0: // No focus on remote target elements like xul:browser having DOM focus and michael@0: // residing in chrome process because it means an element in content process michael@0: // keeps the focus. michael@0: if (focusedElm) { michael@0: if (EventStateManager::IsRemoteTarget(focusedElm)) { michael@0: return nullptr; michael@0: } michael@0: return focusedElm; michael@0: } michael@0: michael@0: // Otherwise the focus can be on DOM document. michael@0: nsPIDOMWindow* focusedWnd = DOMFocusManager->GetFocusedWindow(); michael@0: return focusedWnd ? focusedWnd->GetExtantDoc() : nullptr; michael@0: } michael@0: michael@0: nsIDocument* michael@0: FocusManager::FocusedDOMDocument() const michael@0: { michael@0: nsINode* focusedNode = FocusedDOMNode(); michael@0: return focusedNode ? focusedNode->OwnerDoc() : nullptr; michael@0: } michael@0: michael@0: } // namespace a11y michael@0: } // namespace mozilla