OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "config.h" |
| 6 #include "core/workers/InProcessWorkerBase.h" |
| 7 |
| 8 #include "bindings/core/v8/ExceptionState.h" |
| 9 #include "core/events/MessageEvent.h" |
| 10 #include "core/fetch/ResourceFetcher.h" |
| 11 #include "core/frame/LocalDOMWindow.h" |
| 12 #include "core/inspector/InspectorInstrumentation.h" |
| 13 #include "core/workers/WorkerGlobalScopeProxy.h" |
| 14 #include "core/workers/WorkerScriptLoader.h" |
| 15 #include "core/workers/WorkerThread.h" |
| 16 #include "wtf/MainThread.h" |
| 17 |
| 18 namespace blink { |
| 19 |
| 20 InProcessWorkerBase::InProcessWorkerBase(ExecutionContext* context) |
| 21 : AbstractWorker(context) |
| 22 , m_contextProxy(nullptr) |
| 23 { |
| 24 } |
| 25 |
| 26 InProcessWorkerBase::~InProcessWorkerBase() |
| 27 { |
| 28 ASSERT(isMainThread()); |
| 29 if (!m_contextProxy) |
| 30 return; |
| 31 m_contextProxy->workerObjectDestroyed(); |
| 32 } |
| 33 |
| 34 void InProcessWorkerBase::postMessage(ExecutionContext*, PassRefPtr<SerializedSc
riptValue> message, const MessagePortArray* ports, ExceptionState& exceptionStat
e) |
| 35 { |
| 36 ASSERT(m_contextProxy); |
| 37 // Disentangle the port in preparation for sending it to the remote context. |
| 38 OwnPtr<MessagePortChannelArray> channels = MessagePort::disentanglePorts(por
ts, exceptionState); |
| 39 if (exceptionState.hadException()) |
| 40 return; |
| 41 m_contextProxy->postMessageToWorkerGlobalScope(message, channels.release()); |
| 42 } |
| 43 |
| 44 bool InProcessWorkerBase::initialize(ExecutionContext* context, const String& ur
l, ExceptionState& exceptionState) |
| 45 { |
| 46 suspendIfNeeded(); |
| 47 |
| 48 KURL scriptURL = resolveURL(url, exceptionState); |
| 49 if (scriptURL.isEmpty()) |
| 50 return false; |
| 51 |
| 52 m_scriptLoader = WorkerScriptLoader::create(); |
| 53 m_scriptLoader->loadAsynchronously(*context, scriptURL, DenyCrossOriginReque
sts, this); |
| 54 |
| 55 m_contextProxy = createWorkerGlobalScopeProxy(context); |
| 56 |
| 57 return true; |
| 58 } |
| 59 |
| 60 void InProcessWorkerBase::terminate() |
| 61 { |
| 62 if (m_contextProxy) |
| 63 m_contextProxy->terminateWorkerGlobalScope(); |
| 64 } |
| 65 |
| 66 void InProcessWorkerBase::stop() |
| 67 { |
| 68 terminate(); |
| 69 } |
| 70 |
| 71 bool InProcessWorkerBase::hasPendingActivity() const |
| 72 { |
| 73 // The worker context does not exist while loading, so we must ensure that t
he worker object is not collected, nor are its event listeners. |
| 74 return (m_contextProxy && m_contextProxy->hasPendingActivity()) || m_scriptL
oader; |
| 75 } |
| 76 |
| 77 void InProcessWorkerBase::didReceiveResponse(unsigned long identifier, const Res
ourceResponse&) |
| 78 { |
| 79 InspectorInstrumentation::didReceiveScriptResponse(executionContext(), ident
ifier); |
| 80 } |
| 81 |
| 82 void InProcessWorkerBase::notifyFinished() |
| 83 { |
| 84 if (m_scriptLoader->failed()) { |
| 85 dispatchEvent(Event::createCancelable(EventTypeNames::error)); |
| 86 } else { |
| 87 ASSERT(m_contextProxy); |
| 88 WorkerThreadStartMode startMode = DontPauseWorkerGlobalScopeOnStart; |
| 89 if (InspectorInstrumentation::shouldPauseDedicatedWorkerOnStart(executio
nContext())) |
| 90 startMode = PauseWorkerGlobalScopeOnStart; |
| 91 m_contextProxy->startWorkerGlobalScope(m_scriptLoader->url(), executionC
ontext()->userAgent(m_scriptLoader->url()), m_scriptLoader->script(), startMode)
; |
| 92 InspectorInstrumentation::scriptImported(executionContext(), m_scriptLoa
der->identifier(), m_scriptLoader->script()); |
| 93 } |
| 94 m_scriptLoader = nullptr; |
| 95 } |
| 96 |
| 97 DEFINE_TRACE(InProcessWorkerBase) |
| 98 { |
| 99 AbstractWorker::trace(visitor); |
| 100 } |
| 101 |
| 102 } // namespace blink |
OLD | NEW |