File: stylus_handwriting_controller_win.cc

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; 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,811; 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 (159 lines) | stat: -rw-r--r-- 5,550 bytes parent folder | download | duplicates (6)
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
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "content/browser/renderer_host/input/stylus_handwriting_controller_win.h"

#include "base/functional/callback_helpers.h"
#include "base/memory/ptr_util.h"
#include "components/stylus_handwriting/win/features.h"
#include "content/browser/renderer_host/input/stylus_handwriting_callback_sink_win.h"
#include "content/public/browser/browser_thread.h"
#include "ui/base/ime/text_input_client.h"
#include "ui/base/ime/win/tsf_bridge.h"
#include "ui/events/win/stylus_handwriting_properties_win.h"

namespace content {

namespace {

StylusHandwritingControllerWin* g_instance = nullptr;
ITfThreadMgr* g_thread_manager_instance_for_testing = nullptr;

}  // namespace

// static
base::ScopedClosureRunner StylusHandwritingControllerWin::InitializeForTesting(
    ITfThreadMgr* thread_manager) {
  CHECK_CURRENTLY_ON(content::BrowserThread::UI);
  // A RAII class that automatically disposes of test-only global state, to
  // hide all implementation details from the caller.
  class StateForTesting {
   public:
    explicit StateForTesting(ITfThreadMgr* thread_manager) {
      CHECK(!g_thread_manager_instance_for_testing);
      CHECK(!g_instance);
      g_thread_manager_instance_for_testing = thread_manager;
      controller_ = base::WrapUnique(new StylusHandwritingControllerWin());
    }
    ~StateForTesting() { g_thread_manager_instance_for_testing = nullptr; }

   private:
    std::unique_ptr<StylusHandwritingControllerWin> controller_;
  };

  // The scoped closure runner guarantees that the closure will be executed and
  // the state, saved as a bound parameter, will be disposed of by unique_ptr.
  return base::ScopedClosureRunner(
      base::BindOnce([](std::unique_ptr<StateForTesting> state) {},
                     std::make_unique<StateForTesting>(thread_manager)));
}

// static
bool StylusHandwritingControllerWin::IsHandwritingAPIAvailable() {
  CHECK_CURRENTLY_ON(content::BrowserThread::UI);
  if (!stylus_handwriting::win::IsStylusHandwritingWinEnabled()) {
    return false;
  }

  return GetInstance();
}

// static
void StylusHandwritingControllerWin::Initialize() {
  CHECK_CURRENTLY_ON(content::BrowserThread::UI);
  // We don't want to create a static instance with no destructor here because
  // the state can leak across test runs. In product builds, the controller is
  // not expected to leave until process shutdown.
  if (!stylus_handwriting::win::IsStylusHandwritingWinEnabled() ||
      g_thread_manager_instance_for_testing) {
    return;
  }

  // See the constructor and BindInterfaces() for more initialization details
  // where g_instance is set.
  static base::NoDestructor<StylusHandwritingControllerWin> instance;
}

// static
StylusHandwritingControllerWin* StylusHandwritingControllerWin::GetInstance() {
  CHECK_CURRENTLY_ON(content::BrowserThread::UI);
  CHECK(stylus_handwriting::win::IsStylusHandwritingWinEnabled());
  return g_instance;
}

// static
Microsoft::WRL::ComPtr<ITfThreadMgr>
StylusHandwritingControllerWin::GetThreadManager() {
  if (g_thread_manager_instance_for_testing) {
    return g_thread_manager_instance_for_testing;
  }

  return ui::TSFBridge::GetInstance()
             ? ui::TSFBridge::GetInstance()->GetThreadManager()
             : nullptr;
}

StylusHandwritingControllerWin::StylusHandwritingControllerWin() {
  BindInterfaces();
}

StylusHandwritingControllerWin::~StylusHandwritingControllerWin() {
  g_instance = nullptr;
}

Microsoft::WRL::ComPtr<StylusHandwritingCallbackSinkWin>
StylusHandwritingControllerWin::GetCallbackSinkForTesting() const {
  return handwriting_callback_sink_;
}

void StylusHandwritingControllerWin::OnStartStylusWriting(
    OnFocusHandwritingTargetCallback callback,
    const ui::StylusHandwritingPropertiesWin& properties,
    ui::TextInputClient& text_input_client) {
  BOOL accepted;
  Microsoft::WRL::ComPtr<ITfHandwritingRequest> handwriting_request = nullptr;
  HRESULT hr = handwriting_->RequestHandwritingForPointer(
      properties.handwriting_pointer_id, properties.handwriting_stroke_id,
      &accepted, &handwriting_request);

  if (SUCCEEDED(hr) && accepted) {
    handwriting_callback_sink_->SetCallback(std::move(callback));
    handwriting_request->SetInputEvaluation(
        ::TfInputEvaluation::TF_IE_HANDWRITING);
  }

  // TODO(crbug.com/355578906): Record instances when
  // RequestHandwritingForPointer() failed.
}

void StylusHandwritingControllerWin::OnFocusHandled(
    ui::TextInputClient& text_input_client) {
  CHECK(handwriting_callback_sink_);
  handwriting_callback_sink_->OnFocusHandled();
}

void StylusHandwritingControllerWin::OnFocusFailed(
    ui::TextInputClient& text_input_client) {
  CHECK(handwriting_callback_sink_);
  handwriting_callback_sink_->OnFocusFailed();
}

void StylusHandwritingControllerWin::BindInterfaces() {
  // There can only be one StylusHandwritingControllerWin at any given time.
  CHECK(!g_instance);
  if (const auto thread_manager = GetThreadManager()) {
    const bool initialized_successfully =
        SUCCEEDED(thread_manager.As(&handwriting_)) &&
        SUCCEEDED(handwriting_->SetHandwritingState(
            ::TF_HANDWRITING_POINTERDELIVERY)) &&
        SUCCEEDED(
            Microsoft::WRL::Details::MakeAndInitialize<
                StylusHandwritingCallbackSinkWin>(&handwriting_callback_sink_));
    if (initialized_successfully) {
      g_instance = this;
    }
  }
}

}  // namespace content