This source file includes following definitions.
- create
- m_channels
- performTask
- m_workerClients
- startWorkerGlobalScope
- postMessageToWorkerObject
- postMessageToWorkerGlobalScope
- postTaskToWorkerGlobalScope
- postTaskToLoader
- reportException
- reportConsoleMessage
- workerThreadCreated
- workerObjectDestroyed
- workerObjectDestroyedInternal
- connectToWorkerGlobalScopeInspectorTask
- connectToInspector
- disconnectFromWorkerGlobalScopeInspectorTask
- disconnectFromInspector
- dispatchOnInspectorBackendTask
- sendMessageToInspector
- workerGlobalScopeDestroyed
- terminateWorkerGlobalScope
- postMessageToPageInspector
- confirmMessageFromWorkerObject
- reportPendingActivity
- hasPendingActivity
#include "config.h"
#include "core/workers/WorkerMessagingProxy.h"
#include "core/dom/CrossThreadTask.h"
#include "core/dom/Document.h"
#include "core/events/ErrorEvent.h"
#include "core/events/MessageEvent.h"
#include "core/frame/DOMWindow.h"
#include "core/frame/csp/ContentSecurityPolicy.h"
#include "core/inspector/InspectorInstrumentation.h"
#include "core/inspector/ScriptCallStack.h"
#include "core/inspector/WorkerDebuggerAgent.h"
#include "core/inspector/WorkerInspectorController.h"
#include "core/loader/DocumentLoadTiming.h"
#include "core/loader/DocumentLoader.h"
#include "core/workers/DedicatedWorkerGlobalScope.h"
#include "core/workers/DedicatedWorkerThread.h"
#include "core/workers/Worker.h"
#include "core/workers/WorkerClients.h"
#include "core/workers/WorkerObjectProxy.h"
#include "core/workers/WorkerThreadStartupData.h"
#include "heap/Handle.h"
#include "platform/NotImplemented.h"
#include "wtf/Functional.h"
#include "wtf/MainThread.h"
namespace WebCore {
class MessageWorkerGlobalScopeTask : public ExecutionContextTask {
public:
static PassOwnPtr<MessageWorkerGlobalScopeTask> create(PassRefPtr<SerializedScriptValue> message, PassOwnPtr<MessagePortChannelArray> channels)
{
return adoptPtr(new MessageWorkerGlobalScopeTask(message, channels));
}
private:
MessageWorkerGlobalScopeTask(PassRefPtr<SerializedScriptValue> message, PassOwnPtr<MessagePortChannelArray> channels)
: m_message(message)
, m_channels(channels)
{
}
virtual void performTask(ExecutionContext* scriptContext)
{
ASSERT_WITH_SECURITY_IMPLICATION(scriptContext->isWorkerGlobalScope());
DedicatedWorkerGlobalScope* context = static_cast<DedicatedWorkerGlobalScope*>(scriptContext);
OwnPtr<MessagePortArray> ports = MessagePort::entanglePorts(*scriptContext, m_channels.release());
context->dispatchEvent(MessageEvent::create(ports.release(), m_message));
context->thread()->workerObjectProxy().confirmMessageFromWorkerObject(context->hasPendingActivity());
}
private:
RefPtr<SerializedScriptValue> m_message;
OwnPtr<MessagePortChannelArray> m_channels;
};
WorkerMessagingProxy::WorkerMessagingProxy(Worker* workerObject, PassOwnPtr<WorkerClients> workerClients)
: m_executionContext(workerObject->executionContext())
, m_workerObjectProxy(WorkerObjectProxy::create(m_executionContext.get(), this))
, m_workerObject(workerObject)
, m_mayBeDestroyed(false)
, m_unconfirmedMessageCount(0)
, m_workerThreadHadPendingActivity(false)
, m_askedToTerminate(false)
, m_pageInspector(0)
, m_workerClients(workerClients)
{
ASSERT(m_workerObject);
ASSERT((m_executionContext->isDocument() && isMainThread())
|| (m_executionContext->isWorkerGlobalScope() && toWorkerGlobalScope(m_executionContext.get())->thread()->isCurrentThread()));
}
WorkerMessagingProxy::~WorkerMessagingProxy()
{
ASSERT(!m_workerObject);
ASSERT((m_executionContext->isDocument() && isMainThread())
|| (m_executionContext->isWorkerGlobalScope() && toWorkerGlobalScope(m_executionContext.get())->thread()->isCurrentThread()));
}
void WorkerMessagingProxy::startWorkerGlobalScope(const KURL& scriptURL, const String& userAgent, const String& sourceCode, WorkerThreadStartMode startMode)
{
ASSERT(m_executionContext->isDocument());
Document* document = toDocument(m_executionContext.get());
OwnPtrWillBeRawPtr<WorkerThreadStartupData> startupData = WorkerThreadStartupData::create(scriptURL, userAgent, sourceCode, startMode, document->contentSecurityPolicy()->deprecatedHeader(), document->contentSecurityPolicy()->deprecatedHeaderType(), m_workerClients.release());
double originTime = document->loader() ? document->loader()->timing()->referenceMonotonicTime() : monotonicallyIncreasingTime();
RefPtr<DedicatedWorkerThread> thread = DedicatedWorkerThread::create(*this, *m_workerObjectProxy.get(), originTime, startupData.release());
workerThreadCreated(thread);
thread->start();
InspectorInstrumentation::didStartWorkerGlobalScope(m_executionContext.get(), this, scriptURL);
}
void WorkerMessagingProxy::postMessageToWorkerObject(PassRefPtr<SerializedScriptValue> message, PassOwnPtr<MessagePortChannelArray> channels)
{
if (!m_workerObject || m_askedToTerminate)
return;
OwnPtr<MessagePortArray> ports = MessagePort::entanglePorts(*m_executionContext.get(), channels);
m_workerObject->dispatchEvent(MessageEvent::create(ports.release(), message));
}
void WorkerMessagingProxy::postMessageToWorkerGlobalScope(PassRefPtr<SerializedScriptValue> message, PassOwnPtr<MessagePortChannelArray> channels)
{
if (m_askedToTerminate)
return;
if (m_workerThread) {
++m_unconfirmedMessageCount;
m_workerThread->runLoop().postTask(MessageWorkerGlobalScopeTask::create(message, channels));
} else
m_queuedEarlyTasks.append(MessageWorkerGlobalScopeTask::create(message, channels));
}
bool WorkerMessagingProxy::postTaskToWorkerGlobalScope(PassOwnPtr<ExecutionContextTask> task)
{
if (m_askedToTerminate)
return false;
ASSERT(m_workerThread);
m_workerThread->runLoop().postTask(task);
return true;
}
void WorkerMessagingProxy::postTaskToLoader(PassOwnPtr<ExecutionContextTask> task)
{
ASSERT(m_executionContext->isDocument());
m_executionContext->postTask(task);
}
void WorkerMessagingProxy::reportException(const String& errorMessage, int lineNumber, int columnNumber, const String& sourceURL)
{
if (!m_workerObject)
return;
RefPtrWillBeRawPtr<ErrorEvent> event = ErrorEvent::create(errorMessage, sourceURL, lineNumber, columnNumber, 0);
bool errorHandled = !m_workerObject->dispatchEvent(event);
if (!errorHandled)
m_executionContext->reportException(event, nullptr, NotSharableCrossOrigin);
}
void WorkerMessagingProxy::reportConsoleMessage(MessageSource source, MessageLevel level, const String& message, int lineNumber, const String& sourceURL)
{
if (m_askedToTerminate)
return;
m_executionContext->addConsoleMessage(source, level, message, sourceURL, lineNumber);
}
void WorkerMessagingProxy::workerThreadCreated(PassRefPtr<DedicatedWorkerThread> workerThread)
{
m_workerThread = workerThread;
if (m_askedToTerminate) {
m_workerThread->stop();
} else {
unsigned taskCount = m_queuedEarlyTasks.size();
ASSERT(!m_unconfirmedMessageCount);
m_unconfirmedMessageCount = taskCount;
m_workerThreadHadPendingActivity = true;
for (unsigned i = 0; i < taskCount; ++i)
m_workerThread->runLoop().postTask(m_queuedEarlyTasks[i].release());
m_queuedEarlyTasks.clear();
}
}
void WorkerMessagingProxy::workerObjectDestroyed()
{
m_workerObject = 0;
m_executionContext->postTask(createCallbackTask(&workerObjectDestroyedInternal, AllowCrossThreadAccess(this)));
}
void WorkerMessagingProxy::workerObjectDestroyedInternal(ExecutionContext*, WorkerMessagingProxy* proxy)
{
proxy->m_mayBeDestroyed = true;
if (proxy->m_workerThread)
proxy->terminateWorkerGlobalScope();
else
proxy->workerGlobalScopeDestroyed();
}
static void connectToWorkerGlobalScopeInspectorTask(ExecutionContext* context, bool)
{
toWorkerGlobalScope(context)->workerInspectorController()->connectFrontend();
}
void WorkerMessagingProxy::connectToInspector(WorkerGlobalScopeProxy::PageInspector* pageInspector)
{
if (m_askedToTerminate)
return;
ASSERT(!m_pageInspector);
m_pageInspector = pageInspector;
m_workerThread->runLoop().postDebuggerTask(createCallbackTask(connectToWorkerGlobalScopeInspectorTask, true));
}
static void disconnectFromWorkerGlobalScopeInspectorTask(ExecutionContext* context, bool)
{
toWorkerGlobalScope(context)->workerInspectorController()->disconnectFrontend();
}
void WorkerMessagingProxy::disconnectFromInspector()
{
m_pageInspector = 0;
if (m_askedToTerminate)
return;
m_workerThread->runLoop().postDebuggerTask(createCallbackTask(disconnectFromWorkerGlobalScopeInspectorTask, true));
}
static void dispatchOnInspectorBackendTask(ExecutionContext* context, const String& message)
{
toWorkerGlobalScope(context)->workerInspectorController()->dispatchMessageFromFrontend(message);
}
void WorkerMessagingProxy::sendMessageToInspector(const String& message)
{
if (m_askedToTerminate)
return;
m_workerThread->runLoop().postDebuggerTask(createCallbackTask(dispatchOnInspectorBackendTask, String(message)));
WorkerDebuggerAgent::interruptAndDispatchInspectorCommands(m_workerThread.get());
}
void WorkerMessagingProxy::workerGlobalScopeDestroyed()
{
m_askedToTerminate = true;
m_workerThread = nullptr;
InspectorInstrumentation::workerGlobalScopeTerminated(m_executionContext.get(), this);
if (m_mayBeDestroyed)
delete this;
}
void WorkerMessagingProxy::terminateWorkerGlobalScope()
{
if (m_askedToTerminate)
return;
m_askedToTerminate = true;
if (m_workerThread)
m_workerThread->stop();
InspectorInstrumentation::workerGlobalScopeTerminated(m_executionContext.get(), this);
}
void WorkerMessagingProxy::postMessageToPageInspector(const String& message)
{
if (m_pageInspector)
m_pageInspector->dispatchMessageFromWorker(message);
}
void WorkerMessagingProxy::confirmMessageFromWorkerObject(bool hasPendingActivity)
{
if (!m_askedToTerminate) {
ASSERT(m_unconfirmedMessageCount);
--m_unconfirmedMessageCount;
}
reportPendingActivity(hasPendingActivity);
}
void WorkerMessagingProxy::reportPendingActivity(bool hasPendingActivity)
{
m_workerThreadHadPendingActivity = hasPendingActivity;
}
bool WorkerMessagingProxy::hasPendingActivity() const
{
return (m_unconfirmedMessageCount || m_workerThreadHadPendingActivity) && !m_askedToTerminate;
}
}