Commit 4a529d57 authored by Mustaq Ahmed's avatar Mustaq Ahmed Committed by Chromium LUCI CQ

[CodeHealth] Move simulated event code off from Mouse/PointerEvent.

Create a new class SimulatedEventUtil to remove duplicate code blocks
from MouseEvent and PointerEvent classes that are used only for
simulated-click related events.

Change-Id: I2cb8ac3cc14dc1a562f8e3966686a8a62e5ecb6b
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2602379Reviewed-by: default avatarRobert Flack <flackr@chromium.org>
Reviewed-by: default avatarLiviu Tinta <liviutinta@chromium.org>
Commit-Queue: Mustaq Ahmed <mustaq@chromium.org>
Auto-Submit: Mustaq Ahmed <mustaq@chromium.org>
Cr-Commit-Position: refs/heads/master@{#842704}
parent d6a56e14
...@@ -38,7 +38,7 @@ ...@@ -38,7 +38,7 @@
#include "third_party/blink/renderer/core/dom/events/window_event_context.h" #include "third_party/blink/renderer/core/dom/events/window_event_context.h"
#include "third_party/blink/renderer/core/events/keyboard_event.h" #include "third_party/blink/renderer/core/events/keyboard_event.h"
#include "third_party/blink/renderer/core/events/mouse_event.h" #include "third_party/blink/renderer/core/events/mouse_event.h"
#include "third_party/blink/renderer/core/events/pointer_event.h" #include "third_party/blink/renderer/core/events/simulated_event_util.h"
#include "third_party/blink/renderer/core/frame/ad_tracker.h" #include "third_party/blink/renderer/core/frame/ad_tracker.h"
#include "third_party/blink/renderer/core/frame/deprecation.h" #include "third_party/blink/renderer/core/frame/deprecation.h"
#include "third_party/blink/renderer/core/frame/local_dom_window.h" #include "third_party/blink/renderer/core/frame/local_dom_window.h"
...@@ -53,27 +53,6 @@ ...@@ -53,27 +53,6 @@
#include "third_party/blink/renderer/platform/instrumentation/use_counter.h" #include "third_party/blink/renderer/platform/instrumentation/use_counter.h"
namespace blink { namespace blink {
namespace {
Event* CreateEvent(const AtomicString& event_type,
Node& node,
const Event* underlying_event,
SimulatedClickCreationScope creation_scope) {
DCHECK(event_type == event_type_names::kClick ||
event_type == event_type_names::kMousedown ||
event_type == event_type_names::kMouseup);
if (RuntimeEnabledFeatures::ClickPointerEventEnabled() &&
event_type == event_type_names::kClick) {
// TODO(crbug.com/1150979): Should we also fire pointer events for
// mousedown/mouseup events? These mouse events are used for accessibility.
return PointerEvent::Create(event_type_names::kClick,
node.GetDocument().domWindow(),
underlying_event, creation_scope);
} else {
return MouseEvent::Create(event_type, node.GetDocument().domWindow(),
underlying_event, creation_scope);
}
}
} // namespace
DispatchEventResult EventDispatcher::DispatchEvent(Node& node, Event& event) { DispatchEventResult EventDispatcher::DispatchEvent(Node& node, Event& event) {
TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("blink.debug"), TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("blink.debug"),
...@@ -120,13 +99,19 @@ void EventDispatcher::DispatchSimulatedClick( ...@@ -120,13 +99,19 @@ void EventDispatcher::DispatchSimulatedClick(
nodes_dispatching_simulated_clicks->insert(&node); nodes_dispatching_simulated_clicks->insert(&node);
Element* element = DynamicTo<Element>(node); Element* element = DynamicTo<Element>(node);
LocalDOMWindow* dom_window = node.GetDocument().domWindow();
// TODO(crbug.com/1150979): Should we also fire pointer events for
// mousedown/mouseup events? These mouse events are used for accessibility.
if (mouse_event_options != kSendNoEvents) { if (mouse_event_options != kSendNoEvents) {
EventDispatcher(node, *CreateEvent(event_type_names::kMousedown, node, EventDispatcher(node, *SimulatedEventUtil::CreateEvent(
event_type_names::kMousedown, dom_window,
underlying_event, creation_scope)) underlying_event, creation_scope))
.Dispatch(); .Dispatch();
if (element) if (element)
element->SetActive(true); element->SetActive(true);
EventDispatcher(node, *CreateEvent(event_type_names::kMouseup, node, EventDispatcher(node, *SimulatedEventUtil::CreateEvent(
event_type_names::kMouseup, dom_window,
underlying_event, creation_scope)) underlying_event, creation_scope))
.Dispatch(); .Dispatch();
} }
...@@ -135,8 +120,9 @@ void EventDispatcher::DispatchSimulatedClick( ...@@ -135,8 +120,9 @@ void EventDispatcher::DispatchSimulatedClick(
if (element) if (element)
element->SetActive(false); element->SetActive(false);
// always send click // Always send click.
EventDispatcher(node, *CreateEvent(event_type_names::kClick, node, EventDispatcher(node, *SimulatedEventUtil::CreateEvent(
event_type_names::kClick, dom_window,
underlying_event, creation_scope)) underlying_event, creation_scope))
.Dispatch(); .Dispatch();
......
...@@ -67,6 +67,8 @@ blink_core_sources_events = [ ...@@ -67,6 +67,8 @@ blink_core_sources_events = [
"resource_progress_event.h", "resource_progress_event.h",
"security_policy_violation_event.cc", "security_policy_violation_event.cc",
"security_policy_violation_event.h", "security_policy_violation_event.h",
"simulated_event_util.cc",
"simulated_event_util.h",
"text_event.cc", "text_event.cc",
"text_event.h", "text_event.h",
"text_event_input_type.h", "text_event_input_type.h",
......
...@@ -128,63 +128,6 @@ MouseEvent* MouseEvent::Create(const AtomicString& event_type, ...@@ -128,63 +128,6 @@ MouseEvent* MouseEvent::Create(const AtomicString& event_type,
menu_source_type); menu_source_type);
} }
void MouseEvent::PopulateMouseEventInit(
const AtomicString& event_type,
AbstractView* view,
const Event* underlying_event,
MouseEventInit* initializer) {
WebInputEvent::Modifiers modifiers = WebInputEvent::kNoModifiers;
if (const UIEventWithKeyState* key_state_event =
FindEventWithKeyState(underlying_event)) {
modifiers = key_state_event->GetModifiers();
}
if (const auto* mouse_event = DynamicTo<MouseEvent>(underlying_event)) {
initializer->setScreenX(mouse_event->screen_location_.X());
initializer->setScreenY(mouse_event->screen_location_.Y());
initializer->setSourceCapabilities(
view ? view->GetInputDeviceCapabilities()->FiresTouchEvents(false)
: nullptr);
}
initializer->setBubbles(true);
initializer->setCancelable(true);
initializer->setView(view);
initializer->setComposed(true);
UIEventWithKeyState::SetFromWebInputEventModifiers(initializer, modifiers);
initializer->setButtons(
MouseEvent::WebInputEventModifiersToButtons(modifiers));
}
MouseEvent* MouseEvent::Create(const AtomicString& event_type,
AbstractView* view,
const Event* underlying_event,
SimulatedClickCreationScope creation_scope) {
MouseEventInit* initializer = MouseEventInit::Create();
MouseEvent::PopulateMouseEventInit(event_type, view, underlying_event,
initializer);
SyntheticEventType synthetic_type = kPositionless;
if (const auto* mouse_event = DynamicTo<MouseEvent>(underlying_event)) {
synthetic_type = kRealOrIndistinguishable;
}
base::TimeTicks timestamp = underlying_event
? underlying_event->PlatformTimeStamp()
: base::TimeTicks::Now();
MouseEvent* created_event = MakeGarbageCollected<MouseEvent>(
event_type, initializer, timestamp, synthetic_type);
created_event->SetTrusted(creation_scope ==
SimulatedClickCreationScope::kFromUserAgent);
created_event->SetUnderlyingEvent(underlying_event);
if (synthetic_type == kRealOrIndistinguishable) {
auto* mouse_event = To<MouseEvent>(created_event->UnderlyingEvent());
created_event->InitCoordinates(mouse_event->client_location_.X(),
mouse_event->client_location_.Y());
}
return created_event;
}
MouseEvent::MouseEvent() MouseEvent::MouseEvent()
: position_type_(PositionType::kPosition), : position_type_(PositionType::kPosition),
button_(0), button_(0),
......
...@@ -29,7 +29,6 @@ ...@@ -29,7 +29,6 @@
#include "third_party/blink/renderer/core/dom/events/simulated_click_options.h" #include "third_party/blink/renderer/core/dom/events/simulated_click_options.h"
#include "third_party/blink/renderer/core/events/ui_event_with_key_state.h" #include "third_party/blink/renderer/core/events/ui_event_with_key_state.h"
#include "third_party/blink/renderer/platform/geometry/double_point.h" #include "third_party/blink/renderer/platform/geometry/double_point.h"
#include "third_party/blink/renderer/platform/runtime_enabled_features.h"
#include "third_party/blink/renderer/platform/wtf/casting.h" #include "third_party/blink/renderer/platform/wtf/casting.h"
namespace blink { namespace blink {
...@@ -67,16 +66,6 @@ class CORE_EXPORT MouseEvent : public UIEventWithKeyState { ...@@ -67,16 +66,6 @@ class CORE_EXPORT MouseEvent : public UIEventWithKeyState {
const AtomicString& event_type, const AtomicString& event_type,
const MouseEventInit*); const MouseEventInit*);
static MouseEvent* Create(const AtomicString& event_type,
AbstractView*,
const Event* underlying_event,
SimulatedClickCreationScope);
static void PopulateMouseEventInit(const AtomicString& event_type,
AbstractView* view,
const Event* underlying_event,
MouseEventInit* initializer);
MouseEvent(const AtomicString& type, MouseEvent(const AtomicString& type,
const MouseEventInit*, const MouseEventInit*,
base::TimeTicks platform_time_stamp = base::TimeTicks::Now(), base::TimeTicks platform_time_stamp = base::TimeTicks::Now(),
...@@ -189,6 +178,8 @@ class CORE_EXPORT MouseEvent : public UIEventWithKeyState { ...@@ -189,6 +178,8 @@ class CORE_EXPORT MouseEvent : public UIEventWithKeyState {
DispatchEventResult DispatchEvent(EventDispatcher&) override; DispatchEventResult DispatchEvent(EventDispatcher&) override;
void InitCoordinates(const double client_x, const double client_y);
void Trace(Visitor*) const override; void Trace(Visitor*) const override;
DoublePoint screen_location_; DoublePoint screen_location_;
...@@ -203,8 +194,6 @@ class CORE_EXPORT MouseEvent : public UIEventWithKeyState { ...@@ -203,8 +194,6 @@ class CORE_EXPORT MouseEvent : public UIEventWithKeyState {
void ComputeRelativePosition(); void ComputeRelativePosition();
void InitCoordinates(const double client_x, const double client_y);
bool has_cached_relative_position_ = false; bool has_cached_relative_position_ = false;
private: private:
......
...@@ -13,34 +13,6 @@ ...@@ -13,34 +13,6 @@
namespace blink { namespace blink {
PointerEvent* PointerEvent::Create(const AtomicString& event_type,
AbstractView* view,
const Event* underlying_event,
SimulatedClickCreationScope creation_scope) {
PointerEventInit* initializer = PointerEventInit::Create();
MouseEvent::PopulateMouseEventInit(event_type, view, underlying_event,
initializer);
base::TimeTicks timestamp = underlying_event
? underlying_event->PlatformTimeStamp()
: base::TimeTicks::Now();
SyntheticEventType synthetic_type = kPositionless;
if (const auto* mouse_event = DynamicTo<MouseEvent>(underlying_event)) {
synthetic_type = kRealOrIndistinguishable;
}
PointerEvent* created_event = MakeGarbageCollected<PointerEvent>(
event_type, initializer, timestamp, synthetic_type);
created_event->SetTrusted(creation_scope ==
SimulatedClickCreationScope::kFromUserAgent);
created_event->SetUnderlyingEvent(underlying_event);
if (synthetic_type == kRealOrIndistinguishable) {
auto* mouse_event = To<MouseEvent>(created_event->UnderlyingEvent());
created_event->InitCoordinates(mouse_event->client_location_.X(),
mouse_event->client_location_.Y());
}
return created_event;
}
PointerEvent::PointerEvent(const AtomicString& type, PointerEvent::PointerEvent(const AtomicString& type,
const PointerEventInit* initializer, const PointerEventInit* initializer,
base::TimeTicks platform_time_stamp, base::TimeTicks platform_time_stamp,
......
...@@ -7,7 +7,6 @@ ...@@ -7,7 +7,6 @@
#include "third_party/blink/public/common/input/pointer_id.h" #include "third_party/blink/public/common/input/pointer_id.h"
#include "third_party/blink/renderer/core/events/mouse_event.h" #include "third_party/blink/renderer/core/events/mouse_event.h"
#include "third_party/blink/renderer/platform/runtime_enabled_features.h"
#include "third_party/blink/renderer/platform/wtf/casting.h" #include "third_party/blink/renderer/platform/wtf/casting.h"
namespace blink { namespace blink {
...@@ -29,10 +28,6 @@ class CORE_EXPORT PointerEvent final : public MouseEvent { ...@@ -29,10 +28,6 @@ class CORE_EXPORT PointerEvent final : public MouseEvent {
type, initializer, platform_time_stamp, synthetic_event_type, type, initializer, platform_time_stamp, synthetic_event_type,
menu_source_type); menu_source_type);
} }
static PointerEvent* Create(const AtomicString& event_type,
AbstractView*,
const Event* underlying_event,
SimulatedClickCreationScope);
PointerEvent(const AtomicString&, PointerEvent(const AtomicString&,
const PointerEventInit*, const PointerEventInit*,
......
// Copyright 2021 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 "third_party/blink/renderer/core/events/simulated_event_util.h"
#include "base/time/time.h"
#include "third_party/blink/renderer/core/dom/events/event.h"
#include "third_party/blink/renderer/core/dom/events/simulated_click_options.h"
#include "third_party/blink/renderer/core/event_type_names.h"
#include "third_party/blink/renderer/core/events/mouse_event.h"
#include "third_party/blink/renderer/core/events/mouse_event_init.h"
#include "third_party/blink/renderer/core/events/pointer_event.h"
#include "third_party/blink/renderer/core/events/pointer_event_init.h"
#include "third_party/blink/renderer/core/events/ui_event.h"
#include "third_party/blink/renderer/core/events/ui_event_with_key_state.h"
#include "third_party/blink/renderer/core/input/input_device_capabilities.h"
#include "third_party/blink/renderer/platform/runtime_enabled_features.h"
#include "third_party/blink/renderer/platform/wtf/casting.h"
#include "third_party/blink/renderer/platform/wtf/text/atomic_string.h"
namespace blink {
namespace {
void PopulateSimulatedMouseEventInit(const AtomicString& event_type,
AbstractView* view,
const Event* underlying_event,
MouseEventInit* initializer) {
WebInputEvent::Modifiers modifiers = WebInputEvent::kNoModifiers;
if (const UIEventWithKeyState* key_state_event =
FindEventWithKeyState(underlying_event)) {
modifiers = key_state_event->GetModifiers();
}
if (const auto* mouse_event = DynamicTo<MouseEvent>(underlying_event)) {
initializer->setScreenX(mouse_event->screen_location_.X());
initializer->setScreenY(mouse_event->screen_location_.Y());
initializer->setSourceCapabilities(
view ? view->GetInputDeviceCapabilities()->FiresTouchEvents(false)
: nullptr);
}
initializer->setBubbles(true);
initializer->setCancelable(true);
initializer->setView(view);
initializer->setComposed(true);
UIEventWithKeyState::SetFromWebInputEventModifiers(initializer, modifiers);
initializer->setButtons(
MouseEvent::WebInputEventModifiersToButtons(modifiers));
}
enum class EventClassType { kMouse, kPointer };
MouseEvent* CreateMouseOrPointerEvent(
EventClassType event_class_type,
const AtomicString& event_type,
AbstractView* view,
const Event* underlying_event,
SimulatedClickCreationScope creation_scope) {
// We picked |PointerEventInit| object to be able to create either
// |MouseEvent| or |PointerEvent| below. When a |PointerEvent| is created,
// any event attributes not initialized in the |PointerEventInit| below get
// their default values, all of which are appropriate for a simulated
// |PointerEvent|.
//
// TODO(mustaq): Set |pointerId| to -1 after we have a spec change to fix the
// issue https://github.com/w3c/pointerevents/issues/343.
PointerEventInit* initializer = PointerEventInit::Create();
PopulateSimulatedMouseEventInit(event_type, view, underlying_event,
initializer);
base::TimeTicks timestamp = underlying_event
? underlying_event->PlatformTimeStamp()
: base::TimeTicks::Now();
MouseEvent::SyntheticEventType synthetic_type = MouseEvent::kPositionless;
if (const auto* mouse_event = DynamicTo<MouseEvent>(underlying_event)) {
synthetic_type = MouseEvent::kRealOrIndistinguishable;
}
MouseEvent* created_event;
if (event_class_type == EventClassType::kPointer) {
created_event = MakeGarbageCollected<PointerEvent>(
event_type, initializer, timestamp, synthetic_type);
} else {
created_event = MakeGarbageCollected<MouseEvent>(event_type, initializer,
timestamp, synthetic_type);
}
created_event->SetTrusted(creation_scope ==
SimulatedClickCreationScope::kFromUserAgent);
created_event->SetUnderlyingEvent(underlying_event);
if (synthetic_type == MouseEvent::kRealOrIndistinguishable) {
auto* mouse_event = To<MouseEvent>(created_event->UnderlyingEvent());
created_event->InitCoordinates(mouse_event->client_location_.X(),
mouse_event->client_location_.Y());
}
return created_event;
}
} // namespace
Event* SimulatedEventUtil::CreateEvent(
const AtomicString& event_type,
AbstractView* view,
const Event* underlying_event,
SimulatedClickCreationScope creation_scope) {
DCHECK(event_type == event_type_names::kClick ||
event_type == event_type_names::kMousedown ||
event_type == event_type_names::kMouseup);
EventClassType event_class_type = EventClassType::kMouse;
if (RuntimeEnabledFeatures::ClickPointerEventEnabled() &&
event_type == event_type_names::kClick) {
event_class_type = EventClassType::kPointer;
}
return CreateMouseOrPointerEvent(event_class_type, event_type, view,
underlying_event, creation_scope);
}
} // namespace blink
// Copyright 2021 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.sy
#ifndef THIRD_PARTY_BLINK_RENDERER_CORE_EVENTS_SIMULATED_EVENT_UTIL_H_
#define THIRD_PARTY_BLINK_RENDERER_CORE_EVENTS_SIMULATED_EVENT_UTIL_H_
#include "third_party/blink/renderer/core/core_export.h"
#include "third_party/blink/renderer/core/dom/events/event.h"
#include "third_party/blink/renderer/core/dom/events/simulated_click_options.h"
#include "third_party/blink/renderer/core/events/ui_event.h"
#include "third_party/blink/renderer/platform/wtf/text/atomic_string.h"
namespace blink {
class CORE_EXPORT SimulatedEventUtil {
public:
SimulatedEventUtil() = delete;
static Event* CreateEvent(const AtomicString& event_type,
AbstractView* view,
const Event* underlying_event,
SimulatedClickCreationScope creation_scope);
};
} // namespace blink
#endif // THIRD_PARTY_BLINK_RENDERER_CORE_EVENTS_SIMULATED_EVENT_UTIL_H_
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment