Commit 5a1f02de authored by Darren Shen's avatar Darren Shen Committed by Commit Bot

ime: Send text input field information to IME service.

Add Mojo and protobuf messages for Chrome to send text input field info
to the IME service, including the type of the input field, whether
autocorrect is enabled and whether the input field is privacy-sensitive.

Bug: 1019541
Change-Id: I06858902697aba891109cdd7934b15862ca0add1
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2483729
Commit-Queue: Darren Shen <shend@chromium.org>
Reviewed-by: default avatarKeith Lee <keithlee@chromium.org>
Reviewed-by: default avatarSam McNally <sammc@chromium.org>
Cr-Commit-Position: refs/heads/master@{#820649}
parent 1849eddb
......@@ -65,6 +65,37 @@ ime::mojom::ModifierStatePtr ModifierStateFromEvent(
return modifier_state;
}
ime::mojom::InputFieldType TextInputTypeToMojoType(ui::TextInputType type) {
using ime::mojom::InputFieldType;
switch (type) {
case ui::TEXT_INPUT_TYPE_PASSWORD:
return InputFieldType::kPassword;
case ui::TEXT_INPUT_TYPE_SEARCH:
return InputFieldType::kSearch;
case ui::TEXT_INPUT_TYPE_EMAIL:
return InputFieldType::kEmail;
case ui::TEXT_INPUT_TYPE_TELEPHONE:
return InputFieldType::kTelephone;
case ui::TEXT_INPUT_TYPE_URL:
return InputFieldType::kURL;
case ui::TEXT_INPUT_TYPE_NUMBER:
return InputFieldType::kNumber;
case ui::TEXT_INPUT_TYPE_NULL:
return InputFieldType::kNoIME;
case ui::TEXT_INPUT_TYPE_TEXT:
return InputFieldType::kText;
default:
return InputFieldType::kText;
}
}
ime::mojom::AutocorrectMode AutocorrectFlagsToMojoType(int flags) {
if (flags & ui::TEXT_INPUT_FLAG_AUTOCORRECT_ON) {
return ime::mojom::AutocorrectMode::kEnabled;
}
return ime::mojom::AutocorrectMode::kDisabled;
}
enum class ImeServiceEvent {
kUnknown = 0,
kInitSuccess = 1,
......@@ -177,7 +208,12 @@ void NativeInputMethodEngine::ImeObserver::OnFocus(
if (active_engine_id_ && ShouldUseFstMojoEngine(*active_engine_id_) &&
remote_to_engine_.is_bound()) {
remote_to_engine_->OnFocus();
remote_to_engine_->OnFocus(ime::mojom::InputFieldInfo::New(
TextInputTypeToMojoType(context.type),
AutocorrectFlagsToMojoType(context.flags),
context.should_do_learning
? ime::mojom::PersonalizationMode::kEnabled
: ime::mojom::PersonalizationMode::kDisabled));
}
base_observer_->OnFocus(context);
......
......@@ -92,7 +92,7 @@ class NativeInputMethodEngine : public InputMethodEngine {
// mojom::InputChannel:
void ProcessMessage(const std::vector<uint8_t>& message,
ProcessMessageCallback callback) override;
void OnFocus() override {}
void OnFocus(ime::mojom::InputFieldInfoPtr input_field_info) override {}
void OnBlur() override {}
void OnSurroundingTextChanged(
const std::string& text,
......
......@@ -138,11 +138,12 @@ bool DecoderEngine::IsImeSupportedByDecoder(const std::string& ime_spec) {
engine_main_entry_->IsImeSupported(ime_spec.c_str());
}
void DecoderEngine::OnFocus() {
void DecoderEngine::OnFocus(mojom::InputFieldInfoPtr input_field_info) {
const uint64_t seq_id = current_seq_id_;
++current_seq_id_;
ProcessMessage(WrapAndSerializeMessage(OnFocusToProto(seq_id)),
ProcessMessage(WrapAndSerializeMessage(
OnFocusToProto(seq_id, std::move(input_field_info))),
base::DoNothing());
}
......
......@@ -32,7 +32,7 @@ class DecoderEngine : public InputEngine {
void ProcessMessage(const std::vector<uint8_t>& message,
ProcessMessageCallback callback) override;
void OnFocus() override;
void OnFocus(mojom::InputFieldInfoPtr input_field_info) override;
void OnBlur() override;
void OnKeyEvent(mojom::PhysicalKeyEventPtr event,
OnKeyEventCallback callback) override;
......
......@@ -53,7 +53,7 @@ class StubInputChannel : public mojom::InputChannel {
ProcessMessageCallback callback) final {
std::move(callback).Run({});
}
void OnFocus() final {}
void OnFocus(mojom::InputFieldInfoPtr input_field_info) final {}
void OnBlur() final {}
void ProcessKeypressForRulebased(
ime::mojom::PhysicalKeyEventPtr event,
......@@ -110,12 +110,18 @@ TEST_F(DecoderEngineTest, OnFocusSendsMessageToSharedLib) {
mojo::Remote<mojom::InputChannel> client;
ASSERT_TRUE(engine.BindRequest(kImeSpec, client.BindNewPipeAndPassReceiver(),
receiver.BindNewPipeAndPassRemote(), {}));
auto info = mojom::InputFieldInfo::New(mojom::InputFieldType::kNumber,
mojom::AutocorrectMode::kEnabled,
mojom::PersonalizationMode::kEnabled);
ime::Wrapper expected_proto;
*expected_proto.mutable_public_message() = OnFocusToProto(/*seq_id=*/0);
*expected_proto.mutable_public_message() =
OnFocusToProto(/*seq_id=*/0, info.Clone());
EXPECT_CALL(mock_main_entry_, Process).With(EqualsProto(expected_proto));
client->OnFocus();
client->OnFocus(info.Clone());
client.FlushForTesting();
}
......
......@@ -19,13 +19,61 @@ ModifierState ModifierStateToProto(mojom::ModifierStatePtr modifier_state) {
return result;
}
InputFieldInfo::InputFieldType InputFieldTypeToProto(
mojom::InputFieldType input_field_type) {
switch (input_field_type) {
case mojom::InputFieldType::kNoIME:
return InputFieldInfo::INPUT_FIELD_TYPE_NO_IME;
case mojom::InputFieldType::kText:
return InputFieldInfo::INPUT_FIELD_TYPE_TEXT;
case mojom::InputFieldType::kSearch:
return InputFieldInfo::INPUT_FIELD_TYPE_SEARCH;
case mojom::InputFieldType::kTelephone:
return InputFieldInfo::INPUT_FIELD_TYPE_TELEPHONE;
case mojom::InputFieldType::kURL:
return InputFieldInfo::INPUT_FIELD_TYPE_URL;
case mojom::InputFieldType::kEmail:
return InputFieldInfo::INPUT_FIELD_TYPE_EMAIL;
case mojom::InputFieldType::kNumber:
return InputFieldInfo::INPUT_FIELD_TYPE_NUMBER;
case mojom::InputFieldType::kPassword:
return InputFieldInfo::INPUT_FIELD_TYPE_PASSWORD;
}
}
InputFieldInfo::AutocorrectMode AutocorrectModeToProto(
mojom::AutocorrectMode autocorrect_mode) {
switch (autocorrect_mode) {
case mojom::AutocorrectMode::kDisabled:
return InputFieldInfo::AUTOCORRECT_MODE_DISABLED;
case mojom::AutocorrectMode::kEnabled:
return InputFieldInfo::AUTOCORRECT_MODE_ENABLED;
}
}
InputFieldInfo::PersonalizationMode PersonalizationModeToProto(
mojom::PersonalizationMode personalization_mode) {
switch (personalization_mode) {
case mojom::PersonalizationMode::kDisabled:
return InputFieldInfo::PERSONALIZATION_MODE_DISABLED;
case mojom::PersonalizationMode::kEnabled:
return InputFieldInfo::PERSONALIZATION_MODE_ENABLED;
}
}
} // namespace
ime::PublicMessage OnFocusToProto(uint64_t seq_id) {
ime::PublicMessage OnFocusToProto(uint64_t seq_id,
mojom::InputFieldInfoPtr input_field_info) {
ime::PublicMessage message;
message.set_seq_id(seq_id);
*message.mutable_on_focus() = ime::OnFocus();
ime::InputFieldInfo& proto_info = *message.mutable_on_focus()->mutable_info();
proto_info.set_type(InputFieldTypeToProto(input_field_info->type));
proto_info.set_autocorrect(
AutocorrectModeToProto(input_field_info->autocorrect));
proto_info.set_personalization(
PersonalizationModeToProto(input_field_info->personalization));
return message;
}
......
......@@ -14,7 +14,8 @@ namespace chromeos {
namespace ime {
// Converts arguments of a Mojo call to InputChannel::OnFocus into a proto.
ime::PublicMessage OnFocusToProto(uint64_t seq_id);
ime::PublicMessage OnFocusToProto(uint64_t seq_id,
mojom::InputFieldInfoPtr input_field_info);
// Converts arguments of a Mojo call to InputChannel::OnBlur into a proto.
ime::PublicMessage OnBlurToProto(uint64_t seq_id);
......
......@@ -11,11 +11,21 @@ namespace chromeos {
namespace ime {
TEST(ProtoConversionTest, OnFocusToProto) {
auto info = mojom::InputFieldInfo::New(mojom::InputFieldType::kNumber,
mojom::AutocorrectMode::kEnabled,
mojom::PersonalizationMode::kEnabled);
ime::PublicMessage expected_message;
expected_message.set_seq_id(42);
*expected_message.mutable_on_focus() = ime::OnFocus();
ime::OnFocus& args = *expected_message.mutable_on_focus();
args.mutable_info()->set_type(ime::InputFieldInfo::INPUT_FIELD_TYPE_NUMBER);
args.mutable_info()->set_autocorrect(
ime::InputFieldInfo::AUTOCORRECT_MODE_ENABLED);
args.mutable_info()->set_personalization(
ime::InputFieldInfo::PERSONALIZATION_MODE_ENABLED);
ime::PublicMessage actual_message = OnFocusToProto(/*seq_id=*/42);
ime::PublicMessage actual_message =
OnFocusToProto(/*seq_id=*/42, info.Clone());
EXPECT_EQ(actual_message.SerializeAsString(),
expected_message.SerializeAsString());
......
......@@ -55,7 +55,7 @@ class TestClientChannel : mojom::InputChannel {
MOCK_METHOD2(ProcessMessage,
void(const std::vector<uint8_t>& message,
ProcessMessageCallback));
MOCK_METHOD0(OnFocus, void());
MOCK_METHOD1(OnFocus, void(mojom::InputFieldInfoPtr input_field_info));
MOCK_METHOD0(OnBlur, void());
MOCK_METHOD2(ProcessKeypressForRulebased,
void(const mojom::PhysicalKeyEventPtr event,
......
......@@ -107,7 +107,7 @@ void InputEngine::ProcessMessage(const std::vector<uint8_t>& message,
NOTIMPLEMENTED(); // Protobuf message is not used in the rulebased engine.
}
void InputEngine::OnFocus() {
void InputEngine::OnFocus(mojom::InputFieldInfoPtr input_field_info) {
NOTIMPLEMENTED(); // Not used in the rulebased engine.
}
......
......@@ -46,7 +46,7 @@ class InputEngine : public mojom::InputChannel {
// mojom::InputChannel overrides:
void ProcessMessage(const std::vector<uint8_t>& message,
ProcessMessageCallback callback) override;
void OnFocus() override;
void OnFocus(mojom::InputFieldInfoPtr input_field_info) override;
void OnBlur() override;
void OnSurroundingTextChanged(
const std::string& text,
......
......@@ -92,6 +92,48 @@ struct SelectionRange {
uint32 focus;
};
// Type of input field.
// Based off ui::TextInputType in ui/base/ime/text_input_type.h.
enum InputFieldType {
// Does not support IME editing. IMEs should only send physical key events,
// and not perform complex operations such as composition.
kNoIME = 0,
// Supports general text input.
kText = 1,
kSearch = 2,
kTelephone = 3,
kURL = 4,
kEmail = 5,
kNumber = 6,
// Input represents a password. IMEs should ensure the password is not stored
// without the user's permission.
kPassword = 7,
};
enum AutocorrectMode {
// The IME must not perform autocorrect.
kDisabled = 0,
// The IME may perform autocorrect as the user is typing.
kEnabled = 1,
};
enum PersonalizationMode {
// The IME should not use anything from the input field to update any
// personalized data (e.g. to improve suggestions quality). Personalization
// may be disabled if the content is privacy-sensitive (e.g. incognito mode in
// Chrome browser), or if using personalization does not make sense (e.g.
// playing a typing game may pollute the IME dictionary with uncommon words).
kDisabled = 0,
// The IME may use the input field contents for personalization.
kEnabled = 1,
};
struct InputFieldInfo {
InputFieldType type;
AutocorrectMode autocorrect;
PersonalizationMode personalization;
};
// Manages access to a set of IME engines, implemented by the IME service
// itself. The IME framework in the browser process is responsible for brokering
// the connection between the IME service and the IME extension, but does not
......@@ -123,7 +165,7 @@ interface InputChannel {
ProcessMessage(array<uint8> message) => (array<uint8> result);
// Called when there's a new focused input field.
OnFocus();
OnFocus(InputFieldInfo input_field_info);
// Called when the input field loses focus.
OnBlur();
......
......@@ -38,10 +38,42 @@ message PublicMessage {
}
}
// Protobuf version of the reply from InputEngine::InputFieldInfo in
// chromeos/services/ime/public/mojom/input_engine.mojom
message InputFieldInfo {
enum InputFieldType {
INPUT_FIELD_TYPE_UNSPECIFIED = 0; // Reserved
INPUT_FIELD_TYPE_NO_IME = 1;
INPUT_FIELD_TYPE_TEXT = 2;
INPUT_FIELD_TYPE_SEARCH = 3;
INPUT_FIELD_TYPE_TELEPHONE = 4;
INPUT_FIELD_TYPE_URL = 5;
INPUT_FIELD_TYPE_EMAIL = 6;
INPUT_FIELD_TYPE_NUMBER = 7;
INPUT_FIELD_TYPE_PASSWORD = 8;
}
enum AutocorrectMode {
AUTOCORRECT_MODE_UNSPECIFIED = 0; // Reserved
AUTOCORRECT_MODE_DISABLED = 1;
AUTOCORRECT_MODE_ENABLED = 2;
}
enum PersonalizationMode {
PERSONALIZATION_MODE_UNSPECIFIED = 0; // Reserved
PERSONALIZATION_MODE_DISABLED = 1;
PERSONALIZATION_MODE_ENABLED = 2;
}
optional InputFieldType type = 1;
optional AutocorrectMode autocorrect = 2;
optional PersonalizationMode personalization = 3;
}
// Protobuf version of InputEngine::OnFocus in
// chromeos/services/ime/public/mojom/input_engine.mojom
message OnFocus {
// TODO(crbug/1019541): Add information about the input field.
optional InputFieldInfo info = 1;
}
// Protobuf version of InputEngine::OnBlur in
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment