File: input_injector_chromeos.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (347 lines) | stat: -rw-r--r-- 12,320 bytes parent folder | download | duplicates (3)
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "remoting/host/input_injector_chromeos.h"

#include <memory>
#include <set>
#include <string>
#include <utility>

#include "ash/display/window_tree_host_manager.h"
#include "ash/shell.h"
#include "base/functional/bind.h"
#include "base/functional/callback_helpers.h"
#include "base/i18n/icu_string_conversions.h"
#include "base/location.h"
#include "base/notimplemented.h"
#include "base/strings/utf_string_conversions.h"
#include "base/system/sys_info.h"
#include "base/task/single_thread_task_runner.h"
#include "remoting/host/chromeos/point_transformer.h"
#include "remoting/host/clipboard.h"
#include "remoting/proto/internal.pb.h"
#include "ui/aura/client/cursor_client.h"
#include "ui/aura/window.h"
#include "ui/aura/window_tree_host.h"
#include "ui/base/ime/ash/ime_keyboard.h"
#include "ui/base/ime/ash/input_method_manager.h"
#include "ui/base/ime/input_method.h"
#include "ui/base/ime/text_input_client.h"
#include "ui/events/keycodes/dom/dom_code.h"
#include "ui/events/keycodes/dom/keycode_converter.h"
#include "ui/ozone/public/ozone_platform.h"
#include "ui/ozone/public/system_input_injector.h"

namespace remoting {

using protocol::ClipboardEvent;
using protocol::KeyEvent;
using protocol::MouseEvent;
using protocol::TextEvent;
using protocol::TouchEvent;

namespace {

ui::EventFlags MouseButtonToUIFlags(MouseEvent::MouseButton button) {
  switch (button) {
    case MouseEvent::BUTTON_LEFT:
      return ui::EF_LEFT_MOUSE_BUTTON;
    case MouseEvent::BUTTON_RIGHT:
      return ui::EF_RIGHT_MOUSE_BUTTON;
    case MouseEvent::BUTTON_MIDDLE:
      return ui::EF_MIDDLE_MOUSE_BUTTON;
    default:
      NOTREACHED();
  }
}

// Check if the given key could be mapped to caps lock
bool IsLockKey(ui::DomCode dom_code) {
  switch (dom_code) {
    // Ignores all the keys that could possibly be mapped to Caps Lock in event
    // rewriter. Please refer to ui::EventRewriterAsh::RewriteModifierKeys.
    case ui::DomCode::F16:
    case ui::DomCode::CAPS_LOCK:
    case ui::DomCode::META_LEFT:
    case ui::DomCode::META_RIGHT:
    case ui::DomCode::CONTROL_LEFT:
    case ui::DomCode::CONTROL_RIGHT:
    case ui::DomCode::ALT_LEFT:
    case ui::DomCode::ALT_RIGHT:
    case ui::DomCode::ESCAPE:
    case ui::DomCode::BACKSPACE:
      return true;
    default:
      return false;
  }
}

// If caps_lock is specified, sets local keyboard state to match.
void SetCapsLockState(bool caps_lock) {
  auto* ime = ash::input_method::InputMethodManager::Get();
  ime->GetImeKeyboard()->SetCapsLockEnabled(caps_lock);
}

class SystemInputInjectorStub : public ui::SystemInputInjector {
 public:
  SystemInputInjectorStub() {
    LOG(WARNING)
        << "Using stubbed input injector; All CRD user input will be ignored.";
  }
  SystemInputInjectorStub(const SystemInputInjectorStub&) = delete;
  SystemInputInjectorStub& operator=(const SystemInputInjectorStub&) = delete;
  ~SystemInputInjectorStub() override = default;

  // SystemInputInjector implementation:
  void SetDeviceId(int device_id) override {}
  void MoveCursorTo(const gfx::PointF& location) override {}
  void InjectMouseButton(ui::EventFlags button, bool down) override {}
  void InjectMouseWheel(int delta_x, int delta_y) override {}
  void InjectKeyEvent(ui::DomCode physical_key,
                      bool down,
                      bool suppress_auto_repeat) override {}
};

}  // namespace

// This class is run exclusively on the UI thread of the browser process.
class InputInjectorChromeos::Core {
 public:
  Core();

  Core(const Core&) = delete;
  Core& operator=(const Core&) = delete;

  ~Core();

  // Mirrors the public InputInjectorChromeos interface.
  void InjectClipboardEvent(const ClipboardEvent& event);
  void InjectKeyEvent(const KeyEvent& event);
  void InjectTextEvent(const TextEvent& event);
  void InjectMouseEvent(const MouseEvent& event);
  void Start(std::unique_ptr<protocol::ClipboardStub> client_clipboard);
  void StartWithDelegate(
      std::unique_ptr<ui::SystemInputInjector> delegate,
      std::unique_ptr<protocol::ClipboardStub> client_clipboard);

 private:
  void SetLockStates(uint32_t states);

  void InjectMouseMove(const MouseEvent& event);

  bool hide_cursor_on_disconnect_ = false;
  std::unique_ptr<ui::SystemInputInjector> delegate_;
  std::unique_ptr<Clipboard> clipboard_;
};

InputInjectorChromeos::Core::Core() = default;

InputInjectorChromeos::Core::~Core() {
  if (hide_cursor_on_disconnect_) {
    aura::client::CursorClient* cursor_client =
        aura::client::GetCursorClient(ash::Shell::GetPrimaryRootWindow());
    if (cursor_client) {
      cursor_client->HideCursor();
    }
  }
}

void InputInjectorChromeos::Core::InjectClipboardEvent(
    const ClipboardEvent& event) {
  clipboard_->InjectClipboardEvent(event);
}

void InputInjectorChromeos::Core::InjectKeyEvent(const KeyEvent& event) {
  DCHECK(event.has_pressed());
  DCHECK(event.has_usb_keycode());

  ui::DomCode dom_code =
      ui::KeycodeConverter::UsbKeycodeToDomCode(event.usb_keycode());

  if (event.pressed() && !IsLockKey(dom_code)) {
    if (event.has_caps_lock_state()) {
      SetCapsLockState(event.caps_lock_state());
    } else if (event.has_lock_states()) {
      SetCapsLockState((event.lock_states() &
                        protocol::KeyEvent::LOCK_STATES_CAPSLOCK) != 0);
    }
  }

  // Ignore events which can't be mapped.
  if (dom_code != ui::DomCode::NONE) {
    VLOG(3) << "Injecting key " << (event.pressed() ? "down" : "up")
            << " event.";
    delegate_->InjectKeyEvent(dom_code, event.pressed(),
                              true /* suppress_auto_repeat */);
  }
}

void InputInjectorChromeos::Core::InjectTextEvent(const TextEvent& event) {
  DCHECK(event.has_text());

  aura::Window* root_window = ash::Shell::GetPrimaryRootWindow();
  if (!root_window) {
    LOG(ERROR) << "root_window is null, can't inject text.";
    return;
  }
  aura::WindowTreeHost* window_tree_host = root_window->GetHost();
  if (!window_tree_host) {
    LOG(ERROR) << "window_tree_host is null, can't inject text.";
    return;
  }
  ui::InputMethod* input_method = window_tree_host->GetInputMethod();
  if (!input_method) {
    LOG(ERROR) << "input_method is null, can't inject text.";
    return;
  }
  ui::TextInputClient* text_input_client = input_method->GetTextInputClient();
  if (!text_input_client ||
      text_input_client->GetTextInputType() == ui::TEXT_INPUT_TYPE_NONE) {
    LOG(ERROR) << "text_input_client is null or none, can't inject text.";
    return;
  }

  std::string normalized_str;
  base::ConvertToUtf8AndNormalize(event.text(), base::kCodepageUTF8,
                                  &normalized_str);
  std::u16string utf16_string = base::UTF8ToUTF16(normalized_str);

  text_input_client->InsertText(
      utf16_string,
      ui::TextInputClient::InsertTextCursorBehavior::kMoveCursorAfterText);
}

void InputInjectorChromeos::Core::InjectMouseEvent(const MouseEvent& event) {
  if (event.has_button() && event.has_button_down()) {
    if (event.has_x() || event.has_y()) {
      // Ensure the mouse button click happens at the correct position.
      InjectMouseMove(event);
    }
    delegate_->InjectMouseButton(MouseButtonToUIFlags(event.button()),
                                 event.button_down());
  } else if (event.has_wheel_delta_x() || event.has_wheel_delta_y()) {
    delegate_->InjectMouseWheel(event.wheel_delta_x(), event.wheel_delta_y());
  } else if (event.has_x() || event.has_y()) {
    InjectMouseMove(event);
  } else {
    LOG(WARNING) << "Ignoring mouse event of unknown type";
  }
}

void InputInjectorChromeos::Core::InjectMouseMove(const MouseEvent& event) {
  gfx::PointF location_in_screen_in_dip = gfx::PointF(event.x(), event.y());
  gfx::PointF location_in_screen_in_pixels =
      PointTransformer::ConvertScreenInDipToScreenInPixel(
          location_in_screen_in_dip);

  delegate_->MoveCursorTo(location_in_screen_in_pixels);
}

void InputInjectorChromeos::Core::Start(
    std::unique_ptr<protocol::ClipboardStub> client_clipboard) {
  auto delegate = ui::OzonePlatform::GetInstance()->CreateSystemInputInjector();
  if (!delegate && !base::SysInfo::IsRunningOnChromeOS()) {
    // This happens when directly running the Chrome binary on linux.
    // We'll simply ignore all input there (instead of crashing).
    // Note: it would be nicer to swap this out with input_injector_x11.cc
    // on linux instead (and properly handle the input), but that runs into
    // dependency issues.
    delegate = std::make_unique<SystemInputInjectorStub>();
  }
  CHECK(delegate);

  StartWithDelegate(std::move(delegate), std::move(client_clipboard));
}

void InputInjectorChromeos::Core::StartWithDelegate(
    std::unique_ptr<ui::SystemInputInjector> delegate,
    std::unique_ptr<protocol::ClipboardStub> client_clipboard) {
  delegate_ = std::move(delegate);

  delegate_->SetDeviceId(ui::ED_REMOTE_INPUT_DEVICE);

  // Implemented by remoting::ClipboardAura.
  clipboard_ = Clipboard::Create();
  clipboard_->Start(std::move(client_clipboard));

  // If the cursor was hidden before we start injecting input then we should try
  // to restore its state when the remote user disconnects.  The main scenario
  // where this is important is for devices in non-interactive Kiosk mode.
  // Since no one is interacting with the screen in this mode, we will leave a
  // visible cursor after disconnecting which can't be hidden w/o restarting.
  aura::client::CursorClient* cursor_client =
      aura::client::GetCursorClient(ash::Shell::GetPrimaryRootWindow());
  if (cursor_client) {
    hide_cursor_on_disconnect_ = !cursor_client->IsCursorVisible();
  }
}

InputInjectorChromeos::InputInjectorChromeos(
    scoped_refptr<base::SingleThreadTaskRunner> task_runner)
    : input_task_runner_(task_runner), core_(std::make_unique<Core>()) {}

InputInjectorChromeos::~InputInjectorChromeos() {
  input_task_runner_->DeleteSoon(FROM_HERE, core_.release());
}

void InputInjectorChromeos::InjectClipboardEvent(const ClipboardEvent& event) {
  input_task_runner_->PostTask(
      FROM_HERE, base::BindOnce(&Core::InjectClipboardEvent,
                                base::Unretained(core_.get()), event));
}

void InputInjectorChromeos::InjectKeyEvent(const KeyEvent& event) {
  input_task_runner_->PostTask(
      FROM_HERE, base::BindOnce(&Core::InjectKeyEvent,
                                base::Unretained(core_.get()), event));
}

void InputInjectorChromeos::InjectTextEvent(const TextEvent& event) {
  input_task_runner_->PostTask(
      FROM_HERE, base::BindOnce(&Core::InjectTextEvent,
                                base::Unretained(core_.get()), event));
}

void InputInjectorChromeos::InjectMouseEvent(const MouseEvent& event) {
  input_task_runner_->PostTask(
      FROM_HERE, base::BindOnce(&Core::InjectMouseEvent,
                                base::Unretained(core_.get()), event));
}

void InputInjectorChromeos::InjectTouchEvent(const TouchEvent& event) {
  NOTIMPLEMENTED() << "Raw touch event injection not implemented for ChromeOS.";
}

void InputInjectorChromeos::Start(
    std::unique_ptr<protocol::ClipboardStub> client_clipboard) {
  input_task_runner_->PostTask(
      FROM_HERE, base::BindOnce(&Core::Start, base::Unretained(core_.get()),
                                std::move(client_clipboard)));
}

void InputInjectorChromeos::StartForTesting(
    std::unique_ptr<ui::SystemInputInjector> input_injector,
    std::unique_ptr<protocol::ClipboardStub> client_clipboard) {
  input_task_runner_->PostTask(
      FROM_HERE,
      base::BindOnce(&Core::StartWithDelegate, base::Unretained(core_.get()),
                     std::move(input_injector), std::move(client_clipboard)));
}

// static
std::unique_ptr<InputInjector> InputInjector::Create(
    scoped_refptr<base::SingleThreadTaskRunner> input_task_runner,
    scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner) {
  // The Ozone input injector must be called on the UI task runner of the
  // browser process.
  return std::make_unique<InputInjectorChromeos>(ui_task_runner);
}

// static
bool InputInjector::SupportsTouchEvents() {
  return false;
}

}  // namespace remoting