NoEventDispatchAssertion.h [plain text]
#pragma once
#include "ContainerNode.h"
#include <wtf/MainThread.h>
namespace WebCore {
class NoEventDispatchAssertion {
public:
NoEventDispatchAssertion()
{
#if !ASSERT_DISABLED
if (!isMainThread())
return;
++s_count;
#endif
}
NoEventDispatchAssertion(const NoEventDispatchAssertion&)
: NoEventDispatchAssertion()
{
}
~NoEventDispatchAssertion()
{
#if !ASSERT_DISABLED
if (!isMainThread())
return;
ASSERT(s_count);
s_count--;
#endif
}
static bool isEventAllowedInMainThread()
{
#if ASSERT_DISABLED
return true;
#else
return !isMainThread() || !s_count;
#endif
}
static bool isEventDispatchAllowedInSubtree(Node& node)
{
return isEventAllowedInMainThread() || EventAllowedScope::isAllowedNode(node);
}
#if !ASSERT_DISABLED
class EventAllowedScope {
public:
explicit EventAllowedScope(ContainerNode& userAgentContentRoot)
: m_eventAllowedTreeRoot(userAgentContentRoot)
, m_previousScope(s_currentScope)
{
s_currentScope = this;
}
~EventAllowedScope()
{
s_currentScope = m_previousScope;
}
static bool isAllowedNode(Node& node)
{
return s_currentScope && s_currentScope->isAllowedNodeInternal(node);
}
private:
bool isAllowedNodeInternal(Node& node)
{
return m_eventAllowedTreeRoot->contains(&node) || (m_previousScope && m_previousScope->isAllowedNodeInternal(node));
}
Ref<ContainerNode> m_eventAllowedTreeRoot;
EventAllowedScope* m_previousScope;
static EventAllowedScope* s_currentScope;
};
#else
class EventAllowedScope {
public:
explicit EventAllowedScope(ContainerNode&) { }
static bool isAllowedNode(Node&) { return true; }
};
#endif
#if !ASSERT_DISABLED
class DisableAssertionsInScope {
public:
DisableAssertionsInScope()
{
if (!isMainThread())
return;
s_existingCount = s_count;
s_count = 0;
}
~DisableAssertionsInScope()
{
s_count = s_existingCount;
s_existingCount = 0;
}
private:
WEBCORE_EXPORT static unsigned s_existingCount;
};
#else
class DisableAssertionsInScope {
public:
DisableAssertionsInScope() { }
};
#endif
#if !ASSERT_DISABLED
private:
WEBCORE_EXPORT static unsigned s_count;
#endif
};
}