/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ /* vim: set ts=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 "ARIAMap.h" #include "CachedTableAccessible.h" #include "DocAccessible.h" #include "mozilla/a11y/DocAccessibleParent.h" #include "mozilla/a11y/DocManager.h" #include "mozilla/a11y/Platform.h" #include "mozilla/a11y/RemoteAccessibleBase.h" #include "mozilla/a11y/RemoteAccessible.h" #include "mozilla/a11y/Role.h" #include "mozilla/BinarySearch.h" #include "mozilla/dom/Element.h" #include "mozilla/dom/BrowserParent.h" #include "mozilla/dom/CanonicalBrowsingContext.h" #include "mozilla/dom/DocumentInlines.h" #include "mozilla/gfx/Matrix.h" #include "mozilla/StaticPrefs_accessibility.h" #include "mozilla/Unused.h" #include "nsAccUtils.h" #include "nsTextEquivUtils.h" #include "Pivot.h" #include "Relation.h" #include "RelationType.h" #include "xpcAccessibleDocument.h" #ifdef A11Y_LOG # include "Logging.h" # define VERIFY_CACHE(domain) \ if (logging::IsEnabled(logging::eCache)) { \ Unused << mDoc->SendVerifyCache(mID, domain, mCachedFields); \ } #else # define VERIFY_CACHE(domain) \ do { \ } while (0) #endif namespace mozilla { namespace a11y { template void RemoteAccessibleBase::Shutdown() { MOZ_DIAGNOSTIC_ASSERT(!IsDoc()); xpcAccessibleDocument* xpcDoc = GetAccService()->GetCachedXPCDocument(Document()); if (xpcDoc) { xpcDoc->NotifyOfShutdown(static_cast(this)); } if (IsTable() || IsTableCell()) { CachedTableAccessible::Invalidate(this); } if (StaticPrefs::accessibility_cache_enabled_AtStartup()) { // Remove this acc's relation map from the doc's map of // reverse relations. We don't need to do additional processing // of the corresponding forward relations, because this shutdown // should trigger a cache update from the content process. // Similarly, we don't need to remove the reverse rels created // by this acc's forward rels because they'll be cleared during // the next update's call to PreProcessRelations(). // In short, accs are responsible for managing their own // reverse relation map, both in PreProcessRelations() and in // Shutdown(). Unused << mDoc->mReverseRelations.Remove(ID()); } // XXX Ideally this wouldn't be necessary, but it seems OuterDoc // accessibles can be destroyed before the doc they own. uint32_t childCount = mChildren.Length(); if (!IsOuterDoc()) { for (uint32_t idx = 0; idx < childCount; idx++) mChildren[idx]->Shutdown(); } else { if (childCount > 1) { MOZ_CRASH("outer doc has too many documents!"); } else if (childCount == 1) { mChildren[0]->AsDoc()->Unbind(); } } mChildren.Clear(); ProxyDestroyed(static_cast(this)); mDoc->RemoveAccessible(static_cast(this)); } template void RemoteAccessibleBase::SetChildDoc( DocAccessibleParent* aChildDoc) { MOZ_ASSERT(aChildDoc); MOZ_ASSERT(mChildren.Length() == 0); mChildren.AppendElement(aChildDoc); } template void RemoteAccessibleBase::ClearChildDoc( DocAccessibleParent* aChildDoc) { MOZ_ASSERT(aChildDoc); // This is possible if we're replacing one document with another: Doc 1 // has not had a chance to remove itself, but was already replaced by Doc 2 // in SetChildDoc(). This could result in two subsequent calls to // ClearChildDoc() even though mChildren.Length() == 1. MOZ_ASSERT(mChildren.Length() <= 1); mChildren.RemoveElement(aChildDoc); } template uint32_t RemoteAccessibleBase::EmbeddedChildCount() { size_t count = 0, kids = mChildren.Length(); for (size_t i = 0; i < kids; i++) { if (mChildren[i]->IsEmbeddedObject()) { count++; } } return count; } template int32_t RemoteAccessibleBase::IndexOfEmbeddedChild( Accessible* aChild) { size_t index = 0, kids = mChildren.Length(); for (size_t i = 0; i < kids; i++) { if (mChildren[i]->IsEmbeddedObject()) { if (mChildren[i] == aChild) { return index; } index++; } } return -1; } template Accessible* RemoteAccessibleBase::EmbeddedChildAt(uint32_t aChildIdx) { size_t index = 0, kids = mChildren.Length(); for (size_t i = 0; i < kids; i++) { if (!mChildren[i]->IsEmbeddedObject()) { continue; } if (index == aChildIdx) { return mChildren[i]; } index++; } return nullptr; } template LocalAccessible* RemoteAccessibleBase::OuterDocOfRemoteBrowser() const { auto tab = static_cast(mDoc->Manager()); dom::Element* frame = tab->GetOwnerElement(); NS_ASSERTION(frame, "why isn't the tab in a frame!"); if (!frame) return nullptr; DocAccessible* chromeDoc = GetExistingDocAccessible(frame->OwnerDoc()); return chromeDoc ? chromeDoc->GetAccessible(frame) : nullptr; } template void RemoteAccessibleBase::SetParent(Derived* aParent) { if (!aParent) { mParent = kNoParent; } else { MOZ_ASSERT(!IsDoc() || !aParent->IsDoc()); mParent = aParent->ID(); } } template Derived* RemoteAccessibleBase::RemoteParent() const { if (mParent == kNoParent) { return nullptr; } // if we are not a document then are parent is another proxy in the same // document. That means we can just ask our document for the proxy with our // parent id. if (!IsDoc()) { return Document()->GetAccessible(mParent); } // If we are a top level document then our parent is not a proxy. if (AsDoc()->IsTopLevel()) { return nullptr; } // Finally if we are a non top level document then our parent id is for a // proxy in our parent document so get the proxy from there. DocAccessibleParent* parentDoc = AsDoc()->ParentDoc(); MOZ_ASSERT(parentDoc); MOZ_ASSERT(mParent); return parentDoc->GetAccessible(mParent); } template ENameValueFlag RemoteAccessibleBase::Name(nsString& aName) const { ENameValueFlag nameFlag = eNameOK; if (mCachedFields) { if (IsText()) { mCachedFields->GetAttribute(nsGkAtoms::text, aName); return eNameOK; } auto cachedNameFlag = mCachedFields->GetAttribute(nsGkAtoms::explicit_name); if (cachedNameFlag) { nameFlag = static_cast(*cachedNameFlag); } if (mCachedFields->GetAttribute(nsGkAtoms::name, aName)) { VERIFY_CACHE(CacheDomain::NameAndDescription); return nameFlag; } } MOZ_ASSERT(aName.IsEmpty()); if (nameFlag != eNoNameOnPurpose) { aName.SetIsVoid(true); } return nameFlag; } template void RemoteAccessibleBase::Description(nsString& aDescription) const { if (mCachedFields) { mCachedFields->GetAttribute(nsGkAtoms::description, aDescription); VERIFY_CACHE(CacheDomain::NameAndDescription); } } template void RemoteAccessibleBase::Value(nsString& aValue) const { if (mCachedFields) { if (mCachedFields->HasAttribute(nsGkAtoms::aria_valuetext)) { mCachedFields->GetAttribute(nsGkAtoms::aria_valuetext, aValue); VERIFY_CACHE(CacheDomain::Value); return; } if (HasNumericValue()) { double checkValue = CurValue(); if (!IsNaN(checkValue)) { aValue.AppendFloat(checkValue); } return; } const nsRoleMapEntry* roleMapEntry = ARIARoleMap(); // Value of textbox is a textified subtree. if (roleMapEntry && roleMapEntry->Is(nsGkAtoms::textbox)) { nsTextEquivUtils::GetTextEquivFromSubtree(this, aValue); return; } if (IsCombobox()) { Pivot p = Pivot(const_cast*>(this)); PivotStateRule rule(states::ACTIVE); Accessible* option = p.First(rule); if (!option) { option = const_cast*>(this)->GetSelectedItem( 0); } if (option) { option->Name(aValue); } return; } if (IsTextLeaf() || IsImage()) { if (const Accessible* actionAcc = ActionAncestor()) { if (const_cast(actionAcc)->State() & states::LINKED) { // Text and image descendants of links expose the link URL as the // value. return actionAcc->Value(aValue); } } } } } template double RemoteAccessibleBase::CurValue() const { if (mCachedFields) { if (auto value = mCachedFields->GetAttribute(nsGkAtoms::value)) { VERIFY_CACHE(CacheDomain::Value); return *value; } } return UnspecifiedNaN(); } template double RemoteAccessibleBase::MinValue() const { if (mCachedFields) { if (auto min = mCachedFields->GetAttribute(nsGkAtoms::min)) { VERIFY_CACHE(CacheDomain::Value); return *min; } } return UnspecifiedNaN(); } template double RemoteAccessibleBase::MaxValue() const { if (mCachedFields) { if (auto max = mCachedFields->GetAttribute(nsGkAtoms::max)) { VERIFY_CACHE(CacheDomain::Value); return *max; } } return UnspecifiedNaN(); } template double RemoteAccessibleBase::Step() const { if (mCachedFields) { if (auto step = mCachedFields->GetAttribute(nsGkAtoms::step)) { VERIFY_CACHE(CacheDomain::Value); return *step; } } return UnspecifiedNaN(); } template Accessible* RemoteAccessibleBase::ChildAtPoint( int32_t aX, int32_t aY, LocalAccessible::EWhichChildAtPoint aWhichChild) { if (IsOuterDoc() && aWhichChild == EWhichChildAtPoint::DirectChild) { // This is an iframe, which is as deep as the viewport cache goes. The // caller wants a direct child, which can only be the embedded document. if (Bounds().Contains(aX, aY)) { return RemoteFirstChild(); } return nullptr; } RemoteAccessible* lastMatch = nullptr; // If `this` is a document, use its viewport cache instead of // the cache of its parent document. if (DocAccessibleParent* doc = IsDoc() ? AsDoc() : mDoc) { if (auto maybeViewportCache = doc->mCachedFields->GetAttribute>( nsGkAtoms::viewport)) { // The retrieved viewport cache contains acc IDs in hittesting order. // That is, items earlier in the list have z-indexes that are larger than // those later in the list. If you were to build a tree by z-index, where // chilren have larger z indices than their parents, iterating this list // is essentially a postorder tree traversal. const nsTArray& viewportCache = *maybeViewportCache; for (auto id : viewportCache) { RemoteAccessible* acc = doc->GetAccessible(id); if (!acc) { // This can happen if the acc died in between // pushing the viewport cache and doing this hittest continue; } if (acc->IsOuterDoc() && aWhichChild == EWhichChildAtPoint::DeepestChild && acc->Bounds().Contains(aX, aY)) { // acc is an iframe, which is as deep as the viewport cache goes. This // iframe contains the requested point. RemoteAccessible* innerDoc = acc->RemoteFirstChild(); if (innerDoc) { MOZ_ASSERT(innerDoc->IsDoc()); // Search the embedded document's viewport cache so we return the // deepest descendant in that embedded document. return innerDoc->ChildAtPoint(aX, aY, EWhichChildAtPoint::DeepestChild); } // If there is no embedded document, the iframe itself is the deepest // descendant. return acc; } if (acc == this) { MOZ_ASSERT(!acc->IsOuterDoc()); // Even though we're searching from the doc's cache // this call shouldn't pass the boundary defined by // the acc this call originated on. If we hit `this`, // return our most recent match. break; } if (acc->Bounds().Contains(aX, aY)) { if (aWhichChild == EWhichChildAtPoint::DeepestChild) { // Because our rects are in hittesting order, the // first match we encounter is guaranteed to be the // deepest match. lastMatch = acc; break; } // We're looking for a DirectChild match. Update our // `lastMatch` marker as we ascend towards `this`. lastMatch = acc; } } } } if (!lastMatch && Bounds().Contains(aX, aY)) { return this; } return lastMatch; } template Maybe RemoteAccessibleBase::RetrieveCachedBounds() const { if (!mCachedFields) { return Nothing(); } Maybe&> maybeArray = mCachedFields->GetAttribute>(nsGkAtoms::relativeBounds); if (maybeArray) { const nsTArray& relativeBoundsArr = *maybeArray; MOZ_ASSERT(relativeBoundsArr.Length() == 4, "Incorrectly sized bounds array"); nsRect relativeBoundsRect(relativeBoundsArr[0], relativeBoundsArr[1], relativeBoundsArr[2], relativeBoundsArr[3]); return Some(relativeBoundsRect); } return Nothing(); } template void RemoteAccessibleBase::ApplyCrossProcOffset( nsRect& aBounds) const { Maybe&> maybeOffset = mCachedFields->GetAttribute>(nsGkAtoms::crossorigin); if (!maybeOffset) { return; } MOZ_ASSERT(maybeOffset->Length() == 2); const nsTArray& offset = *maybeOffset; // Our retrieved value is in app units, so we don't need to do any // unit conversion here. aBounds.MoveBy(offset[0], offset[1]); } template bool RemoteAccessibleBase::ApplyTransform(nsRect& aBounds) const { // First, attempt to retrieve the transform from the cache. Maybe&> maybeTransform = mCachedFields->GetAttribute>( nsGkAtoms::transform); if (!maybeTransform) { return false; } // The transform matrix we cache is meant to operate on rects // within the coordinate space of the frame to which the // transform is applied (self-relative rects). We cache bounds // relative to some ancestor. Remove the relative offset before // transforming. The transform matrix will add it back in. aBounds.MoveTo(0, 0); auto mtxInPixels = gfx::Matrix4x4Typed::FromUnknownMatrix( *(*maybeTransform)); // Our matrix is in CSS Pixels, so we need our rect to be in CSS // Pixels too. Convert before applying. auto boundsInPixels = CSSRect::FromAppUnits(aBounds); boundsInPixels = mtxInPixels.TransformBounds(boundsInPixels); aBounds = CSSRect::ToAppUnits(boundsInPixels); return true; } template void RemoteAccessibleBase::ApplyScrollOffset(nsRect& aBounds) const { Maybe&> maybeScrollPosition = mCachedFields->GetAttribute>(nsGkAtoms::scrollPosition); if (!maybeScrollPosition || maybeScrollPosition->Length() != 2) { return; } // Our retrieved value is in app units, so we don't need to do any // unit conversion here. const nsTArray& scrollPosition = *maybeScrollPosition; // Scroll position is an inverse representation of scroll offset (since the // further the scroll bar moves down the page, the further the page content // moves up/closer to the origin). nsPoint scrollOffset(-scrollPosition[0], -scrollPosition[1]); aBounds.MoveBy(scrollOffset.x, scrollOffset.y); } template nsRect RemoteAccessibleBase::BoundsInAppUnits() const { if (dom::CanonicalBrowsingContext* cbc = mDoc->GetBrowsingContext()->Top()) { if (dom::BrowserParent* bp = cbc->GetBrowserParent()) { DocAccessibleParent* topDoc = bp->GetTopLevelDocAccessible(); if (topDoc && topDoc->mCachedFields) { auto appUnitsPerDevPixel = topDoc->mCachedFields->GetAttribute( nsGkAtoms::_moz_device_pixel_ratio); MOZ_ASSERT(appUnitsPerDevPixel); return LayoutDeviceIntRect::ToAppUnits(Bounds(), *appUnitsPerDevPixel); } } } return LayoutDeviceIntRect::ToAppUnits(Bounds(), AppUnitsPerCSSPixel()); } template LayoutDeviceIntRect RemoteAccessibleBase::BoundsWithOffset( Maybe aOffset) const { Maybe maybeBounds = RetrieveCachedBounds(); if (maybeBounds) { nsRect bounds = *maybeBounds; const DocAccessibleParent* topDoc = IsDoc() ? AsDoc() : nullptr; if (aOffset.isSome()) { // The rect we've passed in is in app units, so no conversion needed. nsRect internalRect = *aOffset; bounds.SetRectX(bounds.x + internalRect.x, internalRect.width); bounds.SetRectY(bounds.y + internalRect.y, internalRect.height); } Unused << ApplyTransform(bounds); LayoutDeviceIntRect devPxBounds; const Accessible* acc = Parent(); const RemoteAccessibleBase* recentAcc = this; while (acc && acc->IsRemote()) { RemoteAccessible* remoteAcc = const_cast(acc)->AsRemote(); if (Maybe maybeRemoteBounds = remoteAcc->RetrieveCachedBounds()) { nsRect remoteBounds = *maybeRemoteBounds; // We need to take into account a non-1 resolution set on the // presshell. This happens with async pinch zooming, among other // things. We can't reliably query this value in the parent process, // so we retrieve it from the document's cache. if (remoteAcc->IsDoc()) { // Apply the document's resolution to the bounds we've gathered // thus far. We do this before applying the document's offset // because document accs should not have their bounds scaled by // their own resolution. They should be scaled by the resolution // of their containing document (if any). Maybe res = remoteAcc->AsDoc()->mCachedFields->GetAttribute( nsGkAtoms::resolution); MOZ_ASSERT(res, "No cached document resolution found."); bounds.ScaleRoundOut(res.valueOr(1.0f)); topDoc = remoteAcc->AsDoc(); } if (remoteAcc->IsOuterDoc()) { if (recentAcc && recentAcc->IsDoc() && !recentAcc->AsDoc()->IsTopLevel() && recentAcc->AsDoc()->IsTopLevelInContentProcess()) { // We're unable to account for the document offset of remote, // cross process iframes when computing parent-relative bounds. // Instead we store this value separately and apply it here. remoteAcc->ApplyCrossProcOffset(remoteBounds); } } // Apply scroll offset, if applicable. Only the contents of an // element are affected by its scroll offset, which is why this call // happens in this loop instead of both inside and outside of // the loop (like ApplyTransform). remoteAcc->ApplyScrollOffset(remoteBounds); // Regardless of whether this is a doc, we should offset `bounds` // by the bounds retrieved here. This is how we build screen // coordinates from relative coordinates. bounds.MoveBy(remoteBounds.X(), remoteBounds.Y()); Unused << remoteAcc->ApplyTransform(bounds); } recentAcc = remoteAcc; acc = acc->Parent(); } MOZ_ASSERT(topDoc); if (topDoc) { // We use the top documents app-units-per-dev-pixel even though // theoretically nested docs can have different values. Practically, // that isn't likely since we only offer zoom controls for the top // document and all subdocuments inherit from it. auto appUnitsPerDevPixel = topDoc->mCachedFields->GetAttribute( nsGkAtoms::_moz_device_pixel_ratio); MOZ_ASSERT(appUnitsPerDevPixel); if (appUnitsPerDevPixel) { // Convert our existing `bounds` rect from app units to dev pixels devPxBounds = LayoutDeviceIntRect::FromAppUnitsToNearest( bounds, *appUnitsPerDevPixel); } } #if !defined(ANDROID) // This block is not thread safe because it queries a LocalAccessible. // It is also not needed in Android since the only local accessible is // the outer doc browser that has an offset of 0. // acc could be null if the OuterDocAccessible died before the top level // DocAccessibleParent. if (LocalAccessible* localAcc = acc ? const_cast(acc)->AsLocal() : nullptr) { // LocalAccessible::Bounds returns screen-relative bounds in // dev pixels. LayoutDeviceIntRect localBounds = localAcc->Bounds(); // The root document will always have an APZ resolution of 1, // so we don't factor in its scale here. We also don't scale // by GetFullZoom because LocalAccessible::Bounds already does // that. devPxBounds.MoveBy(localBounds.X(), localBounds.Y()); } #endif return devPxBounds; } return LayoutDeviceIntRect(); } template LayoutDeviceIntRect RemoteAccessibleBase::Bounds() const { return BoundsWithOffset(Nothing()); } template Relation RemoteAccessibleBase::RelationByType( RelationType aType) const { // We are able to handle some relations completely in the // parent process, without the help of the cache. Those // relations are enumerated here. Other relations, whose // types are stored in kRelationTypeAtoms, are processed // below using the cache. if (aType == RelationType::CONTAINING_TAB_PANE) { if (dom::CanonicalBrowsingContext* cbc = mDoc->GetBrowsingContext()) { if (dom::CanonicalBrowsingContext* topCbc = cbc->Top()) { if (dom::BrowserParent* bp = topCbc->GetBrowserParent()) { return Relation(bp->GetTopLevelDocAccessible()); } } } return Relation(); } Relation rel; if (!mCachedFields) { return rel; } for (auto data : kRelationTypeAtoms) { if (data.mType != aType || (data.mValidTag && TagName() != data.mValidTag)) { continue; } if (auto maybeIds = mCachedFields->GetAttribute>(data.mAtom)) { rel.AppendIter(new RemoteAccIterator(*maybeIds, Document())); } // Each relation type has only one relevant cached attribute, // so break after we've handled the attr for this type, // even if we didn't find any targets. break; } if (auto accRelMapEntry = mDoc->mReverseRelations.Lookup(ID())) { if (auto reverseIdsEntry = accRelMapEntry.Data().Lookup(static_cast(aType))) { rel.AppendIter(new RemoteAccIterator(reverseIdsEntry.Data(), Document())); } } return rel; } template void RemoteAccessibleBase::AppendTextTo(nsAString& aText, uint32_t aStartOffset, uint32_t aLength) { if (IsText()) { if (mCachedFields) { if (auto text = mCachedFields->GetAttribute(nsGkAtoms::text)) { aText.Append(Substring(*text, aStartOffset, aLength)); } VERIFY_CACHE(CacheDomain::Text); } return; } if (aStartOffset != 0 || aLength == 0) { return; } if (IsHTMLBr()) { aText += kForcedNewLineChar; } else if (RemoteParent() && nsAccUtils::MustPrune(RemoteParent())) { // Expose the embedded object accessible as imaginary embedded object // character if its parent hypertext accessible doesn't expose children to // AT. aText += kImaginaryEmbeddedObjectChar; } else { aText += kEmbeddedObjectChar; } } template nsTArray RemoteAccessibleBase::PreProcessRelations( AccAttributes* aFields) { nsTArray updateTracker(ArrayLength(kRelationTypeAtoms)); for (auto const& data : kRelationTypeAtoms) { if (data.mValidTag) { // The relation we're currently processing only applies to particular // elements. Check to see if we're one of them. nsAtom* tag = TagName(); if (!tag) { // TagName() returns null on an initial cache push -- check aFields // for a tag name instead. if (auto maybeTag = aFields->GetAttribute>(nsGkAtoms::tag)) { tag = *maybeTag; } } MOZ_ASSERT( tag || IsTextLeaf(), "Could not fetch tag via TagName() or from initial cache push!"); if (tag != data.mValidTag) { // If this rel doesn't apply to us, do no pre-processing. Also, // note in our updateTracker that we should do no post-processing. updateTracker.AppendElement(false); continue; } } if (!data.mReverseType) { updateTracker.AppendElement(false); continue; } nsStaticAtom* const relAtom = data.mAtom; auto newRelationTargets = aFields->GetAttribute>(relAtom); bool shouldAddNewImplicitRels = newRelationTargets && newRelationTargets->Length(); // Remove existing implicit relations if we need to perform an update, or // if we've recieved a DeleteEntry(). Only do this if mCachedFields is // initialized. If mCachedFields is not initialized, we still need to // construct the update array so we correctly handle reverse rels in // PostProcessRelations. if ((shouldAddNewImplicitRels || aFields->GetAttribute(relAtom)) && mCachedFields) { if (auto maybeOldIDs = mCachedFields->GetAttribute>(relAtom)) { for (uint64_t id : *maybeOldIDs) { // For each target, fetch its reverse relation map nsTHashMap>& reverseRels = Document()->mReverseRelations.LookupOrInsert(id); // Then fetch its reverse relation's ID list nsTArray& reverseRelIDs = reverseRels.LookupOrInsert( static_cast(*data.mReverseType)); // There might be other reverse relations stored for this acc, so // remove our ID instead of deleting the array entirely. DebugOnly removed = reverseRelIDs.RemoveElement(ID()); MOZ_ASSERT(removed, "Can't find old reverse relation"); } } } updateTracker.AppendElement(shouldAddNewImplicitRels); } return updateTracker; } template void RemoteAccessibleBase::PostProcessRelations( const nsTArray& aToUpdate) { size_t updateCount = aToUpdate.Length(); MOZ_ASSERT(updateCount == ArrayLength(kRelationTypeAtoms), "Did not note update status for every relation type!"); for (size_t i = 0; i < updateCount; i++) { if (aToUpdate.ElementAt(i)) { // Since kRelationTypeAtoms was used to generate aToUpdate, we // know the ith entry of aToUpdate corresponds to the relation type in // the ith entry of kRelationTypeAtoms. Fetch the related data here. auto const& data = kRelationTypeAtoms[i]; const nsTArray& newIDs = *mCachedFields->GetAttribute>(data.mAtom); for (uint64_t id : newIDs) { nsTHashMap>& relations = Document()->mReverseRelations.LookupOrInsert(id); MOZ_ASSERT(data.mReverseType, "Updating implicit rels, but no implicit rel exists?"); nsTArray& ids = relations.LookupOrInsert(static_cast(*data.mReverseType)); ids.AppendElement(ID()); } } } } template uint32_t RemoteAccessibleBase::GetCachedTextLength() { MOZ_ASSERT(!HasChildren()); if (!mCachedFields) { return 0; } VERIFY_CACHE(CacheDomain::Text); auto text = mCachedFields->GetAttribute(nsGkAtoms::text); if (!text) { return 0; } return text->Length(); } template Maybe&> RemoteAccessibleBase::GetCachedTextLines() { MOZ_ASSERT(!HasChildren()); if (!mCachedFields) { return Nothing(); } VERIFY_CACHE(CacheDomain::Text); return mCachedFields->GetAttribute>(nsGkAtoms::line); } template Maybe> RemoteAccessibleBase::GetCachedCharData() { MOZ_ASSERT(IsText()); if (!mCachedFields) { return Nothing(); } if (Maybe&> maybeCharData = mCachedFields->GetAttribute>( nsGkAtoms::characterData)) { const nsTArray& charData = *maybeCharData; nsTArray rects; for (int i = 0; i < static_cast(charData.Length()); i += 4) { nsRect r(charData[i], charData[i + 1], charData[i + 2], charData[i + 3]); rects.AppendElement(r); } return Some(std::move(rects)); } return Nothing(); } template void RemoteAccessibleBase::DOMNodeID(nsString& aID) const { if (mCachedFields) { mCachedFields->GetAttribute(nsGkAtoms::id, aID); VERIFY_CACHE(CacheDomain::DOMNodeID); } } template RefPtr RemoteAccessibleBase::GetCachedTextAttributes() { MOZ_ASSERT(IsText() || IsHyperText()); if (mCachedFields) { auto attrs = mCachedFields->GetAttributeRefPtr(nsGkAtoms::style); VERIFY_CACHE(CacheDomain::Text); return attrs; } return nullptr; } template already_AddRefed RemoteAccessibleBase::DefaultTextAttributes() { RefPtr attrs = GetCachedTextAttributes(); RefPtr result = new AccAttributes(); if (attrs) { attrs->CopyTo(result); } return result.forget(); } template RefPtr RemoteAccessibleBase::GetCachedARIAAttributes() const { if (mCachedFields) { auto attrs = mCachedFields->GetAttributeRefPtr(nsGkAtoms::aria); VERIFY_CACHE(CacheDomain::ARIA); return attrs; } return nullptr; } template uint64_t RemoteAccessibleBase::State() { uint64_t state = 0; if (mCachedFields) { if (auto rawState = mCachedFields->GetAttribute(nsGkAtoms::state)) { VERIFY_CACHE(CacheDomain::State); state = *rawState; // Handle states that are derived from other states. if (!(state & states::UNAVAILABLE)) { state |= states::ENABLED | states::SENSITIVE; } if (state & states::EXPANDABLE && !(state & states::EXPANDED)) { state |= states::COLLAPSED; } } // Fetch our current opacity value from the cache. auto opacity = Opacity(); if (opacity && *opacity == 1.0f) { state |= states::OPAQUE1; } else { // If we can't retrieve an opacity value, or if the value we retrieve // is less than one, ensure the OPAQUE1 bit is cleared. // It's possible this bit was set in the cached `rawState` vector, but // we've since been notified of a style change invalidating that state. state &= ~states::OPAQUE1; } auto* cbc = mDoc->GetBrowsingContext(); if (cbc && !cbc->IsActive()) { state |= states::OFFSCREEN; } } auto* browser = static_cast(Document()->Manager()); if (browser == dom::BrowserParent::GetFocused()) { if (this == Document()->GetFocusedAcc()) { state |= states::FOCUSED; } } return state; } template already_AddRefed RemoteAccessibleBase::Attributes() { RefPtr attributes = new AccAttributes(); nsAccessibilityService* accService = GetAccService(); if (!accService) { // The service can be shut down before RemoteAccessibles. If it is shut // down, we can't calculate some attributes. We're about to die anyway. return attributes.forget(); } if (mCachedFields) { // We use GetAttribute instead of GetAttributeRefPtr because we need // nsAtom, not const nsAtom. if (auto tag = mCachedFields->GetAttribute>(nsGkAtoms::tag)) { attributes->SetAttribute(nsGkAtoms::tag, *tag); } GroupPos groupPos = GroupPosition(); nsAccUtils::SetAccGroupAttrs(attributes, groupPos.level, groupPos.setSize, groupPos.posInSet); bool hierarchical = false; uint32_t itemCount = AccGroupInfo::TotalItemCount(this, &hierarchical); if (itemCount) { attributes->SetAttribute(nsGkAtoms::child_item_count, static_cast(itemCount)); } if (hierarchical) { attributes->SetAttribute(nsGkAtoms::tree, true); } if (auto inputType = mCachedFields->GetAttribute>( nsGkAtoms::textInputType)) { attributes->SetAttribute(nsGkAtoms::textInputType, *inputType); } if (RefPtr display = DisplayStyle()) { attributes->SetAttribute(nsGkAtoms::display, display); } if (TableCellAccessibleBase* cell = AsTableCellBase()) { TableAccessibleBase* table = cell->Table(); uint32_t row = cell->RowIdx(); uint32_t col = cell->ColIdx(); int32_t cellIdx = table->CellIndexAt(row, col); if (cellIdx != -1) { attributes->SetAttribute(nsGkAtoms::tableCellIndex, cellIdx); } } if (bool layoutGuess = TableIsProbablyForLayout()) { attributes->SetAttribute(nsGkAtoms::layout_guess, layoutGuess); } accService->MarkupAttributes(this, attributes); const nsRoleMapEntry* roleMap = ARIARoleMap(); nsAutoString role; mCachedFields->GetAttribute(nsGkAtoms::role, role); if (role.IsEmpty()) { if (roleMap && roleMap->roleAtom != nsGkAtoms::_empty) { // Single, known role. attributes->SetAttribute(nsGkAtoms::xmlroles, roleMap->roleAtom); } else if (nsAtom* landmark = LandmarkRole()) { // Landmark role from markup; e.g. HTML
. attributes->SetAttribute(nsGkAtoms::xmlroles, landmark); } } else { // Unknown role or multiple roles. attributes->SetAttribute(nsGkAtoms::xmlroles, std::move(role)); } if (roleMap) { nsAutoString live; if (nsAccUtils::GetLiveAttrValue(roleMap->liveAttRule, live)) { attributes->SetAttribute(nsGkAtoms::aria_live, std::move(live)); } } if (auto ariaAttrs = GetCachedARIAAttributes()) { ariaAttrs->CopyTo(attributes); } nsAccUtils::SetLiveContainerAttributes(attributes, this); } nsAutoString name; if (Name(name) != eNameFromSubtree && !name.IsVoid()) { attributes->SetAttribute(nsGkAtoms::explicit_name, true); } return attributes.forget(); } template nsAtom* RemoteAccessibleBase::TagName() const { if (mCachedFields) { if (auto tag = mCachedFields->GetAttribute>(nsGkAtoms::tag)) { return *tag; } } return nullptr; } template already_AddRefed RemoteAccessibleBase::DisplayStyle() const { if (mCachedFields) { if (auto display = mCachedFields->GetAttribute>(nsGkAtoms::display)) { RefPtr result = *display; return result.forget(); } } return nullptr; } template Maybe RemoteAccessibleBase::Opacity() const { if (mCachedFields) { // GetAttribute already returns a Maybe, so we don't // need to do any additional manipulation. return mCachedFields->GetAttribute(nsGkAtoms::opacity); } return Nothing(); } template void RemoteAccessibleBase::LiveRegionAttributes( nsAString* aLive, nsAString* aRelevant, Maybe* aAtomic, nsAString* aBusy) const { if (!mCachedFields) { return; } RefPtr attrs = GetCachedARIAAttributes(); if (!attrs) { return; } if (aLive) { attrs->GetAttribute(nsGkAtoms::aria_live, *aLive); } if (aRelevant) { attrs->GetAttribute(nsGkAtoms::aria_relevant, *aRelevant); } if (aAtomic) { if (auto value = attrs->GetAttribute>(nsGkAtoms::aria_atomic)) { *aAtomic = Some(*value == nsGkAtoms::_true); } } if (aBusy) { attrs->GetAttribute(nsGkAtoms::aria_busy, *aBusy); } } template nsAtom* RemoteAccessibleBase::GetPrimaryAction() const { if (mCachedFields) { if (auto action = mCachedFields->GetAttribute>(nsGkAtoms::action)) { return *action; } } return nullptr; } template uint8_t RemoteAccessibleBase::ActionCount() const { uint8_t actionCount = 0; if (mCachedFields) { if (HasPrimaryAction() || ActionAncestor()) { actionCount++; } if (mCachedFields->HasAttribute(nsGkAtoms::longdesc)) { actionCount++; } VERIFY_CACHE(CacheDomain::Actions); } return actionCount; } template void RemoteAccessibleBase::ActionNameAt(uint8_t aIndex, nsAString& aName) { if (mCachedFields) { aName.Truncate(); nsAtom* action = GetPrimaryAction(); bool hasActionAncestor = !action && ActionAncestor(); switch (aIndex) { case 0: if (action) { action->ToString(aName); } else if (hasActionAncestor) { aName.AssignLiteral("click ancestor"); } else if (mCachedFields->HasAttribute(nsGkAtoms::longdesc)) { aName.AssignLiteral("showlongdesc"); } break; case 1: if ((action || hasActionAncestor) && mCachedFields->HasAttribute(nsGkAtoms::longdesc)) { aName.AssignLiteral("showlongdesc"); } break; default: break; } } VERIFY_CACHE(CacheDomain::Actions); } template bool RemoteAccessibleBase::DoAction(uint8_t aIndex) const { if (ActionCount() < aIndex + 1) { return false; } Unused << mDoc->SendDoActionAsync(mID, aIndex); return true; } template KeyBinding RemoteAccessibleBase::AccessKey() const { if (mCachedFields) { if (auto value = mCachedFields->GetAttribute(nsGkAtoms::accesskey)) { return KeyBinding(*value); } } return KeyBinding(); } template void RemoteAccessibleBase::SelectionRanges( nsTArray* aRanges) const { Document()->SelectionRanges(aRanges); } template void RemoteAccessibleBase::ARIAGroupPosition( int32_t* aLevel, int32_t* aSetSize, int32_t* aPosInSet) const { if (!mCachedFields) { return; } if (aLevel) { if (auto level = mCachedFields->GetAttribute(nsGkAtoms::aria_level)) { *aLevel = *level; } } if (aSetSize) { if (auto setsize = mCachedFields->GetAttribute(nsGkAtoms::aria_setsize)) { *aSetSize = *setsize; } } if (aPosInSet) { if (auto posinset = mCachedFields->GetAttribute(nsGkAtoms::aria_posinset)) { *aPosInSet = *posinset; } } } template AccGroupInfo* RemoteAccessibleBase::GetGroupInfo() const { if (!mCachedFields) { return nullptr; } if (auto groupInfo = mCachedFields->GetAttribute>( nsGkAtoms::group)) { return groupInfo->get(); } return nullptr; } template AccGroupInfo* RemoteAccessibleBase::GetOrCreateGroupInfo() { AccGroupInfo* groupInfo = GetGroupInfo(); if (groupInfo) { return groupInfo; } groupInfo = AccGroupInfo::CreateGroupInfo(this); if (groupInfo) { if (!mCachedFields) { mCachedFields = new AccAttributes(); } mCachedFields->SetAttribute(nsGkAtoms::group, groupInfo); } return groupInfo; } template void RemoteAccessibleBase::InvalidateGroupInfo() { if (mCachedFields) { mCachedFields->Remove(nsGkAtoms::group); } } template bool RemoteAccessibleBase::HasPrimaryAction() const { return mCachedFields && mCachedFields->HasAttribute(nsGkAtoms::action); } template void RemoteAccessibleBase::TakeFocus() const { Unused << mDoc->SendTakeFocus(mID); } template void RemoteAccessibleBase::ScrollTo(uint32_t aHow) const { Unused << mDoc->SendScrollTo(mID, aHow); } //////////////////////////////////////////////////////////////////////////////// // SelectAccessible template void RemoteAccessibleBase::SelectedItems( nsTArray* aItems) { Pivot p = Pivot(this); PivotStateRule rule(states::SELECTED); for (Accessible* selected = p.First(rule); selected; selected = p.Next(selected, rule)) { aItems->AppendElement(selected); } } template uint32_t RemoteAccessibleBase::SelectedItemCount() { uint32_t count = 0; Pivot p = Pivot(this); PivotStateRule rule(states::SELECTED); for (Accessible* selected = p.First(rule); selected; selected = p.Next(selected, rule)) { count++; } return count; } template Accessible* RemoteAccessibleBase::GetSelectedItem(uint32_t aIndex) { uint32_t index = 0; Accessible* selected = nullptr; Pivot p = Pivot(this); PivotStateRule rule(states::SELECTED); for (selected = p.First(rule); selected && index < aIndex; selected = p.Next(selected, rule)) { index++; } return selected; } template bool RemoteAccessibleBase::IsItemSelected(uint32_t aIndex) { uint32_t index = 0; Accessible* selectable = nullptr; Pivot p = Pivot(this); PivotStateRule rule(states::SELECTABLE); for (selectable = p.First(rule); selectable && index < aIndex; selectable = p.Next(selectable, rule)) { index++; } return selectable && selectable->State() & states::SELECTED; } template bool RemoteAccessibleBase::AddItemToSelection(uint32_t aIndex) { uint32_t index = 0; Accessible* selectable = nullptr; Pivot p = Pivot(this); PivotStateRule rule(states::SELECTABLE); for (selectable = p.First(rule); selectable && index < aIndex; selectable = p.Next(selectable, rule)) { index++; } if (selectable) selectable->SetSelected(true); return static_cast(selectable); } template bool RemoteAccessibleBase::RemoveItemFromSelection(uint32_t aIndex) { uint32_t index = 0; Accessible* selectable = nullptr; Pivot p = Pivot(this); PivotStateRule rule(states::SELECTABLE); for (selectable = p.First(rule); selectable && index < aIndex; selectable = p.Next(selectable, rule)) { index++; } if (selectable) selectable->SetSelected(false); return static_cast(selectable); } template bool RemoteAccessibleBase::SelectAll() { if ((State() & states::MULTISELECTABLE) == 0) { return false; } bool success = false; Accessible* selectable = nullptr; Pivot p = Pivot(this); PivotStateRule rule(states::SELECTABLE); for (selectable = p.First(rule); selectable; selectable = p.Next(selectable, rule)) { success = true; selectable->SetSelected(true); } return success; } template bool RemoteAccessibleBase::UnselectAll() { if ((State() & states::MULTISELECTABLE) == 0) { return false; } bool success = false; Accessible* selectable = nullptr; Pivot p = Pivot(this); PivotStateRule rule(states::SELECTABLE); for (selectable = p.First(rule); selectable; selectable = p.Next(selectable, rule)) { success = true; selectable->SetSelected(false); } return success; } template void RemoteAccessibleBase::TakeSelection() { Unused << mDoc->SendTakeSelection(mID); } template void RemoteAccessibleBase::SetSelected(bool aSelect) { Unused << mDoc->SendSetSelected(mID, aSelect); } template TableAccessibleBase* RemoteAccessibleBase::AsTableBase() { MOZ_ASSERT(StaticPrefs::accessibility_cache_enabled_AtStartup()); if (IsTable()) { return CachedTableAccessible::GetFrom(this); } return nullptr; } template TableCellAccessibleBase* RemoteAccessibleBase::AsTableCellBase() { MOZ_ASSERT(StaticPrefs::accessibility_cache_enabled_AtStartup()); if (IsTableCell()) { return CachedTableCellAccessible::GetFrom(this); } return nullptr; } template bool RemoteAccessibleBase::TableIsProbablyForLayout() { MOZ_ASSERT(StaticPrefs::accessibility_cache_enabled_AtStartup()); if (mCachedFields) { if (auto layoutGuess = mCachedFields->GetAttribute(nsGkAtoms::layout_guess)) { return *layoutGuess; } } return false; } template const nsTArray& RemoteAccessibleBase::GetCachedHyperTextOffsets() const { if (mCachedFields) { if (auto offsets = mCachedFields->GetAttribute>(nsGkAtoms::offset)) { return *offsets; } } nsTArray newOffsets; BuildCachedHyperTextOffsets(newOffsets); if (!mCachedFields) { const_cast*>(this)->mCachedFields = new AccAttributes(); } mCachedFields->SetAttribute(nsGkAtoms::offset, std::move(newOffsets)); return *mCachedFields->GetAttribute>(nsGkAtoms::offset); } template void RemoteAccessibleBase::SetCaretOffset(int32_t aOffset) { Unused << mDoc->SendSetCaretOffset(mID, aOffset); } template Maybe RemoteAccessibleBase::GetIntARIAAttr( nsAtom* aAttrName) const { if (RefPtr attrs = GetCachedARIAAttributes()) { if (auto val = attrs->GetAttribute(aAttrName)) { return val; } } return Nothing(); } template class RemoteAccessibleBase; } // namespace a11y } // namespace mozilla