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

ash / webui / common / backend / accelerator_fetcher.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 ASH_WEBUI_COMMON_BACKEND_ACCELERATOR_FETCHER_H_
#define ASH_WEBUI_COMMON_BACKEND_ACCELERATOR_FETCHER_H_

#include "ash/accelerators/ash_accelerator_configuration.h"
#include "ash/public/mojom/accelerator_actions.mojom.h"
#include "ash/webui/common/mojom/accelerator_fetcher.mojom.h"
#include "base/memory/weak_ptr.h"
#include "mojo/public/cpp/bindings/pending_remote.h"
#include "mojo/public/cpp/bindings/receiver.h"
#include "mojo/public/cpp/bindings/remote_set.h"

namespace ash {

// Provides customized shortcut input accelerators and displays the
// accelerators in the frontend apps. Each app will create one instance
// of AcceleratorFetcherPovider.
class AcceleratorFetcher : public common::mojom::AcceleratorFetcher,
                           public AshAcceleratorConfiguration::Observer {
 public:
  AcceleratorFetcher();

  ~AcceleratorFetcher() override;

  void BindInterface(
      mojo::PendingReceiver<common::mojom::AcceleratorFetcher> receiver);
  void GetMetaKeyToDisplay(GetMetaKeyToDisplayCallback callback) override;

  // common::mojom::AcceleratorFetcher:
  void ObserveAcceleratorChanges(
      const std::vector<AcceleratorAction>& actionIds,
      mojo::PendingRemote<common::mojom::AcceleratorFetcherObserver> observer)
      override;

  // shortcut_ui::AcceleratorConfigurationProvider:
  void OnAcceleratorsUpdated() override;

  void FlushMojoForTesting();

 private:
  // Handlers for disconnection of observers.
  void OnObserverDisconnect(mojo::RemoteSetElementId id);

  mojo::Receiver<common::mojom::AcceleratorFetcher>
      accelerator_fetcher_receiver_{this};

  // Storage of accelerator actions for each remote receiver, keyed by
  // mojo::RemoteSetElementId in |accelerator_observers_|.
  std::map<mojo::RemoteSetElementId, std::vector<AcceleratorAction>>
      actions_for_receivers_;
  mojo::RemoteSet<common::mojom::AcceleratorFetcherObserver>
      accelerator_observers_;
  base::WeakPtrFactory<AcceleratorFetcher> weak_ptr_factory_{this};
};

}  // namespace ash

#endif  // ASH_WEBUI_COMMON_BACKEND_ACCELERATOR_FETCHER_H_