tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

CacheConstants.cpp (2694B)


      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    if (!accService->ShouldAllowNewCacheDomains()) {
     47      // Return true to indicate that the domain is not active, but don't
     48      // actually request it.
     49      return true;
     50    }
     51    aRequiredCacheDomains = GetCacheDomainSuperset(aRequiredCacheDomains);
     52 
     53    const uint64_t cacheDomains = aRequiredCacheDomains | activeCacheDomains;
     54 #if defined(ANDROID)
     55    // We might not be on the main Android thread, but we must be in order to
     56    // send IPDL messages. Dispatch to the main thread to set cache domains.
     57    NS_DispatchToMainThread(
     58        NS_NewRunnableFunction("a11y::SetCacheDomains", [cacheDomains]() {
     59          if (nsAccessibilityService* accService = GetAccService()) {
     60            accService->SetCacheDomains(cacheDomains);
     61          }
     62        }));
     63 #else
     64    accService->SetCacheDomains(cacheDomains);
     65 #endif
     66 
     67    return true;
     68  }
     69  return false;
     70 }
     71 
     72 }  // namespace mozilla::a11y