| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "components/test_runner/text_input_controller.h" | 5 #include "components/test_runner/text_input_controller.h" |
| 6 | 6 |
| 7 #include "base/macros.h" | 7 #include "base/macros.h" |
| 8 #include "components/test_runner/web_test_proxy.h" |
| 8 #include "gin/arguments.h" | 9 #include "gin/arguments.h" |
| 9 #include "gin/handle.h" | 10 #include "gin/handle.h" |
| 10 #include "gin/object_template_builder.h" | 11 #include "gin/object_template_builder.h" |
| 11 #include "gin/wrappable.h" | 12 #include "gin/wrappable.h" |
| 12 #include "third_party/WebKit/public/web/WebCompositionUnderline.h" | 13 #include "third_party/WebKit/public/web/WebCompositionUnderline.h" |
| 13 #include "third_party/WebKit/public/web/WebFrame.h" | |
| 14 #include "third_party/WebKit/public/web/WebInputEvent.h" | 14 #include "third_party/WebKit/public/web/WebInputEvent.h" |
| 15 #include "third_party/WebKit/public/web/WebKit.h" | 15 #include "third_party/WebKit/public/web/WebKit.h" |
| 16 #include "third_party/WebKit/public/web/WebLocalFrame.h" |
| 16 #include "third_party/WebKit/public/web/WebRange.h" | 17 #include "third_party/WebKit/public/web/WebRange.h" |
| 17 #include "third_party/WebKit/public/web/WebView.h" | 18 #include "third_party/WebKit/public/web/WebView.h" |
| 18 #include "third_party/skia/include/core/SkColor.h" | 19 #include "third_party/skia/include/core/SkColor.h" |
| 19 #include "v8/include/v8.h" | 20 #include "v8/include/v8.h" |
| 20 | 21 |
| 21 namespace test_runner { | 22 namespace test_runner { |
| 22 | 23 |
| 23 class TextInputControllerBindings | 24 class TextInputControllerBindings |
| 24 : public gin::Wrappable<TextInputControllerBindings> { | 25 : public gin::Wrappable<TextInputControllerBindings> { |
| 25 public: | 26 public: |
| 26 static gin::WrapperInfo kWrapperInfo; | 27 static gin::WrapperInfo kWrapperInfo; |
| 27 | 28 |
| 28 static void Install(base::WeakPtr<TextInputController> controller, | 29 static void Install(base::WeakPtr<TextInputController> controller, |
| 29 blink::WebFrame* frame); | 30 blink::WebLocalFrame* frame); |
| 30 | 31 |
| 31 private: | 32 private: |
| 32 explicit TextInputControllerBindings( | 33 explicit TextInputControllerBindings( |
| 33 base::WeakPtr<TextInputController> controller); | 34 base::WeakPtr<TextInputController> controller); |
| 34 ~TextInputControllerBindings() override; | 35 ~TextInputControllerBindings() override; |
| 35 | 36 |
| 36 // gin::Wrappable: | 37 // gin::Wrappable: |
| 37 gin::ObjectTemplateBuilder GetObjectTemplateBuilder( | 38 gin::ObjectTemplateBuilder GetObjectTemplateBuilder( |
| 38 v8::Isolate* isolate) override; | 39 v8::Isolate* isolate) override; |
| 39 | 40 |
| (...skipping 12 matching lines...) Expand all Loading... |
| 52 | 53 |
| 53 DISALLOW_COPY_AND_ASSIGN(TextInputControllerBindings); | 54 DISALLOW_COPY_AND_ASSIGN(TextInputControllerBindings); |
| 54 }; | 55 }; |
| 55 | 56 |
| 56 gin::WrapperInfo TextInputControllerBindings::kWrapperInfo = { | 57 gin::WrapperInfo TextInputControllerBindings::kWrapperInfo = { |
| 57 gin::kEmbedderNativeGin}; | 58 gin::kEmbedderNativeGin}; |
| 58 | 59 |
| 59 // static | 60 // static |
| 60 void TextInputControllerBindings::Install( | 61 void TextInputControllerBindings::Install( |
| 61 base::WeakPtr<TextInputController> controller, | 62 base::WeakPtr<TextInputController> controller, |
| 62 blink::WebFrame* frame) { | 63 blink::WebLocalFrame* frame) { |
| 63 v8::Isolate* isolate = blink::mainThreadIsolate(); | 64 v8::Isolate* isolate = blink::mainThreadIsolate(); |
| 64 v8::HandleScope handle_scope(isolate); | 65 v8::HandleScope handle_scope(isolate); |
| 65 v8::Local<v8::Context> context = frame->mainWorldScriptContext(); | 66 v8::Local<v8::Context> context = frame->mainWorldScriptContext(); |
| 66 if (context.IsEmpty()) | 67 if (context.IsEmpty()) |
| 67 return; | 68 return; |
| 68 | 69 |
| 69 v8::Context::Scope context_scope(context); | 70 v8::Context::Scope context_scope(context); |
| 70 | 71 |
| 71 gin::Handle<TextInputControllerBindings> bindings = | 72 gin::Handle<TextInputControllerBindings> bindings = |
| 72 gin::CreateHandle(isolate, new TextInputControllerBindings(controller)); | 73 gin::CreateHandle(isolate, new TextInputControllerBindings(controller)); |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 140 : std::vector<int>(); | 141 : std::vector<int>(); |
| 141 } | 142 } |
| 142 | 143 |
| 143 void TextInputControllerBindings::SetComposition(const std::string& text) { | 144 void TextInputControllerBindings::SetComposition(const std::string& text) { |
| 144 if (controller_) | 145 if (controller_) |
| 145 controller_->SetComposition(text); | 146 controller_->SetComposition(text); |
| 146 } | 147 } |
| 147 | 148 |
| 148 // TextInputController --------------------------------------------------------- | 149 // TextInputController --------------------------------------------------------- |
| 149 | 150 |
| 150 TextInputController::TextInputController() | 151 TextInputController::TextInputController(WebTestProxyBase* web_test_proxy_base) |
| 151 : view_(NULL), weak_factory_(this) {} | 152 : web_test_proxy_base_(web_test_proxy_base), weak_factory_(this) {} |
| 152 | 153 |
| 153 TextInputController::~TextInputController() {} | 154 TextInputController::~TextInputController() {} |
| 154 | 155 |
| 155 void TextInputController::Install(blink::WebFrame* frame) { | 156 void TextInputController::Install(blink::WebLocalFrame* frame) { |
| 156 TextInputControllerBindings::Install(weak_factory_.GetWeakPtr(), frame); | 157 TextInputControllerBindings::Install(weak_factory_.GetWeakPtr(), frame); |
| 157 } | 158 } |
| 158 | 159 |
| 159 void TextInputController::SetWebView(blink::WebView* view) { | |
| 160 view_ = view; | |
| 161 } | |
| 162 | |
| 163 void TextInputController::InsertText(const std::string& text) { | 160 void TextInputController::InsertText(const std::string& text) { |
| 164 view_->confirmComposition(blink::WebString::fromUTF8(text)); | 161 view()->confirmComposition(blink::WebString::fromUTF8(text)); |
| 165 } | 162 } |
| 166 | 163 |
| 167 void TextInputController::UnmarkText() { | 164 void TextInputController::UnmarkText() { |
| 168 view_->confirmComposition(); | 165 view()->confirmComposition(); |
| 169 } | 166 } |
| 170 | 167 |
| 171 void TextInputController::DoCommand(const std::string& text) { | 168 void TextInputController::DoCommand(const std::string& text) { |
| 172 if (view_->mainFrame()) | 169 if (view()->mainFrame()) |
| 173 view_->mainFrame()->executeCommand(blink::WebString::fromUTF8(text)); | 170 view()->mainFrame()->executeCommand(blink::WebString::fromUTF8(text)); |
| 174 } | 171 } |
| 175 | 172 |
| 176 void TextInputController::SetMarkedText(const std::string& text, | 173 void TextInputController::SetMarkedText(const std::string& text, |
| 177 int start, | 174 int start, |
| 178 int length) { | 175 int length) { |
| 179 blink::WebString web_text(blink::WebString::fromUTF8(text)); | 176 blink::WebString web_text(blink::WebString::fromUTF8(text)); |
| 180 | 177 |
| 181 // Split underline into up to 3 elements (before, selection, and after). | 178 // Split underline into up to 3 elements (before, selection, and after). |
| 182 std::vector<blink::WebCompositionUnderline> underlines; | 179 std::vector<blink::WebCompositionUnderline> underlines; |
| 183 blink::WebCompositionUnderline underline; | 180 blink::WebCompositionUnderline underline; |
| 184 if (!start) { | 181 if (!start) { |
| 185 underline.endOffset = length; | 182 underline.endOffset = length; |
| 186 } else { | 183 } else { |
| 187 underline.endOffset = start; | 184 underline.endOffset = start; |
| 188 underlines.push_back(underline); | 185 underlines.push_back(underline); |
| 189 underline.startOffset = start; | 186 underline.startOffset = start; |
| 190 underline.endOffset = start + length; | 187 underline.endOffset = start + length; |
| 191 } | 188 } |
| 192 underline.thick = true; | 189 underline.thick = true; |
| 193 underlines.push_back(underline); | 190 underlines.push_back(underline); |
| 194 if (start + length < static_cast<int>(web_text.length())) { | 191 if (start + length < static_cast<int>(web_text.length())) { |
| 195 underline.startOffset = underline.endOffset; | 192 underline.startOffset = underline.endOffset; |
| 196 underline.endOffset = web_text.length(); | 193 underline.endOffset = web_text.length(); |
| 197 underline.thick = false; | 194 underline.thick = false; |
| 198 underlines.push_back(underline); | 195 underlines.push_back(underline); |
| 199 } | 196 } |
| 200 | 197 |
| 201 view_->setComposition(web_text, underlines, start, start + length); | 198 view()->setComposition(web_text, underlines, start, start + length); |
| 202 } | 199 } |
| 203 | 200 |
| 204 bool TextInputController::HasMarkedText() { | 201 bool TextInputController::HasMarkedText() { |
| 205 return view_->mainFrame() && view_->mainFrame()->hasMarkedText(); | 202 return view()->mainFrame() && view()->mainFrame()->hasMarkedText(); |
| 206 } | 203 } |
| 207 | 204 |
| 208 std::vector<int> TextInputController::MarkedRange() { | 205 std::vector<int> TextInputController::MarkedRange() { |
| 209 if (!view_->mainFrame()) | 206 if (!view()->mainFrame()) |
| 210 return std::vector<int>(); | 207 return std::vector<int>(); |
| 211 | 208 |
| 212 blink::WebRange range = view_->mainFrame()->markedRange(); | 209 blink::WebRange range = view()->mainFrame()->markedRange(); |
| 213 std::vector<int> int_array(2); | 210 std::vector<int> int_array(2); |
| 214 int_array[0] = range.startOffset(); | 211 int_array[0] = range.startOffset(); |
| 215 int_array[1] = range.endOffset(); | 212 int_array[1] = range.endOffset(); |
| 216 | 213 |
| 217 return int_array; | 214 return int_array; |
| 218 } | 215 } |
| 219 | 216 |
| 220 std::vector<int> TextInputController::SelectedRange() { | 217 std::vector<int> TextInputController::SelectedRange() { |
| 221 if (!view_->mainFrame()) | 218 if (!view()->mainFrame()) |
| 222 return std::vector<int>(); | 219 return std::vector<int>(); |
| 223 | 220 |
| 224 blink::WebRange range = view_->mainFrame()->selectionRange(); | 221 blink::WebRange range = view()->mainFrame()->selectionRange(); |
| 225 std::vector<int> int_array(2); | 222 std::vector<int> int_array(2); |
| 226 int_array[0] = range.startOffset(); | 223 int_array[0] = range.startOffset(); |
| 227 int_array[1] = range.endOffset(); | 224 int_array[1] = range.endOffset(); |
| 228 | 225 |
| 229 return int_array; | 226 return int_array; |
| 230 } | 227 } |
| 231 | 228 |
| 232 std::vector<int> TextInputController::FirstRectForCharacterRange( | 229 std::vector<int> TextInputController::FirstRectForCharacterRange( |
| 233 unsigned location, | 230 unsigned location, |
| 234 unsigned length) { | 231 unsigned length) { |
| 235 blink::WebRect rect; | 232 blink::WebRect rect; |
| 236 if (!view_->focusedFrame() || | 233 if (!view()->focusedFrame() || |
| 237 !view_->focusedFrame()->firstRectForCharacterRange( | 234 !view()->focusedFrame()->firstRectForCharacterRange(location, length, |
| 238 location, length, rect)) { | 235 rect)) { |
| 239 return std::vector<int>(); | 236 return std::vector<int>(); |
| 240 } | 237 } |
| 241 | 238 |
| 242 std::vector<int> int_array(4); | 239 std::vector<int> int_array(4); |
| 243 int_array[0] = rect.x; | 240 int_array[0] = rect.x; |
| 244 int_array[1] = rect.y; | 241 int_array[1] = rect.y; |
| 245 int_array[2] = rect.width; | 242 int_array[2] = rect.width; |
| 246 int_array[3] = rect.height; | 243 int_array[3] = rect.height; |
| 247 | 244 |
| 248 return int_array; | 245 return int_array; |
| 249 } | 246 } |
| 250 | 247 |
| 251 void TextInputController::SetComposition(const std::string& text) { | 248 void TextInputController::SetComposition(const std::string& text) { |
| 252 // Sends a keydown event with key code = 0xE5 to emulate input method | 249 // Sends a keydown event with key code = 0xE5 to emulate input method |
| 253 // behavior. | 250 // behavior. |
| 254 blink::WebKeyboardEvent key_down; | 251 blink::WebKeyboardEvent key_down; |
| 255 key_down.type = blink::WebInputEvent::RawKeyDown; | 252 key_down.type = blink::WebInputEvent::RawKeyDown; |
| 256 key_down.modifiers = 0; | 253 key_down.modifiers = 0; |
| 257 key_down.windowsKeyCode = 0xE5; // VKEY_PROCESSKEY | 254 key_down.windowsKeyCode = 0xE5; // VKEY_PROCESSKEY |
| 258 key_down.setKeyIdentifierFromWindowsKeyCode(); | 255 key_down.setKeyIdentifierFromWindowsKeyCode(); |
| 259 view_->handleInputEvent(key_down); | 256 view()->handleInputEvent(key_down); |
| 260 | 257 |
| 261 std::vector<blink::WebCompositionUnderline> underlines; | 258 std::vector<blink::WebCompositionUnderline> underlines; |
| 262 underlines.push_back(blink::WebCompositionUnderline(0, text.length(), | 259 underlines.push_back(blink::WebCompositionUnderline(0, text.length(), |
| 263 SK_ColorBLACK, false, | 260 SK_ColorBLACK, false, |
| 264 SK_ColorTRANSPARENT)); | 261 SK_ColorTRANSPARENT)); |
| 265 view_->setComposition( | 262 view()->setComposition( |
| 266 blink::WebString::fromUTF8(text), | 263 blink::WebString::fromUTF8(text), |
| 267 blink::WebVector<blink::WebCompositionUnderline>(underlines), | 264 blink::WebVector<blink::WebCompositionUnderline>(underlines), |
| 268 text.length(), | 265 text.length(), text.length()); |
| 269 text.length()); | 266 } |
| 267 |
| 268 blink::WebView* TextInputController::view() { |
| 269 return web_test_proxy_base_->web_view(); |
| 270 } | 270 } |
| 271 | 271 |
| 272 } // namespace test_runner | 272 } // namespace test_runner |
| OLD | NEW |