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 "modules/worklet/WorkletThread.h" |
| 6 |
| 7 #include "bindings/core/v8/ScriptSourceCode.h" |
| 8 #include "bindings/core/v8/WorkerOrWorkletScriptController.h" |
| 9 #include "core/inspector/InspectorInstrumentation.h" |
| 10 #include "core/workers/WorkerBackingThread.h" |
| 11 #include "core/workers/WorkletGlobalScope.h" |
| 12 #include "modules/worklet/WorkletThreadStartupData.h" |
| 13 #include "platform/CrossThreadCopier.h" |
| 14 #include "platform/ThreadSafeFunctional.h" |
| 15 #include "platform/WebThreadSupportingGC.h" |
| 16 |
| 17 namespace blink { |
| 18 |
| 19 WorkletThread::WorkletThread() |
| 20 : m_terminated(false) |
| 21 , m_shutdown(false) |
| 22 { |
| 23 DCHECK(isMainThread()); |
| 24 } |
| 25 |
| 26 WorkletThread::~WorkletThread() |
| 27 { |
| 28 } |
| 29 |
| 30 void WorkletThread::initialize(PassOwnPtr<WorkletThreadStartupData> startupData) |
| 31 { |
| 32 DCHECK(isMainThread()); |
| 33 // TODO check this call. |
| 34 workerBackingThread().backingThread().postTask(BLINK_FROM_HERE, threadSafeBi
nd(&WorkletThread::initializeInternal, AllowCrossThreadAccess(this), passed(std:
:move(startupData)))); |
| 35 } |
| 36 |
| 37 bool WorkletThread::isCurrentThread() |
| 38 { |
| 39 return workerBackingThread().backingThread().isCurrentThread(); |
| 40 } |
| 41 |
| 42 void WorkletThread::postTask(const WebTraceLocation& location, std::unique_ptr<E
xecutionContextTask> task) |
| 43 { |
| 44 workerBackingThread().backingThread().postTask(location, createWorkletThread
Task(std::move(task), true)); |
| 45 } |
| 46 |
| 47 WorkletGlobalScope* WorkletThread::workletGlobalScope() |
| 48 { |
| 49 DCHECK(isCurrentThread()); |
| 50 return m_workletGlobalScope.get(); |
| 51 } |
| 52 |
| 53 void WorkletThread::initializeInternal(PassOwnPtr<WorkletThreadStartupData> star
tupData) |
| 54 { |
| 55 { |
| 56 MutexLocker lock(m_threadStateMutex); |
| 57 |
| 58 // The worker was terminated before the thread had a chance to run. |
| 59 if (m_terminated) { |
| 60 // Notify the proxy that the WorkerGlobalScope has been disposed of. |
| 61 // This can free this thread object, hence it must not be touched af
terwards. |
| 62 // m_workerReportingProxy.workerThreadTerminated(); |
| 63 // Notify the main thread that it is safe to deallocate our resource
s. |
| 64 // m_terminationEvent->signal(); |
| 65 return; |
| 66 } |
| 67 |
| 68 workerBackingThread().attach(); |
| 69 |
| 70 m_workletGlobalScope = createWorkletGlobalScope(startupData->m_url, star
tupData->m_userAgent, startupData->m_securityOrigin); |
| 71 m_workletGlobalScope->scriptController()->initializeContextIfNeeded(); |
| 72 |
| 73 // Notify proxy that a new WorkerGlobalScope has been created and starte
d. |
| 74 // m_workerReportingProxy.workerGlobalScopeStarted(m_workerGlobalScope.ge
t()); |
| 75 } |
| 76 } |
| 77 |
| 78 void WorkletThread::performTask(std::unique_ptr<ExecutionContextTask> task, bool
isInstrumented) |
| 79 { |
| 80 DCHECK(isCurrentThread()); |
| 81 WorkletGlobalScope* globalScope = workletGlobalScope(); |
| 82 // If the thread is terminated before it had a chance initialize (see |
| 83 // WorkletThread::Initialize()), we mustn't run any of the posted tasks. |
| 84 if (!globalScope) { |
| 85 // TODO DCHECK(terminated()); |
| 86 return; |
| 87 } |
| 88 |
| 89 InspectorInstrumentation::AsyncTask asyncTask(globalScope, task.get(), isIns
trumented); |
| 90 task->performTask(globalScope); |
| 91 } |
| 92 |
| 93 std::unique_ptr<CrossThreadClosure> WorkletThread::createWorkletThreadTask(std::
unique_ptr<ExecutionContextTask> task, bool isInstrumented) |
| 94 { |
| 95 if (isInstrumented) |
| 96 isInstrumented = !task->taskNameForInstrumentation().isEmpty(); |
| 97 if (isInstrumented) { |
| 98 DCHECK(isCurrentThread()); |
| 99 InspectorInstrumentation::asyncTaskScheduled(workletGlobalScope(), "Work
let task", task.get()); |
| 100 } |
| 101 return threadSafeBind(&WorkletThread::performTask, AllowCrossThreadAccess(th
is), passed(std::move(task)), isInstrumented); |
| 102 } |
| 103 |
| 104 } // namespace blink |
OLD | NEW |