Index: Source/core/workers/WorkerBase.cpp |
diff --git a/Source/core/workers/WorkerBase.cpp b/Source/core/workers/WorkerBase.cpp |
new file mode 100644 |
index 0000000000000000000000000000000000000000..0aa09a0cf2d2c36253ed0f905d884a522aadd2b8 |
--- /dev/null |
+++ b/Source/core/workers/WorkerBase.cpp |
@@ -0,0 +1,102 @@ |
+// Copyright 2015 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "config.h" |
+#include "core/workers/WorkerBase.h" |
+ |
+#include "bindings/core/v8/ExceptionState.h" |
+#include "core/events/MessageEvent.h" |
+#include "core/fetch/ResourceFetcher.h" |
+#include "core/frame/LocalDOMWindow.h" |
+#include "core/inspector/InspectorInstrumentation.h" |
+#include "core/workers/WorkerGlobalScopeProxy.h" |
+#include "core/workers/WorkerScriptLoader.h" |
+#include "core/workers/WorkerThread.h" |
+#include "wtf/MainThread.h" |
+ |
+namespace blink { |
+ |
+WorkerBase::WorkerBase(ExecutionContext* context) |
+ : AbstractWorker(context) |
+ , m_contextProxy(nullptr) |
+{ |
+} |
+ |
+WorkerBase::~WorkerBase() |
+{ |
+ ASSERT(isMainThread()); |
+ if (!m_contextProxy) |
+ return; |
+ m_contextProxy->workerObjectDestroyed(); |
+} |
+ |
+void WorkerBase::postMessage(ExecutionContext*, PassRefPtr<SerializedScriptValue> message, const MessagePortArray* ports, ExceptionState& exceptionState) |
+{ |
+ ASSERT(m_contextProxy); |
+ // Disentangle the port in preparation for sending it to the remote context. |
+ OwnPtr<MessagePortChannelArray> channels = MessagePort::disentanglePorts(ports, exceptionState); |
+ if (exceptionState.hadException()) |
+ return; |
+ m_contextProxy->postMessageToWorkerGlobalScope(message, channels.release()); |
+} |
+ |
+bool WorkerBase::initialize(ExecutionContext* context, const String& url, ExceptionState& exceptionState) |
+{ |
+ suspendIfNeeded(); |
+ |
+ KURL scriptURL = resolveURL(url, exceptionState); |
+ if (scriptURL.isEmpty()) |
+ return false; |
+ |
+ m_scriptLoader = WorkerScriptLoader::create(); |
+ m_scriptLoader->loadAsynchronously(*context, scriptURL, DenyCrossOriginRequests, this); |
+ |
+ m_contextProxy = createWorkerGlobalScopeProxy(context); |
+ |
+ return true; |
+} |
+ |
+void WorkerBase::terminate() |
+{ |
+ if (m_contextProxy) |
+ m_contextProxy->terminateWorkerGlobalScope(); |
+} |
+ |
+void WorkerBase::stop() |
+{ |
+ terminate(); |
+} |
+ |
+bool WorkerBase::hasPendingActivity() const |
+{ |
+ // The worker context does not exist while loading, so we must ensure that the worker object is not collected, nor are its event listeners. |
+ return (m_contextProxy && m_contextProxy->hasPendingActivity()) || m_scriptLoader; |
+} |
+ |
+void WorkerBase::didReceiveResponse(unsigned long identifier, const ResourceResponse&) |
+{ |
+ InspectorInstrumentation::didReceiveScriptResponse(executionContext(), identifier); |
+} |
+ |
+void WorkerBase::notifyFinished() |
+{ |
+ if (m_scriptLoader->failed()) { |
+ dispatchEvent(Event::createCancelable(EventTypeNames::error)); |
+ } else { |
+ ASSERT(m_contextProxy); |
+ WorkerThreadStartMode startMode = DontPauseWorkerGlobalScopeOnStart; |
+ if (InspectorInstrumentation::shouldPauseDedicatedWorkerOnStart(executionContext())) |
+ startMode = PauseWorkerGlobalScopeOnStart; |
+ m_contextProxy->startWorkerGlobalScope(m_scriptLoader->url(), executionContext()->userAgent(m_scriptLoader->url()), m_scriptLoader->script(), startMode); |
+ InspectorInstrumentation::scriptImported(executionContext(), m_scriptLoader->identifier(), m_scriptLoader->script()); |
+ } |
+ m_scriptLoader = nullptr; |
+} |
+ |
+DEFINE_TRACE(WorkerBase) |
+{ |
+ AbstractWorker::trace(visitor); |
+} |
+ |
+} // namespace blink |