| Index: content/shell/renderer/test_runner/text_input_controller.cc
|
| diff --git a/content/shell/renderer/test_runner/text_input_controller.cc b/content/shell/renderer/test_runner/text_input_controller.cc
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..78419e7f88a136f2e6deab34473bca9cff723259
|
| --- /dev/null
|
| +++ b/content/shell/renderer/test_runner/text_input_controller.cc
|
| @@ -0,0 +1,249 @@
|
| +// Copyright 2014 The Chromium Authors. All rights reserved.
|
| +// Use of this source code is governed by a BSD-style license that can be
|
| +// found in the LICENSE file.
|
| +
|
| +#include "content/shell/renderer/test_runner/text_input_controller.h"
|
| +
|
| +#include "gin/arguments.h"
|
| +#include "gin/handle.h"
|
| +#include "gin/object_template_builder.h"
|
| +#include "gin/wrappable.h"
|
| +#include "third_party/WebKit/public/web/WebBindings.h"
|
| +#include "third_party/WebKit/public/web/WebCompositionUnderline.h"
|
| +#include "third_party/WebKit/public/web/WebFrame.h"
|
| +#include "third_party/WebKit/public/web/WebInputEvent.h"
|
| +#include "third_party/WebKit/public/web/WebKit.h"
|
| +#include "third_party/WebKit/public/web/WebRange.h"
|
| +#include "third_party/WebKit/public/web/WebView.h"
|
| +#include "v8/include/v8.h"
|
| +
|
| +namespace content {
|
| +
|
| +class TextInputControllerBindings
|
| + : public gin::Wrappable<TextInputControllerBindings> {
|
| + public:
|
| + static gin::WrapperInfo kWrapperInfo;
|
| +
|
| + static void Install(base::WeakPtr<TextInputController> controller,
|
| + blink::WebFrame* frame);
|
| +
|
| + private:
|
| + explicit TextInputControllerBindings(
|
| + base::WeakPtr<TextInputController> controller);
|
| + virtual ~TextInputControllerBindings();
|
| +
|
| + // gin::Wrappable:
|
| + virtual gin::ObjectTemplateBuilder GetObjectTemplateBuilder(
|
| + v8::Isolate* isolate) OVERRIDE;
|
| +
|
| + void InsertText(const std::string& text);
|
| + void UnmarkText();
|
| + void DoCommand(const std::string& text);
|
| + void SetMarkedText(const std::string& text);
|
| + bool HasMarkedText();
|
| + NPObject* MarkedRange();
|
| + NPObject* SelectedRange();
|
| + NPObject* FirstRectForCharacterRange(const std::string& text);
|
| + void SetComposition(const std::string& text);
|
| +
|
| + base::WeakPtr<TextInputController> controller_;
|
| +
|
| + DISALLOW_COPY_AND_ASSIGN(TextInputControllerBindings);
|
| +};
|
| +
|
| +// static
|
| +gin::WrapperInfo TextInputControllerBindings::kWrapperInfo = {
|
| + gin::kEmbedderNativeGin
|
| +};
|
| +
|
| +// static
|
| +void TextInputControllerBindings::Install(
|
| + base::WeakPtr<TextInputController> controller,
|
| + blink::WebFrame* frame) {
|
| + v8::Isolate* isolate = blink::mainThreadIsolate();
|
| + v8::HandleScope handle_scope(isolate);
|
| + v8::Handle<v8::Context> context = frame->mainWorldScriptContext();
|
| + if (context.IsEmpty())
|
| + return;
|
| +
|
| + v8::Context::Scope context_scope(context);
|
| +
|
| + gin::Handle<TextInputControllerBindings> bindings =
|
| + gin::CreateHandle(isolate, new TextInputControllerBindings(controller));
|
| + v8::Handle<v8::Object> global = context->Global();
|
| + global->Set(gin::StringToV8(isolate, "textInputController"), bindings.ToV8());
|
| +}
|
| +
|
| +TextInputControllerBindings::TextInputControllerBindings(
|
| + base::WeakPtr<TextInputController> controller)
|
| + : controller_(controller) {}
|
| +
|
| +TextInputControllerBindings::~TextInputControllerBindings() {}
|
| +
|
| +gin::ObjectTemplateBuilder
|
| +TextInputControllerBindings::GetObjectTemplateBuilder(v8::Isolate* isolate) {
|
| + return gin::Wrappable<TextInputControllerBindings>::GetObjectTemplateBuilder(isolate)
|
| + .SetMethod("insertText", &TextInputControllerBindings::InsertText)
|
| + .SetMethod("unmarkText", &TextInputControllerBindings::UnmarkText)
|
| + .SetMethod("doCommand", &TextInputControllerBindings::DoCommand)
|
| + .SetMethod("setMarkedText", &TextInputControllerBindings::SetMarkedText)
|
| + .SetMethod("hasMarkedText", &TextInputControllerBindings::HasMarkedText)
|
| + .SetMethod("markedRange", &TextInputControllerBindings::MarkedRange)
|
| + .SetMethod("selectedRange", &TextInputControllerBindings::SelectedRange)
|
| + .SetMethod("firstRectForCharacterRange",
|
| + &TextInputControllerBindings::FirstRectForCharacterRange)
|
| + .SetMethod("setComposition",
|
| + &TextInputControllerBindings::SetComposition);
|
| +}
|
| +
|
| +void TextInputControllerBindings::InsertText(const std::string& text) {
|
| + if (controller_)
|
| + controller_->InsertText(text);
|
| +}
|
| +
|
| +void TextInputControllerBindings::UnmarkText() {
|
| + if (controller_)
|
| + controller_->UnmarkText();
|
| +}
|
| +
|
| +void TextInputControllerBindings::DoCommand(const std::string& text) {
|
| + if (controller_)
|
| + controller_->DoCommand(text);
|
| +}
|
| +
|
| +void TextInputControllerBindings::SetMarkedText(const std::string& text) {
|
| + if (controller_)
|
| + controller_->SetMarkedText(text);
|
| +}
|
| +
|
| +bool TextInputControllerBindings::HasMarkedText() {
|
| + return controller_ ? controller_->HasMarkedText() : false;
|
| +}
|
| +
|
| +NPObject* TextInputControllerBindings::MarkedRange() {
|
| + return controller_ ? controller_->MarkedRange() : NULL;
|
| +}
|
| +
|
| +NPObject* TextInputControllerBindings::SelectedRange() {
|
| + return controller_ ? controller_->SelectedRange() : NULL;
|
| +}
|
| +
|
| +NPObject* TextInputControllerBindings::FirstRectForCharacterRange(
|
| + const std::string& text) {
|
| + return controller_ ? controller_->FirstRectForCharacterRange(text) : NULL;
|
| +}
|
| +
|
| +void TextInputControllerBindings::SetComposition(const std::string& text) {
|
| + if (controller_)
|
| + controller_->SetComposition(text);
|
| +}
|
| +
|
| +// TextInputController ---------------------------------------------------------
|
| +
|
| +TextInputController::TextInputController()
|
| + : frame_(NULL), weak_factory_(this) {}
|
| +
|
| +TextInputController::~TextInputController() {}
|
| +
|
| +void TextInputController::Install(blink::WebFrame* frame) {
|
| + frame_ = frame;
|
| +
|
| + TextInputControllerBindings::Install(weak_factory_.GetWeakPtr(), frame);
|
| +}
|
| +
|
| +void TextInputController::InsertText(const std::string& text) {
|
| + frame_->view()->confirmComposition(blink::WebString::fromUTF8(text));
|
| +}
|
| +
|
| +void TextInputController::UnmarkText() {
|
| + frame_->view()->confirmComposition();
|
| +}
|
| +
|
| +void TextInputController::DoCommand(const std::string& text) {
|
| + frame_->executeCommand(blink::WebString::fromUTF8(text));
|
| +}
|
| +
|
| +void TextInputController::SetMarkedText(const std::string& text) {
|
| + /*WebString web_text(WebString::fromUTF8(text));
|
| + int start = text[1].toInt32();
|
| + int length = text[2].toInt32();
|
| +
|
| + // Split underline into up to 3 elements (before, selection, and after).
|
| + std::vector<WebCompositionUnderline> underlines;
|
| + WebCompositionUnderline underline;
|
| + if (!start) {
|
| + underline.endOffset = length;
|
| + } else {
|
| + underline.endOffset = start;
|
| + underlines.push_back(underline);
|
| + underline.startOffset = start;
|
| + underline.endOffset = start + length;
|
| + }
|
| + underline.thick = true;
|
| + underlines.push_back(underline);
|
| + if (start + length < static_cast<int>(text.length())) {
|
| + underline.startOffset = underline.endOffset;
|
| + underline.endOffset = text.length();
|
| + underline.thick = false;
|
| + underlines.push_back(underline);
|
| + }
|
| +
|
| + frame_->view()->setComposition(web_text, underlines, start, start + length);
|
| + */
|
| +}
|
| +
|
| +bool TextInputController::HasMarkedText() {
|
| + return frame_->hasMarkedText();
|
| +}
|
| +
|
| +NPObject* TextInputController::MarkedRange() {
|
| + blink::WebRange range = frame_->markedRange();
|
| + std::vector<int> int_array(2);
|
| + int_array[0] = range.startOffset();
|
| + int_array[1] = range.endOffset();
|
| +
|
| + return blink::WebBindings::makeIntArray(int_array);
|
| +}
|
| +
|
| +NPObject* TextInputController::SelectedRange() {
|
| + blink::WebRange range = frame_->selectionRange();
|
| + std::vector<int> int_array(2);
|
| + int_array[0] = range.startOffset();
|
| + int_array[1] = range.endOffset();
|
| +
|
| + return blink::WebBindings::makeIntArray(int_array);
|
| +}
|
| +
|
| +NPObject* TextInputController::FirstRectForCharacterRange(
|
| + const std::string& text) {
|
| + blink::WebRect rect;
|
| + /* if (!frame_->firstRectForCharacterRange(
|
| + arguments[0].toInt32(), arguments[1].toInt32(), rect))
|
| + return;
|
| +
|
| + */
|
| + std::vector<int> int_array(4);
|
| + int_array[0] = rect.x;
|
| + int_array[1] = rect.y;
|
| + int_array[2] = rect.width;
|
| + int_array[3] = rect.height;
|
| +
|
| + return blink::WebBindings::makeIntArray(int_array);
|
| +}
|
| +
|
| +void TextInputController::SetComposition(const std::string& text) {
|
| + // Sends a keydown event with key code = 0xE5 to emulate input method
|
| + // behavior.
|
| + blink::WebKeyboardEvent key_down;
|
| + key_down.type = blink::WebInputEvent::RawKeyDown;
|
| + key_down.modifiers = 0;
|
| + key_down.windowsKeyCode = 0xE5; // VKEY_PROCESSKEY
|
| + key_down.setKeyIdentifierFromWindowsKeyCode();
|
| + frame_->view()->handleInputEvent(key_down);
|
| +
|
| + blink::WebVector<blink::WebCompositionUnderline> underlines;
|
| + blink::WebString web_text(blink::WebString::fromUTF8(text));
|
| + frame_->view()->setComposition(web_text, underlines, 0, web_text.length());
|
| +}
|
| +
|
| +} // namespace content
|
|
|