ActiveDOMObject.cpp [plain text]
#include "config.h"
#include "ActiveDOMObject.h"
#include "Document.h"
#include "EventLoop.h"
#include "ScriptExecutionContext.h"
namespace WebCore {
static inline ScriptExecutionContext* suitableScriptExecutionContext(ScriptExecutionContext* scriptExecutionContext)
{
return is<Document>(scriptExecutionContext) ? &downcast<Document>(*scriptExecutionContext).contextDocument() : scriptExecutionContext;
}
inline ActiveDOMObject::ActiveDOMObject(ScriptExecutionContext* context, CheckedScriptExecutionContextType)
: ContextDestructionObserver(context)
{
ASSERT(!is<Document>(context) || &downcast<Document>(context)->contextDocument() == downcast<Document>(context));
if (!context)
return;
ASSERT(context->isContextThread());
context->didCreateActiveDOMObject(*this);
}
ActiveDOMObject::ActiveDOMObject(ScriptExecutionContext* scriptExecutionContext)
: ActiveDOMObject(suitableScriptExecutionContext(scriptExecutionContext), CheckedScriptExecutionContext)
{
}
ActiveDOMObject::ActiveDOMObject(Document* document)
: ActiveDOMObject(document ? &document->contextDocument() : nullptr, CheckedScriptExecutionContext)
{
}
ActiveDOMObject::ActiveDOMObject(Document& document)
: ActiveDOMObject(&document.contextDocument(), CheckedScriptExecutionContext)
{
}
ActiveDOMObject::~ActiveDOMObject()
{
ASSERT(canCurrentThreadAccessThreadLocalData(m_creationThread));
if (!m_scriptExecutionContext)
return;
ASSERT(m_suspendIfNeededWasCalled);
ASSERT(m_scriptExecutionContext->isContextThread());
m_scriptExecutionContext->willDestroyActiveDOMObject(*this);
}
void ActiveDOMObject::suspendIfNeeded()
{
#if ASSERT_ENABLED
ASSERT(!m_suspendIfNeededWasCalled);
m_suspendIfNeededWasCalled = true;
#endif
if (!m_scriptExecutionContext)
return;
m_scriptExecutionContext->suspendActiveDOMObjectIfNeeded(*this);
}
#if ASSERT_ENABLED
void ActiveDOMObject::assertSuspendIfNeededWasCalled() const
{
if (!m_suspendIfNeededWasCalled)
WTFLogAlways("Failed to call suspendIfNeeded() for %s", activeDOMObjectName());
ASSERT(m_suspendIfNeededWasCalled);
}
#endif // ASSERT_ENABLED
void ActiveDOMObject::suspend(ReasonForSuspension)
{
}
void ActiveDOMObject::resume()
{
}
void ActiveDOMObject::stop()
{
}
bool ActiveDOMObject::isContextStopped() const
{
return !scriptExecutionContext() || scriptExecutionContext()->activeDOMObjectsAreStopped();
}
bool ActiveDOMObject::isAllowedToRunScript() const
{
return scriptExecutionContext() && !scriptExecutionContext()->activeDOMObjectsAreStopped() && !scriptExecutionContext()->activeDOMObjectsAreSuspended();
}
void ActiveDOMObject::queueTaskInEventLoop(TaskSource source, Function<void ()>&& function)
{
auto* context = scriptExecutionContext();
if (!context)
return;
context->eventLoop().queueTask(source, WTFMove(function));
}
class ActiveDOMObjectEventDispatchTask : public EventLoopTask {
public:
ActiveDOMObjectEventDispatchTask(TaskSource source, EventLoopTaskGroup& group, ActiveDOMObject& object, EventTarget& target, Ref<Event>&& event)
: EventLoopTask(source, group)
, m_object(object)
, m_target(target)
, m_event(WTFMove(event))
{
++m_object.m_pendingActivityInstanceCount;
}
~ActiveDOMObjectEventDispatchTask()
{
ASSERT(m_object.m_pendingActivityInstanceCount);
--m_object.m_pendingActivityInstanceCount;
}
void execute() final
{
if (m_object.isAllowedToRunScript())
m_target->dispatchEvent(m_event.get());
}
private:
ActiveDOMObject& m_object;
Ref<EventTarget> m_target;
Ref<Event> m_event;
};
void ActiveDOMObject::queueTaskToDispatchEventInternal(EventTarget& target, TaskSource source, Ref<Event>&& event)
{
ASSERT(!event->target() || &target == event->target());
auto* context = scriptExecutionContext();
if (!context)
return;
auto& eventLoopTaskGroup = context->eventLoop();
auto task = makeUnique<ActiveDOMObjectEventDispatchTask>(source, eventLoopTaskGroup, *this, target, WTFMove(event));
eventLoopTaskGroup.queueTask(WTFMove(task));
}
}