1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455
|
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "FocusManager.h"
#include "LocalAccessible-inl.h"
#include "DocAccessible-inl.h"
#include "nsAccessibilityService.h"
#include "nsEventShell.h"
#include "nsFocusManager.h"
#include "mozilla/a11y/DocAccessibleParent.h"
#include "mozilla/EventStateManager.h"
#include "mozilla/dom/Element.h"
#include "mozilla/dom/BrowsingContext.h"
#include "mozilla/dom/BrowserParent.h"
namespace mozilla {
namespace a11y {
FocusManager::FocusManager() {}
FocusManager::~FocusManager() {}
LocalAccessible* FocusManager::FocusedLocalAccessible() const {
MOZ_ASSERT(NS_IsMainThread());
if (mActiveItem) {
if (mActiveItem->IsDefunct()) {
MOZ_ASSERT_UNREACHABLE("Stored active item is unbound from document");
return nullptr;
}
return mActiveItem;
}
if (nsAccessibilityService::IsShutdown()) {
// We might try to get or create a DocAccessible below, which isn't safe (or
// useful) if the accessibility service is shutting down.
return nullptr;
}
nsINode* focusedNode = FocusedDOMNode();
if (focusedNode) {
DocAccessible* doc =
GetAccService()->GetDocAccessible(focusedNode->OwnerDoc());
return doc ? doc->GetAccessibleEvenIfNotInMapOrContainer(focusedNode)
: nullptr;
}
return nullptr;
}
Accessible* FocusManager::FocusedAccessible() const {
#if defined(ANDROID)
// It's not safe to call FocusedLocalAccessible() except on the main thread.
// Android might query RemoteAccessibles on the UI thread, which might call
// FocusedAccessible(). Never try to get the focused LocalAccessible in this
// case.
if (NS_IsMainThread()) {
if (Accessible* focusedAcc = FocusedLocalAccessible()) {
return focusedAcc;
}
} else {
nsAccessibilityService::GetAndroidMonitor().AssertCurrentThreadOwns();
}
return mFocusedRemoteDoc ? mFocusedRemoteDoc->GetFocusedAcc() : nullptr;
#else
if (Accessible* focusedAcc = FocusedLocalAccessible()) {
return focusedAcc;
}
if (!XRE_IsParentProcess()) {
// DocAccessibleParent's don't exist in the content
// process, so we can't return anything useful if this
// is the case.
return nullptr;
}
nsFocusManager* focusManagerDOM = nsFocusManager::GetFocusManager();
if (!focusManagerDOM) {
return nullptr;
}
// If we call GetFocusedBrowsingContext from the chrome process
// it returns the BrowsingContext for the focused _window_, which
// is not helpful here. Instead use GetFocusedBrowsingContextInChrome
// which returns the content BrowsingContext that has focus.
dom::BrowsingContext* focusedContext =
focusManagerDOM->GetFocusedBrowsingContextInChrome();
DocAccessibleParent* focusedDoc =
DocAccessibleParent::GetFrom(focusedContext);
return focusedDoc ? focusedDoc->GetFocusedAcc() : nullptr;
#endif // defined(ANDROID)
}
bool FocusManager::IsFocusWithin(const Accessible* aContainer) const {
Accessible* child = FocusedAccessible();
while (child) {
if (child == aContainer) return true;
child = child->Parent();
}
return false;
}
FocusManager::FocusDisposition FocusManager::IsInOrContainsFocus(
const LocalAccessible* aAccessible) const {
LocalAccessible* focus = FocusedLocalAccessible();
if (!focus) return eNone;
// If focused.
if (focus == aAccessible) return eFocused;
// If contains the focus.
LocalAccessible* child = focus->LocalParent();
while (child) {
if (child == aAccessible) return eContainsFocus;
child = child->LocalParent();
}
// If contained by focus.
child = aAccessible->LocalParent();
while (child) {
if (child == focus) return eContainedByFocus;
child = child->LocalParent();
}
return eNone;
}
bool FocusManager::WasLastFocused(const LocalAccessible* aAccessible) const {
return mLastFocus == aAccessible;
}
void FocusManager::NotifyOfDOMFocus(nsISupports* aTarget) {
#ifdef A11Y_LOG
if (logging::IsEnabled(logging::eFocus)) {
logging::FocusNotificationTarget("DOM focus", "Target", aTarget);
}
#endif
mActiveItem = nullptr;
nsCOMPtr<nsINode> targetNode(do_QueryInterface(aTarget));
if (targetNode) {
DocAccessible* document =
GetAccService()->GetDocAccessible(targetNode->OwnerDoc());
if (document) {
// Set selection listener for focused element.
if (targetNode->IsElement()) {
SelectionMgr()->SetControlSelectionListener(targetNode->AsElement());
}
document->HandleNotification<FocusManager, nsINode>(
this, &FocusManager::ProcessDOMFocus, targetNode);
}
}
}
void FocusManager::NotifyOfDOMBlur(nsISupports* aTarget) {
#ifdef A11Y_LOG
if (logging::IsEnabled(logging::eFocus)) {
logging::FocusNotificationTarget("DOM blur", "Target", aTarget);
}
#endif
mActiveItem = nullptr;
// If DOM document stays focused then fire accessible focus event to process
// the case when no element within this DOM document will be focused.
nsCOMPtr<nsINode> targetNode(do_QueryInterface(aTarget));
if (targetNode && targetNode->OwnerDoc() == FocusedDOMDocument()) {
dom::Document* DOMDoc = targetNode->OwnerDoc();
DocAccessible* document = GetAccService()->GetDocAccessible(DOMDoc);
if (document) {
// Clear selection listener for previously focused element.
if (targetNode->IsElement()) {
SelectionMgr()->ClearControlSelectionListener();
}
document->HandleNotification<FocusManager, nsINode>(
this, &FocusManager::ProcessDOMFocus, DOMDoc);
}
}
}
void FocusManager::ActiveItemChanged(LocalAccessible* aItem,
bool aCheckIfActive) {
#ifdef A11Y_LOG
if (logging::IsEnabled(logging::eFocus)) {
logging::FocusNotificationTarget("active item changed", "Item", aItem);
}
#endif
// Nothing changed, happens for XUL trees and HTML selects.
if (aItem && aItem == mActiveItem) {
return;
}
mActiveItem = nullptr;
if (aItem && aCheckIfActive) {
LocalAccessible* widget = aItem->ContainerWidget();
#ifdef A11Y_LOG
if (logging::IsEnabled(logging::eFocus)) logging::ActiveWidget(widget);
#endif
if (!widget || !widget->IsActiveWidget() || !widget->AreItemsOperable()) {
return;
}
}
mActiveItem = aItem;
// If mActiveItem is null we may need to shift a11y focus back to a remote
// document. For example, when combobox popup is closed, then
// the focus should be moved back to the combobox.
if (!mActiveItem && XRE_IsParentProcess()) {
dom::BrowserParent* browser = dom::BrowserParent::GetFocused();
if (browser) {
a11y::DocAccessibleParent* dap = browser->GetTopLevelDocAccessible();
if (dap) {
(void)dap->SendRestoreFocus();
}
}
}
// If active item is changed then fire accessible focus event on it, otherwise
// if there's no an active item then fire focus event to accessible having
// DOM focus.
LocalAccessible* target = FocusedLocalAccessible();
if (target) {
DispatchFocusEvent(target->Document(), target);
}
}
void FocusManager::ForceFocusEvent() {
nsINode* focusedNode = FocusedDOMNode();
if (focusedNode) {
DocAccessible* document =
GetAccService()->GetDocAccessible(focusedNode->OwnerDoc());
if (document) {
document->HandleNotification<FocusManager, nsINode>(
this, &FocusManager::ProcessDOMFocus, focusedNode);
}
}
}
void FocusManager::DispatchFocusEvent(DocAccessible* aDocument,
LocalAccessible* aTarget) {
MOZ_ASSERT(aDocument, "No document for focused accessible!");
if (aDocument) {
RefPtr<AccEvent> event =
new AccEvent(nsIAccessibleEvent::EVENT_FOCUS, aTarget, eAutoDetect,
AccEvent::eCoalesceOfSameType);
aDocument->FireDelayedEvent(event);
mLastFocus = aTarget;
if (mActiveItem != aTarget) {
// This new focus overrides the stored active item, so clear the active
// item. Among other things, the old active item might die.
mActiveItem = nullptr;
}
#ifdef A11Y_LOG
if (logging::IsEnabled(logging::eFocus)) logging::FocusDispatched(aTarget);
#endif
}
}
void FocusManager::ProcessDOMFocus(nsINode* aTarget) {
#ifdef A11Y_LOG
if (logging::IsEnabled(logging::eFocus)) {
logging::FocusNotificationTarget("process DOM focus", "Target", aTarget);
}
#endif
DocAccessible* document =
GetAccService()->GetDocAccessible(aTarget->OwnerDoc());
if (!document) return;
LocalAccessible* target =
document->GetAccessibleEvenIfNotInMapOrContainer(aTarget);
if (target) {
// Check if still focused. Otherwise we can end up with storing the active
// item for control that isn't focused anymore.
nsINode* focusedNode = FocusedDOMNode();
if (!focusedNode) return;
LocalAccessible* DOMFocus =
document->GetAccessibleEvenIfNotInMapOrContainer(focusedNode);
if (target != DOMFocus) return;
LocalAccessible* activeItem = target->CurrentItem();
if (activeItem) {
mActiveItem = activeItem;
target = activeItem;
}
DispatchFocusEvent(document, target);
}
}
void FocusManager::ProcessFocusEvent(AccEvent* aEvent) {
MOZ_ASSERT(aEvent->GetEventType() == nsIAccessibleEvent::EVENT_FOCUS,
"Focus event is expected!");
// Emit focus event if event target is the active item. Otherwise then check
// if it's still focused and then update active item and emit focus event.
LocalAccessible* target = aEvent->GetAccessible();
MOZ_ASSERT(!target->IsDefunct());
if (target != mActiveItem) {
// Check if still focused. Otherwise we can end up with storing the active
// item for control that isn't focused anymore.
DocAccessible* document = aEvent->Document();
nsINode* focusedNode = FocusedDOMNode();
if (!focusedNode) return;
LocalAccessible* DOMFocus =
document->GetAccessibleEvenIfNotInMapOrContainer(focusedNode);
if (target != DOMFocus) return;
LocalAccessible* activeItem = target->CurrentItem();
if (activeItem) {
mActiveItem = activeItem;
target = activeItem;
MOZ_ASSERT(!target->IsDefunct());
}
}
// Fire menu start/end events for ARIA menus.
if (target->IsARIARole(nsGkAtoms::menuitem)) {
// The focus was moved into menu.
LocalAccessible* ARIAMenubar = nullptr;
for (LocalAccessible* parent = target->LocalParent(); parent;
parent = parent->LocalParent()) {
if (parent->IsARIARole(nsGkAtoms::menubar)) {
ARIAMenubar = parent;
break;
}
// Go up in the parent chain of the menu hierarchy.
if (!parent->IsARIARole(nsGkAtoms::menuitem) &&
!parent->IsARIARole(nsGkAtoms::menu)) {
break;
}
}
if (ARIAMenubar != mActiveARIAMenubar) {
// Leaving ARIA menu. Fire menu_end event on current menubar.
if (mActiveARIAMenubar) {
RefPtr<AccEvent> menuEndEvent =
new AccEvent(nsIAccessibleEvent::EVENT_MENU_END, mActiveARIAMenubar,
aEvent->FromUserInput());
nsEventShell::FireEvent(menuEndEvent);
}
mActiveARIAMenubar = ARIAMenubar;
// Entering ARIA menu. Fire menu_start event.
if (mActiveARIAMenubar) {
RefPtr<AccEvent> menuStartEvent =
new AccEvent(nsIAccessibleEvent::EVENT_MENU_START,
mActiveARIAMenubar, aEvent->FromUserInput());
nsEventShell::FireEvent(menuStartEvent);
}
}
} else if (mActiveARIAMenubar) {
// Focus left a menu. Fire menu_end event.
RefPtr<AccEvent> menuEndEvent =
new AccEvent(nsIAccessibleEvent::EVENT_MENU_END, mActiveARIAMenubar,
aEvent->FromUserInput());
nsEventShell::FireEvent(menuEndEvent);
mActiveARIAMenubar = nullptr;
}
#ifdef A11Y_LOG
if (logging::IsEnabled(logging::eFocus)) {
logging::FocusNotificationTarget("fire focus event", "Target", target);
}
#endif
// Reset cached caret value. The cache will be updated upon processing the
// next caret move event. This ensures that we will return the correct caret
// offset before the caret move event is handled.
SelectionMgr()->ResetCaretOffset();
RefPtr<AccEvent> focusEvent = new AccEvent(nsIAccessibleEvent::EVENT_FOCUS,
target, aEvent->FromUserInput());
nsEventShell::FireEvent(focusEvent);
if (NS_WARN_IF(target->IsDefunct())) {
// target died during nsEventShell::FireEvent.
return;
}
// An anchor jump may have occurred while the document was not focused. This
// could even be before the document was first ever focused. Process it now.
DocAccessible* targetDocument = target->Document();
if (!targetDocument->ProcessAnchorJump()) {
// The anchor jump was ignored due to the focus. That means that this focus
// change overrides the anchor jump, so clear the jump.
targetDocument->SetAnchorJump(nullptr);
}
}
nsINode* FocusManager::FocusedDOMNode() const {
nsFocusManager* DOMFocusManager = nsFocusManager::GetFocusManager();
nsIContent* focusedElm = DOMFocusManager->GetFocusedElement();
nsIFrame* focusedFrame = focusedElm ? focusedElm->GetPrimaryFrame() : nullptr;
// DOM elements retain their focused state when they get styled as display:
// none/content or visibility: hidden. We should treat those cases as if those
// elements were removed, and focus on doc.
if (focusedFrame && focusedFrame->StyleVisibility()->IsVisible()) {
// Print preview documents don't get DocAccessibles, but we still want a11y
// focus to go somewhere useful. Therefore, we allow a11y focus to land on
// the OuterDocAccessible in this case.
// Note that this code only handles remote print preview documents.
if (EventStateManager::IsTopLevelRemoteTarget(focusedElm) &&
focusedElm->AsElement()->HasAttribute(u"printpreview"_ns)) {
return focusedElm;
}
// No focus on remote target elements like xul:browser having DOM focus and
// residing in chrome process because it means an element in content process
// keeps the focus. Similarly, suppress focus on OOP iframes because an
// element in another content process should now have the focus.
if (EventStateManager::IsRemoteTarget(focusedElm)) {
return nullptr;
}
return focusedElm;
}
// Otherwise the focus can be on DOM document.
dom::BrowsingContext* context = DOMFocusManager->GetFocusedBrowsingContext();
if (context) {
// GetDocShell will return null if the document isn't in our process.
nsIDocShell* shell = context->GetDocShell();
if (shell) {
return shell->GetDocument();
}
}
// Focus isn't in this process.
return nullptr;
}
dom::Document* FocusManager::FocusedDOMDocument() const {
nsINode* focusedNode = FocusedDOMNode();
return focusedNode ? focusedNode->OwnerDoc() : nullptr;
}
} // namespace a11y
} // namespace mozilla
|