UserGestureIndicator.h [plain text]
#pragma once
#include "DOMPasteAccess.h"
#include <wtf/Function.h>
#include <wtf/MonotonicTime.h>
#include <wtf/Noncopyable.h>
#include <wtf/RefCounted.h>
#include <wtf/Vector.h>
#include <wtf/WeakPtr.h>
namespace WebCore {
class Document;
enum ProcessingUserGestureState {
ProcessingUserGesture,
ProcessingPotentialUserGesture,
NotProcessingUserGesture
};
enum class UserGestureType { EscapeKey, Other };
class UserGestureToken : public RefCounted<UserGestureToken>, public CanMakeWeakPtr<UserGestureToken> {
public:
static constexpr Seconds maximumIntervalForUserGestureForwarding { 1_s }; static const Seconds& maximumIntervalForUserGestureForwardingForFetch();
WEBCORE_EXPORT static void setMaximumIntervalForUserGestureForwardingForFetchForTesting(Seconds);
static Ref<UserGestureToken> create(ProcessingUserGestureState state, UserGestureType gestureType)
{
return adoptRef(*new UserGestureToken(state, gestureType));
}
WEBCORE_EXPORT ~UserGestureToken();
ProcessingUserGestureState state() const { return m_state; }
bool processingUserGesture() const { return m_scope == GestureScope::All && m_state == ProcessingUserGesture; }
bool processingUserGestureForMedia() const { return m_state == ProcessingUserGesture || m_state == ProcessingPotentialUserGesture; }
UserGestureType gestureType() const { return m_gestureType; }
void addDestructionObserver(WTF::Function<void (UserGestureToken&)>&& observer)
{
m_destructionObservers.append(WTFMove(observer));
}
DOMPasteAccessPolicy domPasteAccessPolicy() const { return m_domPasteAccessPolicy; }
void didRequestDOMPasteAccess(DOMPasteAccessResponse response)
{
switch (response) {
case DOMPasteAccessResponse::DeniedForGesture:
m_domPasteAccessPolicy = DOMPasteAccessPolicy::Denied;
break;
case DOMPasteAccessResponse::GrantedForCommand:
break;
case DOMPasteAccessResponse::GrantedForGesture:
m_domPasteAccessPolicy = DOMPasteAccessPolicy::Granted;
break;
}
}
void resetDOMPasteAccess() { m_domPasteAccessPolicy = DOMPasteAccessPolicy::NotRequestedYet; }
enum class GestureScope { All, MediaOnly };
void setScope(GestureScope scope) { m_scope = scope; }
void resetScope() { m_scope = GestureScope::All; }
enum class IsPropagatedFromFetch { Yes, No };
void setIsPropagatedFromFetch(IsPropagatedFromFetch is) { m_isPropagatedFromFetch = is; }
void resetIsPropagatedFromFetch() { m_isPropagatedFromFetch = IsPropagatedFromFetch::No; }
bool isPropagatedFromFetch() const { return m_isPropagatedFromFetch == IsPropagatedFromFetch::Yes; }
bool hasExpired(Seconds expirationInterval) const
{
return m_startTime + expirationInterval < MonotonicTime::now();
}
MonotonicTime startTime() const { return m_startTime; }
private:
UserGestureToken(ProcessingUserGestureState state, UserGestureType gestureType)
: m_state(state)
, m_gestureType(gestureType)
{
}
ProcessingUserGestureState m_state = NotProcessingUserGesture;
Vector<WTF::Function<void (UserGestureToken&)>> m_destructionObservers;
UserGestureType m_gestureType;
DOMPasteAccessPolicy m_domPasteAccessPolicy { DOMPasteAccessPolicy::NotRequestedYet };
GestureScope m_scope { GestureScope::All };
MonotonicTime m_startTime { MonotonicTime::now() };
IsPropagatedFromFetch m_isPropagatedFromFetch { IsPropagatedFromFetch::No };
};
class UserGestureIndicator {
WTF_MAKE_FAST_ALLOCATED;
WTF_MAKE_NONCOPYABLE(UserGestureIndicator);
public:
WEBCORE_EXPORT static RefPtr<UserGestureToken> currentUserGesture();
WEBCORE_EXPORT static bool processingUserGesture();
WEBCORE_EXPORT static bool processingUserGestureForMedia();
enum class ProcessInteractionStyle { Immediate, Delayed };
WEBCORE_EXPORT explicit UserGestureIndicator(Optional<ProcessingUserGestureState>, Document* = nullptr, UserGestureType = UserGestureType::Other, ProcessInteractionStyle = ProcessInteractionStyle::Immediate);
WEBCORE_EXPORT explicit UserGestureIndicator(RefPtr<UserGestureToken>, UserGestureToken::GestureScope = UserGestureToken::GestureScope::All, UserGestureToken::IsPropagatedFromFetch = UserGestureToken::IsPropagatedFromFetch::No);
WEBCORE_EXPORT ~UserGestureIndicator();
private:
RefPtr<UserGestureToken> m_previousToken;
};
}