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

Unified Diff: content/browser/devtools/protocol/input_handler.cc

Issue 1054253004: [DevTools] Input domain implementation in browser. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: rebased Created 5 years, 8 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: content/browser/devtools/protocol/input_handler.cc
diff --git a/content/browser/devtools/protocol/input_handler.cc b/content/browser/devtools/protocol/input_handler.cc
index b8d6fbd1d18c4797a8150534232f2f7a513a9193..77b32b9147c05a68dc43e24a19ddb3553ad444e5 100644
--- a/content/browser/devtools/protocol/input_handler.cc
+++ b/content/browser/devtools/protocol/input_handler.cc
@@ -5,8 +5,10 @@
#include "content/browser/devtools/protocol/input_handler.h"
#include "base/strings/stringprintf.h"
+#include "base/strings/utf_string_conversions.h"
#include "content/browser/renderer_host/render_view_host_impl.h"
#include "third_party/WebKit/public/web/WebInputEvent.h"
+#include "ui/events/keycodes/dom4/keycode_converter.h"
namespace content {
namespace devtools {
@@ -14,6 +16,76 @@ namespace input {
typedef DevToolsProtocolClient::Response Response;
+namespace {
+
+void SetEventModifiers(blink::WebInputEvent* event, const int* modifiers) {
+ if (!modifiers)
+ return;
+ if (*modifiers & 1)
+ event->modifiers |= blink::WebInputEvent::AltKey;
+ if (*modifiers & 2)
+ event->modifiers |= blink::WebInputEvent::ControlKey;
+ if (*modifiers & 4)
+ event->modifiers |= blink::WebInputEvent::MetaKey;
+ if (*modifiers & 8)
+ event->modifiers |= blink::WebInputEvent::ShiftKey;
+}
+
+void SetEventTimestamp(blink::WebInputEvent* event, const double* timestamp) {
+ event->timeStampSeconds =
+ timestamp ? *timestamp : base::Time::Now().ToDoubleT();
+}
+
+bool SetKeyboardEventText(blink::WebUChar* to, const std::string* from) {
+ if (!from)
+ return true;
+
+ base::string16 text16 = base::UTF8ToUTF16(*from);
+ if (text16.size() > blink::WebKeyboardEvent::textLengthCap)
+ return false;
+
+ for (size_t i = 0; i < text16.size(); ++i)
+ to[i] = text16[i];
+ return true;
+}
+
+bool SetMouseEventButton(blink::WebMouseEvent* event,
+ const std::string* button) {
+ if (!button)
+ return true;
+
+ if (*button == dispatch_mouse_event::kButtonNone) {
+ event->button = blink::WebMouseEvent::ButtonNone;
+ } else if (*button == dispatch_mouse_event::kButtonLeft) {
+ event->button = blink::WebMouseEvent::ButtonLeft;
+ event->modifiers |= blink::WebInputEvent::LeftButtonDown;
+ } else if (*button == dispatch_mouse_event::kButtonMiddle) {
+ event->button = blink::WebMouseEvent::ButtonMiddle;
+ event->modifiers |= blink::WebInputEvent::MiddleButtonDown;
+ } else if (*button == dispatch_mouse_event::kButtonRight) {
+ event->button = blink::WebMouseEvent::ButtonRight;
+ event->modifiers |= blink::WebInputEvent::RightButtonDown;
+ } else {
+ return false;
+ }
+ return true;
+}
+
+bool SetMouseEventType(blink::WebMouseEvent* event, const std::string& type) {
+ if (type == dispatch_mouse_event::kTypeMousePressed) {
+ event->type = blink::WebInputEvent::MouseDown;
+ } else if (type == dispatch_mouse_event::kTypeMouseReleased) {
+ event->type = blink::WebInputEvent::MouseUp;
+ } else if (type == dispatch_mouse_event::kTypeMouseMoved) {
+ event->type = blink::WebInputEvent::MouseMove;
+ } else {
+ return false;
+ }
+ return true;
+}
+
+} // namespace
+
InputHandler::InputHandler()
: host_(NULL) {
}
@@ -41,7 +113,60 @@ Response InputHandler::DispatchKeyEvent(
const bool* auto_repeat,
const bool* is_keypad,
const bool* is_system_key) {
- return Response::FallThrough();
+ NativeWebKeyboardEvent event;
+
+ if (type == dispatch_key_event::kTypeKeyDown) {
+ event.type = blink::WebInputEvent::KeyDown;
+ } else if (type == dispatch_key_event::kTypeKeyUp) {
+ event.type = blink::WebInputEvent::KeyUp;
+ } else if (type == dispatch_key_event::kTypeChar) {
+ event.type = blink::WebInputEvent::Char;
+ } else if (type == dispatch_key_event::kTypeRawKeyDown) {
+ event.type = blink::WebInputEvent::RawKeyDown;
+ } else {
+ return Response::InvalidParams(
+ base::StringPrintf("Unexpected event type '%s'", type.c_str()));
+ }
+
+ SetEventModifiers(&event, modifiers);
+ SetEventTimestamp(&event, timestamp);
+ if (!SetKeyboardEventText(event.text, text))
+ return Response::InvalidParams("Invalid 'text' parameter");
+ if (!SetKeyboardEventText(event.unmodifiedText, unmodified_text))
+ return Response::InvalidParams("Invalid 'unmodifiedText' parameter");
+
+ if (key_identifier) {
+ if (key_identifier->size() >
+ blink::WebKeyboardEvent::keyIdentifierLengthCap) {
+ return Response::InvalidParams("Invalid 'keyIdentifier' parameter");
+ }
+ for (size_t i = 0; i < key_identifier->size(); ++i)
+ event.keyIdentifier[i] = (*key_identifier)[i];
+ }
+
+ if (code) {
+ event.domCode = static_cast<int>(
+ ui::KeycodeConverter::CodeStringToDomCode(code->c_str()));
+ }
+
+ if (windows_virtual_key_code)
+ event.windowsKeyCode = *windows_virtual_key_code;
+ if (native_virtual_key_code)
+ event.nativeKeyCode = *native_virtual_key_code;
+ if (auto_repeat && *auto_repeat)
+ event.modifiers |= blink::WebInputEvent::IsAutoRepeat;
+ if (is_keypad && *is_keypad)
+ event.modifiers |= blink::WebInputEvent::IsKeyPad;
+ if (is_system_key)
+ event.isSystemKey = *is_system_key;
+
+ if (!host_)
+ return Response::ServerError("Could not connect to view");
+
+ if (!host_->is_focused())
+ host_->Focus();
+ host_->ForwardKeyboardEvent(event);
+ return Response::OK();
}
Response InputHandler::DispatchMouseEvent(
@@ -52,7 +177,32 @@ Response InputHandler::DispatchMouseEvent(
const double* timestamp,
const std::string* button,
const int* click_count) {
- return Response::FallThrough();
+ blink::WebMouseEvent event;
+
+ if (!SetMouseEventType(&event, type)) {
+ return Response::InvalidParams(
+ base::StringPrintf("Unexpected event type '%s'", type.c_str()));
+ }
+ SetEventModifiers(&event, modifiers);
+ SetEventTimestamp(&event, timestamp);
+ if (!SetMouseEventButton(&event, button))
+ return Response::InvalidParams("Invalid mouse button");
+
+ event.x = x;
+ event.y = y;
+ event.windowX = x;
+ event.windowY = y;
+ event.globalX = x;
+ event.globalY = y;
+ event.clickCount = click_count ? *click_count : 0;
+
+ if (!host_)
+ return Response::ServerError("Could not connect to view");
+
+ if (!host_->is_focused())
+ host_->Focus();
+ host_->ForwardMouseEvent(event);
+ return Response::OK();
}
Response InputHandler::EmulateTouchFromMouseEvent(const std::string& type,
@@ -68,13 +218,7 @@ Response InputHandler::EmulateTouchFromMouseEvent(const std::string& type,
blink::WebMouseEvent mouse_event;
blink::WebMouseEvent* event = &mouse_event;
- if (type == emulate_touch_from_mouse_event::kTypeMousePressed) {
- event->type = blink::WebInputEvent::MouseDown;
- } else if (type == emulate_touch_from_mouse_event::kTypeMouseReleased) {
- event->type = blink::WebInputEvent::MouseUp;
- } else if (type == emulate_touch_from_mouse_event::kTypeMouseMoved) {
- event->type = blink::WebInputEvent::MouseMove;
- } else if (type == emulate_touch_from_mouse_event::kTypeMouseWheel) {
+ if (type == emulate_touch_from_mouse_event::kTypeMouseWheel) {
if (!delta_x || !delta_y) {
return Response::InvalidParams(
"'deltaX' and 'deltaY' are expected for mouseWheel event");
@@ -83,48 +227,23 @@ Response InputHandler::EmulateTouchFromMouseEvent(const std::string& type,
wheel_event.deltaY = static_cast<float>(*delta_y);
event = &wheel_event;
event->type = blink::WebInputEvent::MouseWheel;
- } else {
+ } else if (!SetMouseEventType(event, type)) {
return Response::InvalidParams(
base::StringPrintf("Unexpected event type '%s'", type.c_str()));
}
- if (modifiers) {
- if (*modifiers & 1)
- event->modifiers |= blink::WebInputEvent::AltKey;
- if (*modifiers & 2)
- event->modifiers |= blink::WebInputEvent::ControlKey;
- if (*modifiers & 4)
- event->modifiers |= blink::WebInputEvent::MetaKey;
- if (*modifiers & 8)
- event->modifiers |= blink::WebInputEvent::ShiftKey;
- }
+ SetEventModifiers(event, modifiers);
+ SetEventTimestamp(event, &timestamp);
+ if (!SetMouseEventButton(event, &button))
+ return Response::InvalidParams("Invalid mouse button");
- event->timeStampSeconds = timestamp;
event->x = x;
event->y = y;
event->windowX = x;
event->windowY = y;
event->globalX = x;
event->globalY = y;
-
- if (click_count)
- event->clickCount = *click_count;
-
- if (button == emulate_touch_from_mouse_event::kButtonNone) {
- event->button = blink::WebMouseEvent::ButtonNone;
- } else if (button == emulate_touch_from_mouse_event::kButtonLeft) {
- event->button = blink::WebMouseEvent::ButtonLeft;
- event->modifiers |= blink::WebInputEvent::LeftButtonDown;
- } else if (button == emulate_touch_from_mouse_event::kButtonMiddle) {
- event->button = blink::WebMouseEvent::ButtonMiddle;
- event->modifiers |= blink::WebInputEvent::MiddleButtonDown;
- } else if (button == emulate_touch_from_mouse_event::kButtonRight) {
- event->button = blink::WebMouseEvent::ButtonRight;
- event->modifiers |= blink::WebInputEvent::RightButtonDown;
- } else {
- return Response::InvalidParams(
- base::StringPrintf("Unexpected mouse button '%s'", button.c_str()));
- }
+ event->clickCount = click_count ? *click_count : 0;
if (!host_)
return Response::ServerError("Could not connect to view");
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698