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
|
// 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 "base/win/windows_version.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 {
inline constexpr uint32_t kHandwritingSupportMinBuild = 26100;
inline constexpr uint32_t kHandwritingSupportMinPatch = 3624;
StylusHandwritingControllerWin* g_instance = nullptr;
ITfThreadMgr* g_thread_manager_instance_for_testing = nullptr;
bool g_bind_interfaces_called_for_testing = false;
} // 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
bool StylusHandwritingControllerWin::StylusHandwritingSupportedOnBuild() {
const uint32_t build =
base::win::OSInfo::GetInstance()->version_number().build;
const uint32_t patch =
base::win::OSInfo::GetInstance()->version_number().patch;
return (build == kHandwritingSupportMinBuild &&
patch >= kHandwritingSupportMinPatch) ||
(build > kHandwritingSupportMinBuild);
}
// static
void StylusHandwritingControllerWin::Initialize() {
CHECK_CURRENTLY_ON(content::BrowserThread::UI);
// TODO(arakeri): This is being done as a workaround for an OS bug (see
// crbug.com/372506009). The Windows team has fixed it but hasn't fully
// backported it to older builds where the issue still exists. This guard will
// be removed once the backporting is complete.
static bool handwriting_supported_on_winbuild =
StylusHandwritingSupportedOnBuild();
// 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() ||
!handwriting_supported_on_winbuild ||
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();
}
// static
bool StylusHandwritingControllerWin::BindInterfacesCalledForTesting() {
return g_bind_interfaces_called_for_testing;
}
void StylusHandwritingControllerWin::BindInterfaces() {
// There can only be one StylusHandwritingControllerWin at any given time.
CHECK(!g_instance);
g_bind_interfaces_called_for_testing = true;
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
|