| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "modules/compositorworker/CompositorWorkerThread.h" | 5 #include "modules/compositorworker/CompositorWorkerThread.h" |
| 6 | 6 |
| 7 #include "bindings/core/v8/ScriptSourceCode.h" | 7 #include "bindings/core/v8/ScriptSourceCode.h" |
| 8 #include "bindings/core/v8/SourceLocation.h" | 8 #include "bindings/core/v8/SourceLocation.h" |
| 9 #include "bindings/core/v8/V8GCController.h" | 9 #include "bindings/core/v8/V8GCController.h" |
| 10 #include "bindings/core/v8/WorkerOrWorkletScriptController.h" | 10 #include "bindings/core/v8/WorkerOrWorkletScriptController.h" |
| 11 #include "core/dom/CompositorWorkerProxyClient.h" | 11 #include "core/dom/CompositorWorkerProxyClient.h" |
| 12 #include "core/inspector/ConsoleMessage.h" | 12 #include "core/inspector/ConsoleMessage.h" |
| 13 #include "core/workers/InProcessWorkerObjectProxy.h" | 13 #include "core/workers/InProcessWorkerObjectProxy.h" |
| 14 #include "core/workers/ParentFrameTaskRunners.h" | |
| 15 #include "core/workers/WorkerBackingThread.h" | 14 #include "core/workers/WorkerBackingThread.h" |
| 16 #include "core/workers/WorkerLoaderProxy.h" | 15 #include "core/workers/WorkerLoaderProxy.h" |
| 17 #include "core/workers/WorkerOrWorkletGlobalScope.h" | 16 #include "core/workers/WorkerOrWorkletGlobalScope.h" |
| 18 #include "core/workers/WorkerThreadStartupData.h" | 17 #include "core/workers/WorkerThreadStartupData.h" |
| 19 #include "platform/CrossThreadFunctional.h" | 18 #include "platform/CrossThreadFunctional.h" |
| 20 #include "platform/WaitableEvent.h" | 19 #include "platform/WaitableEvent.h" |
| 21 #include "platform/WebThreadSupportingGC.h" | 20 #include "platform/WebThreadSupportingGC.h" |
| 22 #include "platform/heap/Handle.h" | 21 #include "platform/heap/Handle.h" |
| 23 #include "platform/testing/TestingPlatformSupport.h" | 22 #include "platform/testing/TestingPlatformSupport.h" |
| 24 #include "platform/testing/UnitTestHelpers.h" | 23 #include "platform/testing/UnitTestHelpers.h" |
| 25 #include "public/platform/Platform.h" | 24 #include "public/platform/Platform.h" |
| 26 #include "public/platform/WebAddressSpace.h" | 25 #include "public/platform/WebAddressSpace.h" |
| 27 #include "testing/gtest/include/gtest/gtest.h" | 26 #include "testing/gtest/include/gtest/gtest.h" |
| 28 #include "wtf/PtrUtil.h" | 27 #include "wtf/PtrUtil.h" |
| 29 #include <memory> | 28 #include <memory> |
| 30 | 29 |
| 31 namespace blink { | 30 namespace blink { |
| 32 namespace { | 31 namespace { |
| 33 | 32 |
| 34 // A null InProcessWorkerObjectProxy, supplied when creating | 33 // A null InProcessWorkerObjectProxy, supplied when creating |
| 35 // CompositorWorkerThreads. | 34 // CompositorWorkerThreads. |
| 36 class TestCompositorWorkerObjectProxy : public InProcessWorkerObjectProxy { | 35 class TestCompositorWorkerObjectProxy : public InProcessWorkerObjectProxy { |
| 37 public: | 36 public: |
| 38 static std::unique_ptr<TestCompositorWorkerObjectProxy> create( | 37 static std::unique_ptr<TestCompositorWorkerObjectProxy> create() { |
| 39 ParentFrameTaskRunners* parentFrameTaskRunners) { | 38 return WTF::wrapUnique(new TestCompositorWorkerObjectProxy); |
| 40 return WTF::wrapUnique( | |
| 41 new TestCompositorWorkerObjectProxy(parentFrameTaskRunners)); | |
| 42 } | 39 } |
| 43 | 40 |
| 44 // (Empty) WorkerReportingProxy implementation: | 41 // (Empty) WorkerReportingProxy implementation: |
| 45 virtual void dispatchErrorEvent(const String& errorMessage, | 42 virtual void dispatchErrorEvent(const String& errorMessage, |
| 46 std::unique_ptr<SourceLocation>, | 43 std::unique_ptr<SourceLocation>, |
| 47 int exceptionId) {} | 44 int exceptionId) {} |
| 48 void reportConsoleMessage(MessageSource, | 45 void reportConsoleMessage(MessageSource, |
| 49 MessageLevel, | 46 MessageLevel, |
| 50 const String& message, | 47 const String& message, |
| 51 SourceLocation*) override {} | 48 SourceLocation*) override {} |
| 52 void postMessageToPageInspector(const String&) override {} | 49 void postMessageToPageInspector(const String&) override {} |
| 53 void didCreateWorkerGlobalScope(WorkerOrWorkletGlobalScope*) override {} | 50 void didCreateWorkerGlobalScope(WorkerOrWorkletGlobalScope*) override {} |
| 54 void didEvaluateWorkerScript(bool success) override {} | 51 void didEvaluateWorkerScript(bool success) override {} |
| 55 void didCloseWorkerGlobalScope() override {} | 52 void didCloseWorkerGlobalScope() override {} |
| 56 void willDestroyWorkerGlobalScope() override {} | 53 void willDestroyWorkerGlobalScope() override {} |
| 57 void didTerminateWorkerThread() override {} | 54 void didTerminateWorkerThread() override {} |
| 58 | 55 |
| 59 private: | 56 private: |
| 60 explicit TestCompositorWorkerObjectProxy( | 57 TestCompositorWorkerObjectProxy() : InProcessWorkerObjectProxy(nullptr) {} |
| 61 ParentFrameTaskRunners* parentFrameTaskRunners) | |
| 62 : InProcessWorkerObjectProxy(nullptr, parentFrameTaskRunners) {} | |
| 63 }; | 58 }; |
| 64 | 59 |
| 65 class TestCompositorWorkerProxyClient : public CompositorWorkerProxyClient { | 60 class TestCompositorWorkerProxyClient : public CompositorWorkerProxyClient { |
| 66 public: | 61 public: |
| 67 TestCompositorWorkerProxyClient() {} | 62 TestCompositorWorkerProxyClient() {} |
| 68 | 63 |
| 69 void dispose() override {} | 64 void dispose() override {} |
| 70 void setGlobalScope(WorkerGlobalScope*) override {} | 65 void setGlobalScope(WorkerGlobalScope*) override {} |
| 71 void requestAnimationFrame() override {} | 66 void requestAnimationFrame() override {} |
| 72 void registerCompositorProxy(CompositorProxy*) override {} | 67 void registerCompositorProxy(CompositorProxy*) override {} |
| (...skipping 15 matching lines...) Expand all Loading... |
| 88 std::unique_ptr<WebThread> m_thread; | 83 std::unique_ptr<WebThread> m_thread; |
| 89 TestingCompositorSupport m_compositorSupport; | 84 TestingCompositorSupport m_compositorSupport; |
| 90 }; | 85 }; |
| 91 | 86 |
| 92 } // namespace | 87 } // namespace |
| 93 | 88 |
| 94 class CompositorWorkerThreadTest : public ::testing::Test { | 89 class CompositorWorkerThreadTest : public ::testing::Test { |
| 95 public: | 90 public: |
| 96 void SetUp() override { | 91 void SetUp() override { |
| 97 CompositorWorkerThread::createSharedBackingThreadForTest(); | 92 CompositorWorkerThread::createSharedBackingThreadForTest(); |
| 98 m_parentFrameTaskRunners = ParentFrameTaskRunners::create(nullptr); | 93 m_objectProxy = TestCompositorWorkerObjectProxy::create(); |
| 99 m_objectProxy = | |
| 100 TestCompositorWorkerObjectProxy::create(m_parentFrameTaskRunners.get()); | |
| 101 m_securityOrigin = | 94 m_securityOrigin = |
| 102 SecurityOrigin::create(KURL(ParsedURLString, "http://fake.url/")); | 95 SecurityOrigin::create(KURL(ParsedURLString, "http://fake.url/")); |
| 103 } | 96 } |
| 104 | 97 |
| 105 void TearDown() override { | 98 void TearDown() override { |
| 106 CompositorWorkerThread::clearSharedBackingThread(); | 99 CompositorWorkerThread::clearSharedBackingThread(); |
| 107 } | 100 } |
| 108 | 101 |
| 109 std::unique_ptr<CompositorWorkerThread> createCompositorWorker() { | 102 std::unique_ptr<CompositorWorkerThread> createCompositorWorker() { |
| 110 std::unique_ptr<CompositorWorkerThread> workerThread = | 103 std::unique_ptr<CompositorWorkerThread> workerThread = |
| 111 CompositorWorkerThread::create(nullptr, *m_objectProxy, 0); | 104 CompositorWorkerThread::create(nullptr, *m_objectProxy, 0); |
| 112 WorkerClients* clients = WorkerClients::create(); | 105 WorkerClients* clients = WorkerClients::create(); |
| 113 provideCompositorWorkerProxyClientTo(clients, | 106 provideCompositorWorkerProxyClientTo(clients, |
| 114 new TestCompositorWorkerProxyClient); | 107 new TestCompositorWorkerProxyClient); |
| 115 workerThread->start( | 108 workerThread->start( |
| 116 WorkerThreadStartupData::create( | 109 WorkerThreadStartupData::create( |
| 117 KURL(ParsedURLString, "http://fake.url/"), "fake user agent", | 110 KURL(ParsedURLString, "http://fake.url/"), "fake user agent", |
| 118 "//fake source code", nullptr, DontPauseWorkerGlobalScopeOnStart, | 111 "//fake source code", nullptr, DontPauseWorkerGlobalScopeOnStart, |
| 119 nullptr, "", m_securityOrigin.get(), clients, WebAddressSpaceLocal, | 112 nullptr, "", m_securityOrigin.get(), clients, WebAddressSpaceLocal, |
| 120 nullptr, nullptr, WorkerV8Settings::Default()), | 113 nullptr, nullptr, WorkerV8Settings::Default()), |
| 121 m_parentFrameTaskRunners.get()); | 114 FrameTaskRunnersHolder::create(nullptr)); |
| 122 return workerThread; | 115 return workerThread; |
| 123 } | 116 } |
| 124 | 117 |
| 125 // Attempts to run some simple script for |worker|. | 118 // Attempts to run some simple script for |worker|. |
| 126 void checkWorkerCanExecuteScript(WorkerThread* worker) { | 119 void checkWorkerCanExecuteScript(WorkerThread* worker) { |
| 127 std::unique_ptr<WaitableEvent> waitEvent = WTF::makeUnique<WaitableEvent>(); | 120 std::unique_ptr<WaitableEvent> waitEvent = WTF::makeUnique<WaitableEvent>(); |
| 128 worker->workerBackingThread().backingThread().postTask( | 121 worker->workerBackingThread().backingThread().postTask( |
| 129 BLINK_FROM_HERE, | 122 BLINK_FROM_HERE, |
| 130 crossThreadBind(&CompositorWorkerThreadTest::executeScriptInWorker, | 123 crossThreadBind(&CompositorWorkerThreadTest::executeScriptInWorker, |
| 131 crossThreadUnretained(this), | 124 crossThreadUnretained(this), |
| 132 crossThreadUnretained(worker), | 125 crossThreadUnretained(worker), |
| 133 crossThreadUnretained(waitEvent.get()))); | 126 crossThreadUnretained(waitEvent.get()))); |
| 134 waitEvent->wait(); | 127 waitEvent->wait(); |
| 135 } | 128 } |
| 136 | 129 |
| 137 private: | 130 private: |
| 138 void executeScriptInWorker(WorkerThread* worker, WaitableEvent* waitEvent) { | 131 void executeScriptInWorker(WorkerThread* worker, WaitableEvent* waitEvent) { |
| 139 WorkerOrWorkletScriptController* scriptController = | 132 WorkerOrWorkletScriptController* scriptController = |
| 140 worker->globalScope()->scriptController(); | 133 worker->globalScope()->scriptController(); |
| 141 bool evaluateResult = scriptController->evaluate( | 134 bool evaluateResult = scriptController->evaluate( |
| 142 ScriptSourceCode("var counter = 0; ++counter;")); | 135 ScriptSourceCode("var counter = 0; ++counter;")); |
| 143 DCHECK(evaluateResult); | 136 DCHECK(evaluateResult); |
| 144 waitEvent->signal(); | 137 waitEvent->signal(); |
| 145 } | 138 } |
| 146 | 139 |
| 147 RefPtr<SecurityOrigin> m_securityOrigin; | 140 RefPtr<SecurityOrigin> m_securityOrigin; |
| 148 std::unique_ptr<InProcessWorkerObjectProxy> m_objectProxy; | 141 std::unique_ptr<InProcessWorkerObjectProxy> m_objectProxy; |
| 149 Persistent<ParentFrameTaskRunners> m_parentFrameTaskRunners; | |
| 150 ScopedTestingPlatformSupport<CompositorWorkerTestPlatform> m_platform; | 142 ScopedTestingPlatformSupport<CompositorWorkerTestPlatform> m_platform; |
| 151 }; | 143 }; |
| 152 | 144 |
| 153 TEST_F(CompositorWorkerThreadTest, Basic) { | 145 TEST_F(CompositorWorkerThreadTest, Basic) { |
| 154 std::unique_ptr<CompositorWorkerThread> compositorWorker = | 146 std::unique_ptr<CompositorWorkerThread> compositorWorker = |
| 155 createCompositorWorker(); | 147 createCompositorWorker(); |
| 156 checkWorkerCanExecuteScript(compositorWorker.get()); | 148 checkWorkerCanExecuteScript(compositorWorker.get()); |
| 157 compositorWorker->terminateAndWait(); | 149 compositorWorker->terminateAndWait(); |
| 158 } | 150 } |
| 159 | 151 |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 239 ASSERT_TRUE(secondIsolate); | 231 ASSERT_TRUE(secondIsolate); |
| 240 EXPECT_EQ(firstIsolate, secondIsolate); | 232 EXPECT_EQ(firstIsolate, secondIsolate); |
| 241 | 233 |
| 242 // Verify that the isolate can run some scripts correctly in the second | 234 // Verify that the isolate can run some scripts correctly in the second |
| 243 // worker. | 235 // worker. |
| 244 checkWorkerCanExecuteScript(secondWorker.get()); | 236 checkWorkerCanExecuteScript(secondWorker.get()); |
| 245 secondWorker->terminateAndWait(); | 237 secondWorker->terminateAndWait(); |
| 246 } | 238 } |
| 247 | 239 |
| 248 } // namespace blink | 240 } // namespace blink |
| OLD | NEW |