From f751653a94ab3b2a187306c1d9c9af004db1e7d8 Mon Sep 17 00:00:00 2001 From: "tfarina@chromium.org" Date: Tue, 18 Feb 2014 16:32:55 +0000 Subject: [PATCH] Reland "Move TextInputController from CppBoundClass to gin::Wrappable." Also, change the class to use chromium coding style. BUG=297480, 331301 R=jochen@chromium.org Review URL: https://codereview.chromium.org/169683006 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@251773 0039d316-1c4b-4281-b951-d872f2087c98 --- content/content_shell.gypi | 4 +- .../renderer/test_runner/TestInterfaces.cpp | 10 +- .../renderer/test_runner/TestInterfaces.h | 4 +- .../test_runner/TextInputController.cpp | 193 -------------- .../test_runner/TextInputController.h | 42 --- .../test_runner/text_input_controller.cc | 252 ++++++++++++++++++ .../test_runner/text_input_controller.h | 56 ++++ 7 files changed, 317 insertions(+), 244 deletions(-) delete mode 100644 content/shell/renderer/test_runner/TextInputController.cpp delete mode 100644 content/shell/renderer/test_runner/TextInputController.h create mode 100644 content/shell/renderer/test_runner/text_input_controller.cc create mode 100644 content/shell/renderer/test_runner/text_input_controller.h diff --git a/content/content_shell.gypi b/content/content_shell.gypi index e27b028c8754..9a9537f5b283 100644 --- a/content/content_shell.gypi +++ b/content/content_shell.gypi @@ -220,8 +220,6 @@ 'shell/renderer/test_runner/TestPlugin.h', 'shell/renderer/test_runner/TestRunner.cpp', 'shell/renderer/test_runner/TestRunner.h', - 'shell/renderer/test_runner/TextInputController.cpp', - 'shell/renderer/test_runner/TextInputController.h', 'shell/renderer/test_runner/WebAXObjectProxy.cpp', 'shell/renderer/test_runner/WebAXObjectProxy.h', 'shell/renderer/test_runner/WebFrameTestProxy.h', @@ -243,6 +241,8 @@ 'shell/renderer/test_runner/WebUserMediaClientMock.h', 'shell/renderer/test_runner/gamepad_controller.cc', 'shell/renderer/test_runner/gamepad_controller.h', + 'shell/renderer/test_runner/text_input_controller.cc', + 'shell/renderer/test_runner/text_input_controller.h', 'shell/renderer/webkit_test_runner.cc', 'shell/renderer/webkit_test_runner.h', 'test/layouttest_support.cc', diff --git a/content/shell/renderer/test_runner/TestInterfaces.cpp b/content/shell/renderer/test_runner/TestInterfaces.cpp index 7981f36a0011..426f652b7fd5 100644 --- a/content/shell/renderer/test_runner/TestInterfaces.cpp +++ b/content/shell/renderer/test_runner/TestInterfaces.cpp @@ -10,9 +10,9 @@ #include "content/shell/renderer/test_runner/AccessibilityController.h" #include "content/shell/renderer/test_runner/EventSender.h" #include "content/shell/renderer/test_runner/TestRunner.h" -#include "content/shell/renderer/test_runner/TextInputController.h" #include "content/shell/renderer/test_runner/WebTestProxy.h" #include "content/shell/renderer/test_runner/gamepad_controller.h" +#include "content/shell/renderer/test_runner/text_input_controller.h" #include "third_party/WebKit/public/platform/WebString.h" #include "third_party/WebKit/public/platform/WebURL.h" #include "third_party/WebKit/public/web/WebCache.h" @@ -29,7 +29,7 @@ TestInterfaces::TestInterfaces() : m_accessibilityController(new AccessibilityController()) , m_eventSender(new EventSender(this)) , m_gamepadController(new content::GamepadController()) - , m_textInputController(new TextInputController()) + , m_textInputController(new content::TextInputController()) , m_testRunner(new TestRunner(this)) , m_delegate(0) { @@ -46,7 +46,7 @@ TestInterfaces::~TestInterfaces() m_accessibilityController->setWebView(0); m_eventSender->setWebView(0); // m_gamepadController doesn't depend on WebView. - m_textInputController->setWebView(0); + m_textInputController->SetWebView(NULL); m_testRunner->setWebView(0, 0); m_accessibilityController->setDelegate(0); @@ -62,7 +62,7 @@ void TestInterfaces::setWebView(WebView* webView, WebTestProxyBase* proxy) m_accessibilityController->setWebView(webView); m_eventSender->setWebView(webView); // m_gamepadController doesn't depend on WebView. - m_textInputController->setWebView(webView); + m_textInputController->SetWebView(webView); m_testRunner->setWebView(webView, proxy); } @@ -81,7 +81,7 @@ void TestInterfaces::bindTo(WebFrame* frame) m_accessibilityController->bindToJavascript(frame, WebString::fromUTF8("accessibilityController")); m_eventSender->bindToJavascript(frame, WebString::fromUTF8("eventSender")); m_gamepadController->Install(frame); - m_textInputController->bindToJavascript(frame, WebString::fromUTF8("textInputController")); + m_textInputController->Install(frame); m_testRunner->bindToJavascript(frame, WebString::fromUTF8("testRunner")); m_testRunner->bindToJavascript(frame, WebString::fromUTF8("layoutTestController")); } diff --git a/content/shell/renderer/test_runner/TestInterfaces.h b/content/shell/renderer/test_runner/TestInterfaces.h index 5e6ed26f7517..69e490c81d51 100644 --- a/content/shell/renderer/test_runner/TestInterfaces.h +++ b/content/shell/renderer/test_runner/TestInterfaces.h @@ -26,6 +26,7 @@ class WebView; namespace content { class GamepadController; +class TextInputController; } namespace WebTestRunner { @@ -33,7 +34,6 @@ namespace WebTestRunner { class AccessibilityController; class EventSender; class TestRunner; -class TextInputController; class WebTestDelegate; class WebTestProxyBase; @@ -65,7 +65,7 @@ class TestInterfaces { scoped_ptr m_accessibilityController; scoped_ptr m_eventSender; scoped_ptr m_gamepadController; - scoped_ptr m_textInputController; + scoped_ptr m_textInputController; scoped_ptr m_testRunner; WebTestDelegate* m_delegate; WebTestProxyBase* m_proxy; diff --git a/content/shell/renderer/test_runner/TextInputController.cpp b/content/shell/renderer/test_runner/TextInputController.cpp deleted file mode 100644 index b90d7b058a1b..000000000000 --- a/content/shell/renderer/test_runner/TextInputController.cpp +++ /dev/null @@ -1,193 +0,0 @@ -// 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/TextInputController.h" - -#include -#include "content/shell/renderer/test_runner/TestCommon.h" -#include "third_party/WebKit/public/platform/WebString.h" -#include "third_party/WebKit/public/platform/WebVector.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/WebRange.h" -#include "third_party/WebKit/public/web/WebView.h" - -using namespace blink; -using namespace std; - -namespace WebTestRunner { - -TextInputController::TextInputController() -{ - bindMethod("doCommand", &TextInputController::doCommand); - bindMethod("firstRectForCharacterRange", &TextInputController::firstRectForCharacterRange); - bindMethod("hasMarkedText", &TextInputController::hasMarkedText); - bindMethod("insertText", &TextInputController::insertText); - bindMethod("markedRange", &TextInputController::markedRange); - bindMethod("selectedRange", &TextInputController::selectedRange); - bindMethod("setMarkedText", &TextInputController::setMarkedText); - bindMethod("unmarkText", &TextInputController::unmarkText); - bindMethod("setComposition", &TextInputController::setComposition); -} - -void TextInputController::insertText(const CppArgumentList& arguments, CppVariant* result) -{ - result->setNull(); - - if (arguments.size() < 1 || !arguments[0].isString()) - return; - - m_webView->confirmComposition(WebString::fromUTF8(arguments[0].toString())); -} - -void TextInputController::doCommand(const CppArgumentList& arguments, CppVariant* result) -{ - result->setNull(); - - WebFrame* mainFrame = m_webView->mainFrame(); - if (!mainFrame) - return; - - if (arguments.size() >= 1 && arguments[0].isString()) - mainFrame->executeCommand(WebString::fromUTF8(arguments[0].toString())); -} - -void TextInputController::setMarkedText(const CppArgumentList& arguments, CppVariant* result) -{ - result->setNull(); - - if (arguments.size() < 3 || !arguments[0].isString() - || !arguments[1].isNumber() || !arguments[2].isNumber()) - return; - - WebString text(WebString::fromUTF8(arguments[0].toString())); - int start = arguments[1].toInt32(); - int length = arguments[2].toInt32(); - - // Split underline into up to 3 elements (before, selection, and after). - vector 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(text.length())) { - underline.startOffset = underline.endOffset; - underline.endOffset = text.length(); - underline.thick = false; - underlines.push_back(underline); - } - - m_webView->setComposition(text, underlines, start, start + length); -} - -void TextInputController::unmarkText(const CppArgumentList&, CppVariant* result) -{ - result->setNull(); - - m_webView->confirmComposition(); -} - -void TextInputController::hasMarkedText(const CppArgumentList&, CppVariant* result) -{ - result->setNull(); - - WebFrame* mainFrame = m_webView->mainFrame(); - if (!mainFrame) - return; - - result->set(mainFrame->hasMarkedText()); -} - -void TextInputController::markedRange(const CppArgumentList&, CppVariant* result) -{ - result->setNull(); - - WebFrame* mainFrame = m_webView->mainFrame(); - if (!mainFrame) - return; - - WebRange range = mainFrame->markedRange(); - vector intArray(2); - intArray[0] = range.startOffset(); - intArray[1] = range.endOffset(); - - NPObject* resultArray = WebBindings::makeIntArray(intArray); - result->set(resultArray); - WebBindings::releaseObject(resultArray); -} - -void TextInputController::selectedRange(const CppArgumentList&, CppVariant* result) -{ - result->setNull(); - - WebFrame* mainFrame = m_webView->mainFrame(); - if (!mainFrame) - return; - - WebRange range = mainFrame->selectionRange(); - vector intArray(2); - intArray[0] = range.startOffset(); - intArray[1] = range.endOffset(); - - NPObject* resultArray = WebBindings::makeIntArray(intArray); - result->set(resultArray); - WebBindings::releaseObject(resultArray); -} - -void TextInputController::firstRectForCharacterRange(const CppArgumentList& arguments, CppVariant* result) -{ - result->setNull(); - - WebFrame* frame = m_webView->focusedFrame(); - if (!frame) - return; - - if (arguments.size() < 2 || !arguments[0].isNumber() || !arguments[1].isNumber()) - return; - - WebRect rect; - if (!frame->firstRectForCharacterRange(arguments[0].toInt32(), arguments[1].toInt32(), rect)) - return; - - vector intArray(4); - intArray[0] = rect.x; - intArray[1] = rect.y; - intArray[2] = rect.width; - intArray[3] = rect.height; - - NPObject* resultArray = WebBindings::makeIntArray(intArray); - result->set(resultArray); - WebBindings::releaseObject(resultArray); -} - -void TextInputController::setComposition(const CppArgumentList& arguments, CppVariant* result) -{ - result->setNull(); - - if (arguments.size() < 1) - return; - - // Sends a keydown event with key code = 0xE5 to emulate input method behavior. - WebKeyboardEvent keyDown; - keyDown.type = WebInputEvent::RawKeyDown; - keyDown.modifiers = 0; - keyDown.windowsKeyCode = 0xE5; // VKEY_PROCESSKEY - keyDown.setKeyIdentifierFromWindowsKeyCode(); - m_webView->handleInputEvent(keyDown); - - WebVector underlines; - WebString text(WebString::fromUTF8(arguments[0].toString())); - m_webView->setComposition(text, underlines, 0, text.length()); -} - -} diff --git a/content/shell/renderer/test_runner/TextInputController.h b/content/shell/renderer/test_runner/TextInputController.h deleted file mode 100644 index d94f69abf904..000000000000 --- a/content/shell/renderer/test_runner/TextInputController.h +++ /dev/null @@ -1,42 +0,0 @@ -// 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. - -// TextInputController is bound to window.textInputController in Javascript -// when DRT is running. Layout tests use it to exercise various corners of -// text input. - -#ifndef CONTENT_SHELL_RENDERER_TEST_RUNNER_TEXTINPUTCONTROLLER_H_ -#define CONTENT_SHELL_RENDERER_TEST_RUNNER_TEXTINPUTCONTROLLER_H_ - -#include "content/shell/renderer/test_runner/CppBoundClass.h" - -namespace blink { -class WebView; -} - -namespace WebTestRunner { - -class TextInputController : public CppBoundClass { -public: - TextInputController(); - - void setWebView(blink::WebView* webView) { m_webView = webView; } - - void insertText(const CppArgumentList&, CppVariant*); - void doCommand(const CppArgumentList&, CppVariant*); - void setMarkedText(const CppArgumentList&, CppVariant*); - void unmarkText(const CppArgumentList&, CppVariant*); - void hasMarkedText(const CppArgumentList&, CppVariant*); - void markedRange(const CppArgumentList&, CppVariant*); - void selectedRange(const CppArgumentList&, CppVariant*); - void firstRectForCharacterRange(const CppArgumentList&, CppVariant*); - void setComposition(const CppArgumentList&, CppVariant*); - -private: - blink::WebView* m_webView; -}; - -} - -#endif // CONTENT_SHELL_RENDERER_TEST_RUNNER_TEXTINPUTCONTROLLER_H_ 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 000000000000..e9d244fed5df --- /dev/null +++ b/content/shell/renderer/test_runner/text_input_controller.cc @@ -0,0 +1,252 @@ +// 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/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 { + public: + static gin::WrapperInfo kWrapperInfo; + + static void Install(base::WeakPtr controller, + blink::WebFrame* frame); + + private: + explicit TextInputControllerBindings( + base::WeakPtr 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, int start, int length); + bool HasMarkedText(); + std::vector MarkedRange(); + std::vector SelectedRange(); + std::vector FirstRectForCharacterRange(unsigned location, + unsigned length); + void SetComposition(const std::string& text); + + base::WeakPtr controller_; + + DISALLOW_COPY_AND_ASSIGN(TextInputControllerBindings); +}; + +gin::WrapperInfo TextInputControllerBindings::kWrapperInfo = { + gin::kEmbedderNativeGin}; + +// static +void TextInputControllerBindings::Install( + base::WeakPtr controller, + blink::WebFrame* frame) { + v8::Isolate* isolate = blink::mainThreadIsolate(); + v8::HandleScope handle_scope(isolate); + v8::Handle context = frame->mainWorldScriptContext(); + if (context.IsEmpty()) + return; + + v8::Context::Scope context_scope(context); + + gin::Handle bindings = + gin::CreateHandle(isolate, new TextInputControllerBindings(controller)); + v8::Handle global = context->Global(); + global->Set(gin::StringToV8(isolate, "textInputController"), bindings.ToV8()); +} + +TextInputControllerBindings::TextInputControllerBindings( + base::WeakPtr controller) + : controller_(controller) {} + +TextInputControllerBindings::~TextInputControllerBindings() {} + +gin::ObjectTemplateBuilder +TextInputControllerBindings::GetObjectTemplateBuilder(v8::Isolate* isolate) { + return gin::Wrappable::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, + int start, + int length) { + if (controller_) + controller_->SetMarkedText(text, start, length); +} + +bool TextInputControllerBindings::HasMarkedText() { + return controller_ ? controller_->HasMarkedText() : false; +} + +std::vector TextInputControllerBindings::MarkedRange() { + return controller_ ? controller_->MarkedRange() : std::vector(); +} + +std::vector TextInputControllerBindings::SelectedRange() { + return controller_ ? controller_->SelectedRange() : std::vector(); +} + +std::vector TextInputControllerBindings::FirstRectForCharacterRange( + unsigned location, + unsigned length) { + return controller_ ? controller_->FirstRectForCharacterRange(location, length) + : std::vector(); +} + +void TextInputControllerBindings::SetComposition(const std::string& text) { + if (controller_) + controller_->SetComposition(text); +} + +// TextInputController --------------------------------------------------------- + +TextInputController::TextInputController() + : view_(NULL), weak_factory_(this) {} + +TextInputController::~TextInputController() {} + +void TextInputController::Install(blink::WebFrame* frame) { + TextInputControllerBindings::Install(weak_factory_.GetWeakPtr(), frame); +} + +void TextInputController::SetWebView(blink::WebView* view) { + view_ = view; +} + +void TextInputController::InsertText(const std::string& text) { + view_->confirmComposition(blink::WebString::fromUTF8(text)); +} + +void TextInputController::UnmarkText() { + view_->confirmComposition(); +} + +void TextInputController::DoCommand(const std::string& text) { + view_->mainFrame()->executeCommand(blink::WebString::fromUTF8(text)); +} + +void TextInputController::SetMarkedText(const std::string& text, + int start, + int length) { + blink::WebString web_text(blink::WebString::fromUTF8(text)); + + // Split underline into up to 3 elements (before, selection, and after). + std::vector underlines; + blink::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(web_text.length())) { + underline.startOffset = underline.endOffset; + underline.endOffset = web_text.length(); + underline.thick = false; + underlines.push_back(underline); + } + + view_->setComposition(web_text, underlines, start, start + length); +} + +bool TextInputController::HasMarkedText() { + return view_->mainFrame()->hasMarkedText(); +} + +std::vector TextInputController::MarkedRange() { + blink::WebRange range = view_->mainFrame()->markedRange(); + std::vector int_array(2); + int_array[0] = range.startOffset(); + int_array[1] = range.endOffset(); + + return int_array; +} + +std::vector TextInputController::SelectedRange() { + blink::WebRange range = view_->mainFrame()->selectionRange(); + std::vector int_array(2); + int_array[0] = range.startOffset(); + int_array[1] = range.endOffset(); + + return int_array; +} + +std::vector TextInputController::FirstRectForCharacterRange( + unsigned location, + unsigned length) { + blink::WebRect rect; + if (!view_->focusedFrame()->firstRectForCharacterRange(location, length, rect)) + return std::vector(); + + std::vector int_array(4); + int_array[0] = rect.x; + int_array[1] = rect.y; + int_array[2] = rect.width; + int_array[3] = rect.height; + + return 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(); + view_->handleInputEvent(key_down); + + blink::WebVector underlines; + blink::WebString web_text(blink::WebString::fromUTF8(text)); + view_->setComposition(web_text, underlines, 0, web_text.length()); +} + +} // namespace content diff --git a/content/shell/renderer/test_runner/text_input_controller.h b/content/shell/renderer/test_runner/text_input_controller.h new file mode 100644 index 000000000000..7a3706c255d5 --- /dev/null +++ b/content/shell/renderer/test_runner/text_input_controller.h @@ -0,0 +1,56 @@ +// 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. + +#ifndef CONTENT_SHELL_RENDERER_TEST_RUNNER_TEXTINPUTCONTROLLER_H_ +#define CONTENT_SHELL_RENDERER_TEST_RUNNER_TEXTINPUTCONTROLLER_H_ + +#include +#include + +#include "base/macros.h" +#include "base/memory/weak_ptr.h" + +namespace blink { +class WebFrame; +class WebView; +} + +namespace content { + +// TextInputController is bound to window.textInputController in Javascript +// when content_shell is running. Layout tests use it to exercise various +// corners of text input. +class TextInputController : public base::SupportsWeakPtr { + public: + TextInputController(); + ~TextInputController(); + + void Install(blink::WebFrame* frame); + + void SetWebView(blink::WebView* view); + + private: + friend class TextInputControllerBindings; + + void InsertText(const std::string& text); + void UnmarkText(); + void DoCommand(const std::string& text); + void SetMarkedText(const std::string& text, int start, int length); + bool HasMarkedText(); + std::vector MarkedRange(); + std::vector SelectedRange(); + std::vector FirstRectForCharacterRange(unsigned location, + unsigned length); + void SetComposition(const std::string& text); + + blink::WebView* view_; + + base::WeakPtrFactory weak_factory_; + + DISALLOW_COPY_AND_ASSIGN(TextInputController); +}; + +} // namespace content + +#endif // CONTENT_SHELL_RENDERER_TEST_RUNNER_TEXTINPUTCONTROLLER_H_