[email protected] | f751653a9 | 2014-02-18 16:32:55 | [diff] [blame] | 1 | // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
jochen | 73e711c | 2015-06-03 10:01:46 | [diff] [blame] | 5 | #include "components/test_runner/text_input_controller.h" |
[email protected] | f751653a9 | 2014-02-18 16:32:55 | [diff] [blame] | 6 | |
avi | 5dd91f8 | 2015-12-25 22:30:46 | [diff] [blame] | 7 | #include "base/macros.h" |
lukasza | 8b6d5f3 | 2016-04-22 16:56:31 | [diff] [blame^] | 8 | #include "components/test_runner/web_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" |
| 13 | #include "third_party/WebKit/public/web/WebCompositionUnderline.h" |
[email protected] | f751653a9 | 2014-02-18 16:32:55 | [diff] [blame] | 14 | #include "third_party/WebKit/public/web/WebInputEvent.h" |
| 15 | #include "third_party/WebKit/public/web/WebKit.h" |
lukasza | 8b6d5f3 | 2016-04-22 16:56:31 | [diff] [blame^] | 16 | #include "third_party/WebKit/public/web/WebLocalFrame.h" |
[email protected] | f751653a9 | 2014-02-18 16:32:55 | [diff] [blame] | 17 | #include "third_party/WebKit/public/web/WebRange.h" |
| 18 | #include "third_party/WebKit/public/web/WebView.h" |
changwan | 9bdc18f0 | 2015-11-20 02:43:52 | [diff] [blame] | 19 | #include "third_party/skia/include/core/SkColor.h" |
[email protected] | f751653a9 | 2014-02-18 16:32:55 | [diff] [blame] | 20 | #include "v8/include/v8.h" |
| 21 | |
jochen | f5f3175 | 2015-06-03 12:06:34 | [diff] [blame] | 22 | namespace test_runner { |
[email protected] | f751653a9 | 2014-02-18 16:32:55 | [diff] [blame] | 23 | |
| 24 | class TextInputControllerBindings |
| 25 | : public gin::Wrappable<TextInputControllerBindings> { |
| 26 | public: |
| 27 | static gin::WrapperInfo kWrapperInfo; |
| 28 | |
| 29 | static void Install(base::WeakPtr<TextInputController> controller, |
lukasza | 8b6d5f3 | 2016-04-22 16:56:31 | [diff] [blame^] | 30 | blink::WebLocalFrame* frame); |
[email protected] | f751653a9 | 2014-02-18 16:32:55 | [diff] [blame] | 31 | |
| 32 | private: |
| 33 | explicit TextInputControllerBindings( |
| 34 | base::WeakPtr<TextInputController> controller); |
dcheng | e933b3e | 2014-10-21 11:44:09 | [diff] [blame] | 35 | ~TextInputControllerBindings() override; |
[email protected] | f751653a9 | 2014-02-18 16:32:55 | [diff] [blame] | 36 | |
| 37 | // gin::Wrappable: |
dcheng | e933b3e | 2014-10-21 11:44:09 | [diff] [blame] | 38 | gin::ObjectTemplateBuilder GetObjectTemplateBuilder( |
anand.ratn | 449f39a4 | 2014-10-06 13:45:57 | [diff] [blame] | 39 | v8::Isolate* isolate) override; |
[email protected] | f751653a9 | 2014-02-18 16:32:55 | [diff] [blame] | 40 | |
| 41 | void InsertText(const std::string& text); |
| 42 | void UnmarkText(); |
| 43 | void DoCommand(const std::string& text); |
| 44 | void SetMarkedText(const std::string& text, int start, int length); |
| 45 | bool HasMarkedText(); |
| 46 | std::vector<int> MarkedRange(); |
| 47 | std::vector<int> SelectedRange(); |
| 48 | std::vector<int> FirstRectForCharacterRange(unsigned location, |
| 49 | unsigned length); |
| 50 | void SetComposition(const std::string& text); |
| 51 | |
| 52 | base::WeakPtr<TextInputController> controller_; |
| 53 | |
| 54 | DISALLOW_COPY_AND_ASSIGN(TextInputControllerBindings); |
| 55 | }; |
| 56 | |
| 57 | gin::WrapperInfo TextInputControllerBindings::kWrapperInfo = { |
| 58 | gin::kEmbedderNativeGin}; |
| 59 | |
| 60 | // static |
| 61 | void TextInputControllerBindings::Install( |
| 62 | base::WeakPtr<TextInputController> controller, |
lukasza | 8b6d5f3 | 2016-04-22 16:56:31 | [diff] [blame^] | 63 | blink::WebLocalFrame* frame) { |
[email protected] | f751653a9 | 2014-02-18 16:32:55 | [diff] [blame] | 64 | v8::Isolate* isolate = blink::mainThreadIsolate(); |
| 65 | v8::HandleScope handle_scope(isolate); |
deepak.s | 750d68f | 2015-04-30 07:32:41 | [diff] [blame] | 66 | v8::Local<v8::Context> context = frame->mainWorldScriptContext(); |
[email protected] | f751653a9 | 2014-02-18 16:32:55 | [diff] [blame] | 67 | if (context.IsEmpty()) |
| 68 | return; |
| 69 | |
| 70 | v8::Context::Scope context_scope(context); |
| 71 | |
| 72 | gin::Handle<TextInputControllerBindings> bindings = |
| 73 | gin::CreateHandle(isolate, new TextInputControllerBindings(controller)); |
[email protected] | ad4d203 | 2014-04-28 13:50:59 | [diff] [blame] | 74 | if (bindings.IsEmpty()) |
| 75 | return; |
deepak.s | 750d68f | 2015-04-30 07:32:41 | [diff] [blame] | 76 | v8::Local<v8::Object> global = context->Global(); |
[email protected] | f751653a9 | 2014-02-18 16:32:55 | [diff] [blame] | 77 | global->Set(gin::StringToV8(isolate, "textInputController"), bindings.ToV8()); |
| 78 | } |
| 79 | |
| 80 | TextInputControllerBindings::TextInputControllerBindings( |
| 81 | base::WeakPtr<TextInputController> controller) |
| 82 | : controller_(controller) {} |
| 83 | |
| 84 | TextInputControllerBindings::~TextInputControllerBindings() {} |
| 85 | |
| 86 | gin::ObjectTemplateBuilder |
| 87 | TextInputControllerBindings::GetObjectTemplateBuilder(v8::Isolate* isolate) { |
| 88 | return gin::Wrappable<TextInputControllerBindings>::GetObjectTemplateBuilder( |
| 89 | isolate) |
| 90 | .SetMethod("insertText", &TextInputControllerBindings::InsertText) |
| 91 | .SetMethod("unmarkText", &TextInputControllerBindings::UnmarkText) |
| 92 | .SetMethod("doCommand", &TextInputControllerBindings::DoCommand) |
| 93 | .SetMethod("setMarkedText", &TextInputControllerBindings::SetMarkedText) |
| 94 | .SetMethod("hasMarkedText", &TextInputControllerBindings::HasMarkedText) |
| 95 | .SetMethod("markedRange", &TextInputControllerBindings::MarkedRange) |
| 96 | .SetMethod("selectedRange", &TextInputControllerBindings::SelectedRange) |
| 97 | .SetMethod("firstRectForCharacterRange", |
| 98 | &TextInputControllerBindings::FirstRectForCharacterRange) |
| 99 | .SetMethod("setComposition", |
| 100 | &TextInputControllerBindings::SetComposition); |
| 101 | } |
| 102 | |
| 103 | void TextInputControllerBindings::InsertText(const std::string& text) { |
| 104 | if (controller_) |
| 105 | controller_->InsertText(text); |
| 106 | } |
| 107 | |
| 108 | void TextInputControllerBindings::UnmarkText() { |
| 109 | if (controller_) |
| 110 | controller_->UnmarkText(); |
| 111 | } |
| 112 | |
| 113 | void TextInputControllerBindings::DoCommand(const std::string& text) { |
| 114 | if (controller_) |
| 115 | controller_->DoCommand(text); |
| 116 | } |
| 117 | |
| 118 | void TextInputControllerBindings::SetMarkedText(const std::string& text, |
| 119 | int start, |
| 120 | int length) { |
| 121 | if (controller_) |
| 122 | controller_->SetMarkedText(text, start, length); |
| 123 | } |
| 124 | |
| 125 | bool TextInputControllerBindings::HasMarkedText() { |
| 126 | return controller_ ? controller_->HasMarkedText() : false; |
| 127 | } |
| 128 | |
| 129 | std::vector<int> TextInputControllerBindings::MarkedRange() { |
| 130 | return controller_ ? controller_->MarkedRange() : std::vector<int>(); |
| 131 | } |
| 132 | |
| 133 | std::vector<int> TextInputControllerBindings::SelectedRange() { |
| 134 | return controller_ ? controller_->SelectedRange() : std::vector<int>(); |
| 135 | } |
| 136 | |
| 137 | std::vector<int> TextInputControllerBindings::FirstRectForCharacterRange( |
| 138 | unsigned location, |
| 139 | unsigned length) { |
| 140 | return controller_ ? controller_->FirstRectForCharacterRange(location, length) |
| 141 | : std::vector<int>(); |
| 142 | } |
| 143 | |
| 144 | void TextInputControllerBindings::SetComposition(const std::string& text) { |
| 145 | if (controller_) |
| 146 | controller_->SetComposition(text); |
| 147 | } |
| 148 | |
| 149 | // TextInputController --------------------------------------------------------- |
| 150 | |
lukasza | 8b6d5f3 | 2016-04-22 16:56:31 | [diff] [blame^] | 151 | TextInputController::TextInputController(WebTestProxyBase* web_test_proxy_base) |
| 152 | : web_test_proxy_base_(web_test_proxy_base), weak_factory_(this) {} |
[email protected] | f751653a9 | 2014-02-18 16:32:55 | [diff] [blame] | 153 | |
| 154 | TextInputController::~TextInputController() {} |
| 155 | |
lukasza | 8b6d5f3 | 2016-04-22 16:56:31 | [diff] [blame^] | 156 | void TextInputController::Install(blink::WebLocalFrame* frame) { |
[email protected] | f751653a9 | 2014-02-18 16:32:55 | [diff] [blame] | 157 | TextInputControllerBindings::Install(weak_factory_.GetWeakPtr(), frame); |
| 158 | } |
| 159 | |
[email protected] | f751653a9 | 2014-02-18 16:32:55 | [diff] [blame] | 160 | void TextInputController::InsertText(const std::string& text) { |
lukasza | 8b6d5f3 | 2016-04-22 16:56:31 | [diff] [blame^] | 161 | view()->confirmComposition(blink::WebString::fromUTF8(text)); |
[email protected] | f751653a9 | 2014-02-18 16:32:55 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | void TextInputController::UnmarkText() { |
lukasza | 8b6d5f3 | 2016-04-22 16:56:31 | [diff] [blame^] | 165 | view()->confirmComposition(); |
[email protected] | f751653a9 | 2014-02-18 16:32:55 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | void TextInputController::DoCommand(const std::string& text) { |
lukasza | 8b6d5f3 | 2016-04-22 16:56:31 | [diff] [blame^] | 169 | if (view()->mainFrame()) |
| 170 | view()->mainFrame()->executeCommand(blink::WebString::fromUTF8(text)); |
[email protected] | f751653a9 | 2014-02-18 16:32:55 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | void TextInputController::SetMarkedText(const std::string& text, |
| 174 | int start, |
| 175 | int length) { |
| 176 | blink::WebString web_text(blink::WebString::fromUTF8(text)); |
| 177 | |
| 178 | // Split underline into up to 3 elements (before, selection, and after). |
| 179 | std::vector<blink::WebCompositionUnderline> underlines; |
| 180 | blink::WebCompositionUnderline underline; |
| 181 | if (!start) { |
| 182 | underline.endOffset = length; |
| 183 | } else { |
| 184 | underline.endOffset = start; |
| 185 | underlines.push_back(underline); |
| 186 | underline.startOffset = start; |
| 187 | underline.endOffset = start + length; |
| 188 | } |
| 189 | underline.thick = true; |
| 190 | underlines.push_back(underline); |
| 191 | if (start + length < static_cast<int>(web_text.length())) { |
| 192 | underline.startOffset = underline.endOffset; |
| 193 | underline.endOffset = web_text.length(); |
| 194 | underline.thick = false; |
| 195 | underlines.push_back(underline); |
| 196 | } |
| 197 | |
lukasza | 8b6d5f3 | 2016-04-22 16:56:31 | [diff] [blame^] | 198 | view()->setComposition(web_text, underlines, start, start + length); |
[email protected] | f751653a9 | 2014-02-18 16:32:55 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | bool TextInputController::HasMarkedText() { |
lukasza | 8b6d5f3 | 2016-04-22 16:56:31 | [diff] [blame^] | 202 | return view()->mainFrame() && view()->mainFrame()->hasMarkedText(); |
[email protected] | f751653a9 | 2014-02-18 16:32:55 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | std::vector<int> TextInputController::MarkedRange() { |
lukasza | 8b6d5f3 | 2016-04-22 16:56:31 | [diff] [blame^] | 206 | if (!view()->mainFrame()) |
[email protected] | 6f939712 | 2014-02-25 09:30:50 | [diff] [blame] | 207 | return std::vector<int>(); |
| 208 | |
lukasza | 8b6d5f3 | 2016-04-22 16:56:31 | [diff] [blame^] | 209 | blink::WebRange range = view()->mainFrame()->markedRange(); |
[email protected] | f751653a9 | 2014-02-18 16:32:55 | [diff] [blame] | 210 | std::vector<int> int_array(2); |
| 211 | int_array[0] = range.startOffset(); |
| 212 | int_array[1] = range.endOffset(); |
| 213 | |
| 214 | return int_array; |
| 215 | } |
| 216 | |
| 217 | std::vector<int> TextInputController::SelectedRange() { |
lukasza | 8b6d5f3 | 2016-04-22 16:56:31 | [diff] [blame^] | 218 | if (!view()->mainFrame()) |
[email protected] | 6f939712 | 2014-02-25 09:30:50 | [diff] [blame] | 219 | return std::vector<int>(); |
| 220 | |
lukasza | 8b6d5f3 | 2016-04-22 16:56:31 | [diff] [blame^] | 221 | blink::WebRange range = view()->mainFrame()->selectionRange(); |
[email protected] | f751653a9 | 2014-02-18 16:32:55 | [diff] [blame] | 222 | std::vector<int> int_array(2); |
| 223 | int_array[0] = range.startOffset(); |
| 224 | int_array[1] = range.endOffset(); |
| 225 | |
| 226 | return int_array; |
| 227 | } |
| 228 | |
| 229 | std::vector<int> TextInputController::FirstRectForCharacterRange( |
| 230 | unsigned location, |
| 231 | unsigned length) { |
| 232 | blink::WebRect rect; |
lukasza | 8b6d5f3 | 2016-04-22 16:56:31 | [diff] [blame^] | 233 | if (!view()->focusedFrame() || |
| 234 | !view()->focusedFrame()->firstRectForCharacterRange(location, length, |
| 235 | rect)) { |
[email protected] | f751653a9 | 2014-02-18 16:32:55 | [diff] [blame] | 236 | return std::vector<int>(); |
[email protected] | 6f939712 | 2014-02-25 09:30:50 | [diff] [blame] | 237 | } |
[email protected] | f751653a9 | 2014-02-18 16:32:55 | [diff] [blame] | 238 | |
| 239 | std::vector<int> int_array(4); |
| 240 | int_array[0] = rect.x; |
| 241 | int_array[1] = rect.y; |
| 242 | int_array[2] = rect.width; |
| 243 | int_array[3] = rect.height; |
| 244 | |
| 245 | return int_array; |
| 246 | } |
| 247 | |
| 248 | void TextInputController::SetComposition(const std::string& text) { |
| 249 | // Sends a keydown event with key code = 0xE5 to emulate input method |
| 250 | // behavior. |
| 251 | blink::WebKeyboardEvent key_down; |
| 252 | key_down.type = blink::WebInputEvent::RawKeyDown; |
| 253 | key_down.modifiers = 0; |
| 254 | key_down.windowsKeyCode = 0xE5; // VKEY_PROCESSKEY |
| 255 | key_down.setKeyIdentifierFromWindowsKeyCode(); |
lukasza | 8b6d5f3 | 2016-04-22 16:56:31 | [diff] [blame^] | 256 | view()->handleInputEvent(key_down); |
[email protected] | f751653a9 | 2014-02-18 16:32:55 | [diff] [blame] | 257 | |
changwan | 9bdc18f0 | 2015-11-20 02:43:52 | [diff] [blame] | 258 | std::vector<blink::WebCompositionUnderline> underlines; |
| 259 | underlines.push_back(blink::WebCompositionUnderline(0, text.length(), |
| 260 | SK_ColorBLACK, false, |
| 261 | SK_ColorTRANSPARENT)); |
lukasza | 8b6d5f3 | 2016-04-22 16:56:31 | [diff] [blame^] | 262 | view()->setComposition( |
changwan | 9bdc18f0 | 2015-11-20 02:43:52 | [diff] [blame] | 263 | blink::WebString::fromUTF8(text), |
| 264 | blink::WebVector<blink::WebCompositionUnderline>(underlines), |
lukasza | 8b6d5f3 | 2016-04-22 16:56:31 | [diff] [blame^] | 265 | text.length(), text.length()); |
| 266 | } |
| 267 | |
| 268 | blink::WebView* TextInputController::view() { |
| 269 | return web_test_proxy_base_->web_view(); |
[email protected] | f751653a9 | 2014-02-18 16:32:55 | [diff] [blame] | 270 | } |
| 271 | |
jochen | f5f3175 | 2015-06-03 12:06:34 | [diff] [blame] | 272 | } // namespace test_runner |