1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
|
/*
* Copyright (C) 2010 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "config.h"
#include "UserGestureIndicator.h"
#include "Document.h"
#include "FrameDestructionObserverInlines.h"
#include "LocalDOMWindow.h"
#include "LocalFrame.h"
#include "ResourceLoadObserver.h"
#include "SecurityOrigin.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(ProcessingUserGestureState state, UserGestureType gestureType, Document* document, std::optional<WTF::UUID> authorizationToken)
: m_state(state)
, m_gestureType(gestureType)
, m_authorizationToken(authorizationToken)
{
if (!document || !processingUserGesture())
return;
// User gesture is valid for the document that received the user gesture, all of its ancestors
// as well as all same-origin documents on the page.
m_documentsImpactedByUserGesture.add(*document);
auto* documentFrame = document->frame();
if (!documentFrame)
return;
for (auto* ancestorFrame = documentFrame->tree().parent(); ancestorFrame; ancestorFrame = ancestorFrame->tree().parent()) {
auto* localAncestor = dynamicDowncast<LocalFrame>(ancestorFrame);
if (!localAncestor)
continue;
if (auto* ancestorDocument = localAncestor->document())
m_documentsImpactedByUserGesture.add(*ancestorDocument);
}
auto& documentOrigin = document->securityOrigin();
for (Frame* frame = &documentFrame->tree().top(); frame; frame = frame->tree().traverseNext()) {
auto* localFrame = dynamicDowncast<LocalFrame>(frame);
if (!localFrame)
continue;
auto* frameDocument = localFrame->document();
if (frameDocument && documentOrigin.isSameOriginDomain(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(const Document& document) const
{
return m_documentsImpactedByUserGesture.contains(document);
}
void UserGestureToken::forEachImpactedDocument(Function<void(Document&)>&& function)
{
m_documentsImpactedByUserGesture.forEach(function);
}
UserGestureIndicator::UserGestureIndicator(std::optional<ProcessingUserGestureState> state, Document* document, UserGestureType gestureType, ProcessInteractionStyle processInteractionStyle, std::optional<WTF::UUID> authorizationToken)
: m_previousToken { currentToken() }
{
ASSERT(isMainThread());
if (state)
currentToken() = UserGestureToken::create(state.value(), gestureType, document, authorizationToken);
if (state && document && currentToken()->processingUserGesture()) {
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 *ancestor = frame; ancestor; ancestor = ancestor->tree().parent()) {
auto* localAncestor = dynamicDowncast<LocalFrame>(ancestor);
if (!localAncestor)
continue;
localAncestor->setHasHadUserInteraction();
}
}
}
// https://html.spec.whatwg.org/multipage/interaction.html#user-activation-processing-model
// When a user interaction causes firing of an activation triggering input event in a Document...
// NOTE: Only activate the relevent DOMWindow when the gestureType is an ActivationTriggering one
auto* window = document->domWindow();
if (window && gestureType == UserGestureType::ActivationTriggering)
window->notifyActivated(currentToken()->startTime());
}
}
UserGestureIndicator::UserGestureIndicator(RefPtr<UserGestureToken> token, UserGestureToken::GestureScope scope, UserGestureToken::IsPropagatedFromFetch isPropagatedFromFetch)
{
// Silently ignore UserGestureIndicators on non main threads.
if (!isMainThread())
return;
// It is only safe to use currentToken() on the main thread.
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(const 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;
}
std::optional<WTF::UUID> UserGestureIndicator::authorizationToken() const
{
if (!isMainThread())
return std::nullopt;
return currentToken() ? currentToken()->authorizationToken() : std::nullopt;
}
}
|