| 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 "wtf/Noncopyable.h" | 30 #include "wtf/Noncopyable.h" |
| 31 #include "wtf/RefCounted.h" | 31 #include "wtf/RefCounted.h" |
| 32 #include "wtf/RefPtr.h" | 32 #include "wtf/RefPtr.h" |
| 33 | 33 |
| 34 namespace blink { | 34 namespace blink { |
| 35 | 35 |
| 36 enum ProcessingUserGestureState { | |
| 37 DefinitelyProcessingNewUserGesture, | |
| 38 DefinitelyProcessingUserGesture, | |
| 39 PossiblyProcessingUserGesture, | |
| 40 DefinitelyNotProcessingUserGesture | |
| 41 }; | |
| 42 | |
| 43 class PLATFORM_EXPORT UserGestureToken : public RefCounted<UserGestureToken> { | |
| 44 WTF_MAKE_NONCOPYABLE(UserGestureToken); | |
| 45 | |
| 46 public: | |
| 47 UserGestureToken() {} | |
| 48 virtual ~UserGestureToken() {} | |
| 49 virtual bool hasGestures() const = 0; | |
| 50 virtual void setOutOfProcess() = 0; | |
| 51 virtual void setJavascriptPrompt() = 0; | |
| 52 virtual void setPauseInDebugger() = 0; | |
| 53 }; | |
| 54 | |
| 55 // Callback to be invoked when the state of a UserGestureIndicator is | 36 // Callback to be invoked when the state of a UserGestureIndicator is |
| 56 // used (only during the scope of a UserGestureIndicator, does | 37 // used (only during the scope of a UserGestureIndicator, does |
| 57 // not flow with the UserGestureToken). It's the responsibility of the | 38 // not flow with the UserGestureToken). It's the responsibility of the |
| 58 // caller to ensure the UserGestureUtilizedCallback is kept alive as long | 39 // caller to ensure the UserGestureUtilizedCallback is kept alive as long |
| 59 // as the UserGestureIndicator it's used in. | 40 // as the UserGestureIndicator it's used in. |
| 60 // Note that this doesn't currently track EVERY way in which the | 41 // Note that this doesn't currently track EVERY way in which the |
| 61 // state of a UserGesture can be read (sometimes it's just propagated | 42 // state of a UserGesture can be read (sometimes it's just propagated |
| 62 // elsewhere, or otherwise read in a way that's hard to know if it will | 43 // elsewhere, or otherwise read in a way that's hard to know if it will |
| 63 // actually be used), but should include the primary use cases. Therefore | 44 // actually be used), but should include the primary use cases. Therefore |
| 64 // this is suitable mainly for diagnostics and measurement purposes. | 45 // this is suitable mainly for diagnostics and measurement purposes. |
| 65 class PLATFORM_EXPORT UserGestureUtilizedCallback { | 46 class PLATFORM_EXPORT UserGestureUtilizedCallback { |
| 66 public: | 47 public: |
| 67 virtual ~UserGestureUtilizedCallback() = default; | 48 virtual ~UserGestureUtilizedCallback() = default; |
| 68 virtual void userGestureUtilized() = 0; | 49 virtual void userGestureUtilized() = 0; |
| 69 }; | 50 }; |
| 70 | 51 |
| 52 // 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 |
| 54 // fire in certain situations). Passing it to a UserGestureIndicator will cause |
| 55 // it to be considered as currently being processed. |
| 56 class PLATFORM_EXPORT UserGestureToken : public RefCounted<UserGestureToken> { |
| 57 WTF_MAKE_NONCOPYABLE(UserGestureToken); |
| 58 |
| 59 public: |
| 60 enum Status { NewGesture, PossiblyExistingGesture }; |
| 61 enum TimeoutPolicy { Default, OutOfProcess, HasPaused }; |
| 62 |
| 63 static PassRefPtr<UserGestureToken> create( |
| 64 Status status = PossiblyExistingGesture) { |
| 65 return adoptRef(new UserGestureToken(status)); |
| 66 } |
| 67 |
| 68 ~UserGestureToken() {} |
| 69 bool hasGestures(); |
| 70 void transferGestureTo(UserGestureToken*); |
| 71 bool consumeGesture(); |
| 72 void setTimeoutPolicy(TimeoutPolicy); |
| 73 |
| 74 // If this UserGestureToken is wrapped in a UserGestureIndicator, and the |
| 75 // UserGestureIndicator is the lowest on the callstack (and therefore this |
| 76 // UserGestureToken is UserGestureIndicator::s_rootToken), then the callback |
| 77 // provided here will be called when this UserGestureToken is utilized. |
| 78 // Calling setUserGestureUtilizedCallback() on a UserGestureToken that is not |
| 79 // UserGestureIndicator::s_rootToken would be unsafe and never result in a |
| 80 // callback, so it will fail a CHECK() instead. |
| 81 void setUserGestureUtilizedCallback(UserGestureUtilizedCallback*); |
| 82 void userGestureUtilized(); |
| 83 |
| 84 private: |
| 85 UserGestureToken(Status); |
| 86 |
| 87 bool hasTimedOut() const; |
| 88 |
| 89 size_t m_consumableGestures; |
| 90 double m_timestamp; |
| 91 TimeoutPolicy m_timeoutPolicy; |
| 92 UserGestureUtilizedCallback* m_usageCallback; |
| 93 }; |
| 94 |
| 71 class PLATFORM_EXPORT UserGestureIndicator final { | 95 class PLATFORM_EXPORT UserGestureIndicator final { |
| 72 USING_FAST_MALLOC(UserGestureIndicator); | 96 USING_FAST_MALLOC(UserGestureIndicator); |
| 73 WTF_MAKE_NONCOPYABLE(UserGestureIndicator); | 97 WTF_MAKE_NONCOPYABLE(UserGestureIndicator); |
| 74 | 98 |
| 75 public: | 99 public: |
| 76 // Returns whether a user gesture is currently in progress. | 100 // Returns whether a user gesture is currently in progress. |
| 77 // Does not invoke the UserGestureUtilizedCallback. Consider calling | 101 // Does not invoke the UserGestureUtilizedCallback. Consider calling |
| 78 // utilizeUserGesture instead if you know for sure that the return value | 102 // utilizeUserGesture instead if you know for sure that the return value |
| 79 // will have an effect. | 103 // will have an effect. |
| 80 static bool processingUserGesture(); | 104 static bool processingUserGesture(); |
| (...skipping 11 matching lines...) Expand all Loading... |
| 92 static bool consumeUserGesture(); | 116 static bool consumeUserGesture(); |
| 93 | 117 |
| 94 static UserGestureToken* currentToken(); | 118 static UserGestureToken* currentToken(); |
| 95 | 119 |
| 96 // Reset the notion of "since load". | 120 // Reset the notion of "since load". |
| 97 static void clearProcessedUserGestureSinceLoad(); | 121 static void clearProcessedUserGestureSinceLoad(); |
| 98 | 122 |
| 99 // Returns whether a user gesture has occurred since page load. | 123 // Returns whether a user gesture has occurred since page load. |
| 100 static bool processedUserGestureSinceLoad(); | 124 static bool processedUserGestureSinceLoad(); |
| 101 | 125 |
| 102 explicit UserGestureIndicator(ProcessingUserGestureState, | 126 explicit UserGestureIndicator(PassRefPtr<UserGestureToken>); |
| 103 UserGestureUtilizedCallback* = 0); | |
| 104 explicit UserGestureIndicator(PassRefPtr<UserGestureToken>, | |
| 105 UserGestureUtilizedCallback* = 0); | |
| 106 ~UserGestureIndicator(); | 127 ~UserGestureIndicator(); |
| 107 | 128 |
| 108 private: | 129 private: |
| 109 static ProcessingUserGestureState s_state; | |
| 110 static UserGestureIndicator* s_topmostIndicator; | |
| 111 static bool s_processedUserGestureSinceLoad; | 130 static bool s_processedUserGestureSinceLoad; |
| 112 ProcessingUserGestureState m_previousState; | 131 static UserGestureToken* s_rootToken; |
| 132 |
| 113 RefPtr<UserGestureToken> m_token; | 133 RefPtr<UserGestureToken> m_token; |
| 114 UserGestureUtilizedCallback* m_usageCallback; | |
| 115 }; | 134 }; |
| 116 | 135 |
| 117 } // namespace blink | 136 } // namespace blink |
| 118 | 137 |
| 119 #endif | 138 #endif |
| OLD | NEW |