UserGestureIndicator.cpp [plain text]
#include "config.h"
#include "UserGestureIndicator.h"
#include "DOMWindow.h"
#include "Document.h"
#include "Frame.h"
#include "ResourceLoadObserver.h"
#include "SecurityOrigin.h"
#include <wtf/MainThread.h>
#include <wtf/NeverDestroyed.h>
#include <wtf/Optional.h>
namespace WebCore {
static RefPtr<UserGestureToken>& currentToken()
{
ASSERT(isMainThread());
static NeverDestroyed<RefPtr<UserGestureToken>> token;
return token;
}
UserGestureToken::UserGestureToken(ProcessingUserGestureState state, UserGestureType gestureType, Document* document)
: m_state(state)
, m_gestureType(gestureType)
{
if (!document || !processingUserGesture())
return;
m_documentsImpactedByUserGesture.add(*document);
auto* documentFrame = document->frame();
if (!documentFrame)
return;
for (auto* ancestorFrame = documentFrame->tree().parent(); ancestorFrame; ancestorFrame = ancestorFrame->tree().parent()) {
if (auto* ancestorDocument = ancestorFrame->document())
m_documentsImpactedByUserGesture.add(ancestorDocument);
}
auto& documentOrigin = document->securityOrigin();
for (auto* frame = &documentFrame->tree().top(); frame; frame = frame->tree().traverseNext()) {
auto* frameDocument = frame->document();
if (frameDocument && documentOrigin.canAccess(frameDocument->securityOrigin()))
m_documentsImpactedByUserGesture.add(*frameDocument);
}
}
UserGestureToken::~UserGestureToken()
{
for (auto& observer : m_destructionObservers)
observer(*this);
}
static Seconds maxIntervalForUserGestureForwardingForFetch { 10 };
const Seconds& UserGestureToken::maximumIntervalForUserGestureForwardingForFetch()
{
return maxIntervalForUserGestureForwardingForFetch;
}
void UserGestureToken::setMaximumIntervalForUserGestureForwardingForFetchForTesting(Seconds value)
{
maxIntervalForUserGestureForwardingForFetch = WTFMove(value);
}
bool UserGestureToken::isValidForDocument(Document& document) const
{
return m_documentsImpactedByUserGesture.contains(document);
}
UserGestureIndicator::UserGestureIndicator(Optional<ProcessingUserGestureState> state, Document* document, UserGestureType gestureType, ProcessInteractionStyle processInteractionStyle)
: m_previousToken { currentToken() }
{
ASSERT(isMainThread());
if (state)
currentToken() = UserGestureToken::create(state.value(), gestureType, document);
if (document && currentToken()->processingUserGesture() && state) {
document->updateLastHandledUserGestureTimestamp(currentToken()->startTime());
if (processInteractionStyle == ProcessInteractionStyle::Immediate)
ResourceLoadObserver::shared().logUserInteractionWithReducedTimeResolution(document->topDocument());
document->topDocument().setUserDidInteractWithPage(true);
if (auto* frame = document->frame()) {
if (!frame->hasHadUserInteraction()) {
for (; frame; frame = frame->tree().parent())
frame->setHasHadUserInteraction();
}
}
if (auto* window = document->domWindow())
window->notifyActivated(currentToken()->startTime());
}
}
UserGestureIndicator::UserGestureIndicator(RefPtr<UserGestureToken> token, UserGestureToken::GestureScope scope, UserGestureToken::IsPropagatedFromFetch isPropagatedFromFetch)
{
if (!isMainThread())
return;
m_previousToken = currentToken();
if (token) {
token->setScope(scope);
token->setIsPropagatedFromFetch(isPropagatedFromFetch);
currentToken() = token;
}
}
UserGestureIndicator::~UserGestureIndicator()
{
if (!isMainThread())
return;
if (auto token = currentToken()) {
token->resetDOMPasteAccess();
token->resetScope();
token->resetIsPropagatedFromFetch();
}
currentToken() = m_previousToken;
}
RefPtr<UserGestureToken> UserGestureIndicator::currentUserGesture()
{
if (!isMainThread())
return nullptr;
return currentToken();
}
bool UserGestureIndicator::processingUserGesture(Document* document)
{
if (!isMainThread())
return false;
if (!currentToken() || !currentToken()->processingUserGesture())
return false;
return !document || currentToken()->isValidForDocument(*document);
}
bool UserGestureIndicator::processingUserGestureForMedia()
{
if (!isMainThread())
return false;
return currentToken() ? currentToken()->processingUserGestureForMedia() : false;
}
}