Chromium Code Reviews| 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, int start, int length); | |
| 42 bool HasMarkedText(); | |
| 43 std::vector<int> MarkedRange(); | |
| 44 std::vector<int> SelectedRange(); | |
| 45 std::vector<int> FirstRectForCharacterRange(unsigned location, | |
| 46 unsigned length); | |
| 47 void SetComposition(const std::string& text); | |
| 48 | |
| 49 base::WeakPtr<TextInputController> controller_; | |
| 50 | |
| 51 DISALLOW_COPY_AND_ASSIGN(TextInputControllerBindings); | |
| 52 }; | |
| 53 | |
| 54 gin::WrapperInfo TextInputControllerBindings::kWrapperInfo = { | |
| 55 gin::kEmbedderNativeGin}; | |
| 56 | |
| 57 // static | |
| 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()) | |
| 65 return; | |
| 66 | |
| 67 v8::Context::Scope context_scope(context); | |
| 68 | |
| 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()); | |
| 73 } | |
| 74 | |
| 75 TextInputControllerBindings::TextInputControllerBindings( | |
| 76 base::WeakPtr<TextInputController> controller) | |
| 77 : controller_(controller) {} | |
| 78 | |
| 79 TextInputControllerBindings::~TextInputControllerBindings() {} | |
| 80 | |
| 81 gin::ObjectTemplateBuilder | |
| 82 TextInputControllerBindings::GetObjectTemplateBuilder(v8::Isolate* isolate) { | |
| 83 return gin::Wrappable<TextInputControllerBindings>::GetObjectTemplateBuilder( | |
| 84 isolate) | |
| 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 int start, | |
| 115 int length) { | |
| 116 if (controller_) | |
| 117 controller_->SetMarkedText(text, start, length); | |
| 118 } | |
| 119 | |
| 120 bool TextInputControllerBindings::HasMarkedText() { | |
| 121 return controller_ ? controller_->HasMarkedText() : false; | |
| 122 } | |
| 123 | |
| 124 std::vector<int> TextInputControllerBindings::MarkedRange() { | |
| 125 return controller_ ? controller_->MarkedRange() : std::vector<int>(); | |
| 126 } | |
| 127 | |
| 128 std::vector<int> TextInputControllerBindings::SelectedRange() { | |
| 129 return controller_ ? controller_->SelectedRange() : std::vector<int>(); | |
| 130 } | |
| 131 | |
| 132 std::vector<int> TextInputControllerBindings::FirstRectForCharacterRange( | |
| 133 unsigned location, | |
| 134 unsigned length) { | |
| 135 return controller_ ? controller_->FirstRectForCharacterRange(location, length) | |
| 136 : std::vector<int>(); | |
| 137 } | |
| 138 | |
| 139 void TextInputControllerBindings::SetComposition(const std::string& text) { | |
| 140 if (controller_) | |
| 141 controller_->SetComposition(text); | |
| 142 } | |
| 143 | |
| 144 // TextInputController --------------------------------------------------------- | |
| 145 | |
| 146 TextInputController::TextInputController() | |
| 147 : view_(NULL), weak_factory_(this) {} | |
| 148 | |
| 149 TextInputController::~TextInputController() {} | |
| 150 | |
| 151 void TextInputController::Install(blink::WebFrame* frame) { | |
| 152 TextInputControllerBindings::Install(weak_factory_.GetWeakPtr(), frame); | |
| 153 } | |
| 154 | |
| 155 void TextInputController::SetWebView(blink::WebView* view) { | |
| 156 view_ = view; | |
| 157 } | |
| 158 | |
| 159 void TextInputController::InsertText(const std::string& text) { | |
| 160 view_->confirmComposition(blink::WebString::fromUTF8(text)); | |
| 161 } | |
| 162 | |
| 163 void TextInputController::UnmarkText() { | |
| 164 view_->confirmComposition(); | |
| 165 } | |
| 166 | |
| 167 void TextInputController::DoCommand(const std::string& text) { | |
| 168 view_->mainFrame()->executeCommand(blink::WebString::fromUTF8(text)); | |
| 169 } | |
| 170 | |
| 171 void TextInputController::SetMarkedText(const std::string& text, | |
| 172 int start, | |
| 173 int length) { | |
| 174 blink::WebString web_text(blink::WebString::fromUTF8(text)); | |
| 175 | |
| 176 // Split underline into up to 3 elements (before, selection, and after). | |
| 177 std::vector<blink::WebCompositionUnderline> underlines; | |
| 178 blink::WebCompositionUnderline underline; | |
| 179 if (!start) { | |
| 180 underline.endOffset = length; | |
| 181 } else { | |
| 182 underline.endOffset = start; | |
| 183 underlines.push_back(underline); | |
| 184 underline.startOffset = start; | |
| 185 underline.endOffset = start + length; | |
| 186 } | |
| 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); | |
| 194 } | |
| 195 | |
| 196 view_->setComposition(web_text, underlines, start, start + length); | |
| 197 } | |
| 198 | |
| 199 bool TextInputController::HasMarkedText() { | |
| 200 return view_->mainFrame()->hasMarkedText(); | |
| 201 } | |
| 202 | |
| 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(); | |
| 208 | |
| 209 return int_array; | |
| 210 } | |
| 211 | |
| 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(); | |
| 217 | |
| 218 return int_array; | |
| 219 } | |
| 220 | |
| 221 std::vector<int> TextInputController::FirstRectForCharacterRange( | |
| 222 unsigned location, | |
| 223 unsigned length) { | |
| 224 blink::WebRect rect; | |
| 225 if (!view_->mainFrame()->firstRectForCharacterRange(location, length, rect)) | |
|
jochen (gone - plz use gerrit)
2014/02/18 09:13:39
this should be focusedFrame()
tfarina
2014/02/18 12:50:42
Done.
| |
| 226 return std::vector<int>(); | |
| 227 | |
| 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; | |
| 233 | |
| 234 return int_array; | |
| 235 } | |
| 236 | |
| 237 void TextInputController::SetComposition(const std::string& text) { | |
| 238 // Sends a keydown event with key code = 0xE5 to emulate input method | |
| 239 // behavior. | |
| 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); | |
| 246 | |
| 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()); | |
| 250 } | |
| 251 | |
| 252 } // namespace content | |
| OLD | NEW |