1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "ash/events/event_rewriter_controller_impl.h"
6 
7 #include <utility>
8 
9 #include "ash/display/mirror_window_controller.h"
10 #include "ash/display/privacy_screen_controller.h"
11 #include "ash/display/window_tree_host_manager.h"
12 #include "ash/events/accessibility_event_rewriter.h"
13 #include "ash/events/keyboard_driven_event_rewriter.h"
14 #include "ash/public/cpp/accessibility_event_rewriter_delegate.h"
15 #include "ash/shell.h"
16 #include "ash/sticky_keys/sticky_keys_controller.h"
17 #include "base/command_line.h"
18 #include "ui/accessibility/accessibility_switches.h"
19 #include "ui/aura/env.h"
20 #include "ui/aura/window_tree_host.h"
21 #include "ui/events/event_sink.h"
22 #include "ui/events/event_source.h"
23 
24 namespace ash {
25 
26 // static
Get()27 EventRewriterController* EventRewriterController::Get() {
28   return Shell::HasInstance() ? Shell::Get()->event_rewriter_controller()
29                               : nullptr;
30 }
31 
EventRewriterControllerImpl()32 EventRewriterControllerImpl::EventRewriterControllerImpl() {
33   // Add the controller as an observer for new root windows.
34   aura::Env::GetInstance()->AddObserver(this);
35 }
36 
~EventRewriterControllerImpl()37 EventRewriterControllerImpl::~EventRewriterControllerImpl() {
38   aura::Env::GetInstance()->RemoveObserver(this);
39   // Remove the rewriters from every root window EventSource and destroy them.
40   for (const auto& rewriter : rewriters_) {
41     for (auto* window : Shell::GetAllRootWindows())
42       window->GetHost()->GetEventSource()->RemoveEventRewriter(rewriter.get());
43   }
44   rewriters_.clear();
45 }
46 
Initialize(ui::EventRewriterChromeOS::Delegate * event_rewriter_delegate,AccessibilityEventRewriterDelegate * accessibility_event_rewriter_delegate)47 void EventRewriterControllerImpl::Initialize(
48     ui::EventRewriterChromeOS::Delegate* event_rewriter_delegate,
49     AccessibilityEventRewriterDelegate* accessibility_event_rewriter_delegate) {
50   std::unique_ptr<KeyboardDrivenEventRewriter> keyboard_driven_event_rewriter =
51       std::make_unique<KeyboardDrivenEventRewriter>();
52   keyboard_driven_event_rewriter_ = keyboard_driven_event_rewriter.get();
53 
54   bool privacy_screen_supported = false;
55   if (Shell::Get()->privacy_screen_controller() &&
56       Shell::Get()->privacy_screen_controller()->IsSupported()) {
57     privacy_screen_supported = true;
58   }
59   std::unique_ptr<ui::EventRewriterChromeOS> event_rewriter_chromeos =
60       std::make_unique<ui::EventRewriterChromeOS>(
61           event_rewriter_delegate, Shell::Get()->sticky_keys_controller(),
62           privacy_screen_supported);
63 
64   std::unique_ptr<AccessibilityEventRewriter> accessibility_event_rewriter =
65       std::make_unique<AccessibilityEventRewriter>(
66           event_rewriter_chromeos.get(), accessibility_event_rewriter_delegate);
67   accessibility_event_rewriter_ = accessibility_event_rewriter.get();
68 
69   // EventRewriters are notified in the order they are added.
70   AddEventRewriter(std::move(accessibility_event_rewriter));
71   AddEventRewriter(std::move(keyboard_driven_event_rewriter));
72   AddEventRewriter(std::move(event_rewriter_chromeos));
73 }
74 
AddEventRewriter(std::unique_ptr<ui::EventRewriter> rewriter)75 void EventRewriterControllerImpl::AddEventRewriter(
76     std::unique_ptr<ui::EventRewriter> rewriter) {
77   // Add the rewriters to each existing root window EventSource.
78   for (auto* window : Shell::GetAllRootWindows())
79     window->GetHost()->GetEventSource()->AddEventRewriter(rewriter.get());
80 
81   // In case there are any mirroring displays, their hosts' EventSources won't
82   // be included above.
83   const auto* mirror_window_controller =
84       Shell::Get()->window_tree_host_manager()->mirror_window_controller();
85   for (auto* window : mirror_window_controller->GetAllRootWindows())
86     window->GetHost()->GetEventSource()->AddEventRewriter(rewriter.get());
87 
88   rewriters_.push_back(std::move(rewriter));
89 }
90 
SetKeyboardDrivenEventRewriterEnabled(bool enabled)91 void EventRewriterControllerImpl::SetKeyboardDrivenEventRewriterEnabled(
92     bool enabled) {
93   keyboard_driven_event_rewriter_->set_enabled(enabled);
94 }
95 
SetArrowToTabRewritingEnabled(bool enabled)96 void EventRewriterControllerImpl::SetArrowToTabRewritingEnabled(bool enabled) {
97   keyboard_driven_event_rewriter_->set_arrow_to_tab_rewriting_enabled(enabled);
98 }
99 
OnUnhandledSpokenFeedbackEvent(std::unique_ptr<ui::Event> event)100 void EventRewriterControllerImpl::OnUnhandledSpokenFeedbackEvent(
101     std::unique_ptr<ui::Event> event) {
102   accessibility_event_rewriter_->OnUnhandledSpokenFeedbackEvent(
103       std::move(event));
104 }
105 
CaptureAllKeysForSpokenFeedback(bool capture)106 void EventRewriterControllerImpl::CaptureAllKeysForSpokenFeedback(
107     bool capture) {
108   accessibility_event_rewriter_->set_chromevox_capture_all_keys(capture);
109 }
110 
SetSendMouseEventsToDelegate(bool value)111 void EventRewriterControllerImpl::SetSendMouseEventsToDelegate(bool value) {
112   accessibility_event_rewriter_->set_chromevox_send_mouse_events(value);
113 }
114 
OnHostInitialized(aura::WindowTreeHost * host)115 void EventRewriterControllerImpl::OnHostInitialized(
116     aura::WindowTreeHost* host) {
117   for (const auto& rewriter : rewriters_)
118     host->GetEventSource()->AddEventRewriter(rewriter.get());
119 }
120 
121 }  // namespace ash
122