ScriptExecutionContext.cpp [plain text]
#include "config.h"
#include "ScriptExecutionContext.h"
#include "CachedScript.h"
#include "DOMTimer.h"
#include "ErrorEvent.h"
#include "MessagePort.h"
#include "PublicURLManager.h"
#include "Settings.h"
#include "WorkerGlobalScope.h"
#include "WorkerThread.h"
#include <inspector/ScriptCallStack.h>
#include <wtf/MainThread.h>
#include <wtf/Ref.h>
#include "JSDOMWindow.h"
#if PLATFORM(IOS)
#include "Document.h"
#endif
#if ENABLE(SQL_DATABASE)
#include "DatabaseContext.h"
#endif
using namespace Inspector;
namespace WebCore {
class ScriptExecutionContext::PendingException {
WTF_MAKE_NONCOPYABLE(PendingException);
public:
PendingException(const String& errorMessage, int lineNumber, int columnNumber, const String& sourceURL, PassRefPtr<ScriptCallStack> callStack)
: m_errorMessage(errorMessage)
, m_lineNumber(lineNumber)
, m_columnNumber(columnNumber)
, m_sourceURL(sourceURL)
, m_callStack(callStack)
{
}
String m_errorMessage;
int m_lineNumber;
int m_columnNumber;
String m_sourceURL;
RefPtr<ScriptCallStack> m_callStack;
};
ScriptExecutionContext::ScriptExecutionContext()
: m_circularSequentialID(0)
, m_inDispatchErrorEvent(false)
, m_activeDOMObjectsAreSuspended(false)
, m_reasonForSuspendingActiveDOMObjects(static_cast<ActiveDOMObject::ReasonForSuspension>(-1))
, m_activeDOMObjectsAreStopped(false)
, m_activeDOMObjectAdditionForbidden(false)
, m_timerNestingLevel(0)
#if !ASSERT_DISABLED
, m_inScriptExecutionContextDestructor(false)
, m_activeDOMObjectRemovalForbidden(false)
#endif
{
}
#if ASSERT_DISABLED
inline void ScriptExecutionContext::checkConsistency() const
{
}
#else
void ScriptExecutionContext::checkConsistency() const
{
for (auto* messagePort : m_messagePorts)
ASSERT(messagePort->scriptExecutionContext() == this);
for (auto* destructionObserver : m_destructionObservers)
ASSERT(destructionObserver->scriptExecutionContext() == this);
for (auto* activeDOMObject : m_activeDOMObjects) {
ASSERT(activeDOMObject->scriptExecutionContext() == this);
activeDOMObject->assertSuspendIfNeededWasCalled();
}
}
#endif
ScriptExecutionContext::~ScriptExecutionContext()
{
checkConsistency();
#if !ASSERT_DISABLED
m_inScriptExecutionContextDestructor = true;
#endif
while (auto* destructionObserver = m_destructionObservers.takeAny())
destructionObserver->contextDestroyed();
for (auto* messagePort : m_messagePorts)
messagePort->contextDestroyed();
#if !ASSERT_DISABLED
m_inScriptExecutionContextDestructor = false;
#endif
}
void ScriptExecutionContext::processMessagePortMessagesSoon()
{
postTask([] (ScriptExecutionContext& context) {
context.dispatchMessagePortEvents();
});
}
void ScriptExecutionContext::dispatchMessagePortEvents()
{
checkConsistency();
Ref<ScriptExecutionContext> protect(*this);
Vector<MessagePort*> possibleMessagePorts;
copyToVector(m_messagePorts, possibleMessagePorts);
for (auto* messagePort : possibleMessagePorts) {
if (m_messagePorts.contains(messagePort) && messagePort->started())
messagePort->dispatchMessages();
}
}
void ScriptExecutionContext::createdMessagePort(MessagePort& messagePort)
{
ASSERT((isDocument() && isMainThread())
|| (isWorkerGlobalScope() && currentThread() == toWorkerGlobalScope(this)->thread().threadID()));
m_messagePorts.add(&messagePort);
}
void ScriptExecutionContext::destroyedMessagePort(MessagePort& messagePort)
{
ASSERT((isDocument() && isMainThread())
|| (isWorkerGlobalScope() && currentThread() == toWorkerGlobalScope(this)->thread().threadID()));
m_messagePorts.remove(&messagePort);
}
void ScriptExecutionContext::didLoadResourceSynchronously(const ResourceRequest&)
{
}
bool ScriptExecutionContext::canSuspendActiveDOMObjects()
{
checkConsistency();
bool canSuspend = true;
m_activeDOMObjectAdditionForbidden = true;
#if !ASSERT_DISABLED
m_activeDOMObjectRemovalForbidden = true;
#endif
for (auto* activeDOMObject : m_activeDOMObjects) {
if (!activeDOMObject->canSuspend()) {
canSuspend = false;
break;
}
}
m_activeDOMObjectAdditionForbidden = false;
#if !ASSERT_DISABLED
m_activeDOMObjectRemovalForbidden = false;
#endif
return canSuspend;
}
void ScriptExecutionContext::suspendActiveDOMObjects(ActiveDOMObject::ReasonForSuspension why)
{
checkConsistency();
#if PLATFORM(IOS)
if (m_activeDOMObjectsAreSuspended) {
ASSERT(m_reasonForSuspendingActiveDOMObjects == ActiveDOMObject::DocumentWillBePaused);
return;
}
#endif
m_activeDOMObjectAdditionForbidden = true;
#if !ASSERT_DISABLED
m_activeDOMObjectRemovalForbidden = true;
#endif
for (auto* activeDOMObject : m_activeDOMObjects)
activeDOMObject->suspend(why);
m_activeDOMObjectAdditionForbidden = false;
#if !ASSERT_DISABLED
m_activeDOMObjectRemovalForbidden = false;
#endif
m_activeDOMObjectsAreSuspended = true;
m_reasonForSuspendingActiveDOMObjects = why;
}
void ScriptExecutionContext::resumeActiveDOMObjects(ActiveDOMObject::ReasonForSuspension why)
{
checkConsistency();
if (m_reasonForSuspendingActiveDOMObjects != why)
return;
m_activeDOMObjectsAreSuspended = false;
m_activeDOMObjectAdditionForbidden = true;
#if !ASSERT_DISABLED
m_activeDOMObjectRemovalForbidden = true;
#endif
for (auto* activeDOMObject : m_activeDOMObjects)
activeDOMObject->resume();
m_activeDOMObjectAdditionForbidden = false;
#if !ASSERT_DISABLED
m_activeDOMObjectRemovalForbidden = false;
#endif
}
void ScriptExecutionContext::stopActiveDOMObjects()
{
checkConsistency();
if (m_activeDOMObjectsAreStopped)
return;
m_activeDOMObjectsAreStopped = true;
Vector<ActiveDOMObject*> possibleActiveDOMObjects;
copyToVector(m_activeDOMObjects, possibleActiveDOMObjects);
m_activeDOMObjectAdditionForbidden = true;
for (auto* activeDOMObject : possibleActiveDOMObjects) {
if (!m_activeDOMObjects.contains(activeDOMObject))
continue;
activeDOMObject->stop();
}
m_activeDOMObjectAdditionForbidden = false;
for (auto* messagePort : m_messagePorts)
messagePort->close();
}
void ScriptExecutionContext::suspendActiveDOMObjectIfNeeded(ActiveDOMObject& activeDOMObject)
{
ASSERT(m_activeDOMObjects.contains(&activeDOMObject));
if (m_activeDOMObjectsAreSuspended)
activeDOMObject.suspend(m_reasonForSuspendingActiveDOMObjects);
if (m_activeDOMObjectsAreStopped)
activeDOMObject.stop();
}
void ScriptExecutionContext::didCreateActiveDOMObject(ActiveDOMObject& activeDOMObject)
{
ASSERT(!m_inScriptExecutionContextDestructor);
RELEASE_ASSERT(!m_activeDOMObjectAdditionForbidden);
m_activeDOMObjects.add(&activeDOMObject);
}
void ScriptExecutionContext::willDestroyActiveDOMObject(ActiveDOMObject& activeDOMObject)
{
ASSERT(!m_activeDOMObjectRemovalForbidden);
m_activeDOMObjects.remove(&activeDOMObject);
}
void ScriptExecutionContext::didCreateDestructionObserver(ContextDestructionObserver& observer)
{
ASSERT(!m_inScriptExecutionContextDestructor);
m_destructionObservers.add(&observer);
}
void ScriptExecutionContext::willDestroyDestructionObserver(ContextDestructionObserver& observer)
{
m_destructionObservers.remove(&observer);
}
bool ScriptExecutionContext::sanitizeScriptError(String& errorMessage, int& lineNumber, int& columnNumber, String& sourceURL, CachedScript* cachedScript)
{
URL targetURL = completeURL(sourceURL);
if (securityOrigin()->canRequest(targetURL) || (cachedScript && cachedScript->passesAccessControlCheck(*securityOrigin())))
return false;
errorMessage = "Script error.";
sourceURL = String();
lineNumber = 0;
columnNumber = 0;
return true;
}
void ScriptExecutionContext::reportException(const String& errorMessage, int lineNumber, int columnNumber, const String& sourceURL, PassRefPtr<ScriptCallStack> callStack, CachedScript* cachedScript)
{
if (m_inDispatchErrorEvent) {
if (!m_pendingExceptions)
m_pendingExceptions = std::make_unique<Vector<std::unique_ptr<PendingException>>>();
m_pendingExceptions->append(std::make_unique<PendingException>(errorMessage, lineNumber, columnNumber, sourceURL, callStack));
return;
}
if (!dispatchErrorEvent(errorMessage, lineNumber, columnNumber, sourceURL, cachedScript))
logExceptionToConsole(errorMessage, sourceURL, lineNumber, columnNumber, callStack);
if (!m_pendingExceptions)
return;
std::unique_ptr<Vector<std::unique_ptr<PendingException>>> pendingExceptions = WTF::move(m_pendingExceptions);
for (auto& exception : *pendingExceptions)
logExceptionToConsole(exception->m_errorMessage, exception->m_sourceURL, exception->m_lineNumber, exception->m_columnNumber, exception->m_callStack);
}
void ScriptExecutionContext::addConsoleMessage(MessageSource source, MessageLevel level, const String& message, const String& sourceURL, unsigned lineNumber, unsigned columnNumber, JSC::ExecState* state, unsigned long requestIdentifier)
{
addMessage(source, level, message, sourceURL, lineNumber, columnNumber, 0, state, requestIdentifier);
}
bool ScriptExecutionContext::dispatchErrorEvent(const String& errorMessage, int lineNumber, int columnNumber, const String& sourceURL, CachedScript* cachedScript)
{
EventTarget* target = errorEventTarget();
if (!target)
return false;
#if PLATFORM(IOS)
if (target == target->toDOMWindow() && isDocument()) {
Settings* settings = static_cast<Document*>(this)->settings();
if (settings && !settings->shouldDispatchJavaScriptWindowOnErrorEvents())
return false;
}
#endif
String message = errorMessage;
int line = lineNumber;
int column = columnNumber;
String sourceName = sourceURL;
sanitizeScriptError(message, line, column, sourceName, cachedScript);
ASSERT(!m_inDispatchErrorEvent);
m_inDispatchErrorEvent = true;
RefPtr<ErrorEvent> errorEvent = ErrorEvent::create(message, sourceName, line, column);
target->dispatchEvent(errorEvent);
m_inDispatchErrorEvent = false;
return errorEvent->defaultPrevented();
}
int ScriptExecutionContext::circularSequentialID()
{
++m_circularSequentialID;
if (m_circularSequentialID <= 0)
m_circularSequentialID = 1;
return m_circularSequentialID;
}
PublicURLManager& ScriptExecutionContext::publicURLManager()
{
if (!m_publicURLManager)
m_publicURLManager = PublicURLManager::create(this);
return *m_publicURLManager;
}
void ScriptExecutionContext::adjustMinimumTimerInterval(double oldMinimumTimerInterval)
{
if (minimumTimerInterval() != oldMinimumTimerInterval) {
for (auto& timer : m_timeouts.values())
timer->updateTimerIntervalIfNecessary();
}
}
double ScriptExecutionContext::minimumTimerInterval() const
{
return Settings::defaultMinDOMTimerInterval();
}
void ScriptExecutionContext::didChangeTimerAlignmentInterval()
{
for (auto& timer : m_timeouts.values())
timer->didChangeAlignmentInterval();
}
double ScriptExecutionContext::timerAlignmentInterval() const
{
return Settings::defaultDOMTimerAlignmentInterval();
}
JSC::VM& ScriptExecutionContext::vm()
{
if (isDocument())
return JSDOMWindow::commonVM();
return toWorkerGlobalScope(*this).script()->vm();
}
#if ENABLE(SQL_DATABASE)
void ScriptExecutionContext::setDatabaseContext(DatabaseContext* databaseContext)
{
ASSERT(!m_databaseContext);
m_databaseContext = databaseContext;
}
#endif
bool ScriptExecutionContext::hasPendingActivity() const
{
checkConsistency();
for (auto* activeDOMObject : m_activeDOMObjects) {
if (activeDOMObject->hasPendingActivity())
return true;
}
for (auto* messagePort : m_messagePorts) {
if (messagePort->hasPendingActivity())
return true;
}
return false;
}
}