| 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 23 matching lines...) Expand all Loading... |
| 34 | 34 |
| 35 // User gestures timeout in 1 second. | 35 // User gestures timeout in 1 second. |
| 36 const double kUserGestureTimeout = 1.0; | 36 const double kUserGestureTimeout = 1.0; |
| 37 | 37 |
| 38 // For out of process tokens we allow a 10 second delay. | 38 // For out of process tokens we allow a 10 second delay. |
| 39 const double kUserGestureOutOfProcessTimeout = 10.0; | 39 const double kUserGestureOutOfProcessTimeout = 10.0; |
| 40 | 40 |
| 41 UserGestureToken::UserGestureToken(Status status) | 41 UserGestureToken::UserGestureToken(Status status) |
| 42 : consumable_gestures_(0), | 42 : consumable_gestures_(0), |
| 43 timestamp_(WTF::CurrentTime()), | 43 timestamp_(WTF::CurrentTime()), |
| 44 timeout_policy_(kDefault), | 44 timeout_policy_(kDefault) { |
| 45 usage_callback_(nullptr) { | |
| 46 if (status == kNewGesture || !UserGestureIndicator::CurrentTokenThreadSafe()) | 45 if (status == kNewGesture || !UserGestureIndicator::CurrentTokenThreadSafe()) |
| 47 consumable_gestures_++; | 46 consumable_gestures_++; |
| 48 } | 47 } |
| 49 | 48 |
| 50 bool UserGestureToken::HasGestures() const { | 49 bool UserGestureToken::HasGestures() const { |
| 51 return consumable_gestures_ && !HasTimedOut(); | 50 return consumable_gestures_ && !HasTimedOut(); |
| 52 } | 51 } |
| 53 | 52 |
| 54 void UserGestureToken::TransferGestureTo(UserGestureToken* other) { | 53 void UserGestureToken::TransferGestureTo(UserGestureToken* other) { |
| 55 if (!HasGestures()) | 54 if (!HasGestures()) |
| (...skipping 20 matching lines...) Expand all Loading... |
| 76 | 75 |
| 77 bool UserGestureToken::HasTimedOut() const { | 76 bool UserGestureToken::HasTimedOut() const { |
| 78 if (timeout_policy_ == kHasPaused) | 77 if (timeout_policy_ == kHasPaused) |
| 79 return false; | 78 return false; |
| 80 double timeout = timeout_policy_ == kOutOfProcess | 79 double timeout = timeout_policy_ == kOutOfProcess |
| 81 ? kUserGestureOutOfProcessTimeout | 80 ? kUserGestureOutOfProcessTimeout |
| 82 : kUserGestureTimeout; | 81 : kUserGestureTimeout; |
| 83 return WTF::CurrentTime() - timestamp_ > timeout; | 82 return WTF::CurrentTime() - timestamp_ > timeout; |
| 84 } | 83 } |
| 85 | 84 |
| 86 void UserGestureToken::SetUserGestureUtilizedCallback( | |
| 87 UserGestureUtilizedCallback* callback) { | |
| 88 CHECK(this == UserGestureIndicator::CurrentToken()); | |
| 89 usage_callback_ = callback; | |
| 90 } | |
| 91 | |
| 92 void UserGestureToken::UserGestureUtilized() { | |
| 93 if (usage_callback_) { | |
| 94 usage_callback_->UserGestureUtilized(); | |
| 95 usage_callback_ = nullptr; | |
| 96 } | |
| 97 } | |
| 98 | |
| 99 // This enum is used in a histogram, so its values shouldn't change. | 85 // This enum is used in a histogram, so its values shouldn't change. |
| 100 enum GestureMergeState { | 86 enum GestureMergeState { |
| 101 kOldTokenHasGesture = 1 << 0, | 87 kOldTokenHasGesture = 1 << 0, |
| 102 kNewTokenHasGesture = 1 << 1, | 88 kNewTokenHasGesture = 1 << 1, |
| 103 kGestureMergeStateEnd = 1 << 2, | 89 kGestureMergeStateEnd = 1 << 2, |
| 104 }; | 90 }; |
| 105 | 91 |
| 106 // Remove this when user gesture propagation is standardized. See | 92 // Remove this when user gesture propagation is standardized. See |
| 107 // https://crbug.com/404161. | 93 // https://crbug.com/404161. |
| 108 static void RecordUserGestureMerge(const UserGestureToken& old_token, | 94 static void RecordUserGestureMerge(const UserGestureToken& old_token, |
| (...skipping 20 matching lines...) Expand all Loading... |
| 129 if (!root_token_) { | 115 if (!root_token_) { |
| 130 root_token_ = token_.Get(); | 116 root_token_ = token_.Get(); |
| 131 } else { | 117 } else { |
| 132 RecordUserGestureMerge(*root_token_, *token_); | 118 RecordUserGestureMerge(*root_token_, *token_); |
| 133 token_->TransferGestureTo(root_token_); | 119 token_->TransferGestureTo(root_token_); |
| 134 } | 120 } |
| 135 token_->ResetTimestamp(); | 121 token_->ResetTimestamp(); |
| 136 } | 122 } |
| 137 | 123 |
| 138 UserGestureIndicator::~UserGestureIndicator() { | 124 UserGestureIndicator::~UserGestureIndicator() { |
| 139 if (IsMainThread() && token_ && token_ == root_token_) { | 125 if (IsMainThread() && token_ && token_ == root_token_) |
| 140 root_token_->SetUserGestureUtilizedCallback(nullptr); | |
| 141 root_token_ = nullptr; | 126 root_token_ = nullptr; |
| 142 } | |
| 143 } | |
| 144 | |
| 145 // static | |
| 146 bool UserGestureIndicator::UtilizeUserGesture() { | |
| 147 if (UserGestureIndicator::ProcessingUserGesture()) { | |
| 148 root_token_->UserGestureUtilized(); | |
| 149 return true; | |
| 150 } | |
| 151 return false; | |
| 152 } | 127 } |
| 153 | 128 |
| 154 bool UserGestureIndicator::ProcessingUserGesture() { | 129 bool UserGestureIndicator::ProcessingUserGesture() { |
| 155 if (auto* token = CurrentToken()) | 130 if (auto* token = CurrentToken()) |
| 156 return token->HasGestures(); | 131 return token->HasGestures(); |
| 157 return false; | 132 return false; |
| 158 } | 133 } |
| 159 | 134 |
| 160 bool UserGestureIndicator::ProcessingUserGestureThreadSafe() { | 135 bool UserGestureIndicator::ProcessingUserGestureThreadSafe() { |
| 161 return IsMainThread() && ProcessingUserGesture(); | 136 return IsMainThread() && ProcessingUserGesture(); |
| 162 } | 137 } |
| 163 | 138 |
| 164 // static | 139 // static |
| 165 bool UserGestureIndicator::ConsumeUserGesture() { | 140 bool UserGestureIndicator::ConsumeUserGesture() { |
| 166 if (auto* token = CurrentToken()) { | 141 if (auto* token = CurrentToken()) { |
| 167 if (token->ConsumeGesture()) { | 142 if (token->ConsumeGesture()) |
| 168 token->UserGestureUtilized(); | |
| 169 return true; | 143 return true; |
| 170 } | |
| 171 } | 144 } |
| 172 return false; | 145 return false; |
| 173 } | 146 } |
| 174 | 147 |
| 175 bool UserGestureIndicator::ConsumeUserGestureThreadSafe() { | 148 bool UserGestureIndicator::ConsumeUserGestureThreadSafe() { |
| 176 return IsMainThread() && ConsumeUserGesture(); | 149 return IsMainThread() && ConsumeUserGesture(); |
| 177 } | 150 } |
| 178 | 151 |
| 179 UserGestureToken* UserGestureIndicator::CurrentToken() { | 152 UserGestureToken* UserGestureIndicator::CurrentToken() { |
| 180 DCHECK(IsMainThread()); | 153 DCHECK(IsMainThread()); |
| 181 return root_token_; | 154 return root_token_; |
| 182 } | 155 } |
| 183 | 156 |
| 184 UserGestureToken* UserGestureIndicator::CurrentTokenThreadSafe() { | 157 UserGestureToken* UserGestureIndicator::CurrentTokenThreadSafe() { |
| 185 return IsMainThread() ? CurrentToken() : nullptr; | 158 return IsMainThread() ? CurrentToken() : nullptr; |
| 186 } | 159 } |
| 187 | 160 |
| 188 } // namespace blink | 161 } // namespace blink |
| OLD | NEW |