SimulatedClick.cpp [plain text]
#include "config.h"
#include "SimulatedClick.h"
#include "DOMRect.h"
#include "DataTransfer.h"
#include "Element.h"
#include "EventNames.h"
#include "MouseEvent.h"
#include <wtf/NeverDestroyed.h>
namespace WebCore {
class SimulatedMouseEvent final : public MouseEvent {
public:
static Ref<SimulatedMouseEvent> create(const AtomicString& eventType, RefPtr<WindowProxy>&& view, RefPtr<Event>&& underlyingEvent, Element& target, SimulatedClickSource source)
{
return adoptRef(*new SimulatedMouseEvent(eventType, WTFMove(view), WTFMove(underlyingEvent), target, source));
}
private:
SimulatedMouseEvent(const AtomicString& eventType, RefPtr<WindowProxy>&& view, RefPtr<Event>&& underlyingEvent, Element& target, SimulatedClickSource source)
: MouseEvent(eventType, true, true, underlyingEvent ? underlyingEvent->timeStamp() : MonotonicTime::now(), WTFMove(view), 0, { }, { },
#if ENABLE(POINTER_LOCK)
{ },
#endif
false, false, false, false, 0, 0, nullptr, 0, 0, nullptr, true)
{
if (source == SimulatedClickSource::Bindings)
setUntrusted();
if (UIEventWithKeyState* keyStateEvent = findEventWithKeyState(underlyingEvent.get())) {
m_ctrlKey = keyStateEvent->ctrlKey();
m_altKey = keyStateEvent->altKey();
m_shiftKey = keyStateEvent->shiftKey();
m_metaKey = keyStateEvent->metaKey();
}
setUnderlyingEvent(underlyingEvent.get());
if (is<MouseEvent>(this->underlyingEvent())) {
MouseEvent& mouseEvent = downcast<MouseEvent>(*this->underlyingEvent());
m_screenLocation = mouseEvent.screenLocation();
initCoordinates(mouseEvent.clientLocation());
} else if (source == SimulatedClickSource::UserAgent) {
m_screenLocation = target.screenRect().center();
initCoordinates(LayoutPoint(target.boundingClientRect().center()));
}
}
};
static void simulateMouseEvent(const AtomicString& eventType, Element& element, Event* underlyingEvent, SimulatedClickSource source)
{
element.dispatchEvent(SimulatedMouseEvent::create(eventType, element.document().windowProxy(), underlyingEvent, element, source));
}
void simulateClick(Element& element, Event* underlyingEvent, SimulatedClickMouseEventOptions mouseEventOptions, SimulatedClickVisualOptions visualOptions, SimulatedClickSource creationOptions)
{
if (element.isDisabledFormControl())
return;
static NeverDestroyed<HashSet<Element*>> elementsDispatchingSimulatedClicks;
if (!elementsDispatchingSimulatedClicks.get().add(&element).isNewEntry)
return;
if (mouseEventOptions == SendMouseOverUpDownEvents)
simulateMouseEvent(eventNames().mouseoverEvent, element, underlyingEvent, creationOptions);
if (mouseEventOptions != SendNoEvents)
simulateMouseEvent(eventNames().mousedownEvent, element, underlyingEvent, creationOptions);
element.setActive(true, visualOptions == ShowPressedLook);
if (mouseEventOptions != SendNoEvents)
simulateMouseEvent(eventNames().mouseupEvent, element, underlyingEvent, creationOptions);
element.setActive(false);
simulateMouseEvent(eventNames().clickEvent, element, underlyingEvent, creationOptions);
elementsDispatchingSimulatedClicks.get().remove(&element);
}
}