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.
5 #include "content/shell/renderer/test_runner/text_input_controller.h"
7 #include "gin/arguments.h"
8 #include "gin/handle.h"
9 #include "gin/object_template_builder.h"
10 #include "gin/wrappable.h"
11 #include "third_party/WebKit/public/web/WebCompositionUnderline.h"
12 #include "third_party/WebKit/public/web/WebFrame.h"
13 #include "third_party/WebKit/public/web/WebInputEvent.h"
14 #include "third_party/WebKit/public/web/WebKit.h"
15 #include "third_party/WebKit/public/web/WebRange.h"
16 #include "third_party/WebKit/public/web/WebView.h"
17 #include "v8/include/v8.h"
21 class TextInputControllerBindings
22 : public gin::Wrappable<TextInputControllerBindings> {
24 static gin::WrapperInfo kWrapperInfo;
26 static void Install(base::WeakPtr<TextInputController> controller,
27 blink::WebFrame* frame);
30 explicit TextInputControllerBindings(
31 base::WeakPtr<TextInputController> controller);
32 virtual ~TextInputControllerBindings();
35 virtual gin::ObjectTemplateBuilder GetObjectTemplateBuilder(
36 v8::Isolate* isolate) OVERRIDE;
38 void InsertText(const std::string& text);
40 void DoCommand(const std::string& text);
41 void SetMarkedText(const std::string& text, int start, int length);
43 std::vector<int> MarkedRange();
44 std::vector<int> SelectedRange();
45 std::vector<int> FirstRectForCharacterRange(unsigned location,
47 void SetComposition(const std::string& text);
49 base::WeakPtr<TextInputController> controller_;
51 DISALLOW_COPY_AND_ASSIGN(TextInputControllerBindings);
54 gin::WrapperInfo TextInputControllerBindings::kWrapperInfo = {
55 gin::kEmbedderNativeGin};
58 void TextInputControllerBindings::Install(
59 base::WeakPtr<TextInputController> controller,
60 blink::WebFrame* frame) {
61 v8::Isolate* isolate = blink::mainThreadIsolate();
62 v8::HandleScope handle_scope(isolate);
63 v8::Handle<v8::Context> context = frame->mainWorldScriptContext();
64 if (context.IsEmpty())
67 v8::Context::Scope context_scope(context);
69 gin::Handle<TextInputControllerBindings> bindings =
70 gin::CreateHandle(isolate, new TextInputControllerBindings(controller));
71 v8::Handle<v8::Object> global = context->Global();
72 global->Set(gin::StringToV8(isolate, "textInputController"), bindings.ToV8());
75 TextInputControllerBindings::TextInputControllerBindings(
76 base::WeakPtr<TextInputController> controller)
77 : controller_(controller) {}
79 TextInputControllerBindings::~TextInputControllerBindings() {}
81 gin::ObjectTemplateBuilder
82 TextInputControllerBindings::GetObjectTemplateBuilder(v8::Isolate* isolate) {
83 return gin::Wrappable<TextInputControllerBindings>::GetObjectTemplateBuilder(
85 .SetMethod("insertText", &TextInputControllerBindings::InsertText)
86 .SetMethod("unmarkText", &TextInputControllerBindings::UnmarkText)
87 .SetMethod("doCommand", &TextInputControllerBindings::DoCommand)
88 .SetMethod("setMarkedText", &TextInputControllerBindings::SetMarkedText)
89 .SetMethod("hasMarkedText", &TextInputControllerBindings::HasMarkedText)
90 .SetMethod("markedRange", &TextInputControllerBindings::MarkedRange)
91 .SetMethod("selectedRange", &TextInputControllerBindings::SelectedRange)
92 .SetMethod("firstRectForCharacterRange",
93 &TextInputControllerBindings::FirstRectForCharacterRange)
94 .SetMethod("setComposition",
95 &TextInputControllerBindings::SetComposition);
98 void TextInputControllerBindings::InsertText(const std::string& text) {
100 controller_->InsertText(text);
103 void TextInputControllerBindings::UnmarkText() {
105 controller_->UnmarkText();
108 void TextInputControllerBindings::DoCommand(const std::string& text) {
110 controller_->DoCommand(text);
113 void TextInputControllerBindings::SetMarkedText(const std::string& text,
117 controller_->SetMarkedText(text, start, length);
120 bool TextInputControllerBindings::HasMarkedText() {
121 return controller_ ? controller_->HasMarkedText() : false;
124 std::vector<int> TextInputControllerBindings::MarkedRange() {
125 return controller_ ? controller_->MarkedRange() : std::vector<int>();
128 std::vector<int> TextInputControllerBindings::SelectedRange() {
129 return controller_ ? controller_->SelectedRange() : std::vector<int>();
132 std::vector<int> TextInputControllerBindings::FirstRectForCharacterRange(
135 return controller_ ? controller_->FirstRectForCharacterRange(location, length)
136 : std::vector<int>();
139 void TextInputControllerBindings::SetComposition(const std::string& text) {
141 controller_->SetComposition(text);
144 // TextInputController ---------------------------------------------------------
146 TextInputController::TextInputController()
147 : view_(NULL), weak_factory_(this) {}
149 TextInputController::~TextInputController() {}
151 void TextInputController::Install(blink::WebFrame* frame) {
152 TextInputControllerBindings::Install(weak_factory_.GetWeakPtr(), frame);
155 void TextInputController::SetWebView(blink::WebView* view) {
159 void TextInputController::InsertText(const std::string& text) {
160 view_->confirmComposition(blink::WebString::fromUTF8(text));
163 void TextInputController::UnmarkText() {
164 view_->confirmComposition();
167 void TextInputController::DoCommand(const std::string& text) {
168 view_->mainFrame()->executeCommand(blink::WebString::fromUTF8(text));
171 void TextInputController::SetMarkedText(const std::string& text,
174 blink::WebString web_text(blink::WebString::fromUTF8(text));
176 // Split underline into up to 3 elements (before, selection, and after).
177 std::vector<blink::WebCompositionUnderline> underlines;
178 blink::WebCompositionUnderline underline;
180 underline.endOffset = length;
182 underline.endOffset = start;
183 underlines.push_back(underline);
184 underline.startOffset = start;
185 underline.endOffset = start + length;
187 underline.thick = true;
188 underlines.push_back(underline);
189 if (start + length < static_cast<int>(web_text.length())) {
190 underline.startOffset = underline.endOffset;
191 underline.endOffset = web_text.length();
192 underline.thick = false;
193 underlines.push_back(underline);
196 view_->setComposition(web_text, underlines, start, start + length);
199 bool TextInputController::HasMarkedText() {
200 return view_->mainFrame()->hasMarkedText();
203 std::vector<int> TextInputController::MarkedRange() {
204 blink::WebRange range = view_->mainFrame()->markedRange();
205 std::vector<int> int_array(2);
206 int_array[0] = range.startOffset();
207 int_array[1] = range.endOffset();
212 std::vector<int> TextInputController::SelectedRange() {
213 blink::WebRange range = view_->mainFrame()->selectionRange();
214 std::vector<int> int_array(2);
215 int_array[0] = range.startOffset();
216 int_array[1] = range.endOffset();
221 std::vector<int> TextInputController::FirstRectForCharacterRange(
225 if (!view_->focusedFrame()->firstRectForCharacterRange(location, length, rect))
226 return std::vector<int>();
228 std::vector<int> int_array(4);
229 int_array[0] = rect.x;
230 int_array[1] = rect.y;
231 int_array[2] = rect.width;
232 int_array[3] = rect.height;
237 void TextInputController::SetComposition(const std::string& text) {
238 // Sends a keydown event with key code = 0xE5 to emulate input method
240 blink::WebKeyboardEvent key_down;
241 key_down.type = blink::WebInputEvent::RawKeyDown;
242 key_down.modifiers = 0;
243 key_down.windowsKeyCode = 0xE5; // VKEY_PROCESSKEY
244 key_down.setKeyIdentifierFromWindowsKeyCode();
245 view_->handleInputEvent(key_down);
247 blink::WebVector<blink::WebCompositionUnderline> underlines;
248 blink::WebString web_text(blink::WebString::fromUTF8(text));
249 view_->setComposition(web_text, underlines, 0, web_text.length());
252 } // namespace content