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

ash / user_education / welcome_tour / welcome_tour_accelerator_handler.cc [blame]

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

#include "ash/user_education/welcome_tour/welcome_tour_accelerator_handler.h"

#include "ash/accelerators/ash_accelerator_configuration.h"
#include "ash/constants/ash_features.h"
#include "ash/shell.h"
#include "base/ranges/algorithm.h"
#include "base/task/sequenced_task_runner.h"
#include "ui/events/event.h"
#include "ui/events/event_target.h"

namespace ash {

// static
constexpr std::array<WelcomeTourAcceleratorHandler::AllowedAction, 13>
    WelcomeTourAcceleratorHandler::kAllowedActions;

WelcomeTourAcceleratorHandler::WelcomeTourAcceleratorHandler(
    base::RepeatingClosure abort_tour_callback)
    : abort_tour_callback_(abort_tour_callback) {
  Shell::Get()->AddPreTargetHandler(this, ui::EventTarget::Priority::kSystem);
}

WelcomeTourAcceleratorHandler::~WelcomeTourAcceleratorHandler() {
  Shell::Get()->RemovePreTargetHandler(this);
}

void WelcomeTourAcceleratorHandler::OnKeyEvent(ui::KeyEvent* event) {
  const AcceleratorAction* const action =
      Shell::Get()->ash_accelerator_configuration()->FindAcceleratorAction(
          ui::Accelerator(*event));
  if (!action) {
    // Return early if `event` does not trigger any accelerator action.
    return;
  }

  auto action_it =
      base::ranges::find(kAllowedActions, *action, &AllowedAction::action);

  if (action_it == kAllowedActions.cend()) {
    // Block `event` if `action` is not allowed.
    event->StopPropagation();
  } else if (action_it->aborts_tour) {
    if (action_it->action == AcceleratorAction::kToggleSpokenFeedback &&
        features::IsWelcomeTourChromeVoxSupported()) {
      return;
    }

    // Aborting the Welcome Tour could affect the enabling of `action`.
    // Therefore, abort the Welcome Tour asynchronously.
    base::SequencedTaskRunner::GetCurrentDefault()->PostTask(
        FROM_HERE, abort_tour_callback_);
  }
}

}  // namespace ash