OLD | NEW |
---|---|
(Empty) | |
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 | |
5 #include "content/shell/renderer/test_runner/text_input_controller.h" | |
6 | |
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" | |
18 | |
19 namespace content { | |
20 | |
21 class TextInputControllerBindings | |
22 : public gin::Wrappable<TextInputControllerBindings> { | |
23 public: | |
24 static gin::WrapperInfo kWrapperInfo; | |
25 | |
26 static void Install(base::WeakPtr<TextInputController> controller, | |
27 blink::WebFrame* frame); | |
28 | |
29 private: | |
30 explicit TextInputControllerBindings( | |
31 base::WeakPtr<TextInputController> controller); | |
32 virtual ~TextInputControllerBindings(); | |
33 | |
34 // gin::Wrappable: | |
35 virtual gin::ObjectTemplateBuilder GetObjectTemplateBuilder( | |
36 v8::Isolate* isolate) OVERRIDE; | |
37 | |
38 void InsertText(const std::string& text); | |
39 void UnmarkText(); | |
40 void DoCommand(const std::string& text); | |
41 void SetMarkedText(const std::string& text); | |
42 bool HasMarkedText(); | |
43 std::vector<int> MarkedRange(); | |
44 std::vector<int> SelectedRange(); | |
45 std::vector<int> FirstRectForCharacterRange(const std::string& text); | |
46 void SetComposition(const std::string& text); | |
47 | |
48 base::WeakPtr<TextInputController> controller_; | |
49 | |
50 DISALLOW_COPY_AND_ASSIGN(TextInputControllerBindings); | |
51 }; | |
52 | |
53 // static | |
jochen (gone - plz use gerrit)
2014/02/14 10:01:17
this comment is only needed for methods
tfarina
2014/02/14 12:46:12
Done.
| |
54 gin::WrapperInfo TextInputControllerBindings::kWrapperInfo = { | |
55 gin::kEmbedderNativeGin | |
jochen (gone - plz use gerrit)
2014/02/14 10:01:17
}; should be on this line.
tfarina
2014/02/14 12:46:12
Done.
| |
56 }; | |
57 | |
58 // static | |
59 void TextInputControllerBindings::Install( | |
60 base::WeakPtr<TextInputController> controller, | |
61 blink::WebFrame* frame) { | |
62 v8::Isolate* isolate = blink::mainThreadIsolate(); | |
63 v8::HandleScope handle_scope(isolate); | |
64 v8::Handle<v8::Context> context = frame->mainWorldScriptContext(); | |
65 if (context.IsEmpty()) | |
66 return; | |
67 | |
68 v8::Context::Scope context_scope(context); | |
69 | |
70 gin::Handle<TextInputControllerBindings> bindings = | |
71 gin::CreateHandle(isolate, new TextInputControllerBindings(controller)); | |
72 v8::Handle<v8::Object> global = context->Global(); | |
73 global->Set(gin::StringToV8(isolate, "textInputController"), bindings.ToV8()); | |
74 } | |
75 | |
76 TextInputControllerBindings::TextInputControllerBindings( | |
77 base::WeakPtr<TextInputController> controller) | |
78 : controller_(controller) {} | |
79 | |
80 TextInputControllerBindings::~TextInputControllerBindings() {} | |
81 | |
82 gin::ObjectTemplateBuilder | |
83 TextInputControllerBindings::GetObjectTemplateBuilder(v8::Isolate* isolate) { | |
84 return gin::Wrappable<TextInputControllerBindings>::GetObjectTemplateBuilder(i solate) | |
jochen (gone - plz use gerrit)
2014/02/14 10:01:17
80c
tfarina
2014/02/14 12:46:12
Done.
| |
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); | |
96 } | |
97 | |
98 void TextInputControllerBindings::InsertText(const std::string& text) { | |
99 if (controller_) | |
100 controller_->InsertText(text); | |
101 } | |
102 | |
103 void TextInputControllerBindings::UnmarkText() { | |
104 if (controller_) | |
105 controller_->UnmarkText(); | |
106 } | |
107 | |
108 void TextInputControllerBindings::DoCommand(const std::string& text) { | |
109 if (controller_) | |
110 controller_->DoCommand(text); | |
111 } | |
112 | |
113 void TextInputControllerBindings::SetMarkedText(const std::string& text) { | |
114 if (controller_) | |
115 controller_->SetMarkedText(text); | |
116 } | |
117 | |
118 bool TextInputControllerBindings::HasMarkedText() { | |
119 return controller_ ? controller_->HasMarkedText() : false; | |
120 } | |
121 | |
122 std::vector<int> TextInputControllerBindings::MarkedRange() { | |
123 return controller_ ? controller_->MarkedRange() : std::vector<int>(); | |
124 } | |
125 | |
126 std::vector<int> TextInputControllerBindings::SelectedRange() { | |
127 return controller_ ? controller_->SelectedRange() : std::vector<int>(); | |
128 } | |
129 | |
130 std::vector<int> TextInputControllerBindings::FirstRectForCharacterRange( | |
131 const std::string& text) { | |
132 return controller_ ? controller_->FirstRectForCharacterRange(text) | |
133 : std::vector<int>(); | |
134 } | |
135 | |
136 void TextInputControllerBindings::SetComposition(const std::string& text) { | |
137 if (controller_) | |
138 controller_->SetComposition(text); | |
139 } | |
140 | |
141 // TextInputController --------------------------------------------------------- | |
142 | |
143 TextInputController::TextInputController() | |
144 : frame_(NULL), weak_factory_(this) {} | |
145 | |
146 TextInputController::~TextInputController() {} | |
147 | |
148 void TextInputController::Install(blink::WebFrame* frame) { | |
149 frame_ = frame; | |
150 | |
151 TextInputControllerBindings::Install(weak_factory_.GetWeakPtr(), frame); | |
152 } | |
153 | |
154 void TextInputController::InsertText(const std::string& text) { | |
155 frame_->view()->confirmComposition(blink::WebString::fromUTF8(text)); | |
156 } | |
157 | |
158 void TextInputController::UnmarkText() { | |
159 frame_->view()->confirmComposition(); | |
160 } | |
161 | |
162 void TextInputController::DoCommand(const std::string& text) { | |
163 frame_->executeCommand(blink::WebString::fromUTF8(text)); | |
164 } | |
165 | |
166 void TextInputController::SetMarkedText(const std::string& text) { | |
167 /*WebString web_text(WebString::fromUTF8(text)); | |
jochen (gone - plz use gerrit)
2014/02/14 10:01:17
why did you comment this out?
| |
168 int start = text[1].toInt32(); | |
169 int length = text[2].toInt32(); | |
170 | |
171 // Split underline into up to 3 elements (before, selection, and after). | |
172 std::vector<WebCompositionUnderline> underlines; | |
173 WebCompositionUnderline underline; | |
174 if (!start) { | |
175 underline.endOffset = length; | |
176 } else { | |
177 underline.endOffset = start; | |
178 underlines.push_back(underline); | |
179 underline.startOffset = start; | |
180 underline.endOffset = start + length; | |
181 } | |
182 underline.thick = true; | |
183 underlines.push_back(underline); | |
184 if (start + length < static_cast<int>(text.length())) { | |
185 underline.startOffset = underline.endOffset; | |
186 underline.endOffset = text.length(); | |
187 underline.thick = false; | |
188 underlines.push_back(underline); | |
189 } | |
190 | |
191 frame_->view()->setComposition(web_text, underlines, start, start + length); | |
192 */ | |
193 } | |
194 | |
195 bool TextInputController::HasMarkedText() { | |
196 return frame_->hasMarkedText(); | |
197 } | |
198 | |
199 std::vector<int> TextInputController::MarkedRange() { | |
200 blink::WebRange range = frame_->markedRange(); | |
201 std::vector<int> int_array(2); | |
202 int_array[0] = range.startOffset(); | |
203 int_array[1] = range.endOffset(); | |
204 | |
205 return int_array; | |
206 } | |
207 | |
208 std::vector<int> TextInputController::SelectedRange() { | |
209 blink::WebRange range = frame_->selectionRange(); | |
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::FirstRectForCharacterRange( | |
218 const std::string& text) { | |
219 blink::WebRect rect; | |
220 /* if (!frame_->firstRectForCharacterRange( | |
221 arguments[0].toInt32(), arguments[1].toInt32(), rect)) | |
222 return; | |
223 | |
224 */ | |
225 std::vector<int> int_array(4); | |
226 int_array[0] = rect.x; | |
227 int_array[1] = rect.y; | |
228 int_array[2] = rect.width; | |
229 int_array[3] = rect.height; | |
230 | |
231 return int_array; | |
232 } | |
233 | |
234 void TextInputController::SetComposition(const std::string& text) { | |
235 // Sends a keydown event with key code = 0xE5 to emulate input method | |
236 // behavior. | |
237 blink::WebKeyboardEvent key_down; | |
238 key_down.type = blink::WebInputEvent::RawKeyDown; | |
239 key_down.modifiers = 0; | |
240 key_down.windowsKeyCode = 0xE5; // VKEY_PROCESSKEY | |
241 key_down.setKeyIdentifierFromWindowsKeyCode(); | |
242 frame_->view()->handleInputEvent(key_down); | |
243 | |
244 blink::WebVector<blink::WebCompositionUnderline> underlines; | |
245 blink::WebString web_text(blink::WebString::fromUTF8(text)); | |
246 frame_->view()->setComposition(web_text, underlines, 0, web_text.length()); | |
247 } | |
248 | |
249 } // namespace content | |
OLD | NEW |