Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(180)

Side by Side Diff: content/shell/renderer/test_runner/text_input_controller.cc

Issue 144013010: Move TextInputController from CppBoundClass to gin::Wrappable. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: TextInputControllerBindings Created 6 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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/WebBindings.h"
12 #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"
15 #include "third_party/WebKit/public/web/WebKit.h"
16 #include "third_party/WebKit/public/web/WebRange.h"
17 #include "third_party/WebKit/public/web/WebView.h"
18 #include "v8/include/v8.h"
19
20 namespace content {
21
22 class TextInputControllerBindings
23 : public gin::Wrappable<TextInputControllerBindings> {
24 public:
25 static gin::WrapperInfo kWrapperInfo;
26
27 static void Install(base::WeakPtr<TextInputController> controller,
28 blink::WebFrame* frame);
29
30 private:
31 explicit TextInputControllerBindings(
32 base::WeakPtr<TextInputController> controller);
33 virtual ~TextInputControllerBindings();
34
35 // gin::Wrappable:
36 virtual gin::ObjectTemplateBuilder GetObjectTemplateBuilder(
37 v8::Isolate* isolate) OVERRIDE;
38
39 void InsertText(const std::string& text);
40 void UnmarkText();
41 void DoCommand(const std::string& text);
42 void SetMarkedText(const std::string& text);
43 bool HasMarkedText();
44 NPObject* MarkedRange();
45 NPObject* SelectedRange();
46 NPObject* FirstRectForCharacterRange(const std::string& text);
47 void SetComposition(const std::string& text);
48
49 base::WeakPtr<TextInputController> controller_;
50
51 DISALLOW_COPY_AND_ASSIGN(TextInputControllerBindings);
52 };
53
54 // static
55 gin::WrapperInfo TextInputControllerBindings::kWrapperInfo = {
56 gin::kEmbedderNativeGin
57 };
58
59 // static
60 void TextInputControllerBindings::Install(
61 base::WeakPtr<TextInputController> controller,
62 blink::WebFrame* frame) {
63 v8::Isolate* isolate = blink::mainThreadIsolate();
64 v8::HandleScope handle_scope(isolate);
65 v8::Handle<v8::Context> context = frame->mainWorldScriptContext();
66 if (context.IsEmpty())
67 return;
68
69 v8::Context::Scope context_scope(context);
70
71 gin::Handle<TextInputControllerBindings> bindings =
72 gin::CreateHandle(isolate, new TextInputControllerBindings(controller));
73 v8::Handle<v8::Object> global = context->Global();
74 global->Set(gin::StringToV8(isolate, "textInputController"), bindings.ToV8());
75 }
76
77 TextInputControllerBindings::TextInputControllerBindings(
78 base::WeakPtr<TextInputController> controller)
79 : controller_(controller) {}
80
81 TextInputControllerBindings::~TextInputControllerBindings() {}
82
83 gin::ObjectTemplateBuilder
84 TextInputControllerBindings::GetObjectTemplateBuilder(v8::Isolate* isolate) {
85 return gin::Wrappable<TextInputControllerBindings>::GetObjectTemplateBuilder(i solate)
86 .SetMethod("insertText", &TextInputControllerBindings::InsertText)
87 .SetMethod("unmarkText", &TextInputControllerBindings::UnmarkText)
88 .SetMethod("doCommand", &TextInputControllerBindings::DoCommand)
89 .SetMethod("setMarkedText", &TextInputControllerBindings::SetMarkedText)
90 .SetMethod("hasMarkedText", &TextInputControllerBindings::HasMarkedText)
91 .SetMethod("markedRange", &TextInputControllerBindings::MarkedRange)
92 .SetMethod("selectedRange", &TextInputControllerBindings::SelectedRange)
93 .SetMethod("firstRectForCharacterRange",
94 &TextInputControllerBindings::FirstRectForCharacterRange)
95 .SetMethod("setComposition",
96 &TextInputControllerBindings::SetComposition);
97 }
98
99 void TextInputControllerBindings::InsertText(const std::string& text) {
100 if (controller_)
101 controller_->InsertText(text);
102 }
103
104 void TextInputControllerBindings::UnmarkText() {
105 if (controller_)
106 controller_->UnmarkText();
107 }
108
109 void TextInputControllerBindings::DoCommand(const std::string& text) {
110 if (controller_)
111 controller_->DoCommand(text);
112 }
113
114 void TextInputControllerBindings::SetMarkedText(const std::string& text) {
115 if (controller_)
116 controller_->SetMarkedText(text);
117 }
118
119 bool TextInputControllerBindings::HasMarkedText() {
120 return controller_ ? controller_->HasMarkedText() : false;
121 }
122
123 NPObject* TextInputControllerBindings::MarkedRange() {
124 return controller_ ? controller_->MarkedRange() : NULL;
125 }
126
127 NPObject* TextInputControllerBindings::SelectedRange() {
128 return controller_ ? controller_->SelectedRange() : NULL;
129 }
130
131 NPObject* TextInputControllerBindings::FirstRectForCharacterRange(
132 const std::string& text) {
133 return controller_ ? controller_->FirstRectForCharacterRange(text) : NULL;
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));
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 NPObject* 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 blink::WebBindings::makeIntArray(int_array);
206 }
207
208 NPObject* 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 blink::WebBindings::makeIntArray(int_array);
215 }
216
217 NPObject* 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 blink::WebBindings::makeIntArray(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
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698