OLD | NEW |
1 /* | 1 /* |
2 * Copyright (C) 2010 Apple Inc. All rights reserved. | 2 * Copyright (C) 2010 Apple Inc. All rights reserved. |
3 * | 3 * |
4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
5 * modification, are permitted provided that the following conditions | 5 * modification, are permitted provided that the following conditions |
6 * are met: | 6 * are met: |
7 * 1. Redistributions of source code must retain the above copyright | 7 * 1. Redistributions of source code must retain the above copyright |
8 * notice, this list of conditions and the following disclaimer. | 8 * notice, this list of conditions and the following disclaimer. |
9 * 2. Redistributions in binary form must reproduce the above copyright | 9 * 2. Redistributions in binary form must reproduce the above copyright |
10 * notice, this list of conditions and the following disclaimer in the | 10 * notice, this list of conditions and the following disclaimer in the |
(...skipping 15 matching lines...) Expand all Loading... |
26 #ifndef UserGestureIndicator_h | 26 #ifndef UserGestureIndicator_h |
27 #define UserGestureIndicator_h | 27 #define UserGestureIndicator_h |
28 | 28 |
29 #include "platform/PlatformExport.h" | 29 #include "platform/PlatformExport.h" |
30 #include "platform/wtf/Noncopyable.h" | 30 #include "platform/wtf/Noncopyable.h" |
31 #include "platform/wtf/RefCounted.h" | 31 #include "platform/wtf/RefCounted.h" |
32 #include "platform/wtf/RefPtr.h" | 32 #include "platform/wtf/RefPtr.h" |
33 | 33 |
34 namespace blink { | 34 namespace blink { |
35 | 35 |
36 // Callback to be invoked when the state of a UserGestureIndicator is | |
37 // used (only during the scope of a UserGestureIndicator, does | |
38 // not flow with the UserGestureToken). It's the responsibility of the | |
39 // caller to ensure the UserGestureUtilizedCallback is kept alive as long | |
40 // as the UserGestureIndicator it's used in. | |
41 // Note that this doesn't currently track EVERY way in which the | |
42 // state of a UserGesture can be read (sometimes it's just propagated | |
43 // elsewhere, or otherwise read in a way that's hard to know if it will | |
44 // actually be used), but should include the primary use cases. Therefore | |
45 // this is suitable mainly for diagnostics and measurement purposes. | |
46 class PLATFORM_EXPORT UserGestureUtilizedCallback { | |
47 public: | |
48 virtual ~UserGestureUtilizedCallback() = default; | |
49 virtual void UserGestureUtilized() = 0; | |
50 }; | |
51 | |
52 // A UserGestureToken represents a user gesture. It can be referenced and saved | 36 // A UserGestureToken represents a user gesture. It can be referenced and saved |
53 // for later (see, e.g., DOMTimer, which propagates user gestures to the timer | 37 // for later (see, e.g., DOMTimer, which propagates user gestures to the timer |
54 // fire in certain situations). Passing it to a UserGestureIndicator will cause | 38 // fire in certain situations). Passing it to a UserGestureIndicator will cause |
55 // it to be considered as currently being processed. | 39 // it to be considered as currently being processed. |
56 class PLATFORM_EXPORT UserGestureToken : public RefCounted<UserGestureToken> { | 40 class PLATFORM_EXPORT UserGestureToken : public RefCounted<UserGestureToken> { |
57 WTF_MAKE_NONCOPYABLE(UserGestureToken); | 41 WTF_MAKE_NONCOPYABLE(UserGestureToken); |
58 | 42 |
59 public: | 43 public: |
60 enum Status { kNewGesture, kPossiblyExistingGesture }; | 44 enum Status { kNewGesture, kPossiblyExistingGesture }; |
61 enum TimeoutPolicy { kDefault, kOutOfProcess, kHasPaused }; | 45 enum TimeoutPolicy { kDefault, kOutOfProcess, kHasPaused }; |
62 | 46 |
63 ~UserGestureToken() {} | 47 ~UserGestureToken() {} |
64 bool HasGestures() const; | 48 bool HasGestures() const; |
65 void TransferGestureTo(UserGestureToken*); | 49 void TransferGestureTo(UserGestureToken*); |
66 bool ConsumeGesture(); | 50 bool ConsumeGesture(); |
67 void SetTimeoutPolicy(TimeoutPolicy); | 51 void SetTimeoutPolicy(TimeoutPolicy); |
68 void ResetTimestamp(); | 52 void ResetTimestamp(); |
69 | 53 |
70 // If this UserGestureToken is wrapped in a UserGestureIndicator, and the | |
71 // UserGestureIndicator is the lowest on the callstack (and therefore this | |
72 // UserGestureToken is UserGestureIndicator::s_rootToken), then the callback | |
73 // provided here will be called when this UserGestureToken is utilized. | |
74 // Calling setUserGestureUtilizedCallback() on a UserGestureToken that is not | |
75 // UserGestureIndicator::s_rootToken would be unsafe and never result in a | |
76 // callback, so it will fail a CHECK() instead. | |
77 void SetUserGestureUtilizedCallback(UserGestureUtilizedCallback*); | |
78 void UserGestureUtilized(); | |
79 | |
80 protected: | 54 protected: |
81 UserGestureToken(Status); | 55 UserGestureToken(Status); |
82 | 56 |
83 private: | 57 private: |
84 bool HasTimedOut() const; | 58 bool HasTimedOut() const; |
85 | 59 |
86 size_t consumable_gestures_; | 60 size_t consumable_gestures_; |
87 double timestamp_; | 61 double timestamp_; |
88 TimeoutPolicy timeout_policy_; | 62 TimeoutPolicy timeout_policy_; |
89 UserGestureUtilizedCallback* usage_callback_; | |
90 }; | 63 }; |
91 | 64 |
92 class PLATFORM_EXPORT UserGestureIndicator final { | 65 class PLATFORM_EXPORT UserGestureIndicator final { |
93 USING_FAST_MALLOC(UserGestureIndicator); | 66 USING_FAST_MALLOC(UserGestureIndicator); |
94 WTF_MAKE_NONCOPYABLE(UserGestureIndicator); | 67 WTF_MAKE_NONCOPYABLE(UserGestureIndicator); |
95 | 68 |
96 public: | 69 public: |
97 // Note: All *ThreadSafe methods are safe to call from any thread. Their | 70 // Note: All *ThreadSafe methods are safe to call from any thread. Their |
98 // non-suffixed counterparts *must* be called on the main thread. Consider | 71 // non-suffixed counterparts *must* be called on the main thread. Consider |
99 // always using the non-suffixed one unless the code really | 72 // always using the non-suffixed one unless the code really |
100 // needs to be thread-safe | 73 // needs to be thread-safe |
101 | 74 |
102 // Returns whether a user gesture is currently in progress. | 75 // Returns whether a user gesture is currently in progress. |
103 // Does not invoke the UserGestureUtilizedCallback. Consider calling | |
104 // utilizeUserGesture instead if you know for sure that the return value | |
105 // will have an effect. | |
106 static bool ProcessingUserGesture(); | 76 static bool ProcessingUserGesture(); |
107 static bool ProcessingUserGestureThreadSafe(); | 77 static bool ProcessingUserGestureThreadSafe(); |
108 | 78 |
109 // Indicates that a user gesture (if any) is being used, without preventing it | |
110 // from being used again. Returns whether a user gesture is currently in | |
111 // progress. If true, invokes (and then clears) any | |
112 // UserGestureUtilizedCallback. | |
113 static bool UtilizeUserGesture(); | |
114 | |
115 // Mark the current user gesture (if any) as having been used, such that | 79 // Mark the current user gesture (if any) as having been used, such that |
116 // it cannot be used again. This is done only for very security-sensitive | 80 // it cannot be used again. This is done only for very security-sensitive |
117 // operations like creating a new process. | 81 // operations like creating a new process. |
118 // Like utilizeUserGesture, may invoke/clear any UserGestureUtilizedCallback. | |
119 static bool ConsumeUserGesture(); | 82 static bool ConsumeUserGesture(); |
120 static bool ConsumeUserGestureThreadSafe(); | 83 static bool ConsumeUserGestureThreadSafe(); |
121 | 84 |
122 static UserGestureToken* CurrentToken(); | 85 static UserGestureToken* CurrentToken(); |
123 static UserGestureToken* CurrentTokenThreadSafe(); | 86 static UserGestureToken* CurrentTokenThreadSafe(); |
124 | 87 |
125 explicit UserGestureIndicator(PassRefPtr<UserGestureToken>); | 88 explicit UserGestureIndicator(PassRefPtr<UserGestureToken>); |
126 ~UserGestureIndicator(); | 89 ~UserGestureIndicator(); |
127 | 90 |
128 private: | 91 private: |
129 static UserGestureToken* root_token_; | 92 static UserGestureToken* root_token_; |
130 | 93 |
131 RefPtr<UserGestureToken> token_; | 94 RefPtr<UserGestureToken> token_; |
132 }; | 95 }; |
133 | 96 |
134 } // namespace blink | 97 } // namespace blink |
135 | 98 |
136 #endif | 99 #endif |
OLD | NEW |