#include "config.h"
#include "Worker.h"
#include "ContentSecurityPolicy.h"
#include "Event.h"
#include "EventNames.h"
#include "InspectorInstrumentation.h"
#include "NetworkStateNotifier.h"
#include "ResourceResponse.h"
#include "SecurityOrigin.h"
#include "WorkerGlobalScopeProxy.h"
#include "WorkerScriptLoader.h"
#include "WorkerThread.h"
#include <inspector/IdentifiersFactory.h>
#include <wtf/HashSet.h>
#include <wtf/MainThread.h>
namespace WebCore {
static HashSet<Worker*>* allWorkers;
void networkStateChanged(bool isOnLine)
{
for (auto& worker : *allWorkers)
worker->notifyNetworkStateChange(isOnLine);
}
inline Worker::Worker(ScriptExecutionContext& context, JSC::RuntimeFlags runtimeFlags)
: ActiveDOMObject(&context)
, m_identifier("worker:" + Inspector::IdentifiersFactory::createIdentifier())
, m_contextProxy(WorkerGlobalScopeProxy::create(this))
, m_runtimeFlags(runtimeFlags)
{
if (!allWorkers) {
allWorkers = new HashSet<Worker*>;
networkStateNotifier().addNetworkStateChangeListener(networkStateChanged);
}
HashSet<Worker*>::AddResult addResult = allWorkers->add(this);
ASSERT_UNUSED(addResult, addResult.isNewEntry);
}
ExceptionOr<Ref<Worker>> Worker::create(ScriptExecutionContext& context, const String& url, JSC::RuntimeFlags runtimeFlags)
{
ASSERT(isMainThread());
ASSERT_WITH_SECURITY_IMPLICATION(context.isDocument());
auto worker = adoptRef(*new Worker(context, runtimeFlags));
worker->suspendIfNeeded();
bool shouldBypassMainWorldContentSecurityPolicy = context.shouldBypassMainWorldContentSecurityPolicy();
auto scriptURL = worker->resolveURL(url, shouldBypassMainWorldContentSecurityPolicy);
if (scriptURL.hasException())
return scriptURL.releaseException();
worker->m_shouldBypassMainWorldContentSecurityPolicy = shouldBypassMainWorldContentSecurityPolicy;
worker->setPendingActivity(worker.ptr());
worker->m_scriptLoader = WorkerScriptLoader::create();
auto contentSecurityPolicyEnforcement = shouldBypassMainWorldContentSecurityPolicy ? ContentSecurityPolicyEnforcement::DoNotEnforce : ContentSecurityPolicyEnforcement::EnforceChildSrcDirective;
worker->m_scriptLoader->loadAsynchronously(&context, scriptURL.releaseReturnValue(), FetchOptions::Mode::SameOrigin, contentSecurityPolicyEnforcement, worker->m_identifier, worker.ptr());
return WTFMove(worker);
}
Worker::~Worker()
{
ASSERT(isMainThread());
ASSERT(scriptExecutionContext()); allWorkers->remove(this);
m_contextProxy->workerObjectDestroyed();
}
ExceptionOr<void> Worker::postMessage(JSC::ExecState& state, JSC::JSValue messageValue, Vector<JSC::Strong<JSC::JSObject>>&& transfer)
{
Vector<RefPtr<MessagePort>> ports;
auto message = SerializedScriptValue::create(state, messageValue, WTFMove(transfer), ports, SerializationContext::WorkerPostMessage);
if (message.hasException())
return message.releaseException();
auto channels = MessagePort::disentanglePorts(WTFMove(ports));
if (channels.hasException())
return channels.releaseException();
m_contextProxy->postMessageToWorkerGlobalScope(message.releaseReturnValue(), channels.releaseReturnValue());
return { };
}
void Worker::terminate()
{
m_contextProxy->terminateWorkerGlobalScope();
}
bool Worker::canSuspendForDocumentSuspension() const
{
return false;
}
const char* Worker::activeDOMObjectName() const
{
return "Worker";
}
void Worker::stop()
{
terminate();
}
bool Worker::hasPendingActivity() const
{
return m_contextProxy->hasPendingActivity() || ActiveDOMObject::hasPendingActivity();
}
void Worker::notifyNetworkStateChange(bool isOnLine)
{
m_contextProxy->notifyNetworkStateChange(isOnLine);
}
void Worker::didReceiveResponse(unsigned long identifier, const ResourceResponse& response)
{
const URL& responseURL = response.url();
if (!responseURL.protocolIsBlob() && !responseURL.protocolIs("file") && !SecurityOrigin::create(responseURL)->isUnique())
m_contentSecurityPolicyResponseHeaders = ContentSecurityPolicyResponseHeaders(response);
InspectorInstrumentation::didReceiveScriptResponse(scriptExecutionContext(), identifier);
}
void Worker::notifyFinished()
{
if (m_scriptLoader->failed())
dispatchEvent(Event::create(eventNames().errorEvent, false, true));
else {
const ContentSecurityPolicyResponseHeaders& contentSecurityPolicyResponseHeaders = m_contentSecurityPolicyResponseHeaders ? m_contentSecurityPolicyResponseHeaders.value() : scriptExecutionContext()->contentSecurityPolicy()->responseHeaders();
m_contextProxy->startWorkerGlobalScope(m_scriptLoader->url(), scriptExecutionContext()->userAgent(m_scriptLoader->url()), m_scriptLoader->script(), contentSecurityPolicyResponseHeaders, m_shouldBypassMainWorldContentSecurityPolicy, m_runtimeFlags);
InspectorInstrumentation::scriptImported(*scriptExecutionContext(), m_scriptLoader->identifier(), m_scriptLoader->script());
}
m_scriptLoader = nullptr;
unsetPendingActivity(this);
}
}