Index: third_party/WebKit/Source/modules/worklet/WorkletThread.cpp |
diff --git a/third_party/WebKit/Source/modules/worklet/WorkletThread.cpp b/third_party/WebKit/Source/modules/worklet/WorkletThread.cpp |
new file mode 100644 |
index 0000000000000000000000000000000000000000..476541199c752f38671118ca13d9e765c802b2d4 |
--- /dev/null |
+++ b/third_party/WebKit/Source/modules/worklet/WorkletThread.cpp |
@@ -0,0 +1,104 @@ |
+// Copyright 2016 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 "modules/worklet/WorkletThread.h" |
+ |
+#include "bindings/core/v8/ScriptSourceCode.h" |
+#include "bindings/core/v8/WorkerOrWorkletScriptController.h" |
+#include "core/inspector/InspectorInstrumentation.h" |
+#include "core/workers/WorkerBackingThread.h" |
+#include "core/workers/WorkletGlobalScope.h" |
+#include "modules/worklet/WorkletThreadStartupData.h" |
+#include "platform/CrossThreadCopier.h" |
+#include "platform/ThreadSafeFunctional.h" |
+#include "platform/WebThreadSupportingGC.h" |
+ |
+namespace blink { |
+ |
+WorkletThread::WorkletThread() |
+ : m_terminated(false) |
+ , m_shutdown(false) |
+{ |
+ DCHECK(isMainThread()); |
+} |
+ |
+WorkletThread::~WorkletThread() |
+{ |
+} |
+ |
+void WorkletThread::initialize(PassOwnPtr<WorkletThreadStartupData> startupData) |
+{ |
+ DCHECK(isMainThread()); |
+ // TODO check this call. |
+ workerBackingThread().backingThread().postTask(BLINK_FROM_HERE, threadSafeBind(&WorkletThread::initializeInternal, AllowCrossThreadAccess(this), passed(std::move(startupData)))); |
+} |
+ |
+bool WorkletThread::isCurrentThread() |
+{ |
+ return workerBackingThread().backingThread().isCurrentThread(); |
+} |
+ |
+void WorkletThread::postTask(const WebTraceLocation& location, std::unique_ptr<ExecutionContextTask> task) |
+{ |
+ workerBackingThread().backingThread().postTask(location, createWorkletThreadTask(std::move(task), true)); |
+} |
+ |
+WorkletGlobalScope* WorkletThread::workletGlobalScope() |
+{ |
+ DCHECK(isCurrentThread()); |
+ return m_workletGlobalScope.get(); |
+} |
+ |
+void WorkletThread::initializeInternal(PassOwnPtr<WorkletThreadStartupData> startupData) |
+{ |
+ { |
+ MutexLocker lock(m_threadStateMutex); |
+ |
+ // The worker was terminated before the thread had a chance to run. |
+ if (m_terminated) { |
+ // Notify the proxy that the WorkerGlobalScope has been disposed of. |
+ // This can free this thread object, hence it must not be touched afterwards. |
+ // m_workerReportingProxy.workerThreadTerminated(); |
+ // Notify the main thread that it is safe to deallocate our resources. |
+ // m_terminationEvent->signal(); |
+ return; |
+ } |
+ |
+ workerBackingThread().attach(); |
+ |
+ m_workletGlobalScope = createWorkletGlobalScope(startupData->m_url, startupData->m_userAgent, startupData->m_securityOrigin); |
+ m_workletGlobalScope->scriptController()->initializeContextIfNeeded(); |
+ |
+ // Notify proxy that a new WorkerGlobalScope has been created and started. |
+// m_workerReportingProxy.workerGlobalScopeStarted(m_workerGlobalScope.get()); |
+ } |
+} |
+ |
+void WorkletThread::performTask(std::unique_ptr<ExecutionContextTask> task, bool isInstrumented) |
+{ |
+ DCHECK(isCurrentThread()); |
+ WorkletGlobalScope* globalScope = workletGlobalScope(); |
+ // If the thread is terminated before it had a chance initialize (see |
+ // WorkletThread::Initialize()), we mustn't run any of the posted tasks. |
+ if (!globalScope) { |
+ // TODO DCHECK(terminated()); |
+ return; |
+ } |
+ |
+ InspectorInstrumentation::AsyncTask asyncTask(globalScope, task.get(), isInstrumented); |
+ task->performTask(globalScope); |
+} |
+ |
+std::unique_ptr<CrossThreadClosure> WorkletThread::createWorkletThreadTask(std::unique_ptr<ExecutionContextTask> task, bool isInstrumented) |
+{ |
+ if (isInstrumented) |
+ isInstrumented = !task->taskNameForInstrumentation().isEmpty(); |
+ if (isInstrumented) { |
+ DCHECK(isCurrentThread()); |
+ InspectorInstrumentation::asyncTaskScheduled(workletGlobalScope(), "Worklet task", task.get()); |
+ } |
+ return threadSafeBind(&WorkletThread::performTask, AllowCrossThreadAccess(this), passed(std::move(task)), isInstrumented); |
+} |
+ |
+} // namespace blink |