blob: 01940f460008398a24b1b0de77ba7011280482b6 [file] [log] [blame]
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ash/components/shortcut_viewer/shortcut_viewer_application.h"
#include "ash/components/shortcut_viewer/last_window_closed_observer.h"
#include "ash/components/shortcut_viewer/views/keyboard_shortcut_view.h"
#include "ash/public/cpp/ash_client.h"
#include "base/bind.h"
#include "base/time/time.h"
#include "base/trace_event/trace_event.h"
#include "services/service_manager/public/cpp/connector.h"
#include "ui/events/devices/input_device_manager.h"
#include "ui/views/mus/aura_init.h"
namespace keyboard_shortcut_viewer {
ShortcutViewerApplication::ShortcutViewerApplication(
service_manager::mojom::ServiceRequest request)
: service_binding_(this, std::move(request)) {
registry_.AddInterface<shortcut_viewer::mojom::ShortcutViewer>(
base::BindRepeating(&ShortcutViewerApplication::AddBinding,
base::Unretained(this)));
}
ShortcutViewerApplication::~ShortcutViewerApplication() = default;
// static
void ShortcutViewerApplication::RegisterForTraceEvents() {
TRACE_EVENT0("shortcut_viewer", "ignored");
}
void ShortcutViewerApplication::OnStart() {
views::AuraInit::InitParams params;
params.connector = service_binding_.GetConnector();
params.identity = service_binding_.identity();
params.register_path_provider = false;
params.use_accessibility_host = true;
aura_init_ = views::AuraInit::Create(params);
if (!aura_init_) {
Terminate();
return;
}
// Register as a client of the window manager.
ash::ash_client::Init();
// Quit the application when the window is closed.
last_window_closed_observer_ = std::make_unique<LastWindowClosedObserver>(
base::BindRepeating(&ShortcutViewerApplication::OnLastWindowClosed,
base::Unretained(this)));
}
void ShortcutViewerApplication::OnBindInterface(
const service_manager::BindSourceInfo& remote_info,
const std::string& interface_name,
mojo::ScopedMessagePipeHandle interface_pipe) {
registry_.BindInterface(interface_name, std::move(interface_pipe));
}
void ShortcutViewerApplication::OnDeviceListsComplete() {
ui::InputDeviceManager::GetInstance()->RemoveObserver(this);
KeyboardShortcutView::Toggle(user_gesture_time_, nullptr);
}
void ShortcutViewerApplication::Toggle(base::TimeTicks user_gesture_time) {
user_gesture_time_ = user_gesture_time;
// This app needs InputDeviceManager information that loads asynchronously via
// InputDeviceClient. If the device list is incomplete, wait for it to load.
DCHECK(ui::InputDeviceManager::HasInstance());
if (ui::InputDeviceManager::GetInstance()->AreDeviceListsComplete())
KeyboardShortcutView::Toggle(user_gesture_time_, nullptr);
else
ui::InputDeviceManager::GetInstance()->AddObserver(this);
}
void ShortcutViewerApplication::AddBinding(
shortcut_viewer::mojom::ShortcutViewerRequest request) {
shortcut_viewer_binding_.Close();
shortcut_viewer_binding_.Bind(std::move(request));
}
void ShortcutViewerApplication::OnLastWindowClosed() {
service_binding_.RequestClose();
}
} // namespace keyboard_shortcut_viewer