Skip to content
This repository was archived by the owner on Jul 9, 2025. It is now read-only.

Commit 8cf50fc

Browse files
author
Nathan LaPre
committed
Bug 1794974: Part 4: Add ability to easily query, request new cache domains,r=Jamie
This revision adds code that will make it simple to request new cache domains as as response to assistive technology queries. Differential Revision: https://phabricator.services.mozilla.com/D220038
1 parent a4eaf2c commit 8cf50fc

File tree

3 files changed

+81
-1
lines changed

3 files changed

+81
-1
lines changed

accessible/base/CacheConstants.cpp

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2+
/* vim: set expandtab shiftwidth=2 tabstop=2: */
3+
/* This Source Code Form is subject to the terms of the Mozilla Public
4+
* License, v. 2.0. If a copy of the MPL was not distributed with this
5+
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6+
7+
#include "CacheConstants.h"
8+
#include "nsAccessibilityService.h"
9+
10+
namespace mozilla::a11y {
11+
12+
// Get the set of cache domains required by the given cache domains, which will
13+
// always be equal to or a superset of the given set of cache domains.
14+
static uint64_t GetCacheDomainSuperset(uint64_t aCacheDomains) {
15+
uint64_t allNecessaryDomains = aCacheDomains;
16+
if (aCacheDomains & CacheDomain::TextOffsetAttributes) {
17+
allNecessaryDomains |= CacheDomain::Text;
18+
}
19+
if (aCacheDomains & CacheDomain::TextBounds) {
20+
allNecessaryDomains |= CacheDomain::Text;
21+
allNecessaryDomains |= CacheDomain::Bounds;
22+
}
23+
MOZ_ASSERT((allNecessaryDomains & aCacheDomains) == aCacheDomains,
24+
"Return value is not a superset of the input.");
25+
return allNecessaryDomains;
26+
}
27+
28+
bool DomainsAreActive(uint64_t aRequiredCacheDomains) {
29+
const uint64_t activeCacheDomains =
30+
nsAccessibilityService::GetActiveCacheDomains();
31+
const bool allRequiredDomainsAreActive =
32+
(aRequiredCacheDomains & ~activeCacheDomains) == 0;
33+
return allRequiredDomainsAreActive;
34+
}
35+
36+
bool RequestDomainsIfInactive(uint64_t aRequiredCacheDomains) {
37+
nsAccessibilityService* accService = GetAccService();
38+
if (!accService) {
39+
return true;
40+
}
41+
const uint64_t activeCacheDomains =
42+
nsAccessibilityService::GetActiveCacheDomains();
43+
const bool isMissingRequiredCacheDomain =
44+
(aRequiredCacheDomains & ~activeCacheDomains) != 0;
45+
if (isMissingRequiredCacheDomain) {
46+
aRequiredCacheDomains = GetCacheDomainSuperset(aRequiredCacheDomains);
47+
48+
const uint64_t cacheDomains = aRequiredCacheDomains | activeCacheDomains;
49+
#if defined(ANDROID)
50+
// We might not be on the main Android thread, but we must be in order to
51+
// send IPDL messages. Dispatch to the main thread to set cache domains.
52+
NS_DispatchToMainThread(
53+
NS_NewRunnableFunction("a11y::SetCacheDomains", [cacheDomains]() {
54+
if (nsAccessibilityService* accService = GetAccService()) {
55+
accService->SetCacheDomains(cacheDomains);
56+
}
57+
}));
58+
return true;
59+
#endif
60+
61+
accService->SetCacheDomains(cacheDomains);
62+
return true;
63+
}
64+
return false;
65+
}
66+
67+
} // namespace mozilla::a11y

accessible/base/CacheConstants.h

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ constexpr int32_t kNumbersInRect = 4;
104104
* cache should generally use these aliases rather than using nsAtoms directly.
105105
* There are two exceptions:
106106
* 1. Some ARIA attributes are copied directly from the DOM node, so these
107-
* aren't aliased. Specifically, aria-level, aria-posinset and aria-setsize
107+
* aren't aliased. Specifically, aria-level, aria-posinset and aria-setsize
108108
* are copied as separate cache keys as part of CacheDomain::GroupInfo.
109109
* 2. Keys for relations are defined in kRelationTypeAtoms above.
110110
*/
@@ -257,6 +257,18 @@ class CacheKey {
257257
static constexpr nsStaticAtom* Viewport = nsGkAtoms::viewport;
258258
};
259259

260+
// Return true if the given cache domains are already active.
261+
bool DomainsAreActive(uint64_t aRequiredCacheDomains);
262+
263+
// Check whether the required cache domains are active. If they aren't, then
264+
// request the requisite cache domains and return true. This function returns
265+
// false if all required domains are already active.
266+
bool RequestDomainsIfInactive(uint64_t aRequiredCacheDomains);
267+
268+
#define ASSERT_DOMAINS_ACTIVE(aCacheDomains) \
269+
MOZ_ASSERT(DomainsAreActive(aCacheDomains), \
270+
"Required domain(s) are not currently active.")
271+
260272
} // namespace a11y
261273
} // namespace mozilla
262274

accessible/base/moz.build

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ UNIFIED_SOURCES += [
4747
"ARIAMap.cpp",
4848
"ARIAStateMap.cpp",
4949
"Asserts.cpp",
50+
"CacheConstants.cpp",
5051
"CachedTableAccessible.cpp",
5152
"CssAltContent.cpp",
5253
"DocManager.cpp",

0 commit comments

Comments
 (0)