[email protected] | f751653a9 | 2014-02-18 16:32:55 | [diff] [blame] | 1 | // Use of this source code is governed by a BSD-style license that can be |
| 2 | // found in the LICENSE file. |
| 3 | |
Scott Violet | fdda96d | 2018-07-27 20:17:23 | [diff] [blame] | 4 | #include "content/shell/test_runner/text_input_controller.h" |
[email protected] | f751653a9 | 2014-02-18 16:32:55 | [diff] [blame] | 5 | |
avi | 5dd91f8 | 2015-12-25 22:30:46 | [diff] [blame] | 6 | #include "base/macros.h" |
danakj | 27e4ebe | 2020-04-15 18:48:27 | [diff] [blame] | 7 | #include "content/shell/renderer/web_test/blink_test_runner.h" |
Scott Violet | fdda96d | 2018-07-27 20:17:23 | [diff] [blame] | 8 | #include "content/shell/test_runner/web_view_test_proxy.h" |
[email protected] | f751653a9 | 2014-02-18 16:32:55 | [diff] [blame] | 9 | #include "gin/arguments.h" |
| 10 | #include "gin/handle.h" |
| 11 | #include "gin/object_template_builder.h" |
| 12 | #include "gin/wrappable.h" |
Dave Tapuska | 129cef8 | 2019-12-19 16:36:48 | [diff] [blame] | 13 | #include "third_party/blink/public/common/input/web_keyboard_event.h" |
Blink Reformat | a30d423 | 2018-04-07 15:31:06 | [diff] [blame] | 14 | #include "third_party/blink/public/platform/web_coalesced_input_event.h" |
| 15 | #include "third_party/blink/public/platform/web_input_event_result.h" |
Blink Reformat | a30d423 | 2018-04-07 15:31:06 | [diff] [blame] | 16 | #include "third_party/blink/public/web/blink.h" |
| 17 | #include "third_party/blink/public/web/web_frame_widget.h" |
| 18 | #include "third_party/blink/public/web/web_ime_text_span.h" |
| 19 | #include "third_party/blink/public/web/web_input_method_controller.h" |
| 20 | #include "third_party/blink/public/web/web_local_frame.h" |
| 21 | #include "third_party/blink/public/web/web_range.h" |
| 22 | #include "third_party/blink/public/web/web_view.h" |
changwan | 9bdc18f0 | 2015-11-20 02:43:52 | [diff] [blame] | 23 | #include "third_party/skia/include/core/SkColor.h" |
dtapuska | 899ac22 | 2017-01-03 18:09:16 | [diff] [blame] | 24 | #include "ui/events/base_event_utils.h" |
[email protected] | f751653a9 | 2014-02-18 16:32:55 | [diff] [blame] | 25 | #include "v8/include/v8.h" |
| 26 | |
danakj | 741848a | 2020-04-07 22:48:06 | [diff] [blame] | 27 | namespace content { |
[email protected] | f751653a9 | 2014-02-18 16:32:55 | [diff] [blame] | 28 | |
| 29 | class TextInputControllerBindings |
| 30 | : public gin::Wrappable<TextInputControllerBindings> { |
| 31 | public: |
| 32 | static gin::WrapperInfo kWrapperInfo; |
| 33 | |
| 34 | static void Install(base::WeakPtr<TextInputController> controller, |
lukasza | 8b6d5f3 | 2016-04-22 16:56:31 | [diff] [blame] | 35 | blink::WebLocalFrame* frame); |
[email protected] | f751653a9 | 2014-02-18 16:32:55 | [diff] [blame] | 36 | |
| 37 | private: |
| 38 | explicit TextInputControllerBindings( |
| 39 | base::WeakPtr<TextInputController> controller); |
dcheng | e933b3e | 2014-10-21 11:44:09 | [diff] [blame] | 40 | ~TextInputControllerBindings() override; |
[email protected] | f751653a9 | 2014-02-18 16:32:55 | [diff] [blame] | 41 | |
| 42 | // gin::Wrappable: |
dcheng | e933b3e | 2014-10-21 11:44:09 | [diff] [blame] | 43 | gin::ObjectTemplateBuilder GetObjectTemplateBuilder( |
anand.ratn | 449f39a4 | 2014-10-06 13:45:57 | [diff] [blame] | 44 | v8::Isolate* isolate) override; |
[email protected] | f751653a9 | 2014-02-18 16:32:55 | [diff] [blame] | 45 | |
| 46 | void InsertText(const std::string& text); |
| 47 | void UnmarkText(); |
Ryan Landay | 7b5dbd5 | 2018-01-11 19:05:59 | [diff] [blame] | 48 | void UnmarkAndUnselectText(); |
[email protected] | f751653a9 | 2014-02-18 16:32:55 | [diff] [blame] | 49 | void DoCommand(const std::string& text); |
Ryan Landay | 9c680942 | 2018-01-17 07:04:15 | [diff] [blame] | 50 | void ExtendSelectionAndDelete(int before, int after); |
Ryan Landay | 6d163ae | 2018-01-17 06:58:06 | [diff] [blame] | 51 | void DeleteSurroundingText(int before, int after); |
[email protected] | f751653a9 | 2014-02-18 16:32:55 | [diff] [blame] | 52 | void SetMarkedText(const std::string& text, int start, int length); |
Ryan Landay | d203467 | 2018-01-12 22:22:32 | [diff] [blame] | 53 | void SetMarkedTextFromExistingText(int start, int length); |
[email protected] | f751653a9 | 2014-02-18 16:32:55 | [diff] [blame] | 54 | bool HasMarkedText(); |
| 55 | std::vector<int> MarkedRange(); |
| 56 | std::vector<int> SelectedRange(); |
| 57 | std::vector<int> FirstRectForCharacterRange(unsigned location, |
| 58 | unsigned length); |
| 59 | void SetComposition(const std::string& text); |
ekaramad | 2daaf67 | 2016-11-10 20:29:01 | [diff] [blame] | 60 | void ForceTextInputStateUpdate(); |
[email protected] | f751653a9 | 2014-02-18 16:32:55 | [diff] [blame] | 61 | |
| 62 | base::WeakPtr<TextInputController> controller_; |
| 63 | |
| 64 | DISALLOW_COPY_AND_ASSIGN(TextInputControllerBindings); |
| 65 | }; |
| 66 | |
| 67 | gin::WrapperInfo TextInputControllerBindings::kWrapperInfo = { |
| 68 | gin::kEmbedderNativeGin}; |
| 69 | |
| 70 | // static |
| 71 | void TextInputControllerBindings::Install( |
| 72 | base::WeakPtr<TextInputController> controller, |
lukasza | 8b6d5f3 | 2016-04-22 16:56:31 | [diff] [blame] | 73 | blink::WebLocalFrame* frame) { |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 74 | v8::Isolate* isolate = blink::MainThreadIsolate(); |
[email protected] | f751653a9 | 2014-02-18 16:32:55 | [diff] [blame] | 75 | v8::HandleScope handle_scope(isolate); |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 76 | v8::Local<v8::Context> context = frame->MainWorldScriptContext(); |
[email protected] | f751653a9 | 2014-02-18 16:32:55 | [diff] [blame] | 77 | if (context.IsEmpty()) |
| 78 | return; |
| 79 | |
| 80 | v8::Context::Scope context_scope(context); |
| 81 | |
| 82 | gin::Handle<TextInputControllerBindings> bindings = |
| 83 | gin::CreateHandle(isolate, new TextInputControllerBindings(controller)); |
[email protected] | ad4d203 | 2014-04-28 13:50:59 | [diff] [blame] | 84 | if (bindings.IsEmpty()) |
| 85 | return; |
deepak.s | 750d68f | 2015-04-30 07:32:41 | [diff] [blame] | 86 | v8::Local<v8::Object> global = context->Global(); |
Dan Elphick | a83be51 | 2019-02-05 15:57:23 | [diff] [blame] | 87 | global |
| 88 | ->Set(context, gin::StringToV8(isolate, "textInputController"), |
| 89 | bindings.ToV8()) |
| 90 | .Check(); |
[email protected] | f751653a9 | 2014-02-18 16:32:55 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | TextInputControllerBindings::TextInputControllerBindings( |
| 94 | base::WeakPtr<TextInputController> controller) |
| 95 | : controller_(controller) {} |
| 96 | |
| 97 | TextInputControllerBindings::~TextInputControllerBindings() {} |
| 98 | |
| 99 | gin::ObjectTemplateBuilder |
| 100 | TextInputControllerBindings::GetObjectTemplateBuilder(v8::Isolate* isolate) { |
| 101 | return gin::Wrappable<TextInputControllerBindings>::GetObjectTemplateBuilder( |
| 102 | isolate) |
| 103 | .SetMethod("insertText", &TextInputControllerBindings::InsertText) |
| 104 | .SetMethod("unmarkText", &TextInputControllerBindings::UnmarkText) |
Ryan Landay | 7b5dbd5 | 2018-01-11 19:05:59 | [diff] [blame] | 105 | .SetMethod("unmarkAndUnselectText", |
| 106 | &TextInputControllerBindings::UnmarkAndUnselectText) |
[email protected] | f751653a9 | 2014-02-18 16:32:55 | [diff] [blame] | 107 | .SetMethod("doCommand", &TextInputControllerBindings::DoCommand) |
Ryan Landay | 9c680942 | 2018-01-17 07:04:15 | [diff] [blame] | 108 | .SetMethod("extendSelectionAndDelete", |
| 109 | &TextInputControllerBindings::ExtendSelectionAndDelete) |
Ryan Landay | 6d163ae | 2018-01-17 06:58:06 | [diff] [blame] | 110 | .SetMethod("deleteSurroundingText", |
| 111 | &TextInputControllerBindings::DeleteSurroundingText) |
[email protected] | f751653a9 | 2014-02-18 16:32:55 | [diff] [blame] | 112 | .SetMethod("setMarkedText", &TextInputControllerBindings::SetMarkedText) |
Ryan Landay | d203467 | 2018-01-12 22:22:32 | [diff] [blame] | 113 | .SetMethod("setMarkedTextFromExistingText", |
| 114 | &TextInputControllerBindings::SetMarkedTextFromExistingText) |
[email protected] | f751653a9 | 2014-02-18 16:32:55 | [diff] [blame] | 115 | .SetMethod("hasMarkedText", &TextInputControllerBindings::HasMarkedText) |
| 116 | .SetMethod("markedRange", &TextInputControllerBindings::MarkedRange) |
| 117 | .SetMethod("selectedRange", &TextInputControllerBindings::SelectedRange) |
| 118 | .SetMethod("firstRectForCharacterRange", |
| 119 | &TextInputControllerBindings::FirstRectForCharacterRange) |
ekaramad | 2daaf67 | 2016-11-10 20:29:01 | [diff] [blame] | 120 | .SetMethod("setComposition", &TextInputControllerBindings::SetComposition) |
| 121 | .SetMethod("forceTextInputStateUpdate", |
| 122 | &TextInputControllerBindings::ForceTextInputStateUpdate); |
[email protected] | f751653a9 | 2014-02-18 16:32:55 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | void TextInputControllerBindings::InsertText(const std::string& text) { |
| 126 | if (controller_) |
| 127 | controller_->InsertText(text); |
| 128 | } |
| 129 | |
| 130 | void TextInputControllerBindings::UnmarkText() { |
| 131 | if (controller_) |
| 132 | controller_->UnmarkText(); |
| 133 | } |
| 134 | |
Ryan Landay | 7b5dbd5 | 2018-01-11 19:05:59 | [diff] [blame] | 135 | void TextInputControllerBindings::UnmarkAndUnselectText() { |
| 136 | if (controller_) |
| 137 | controller_->UnmarkAndUnselectText(); |
| 138 | } |
| 139 | |
[email protected] | f751653a9 | 2014-02-18 16:32:55 | [diff] [blame] | 140 | void TextInputControllerBindings::DoCommand(const std::string& text) { |
| 141 | if (controller_) |
| 142 | controller_->DoCommand(text); |
| 143 | } |
| 144 | |
Ryan Landay | 9c680942 | 2018-01-17 07:04:15 | [diff] [blame] | 145 | void TextInputControllerBindings::ExtendSelectionAndDelete(int before, |
| 146 | int after) { |
| 147 | if (controller_) |
| 148 | controller_->ExtendSelectionAndDelete(before, after); |
| 149 | } |
| 150 | |
Ryan Landay | 6d163ae | 2018-01-17 06:58:06 | [diff] [blame] | 151 | void TextInputControllerBindings::DeleteSurroundingText(int before, int after) { |
| 152 | if (controller_) |
| 153 | controller_->DeleteSurroundingText(before, after); |
| 154 | } |
| 155 | |
[email protected] | f751653a9 | 2014-02-18 16:32:55 | [diff] [blame] | 156 | void TextInputControllerBindings::SetMarkedText(const std::string& text, |
| 157 | int start, |
| 158 | int length) { |
| 159 | if (controller_) |
| 160 | controller_->SetMarkedText(text, start, length); |
| 161 | } |
| 162 | |
Ryan Landay | d203467 | 2018-01-12 22:22:32 | [diff] [blame] | 163 | void TextInputControllerBindings::SetMarkedTextFromExistingText(int start, |
| 164 | int end) { |
| 165 | if (controller_) |
| 166 | controller_->SetMarkedTextFromExistingText(start, end); |
| 167 | } |
| 168 | |
[email protected] | f751653a9 | 2014-02-18 16:32:55 | [diff] [blame] | 169 | bool TextInputControllerBindings::HasMarkedText() { |
| 170 | return controller_ ? controller_->HasMarkedText() : false; |
| 171 | } |
| 172 | |
| 173 | std::vector<int> TextInputControllerBindings::MarkedRange() { |
| 174 | return controller_ ? controller_->MarkedRange() : std::vector<int>(); |
| 175 | } |
| 176 | |
| 177 | std::vector<int> TextInputControllerBindings::SelectedRange() { |
| 178 | return controller_ ? controller_->SelectedRange() : std::vector<int>(); |
| 179 | } |
| 180 | |
| 181 | std::vector<int> TextInputControllerBindings::FirstRectForCharacterRange( |
| 182 | unsigned location, |
| 183 | unsigned length) { |
| 184 | return controller_ ? controller_->FirstRectForCharacterRange(location, length) |
| 185 | : std::vector<int>(); |
| 186 | } |
| 187 | |
| 188 | void TextInputControllerBindings::SetComposition(const std::string& text) { |
| 189 | if (controller_) |
| 190 | controller_->SetComposition(text); |
| 191 | } |
ekaramad | 2daaf67 | 2016-11-10 20:29:01 | [diff] [blame] | 192 | void TextInputControllerBindings::ForceTextInputStateUpdate() { |
| 193 | if (controller_) |
| 194 | controller_->ForceTextInputStateUpdate(); |
| 195 | } |
[email protected] | f751653a9 | 2014-02-18 16:32:55 | [diff] [blame] | 196 | // TextInputController --------------------------------------------------------- |
| 197 | |
Albert J. Wong | 2727e8a8 | 2019-02-15 16:56:11 | [diff] [blame] | 198 | TextInputController::TextInputController(WebViewTestProxy* web_view_test_proxy) |
Jeremy Roman | 3bca4bf | 2019-07-11 03:41:25 | [diff] [blame] | 199 | : web_view_test_proxy_(web_view_test_proxy) {} |
[email protected] | f751653a9 | 2014-02-18 16:32:55 | [diff] [blame] | 200 | |
| 201 | TextInputController::~TextInputController() {} |
| 202 | |
lukasza | 8b6d5f3 | 2016-04-22 16:56:31 | [diff] [blame] | 203 | void TextInputController::Install(blink::WebLocalFrame* frame) { |
[email protected] | f751653a9 | 2014-02-18 16:32:55 | [diff] [blame] | 204 | TextInputControllerBindings::Install(weak_factory_.GetWeakPtr(), frame); |
| 205 | } |
| 206 | |
[email protected] | f751653a9 | 2014-02-18 16:32:55 | [diff] [blame] | 207 | void TextInputController::InsertText(const std::string& text) { |
ekaramad | c75b1b3b3 | 2016-12-02 03:57:52 | [diff] [blame] | 208 | if (auto* controller = GetInputMethodController()) { |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 209 | controller->CommitText(blink::WebString::FromUTF8(text), |
Ryan Landay | 9e42fd74 | 2017-08-12 01:59:11 | [diff] [blame] | 210 | std::vector<blink::WebImeTextSpan>(), |
ekaramad | ce32ef9f | 2017-02-09 17:33:56 | [diff] [blame] | 211 | blink::WebRange(), 0); |
ekaramad | c75b1b3b3 | 2016-12-02 03:57:52 | [diff] [blame] | 212 | } |
[email protected] | f751653a9 | 2014-02-18 16:32:55 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | void TextInputController::UnmarkText() { |
ekaramad | c75b1b3b3 | 2016-12-02 03:57:52 | [diff] [blame] | 216 | if (auto* controller = GetInputMethodController()) { |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 217 | controller->FinishComposingText( |
| 218 | blink::WebInputMethodController::kKeepSelection); |
ekaramad | c75b1b3b3 | 2016-12-02 03:57:52 | [diff] [blame] | 219 | } |
[email protected] | f751653a9 | 2014-02-18 16:32:55 | [diff] [blame] | 220 | } |
| 221 | |
Ryan Landay | 7b5dbd5 | 2018-01-11 19:05:59 | [diff] [blame] | 222 | void TextInputController::UnmarkAndUnselectText() { |
| 223 | if (auto* controller = GetInputMethodController()) { |
| 224 | controller->FinishComposingText( |
| 225 | blink::WebInputMethodController::kDoNotKeepSelection); |
| 226 | } |
| 227 | } |
| 228 | |
[email protected] | f751653a9 | 2014-02-18 16:32:55 | [diff] [blame] | 229 | void TextInputController::DoCommand(const std::string& text) { |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 230 | if (view()->MainFrame()) { |
Ryan Landay | d203467 | 2018-01-12 22:22:32 | [diff] [blame] | 231 | CHECK(view()->MainFrame()->ToWebLocalFrame()) << "This function cannot be " |
| 232 | "called if the main frame " |
| 233 | "is not a local frame."; |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 234 | view()->MainFrame()->ToWebLocalFrame()->ExecuteCommand( |
| 235 | blink::WebString::FromUTF8(text)); |
yabinh | 8204efc | 2016-07-08 13:58:57 | [diff] [blame] | 236 | } |
[email protected] | f751653a9 | 2014-02-18 16:32:55 | [diff] [blame] | 237 | } |
| 238 | |
Ryan Landay | 9c680942 | 2018-01-17 07:04:15 | [diff] [blame] | 239 | void TextInputController::ExtendSelectionAndDelete(int before, int after) { |
| 240 | if (view()->MainFrame()) { |
| 241 | CHECK(view()->MainFrame()->ToWebLocalFrame()) << "This function cannot be " |
| 242 | "called if the main frame " |
| 243 | "is not a local frame."; |
| 244 | view()->MainFrame()->ToWebLocalFrame()->ExtendSelectionAndDelete(before, |
| 245 | after); |
| 246 | } |
| 247 | } |
| 248 | |
Ryan Landay | 6d163ae | 2018-01-17 06:58:06 | [diff] [blame] | 249 | void TextInputController::DeleteSurroundingText(int before, int after) { |
| 250 | if (view()->MainFrame()) { |
| 251 | CHECK(view()->MainFrame()->ToWebLocalFrame()) << "This function cannot be " |
| 252 | "called if the main frame " |
| 253 | "is not a local frame."; |
| 254 | view()->MainFrame()->ToWebLocalFrame()->DeleteSurroundingText(before, |
| 255 | after); |
| 256 | } |
| 257 | } |
| 258 | |
[email protected] | f751653a9 | 2014-02-18 16:32:55 | [diff] [blame] | 259 | void TextInputController::SetMarkedText(const std::string& text, |
| 260 | int start, |
| 261 | int length) { |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 262 | blink::WebString web_text(blink::WebString::FromUTF8(text)); |
[email protected] | f751653a9 | 2014-02-18 16:32:55 | [diff] [blame] | 263 | |
| 264 | // Split underline into up to 3 elements (before, selection, and after). |
Ryan Landay | 9e42fd74 | 2017-08-12 01:59:11 | [diff] [blame] | 265 | std::vector<blink::WebImeTextSpan> ime_text_spans; |
| 266 | blink::WebImeTextSpan ime_text_span; |
[email protected] | f751653a9 | 2014-02-18 16:32:55 | [diff] [blame] | 267 | if (!start) { |
Ryan Landay | 9e42fd74 | 2017-08-12 01:59:11 | [diff] [blame] | 268 | ime_text_span.end_offset = length; |
[email protected] | f751653a9 | 2014-02-18 16:32:55 | [diff] [blame] | 269 | } else { |
Ryan Landay | 9e42fd74 | 2017-08-12 01:59:11 | [diff] [blame] | 270 | ime_text_span.end_offset = start; |
| 271 | ime_text_spans.push_back(ime_text_span); |
| 272 | ime_text_span.start_offset = start; |
| 273 | ime_text_span.end_offset = start + length; |
[email protected] | f751653a9 | 2014-02-18 16:32:55 | [diff] [blame] | 274 | } |
Shu Chen | 855e1dc | 2019-03-22 00:27:03 | [diff] [blame] | 275 | ime_text_span.thickness = ui::mojom::ImeTextSpanThickness::kThick; |
Anupam Snigdha | 614b7ad | 2020-01-16 06:09:27 | [diff] [blame] | 276 | ime_text_span.underline_style = ui::mojom::ImeTextSpanUnderlineStyle::kSolid; |
Ryan Landay | 9e42fd74 | 2017-08-12 01:59:11 | [diff] [blame] | 277 | ime_text_spans.push_back(ime_text_span); |
[email protected] | f751653a9 | 2014-02-18 16:32:55 | [diff] [blame] | 278 | if (start + length < static_cast<int>(web_text.length())) { |
Ryan Landay | 9e42fd74 | 2017-08-12 01:59:11 | [diff] [blame] | 279 | ime_text_span.start_offset = ime_text_span.end_offset; |
| 280 | ime_text_span.end_offset = web_text.length(); |
Shu Chen | 855e1dc | 2019-03-22 00:27:03 | [diff] [blame] | 281 | ime_text_span.thickness = ui::mojom::ImeTextSpanThickness::kThin; |
Anupam Snigdha | 614b7ad | 2020-01-16 06:09:27 | [diff] [blame] | 282 | ime_text_span.underline_style = |
| 283 | ui::mojom::ImeTextSpanUnderlineStyle::kSolid; |
Ryan Landay | 9e42fd74 | 2017-08-12 01:59:11 | [diff] [blame] | 284 | ime_text_spans.push_back(ime_text_span); |
[email protected] | f751653a9 | 2014-02-18 16:32:55 | [diff] [blame] | 285 | } |
| 286 | |
ekaramad | c75b1b3b3 | 2016-12-02 03:57:52 | [diff] [blame] | 287 | if (auto* controller = GetInputMethodController()) { |
Ryan Landay | 9e42fd74 | 2017-08-12 01:59:11 | [diff] [blame] | 288 | controller->SetComposition(web_text, ime_text_spans, blink::WebRange(), |
| 289 | start, start + length); |
ekaramad | c75b1b3b3 | 2016-12-02 03:57:52 | [diff] [blame] | 290 | } |
[email protected] | f751653a9 | 2014-02-18 16:32:55 | [diff] [blame] | 291 | } |
| 292 | |
Ryan Landay | d203467 | 2018-01-12 22:22:32 | [diff] [blame] | 293 | void TextInputController::SetMarkedTextFromExistingText(int start, int end) { |
| 294 | if (!view()->MainFrame()) |
| 295 | return; |
| 296 | |
| 297 | CHECK(view()->MainFrame()->ToWebLocalFrame()) << "This function cannot be " |
| 298 | "called if the main frame " |
| 299 | "is not a local frame."; |
| 300 | |
| 301 | view()->MainFrame()->ToWebLocalFrame()->SetCompositionFromExistingText( |
| 302 | start, end, std::vector<blink::WebImeTextSpan>()); |
| 303 | } |
| 304 | |
[email protected] | f751653a9 | 2014-02-18 16:32:55 | [diff] [blame] | 305 | bool TextInputController::HasMarkedText() { |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 306 | if (!view()->MainFrame()) |
yabinh | 8204efc | 2016-07-08 13:58:57 | [diff] [blame] | 307 | return false; |
| 308 | |
Ryan Landay | d203467 | 2018-01-12 22:22:32 | [diff] [blame] | 309 | CHECK(view()->MainFrame()->ToWebLocalFrame()) << "This function cannot be " |
| 310 | "called if the main frame " |
| 311 | "is not a local frame."; |
yabinh | 8204efc | 2016-07-08 13:58:57 | [diff] [blame] | 312 | |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 313 | return view()->MainFrame()->ToWebLocalFrame()->HasMarkedText(); |
[email protected] | f751653a9 | 2014-02-18 16:32:55 | [diff] [blame] | 314 | } |
| 315 | |
| 316 | std::vector<int> TextInputController::MarkedRange() { |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 317 | if (!view()->MainFrame()) |
[email protected] | 6f939712 | 2014-02-25 09:30:50 | [diff] [blame] | 318 | return std::vector<int>(); |
| 319 | |
Ryan Landay | d203467 | 2018-01-12 22:22:32 | [diff] [blame] | 320 | CHECK(view()->MainFrame()->ToWebLocalFrame()) << "This function cannot be " |
| 321 | "called if the main frame " |
| 322 | "is not a local frame."; |
yabinh | 8204efc | 2016-07-08 13:58:57 | [diff] [blame] | 323 | |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 324 | blink::WebRange range = view()->MainFrame()->ToWebLocalFrame()->MarkedRange(); |
[email protected] | f751653a9 | 2014-02-18 16:32:55 | [diff] [blame] | 325 | std::vector<int> int_array(2); |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 326 | int_array[0] = range.StartOffset(); |
| 327 | int_array[1] = range.EndOffset(); |
[email protected] | f751653a9 | 2014-02-18 16:32:55 | [diff] [blame] | 328 | |
| 329 | return int_array; |
| 330 | } |
| 331 | |
| 332 | std::vector<int> TextInputController::SelectedRange() { |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 333 | if (!view()->MainFrame()) |
[email protected] | 6f939712 | 2014-02-25 09:30:50 | [diff] [blame] | 334 | return std::vector<int>(); |
| 335 | |
Ryan Landay | d203467 | 2018-01-12 22:22:32 | [diff] [blame] | 336 | CHECK(view()->MainFrame()->ToWebLocalFrame()) << "This function cannot be " |
| 337 | "called if the main frame " |
| 338 | "is not a local frame."; |
yabinh | 8204efc | 2016-07-08 13:58:57 | [diff] [blame] | 339 | |
| 340 | blink::WebRange range = |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 341 | view()->MainFrame()->ToWebLocalFrame()->SelectionRange(); |
| 342 | if (range.IsNull()) |
yosin | 702c78a3 | 2016-06-13 08:39:13 | [diff] [blame] | 343 | return std::vector<int>(); |
[email protected] | f751653a9 | 2014-02-18 16:32:55 | [diff] [blame] | 344 | std::vector<int> int_array(2); |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 345 | int_array[0] = range.StartOffset(); |
| 346 | int_array[1] = range.EndOffset(); |
[email protected] | f751653a9 | 2014-02-18 16:32:55 | [diff] [blame] | 347 | |
| 348 | return int_array; |
| 349 | } |
| 350 | |
| 351 | std::vector<int> TextInputController::FirstRectForCharacterRange( |
| 352 | unsigned location, |
| 353 | unsigned length) { |
| 354 | blink::WebRect rect; |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 355 | if (!view()->FocusedFrame() || |
| 356 | !view()->FocusedFrame()->FirstRectForCharacterRange(location, length, |
lukasza | 8b6d5f3 | 2016-04-22 16:56:31 | [diff] [blame] | 357 | rect)) { |
[email protected] | f751653a9 | 2014-02-18 16:32:55 | [diff] [blame] | 358 | return std::vector<int>(); |
[email protected] | 6f939712 | 2014-02-25 09:30:50 | [diff] [blame] | 359 | } |
[email protected] | f751653a9 | 2014-02-18 16:32:55 | [diff] [blame] | 360 | |
| 361 | std::vector<int> int_array(4); |
| 362 | int_array[0] = rect.x; |
| 363 | int_array[1] = rect.y; |
| 364 | int_array[2] = rect.width; |
| 365 | int_array[3] = rect.height; |
| 366 | |
| 367 | return int_array; |
| 368 | } |
| 369 | |
| 370 | void TextInputController::SetComposition(const std::string& text) { |
| 371 | // Sends a keydown event with key code = 0xE5 to emulate input method |
| 372 | // behavior. |
Dave Tapuska | 347d60a | 2020-04-21 23:55:47 | [diff] [blame] | 373 | blink::WebKeyboardEvent key_down(blink::WebInputEvent::Type::kRawKeyDown, |
Daniel Cheng | 224569ee | 2018-04-25 05:45:06 | [diff] [blame] | 374 | blink::WebInputEvent::kNoModifiers, |
| 375 | ui::EventTimeForNow()); |
dtapuska | 899ac22 | 2017-01-03 18:09:16 | [diff] [blame] | 376 | |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 377 | key_down.windows_key_code = 0xE5; // VKEY_PROCESSKEY |
danakj | 763c240 | 2018-11-09 02:46:22 | [diff] [blame] | 378 | view()->MainFrameWidget()->HandleInputEvent( |
| 379 | blink::WebCoalescedInputEvent(key_down)); |
[email protected] | f751653a9 | 2014-02-18 16:32:55 | [diff] [blame] | 380 | |
yabinh | dd6d1b6 | 2016-08-23 07:09:02 | [diff] [blame] | 381 | // The value returned by std::string::length() may not correspond to the |
| 382 | // actual number of encoded characters in sequences of multi-byte or |
| 383 | // variable-length characters. |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 384 | blink::WebString newText = blink::WebString::FromUTF8(text); |
yabinh | dd6d1b6 | 2016-08-23 07:09:02 | [diff] [blame] | 385 | size_t textLength = newText.length(); |
| 386 | |
Ryan Landay | 9e42fd74 | 2017-08-12 01:59:11 | [diff] [blame] | 387 | std::vector<blink::WebImeTextSpan> ime_text_spans; |
Ryan Landay | 9387aa9 | 2017-08-16 23:16:52 | [diff] [blame] | 388 | ime_text_spans.push_back(blink::WebImeTextSpan( |
Manuel Rego Casasnovas | 6743b45 | 2018-03-22 20:42:39 | [diff] [blame] | 389 | blink::WebImeTextSpan::Type::kComposition, 0, textLength, |
Anupam Snigdha | 614b7ad | 2020-01-16 06:09:27 | [diff] [blame] | 390 | ui::mojom::ImeTextSpanThickness::kThin, |
| 391 | ui::mojom::ImeTextSpanUnderlineStyle::kSolid, SK_ColorTRANSPARENT)); |
ekaramad | c75b1b3b3 | 2016-12-02 03:57:52 | [diff] [blame] | 392 | if (auto* controller = GetInputMethodController()) { |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 393 | controller->SetComposition( |
Ryan Landay | 9e42fd74 | 2017-08-12 01:59:11 | [diff] [blame] | 394 | newText, blink::WebVector<blink::WebImeTextSpan>(ime_text_spans), |
ekaramad | ce32ef9f | 2017-02-09 17:33:56 | [diff] [blame] | 395 | blink::WebRange(), textLength, textLength); |
ekaramad | c75b1b3b3 | 2016-12-02 03:57:52 | [diff] [blame] | 396 | } |
lukasza | 8b6d5f3 | 2016-04-22 16:56:31 | [diff] [blame] | 397 | } |
| 398 | |
ekaramad | 2daaf67 | 2016-11-10 20:29:01 | [diff] [blame] | 399 | void TextInputController::ForceTextInputStateUpdate() { |
Kent Tamura | 21d1de6 | 2018-12-10 04:45:20 | [diff] [blame] | 400 | // TODO(lukasza): Finish adding OOPIF support to the web tests harness. |
danakj | e3e48d4 | 2020-05-01 23:47:33 | [diff] [blame^] | 401 | RenderFrameImpl* main_frame = web_view_test_proxy_->GetMainRenderFrame(); |
| 402 | CHECK(main_frame) << "WebView does not have a local main frame and" |
| 403 | << " cannot handle input method controller tasks."; |
| 404 | RenderWidget* main_widget = main_frame->GetLocalRootRenderWidget(); |
| 405 | main_widget->ShowVirtualKeyboard(); |
ekaramad | 2daaf67 | 2016-11-10 20:29:01 | [diff] [blame] | 406 | } |
| 407 | |
lukasza | 8b6d5f3 | 2016-04-22 16:56:31 | [diff] [blame] | 408 | blink::WebView* TextInputController::view() { |
Antonio Gomes | 778a0f7 | 2020-02-24 13:52:44 | [diff] [blame] | 409 | return web_view_test_proxy_->GetWebView(); |
[email protected] | f751653a9 | 2014-02-18 16:32:55 | [diff] [blame] | 410 | } |
| 411 | |
ekaramad | c75b1b3b3 | 2016-12-02 03:57:52 | [diff] [blame] | 412 | blink::WebInputMethodController* |
| 413 | TextInputController::GetInputMethodController() { |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 414 | if (!view()->MainFrame()) |
ekaramad | c75b1b3b3 | 2016-12-02 03:57:52 | [diff] [blame] | 415 | return nullptr; |
| 416 | |
Kent Tamura | 21d1de6 | 2018-12-10 04:45:20 | [diff] [blame] | 417 | // TODO(lukasza): Finish adding OOPIF support to the web tests harness. |
Daniel Cheng | 3c82943 | 2017-06-19 03:42:31 | [diff] [blame] | 418 | CHECK(view()->MainFrame()->IsWebLocalFrame()) |
| 419 | << "WebView does not have a local main frame and" |
| 420 | " cannot handle input method controller tasks."; |
| 421 | |
danakj | e3e48d4 | 2020-05-01 23:47:33 | [diff] [blame^] | 422 | return view()->MainFrameWidget()->GetActiveWebInputMethodController(); |
ekaramad | 2daaf67 | 2016-11-10 20:29:01 | [diff] [blame] | 423 | } |
| 424 | |
danakj | 741848a | 2020-04-07 22:48:06 | [diff] [blame] | 425 | } // namespace content |