OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 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 "core/workers/ThreadedWorkletMessagingProxy.h" |
| 6 |
| 7 #include "bindings/core/v8/ScriptSourceCode.h" |
| 8 #include "bindings/core/v8/SourceLocation.h" |
| 9 #include "bindings/core/v8/WorkerOrWorkletScriptController.h" |
| 10 #include "core/dom/Document.h" |
| 11 #include "core/dom/ExecutionContextTask.h" |
| 12 #include "core/frame/LocalFrame.h" |
| 13 #include "core/origin_trials/OriginTrialContext.h" |
| 14 #include "core/workers/ThreadedWorkletObjectProxy.h" |
| 15 #include "core/workers/WorkerInspectorProxy.h" |
| 16 #include "core/workers/WorkerLoaderProxy.h" |
| 17 #include "core/workers/WorkerThreadStartupData.h" |
| 18 #include "core/workers/WorkletGlobalScope.h" |
| 19 |
| 20 namespace blink { |
| 21 |
| 22 namespace { |
| 23 |
| 24 void processEvaluateScript(const String& source, const KURL& scriptURL, Exec
utionContext* executionContext) |
| 25 { |
| 26 WorkletGlobalScope* globalScope = toWorkletGlobalScope(executionContext)
; |
| 27 globalScope->scriptController()->evaluate(ScriptSourceCode(source, scrip
tURL)); |
| 28 } |
| 29 |
| 30 } // namespace |
| 31 |
| 32 ThreadedWorkletMessagingProxy::ThreadedWorkletMessagingProxy(LocalFrame* frame) |
| 33 : m_executionContext(frame->document()) |
| 34 , m_workletObjectProxy(ThreadedWorkletObjectProxy::create(this)) |
| 35 , m_workerInspectorProxy(WorkerInspectorProxy::create()) |
| 36 , m_workerThreadHadPendingActivity(false) |
| 37 , m_askedToTerminate(false) |
| 38 , m_mayBeDestroyed(false) |
| 39 { |
| 40 DCHECK(isMainThread()); |
| 41 } |
| 42 |
| 43 ThreadedWorkletMessagingProxy::~ThreadedWorkletMessagingProxy() |
| 44 { |
| 45 DCHECK(isMainThread()); |
| 46 if (m_loaderProxy) |
| 47 m_loaderProxy->detachProvider(this); |
| 48 } |
| 49 |
| 50 void ThreadedWorkletMessagingProxy::initialize() |
| 51 { |
| 52 DCHECK(isMainThread()); |
| 53 DCHECK(!m_askedToTerminate); |
| 54 |
| 55 Document* document = toDocument(m_executionContext); |
| 56 SecurityOrigin* starterOrigin = document->getSecurityOrigin(); |
| 57 |
| 58 KURL scriptURL = m_executionContext->url(); |
| 59 std::unique_ptr<WorkerSettings> workerSettings = wrapUnique(new WorkerSettin
gs(document->settings())); |
| 60 std::unique_ptr<WorkerThreadStartupData> startupData = WorkerThreadStartupDa
ta::create( |
| 61 scriptURL, |
| 62 m_executionContext->userAgent(), |
| 63 String() /* sourceCode */, |
| 64 nullptr, |
| 65 m_workerInspectorProxy->workerStartMode(document), |
| 66 nullptr, |
| 67 String() /* referrerPolicy */, |
| 68 starterOrigin, |
| 69 nullptr, |
| 70 document->addressSpace(), |
| 71 OriginTrialContext::getTokens(document).get(), |
| 72 std::move(workerSettings)); |
| 73 |
| 74 m_loaderProxy = WorkerLoaderProxy::create(this); |
| 75 m_workerThread = createWorkerThread(); |
| 76 m_workerThread->start(std::move(startupData)); |
| 77 m_workerInspectorProxy->workerThreadCreated(document, m_workerThread.get(),
scriptURL); |
| 78 } |
| 79 |
| 80 void ThreadedWorkletMessagingProxy::evaluateScript(const ScriptSourceCode& scrip
tSourceCode) |
| 81 { |
| 82 DCHECK(isMainThread()); |
| 83 m_workerThread->postTask(BLINK_FROM_HERE, createCrossThreadTask(&processEval
uateScript, scriptSourceCode.source(), scriptSourceCode.url())); |
| 84 } |
| 85 |
| 86 void ThreadedWorkletMessagingProxy::reportConsoleMessage(MessageSource source, M
essageLevel level, const String& message, std::unique_ptr<SourceLocation> locati
on) |
| 87 { |
| 88 DCHECK(isMainThread()); |
| 89 if (m_askedToTerminate) |
| 90 return; |
| 91 if (m_workerInspectorProxy) |
| 92 m_workerInspectorProxy->addConsoleMessageFromWorker(level, message, std:
:move(location)); |
| 93 } |
| 94 |
| 95 void ThreadedWorkletMessagingProxy::postMessageToPageInspector(const String& mes
sage) |
| 96 { |
| 97 DCHECK(isMainThread()); |
| 98 if (m_workerInspectorProxy) |
| 99 m_workerInspectorProxy->dispatchMessageFromWorker(message); |
| 100 } |
| 101 |
| 102 void ThreadedWorkletMessagingProxy::reportPendingActivity(bool hasPendingActivit
y) |
| 103 { |
| 104 DCHECK(isMainThread()); |
| 105 m_workerThreadHadPendingActivity = hasPendingActivity; |
| 106 } |
| 107 |
| 108 void ThreadedWorkletMessagingProxy::workletObjectDestroyed() |
| 109 { |
| 110 DCHECK(isMainThread()); |
| 111 getExecutionContext()->postTask(BLINK_FROM_HERE, createCrossThreadTask(&Thre
adedWorkletMessagingProxy::workerObjectDestroyedInternal, crossThreadUnretained(
this))); |
| 112 } |
| 113 |
| 114 void ThreadedWorkletMessagingProxy::workerObjectDestroyedInternal() |
| 115 { |
| 116 DCHECK(isMainThread()); |
| 117 m_mayBeDestroyed = true; |
| 118 if (m_workerThread) |
| 119 terminateWorkletGlobalScope(); |
| 120 else |
| 121 workerThreadTerminated(); |
| 122 } |
| 123 |
| 124 void ThreadedWorkletMessagingProxy::workerThreadTerminated() |
| 125 { |
| 126 DCHECK(isMainThread()); |
| 127 |
| 128 // This method is always the last to be performed, so the proxy is not |
| 129 // needed for communication in either side any more. However, the Worker |
| 130 // object may still exist, and it assumes that the proxy exists, too. |
| 131 m_askedToTerminate = true; |
| 132 m_workerThread = nullptr; |
| 133 m_workerInspectorProxy->workerThreadTerminated(); |
| 134 if (m_mayBeDestroyed) |
| 135 delete this; |
| 136 } |
| 137 |
| 138 void ThreadedWorkletMessagingProxy::terminateWorkletGlobalScope() |
| 139 { |
| 140 DCHECK(isMainThread()); |
| 141 if (m_askedToTerminate) |
| 142 return; |
| 143 m_askedToTerminate = true; |
| 144 |
| 145 if (m_workerThread) |
| 146 m_workerThread->terminate(); |
| 147 |
| 148 m_workerInspectorProxy->workerThreadTerminated(); |
| 149 } |
| 150 |
| 151 void ThreadedWorkletMessagingProxy::workerThreadCreated() |
| 152 { |
| 153 DCHECK(isMainThread()); |
| 154 DCHECK(!m_askedToTerminate); |
| 155 DCHECK(m_workerThread); |
| 156 |
| 157 // Worker initialization means a pending activity. |
| 158 m_workerThreadHadPendingActivity = true; |
| 159 } |
| 160 |
| 161 void ThreadedWorkletMessagingProxy::postTaskToLoader(const WebTraceLocation& loc
ation, std::unique_ptr<ExecutionContextTask> task) |
| 162 { |
| 163 DCHECK(m_executionContext->isDocument()); |
| 164 m_executionContext->postTask(location, std::move(task)); |
| 165 } |
| 166 |
| 167 void ThreadedWorkletMessagingProxy::postTaskToWorkerGlobalScope(const WebTraceLo
cation& location, std::unique_ptr<ExecutionContextTask> task) |
| 168 { |
| 169 if (m_askedToTerminate) |
| 170 return; |
| 171 |
| 172 /* |
| 173 DCHECK(m_workerThread); |
| 174 m_workerThread->postTask(location, std::move(task)); |
| 175 */ // TODO need a thread. |
| 176 } |
| 177 |
| 178 ThreadedWorkletObjectProxy& ThreadedWorkletMessagingProxy::workerObjectProxy() |
| 179 { |
| 180 return *m_workletObjectProxy.get(); |
| 181 } |
| 182 |
| 183 } // namespace blink |
OLD | NEW |