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

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

Issue 2569273002: Add constructors to WebInputEvents and setters so we can work at cleaning up these public structs. (Closed)
Patch Set: Fix mouse up event sender not modifying modifiers Created 4 years 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
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 b963044209c287436628372f8df442313c35867a..451f29d5084392446001e56707cbdd2376f27b78 100644
--- a/content/browser/devtools/protocol/input_handler.cc
+++ b/content/browser/devtools/protocol/input_handler.cc
@@ -55,27 +55,39 @@ 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;
+int GetEventModifiers(const int* modifiers,
+ const bool* auto_repeat,
+ const bool* is_keypad) {
+ int result = 0;
+ if (auto_repeat && *auto_repeat)
+ result |= blink::WebInputEvent::IsAutoRepeat;
+ if (is_keypad && *is_keypad)
+ result |= blink::WebInputEvent::IsKeyPad;
+
+ if (modifiers) {
+ if (*modifiers & 1)
+ result |= blink::WebInputEvent::AltKey;
+ if (*modifiers & 2)
+ result |= blink::WebInputEvent::ControlKey;
+ if (*modifiers & 4)
+ result |= blink::WebInputEvent::MetaKey;
+ if (*modifiers & 8)
+ result |= blink::WebInputEvent::ShiftKey;
+ }
+ return result;
}
-void SetEventTimestamp(blink::WebInputEvent* event, const double* timestamp) {
+base::TimeTicks GetEventTimeTicks(const double* timestamp) {
// Convert timestamp, in seconds since unix epoch, to an event timestamp
// which is time ticks since platform start time.
- base::TimeTicks ticks = timestamp
- ? base::TimeDelta::FromSecondsD(*timestamp) +
- base::TimeTicks::UnixEpoch()
- : base::TimeTicks::Now();
- event->timeStampSeconds = (ticks - base::TimeTicks()).InSecondsF();
+ return timestamp
+ ? base::TimeDelta::FromSecondsD(*timestamp) +
+ base::TimeTicks::UnixEpoch()
+ : base::TimeTicks::Now();
+}
+
+double GetEventTimestamp(const double* timestamp) {
+ return (GetEventTimeTicks(timestamp) - base::TimeTicks()).InSecondsF();
}
bool SetKeyboardEventText(blink::WebUChar* to, const std::string* from) {
@@ -91,39 +103,37 @@ bool SetKeyboardEventText(blink::WebUChar* to, const std::string* from) {
return true;
}
-bool SetMouseEventButton(blink::WebMouseEvent* event,
- const std::string* button) {
+bool GetMouseEventButton(const std::string* button,
+ blink::WebPointerProperties::Button* event_button,
+ int* event_modifiers) {
if (!button)
return true;
if (*button == dispatch_mouse_event::kButtonNone) {
- event->button = blink::WebMouseEvent::Button::NoButton;
+ *event_button = blink::WebMouseEvent::Button::NoButton;
} else if (*button == dispatch_mouse_event::kButtonLeft) {
- event->button = blink::WebMouseEvent::Button::Left;
- event->modifiers |= blink::WebInputEvent::LeftButtonDown;
+ *event_button = blink::WebMouseEvent::Button::Left;
+ *event_modifiers = blink::WebInputEvent::LeftButtonDown;
} else if (*button == dispatch_mouse_event::kButtonMiddle) {
- event->button = blink::WebMouseEvent::Button::Middle;
- event->modifiers |= blink::WebInputEvent::MiddleButtonDown;
+ *event_button = blink::WebMouseEvent::Button::Middle;
+ *event_modifiers = blink::WebInputEvent::MiddleButtonDown;
} else if (*button == dispatch_mouse_event::kButtonRight) {
- event->button = blink::WebMouseEvent::Button::Right;
- event->modifiers |= blink::WebInputEvent::RightButtonDown;
+ *event_button = blink::WebMouseEvent::Button::Right;
+ *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;
+blink::WebInputEvent::Type GetMouseEventType(const std::string& type) {
+ if (type == dispatch_mouse_event::kTypeMousePressed)
+ return blink::WebInputEvent::MouseDown;
+ if (type == dispatch_mouse_event::kTypeMouseReleased)
+ return blink::WebInputEvent::MouseUp;
+ if (type == dispatch_mouse_event::kTypeMouseMoved)
+ return blink::WebInputEvent::MouseMove;
+ return blink::WebInputEvent::Undefined;
}
} // namespace
@@ -165,24 +175,24 @@ Response InputHandler::DispatchKeyEvent(
const bool* auto_repeat,
const bool* is_keypad,
const bool* is_system_key) {
- NativeWebKeyboardEvent event;
- event.skip_in_browser = true;
-
+ blink::WebInputEvent::Type web_event_type = blink::WebInputEvent::Undefined;
if (type == dispatch_key_event::kTypeKeyDown) {
- event.type = blink::WebInputEvent::KeyDown;
+ web_event_type = blink::WebInputEvent::KeyDown;
} else if (type == dispatch_key_event::kTypeKeyUp) {
- event.type = blink::WebInputEvent::KeyUp;
+ web_event_type = blink::WebInputEvent::KeyUp;
} else if (type == dispatch_key_event::kTypeChar) {
- event.type = blink::WebInputEvent::Char;
+ web_event_type = blink::WebInputEvent::Char;
} else if (type == dispatch_key_event::kTypeRawKeyDown) {
- event.type = blink::WebInputEvent::RawKeyDown;
+ web_event_type = blink::WebInputEvent::RawKeyDown;
} else {
return Response::InvalidParams(
base::StringPrintf("Unexpected event type '%s'", type.c_str()));
}
- SetEventModifiers(&event, modifiers);
- SetEventTimestamp(&event, timestamp);
+ NativeWebKeyboardEvent event(
+ web_event_type, GetEventModifiers(modifiers, auto_repeat, is_keypad),
+ GetEventTimeTicks(timestamp));
+ event.skip_in_browser = true;
if (!SetKeyboardEventText(event.text, text))
return Response::InvalidParams("Invalid 'text' parameter");
if (!SetKeyboardEventText(event.unmodifiedText, unmodified_text))
@@ -192,10 +202,6 @@ Response InputHandler::DispatchKeyEvent(
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;
@@ -225,17 +231,23 @@ Response InputHandler::DispatchMouseEvent(
const double* timestamp,
const std::string* button,
const int* click_count) {
- blink::WebMouseEvent event;
-
- if (!SetMouseEventType(&event, type)) {
+ blink::WebInputEvent::Type event_type = GetMouseEventType(type);
+ if (event_type == blink::WebInputEvent::Undefined) {
return Response::InvalidParams(
base::StringPrintf("Unexpected event type '%s'", type.c_str()));
}
- SetEventModifiers(&event, modifiers);
- SetEventTimestamp(&event, timestamp);
- if (!SetMouseEventButton(&event, button))
+ blink::WebPointerProperties::Button event_button =
+ blink::WebPointerProperties::Button::NoButton;
+ int button_modifiers = 0;
+ if (!GetMouseEventButton(button, &event_button, &button_modifiers))
return Response::InvalidParams("Invalid mouse button");
+ blink::WebMouseEvent event(
+ event_type,
+ GetEventModifiers(modifiers, nullptr, nullptr) | button_modifiers,
+ GetEventTimestamp(timestamp));
+
+ event.button = event_button;
event.x = x * page_scale_factor_;
event.y = y * page_scale_factor_;
event.windowX = x * page_scale_factor_;
@@ -262,45 +274,64 @@ Response InputHandler::EmulateTouchFromMouseEvent(const std::string& type,
double* delta_y,
int* modifiers,
int* click_count) {
- blink::WebMouseWheelEvent wheel_event;
- blink::WebMouseEvent mouse_event;
- blink::WebMouseEvent* event = &mouse_event;
-
+ blink::WebInputEvent::Type event_type;
if (type == emulate_touch_from_mouse_event::kTypeMouseWheel) {
+ event_type = blink::WebInputEvent::MouseWheel;
if (!delta_x || !delta_y) {
return Response::InvalidParams(
"'deltaX' and 'deltaY' are expected for mouseWheel event");
}
- wheel_event.deltaX = static_cast<float>(*delta_x);
- wheel_event.deltaY = static_cast<float>(*delta_y);
- event = &wheel_event;
- event->type = blink::WebInputEvent::MouseWheel;
- } else if (!SetMouseEventType(event, type)) {
- return Response::InvalidParams(
- base::StringPrintf("Unexpected event type '%s'", type.c_str()));
+ } else {
+ event_type = GetMouseEventType(type);
+ if (event_type == blink::WebInputEvent::Undefined) {
+ return Response::InvalidParams(
+ base::StringPrintf("Unexpected event type '%s'", type.c_str()));
+ }
}
- SetEventModifiers(event, modifiers);
- SetEventTimestamp(event, &timestamp);
- if (!SetMouseEventButton(event, &button))
+ blink::WebPointerProperties::Button event_button =
+ blink::WebPointerProperties::Button::NoButton;
+ int button_modifiers = 0;
+ if (!GetMouseEventButton(&button, &event_button, &button_modifiers))
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;
- event->pointerType = blink::WebPointerProperties::PointerType::Touch;
+ ui::ScopedWebInputEvent event;
+ blink::WebMouseWheelEvent* wheel_event = nullptr;
+ blink::WebMouseEvent* mouse_event = nullptr;
+ if (type == emulate_touch_from_mouse_event::kTypeMouseWheel) {
+ wheel_event = new blink::WebMouseWheelEvent(
+ event_type,
+ GetEventModifiers(modifiers, nullptr, nullptr) | button_modifiers,
+ GetEventTimestamp(&timestamp));
+ mouse_event = wheel_event;
+ event.reset(wheel_event);
+ wheel_event->deltaX = static_cast<float>(*delta_x);
+ wheel_event->deltaY = static_cast<float>(*delta_y);
+ } else {
+ mouse_event = new blink::WebMouseEvent(
+ event_type,
+ GetEventModifiers(modifiers, nullptr, nullptr) | button_modifiers,
+ GetEventTimestamp(&timestamp));
+ event.reset(mouse_event);
+ }
+
+ mouse_event->x = x;
+ mouse_event->y = y;
+ mouse_event->button = event_button;
+ mouse_event->windowX = x;
+ mouse_event->windowY = y;
+ mouse_event->globalX = x;
+ mouse_event->globalY = y;
+ mouse_event->clickCount = click_count ? *click_count : 0;
+ mouse_event->pointerType = blink::WebPointerProperties::PointerType::Touch;
if (!host_)
return Response::ServerError("Could not connect to view");
- if (event->type == blink::WebInputEvent::MouseWheel)
- host_->ForwardWheelEvent(wheel_event);
+ if (wheel_event)
+ host_->ForwardWheelEvent(*wheel_event);
else
- host_->ForwardMouseEvent(mouse_event);
+ host_->ForwardMouseEvent(*mouse_event);
return Response::OK();
}

Powered by Google App Engine
This is Rietveld 408576698