UserGestureIndicator.cpp [plain text]
#include "config.h"
#include "UserGestureIndicator.h"
#include "Document.h"
#include "ResourceLoadObserver.h"
#include <wtf/MainThread.h>
#include <wtf/NeverDestroyed.h>
namespace WebCore {
static RefPtr<UserGestureToken>& currentToken()
{
ASSERT(isMainThread());
static NeverDestroyed<RefPtr<UserGestureToken>> token;
return token;
}
UserGestureToken::~UserGestureToken()
{
for (auto& observer : m_destructionObservers)
observer(*this);
}
UserGestureIndicator::UserGestureIndicator(Optional<ProcessingUserGestureState> state, Document* document, UserGestureType gestureType, ProcessInteractionStyle processInteractionStyle)
: m_previousToken { currentToken() }
{
ASSERT(isMainThread());
if (state)
currentToken() = UserGestureToken::create(state.value(), gestureType);
if (document && currentToken()->processingUserGesture()) {
document->updateLastHandledUserGestureTimestamp(MonotonicTime::now());
if (processInteractionStyle == ProcessInteractionStyle::Immediate)
ResourceLoadObserver::shared().logUserInteractionWithReducedTimeResolution(document->topDocument());
document->topDocument().setUserDidInteractWithPage(true);
}
}
UserGestureIndicator::UserGestureIndicator(RefPtr<UserGestureToken> token)
{
if (!isMainThread())
return;
m_previousToken = currentToken();
if (token)
currentToken() = token;
}
UserGestureIndicator::~UserGestureIndicator()
{
if (!isMainThread())
return;
currentToken() = m_previousToken;
}
RefPtr<UserGestureToken> UserGestureIndicator::currentUserGesture()
{
if (!isMainThread())
return nullptr;
return currentToken();
}
bool UserGestureIndicator::processingUserGesture()
{
if (!isMainThread())
return false;
return currentToken() ? currentToken()->processingUserGesture() : false;
}
bool UserGestureIndicator::processingUserGestureForMedia()
{
if (!isMainThread())
return false;
return currentToken() ? currentToken()->processingUserGestureForMedia() : false;
}
}