1
    2
    3
    4
    5
    6
    7
    8
    9
   10
   11
   12
   13
   14
   15
   16
   17
   18
   19
   20
   21
   22
   23
   24
   25
   26
   27
   28
   29
   30
   31
   32
   33
   34
   35
   36
   37
   38
   39
   40
   41
   42
   43
   44
   45
   46
   47
   48
   49
   50
   51
   52
   53
   54
   55
   56
   57
   58
   59
   60
   61
   62
   63
   64
   65
   66
   67
   68
   69
   70
   71
   72
   73
   74
   75
   76
   77
   78
   79
   80
   81
   82
   83
   84
   85
   86

content / browser / accessibility / scoped_mode_collection.h [blame]

// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CONTENT_BROWSER_ACCESSIBILITY_SCOPED_MODE_COLLECTION_H_
#define CONTENT_BROWSER_ACCESSIBILITY_SCOPED_MODE_COLLECTION_H_

#include <list>
#include <memory>

#include "base/functional/callback.h"
#include "base/memory/raw_ptr.h"
#include "content/common/content_export.h"
#include "ui/accessibility/ax_mode.h"

namespace content {

class ScopedAccessibilityMode;

// A collection of ScopedAccessibilityMode instances. The collection maintains
// an always up-to-date view of the union of all contained scopers, accessible
// via `accessibility_mode()`. Any change to this value (via calls to `Add()` to
// add a new item to the collection or via destruction of a scoper belonging to
// the collection) results in running the callback provided at construction. It
// is permissible for the collection to be destroyed while scopers minted from
// it remain alive.
class CONTENT_EXPORT ScopedModeCollection {
 public:
  // The type of a callback that is run when the effective mode for the
  // collection changes (i.e., when the union of all mode flags indicated by
  // the scopers in the collection changes).
  using OnModeChangedCallback =
      base::RepeatingCallback<void(ui::AXMode old_mode, ui::AXMode new_mode)>;

  // `on_mode_changed` is run on any change to the collection that results in
  // a different combined accessibility mode.
  explicit ScopedModeCollection(OnModeChangedCallback on_mode_changed);
  ScopedModeCollection(const ScopedModeCollection&) = delete;
  ScopedModeCollection& operator=(const ScopedModeCollection&) = delete;
  ~ScopedModeCollection();

  // Returns the union of all mode flags indicated by the scopers in the
  // collection. Recalculated on each addition, removal, and modification of
  // a scoper in the collection.
  ui::AXMode accessibility_mode() const { return accessibility_mode_; }

  // Returns a new scoper for `mode`, recalculating the effective
  // accessibility mode for the collection and running `on_mode_changed` if it
  // has changed. When the returned scoper is destroyed, the effective
  // accessibility mode for the collection is once again computed and
  // `on_mode_changed` is run if it has changed.
  std::unique_ptr<ScopedAccessibilityMode> Add(ui::AXMode mode);

  // Returns true if the collection is empty.
  bool empty() const { return scopers_.empty(); }

 private:
  class ScopedAccessibilityModeImpl;

  using ScoperContainer = std::list<raw_ptr<ScopedAccessibilityModeImpl>>;
  using ScoperKey = ScoperContainer::iterator;

  // Removes the scoper identified by `scoper_key`, recalculates the effective
  // accessibility mode for the collection, and runs `on_mode_changed` if it
  // has changed.
  void OnDestroyed(ScoperKey scoper_key);

  // Recalculate the effective mode following a change to the collection of
  // scopers. Runs `on_mode_changed` if there is a change.
  void RecalculateEffectiveModeAndNotify();

  // Run on any change to the collection that results in a different combined
  // accessibility mode.
  const OnModeChangedCallback on_mode_changed_;

  // The collection of ScopedAccessibilityMode instances.
  ScoperContainer scopers_;

  // The effective accessibility mode computed from all scopers held by this
  // instance. Recalculated each time a scoper is added, removed, or modified.
  ui::AXMode accessibility_mode_;
};

}  // namespace content

#endif  // CONTENT_BROWSER_ACCESSIBILITY_SCOPED_MODE_COLLECTION_H_