File: touch_ui_controller.cc

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (308 lines) | stat: -rw-r--r-- 10,241 bytes parent folder | download | duplicates (5)
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
// Copyright 2015 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ui/base/pointer/touch_ui_controller.h"

#include <memory>
#include <string>

#include "base/command_line.h"
#include "base/functional/bind.h"
#include "base/metrics/histogram_functions.h"
#include "base/metrics/user_metrics.h"
#include "base/no_destructor.h"
#include "base/task/current_thread.h"
#include "base/trace_event/trace_event.h"
#include "ui/base/ui_base_switches.h"

#if BUILDFLAG(IS_WIN)
#include "base/win/win_util.h"
#include "ui/gfx/win/singleton_hwnd.h"
#include "ui/gfx/win/singleton_hwnd_observer.h"
#endif

namespace ui {

namespace {

#if BUILDFLAG(USE_BLINK)
void RecordPointerDigitizerTypeOnStartup(const PointerDevice& device) {
  base::UmaHistogramEnumeration("Input.Digitizer.OnStartup", device.digitizer);
}

void RecordPointerDigitizerTypeOnConnected(const PointerDevice& device) {
  base::UmaHistogramEnumeration("Input.Digitizer.OnConnected",
                                device.digitizer);
}

void RecordPointerDigitizerTypeOnDisconnected(const PointerDevice& device) {
  base::UmaHistogramEnumeration("Input.Digitizer.OnDisconnected",
                                device.digitizer);
}

void RecordMaxTouchPointsHistogram(const char* histogram_name, int32_t sample) {
  // Record max touch points. Maximum value is 30, after which the values will
  // be recorded in an overflow bucket. The maximum of 30 is chosen based on 3
  // people interacting with both hands on a screen simultaneously.
  base::UmaHistogramExactLinear(histogram_name, sample, 30);
}

constexpr const char* GetMaxTouchPointsHistogramName(
    PointerDigitizerType type) {
  switch (type) {
    case PointerDigitizerType::kUnknown:
      return "Input.Digitizer.MaxTouchPoints.Unknown";
    case PointerDigitizerType::kDirectPen:
      return "Input.Digitizer.MaxTouchPoints.DirectPen";
    case PointerDigitizerType::kIndirectPen:
      return "Input.Digitizer.MaxTouchPoints.IndirectPen";
    case PointerDigitizerType::kTouch:
      return "Input.Digitizer.MaxTouchPoints.Touch";
    case PointerDigitizerType::kTouchPad:
      return "Input.Digitizer.MaxTouchPoints.TouchPad";
  }
  NOTREACHED();
}

void RecordPointerDigitizerTypeMaxTouchPoints(const PointerDevice& device) {
  RecordMaxTouchPointsHistogram(
      GetMaxTouchPointsHistogramName(device.digitizer),
      device.max_active_contacts);
}

void RecordMaxTouchPointsSupportedBySystem(int max_touch_points) {
  RecordMaxTouchPointsHistogram(
      "Input.Digitizer.MaxTouchPointsSupportedBySystemAtStartup",
      max_touch_points);
}
#endif  // BUILDFLAG(USE_BLINK)

#if BUILDFLAG(IS_WIN)

bool IsWndProcMessageObserved(UINT message) {
#if BUILDFLAG(USE_BLINK)
  return message == WM_SETTINGCHANGE || message == WM_POINTERDEVICECHANGE;
#elif   // BUILDFLAG(USE_BLINK)
  return message == WM_SETTINGCHANGE;
#endif  // BUILDFLAG(USE_BLINK)
}

void SequencedWndProcHandler(UINT message, WPARAM wparam, LPARAM lparam) {
  switch (message) {
    case WM_SETTINGCHANGE:
      TouchUiController::Get()->RefreshTabletMode();
      break;
#if BUILDFLAG(USE_BLINK)
    case WM_POINTERDEVICECHANGE:
      if (wparam == PDC_ARRIVAL) {
        TouchUiController::Get()->OnPointerDeviceConnected(
            reinterpret_cast<HANDLE>(lparam));
      } else if (wparam == PDC_REMOVAL) {
        TouchUiController::Get()->OnPointerDeviceDisconnected(
            reinterpret_cast<HANDLE>(lparam));
      }
      break;
#endif  // BUILDFLAG(USE_BLINK)
    default:
      NOTREACHED();
  }
}

void OnWndProc(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam) {
  if (!IsWndProcMessageObserved(message)) {
    return;
  }
  // Pass the work to a separate task to avoid possible jank when handling
  // winapi events. This also makes sure events are processed after
  // OnInitializePointerDevices which needs to run before handling
  // WM_POINTERDEVICECHANGE.
  base::SequencedTaskRunner::GetCurrentDefault()->PostTask(
      FROM_HERE,
      base::BindOnce(&SequencedWndProcHandler, message, wparam, lparam));
}

#endif  // BUILDFLAG(IS_WIN)

void RecordEnteredTouchMode() {
  base::RecordAction(base::UserMetricsAction("TouchMode.EnteredTouchMode"));
}

void RecordEnteredNonTouchMode() {
  base::RecordAction(base::UserMetricsAction("TouchMode.EnteredNonTouchMode"));
}

}  // namespace

TouchUiController::TouchUiScoperForTesting::TouchUiScoperForTesting(
    bool enabled,
    TouchUiController* controller)
    : controller_(controller),
      old_state_(controller_->SetTouchUiState(
          enabled ? TouchUiState::kEnabled : TouchUiState::kDisabled)) {}

TouchUiController::TouchUiScoperForTesting::~TouchUiScoperForTesting() {
  controller_->SetTouchUiState(old_state_);
}

void TouchUiController::TouchUiScoperForTesting::UpdateState(bool enabled) {
  controller_->SetTouchUiState(enabled ? TouchUiState::kEnabled
                                       : TouchUiState::kDisabled);
}

// static
TouchUiController* TouchUiController::Get() {
  static base::NoDestructor<TouchUiController> instance([] {
    const std::string switch_value =
        base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
            switches::kTopChromeTouchUi);
    if (switch_value == switches::kTopChromeTouchUiDisabled)
      return TouchUiState::kDisabled;
    const bool enabled = switch_value == switches::kTopChromeTouchUiEnabled;
    return enabled ? TouchUiState::kEnabled : TouchUiState::kAuto;
  }());
  return instance.get();
}

TouchUiController::TouchUiController(TouchUiState touch_ui_state)
    : touch_ui_state_(touch_ui_state) {
  if (base::CurrentUIThread::IsSet()) {
#if BUILDFLAG(USE_BLINK)
    // Pass the work to a separate task to avoid affecting browser startup time.
    base::SequencedTaskRunner::GetCurrentDefault()->PostTask(
        FROM_HERE,
        base::BindOnce(&TouchUiController::OnInitializePointerDevices,
                       weak_factory_.GetWeakPtr()));
#if BUILDFLAG(IS_WIN)
    // Register to listen for WM_POINTERDEVICECHANGE.
    base::win::RegisterPointerDeviceNotifications(
        gfx::SingletonHwnd::GetInstance()->hwnd(),
        /*notify_proximity_changes=*/false);
#endif  // BUILDFLAG(IS_WIN)
#endif  // BUILDFLAG(USE_BLINK)

#if BUILDFLAG(IS_WIN)
    singleton_hwnd_observer_ = std::make_unique<gfx::SingletonHwndObserver>(
        base::BindRepeating(&OnWndProc));
    base::win::IsDeviceInTabletMode(
        gfx::SingletonHwnd::GetInstance()->hwnd(),
        base::BindOnce(&TouchUiController::SetInitialTabletMode,
                       weak_factory_.GetWeakPtr()));
#endif  // BUILDFLAG(IS_WIN)
  }
#if !BUILDFLAG(IS_WIN)
  if (touch_ui())
    RecordEnteredTouchMode();
  else
    RecordEnteredNonTouchMode();
#endif  // !BUILDFLAG(IS_WIN)
}

TouchUiController::~TouchUiController() = default;

void TouchUiController::OnTabletModeToggled(bool enabled) {
  const bool was_touch_ui = touch_ui();
  tablet_mode_ = enabled;
  if (touch_ui() != was_touch_ui)
    TouchUiChanged();
}

#if BUILDFLAG(IS_WIN)
void TouchUiController::RefreshTabletMode() {
  base::win::IsDeviceInTabletMode(
      gfx::SingletonHwnd::GetInstance()->hwnd(),
      base::BindOnce(&TouchUiController::OnTabletModeToggled,
                     weak_factory_.GetWeakPtr()));
}

void TouchUiController::SetInitialTabletMode(bool enabled) {
  const bool was_touch_ui = touch_ui();
  tablet_mode_ = enabled;
  // Unconditionally record the histogram following discovery of the initial
  // mode.
  if (touch_ui()) {
    RecordEnteredTouchMode();
  } else {
    RecordEnteredNonTouchMode();
  }
  // Notify observers only if the mode has changed.
  if (touch_ui() != was_touch_ui) {
    TRACE_EVENT0("ui", "TouchUiController.NotifyListeners");
    callback_list_.Notify();
  }
}
#endif  // BUILDFLAG(IS_WIN)

base::CallbackListSubscription TouchUiController::RegisterCallback(
    const base::RepeatingClosure& closure) {
  return callback_list_.Add(closure);
}

TouchUiController::TouchUiState TouchUiController::SetTouchUiState(
    TouchUiState touch_ui_state) {
  const bool was_touch_ui = touch_ui();
  const TouchUiState old_state = std::exchange(touch_ui_state_, touch_ui_state);
  if (touch_ui() != was_touch_ui)
    TouchUiChanged();
  return old_state;
}

void TouchUiController::TouchUiChanged() {
  if (touch_ui())
    RecordEnteredTouchMode();
  else
    RecordEnteredNonTouchMode();

  TRACE_EVENT0("ui", "TouchUiController.NotifyListeners");
  callback_list_.Notify();
}

#if BUILDFLAG(USE_BLINK)
void TouchUiController::OnPointerDeviceConnected(PointerDevice::Key key) {
  if (const std::optional<PointerDevice> device = GetPointerDevice(key)) {
    RecordPointerDigitizerTypeOnConnected(*device);
    RecordPointerDigitizerTypeMaxTouchPoints(*device);
    last_known_pointer_devices_.emplace_back(*device);
  }
}

void TouchUiController::OnPointerDeviceDisconnected(PointerDevice::Key key) {
  // Iterative search should be fine because `last_known_pointer_devices_` is
  // expected to be a very small set.
  const auto iter = std::find(last_known_pointer_devices_.begin(),
                              last_known_pointer_devices_.end(), key);
  if (iter != last_known_pointer_devices_.end()) {
    RecordPointerDigitizerTypeOnDisconnected(*iter);
    last_known_pointer_devices_.erase(iter);
  }
}

int TouchUiController::MaxTouchPoints() const {
  return ::ui::MaxTouchPoints();
}

std::optional<PointerDevice> TouchUiController::GetPointerDevice(
    PointerDevice::Key key) const {
  return ::ui::GetPointerDevice(key);
}

std::vector<PointerDevice> TouchUiController::GetPointerDevices() const {
  return ::ui::GetPointerDevices();
}

const std::vector<PointerDevice>&
TouchUiController::GetLastKnownPointerDevicesForTesting() const {
  return last_known_pointer_devices_;
}

void TouchUiController::OnInitializePointerDevices() {
  last_known_pointer_devices_ = GetPointerDevices();
  for (const PointerDevice& device : last_known_pointer_devices_) {
    RecordPointerDigitizerTypeOnStartup(device);
    RecordPointerDigitizerTypeMaxTouchPoints(device);
  }
  RecordMaxTouchPointsSupportedBySystem(MaxTouchPoints());
}
#endif  // BUILDFLAG(USE_BLINK)

}  // namespace ui