SimulatedInputDispatcher.cpp [plain text]
#include "config.h"
#include "SimulatedInputDispatcher.h"
#include "AutomationProtocolObjects.h"
#include "WebAutomationSession.h"
#include "WebAutomationSessionMacros.h"
namespace WebKit {
SimulatedInputSourceState SimulatedInputSourceState::emptyStateForSourceType(SimulatedInputSourceType type)
{
SimulatedInputSourceState result { };
switch (type) {
case SimulatedInputSourceType::Null:
case SimulatedInputSourceType::Keyboard:
break;
case SimulatedInputSourceType::Mouse:
case SimulatedInputSourceType::Touch:
result.location = WebCore::IntPoint();
}
return result;
}
SimulatedInputKeyFrame::SimulatedInputKeyFrame(Vector<StateEntry>&& entries)
: states(WTFMove(entries))
{
}
Seconds SimulatedInputKeyFrame::maximumDuration() const
{
Seconds result;
for (auto& entry : states)
result = std::max(result, entry.second.duration.value_or(Seconds(0)));
return result;
}
SimulatedInputKeyFrame SimulatedInputKeyFrame::keyFrameFromStateOfInputSources(HashSet<Ref<SimulatedInputSource>>& inputSources)
{
Vector<SimulatedInputKeyFrame::StateEntry> entries;
entries.reserveCapacity(inputSources.size());
for (auto& inputSource : inputSources)
entries.uncheckedAppend(std::pair<SimulatedInputSource&, SimulatedInputSourceState> { inputSource.get(), inputSource->state });
return SimulatedInputKeyFrame(WTFMove(entries));
}
SimulatedInputKeyFrame SimulatedInputKeyFrame::keyFrameToResetInputSources(HashSet<Ref<SimulatedInputSource>>& inputSources)
{
Vector<SimulatedInputKeyFrame::StateEntry> entries;
entries.reserveCapacity(inputSources.size());
for (auto& inputSource : inputSources)
entries.uncheckedAppend(std::pair<SimulatedInputSource&, SimulatedInputSourceState> { inputSource.get(), SimulatedInputSourceState::emptyStateForSourceType(inputSource->type) });
return SimulatedInputKeyFrame(WTFMove(entries));
}
SimulatedInputDispatcher::SimulatedInputDispatcher(WebPageProxy& page, SimulatedInputDispatcher::Client& client)
: m_page(page)
, m_client(client)
, m_keyFrameTransitionDurationTimer(RunLoop::current(), this, &SimulatedInputDispatcher::keyFrameTransitionDurationTimerFired)
{
}
SimulatedInputDispatcher::~SimulatedInputDispatcher()
{
ASSERT(!m_runCompletionHandler);
ASSERT(!m_keyFrameTransitionDurationTimer.isActive());
}
bool SimulatedInputDispatcher::isActive() const
{
return !!m_runCompletionHandler;
}
void SimulatedInputDispatcher::keyFrameTransitionDurationTimerFired()
{
ASSERT(m_keyFrameTransitionCompletionHandler);
m_keyFrameTransitionDurationTimer.stop();
if (isKeyFrameTransitionComplete()) {
auto finish = std::exchange(m_keyFrameTransitionCompletionHandler, nullptr);
finish(std::nullopt);
}
}
bool SimulatedInputDispatcher::isKeyFrameTransitionComplete() const
{
ASSERT(m_keyframeIndex < m_keyframes.size());
if (m_inputSourceStateIndex < m_keyframes[m_keyframeIndex].states.size())
return false;
if (m_keyFrameTransitionDurationTimer.isActive())
return false;
return true;
}
void SimulatedInputDispatcher::transitionToNextKeyFrame()
{
++m_keyframeIndex;
if (m_keyframeIndex == m_keyframes.size()) {
finishDispatching(std::nullopt);
return;
}
transitionBetweenKeyFrames(m_keyframes[m_keyframeIndex - 1], m_keyframes[m_keyframeIndex], [this, protectedThis = makeRef(*this)](std::optional<AutomationCommandError> error) {
if (error) {
finishDispatching(error);
return;
}
transitionToNextKeyFrame();
});
}
void SimulatedInputDispatcher::transitionToNextInputSourceState()
{
if (isKeyFrameTransitionComplete()) {
auto finish = std::exchange(m_keyFrameTransitionCompletionHandler, nullptr);
finish(std::nullopt);
return;
}
if (m_inputSourceStateIndex == m_keyframes[m_keyframeIndex].states.size())
return;
auto& nextKeyFrame = m_keyframes[m_keyframeIndex];
auto& postStateEntry = nextKeyFrame.states[m_inputSourceStateIndex];
SimulatedInputSource& inputSource = postStateEntry.first;
transitionInputSourceToState(inputSource, postStateEntry.second, [this, protectedThis = makeRef(*this)](std::optional<AutomationCommandError> error) {
if (error) {
auto finish = std::exchange(m_keyFrameTransitionCompletionHandler, nullptr);
finish(error);
return;
}
++m_inputSourceStateIndex;
transitionToNextInputSourceState();
});
}
void SimulatedInputDispatcher::transitionBetweenKeyFrames(const SimulatedInputKeyFrame& a, const SimulatedInputKeyFrame& b, AutomationCompletionHandler&& completionHandler)
{
m_inputSourceStateIndex = 0;
m_keyFrameTransitionCompletionHandler = WTFMove(completionHandler);
m_keyFrameTransitionDurationTimer.startOneShot(b.maximumDuration());
transitionToNextInputSourceState();
}
void SimulatedInputDispatcher::resolveLocation(const WebCore::IntPoint& currentLocation, std::optional<WebCore::IntPoint> location, MouseMoveOrigin origin, std::optional<String> nodeHandle, Function<void (std::optional<WebCore::IntPoint>, std::optional<AutomationCommandError>)>&& completionHandler)
{
if (!location) {
completionHandler(currentLocation, std::nullopt);
return;
}
switch (origin) {
case MouseMoveOrigin::Viewport:
completionHandler(location.value(), std::nullopt);
break;
case MouseMoveOrigin::Pointer: {
WebCore::IntPoint destination(currentLocation);
destination.moveBy(location.value());
completionHandler(destination, std::nullopt);
break;
}
case MouseMoveOrigin::Element: {
m_client.viewportInViewCenterPointOfElement(m_page, m_frameID.value(), nodeHandle.value(), [destination = location.value(), completionHandler = WTFMove(completionHandler)](std::optional<WebCore::IntPoint> inViewCenterPoint, std::optional<AutomationCommandError> error) mutable {
if (error) {
completionHandler(std::nullopt, error);
return;
}
ASSERT(inViewCenterPoint);
destination.moveBy(inViewCenterPoint.value());
completionHandler(destination, std::nullopt);
});
break;
}
}
}
void SimulatedInputDispatcher::transitionInputSourceToState(SimulatedInputSource& inputSource, SimulatedInputSourceState& newState, AutomationCompletionHandler&& completionHandler)
{
SimulatedInputSourceState& a = inputSource.state;
SimulatedInputSourceState& b = newState;
AutomationCompletionHandler eventDispatchFinished = [&inputSource, &newState, completionHandler = WTFMove(completionHandler)](std::optional<AutomationCommandError> error) {
if (error) {
completionHandler(error);
return;
}
inputSource.state = newState;
completionHandler(std::nullopt);
};
switch (inputSource.type) {
case SimulatedInputSourceType::Null:
eventDispatchFinished(std::nullopt);
break;
case SimulatedInputSourceType::Mouse: {
resolveLocation(a.location.value_or(WebCore::IntPoint()), b.location, b.origin.value_or(MouseMoveOrigin::Viewport), b.nodeHandle, [this, &a, &b, eventDispatchFinished = WTFMove(eventDispatchFinished)](std::optional<WebCore::IntPoint> location, std::optional<AutomationCommandError> error) mutable {
if (error) {
eventDispatchFinished(error);
return;
}
RELEASE_ASSERT(location);
b.location = location;
if (!a.pressedMouseButton && b.pressedMouseButton)
m_client.simulateMouseInteraction(m_page, MouseInteraction::Down, b.pressedMouseButton.value(), b.location.value(), WTFMove(eventDispatchFinished));
else if (a.pressedMouseButton && !b.pressedMouseButton)
m_client.simulateMouseInteraction(m_page, MouseInteraction::Up, a.pressedMouseButton.value(), b.location.value(), WTFMove(eventDispatchFinished));
else if (a.location != b.location) {
m_client.simulateMouseInteraction(m_page, MouseInteraction::Move, b.pressedMouseButton.value_or(MouseButton::NoButton), b.location.value(), WTFMove(eventDispatchFinished));
} else
eventDispatchFinished(std::nullopt);
});
break;
}
case SimulatedInputSourceType::Keyboard:
if (!a.pressedCharKey && b.pressedCharKey)
m_client.simulateKeyboardInteraction(m_page, KeyboardInteraction::KeyPress, b.pressedCharKey.value(), WTFMove(eventDispatchFinished));
else if (a.pressedCharKey && !b.pressedCharKey)
m_client.simulateKeyboardInteraction(m_page, KeyboardInteraction::KeyRelease, a.pressedCharKey.value(), WTFMove(eventDispatchFinished));
else if (a.pressedVirtualKeys != b.pressedVirtualKeys) {
for (VirtualKey key : b.pressedVirtualKeys) {
if (!a.pressedVirtualKeys.contains(key))
m_client.simulateKeyboardInteraction(m_page, KeyboardInteraction::KeyPress, key, WTFMove(eventDispatchFinished));
}
for (VirtualKey key : a.pressedVirtualKeys) {
if (!b.pressedVirtualKeys.contains(key))
m_client.simulateKeyboardInteraction(m_page, KeyboardInteraction::KeyRelease, key, WTFMove(eventDispatchFinished));
}
} else
eventDispatchFinished(std::nullopt);
break;
case SimulatedInputSourceType::Touch:
ASSERT_NOT_REACHED();
eventDispatchFinished(AUTOMATION_COMMAND_ERROR_WITH_NAME(NotImplemented));
break;
}
}
void SimulatedInputDispatcher::run(uint64_t frameID, Vector<SimulatedInputKeyFrame>&& keyFrames, HashSet<Ref<SimulatedInputSource>>& inputSources, AutomationCompletionHandler&& completionHandler)
{
ASSERT(!isActive());
if (isActive()) {
completionHandler(AUTOMATION_COMMAND_ERROR_WITH_NAME(InternalError));
return;
}
m_frameID = frameID;
m_runCompletionHandler = WTFMove(completionHandler);
for (const Ref<SimulatedInputSource>& inputSource : inputSources)
m_inputSources.add(inputSource.copyRef());
m_keyframes.reserveCapacity(keyFrames.size() + 1);
m_keyframes.append(SimulatedInputKeyFrame::keyFrameFromStateOfInputSources(m_inputSources));
m_keyframes.appendVector(WTFMove(keyFrames));
transitionToNextKeyFrame();
}
void SimulatedInputDispatcher::cancel()
{
if (isActive())
finishDispatching(AUTOMATION_COMMAND_ERROR_WITH_NAME(InternalError));
}
void SimulatedInputDispatcher::finishDispatching(std::optional<AutomationCommandError> error)
{
m_keyFrameTransitionDurationTimer.stop();
auto finish = std::exchange(m_runCompletionHandler, nullptr);
m_frameID = std::nullopt;
m_keyframes.clear();
m_inputSources.clear();
m_keyframeIndex = 0;
m_inputSourceStateIndex = 0;
finish(error);
}
}