| 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 "core/dom/CompositorProxyClient.h" | 10 #include "core/dom/CompositorProxyClient.h" |
| 11 #include "core/inspector/ConsoleMessage.h" | 11 #include "core/inspector/ConsoleMessage.h" |
| 12 #include "core/testing/DummyPageHolder.h" | 12 #include "core/testing/DummyPageHolder.h" |
| 13 #include "core/workers/InProcessWorkerObjectProxy.h" | 13 #include "core/workers/InProcessWorkerObjectProxy.h" |
| 14 #include "core/workers/WorkerBackingThread.h" | 14 #include "core/workers/WorkerBackingThread.h" |
| 15 #include "core/workers/WorkerLoaderProxy.h" | 15 #include "core/workers/WorkerLoaderProxy.h" |
| 16 #include "core/workers/WorkerThreadStartupData.h" | 16 #include "core/workers/WorkerThreadStartupData.h" |
| 17 #include "platform/ThreadSafeFunctional.h" | 17 #include "platform/ThreadSafeFunctional.h" |
| 18 #include "platform/WaitableEvent.h" | 18 #include "platform/WaitableEvent.h" |
| 19 #include "platform/WebThreadSupportingGC.h" | 19 #include "platform/WebThreadSupportingGC.h" |
| 20 #include "platform/heap/Handle.h" | 20 #include "platform/heap/Handle.h" |
| 21 #include "platform/testing/TestingPlatformSupport.h" | 21 #include "platform/testing/TestingPlatformSupport.h" |
| 22 #include "platform/testing/UnitTestHelpers.h" | 22 #include "platform/testing/UnitTestHelpers.h" |
| 23 #include "public/platform/Platform.h" | 23 #include "public/platform/Platform.h" |
| 24 #include "public/platform/WebAddressSpace.h" | 24 #include "public/platform/WebAddressSpace.h" |
| 25 #include "testing/gtest/include/gtest/gtest.h" | 25 #include "testing/gtest/include/gtest/gtest.h" |
| 26 #include "wtf/PtrUtil.h" |
| 27 #include <memory> |
| 26 | 28 |
| 27 namespace blink { | 29 namespace blink { |
| 28 namespace { | 30 namespace { |
| 29 | 31 |
| 30 // A null InProcessWorkerObjectProxy, supplied when creating CompositorWorkerThr
eads. | 32 // A null InProcessWorkerObjectProxy, supplied when creating CompositorWorkerThr
eads. |
| 31 class TestCompositorWorkerObjectProxy : public InProcessWorkerObjectProxy { | 33 class TestCompositorWorkerObjectProxy : public InProcessWorkerObjectProxy { |
| 32 public: | 34 public: |
| 33 static PassOwnPtr<TestCompositorWorkerObjectProxy> create(ExecutionContext*
context) | 35 static std::unique_ptr<TestCompositorWorkerObjectProxy> create(ExecutionCont
ext* context) |
| 34 { | 36 { |
| 35 return adoptPtr(new TestCompositorWorkerObjectProxy(context)); | 37 return wrapUnique(new TestCompositorWorkerObjectProxy(context)); |
| 36 } | 38 } |
| 37 | 39 |
| 38 // (Empty) WorkerReportingProxy implementation: | 40 // (Empty) WorkerReportingProxy implementation: |
| 39 virtual void reportException(const String& errorMessage, PassOwnPtr<SourceLo
cation>) {} | 41 virtual void reportException(const String& errorMessage, std::unique_ptr<Sou
rceLocation>) {} |
| 40 void reportConsoleMessage(ConsoleMessage*) override {} | 42 void reportConsoleMessage(ConsoleMessage*) override {} |
| 41 void postMessageToPageInspector(const String&) override {} | 43 void postMessageToPageInspector(const String&) override {} |
| 42 void postWorkerConsoleAgentEnabled() override {} | 44 void postWorkerConsoleAgentEnabled() override {} |
| 43 | 45 |
| 44 void didEvaluateWorkerScript(bool success) override {} | 46 void didEvaluateWorkerScript(bool success) override {} |
| 45 void workerGlobalScopeStarted(WorkerGlobalScope*) override {} | 47 void workerGlobalScopeStarted(WorkerGlobalScope*) override {} |
| 46 void workerGlobalScopeClosed() override {} | 48 void workerGlobalScopeClosed() override {} |
| 47 void workerThreadTerminated() override {} | 49 void workerThreadTerminated() override {} |
| 48 void willDestroyWorkerGlobalScope() override {} | 50 void willDestroyWorkerGlobalScope() override {} |
| 49 | 51 |
| (...skipping 18 matching lines...) Expand all Loading... |
| 68 | 70 |
| 69 void setGlobalScope(WorkerGlobalScope*) override {} | 71 void setGlobalScope(WorkerGlobalScope*) override {} |
| 70 void requestAnimationFrame() override {} | 72 void requestAnimationFrame() override {} |
| 71 void registerCompositorProxy(CompositorProxy*) override {} | 73 void registerCompositorProxy(CompositorProxy*) override {} |
| 72 void unregisterCompositorProxy(CompositorProxy*) override {} | 74 void unregisterCompositorProxy(CompositorProxy*) override {} |
| 73 }; | 75 }; |
| 74 | 76 |
| 75 class CompositorWorkerTestPlatform : public TestingPlatformSupport { | 77 class CompositorWorkerTestPlatform : public TestingPlatformSupport { |
| 76 public: | 78 public: |
| 77 CompositorWorkerTestPlatform() | 79 CompositorWorkerTestPlatform() |
| 78 : m_thread(adoptPtr(m_oldPlatform->createThread("Compositor"))) | 80 : m_thread(wrapUnique(m_oldPlatform->createThread("Compositor"))) |
| 79 { | 81 { |
| 80 } | 82 } |
| 81 | 83 |
| 82 WebThread* compositorThread() const override | 84 WebThread* compositorThread() const override |
| 83 { | 85 { |
| 84 return m_thread.get(); | 86 return m_thread.get(); |
| 85 } | 87 } |
| 86 | 88 |
| 87 WebCompositorSupport* compositorSupport() override { return &m_compositorSup
port; } | 89 WebCompositorSupport* compositorSupport() override { return &m_compositorSup
port; } |
| 88 | 90 |
| 89 private: | 91 private: |
| 90 OwnPtr<WebThread> m_thread; | 92 std::unique_ptr<WebThread> m_thread; |
| 91 TestingCompositorSupport m_compositorSupport; | 93 TestingCompositorSupport m_compositorSupport; |
| 92 }; | 94 }; |
| 93 | 95 |
| 94 } // namespace | 96 } // namespace |
| 95 | 97 |
| 96 class CompositorWorkerThreadTest : public ::testing::Test { | 98 class CompositorWorkerThreadTest : public ::testing::Test { |
| 97 public: | 99 public: |
| 98 void SetUp() override | 100 void SetUp() override |
| 99 { | 101 { |
| 100 CompositorWorkerThread::createSharedBackingThreadForTest(); | 102 CompositorWorkerThread::createSharedBackingThreadForTest(); |
| 101 m_page = DummyPageHolder::create(); | 103 m_page = DummyPageHolder::create(); |
| 102 m_objectProxy = TestCompositorWorkerObjectProxy::create(&m_page->documen
t()); | 104 m_objectProxy = TestCompositorWorkerObjectProxy::create(&m_page->documen
t()); |
| 103 m_securityOrigin = SecurityOrigin::create(KURL(ParsedURLString, "http://
fake.url/")); | 105 m_securityOrigin = SecurityOrigin::create(KURL(ParsedURLString, "http://
fake.url/")); |
| 104 } | 106 } |
| 105 | 107 |
| 106 void TearDown() override | 108 void TearDown() override |
| 107 { | 109 { |
| 108 m_page.reset(); | 110 m_page.reset(); |
| 109 CompositorWorkerThread::clearSharedBackingThread(); | 111 CompositorWorkerThread::clearSharedBackingThread(); |
| 110 } | 112 } |
| 111 | 113 |
| 112 PassOwnPtr<CompositorWorkerThread> createCompositorWorker() | 114 std::unique_ptr<CompositorWorkerThread> createCompositorWorker() |
| 113 { | 115 { |
| 114 OwnPtr<CompositorWorkerThread> workerThread = CompositorWorkerThread::cr
eate(nullptr, *m_objectProxy, 0); | 116 std::unique_ptr<CompositorWorkerThread> workerThread = CompositorWorkerT
hread::create(nullptr, *m_objectProxy, 0); |
| 115 WorkerClients* clients = WorkerClients::create(); | 117 WorkerClients* clients = WorkerClients::create(); |
| 116 provideCompositorProxyClientTo(clients, new TestCompositorProxyClient); | 118 provideCompositorProxyClientTo(clients, new TestCompositorProxyClient); |
| 117 workerThread->start(WorkerThreadStartupData::create( | 119 workerThread->start(WorkerThreadStartupData::create( |
| 118 KURL(ParsedURLString, "http://fake.url/"), | 120 KURL(ParsedURLString, "http://fake.url/"), |
| 119 "fake user agent", | 121 "fake user agent", |
| 120 "//fake source code", | 122 "//fake source code", |
| 121 nullptr, | 123 nullptr, |
| 122 DontPauseWorkerGlobalScopeOnStart, | 124 DontPauseWorkerGlobalScopeOnStart, |
| 123 nullptr, | 125 nullptr, |
| 124 m_securityOrigin.get(), | 126 m_securityOrigin.get(), |
| 125 clients, | 127 clients, |
| 126 WebAddressSpaceLocal, | 128 WebAddressSpaceLocal, |
| 127 nullptr, | 129 nullptr, |
| 128 V8CacheOptionsDefault)); | 130 V8CacheOptionsDefault)); |
| 129 return workerThread; | 131 return workerThread; |
| 130 } | 132 } |
| 131 | 133 |
| 132 // Attempts to run some simple script for |worker|. | 134 // Attempts to run some simple script for |worker|. |
| 133 void checkWorkerCanExecuteScript(WorkerThread* worker) | 135 void checkWorkerCanExecuteScript(WorkerThread* worker) |
| 134 { | 136 { |
| 135 OwnPtr<WaitableEvent> waitEvent = adoptPtr(new WaitableEvent()); | 137 std::unique_ptr<WaitableEvent> waitEvent = wrapUnique(new WaitableEvent(
)); |
| 136 worker->workerBackingThread().backingThread().postTask(BLINK_FROM_HERE,
threadSafeBind(&CompositorWorkerThreadTest::executeScriptInWorker, AllowCrossThr
eadAccess(this), | 138 worker->workerBackingThread().backingThread().postTask(BLINK_FROM_HERE,
threadSafeBind(&CompositorWorkerThreadTest::executeScriptInWorker, AllowCrossThr
eadAccess(this), |
| 137 AllowCrossThreadAccess(worker), AllowCrossThreadAccess(waitEvent.get
()))); | 139 AllowCrossThreadAccess(worker), AllowCrossThreadAccess(waitEvent.get
()))); |
| 138 waitEvent->wait(); | 140 waitEvent->wait(); |
| 139 } | 141 } |
| 140 | 142 |
| 141 private: | 143 private: |
| 142 void executeScriptInWorker(WorkerThread* worker, WaitableEvent* waitEvent) | 144 void executeScriptInWorker(WorkerThread* worker, WaitableEvent* waitEvent) |
| 143 { | 145 { |
| 144 WorkerOrWorkletScriptController* scriptController = worker->workerGlobal
Scope()->scriptController(); | 146 WorkerOrWorkletScriptController* scriptController = worker->workerGlobal
Scope()->scriptController(); |
| 145 bool evaluateResult = scriptController->evaluate(ScriptSourceCode("var c
ounter = 0; ++counter;")); | 147 bool evaluateResult = scriptController->evaluate(ScriptSourceCode("var c
ounter = 0; ++counter;")); |
| 146 ASSERT_UNUSED(evaluateResult, evaluateResult); | 148 ASSERT_UNUSED(evaluateResult, evaluateResult); |
| 147 waitEvent->signal(); | 149 waitEvent->signal(); |
| 148 } | 150 } |
| 149 | 151 |
| 150 OwnPtr<DummyPageHolder> m_page; | 152 std::unique_ptr<DummyPageHolder> m_page; |
| 151 RefPtr<SecurityOrigin> m_securityOrigin; | 153 RefPtr<SecurityOrigin> m_securityOrigin; |
| 152 OwnPtr<InProcessWorkerObjectProxy> m_objectProxy; | 154 std::unique_ptr<InProcessWorkerObjectProxy> m_objectProxy; |
| 153 CompositorWorkerTestPlatform m_testPlatform; | 155 CompositorWorkerTestPlatform m_testPlatform; |
| 154 }; | 156 }; |
| 155 | 157 |
| 156 TEST_F(CompositorWorkerThreadTest, Basic) | 158 TEST_F(CompositorWorkerThreadTest, Basic) |
| 157 { | 159 { |
| 158 OwnPtr<CompositorWorkerThread> compositorWorker = createCompositorWorker(); | 160 std::unique_ptr<CompositorWorkerThread> compositorWorker = createCompositorW
orker(); |
| 159 checkWorkerCanExecuteScript(compositorWorker.get()); | 161 checkWorkerCanExecuteScript(compositorWorker.get()); |
| 160 compositorWorker->terminateAndWait(); | 162 compositorWorker->terminateAndWait(); |
| 161 } | 163 } |
| 162 | 164 |
| 163 // Tests that the same WebThread is used for new workers if the WebThread is sti
ll alive. | 165 // Tests that the same WebThread is used for new workers if the WebThread is sti
ll alive. |
| 164 TEST_F(CompositorWorkerThreadTest, CreateSecondAndTerminateFirst) | 166 TEST_F(CompositorWorkerThreadTest, CreateSecondAndTerminateFirst) |
| 165 { | 167 { |
| 166 // Create the first worker and wait until it is initialized. | 168 // Create the first worker and wait until it is initialized. |
| 167 OwnPtr<CompositorWorkerThread> firstWorker = createCompositorWorker(); | 169 std::unique_ptr<CompositorWorkerThread> firstWorker = createCompositorWorker
(); |
| 168 WebThreadSupportingGC* firstThread = &firstWorker->workerBackingThread().bac
kingThread(); | 170 WebThreadSupportingGC* firstThread = &firstWorker->workerBackingThread().bac
kingThread(); |
| 169 checkWorkerCanExecuteScript(firstWorker.get()); | 171 checkWorkerCanExecuteScript(firstWorker.get()); |
| 170 v8::Isolate* firstIsolate = firstWorker->isolate(); | 172 v8::Isolate* firstIsolate = firstWorker->isolate(); |
| 171 ASSERT_TRUE(firstIsolate); | 173 ASSERT_TRUE(firstIsolate); |
| 172 | 174 |
| 173 // Create the second worker and immediately destroy the first worker. | 175 // Create the second worker and immediately destroy the first worker. |
| 174 OwnPtr<CompositorWorkerThread> secondWorker = createCompositorWorker(); | 176 std::unique_ptr<CompositorWorkerThread> secondWorker = createCompositorWorke
r(); |
| 175 // We don't use terminateAndWait here to avoid forcible termination. | 177 // We don't use terminateAndWait here to avoid forcible termination. |
| 176 firstWorker->terminate(); | 178 firstWorker->terminate(); |
| 177 firstWorker->waitForShutdownForTesting(); | 179 firstWorker->waitForShutdownForTesting(); |
| 178 | 180 |
| 179 // Wait until the second worker is initialized. Verify that the second worke
r is using the same | 181 // Wait until the second worker is initialized. Verify that the second worke
r is using the same |
| 180 // thread and Isolate as the first worker. | 182 // thread and Isolate as the first worker. |
| 181 WebThreadSupportingGC* secondThread = &secondWorker->workerBackingThread().b
ackingThread(); | 183 WebThreadSupportingGC* secondThread = &secondWorker->workerBackingThread().b
ackingThread(); |
| 182 ASSERT_EQ(firstThread, secondThread); | 184 ASSERT_EQ(firstThread, secondThread); |
| 183 | 185 |
| 184 v8::Isolate* secondIsolate = secondWorker->isolate(); | 186 v8::Isolate* secondIsolate = secondWorker->isolate(); |
| 185 ASSERT_TRUE(secondIsolate); | 187 ASSERT_TRUE(secondIsolate); |
| 186 EXPECT_EQ(firstIsolate, secondIsolate); | 188 EXPECT_EQ(firstIsolate, secondIsolate); |
| 187 | 189 |
| 188 // Verify that the worker can still successfully execute script. | 190 // Verify that the worker can still successfully execute script. |
| 189 checkWorkerCanExecuteScript(secondWorker.get()); | 191 checkWorkerCanExecuteScript(secondWorker.get()); |
| 190 | 192 |
| 191 secondWorker->terminateAndWait(); | 193 secondWorker->terminateAndWait(); |
| 192 } | 194 } |
| 193 | 195 |
| 194 // Tests that a new WebThread is created if all existing workers are terminated
before a new worker is created. | 196 // Tests that a new WebThread is created if all existing workers are terminated
before a new worker is created. |
| 195 TEST_F(CompositorWorkerThreadTest, TerminateFirstAndCreateSecond) | 197 TEST_F(CompositorWorkerThreadTest, TerminateFirstAndCreateSecond) |
| 196 { | 198 { |
| 197 // Create the first worker, wait until it is initialized, and terminate it. | 199 // Create the first worker, wait until it is initialized, and terminate it. |
| 198 OwnPtr<CompositorWorkerThread> compositorWorker = createCompositorWorker(); | 200 std::unique_ptr<CompositorWorkerThread> compositorWorker = createCompositorW
orker(); |
| 199 WebThreadSupportingGC* firstThread = &compositorWorker->workerBackingThread(
).backingThread(); | 201 WebThreadSupportingGC* firstThread = &compositorWorker->workerBackingThread(
).backingThread(); |
| 200 checkWorkerCanExecuteScript(compositorWorker.get()); | 202 checkWorkerCanExecuteScript(compositorWorker.get()); |
| 201 | 203 |
| 202 // We don't use terminateAndWait here to avoid forcible termination. | 204 // We don't use terminateAndWait here to avoid forcible termination. |
| 203 compositorWorker->terminate(); | 205 compositorWorker->terminate(); |
| 204 compositorWorker->waitForShutdownForTesting(); | 206 compositorWorker->waitForShutdownForTesting(); |
| 205 | 207 |
| 206 // Create the second worker. The backing thread is same. | 208 // Create the second worker. The backing thread is same. |
| 207 compositorWorker = createCompositorWorker(); | 209 compositorWorker = createCompositorWorker(); |
| 208 WebThreadSupportingGC* secondThread = &compositorWorker->workerBackingThread
().backingThread(); | 210 WebThreadSupportingGC* secondThread = &compositorWorker->workerBackingThread
().backingThread(); |
| 209 EXPECT_EQ(firstThread, secondThread); | 211 EXPECT_EQ(firstThread, secondThread); |
| 210 checkWorkerCanExecuteScript(compositorWorker.get()); | 212 checkWorkerCanExecuteScript(compositorWorker.get()); |
| 211 | 213 |
| 212 compositorWorker->terminateAndWait(); | 214 compositorWorker->terminateAndWait(); |
| 213 } | 215 } |
| 214 | 216 |
| 215 // Tests that v8::Isolate and WebThread are correctly set-up if a worker is crea
ted while another is terminating. | 217 // Tests that v8::Isolate and WebThread are correctly set-up if a worker is crea
ted while another is terminating. |
| 216 TEST_F(CompositorWorkerThreadTest, CreatingSecondDuringTerminationOfFirst) | 218 TEST_F(CompositorWorkerThreadTest, CreatingSecondDuringTerminationOfFirst) |
| 217 { | 219 { |
| 218 OwnPtr<CompositorWorkerThread> firstWorker = createCompositorWorker(); | 220 std::unique_ptr<CompositorWorkerThread> firstWorker = createCompositorWorker
(); |
| 219 checkWorkerCanExecuteScript(firstWorker.get()); | 221 checkWorkerCanExecuteScript(firstWorker.get()); |
| 220 v8::Isolate* firstIsolate = firstWorker->isolate(); | 222 v8::Isolate* firstIsolate = firstWorker->isolate(); |
| 221 ASSERT_TRUE(firstIsolate); | 223 ASSERT_TRUE(firstIsolate); |
| 222 | 224 |
| 223 // Request termination of the first worker and create the second worker | 225 // Request termination of the first worker and create the second worker |
| 224 // as soon as possible. | 226 // as soon as possible. |
| 225 firstWorker->terminate(); | 227 firstWorker->terminate(); |
| 226 // We don't wait for its termination. | 228 // We don't wait for its termination. |
| 227 // Note: We rely on the assumption that the termination steps don't run | 229 // Note: We rely on the assumption that the termination steps don't run |
| 228 // on the worker thread so quickly. This could be a source of flakiness. | 230 // on the worker thread so quickly. This could be a source of flakiness. |
| 229 | 231 |
| 230 OwnPtr<CompositorWorkerThread> secondWorker = createCompositorWorker(); | 232 std::unique_ptr<CompositorWorkerThread> secondWorker = createCompositorWorke
r(); |
| 231 | 233 |
| 232 v8::Isolate* secondIsolate = secondWorker->isolate(); | 234 v8::Isolate* secondIsolate = secondWorker->isolate(); |
| 233 ASSERT_TRUE(secondIsolate); | 235 ASSERT_TRUE(secondIsolate); |
| 234 EXPECT_EQ(firstIsolate, secondIsolate); | 236 EXPECT_EQ(firstIsolate, secondIsolate); |
| 235 | 237 |
| 236 // Verify that the isolate can run some scripts correctly in the second work
er. | 238 // Verify that the isolate can run some scripts correctly in the second work
er. |
| 237 checkWorkerCanExecuteScript(secondWorker.get()); | 239 checkWorkerCanExecuteScript(secondWorker.get()); |
| 238 secondWorker->terminateAndWait(); | 240 secondWorker->terminateAndWait(); |
| 239 } | 241 } |
| 240 | 242 |
| 241 } // namespace blink | 243 } // namespace blink |
| OLD | NEW |