|
| 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 |
0 commit comments