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 348 349
|
// Copyright 2018 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/ash/arc/input_method_manager/input_connection_impl.h"
#include <tuple>
#include <utility>
#include "base/functional/bind.h"
#include "base/strings/utf_string_conversions.h"
#include "ui/base/ime/ash/ime_bridge.h"
#include "ui/base/ime/ash/ime_keymap.h"
#include "ui/events/base_event_utils.h"
#include "ui/events/keycodes/dom/dom_code.h"
#include "ui/events/keycodes/dom/keycode_converter.h"
#include "ui/events/keycodes/keyboard_code_conversion.h"
#include "ui/events/keycodes/keyboard_codes.h"
namespace arc {
namespace {
// Timeout threshold after the IME operation is sent to TextInputClient.
// If no text input state observer methods in below ArcProxyInputMethodObserver
// is called during this time period, the current text input state is sent to
// Android.
// TODO(yhanada): Implement a way to observe an IME operation completion and
// send the current text input state right after the IME operation completion.
constexpr base::TimeDelta kStateUpdateTimeout = base::Seconds(1);
// Characters which should be sent as a KeyEvent and attributes of generated
// KeyEvent.
constexpr std::tuple<char, ui::KeyboardCode, ui::DomCode, ui::DomKey>
kControlCharToKeyEvent[] = {
{'\n', ui::VKEY_RETURN, ui::DomCode::ENTER, ui::DomKey::ENTER}};
bool IsControlChar(const std::u16string& text) {
const std::string str = base::UTF16ToUTF8(text);
if (str.length() != 1)
return false;
for (const auto& t : kControlCharToKeyEvent) {
if (str[0] == std::get<0>(t))
return true;
}
return false;
}
ui::TextInputClient* GetTextInputClient() {
ash::IMEBridge* bridge = ash::IMEBridge::Get();
DCHECK(bridge);
ash::TextInputTarget* handler = bridge->GetInputContextHandler();
if (!handler)
return nullptr;
ui::TextInputClient* client = handler->GetInputMethod()->GetTextInputClient();
DCHECK(client);
return client;
}
ui::KeyEvent CreateKeyEvent(ui::EventType type,
ui::KeyboardCode key_code,
ui::DomCode code,
ui::DomKey key) {
return ui::KeyEvent(type, key_code, code, ui::EF_NONE, key,
ui::EventTimeForNow());
}
} // namespace
InputConnectionImpl::InputConnectionImpl(
ash::input_method::InputMethodEngine* ime_engine,
ArcInputMethodManagerBridge* imm_bridge,
int input_context_id)
: ime_engine_(ime_engine),
imm_bridge_(imm_bridge),
input_context_id_(input_context_id),
state_update_timer_() {}
InputConnectionImpl::~InputConnectionImpl() = default;
void InputConnectionImpl::Bind(
mojo::PendingRemote<mojom::InputConnection>* remote) {
receiver_.Bind(remote->InitWithNewPipeAndPassReceiver());
}
void InputConnectionImpl::UpdateTextInputState(
bool is_input_state_update_requested) {
if (state_update_timer_.IsRunning()) {
// There is a pending request.
is_input_state_update_requested = true;
}
state_update_timer_.Stop();
imm_bridge_->SendUpdateTextInputState(
GetTextInputState(is_input_state_update_requested));
}
mojom::TextInputStatePtr InputConnectionImpl::GetTextInputState(
bool is_input_state_update_requested) const {
ui::TextInputClient* client = GetTextInputClient();
gfx::Range text_range = gfx::Range();
gfx::Range selection_range = gfx::Range();
std::optional<gfx::Range> composition_text_range = gfx::Range();
std::u16string text;
if (!client) {
return mojom::TextInputStatePtr(std::in_place, 0, text, text_range,
selection_range, ui::TEXT_INPUT_TYPE_NONE,
false, 0, is_input_state_update_requested,
composition_text_range);
}
client->GetTextRange(&text_range);
client->GetEditableSelectionRange(&selection_range);
if (!client->GetCompositionTextRange(&composition_text_range.value()))
composition_text_range.reset();
client->GetTextFromRange(text_range, &text);
return mojom::TextInputStatePtr(
std::in_place, selection_range.start(), text, text_range, selection_range,
client->GetTextInputType(), client->ShouldDoLearning(),
client->GetTextInputFlags(), is_input_state_update_requested,
composition_text_range);
}
void InputConnectionImpl::CommitText(const std::u16string& text,
int new_cursor_pos) {
StartStateUpdateTimer();
std::string error;
// Clear the current composing text at first.
if (!ime_engine_->ClearComposition(input_context_id_, &error))
LOG(ERROR) << "ClearComposition failed: error=\"" << error << "\"";
if (IsControlChar(text)) {
SendControlKeyEvent(text);
return;
}
if (!ime_engine_->CommitText(input_context_id_, text, &error))
LOG(ERROR) << "CommitText failed: error=\"" << error << "\"";
}
void InputConnectionImpl::DeleteSurroundingText(int before, int after) {
StartStateUpdateTimer();
if (before == 0 && after == 0) {
// This should be no-op.
// Return the current state immediately.
UpdateTextInputState(true);
return;
}
std::string error;
// DeleteSurroundingText takes a start position relative to the current cursor
// position and a length of the text is going to be deleted.
// |before| is a number of characters is going to be deleted before the cursor
// and |after| is a number of characters is going to be deleted after the
// cursor.
if (!ime_engine_->DeleteSurroundingText(input_context_id_, -before,
before + after, &error)) {
LOG(ERROR) << "DeleteSurroundingText failed: before = " << before
<< ", after = " << after << ", error = \"" << error << "\"";
}
}
void InputConnectionImpl::FinishComposingText() {
StartStateUpdateTimer();
ui::TextInputClient* client = GetTextInputClient();
if (!client)
return;
gfx::Range selection_range, composition_range;
client->GetEditableSelectionRange(&selection_range);
client->GetCompositionTextRange(&composition_range);
if (composition_range.is_empty()) {
// There is no ongoing composing. Do nothing.
UpdateTextInputState(true);
return;
}
std::u16string composing_text;
client->GetTextFromRange(composition_range, &composing_text);
std::string error;
if (!ime_engine_->CommitText(input_context_id_, composing_text, &error)) {
LOG(ERROR) << "FinishComposingText: CommitText() failed, error=\"" << error
<< "\"";
}
if (selection_range.start() == selection_range.end() &&
selection_range.start() == composition_range.end()) {
// The call of CommitText won't update the state.
// Return the current state immediately.
UpdateTextInputState(true);
}
}
void InputConnectionImpl::SetComposingText(
const std::u16string& text,
int new_cursor_pos,
const std::optional<gfx::Range>& new_selection_range) {
// It's relative to the last character of the composing text,
// so 0 means the cursor should be just before the last character of the text.
new_cursor_pos += text.length() - 1;
StartStateUpdateTimer();
ui::TextInputClient* client = GetTextInputClient();
if (!client)
return;
// Calculate the position of composition insertion point
gfx::Range selection_range, composition_range;
client->GetEditableSelectionRange(&selection_range);
client->GetCompositionTextRange(&composition_range);
const int insertion_point = composition_range.is_empty()
? selection_range.start()
: composition_range.start();
const int selection_start =
new_selection_range
? new_selection_range.value().start() - insertion_point
: new_cursor_pos;
const int selection_end =
new_selection_range ? new_selection_range.value().end() - insertion_point
: new_cursor_pos;
if (text.empty() &&
selection_range.start() == static_cast<uint32_t>(selection_start) &&
selection_range.end() == static_cast<uint32_t>(selection_end)) {
// This SetComposingText call is no-op.
// Return the current state immediately.
UpdateTextInputState(true);
}
std::string error;
if (!ime_engine_->SetComposition(
input_context_id_, base::UTF16ToUTF8(text).c_str(), selection_start,
selection_end, new_cursor_pos,
std::vector<ash::input_method::InputMethodEngine::SegmentInfo>(),
&error)) {
LOG(ERROR) << "SetComposition failed: pos=" << new_cursor_pos
<< ", error=\"" << error << "\"";
return;
}
}
void InputConnectionImpl::RequestTextInputState(
mojom::InputConnection::RequestTextInputStateCallback callback) {
std::move(callback).Run(GetTextInputState(false));
}
void InputConnectionImpl::SetSelection(const gfx::Range& new_selection_range) {
ui::TextInputClient* client = GetTextInputClient();
if (!client)
return;
gfx::Range selection_range;
client->GetEditableSelectionRange(&selection_range);
if (new_selection_range == selection_range) {
// This SetSelection call is no-op.
// Return the current state immediately.
UpdateTextInputState(true);
}
StartStateUpdateTimer();
client->SetEditableSelectionRange(new_selection_range);
}
void InputConnectionImpl::SendKeyEvent(
std::unique_ptr<ui::KeyEvent> key_event) {
DCHECK(key_event);
std::string error;
if (!ime_engine_->SendKeyEvents(input_context_id_, {*key_event}, &error)) {
LOG(ERROR) << error;
}
}
void InputConnectionImpl::SetCompositionRange(
const gfx::Range& new_composition_range) {
ui::TextInputClient* client = GetTextInputClient();
if (!client)
return;
gfx::Range selection_range = gfx::Range();
if (!client->GetEditableSelectionRange(&selection_range)) {
LOG(ERROR)
<< "SetCompositionRange failed: Editable text field is not focused";
return;
}
StartStateUpdateTimer();
ash::input_method::InputMethodEngine::SegmentInfo segment_info;
segment_info.start = 0;
segment_info.end = new_composition_range.length();
segment_info.style =
ash::input_method::InputMethodEngine::SEGMENT_STYLE_UNDERLINE;
std::string error;
if (!ime_engine_->ash::input_method::InputMethodEngine::SetComposingRange(
input_context_id_, new_composition_range.start(),
new_composition_range.end(), {segment_info}, &error)) {
LOG(ERROR) << "SetComposingRange failed: range="
<< new_composition_range.ToString() << ", error=\"" << error
<< "\"";
}
}
void InputConnectionImpl::StartStateUpdateTimer() {
// It's safe to use Unretained() here because the timer is automatically
// canceled when it go out of scope.
state_update_timer_.Start(
FROM_HERE, kStateUpdateTimeout,
base::BindOnce(&InputConnectionImpl::UpdateTextInputState,
base::Unretained(this),
true /* is_input_state_update_requested */));
}
void InputConnectionImpl::SendControlKeyEvent(const std::u16string& text) {
DCHECK(IsControlChar(text));
const std::string str = base::UTF16ToUTF8(text);
DCHECK_EQ(1u, str.length());
for (const auto& t : kControlCharToKeyEvent) {
if (std::get<0>(t) == str[0]) {
ui::KeyEvent press =
CreateKeyEvent(ui::EventType::kKeyPressed, std::get<1>(t),
std::get<2>(t), std::get<3>(t));
ui::KeyEvent release =
CreateKeyEvent(ui::EventType::kKeyReleased, std::get<1>(t),
std::get<2>(t), std::get<3>(t));
std::string error;
if (!ime_engine_->SendKeyEvents(input_context_id_, {press, release},
&error)) {
LOG(ERROR) << error;
}
break;
}
}
return;
}
} // namespace arc
|