Source code

Revision control

Other Tools

/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* 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 "LocalAccessible-inl.h"
#include "AccIterator.h"
#include "AccAttributes.h"
#include "DocAccessible-inl.h"
#include "DocAccessibleChild.h"
#include "HTMLImageMapAccessible.h"
#include "nsAccCache.h"
#include "nsAccessiblePivot.h"
#include "nsAccUtils.h"
#include "nsDeckFrame.h"
#include "nsEventShell.h"
#include "nsLayoutUtils.h"
#include "nsTextEquivUtils.h"
#include "Pivot.h"
#include "Role.h"
#include "RootAccessible.h"
#include "TreeWalker.h"
#include "xpcAccessibleDocument.h"
#include "nsCommandManager.h"
#include "nsContentUtils.h"
#include "nsIDocShell.h"
#include "mozilla/dom/Document.h"
#include "nsPIDOMWindow.h"
#include "nsIContentInlines.h"
#include "nsIEditingSession.h"
#include "nsIFrame.h"
#include "nsIInterfaceRequestorUtils.h"
#include "nsImageFrame.h"
#include "nsViewManager.h"
#include "nsIScrollableFrame.h"
#include "nsUnicharUtils.h"
#include "nsIURI.h"
#include "nsIWebNavigation.h"
#include "nsFocusManager.h"
#include "nsTHashSet.h"
#include "mozilla/ArrayUtils.h"
#include "mozilla/Assertions.h"
#include "mozilla/EditorBase.h"
#include "mozilla/EventStates.h"
#include "mozilla/HTMLEditor.h"
#include "mozilla/PresShell.h"
#include "mozilla/StaticPrefs_accessibility.h"
#include "mozilla/dom/AncestorIterator.h"
#include "mozilla/dom/BrowserChild.h"
#include "mozilla/dom/DocumentType.h"
#include "mozilla/dom/Element.h"
#include "mozilla/dom/HTMLSelectElement.h"
#include "mozilla/dom/MutationEventBinding.h"
#include "mozilla/dom/UserActivation.h"
using namespace mozilla;
using namespace mozilla::a11y;
////////////////////////////////////////////////////////////////////////////////
// Static member initialization
static nsStaticAtom* const kRelationAttrs[] = {nsGkAtoms::aria_labelledby,
nsGkAtoms::aria_describedby,
nsGkAtoms::aria_details,
nsGkAtoms::aria_owns,
nsGkAtoms::aria_controls,
nsGkAtoms::aria_flowto,
nsGkAtoms::aria_errormessage,
nsGkAtoms::_for,
nsGkAtoms::control};
static const uint32_t kRelationAttrsLen = ArrayLength(kRelationAttrs);
////////////////////////////////////////////////////////////////////////////////
// Constructor/desctructor
DocAccessible::DocAccessible(dom::Document* aDocument,
PresShell* aPresShell)
: // XXX don't pass a document to the LocalAccessible constructor so that
// we don't set mDoc until our vtable is fully setup. If we set mDoc
// before setting up the vtable we will call LocalAccessible::AddRef()
// but not the overrides of it for subclasses. It is important to call
// those overrides to avoid confusing leak checking machinary.
HyperTextAccessibleWrap(nullptr, nullptr),
// XXX aaronl should we use an algorithm for the initial cache size?
mAccessibleCache(kDefaultCacheLength),
mNodeToAccessibleMap(kDefaultCacheLength),
mDocumentNode(aDocument),
mLoadState(eTreeConstructionPending),
mDocFlags(0),
mLoadEventType(0),
mPrevStateBits(0),
mVirtualCursor(nullptr),
mPresShell(aPresShell),
mIPCDoc(nullptr) {
mGenericTypes |= eDocument;
mStateFlags |= eNotNodeMapEntry;
mDoc = this;
MOZ_ASSERT(mPresShell, "should have been given a pres shell");
mPresShell->SetDocAccessible(this);
}
DocAccessible::~DocAccessible() {
NS_ASSERTION(!mPresShell, "LastRelease was never called!?!");
}
////////////////////////////////////////////////////////////////////////////////
// nsISupports
NS_IMPL_CYCLE_COLLECTION_CLASS(DocAccessible)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(DocAccessible,
LocalAccessible)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mNotificationController)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mVirtualCursor)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mChildDocuments)
for (const auto& hashEntry : tmp->mDependentIDsHashes.Values()) {
for (const auto& providers : hashEntry->Values()) {
for (int32_t provIdx = providers->Length() - 1; provIdx >= 0; provIdx--) {
NS_CYCLE_COLLECTION_NOTE_EDGE_NAME(
cb, "content of dependent ids hash entry of document accessible");
const auto& provider = (*providers)[provIdx];
cb.NoteXPCOMChild(provider->mContent);
}
}
}
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mAccessibleCache)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mAnchorJumpElm)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mInvalidationList)
for (const auto& ar : tmp->mARIAOwnsHash.Values()) {
for (uint32_t i = 0; i < ar->Length(); i++) {
NS_CYCLE_COLLECTION_NOTE_EDGE_NAME(cb, "mARIAOwnsHash entry item");
cb.NoteXPCOMChild(ar->ElementAt(i));
}
}
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(DocAccessible, LocalAccessible)
NS_IMPL_CYCLE_COLLECTION_UNLINK(mNotificationController)
NS_IMPL_CYCLE_COLLECTION_UNLINK(mVirtualCursor)
NS_IMPL_CYCLE_COLLECTION_UNLINK(mChildDocuments)
tmp->mDependentIDsHashes.Clear();
tmp->mNodeToAccessibleMap.Clear();
NS_IMPL_CYCLE_COLLECTION_UNLINK(mAccessibleCache)
NS_IMPL_CYCLE_COLLECTION_UNLINK(mAnchorJumpElm)
NS_IMPL_CYCLE_COLLECTION_UNLINK(mInvalidationList)
NS_IMPL_CYCLE_COLLECTION_UNLINK_WEAK_REFERENCE
tmp->mARIAOwnsHash.Clear();
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(DocAccessible)
NS_INTERFACE_MAP_ENTRY(nsIDocumentObserver)
NS_INTERFACE_MAP_ENTRY(nsIMutationObserver)
NS_INTERFACE_MAP_ENTRY(nsISupportsWeakReference)
NS_INTERFACE_MAP_ENTRY(nsIObserver)
NS_INTERFACE_MAP_ENTRY(nsIAccessiblePivotObserver)
NS_INTERFACE_MAP_END_INHERITING(HyperTextAccessible)
NS_IMPL_ADDREF_INHERITED(DocAccessible, HyperTextAccessible)
NS_IMPL_RELEASE_INHERITED(DocAccessible, HyperTextAccessible)
////////////////////////////////////////////////////////////////////////////////
// nsIAccessible
ENameValueFlag DocAccessible::Name(nsString& aName) const {
aName.Truncate();
if (mParent) {
mParent->Name(aName); // Allow owning iframe to override the name
}
if (aName.IsEmpty()) {
// Allow name via aria-labelledby or title attribute
LocalAccessible::Name(aName);
}
if (aName.IsEmpty()) {
Title(aName); // Try title element
}
if (aName.IsEmpty()) { // Last resort: use URL
URL(aName);
}
return eNameOK;
}
// LocalAccessible public method
role DocAccessible::NativeRole() const {
nsCOMPtr<nsIDocShell> docShell = nsCoreUtils::GetDocShellFor(mDocumentNode);
if (docShell) {
nsCOMPtr<nsIDocShellTreeItem> sameTypeRoot;
docShell->GetInProcessSameTypeRootTreeItem(getter_AddRefs(sameTypeRoot));
int32_t itemType = docShell->ItemType();
if (sameTypeRoot == docShell) {
// Root of content or chrome tree
if (itemType == nsIDocShellTreeItem::typeChrome) {
return roles::CHROME_WINDOW;
}
if (itemType == nsIDocShellTreeItem::typeContent) {
return roles::DOCUMENT;
}
} else if (itemType == nsIDocShellTreeItem::typeContent) {
return roles::DOCUMENT;
}
}
return roles::PANE; // Fall back;
}
void DocAccessible::Description(nsString& aDescription) const {
if (mParent) mParent->Description(aDescription);
if (HasOwnContent() && aDescription.IsEmpty()) {
nsTextEquivUtils::GetTextEquivFromIDRefs(this, nsGkAtoms::aria_describedby,
aDescription);
}
}
// LocalAccessible public method
uint64_t DocAccessible::NativeState() const {
// Document is always focusable.
uint64_t state =
states::FOCUSABLE; // keep in sync with NativeInteractiveState() impl
if (FocusMgr()->IsFocused(this)) state |= states::FOCUSED;
// Expose stale state until the document is ready (DOM is loaded and tree is
// constructed).
if (!HasLoadState(eReady)) state |= states::STALE;
// Expose state busy until the document and all its subdocuments is completely
// loaded.
if (!HasLoadState(eCompletelyLoaded)) state |= states::BUSY;
nsIFrame* frame = GetFrame();
if (!frame || !frame->IsVisibleConsideringAncestors(
nsIFrame::VISIBILITY_CROSS_CHROME_CONTENT_BOUNDARY)) {
state |= states::INVISIBLE | states::OFFSCREEN;
}
RefPtr<EditorBase> editorBase = GetEditor();
state |= editorBase ? states::EDITABLE : states::READONLY;
return state;
}
uint64_t DocAccessible::NativeInteractiveState() const {
// Document is always focusable.
return states::FOCUSABLE;
}
bool DocAccessible::NativelyUnavailable() const { return false; }
// LocalAccessible public method
void DocAccessible::ApplyARIAState(uint64_t* aState) const {
// Grab states from content element.
if (mContent) LocalAccessible::ApplyARIAState(aState);
// Allow iframe/frame etc. to have final state override via ARIA.
if (mParent) mParent->ApplyARIAState(aState);
}
already_AddRefed<AccAttributes> DocAccessible::Attributes() {
RefPtr<AccAttributes> attributes = HyperTextAccessibleWrap::Attributes();
// No attributes if document is not attached to the tree or if it's a root
// document.
if (!mParent || IsRoot()) return attributes.forget();
// Override ARIA object attributes from outerdoc.
aria::AttrIterator attribIter(mParent->GetContent());
while (attribIter.Next()) {
nsString value;
attribIter.AttrValue(value);
attributes->SetAttribute(attribIter.AttrName(), std::move(value));
}
return attributes.forget();
}
LocalAccessible* DocAccessible::FocusedChild() {
// Return an accessible for the current global focus, which does not have to
// be contained within the current document.
return FocusMgr()->FocusedAccessible();
}
void DocAccessible::TakeFocus() const {
// Focus the document.
nsFocusManager* fm = nsFocusManager::GetFocusManager();
RefPtr<dom::Element> newFocus;
dom::AutoHandlingUserInputStatePusher inputStatePusher(true);
fm->MoveFocus(mDocumentNode->GetWindow(), nullptr,
nsFocusManager::MOVEFOCUS_ROOT, 0, getter_AddRefs(newFocus));
}
// HyperTextAccessible method
already_AddRefed<EditorBase> DocAccessible::GetEditor() const {
// Check if document is editable (designMode="on" case). Otherwise check if
// the html:body (for HTML document case) or document element is editable.
if (!mDocumentNode->IsInDesignMode() &&
(!mContent || !mContent->HasFlag(NODE_IS_EDITABLE))) {
return nullptr;
}
nsCOMPtr<nsIDocShell> docShell = mDocumentNode->GetDocShell();
if (!docShell) {
return nullptr;
}
nsCOMPtr<nsIEditingSession> editingSession;
docShell->GetEditingSession(getter_AddRefs(editingSession));
if (!editingSession) return nullptr; // No editing session interface
RefPtr<HTMLEditor> htmlEditor =
editingSession->GetHTMLEditorForWindow(mDocumentNode->GetWindow());
if (!htmlEditor) {
return nullptr;
}
bool isEditable = false;
htmlEditor->GetIsDocumentEditable(&isEditable);
if (isEditable) {
return htmlEditor.forget();
}
return nullptr;
}
// DocAccessible public method
void DocAccessible::URL(nsAString& aURL) const {
nsCOMPtr<nsISupports> container = mDocumentNode->GetContainer();
nsCOMPtr<nsIWebNavigation> webNav(do_GetInterface(container));
nsAutoCString theURL;
if (webNav) {
nsCOMPtr<nsIURI> pURI;
webNav->GetCurrentURI(getter_AddRefs(pURI));
if (pURI) pURI->GetSpec(theURL);
}
CopyUTF8toUTF16(theURL, aURL);
}
void DocAccessible::Title(nsString& aTitle) const {
mDocumentNode->GetTitle(aTitle);
}
void DocAccessible::MimeType(nsAString& aType) const {
mDocumentNode->GetContentType(aType);
}
void DocAccessible::DocType(nsAString& aType) const {
dom::DocumentType* docType = mDocumentNode->GetDoctype();
if (docType) docType->GetPublicId(aType);
}
////////////////////////////////////////////////////////////////////////////////
// LocalAccessible
void DocAccessible::Init() {
#ifdef A11Y_LOG
if (logging::IsEnabled(logging::eDocCreate)) {
logging::DocCreate("document initialize", mDocumentNode, this);
}
#endif
// Initialize notification controller.
mNotificationController = new NotificationController(this, mPresShell);
// Mark the DocAccessible as loaded if its DOM document is already loaded at
// this point. This can happen for one of three reasons:
// 1. A11y was started late.
// 2. DOM loading for a document (probably an in-process iframe) completed
// before its Accessible container was created.
// 3. The PresShell for the document was created after DOM loading completed.
// In that case, we tried to create the DocAccessible when DOM loading
// completed, but we can't create a DocAccessible without a PresShell, so
// this failed. The DocAccessible was subsequently created due to a layout
// notification.
if (mDocumentNode->GetReadyStateEnum() ==
dom::Document::READYSTATE_COMPLETE) {
mLoadState |= eDOMLoaded;
// If this happened due to reasons 1 or 2, it isn't *necessary* to fire a
// doc load complete event. If it happened due to reason 3, we need to fire
// doc load complete because clients (especially tests) might be waiting
// for the document to load using this event. We can't distinguish why this
// happened at this point, so just fire it regardless. It won't do any
// harm even if it isn't necessary. We set mLoadEventType here and it will
// be fired in ProcessLoad as usual.
mLoadEventType = nsIAccessibleEvent::EVENT_DOCUMENT_LOAD_COMPLETE;
}
AddEventListeners();
}
void DocAccessible::Shutdown() {
if (!mPresShell) { // already shutdown
return;
}
#ifdef A11Y_LOG
if (logging::IsEnabled(logging::eDocDestroy)) {
logging::DocDestroy("document shutdown", mDocumentNode, this);
}
#endif
// Mark the document as shutdown before AT is notified about the document
// removal from its container (valid for root documents on ATK and due to
// some reason for MSAA, refer to bug 757392 for details).
mStateFlags |= eIsDefunct;
if (mNotificationController) {
mNotificationController->Shutdown();
mNotificationController = nullptr;
}
RemoveEventListeners();
// mParent->RemoveChild clears mParent, but we need to know whether we were a
// child later, so use a flag.
const bool isChild = !!mParent;
if (mParent) {
DocAccessible* parentDocument = mParent->Document();
if (parentDocument) parentDocument->RemoveChildDocument(this);
mParent->RemoveChild(this);
MOZ_ASSERT(!mParent, "Parent has to be null!");
}
mPresShell->SetDocAccessible(nullptr);
mPresShell = nullptr; // Avoid reentrancy
// Walk the array backwards because child documents remove themselves from the
// array as they are shutdown.
int32_t childDocCount = mChildDocuments.Length();
for (int32_t idx = childDocCount - 1; idx >= 0; idx--) {
mChildDocuments[idx]->Shutdown();
}
mChildDocuments.Clear();
// XXX thinking about ordering?
if (mIPCDoc) {
MOZ_ASSERT(IPCAccessibilityActive());
mIPCDoc->Shutdown();
MOZ_ASSERT(!mIPCDoc);
}
if (mVirtualCursor) {
mVirtualCursor->RemoveObserver(this);
mVirtualCursor = nullptr;
}
mDependentIDsHashes.Clear();
mNodeToAccessibleMap.Clear();
mAnchorJumpElm = nullptr;
mInvalidationList.Clear();
for (auto iter = mAccessibleCache.Iter(); !iter.Done(); iter.Next()) {
LocalAccessible* accessible = iter.Data();
MOZ_ASSERT(accessible);
if (accessible && !accessible->IsDefunct()) {
// Unlink parent to avoid its cleaning overhead in shutdown.
accessible->mParent = nullptr;
accessible->Shutdown();
}
iter.Remove();
}
HyperTextAccessibleWrap::Shutdown();
MOZ_ASSERT(GetAccService());
GetAccService()->NotifyOfDocumentShutdown(
this, mDocumentNode,
// Make sure we don't shut down AccService while a parent document is
// still shutting down. The parent will allow service shutdown when it
// reaches this point.
/* aAllowServiceShutdown */ !isChild);
mDocumentNode = nullptr;
}
nsIFrame* DocAccessible::GetFrame() const {
nsIFrame* root = nullptr;
if (mPresShell) {
root = mPresShell->GetRootFrame();
}
return root;
}
nsINode* DocAccessible::GetNode() const { return mDocumentNode; }
// DocAccessible protected member
nsRect DocAccessible::RelativeBounds(nsIFrame** aRelativeFrame) const {
*aRelativeFrame = GetFrame();
dom::Document* document = mDocumentNode;
dom::Document* parentDoc = nullptr;
nsRect bounds;
while (document) {
PresShell* presShell = document->GetPresShell();
if (!presShell) {
return nsRect();
}
nsRect scrollPort;
nsIScrollableFrame* sf = presShell->GetRootScrollFrameAsScrollable();
if (sf) {
scrollPort = sf->GetScrollPortRect();
} else {
nsIFrame* rootFrame = presShell->GetRootFrame();
if (!rootFrame) return nsRect();
scrollPort = rootFrame->GetRect();
}
if (parentDoc) { // After first time thru loop
// XXXroc bogus code! scrollPort is relative to the viewport of
// this document, but we're intersecting rectangles derived from
// multiple documents and assuming they're all in the same coordinate
// system. See bug 514117.
bounds.IntersectRect(scrollPort, bounds);
} else { // First time through loop
bounds = scrollPort;
}
document = parentDoc = document->GetInProcessParentDocument();
}
return bounds;
}
// DocAccessible protected member
nsresult DocAccessible::AddEventListeners() {
nsCOMPtr<nsIDocShell> docShell(mDocumentNode->GetDocShell());
// We want to add a command observer only if the document is content and has
// an editor.
if (docShell->ItemType() == nsIDocShellTreeItem::typeContent) {
RefPtr<nsCommandManager> commandManager = docShell->GetCommandManager();
if (commandManager) {
commandManager->AddCommandObserver(this, "obs_documentCreated");
}
}
SelectionMgr()->AddDocSelectionListener(mPresShell);
// Add document observer.
mDocumentNode->AddObserver(this);
return NS_OK;
}
// DocAccessible protected member
nsresult DocAccessible::RemoveEventListeners() {
// Remove listeners associated with content documents
NS_ASSERTION(mDocumentNode, "No document during removal of listeners.");
if (mDocumentNode) {
mDocumentNode->RemoveObserver(this);
nsCOMPtr<nsIDocShell> docShell(mDocumentNode->GetDocShell());
NS_ASSERTION(docShell, "doc should support nsIDocShellTreeItem.");
if (docShell) {
if (docShell->ItemType() == nsIDocShellTreeItem::typeContent) {
RefPtr<nsCommandManager> commandManager = docShell->GetCommandManager();
if (commandManager) {
commandManager->RemoveCommandObserver(this, "obs_documentCreated");
}
}
}
}
if (mScrollWatchTimer) {
mScrollWatchTimer->Cancel();
mScrollWatchTimer = nullptr;
NS_RELEASE_THIS(); // Kung fu death grip
}
SelectionMgr()->RemoveDocSelectionListener(mPresShell);
return NS_OK;
}
void DocAccessible::ScrollTimerCallback(nsITimer* aTimer, void* aClosure) {
DocAccessible* docAcc = reinterpret_cast<DocAccessible*>(aClosure);
if (docAcc) {
// Dispatch a scroll-end for all entries in table. They have not
// been scrolled in at least `kScrollEventInterval`.
for (auto iter = docAcc->mLastScrollingDispatch.Iter(); !iter.Done();
iter.Next()) {
docAcc->DispatchScrollingEvent(iter.Key(),
nsIAccessibleEvent::EVENT_SCROLLING_END);
iter.Remove();
}
if (docAcc->mScrollWatchTimer) {
docAcc->mScrollWatchTimer = nullptr;
NS_RELEASE(docAcc); // Release kung fu death grip
}
}
}
void DocAccessible::HandleScroll(nsINode* aTarget) {
const uint32_t kScrollEventInterval = 100;
// If we haven't dispatched a scrolling event for a target in at least
// kScrollEventInterval milliseconds, dispatch one now.
mLastScrollingDispatch.WithEntryHandle(aTarget, [&](auto&& lastDispatch) {
const TimeStamp now = TimeStamp::Now();
if (!lastDispatch ||
(now - lastDispatch.Data()).ToMilliseconds() >= kScrollEventInterval) {
// We can't fire events on a document whose tree isn't constructed yet.
if (HasLoadState(eTreeConstructed)) {
DispatchScrollingEvent(aTarget, nsIAccessibleEvent::EVENT_SCROLLING);
}
lastDispatch.InsertOrUpdate(now);
}
});
// If timer callback is still pending, push it 100ms into the future.
// When scrolling ends and we don't fire this callback anymore, the
// timer callback will fire and dispatch an EVENT_SCROLLING_END.
if (mScrollWatchTimer) {
mScrollWatchTimer->SetDelay(kScrollEventInterval);
} else {
NS_NewTimerWithFuncCallback(getter_AddRefs(mScrollWatchTimer),
ScrollTimerCallback, this, kScrollEventInterval,
nsITimer::TYPE_ONE_SHOT,
"a11y::DocAccessible::ScrollPositionDidChange");
if (mScrollWatchTimer) {
NS_ADDREF_THIS(); // Kung fu death grip
}
}
}
////////////////////////////////////////////////////////////////////////////////
// nsIObserver
NS_IMETHODIMP
DocAccessible::Observe(nsISupports* aSubject, const char* aTopic,
const char16_t* aData) {
if (!nsCRT::strcmp(aTopic, "obs_documentCreated")) {
// State editable will now be set, readonly is now clear
// Normally we only fire delayed events created from the node, not an
// accessible object. See the AccStateChangeEvent constructor for details
// about this exceptional case.
RefPtr<AccEvent> event =
new AccStateChangeEvent(this, states::EDITABLE, true);
FireDelayedEvent(event);
}
return NS_OK;
}
////////////////////////////////////////////////////////////////////////////////
// nsIAccessiblePivotObserver
NS_IMETHODIMP
DocAccessible::OnPivotChanged(nsIAccessiblePivot* aPivot,
nsIAccessible* aOldAccessible, int32_t aOldStart,
int32_t aOldEnd, nsIAccessible* aNewAccessible,
int32_t aNewStart, int32_t aNewEnd,
PivotMoveReason aReason,
TextBoundaryType aBoundaryType,
bool aIsFromUserInput) {
RefPtr<AccEvent> event = new AccVCChangeEvent(
this, (aOldAccessible ? aOldAccessible->ToInternalAccessible() : nullptr),
aOldStart, aOldEnd,
(aNewAccessible ? aNewAccessible->ToInternalAccessible() : nullptr),
aNewStart, aNewEnd, aReason, aBoundaryType,
aIsFromUserInput ? eFromUserInput : eNoUserInput);
nsEventShell::FireEvent(event);
return NS_OK;
}
////////////////////////////////////////////////////////////////////////////////
// nsIDocumentObserver
NS_IMPL_NSIDOCUMENTOBSERVER_CORE_STUB(DocAccessible)
NS_IMPL_NSIDOCUMENTOBSERVER_LOAD_STUB(DocAccessible)
void DocAccessible::AttributeWillChange(dom::Element* aElement,
int32_t aNameSpaceID,
nsAtom* aAttribute, int32_t aModType) {
LocalAccessible* accessible = GetAccessible(aElement);
if (!accessible) {
if (aElement != mContent) return;
accessible = this;
}
// Update dependent IDs cache. Take care of elements that are accessible
// because dependent IDs cache doesn't contain IDs from non accessible
// elements.
if (aModType != dom::MutationEvent_Binding::ADDITION) {
RemoveDependentIDsFor(accessible, aAttribute);
}
if (aAttribute == nsGkAtoms::id) {
RelocateARIAOwnedIfNeeded(aElement);
}
// If attribute affects accessible's state, store the old state so we can
// later compare it against the state of the accessible after the attribute
// change.
if (accessible->AttributeChangesState(aAttribute)) {
mPrevStateBits = accessible->State();
} else {
mPrevStateBits = 0;
}
}
void DocAccessible::NativeAnonymousChildListChange(nsIContent* aContent,
bool aIsRemove) {
if (aIsRemove) {
#ifdef A11Y_LOG
if (logging::IsEnabled(logging::eTree)) {
logging::MsgBegin("TREE", "Anonymous content removed; doc: %p", this);
logging::Node("node", aContent);
logging::MsgEnd();
}
#endif
ContentRemoved(aContent);
}
}
void DocAccessible::AttributeChanged(dom::Element* aElement,
int32_t aNameSpaceID, nsAtom* aAttribute,
int32_t aModType,
const nsAttrValue* aOldValue) {
NS_ASSERTION(!IsDefunct(),
"Attribute changed called on defunct document accessible!");
// Proceed even if the element is not accessible because element may become
// accessible if it gets certain attribute.
if (UpdateAccessibleOnAttrChange(aElement, aAttribute)) return;
// Update the accessible tree on aria-hidden change. Make sure to not create
// a tree under aria-hidden='true'.
if (aAttribute == nsGkAtoms::aria_hidden) {
if (aria::HasDefinedARIAHidden(aElement)) {
ContentRemoved(aElement);
} else {
ContentInserted(aElement, aElement->GetNextSibling());
}
return;
}
// Ignore attribute change if the element doesn't have an accessible (at all
// or still) if the element is not a root content of this document accessible
// (which is treated as attribute change on this document accessible).
// Note: we don't bail if all the content hasn't finished loading because
// these attributes are changing for a loaded part of the content.
LocalAccessible* accessible = GetAccessible(aElement);
if (!accessible) {
if (mContent != aElement) return;
accessible = this;
}
MOZ_ASSERT(accessible->IsBoundToParent() || accessible->IsDoc(),
"DOM attribute change on an accessible detached from the tree");
if (aAttribute == nsGkAtoms::id) {
dom::Element* elm = accessible->Elm();
RelocateARIAOwnedIfNeeded(elm);
ARIAActiveDescendantIDMaybeMoved(elm);
accessible->SendCache(CacheDomain::DOMNodeID, CacheUpdateType::Update);
}
// The activedescendant universal property redirects accessible focus events
// to the element with the id that activedescendant points to. Make sure
// the tree up to date before processing. In other words, when a node has just
// been inserted, the tree won't be up to date yet, so we must always schedule
// an async notification so that a newly inserted node will be present in
// the tree.
if (aAttribute == nsGkAtoms::aria_activedescendant) {
mNotificationController
->ScheduleNotification<DocAccessible, LocalAccessible>(
this, &DocAccessible::ARIAActiveDescendantChanged, accessible);
return;
}
// Defer to accessible any needed actions like changing states or emiting
// events.
accessible->DOMAttributeChanged(aNameSpaceID, aAttribute, aModType, aOldValue,
mPrevStateBits);
// Update dependent IDs cache. We handle elements with accessibles.
// If the accessible or element with the ID doesn't exist yet the cache will
// be updated when they are added.
if (aModType == dom::MutationEvent_Binding::MODIFICATION ||
aModType == dom::MutationEvent_Binding::ADDITION) {
AddDependentIDsFor(accessible, aAttribute);
}
}
void DocAccessible::ARIAActiveDescendantChanged(LocalAccessible* aAccessible) {
nsIContent* elm = aAccessible->GetContent();
if (elm && elm->IsElement() && aAccessible->IsActiveWidget()) {
nsAutoString id;
if (elm->AsElement()->GetAttr(kNameSpaceID_None,
nsGkAtoms::aria_activedescendant, id)) {
dom::Element* activeDescendantElm = IDRefsIterator::GetElem(elm, id);
if (activeDescendantElm) {
LocalAccessible* activeDescendant = GetAccessible(activeDescendantElm);
if (activeDescendant) {
FocusMgr()->ActiveItemChanged(activeDescendant, false);
#ifdef A11Y_LOG
if (logging::IsEnabled(logging::eFocus)) {
logging::ActiveItemChangeCausedBy("ARIA activedescedant changed",
activeDescendant);
}
#endif
return;
}
}
}
// aria-activedescendant was cleared or changed to a non-existent node.
// Move focus back to the element itself.
FocusMgr()->ActiveItemChanged(aAccessible, false);
#ifdef A11Y_LOG
if (logging::IsEnabled(logging::eFocus)) {
logging::ActiveItemChangeCausedBy("ARIA activedescedant cleared",
aAccessible);
}
#endif
}
}
void DocAccessible::ContentAppended(nsIContent* aFirstNewContent) {}
void DocAccessible::ContentStateChanged(dom::Document* aDocument,
nsIContent* aContent,
EventStates aStateMask) {
LocalAccessible* accessible = GetAccessible(aContent);
if (!accessible) return;
if (aStateMask.HasState(NS_EVENT_STATE_CHECKED)) {
LocalAccessible* widget = accessible->ContainerWidget();
if (widget && widget->IsSelect()) {
AccSelChangeEvent::SelChangeType selChangeType =
aContent->AsElement()->State().HasState(NS_EVENT_STATE_CHECKED)
? AccSelChangeEvent::eSelectionAdd
: AccSelChangeEvent::eSelectionRemove;
RefPtr<AccEvent> event =
new AccSelChangeEvent(widget, accessible, selChangeType);
FireDelayedEvent(event);
return;
}
RefPtr<AccEvent> event = new AccStateChangeEvent(
accessible, states::CHECKED,
aContent->AsElement()->State().HasState(NS_EVENT_STATE_CHECKED));
FireDelayedEvent(event);
}
if (aStateMask.HasState(NS_EVENT_STATE_INVALID)) {
RefPtr<AccEvent> event =
new AccStateChangeEvent(accessible, states::INVALID, true);
FireDelayedEvent(event);
}
if (aStateMask.HasState(NS_EVENT_STATE_REQUIRED)) {
RefPtr<AccEvent> event =
new AccStateChangeEvent(accessible, states::REQUIRED);
FireDelayedEvent(event);
}
if (aStateMask.HasState(NS_EVENT_STATE_VISITED)) {
RefPtr<AccEvent> event =
new AccStateChangeEvent(accessible, states::TRAVERSED, true);
FireDelayedEvent(event);
}
if (aStateMask.HasState(NS_EVENT_STATE_DEFAULT)) {
RefPtr<AccEvent> event =
new AccStateChangeEvent(accessible, states::DEFAULT);
FireDelayedEvent(event);
}
}
void DocAccessible::CharacterDataWillChange(nsIContent* aContent,
const CharacterDataChangeInfo&) {}
void DocAccessible::CharacterDataChanged(nsIContent* aContent,
const CharacterDataChangeInfo&) {}
void DocAccessible::ContentInserted(nsIContent* aChild) {}
void DocAccessible::ContentRemoved(nsIContent* aChildNode,
nsIContent* aPreviousSiblingNode) {
#ifdef A11Y_LOG
if (logging::IsEnabled(logging::eTree)) {
logging::MsgBegin("TREE", "DOM content removed; doc: %p", this);
logging::Node("container node", aChildNode->GetParent());
logging::Node("content node", aChildNode);
logging::MsgEnd();
}
#endif
// This one and content removal notification from layout may result in
// double processing of same subtrees. If it pops up in profiling, then
// consider reusing a document node cache to reject these notifications early.
ContentRemoved(aChildNode);
}
void DocAccessible::MarkForBoundsProcessing(LocalAccessible* aAcc) {
mMaybeBoundsChanged.EnsureInserted(aAcc);
}
void DocAccessible::ParentChainChanged(nsIContent* aContent) {}
////////////////////////////////////////////////////////////////////////////////
// LocalAccessible
#ifdef A11Y_LOG
nsresult DocAccessible::HandleAccEvent(AccEvent* aEvent) {
if (logging::IsEnabled(logging::eDocLoad)) {
logging::DocLoadEventHandled(aEvent);
}
return HyperTextAccessible::HandleAccEvent(aEvent);
}
#endif
////////////////////////////////////////////////////////////////////////////////
// Public members
nsPresContext* DocAccessible::PresContext() const {
return mPresShell->GetPresContext();
}
void* DocAccessible::GetNativeWindow() const {
if (!mPresShell) {
return nullptr;
}
nsViewManager* vm = mPresShell->GetViewManager();
if (!vm) return nullptr;
nsCOMPtr<nsIWidget> widget = vm->GetRootWidget();
if (widget) return widget->GetNativeData(NS_NATIVE_WINDOW);
return nullptr;
}
LocalAccessible* DocAccessible::GetAccessibleByUniqueIDInSubtree(
void* aUniqueID) {
LocalAccessible* child = GetAccessibleByUniqueID(aUniqueID);
if (child) return child;
uint32_t childDocCount = mChildDocuments.Length();
for (uint32_t childDocIdx = 0; childDocIdx < childDocCount; childDocIdx++) {
DocAccessible* childDocument = mChildDocuments.ElementAt(childDocIdx);
child = childDocument->GetAccessibleByUniqueIDInSubtree(aUniqueID);
if (child) return child;
}
return nullptr;
}
LocalAccessible* DocAccessible::GetAccessibleOrContainer(
nsINode* aNode, bool aNoContainerIfPruned) const {
if (!aNode || !aNode->GetComposedDoc()) {
return nullptr;
}
nsINode* start = aNode;
if (auto* shadowRoot = dom::ShadowRoot::FromNode(aNode)) {
// This can happen, for example, when called within
// SelectionManager::ProcessSelectionChanged due to focusing a direct
// child of a shadow root.
// GetFlattenedTreeParent works on children of a shadow root, but not the
// shadow root itself.
start = shadowRoot->GetHost();
if (!start) {
return nullptr;
}
}
for (nsINode* currNode : dom::InclusiveFlatTreeAncestors(*start)) {
// No container if is inside of aria-hidden subtree.
if (aNoContainerIfPruned && currNode->IsElement() &&
aria::HasDefinedARIAHidden(currNode->AsElement())) {
return nullptr;
}
// Check if node is in an unselected deck panel
if (aNoContainerIfPruned && currNode->IsXULElement()) {
if (nsIFrame* frame = currNode->AsContent()->GetPrimaryFrame()) {
nsDeckFrame* deckFrame = do_QueryFrame(frame->GetParent());
if (deckFrame && deckFrame->GetSelectedBox() != frame) {
// If deck is not a <tabpanels>, return null
nsIContent* parentFrameContent = deckFrame->GetContent();
if (!parentFrameContent ||
!parentFrameContent->IsXULElement(nsGkAtoms::tabpanels)) {
return nullptr;
}
}
}
}
// Check if node is in zero-sized map
if (aNoContainerIfPruned && currNode->IsHTMLElement(nsGkAtoms::map)) {
if (nsIFrame* frame = currNode->AsContent()->GetPrimaryFrame()) {
if (nsLayoutUtils::GetAllInFlowRectsUnion(frame, frame->GetParent())
.IsEmpty()) {
return nullptr;
}
}
}
if (LocalAccessible* accessible = GetAccessible(currNode)) {
return accessible;
}
}
return nullptr;
}
LocalAccessible* DocAccessible::GetContainerAccessible(nsINode* aNode) const {
return aNode ? GetAccessibleOrContainer(aNode->GetFlattenedTreeParentNode())
: nullptr;
}
LocalAccessible* DocAccessible::GetAccessibleOrDescendant(
nsINode* aNode) const {
LocalAccessible* acc = GetAccessible(aNode);
if (acc) return acc;
if (aNode == mContent || aNode == mDocumentNode->GetRootElement()) {
// If the node is the doc's body or root element, return the doc accessible.
return const_cast<DocAccessible*>(this);
}
acc = GetContainerAccessible(aNode);
if (acc) {
TreeWalker walker(acc, aNode->AsContent(),
TreeWalker::eWalkCache | TreeWalker::eScoped);
return walker.Next();
}
return nullptr;
}
void DocAccessible::BindToDocument(LocalAccessible* aAccessible,
const nsRoleMapEntry* aRoleMapEntry) {
// Put into DOM node cache.
if (aAccessible->IsNodeMapEntry()) {
mNodeToAccessibleMap.InsertOrUpdate(aAccessible->GetNode(), aAccessible);
}
// Put into unique ID cache.
mAccessibleCache.InsertOrUpdate(aAccessible->UniqueID(), RefPtr{aAccessible});
aAccessible->SetRoleMapEntry(aRoleMapEntry);
if (aAccessible->HasOwnContent()) {
AddDependentIDsFor(aAccessible);
nsIContent* content = aAccessible->GetContent();
if (content->IsElement() && content->AsElement()->HasAttr(
kNameSpaceID_None, nsGkAtoms::aria_owns)) {
mNotificationController->ScheduleRelocation(aAccessible);
}
}
}
void DocAccessible::UnbindFromDocument(LocalAccessible* aAccessible) {
NS_ASSERTION(mAccessibleCache.GetWeak(aAccessible->UniqueID()),
"Unbinding the unbound accessible!");
// Fire focus event on accessible having DOM focus if last focus was removed
// from the tree.
if (FocusMgr()->WasLastFocused(aAccessible)) {
FocusMgr()->ActiveItemChanged(nullptr);
#ifdef A11Y_LOG
if (logging::IsEnabled(logging::eFocus)) {
logging::ActiveItemChangeCausedBy("tree shutdown", aAccessible);
}
#endif
}
// Remove an accessible from node-to-accessible map if it exists there.
if (aAccessible->IsNodeMapEntry() &&
mNodeToAccessibleMap.Get(aAccessible->GetNode()) == aAccessible) {
mNodeToAccessibleMap.Remove(aAccessible->GetNode());
}
aAccessible->mStateFlags |= eIsNotInDocument;
// Update XPCOM part.
xpcAccessibleDocument* xpcDoc = GetAccService()->GetCachedXPCDocument(this);
if (xpcDoc) xpcDoc->NotifyOfShutdown(aAccessible);
void* uniqueID = aAccessible->UniqueID();
NS_ASSERTION(!aAccessible->IsDefunct(), "Shutdown the shutdown accessible!");
aAccessible->Shutdown();
mAccessibleCache.Remove(uniqueID);
}
void DocAccessible::ContentInserted(nsIContent* aStartChildNode,
nsIContent* aEndChildNode) {
// Ignore content insertions until we constructed accessible tree. Otherwise
// schedule tree update on content insertion after layout.
if (!mNotificationController || !HasLoadState(eTreeConstructed)) {
return;
}
// The frame constructor guarantees that only ranges with the same parent
// arrive here in presence of dynamic changes to the page, see
// nsCSSFrameConstructor::IssueSingleInsertNotifications' callers.
nsINode* parent = aStartChildNode->GetFlattenedTreeParentNode();
if (!parent) {
return;
}
LocalAccessible* container = AccessibleOrTrueContainer(parent);
if (!container) {
return;
}
AutoTArray<nsCOMPtr<nsIContent>, 10> list;
for (nsIContent* node = aStartChildNode; node != aEndChildNode;
node = node->GetNextSibling()) {
MOZ_ASSERT(parent == node->GetFlattenedTreeParentNode());
if (PruneOrInsertSubtree(node)) {
list.AppendElement(node);
}
}
mNotificationController->ScheduleContentInsertion(container, list);
}
bool DocAccessible::PruneOrInsertSubtree(nsIContent* aRoot) {
bool insert = false;
// In the case that we are, or are in, a shadow host, we need to assure
// some accessibles are removed if they are not rendered anymore.
nsIContent* shadowHost =
aRoot->GetShadowRoot() ? aRoot : aRoot->GetContainingShadowHost();
if (shadowHost) {
dom::ExplicitChildIterator iter(shadowHost);
// Check all explicit children in the host, if they are not slotted
// then remove their accessibles and subtrees.
while (nsIContent* childNode = iter.GetNextChild()) {
if (!childNode->GetPrimaryFrame() &&
!nsCoreUtils::IsDisplayContents(childNode)) {
ContentRemoved(childNode);
}
}
// If this is a slot, check to see if its fallback content is rendered,
// if not - remove it.
if (aRoot->IsHTMLElement(nsGkAtoms::slot)) {
for (nsIContent* childNode = aRoot->GetFirstChild(); childNode;
childNode = childNode->GetNextSibling()) {
if (!childNode->GetPrimaryFrame() &&
!nsCoreUtils::IsDisplayContents(childNode)) {
ContentRemoved(childNode);
}
}
}
}
// If we already have an accessible, check if we need to remove it, recreate
// it, or keep it in place.
LocalAccessible* acc = GetAccessible(aRoot);
if (acc) {
MOZ_ASSERT(aRoot == acc->GetContent(),
"LocalAccessible has differing content!");
#ifdef A11Y_LOG
if (logging::IsEnabled(logging::eTree)) {
logging::MsgBegin(
"TREE", "inserted content already has accessible; doc: %p", this);
logging::Node("content node", aRoot);
logging::AccessibleInfo("accessible node", acc);
logging::MsgEnd();
}
#endif
nsIFrame* frame = acc->GetFrame();
// LocalAccessible has no frame and it's not display:contents. Remove it.
// As well as removing the a11y subtree, we must also remove Accessibles
// for DOM descendants, since some of these might be relocated Accessibles
// and their DOM nodes are now hidden as well.
if (!frame && !nsCoreUtils::IsDisplayContents(aRoot)) {
ContentRemoved(aRoot);
return false;
}
// If it's a XULLabel it was probably reframed because a `value` attribute
// was added. The accessible creates its text leaf upon construction, so we
// need to recreate. Remove it, and schedule for reconstruction.
if (acc->IsXULLabel()) {
ContentRemoved(acc);
return true;
}
// It is a broken image that is being reframed because it either got
// or lost an `alt` tag that would rerender this node as text.
if (frame && (acc->IsImage() != (frame->AccessibleType() == eImageType))) {
ContentRemoved(aRoot);
return true;
}
// If the frame is an OuterDoc frame but this isn't an OuterDocAccessible,
// we need to recreate the LocalAccessible. This can happen for embed or
// object elements if their embedded content changes to be web content.
if (frame && !acc->IsOuterDoc() &&
frame->AccessibleType() == eOuterDocType) {
ContentRemoved(aRoot);
return true;
}
// If the content is focused, and is being re-framed, reset the selection
// listener for the node because the previous selection listener is on the
// old frame.
if (aRoot->IsElement() && FocusMgr()->HasDOMFocus(aRoot)) {
SelectionMgr()->SetControlSelectionListener(aRoot->AsElement());
}
// If the accessible is a table, or table part, its layout table
// status may have changed. We need to invalidate the associated
// cache, which listens for the following event.
if (acc->IsTable() || acc->IsTableRow() || acc->IsTableCell()) {
FireDelayedEvent(nsIAccessibleEvent::EVENT_TABLE_STYLING_CHANGED, acc);
}
// The accessible can be reparented or reordered in its parent.
// We schedule it for reinsertion. For example, a slotted element
// can change its slot attribute to a different slot.
insert = true;
// If the frame is invisible, remove it.
// Normally, layout sends explicit a11y notifications for visibility
// changes (see SendA11yNotifications in RestyleManager). However, if a
// visibility change also reconstructs the frame, we must handle it here.
if (frame && !frame->StyleVisibility()->IsVisible()) {
ContentRemoved(aRoot);
// There might be visible descendants, so we want to walk the subtree.
// However, we know we don't want to reinsert this node, so we set insert
// to false.
insert = false;
}
} else {
// If there is no current accessible, and the node has a frame, or is
// display:contents, schedule it for insertion.
if (aRoot->GetPrimaryFrame() || nsCoreUtils::IsDisplayContents(aRoot)) {
// This may be a new subtree, the insertion process will recurse through
// its descendants.
if (!GetAccessibleOrDescendant(aRoot)) {
return true;
}
// Content is not an accessible, but has accessible descendants.
// We schedule this container for insertion strictly for the case where it
// itself now needs an accessible. We will still need to recurse into the
// descendant content to prune accessibles, and in all likelyness to
// insert accessibles since accessible insertions will likeley get missed
// in an existing subtree.
insert = true;
}
}
if (LocalAccessible* container = AccessibleOrTrueContainer(aRoot)) {
AutoTArray<nsCOMPtr<nsIContent>, 10> list;
dom::AllChildrenIterator iter =
dom::AllChildrenIterator(aRoot, nsIContent::eAllChildren, true);
while (nsIContent* childNode = iter.GetNextChild()) {
if (PruneOrInsertSubtree(childNode)) {
list.AppendElement(childNode);
}
}
if (!list.IsEmpty()) {
mNotificationController->ScheduleContentInsertion(container, list);
}
}
return insert;
}
void DocAccessible::RecreateAccessible(nsIContent* aContent) {
#ifdef A11Y_LOG
if (logging::IsEnabled(logging::eTree)) {
logging::MsgBegin("TREE", "accessible recreated");
logging::Node("content", aContent);
logging::MsgEnd();
}
#endif
// XXX: we shouldn't recreate whole accessible subtree, instead we should
// subclass hide and show events to handle them separately and implement their
// coalescence with normal hide and show events. Note, in this case they
// should be coalesced with normal show/hide events.
ContentRemoved(aContent);
ContentInserted(aContent, aContent->GetNextSibling());
}
void DocAccessible::ProcessInvalidationList() {
// Invalidate children of container accessible for each element in
// invalidation list. Allow invalidation list insertions while container
// children are recached.
for (uint32_t idx = 0; idx < mInvalidationList.Length(); idx++) {
nsIContent* content = mInvalidationList[idx];
if (!HasAccessible(content) && content->HasID()) {
LocalAccessible* container = GetContainerAccessible(content);
if (container) {
// Check if the node is a target of aria-owns, and if so, don't process
// it here and let DoARIAOwnsRelocation process it.
AttrRelProviders* list = GetRelProviders(
content->AsElement(), nsDependentAtomString(content->GetID()));
bool shouldProcess = !!list;
if (shouldProcess) {
for (uint32_t idx = 0; idx < list->Length(); idx++) {
if (list->ElementAt(idx)->mRelAttr == nsGkAtoms::aria_owns) {
shouldProcess = false;
break;
}
}
if (shouldProcess) {
ProcessContentInserted(container, content);
}
}
}
}
}
mInvalidationList.Clear();
}
void DocAccessible::ProcessBoundsChanged() {
if (!StaticPrefs::accessibility_cache_enabled_AtStartup()) {
return;
}
nsTArray<CacheData> data;
for (auto* acc : mMaybeBoundsChanged) {
if (!acc->IsDefunct()) {
RefPtr<AccAttributes> fields = acc->BundleFieldsForCache(
CacheDomain::Bounds, CacheUpdateType::Update);
if (fields->Count()) {
data.AppendElement(CacheData(
acc->IsDoc() ? 0 : reinterpret_cast<uint64_t>(acc->UniqueID()),
fields));
}
}
}
mMaybeBoundsChanged.Clear();
if (data.Length()) {
IPCDoc()->SendCache(CacheUpdateType::Update, data, true);
}
}
LocalAccessible* DocAccessible::GetAccessibleEvenIfNotInMap(
nsINode* aNode) const {
if (!aNode->IsContent() ||
!aNode->AsContent()->IsHTMLElement(nsGkAtoms::area)) {
return GetAccessible(aNode);
}
// XXX Bug 135040, incorrect when multiple images use the same map.
nsIFrame* frame = aNode->AsContent()->GetPrimaryFrame();
nsImageFrame* imageFrame = do_QueryFrame(frame);
if (imageFrame) {
LocalAccessible* parent = GetAccessible(imageFrame->GetContent());
if (parent) {
LocalAccessible* area =
parent->AsImageMap()->GetChildAccessibleFor(aNode);
if (area) return area;
return nullptr;
}
}
return GetAccessible(aNode);
}
////////////////////////////////////////////////////////////////////////////////
// Protected members
void DocAccessible::NotifyOfLoading(bool aIsReloading) {
// Mark the document accessible as loading, if it stays alive then we'll mark
// it as loaded when we receive proper notification.
mLoadState &= ~eDOMLoaded;
if (!IsLoadEventTarget()) return;
if (aIsReloading && !mLoadEventType &&
// We can't fire events on a document whose tree isn't constructed yet.
HasLoadState(eTreeConstructed)) {
// Fire reload and state busy events on existing document accessible while
// event from user input flag can be calculated properly and accessible
// is alive. When new document gets loaded then this one is destroyed.
RefPtr<AccEvent> reloadEvent =
new AccEvent(nsIAccessibleEvent::EVENT_DOCUMENT_RELOAD, this);
nsEventShell::FireEvent(reloadEvent);
}
// Fire state busy change event. Use delayed event since we don't care
// actually if event isn't delivered when the document goes away like a shot.
RefPtr<AccEvent> stateEvent =
new AccStateChangeEvent(this, states::BUSY, true);
FireDelayedEvent(stateEvent);
}
void DocAccessible::DoInitialUpdate() {
if (nsCoreUtils::IsTopLevelContentDocInProcess(mDocumentNode)) {
mDocFlags |= eTopLevelContentDocInProcess;
if (IPCAccessibilityActive()) {
nsIDocShell* docShell = mDocumentNode->GetDocShell();
if (RefPtr<dom::BrowserChild> browserChild =
dom::BrowserChild::GetFrom(docShell)) {
// In content processes, top level content documents are always
// RootAccessibles.
MOZ_ASSERT(IsRoot());
DocAccessibleChild* ipcDoc = IPCDoc();
if (ipcDoc) {
browserChild->SetTopLevelDocAccessibleChild(ipcDoc);
} else {
ipcDoc = new DocAccessibleChild(this, browserChild);
SetIPCDoc(ipcDoc);
// Subsequent initialization might depend on being able to get the
// top level DocAccessibleChild, so set that as early as possible.
browserChild->SetTopLevelDocAccessibleChild(ipcDoc);
#if defined(XP_WIN)
IAccessibleHolder holder;
int32_t childID;
if (StaticPrefs::accessibility_cache_enabled_AtStartup()) {
childID = 0;
} else {
holder = CreateHolderFromAccessible(WrapNotNull(this));
MOZ_ASSERT(!holder.IsNull());
childID = MsaaAccessible::GetChildIDFor(this);
}
#else
int32_t holder = 0, childID = 0;
#endif
browserChild->SendPDocAccessibleConstructor(ipcDoc, nullptr, 0,
childID, holder);
#if !defined(XP_WIN)
ipcDoc->SendPDocAccessiblePlatformExtConstructor();
#endif
}
#if !defined(XP_WIN)
// It's safe for us to mark top level documents as constructed in the
// parent process without receiving an explicit message, since we can
// never get queries for this document or descendants before parent
// process construction is complete.
ipcDoc->SetConstructedInParentProcess();
#endif
}
}
}
mLoadState |= eTreeConstructed;
// Set up a root element and ARIA role mapping.
UpdateRootElIfNeeded();
// Build initial tree.
CacheChildrenInSubtree(this);
#ifdef A11Y_LOG
if (logging::IsEnabled(logging::eVerbose)) {
logging::Tree("TREE", "Initial subtree", this);
}
if (logging::IsEnabled(logging::eTreeSize)) {
logging::TreeSize("TREE SIZE", "Initial subtree", this);
}
#endif
// Fire reorder event after the document tree is constructed. Note, since
// this reorder event is processed by parent document then events targeted to
// this document may be fired prior to this reorder event. If this is
// a problem then consider to keep event processing per tab document.
if (!IsRoot()) {
RefPtr<AccReorderEvent> reorderEvent = new AccReorderEvent(LocalParent());
ParentDocument()->FireDelayedEvent(reorderEvent);
}
if (IPCAccessibilityActive()) {
DocAccessibleChild* ipcDoc = IPCDoc();
MOZ_ASSERT(ipcDoc);
if (ipcDoc) {
if (StaticPrefs::accessibility_cache_enabled_AtStartup()) {
// If we're caching, we should send an initial update for this document
// and its attributes. Each acc contained in this doc will have its
// initial update sent in `InsertIntoIpcTree`.
SendCache(CacheDomain::All, CacheUpdateType::Initial);
}
for (auto idx = 0U; idx < mChildren.Length(); idx++) {
ipcDoc->InsertIntoIpcTree(this, mChildren.ElementAt(idx), idx, true);
}
}
}
}
void DocAccessible::ProcessLoad() {
mLoadState |= eCompletelyLoaded;
#ifdef A11Y_LOG
if (logging::IsEnabled(logging::eDocLoad)) {
logging::DocCompleteLoad(this, IsLoadEventTarget());
}
#endif
// Do not fire document complete/stop events for root chrome document
// accessibles and for frame/iframe documents because
// a) screen readers start working on focus event in the case of root chrome
// documents
// b) document load event on sub documents causes screen readers to act is if
// entire page is reloaded.
if (!IsLoadEventTarget()) return;
// Fire complete/load stopped if the load event type is given.
if (mLoadEventType) {
RefPtr<AccEvent> loadEvent = new AccEvent(mLoadEventType, this);
FireDelayedEvent(loadEvent);
mLoadEventType = 0;
}
// Fire busy state change event.
RefPtr<AccEvent> stateEvent =
new AccStateChangeEvent(this, states::BUSY, false);
FireDelayedEvent(stateEvent);
}
void DocAccessible::AddDependentIDsFor(LocalAccessible* aRelProvider,
nsAtom* aRelAttr) {
dom::Element* relProviderEl = aRelProvider->Elm();
if (!relProviderEl) return;
for (uint32_t idx = 0; idx < kRelationAttrsLen; idx++) {
nsStaticAtom* relAttr = kRelationAttrs[idx];
if (aRelAttr && aRelAttr != relAttr) continue;
if (relAttr == nsGkAtoms::_for) {
if (!relProviderEl->IsAnyOfHTMLElements(nsGkAtoms::label,
nsGkAtoms::output)) {
continue;
}
} else if (relAttr == nsGkAtoms::control) {
if (!relProviderEl->IsAnyOfXULElements(nsGkAtoms::label,
nsGkAtoms::description)) {
continue;
}
}
IDRefsIterator iter(this, relProviderEl, relAttr);
while (true) {
const nsDependentSubstring id = iter.NextID();
if (id.IsEmpty()) break;
AttrRelProviders* providers = GetOrCreateRelProviders(relProviderEl, id);
if (providers) {
AttrRelProvider* provider = new AttrRelProvider(relAttr, relProviderEl);
if (provider) {
providers->AppendElement(provider);
// We've got here during the children caching. If the referenced
// content is not accessible then store it to pend its container
// children invalidation (this happens immediately after the caching
// is finished).
nsIContent* dependentContent = iter.GetElem(id);
if (dependentContent) {
if (!HasAccessible(dependentContent)) {
mInvalidationList.AppendElement(dependentContent);
}
}
}
}
}
// If the relation attribute is given then we don't have anything else to
// check.
if (aRelAttr) break;
}
// Make sure to schedule the tree update if needed.
mNotificationController->ScheduleProcessing();
}
void DocAccessible::RemoveDependentIDsFor(LocalAccessible* aRelProvider,
nsAtom* aRelAttr) {
dom::Element* relProviderElm = aRelProvider->Elm();
if (!relProviderElm) return;
for (uint32_t idx = 0; idx < kRelationAttrsLen; idx++) {
nsStaticAtom* relAttr = kRelationAttrs[idx];
if (aRelAttr && aRelAttr != kRelationAttrs[idx]) continue;
IDRefsIterator iter(this, relProviderElm, relAttr);
while (true) {
const nsDependentSubstring id = iter.NextID();
if (id.IsEmpty()) break;
AttrRelProviders* providers = GetRelProviders(relProviderElm, id);
if (providers) {
providers->RemoveElementsBy(
[relAttr, relProviderElm](const auto& provider) {
return provider->mRelAttr == relAttr &&
provider->mContent == relProviderElm;
});
RemoveRelProvidersIfEmpty(relProviderElm, id);
}
}
// If the relation attribute is given then we don't have anything else to
// check.
if (aRelAttr) break;
}
}
bool DocAccessible::UpdateAccessibleOnAttrChange(dom::Element* aElement,
nsAtom* aAttribute) {
if (aAttribute == nsGkAtoms::role) {
// It is common for js libraries to set the role on the body element after
// the document has loaded. In this case we just update the role map entry.
if (mContent == aElement) {
SetRoleMapEntryForDoc(aElement);
if (mIPCDoc) {
mIPCDoc->SendRoleChangedEvent(Role());
}
return true;
}
// Recreate the accessible when role is changed because we might require a
// different accessible class for the new role or the accessible may expose
// a different sets of interfaces (COM restriction).
RecreateAccessible(aElement);
return true;
}
if (aAttribute == nsGkAtoms::multiple) {
if (dom::HTMLSelectElement* select =
dom::HTMLSelectElement::FromNode(aElement)) {
if (select->Size() <= 1) {
// Adding the 'multiple' attribute to a select that has a size of 1
// creates a listbox as opposed to a combobox with a popup combobox
// list. Removing the attribute does the opposite.
RecreateAccessible(aElement);
return true;