This source file includes following definitions.
- create
- hasGestures
- addGesture
- resetTimestamp
- consumeGesture
- setOutOfProcess
- setJavascriptPrompt
- m_javascriptPrompt
- isDefinite
- processingUserGesture
- consumeUserGesture
- currentToken
- m_savedIndicator
#include "config.h"
#include "platform/UserGestureIndicator.h"
#include "wtf/Assertions.h"
#include "wtf/CurrentTime.h"
#include "wtf/MainThread.h"
namespace WebCore {
namespace {
const double userGestureTimeout = 1.0;
const double userGestureOutOfProcessTimeout = 10.0;
class GestureToken : public UserGestureToken {
public:
static PassRefPtr<UserGestureToken> create() { return adoptRef(new GestureToken); }
virtual ~GestureToken() { }
virtual bool hasGestures() const OVERRIDE
{
if (m_consumableGestures < 1 || (WTF::currentTime() - m_timestamp > (m_outOfProcess ? userGestureOutOfProcessTimeout : userGestureTimeout) && !m_javascriptPrompt))
return false;
return true;
}
void addGesture()
{
m_consumableGestures++;
m_timestamp = WTF::currentTime();
}
void resetTimestamp()
{
m_timestamp = WTF::currentTime();
}
bool consumeGesture()
{
if (!m_consumableGestures)
return false;
m_consumableGestures--;
return true;
}
virtual void setOutOfProcess() OVERRIDE
{
if (WTF::currentTime() - m_timestamp > userGestureTimeout)
return;
if (hasGestures())
m_outOfProcess = true;
}
virtual void setJavascriptPrompt() OVERRIDE
{
if (WTF::currentTime() - m_timestamp > userGestureTimeout)
return;
if (hasGestures())
m_javascriptPrompt = true;
}
private:
GestureToken()
: m_consumableGestures(0),
m_timestamp(0),
m_outOfProcess(false),
m_javascriptPrompt(false)
{
}
size_t m_consumableGestures;
double m_timestamp;
bool m_outOfProcess;
bool m_javascriptPrompt;
};
}
static bool isDefinite(ProcessingUserGestureState state)
{
return state == DefinitelyProcessingNewUserGesture || state == DefinitelyProcessingUserGesture || state == DefinitelyNotProcessingUserGesture;
}
ProcessingUserGestureState UserGestureIndicator::s_state = DefinitelyNotProcessingUserGesture;
UserGestureIndicator* UserGestureIndicator::s_topmostIndicator = 0;
UserGestureIndicator::UserGestureIndicator(ProcessingUserGestureState state)
: m_previousState(s_state)
{
if (!isMainThread())
return;
if (isDefinite(state)) {
if (!s_topmostIndicator) {
s_topmostIndicator = this;
m_token = GestureToken::create();
} else {
m_token = s_topmostIndicator->currentToken();
}
s_state = state;
}
if (state == DefinitelyProcessingNewUserGesture)
static_cast<GestureToken*>(m_token.get())->addGesture();
else if (state == DefinitelyProcessingUserGesture && s_topmostIndicator == this)
static_cast<GestureToken*>(m_token.get())->addGesture();
ASSERT(isDefinite(s_state));
}
UserGestureIndicator::UserGestureIndicator(PassRefPtr<UserGestureToken> token)
: m_previousState(s_state)
{
if (!isMainThread())
return;
if (token) {
static_cast<GestureToken*>(token.get())->resetTimestamp();
if (!s_topmostIndicator) {
s_topmostIndicator = this;
m_token = token;
} else {
m_token = s_topmostIndicator->currentToken();
if (static_cast<GestureToken*>(token.get())->hasGestures()) {
static_cast<GestureToken*>(m_token.get())->addGesture();
static_cast<GestureToken*>(token.get())->consumeGesture();
}
}
s_state = DefinitelyProcessingUserGesture;
}
ASSERT(isDefinite(s_state));
}
UserGestureIndicator::~UserGestureIndicator()
{
if (!isMainThread())
return;
s_state = m_previousState;
if (s_topmostIndicator == this)
s_topmostIndicator = 0;
ASSERT(isDefinite(s_state));
}
bool UserGestureIndicator::processingUserGesture()
{
if (!isMainThread())
return false;
return s_topmostIndicator && static_cast<GestureToken*>(s_topmostIndicator->currentToken())->hasGestures() && (s_state == DefinitelyProcessingNewUserGesture || s_state == DefinitelyProcessingUserGesture);
}
bool UserGestureIndicator::consumeUserGesture()
{
if (!isMainThread() || !s_topmostIndicator)
return false;
return static_cast<GestureToken*>(s_topmostIndicator->currentToken())->consumeGesture();
}
UserGestureToken* UserGestureIndicator::currentToken()
{
if (!isMainThread() || !s_topmostIndicator)
return 0;
return s_topmostIndicator->m_token.get();
}
UserGestureIndicatorDisabler::UserGestureIndicatorDisabler()
: m_savedState(UserGestureIndicator::s_state)
, m_savedIndicator(UserGestureIndicator::s_topmostIndicator)
{
RELEASE_ASSERT(isMainThread());
UserGestureIndicator::s_state = DefinitelyNotProcessingUserGesture;
UserGestureIndicator::s_topmostIndicator = 0;
}
UserGestureIndicatorDisabler::~UserGestureIndicatorDisabler()
{
RELEASE_ASSERT(isMainThread());
UserGestureIndicator::s_state = m_savedState;
UserGestureIndicator::s_topmostIndicator = m_savedIndicator;
}
}