1/*
2 * Copyright (C) 2010-2017 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#pragma once
27
28#include "DOMPasteAccess.h"
29#include <wtf/Function.h>
30#include <wtf/MonotonicTime.h>
31#include <wtf/Noncopyable.h>
32#include <wtf/RefCounted.h>
33#include <wtf/Vector.h>
34
35namespace WebCore {
36
37class Document;
38
39enum ProcessingUserGestureState {
40 ProcessingUserGesture,
41 ProcessingPotentialUserGesture,
42 NotProcessingUserGesture
43};
44
45enum class UserGestureType { EscapeKey, Other };
46
47class UserGestureToken : public RefCounted<UserGestureToken> {
48public:
49 static Ref<UserGestureToken> create(ProcessingUserGestureState state, UserGestureType gestureType)
50 {
51 return adoptRef(*new UserGestureToken(state, gestureType));
52 }
53
54 WEBCORE_EXPORT ~UserGestureToken();
55
56 ProcessingUserGestureState state() const { return m_state; }
57 bool processingUserGesture() const { return m_scope == GestureScope::All && m_state == ProcessingUserGesture; }
58 bool processingUserGestureForMedia() const { return m_state == ProcessingUserGesture || m_state == ProcessingPotentialUserGesture; }
59 UserGestureType gestureType() const { return m_gestureType; }
60
61 void addDestructionObserver(WTF::Function<void (UserGestureToken&)>&& observer)
62 {
63 m_destructionObservers.append(WTFMove(observer));
64 }
65
66 DOMPasteAccessPolicy domPasteAccessPolicy() const { return m_domPasteAccessPolicy; }
67 void didRequestDOMPasteAccess(DOMPasteAccessResponse response)
68 {
69 switch (response) {
70 case DOMPasteAccessResponse::DeniedForGesture:
71 m_domPasteAccessPolicy = DOMPasteAccessPolicy::Denied;
72 break;
73 case DOMPasteAccessResponse::GrantedForCommand:
74 break;
75 case DOMPasteAccessResponse::GrantedForGesture:
76 m_domPasteAccessPolicy = DOMPasteAccessPolicy::Granted;
77 break;
78 }
79 }
80 void resetDOMPasteAccess() { m_domPasteAccessPolicy = DOMPasteAccessPolicy::NotRequestedYet; }
81
82 enum class GestureScope { All, MediaOnly };
83 void setScope(GestureScope scope) { m_scope = scope; }
84 void resetScope() { m_scope = GestureScope::All; }
85
86 bool hasExpired(Seconds expirationInterval) const
87 {
88 return m_startTime + expirationInterval < MonotonicTime::now();
89 }
90
91private:
92 UserGestureToken(ProcessingUserGestureState state, UserGestureType gestureType)
93 : m_state(state)
94 , m_gestureType(gestureType)
95 {
96 }
97
98 ProcessingUserGestureState m_state = NotProcessingUserGesture;
99 Vector<WTF::Function<void (UserGestureToken&)>> m_destructionObservers;
100 UserGestureType m_gestureType;
101 DOMPasteAccessPolicy m_domPasteAccessPolicy { DOMPasteAccessPolicy::NotRequestedYet };
102 GestureScope m_scope { GestureScope::All };
103 MonotonicTime m_startTime { MonotonicTime::now() };
104};
105
106class UserGestureIndicator {
107 WTF_MAKE_FAST_ALLOCATED;
108 WTF_MAKE_NONCOPYABLE(UserGestureIndicator);
109public:
110 WEBCORE_EXPORT static RefPtr<UserGestureToken> currentUserGesture();
111
112 WEBCORE_EXPORT static bool processingUserGesture();
113 WEBCORE_EXPORT static bool processingUserGestureForMedia();
114
115 // If a document is provided, its last known user gesture timestamp is updated.
116 enum class ProcessInteractionStyle { Immediate, Delayed };
117 WEBCORE_EXPORT explicit UserGestureIndicator(Optional<ProcessingUserGestureState>, Document* = nullptr, UserGestureType = UserGestureType::Other, ProcessInteractionStyle = ProcessInteractionStyle::Immediate);
118 WEBCORE_EXPORT explicit UserGestureIndicator(RefPtr<UserGestureToken>, UserGestureToken::GestureScope = UserGestureToken::GestureScope::All);
119 WEBCORE_EXPORT ~UserGestureIndicator();
120
121private:
122 RefPtr<UserGestureToken> m_previousToken;
123};
124
125}
126