ScriptDisallowedScope.h [plain text]
#pragma once
#include "ContainerNode.h"
#include <wtf/MainThread.h>
namespace WebCore {
class ScriptDisallowedScope {
public:
ScriptDisallowedScope()
{
if (!isMainThread())
return;
++s_count;
}
ScriptDisallowedScope(const ScriptDisallowedScope&)
: ScriptDisallowedScope()
{
}
~ScriptDisallowedScope()
{
if (!isMainThread())
return;
ASSERT(s_count);
s_count--;
}
static bool isEventAllowedInMainThread()
{
return !isMainThread() || !s_count;
}
class InMainThread {
public:
InMainThread()
{
ASSERT(isMainThread());
++s_count;
}
~InMainThread()
{
ASSERT(isMainThread());
ASSERT(s_count);
--s_count;
}
static bool isEventDispatchAllowedInSubtree(Node& node)
{
#if !ASSERT_DISABLED || ENABLE(SECURITY_ASSERTIONS)
return isScriptAllowed() || EventAllowedScope::isAllowedNode(node);
#else
UNUSED_PARAM(node);
return true;
#endif
}
static bool isScriptAllowed()
{
ASSERT(isMainThread());
return !s_count;
}
};
#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
class DisableAssertionsInScope {
public:
DisableAssertionsInScope()
{
ASSERT(isMainThread());
std::swap(s_count, m_originalCount);
}
~DisableAssertionsInScope()
{
s_count = m_originalCount;
}
private:
unsigned m_originalCount { 0 };
};
class LayoutAssertionDisableScope {
public:
LayoutAssertionDisableScope()
{
s_layoutAssertionDisableCount++;
}
~LayoutAssertionDisableScope()
{
s_layoutAssertionDisableCount--;
}
static bool shouldDisable() { return s_layoutAssertionDisableCount; }
private:
static unsigned s_layoutAssertionDisableCount;
};
private:
WEBCORE_EXPORT static unsigned s_count;
};
}