| 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::terminateExecution(); | 111 CompositorWorkerThread::terminateExecution(); |
| 110 CompositorWorkerThread::clearSharedBackingThread(); | 112 CompositorWorkerThread::clearSharedBackingThread(); |
| 111 } | 113 } |
| 112 | 114 |
| 113 PassOwnPtr<CompositorWorkerThread> createCompositorWorker() | 115 std::unique_ptr<CompositorWorkerThread> createCompositorWorker() |
| 114 { | 116 { |
| 115 OwnPtr<CompositorWorkerThread> workerThread = CompositorWorkerThread::cr
eate(nullptr, *m_objectProxy, 0); | 117 std::unique_ptr<CompositorWorkerThread> workerThread = CompositorWorkerT
hread::create(nullptr, *m_objectProxy, 0); |
| 116 WorkerClients* clients = WorkerClients::create(); | 118 WorkerClients* clients = WorkerClients::create(); |
| 117 provideCompositorProxyClientTo(clients, new TestCompositorProxyClient); | 119 provideCompositorProxyClientTo(clients, new TestCompositorProxyClient); |
| 118 workerThread->start(WorkerThreadStartupData::create( | 120 workerThread->start(WorkerThreadStartupData::create( |
| 119 KURL(ParsedURLString, "http://fake.url/"), | 121 KURL(ParsedURLString, "http://fake.url/"), |
| 120 "fake user agent", | 122 "fake user agent", |
| 121 "//fake source code", | 123 "//fake source code", |
| 122 nullptr, | 124 nullptr, |
| 123 DontPauseWorkerGlobalScopeOnStart, | 125 DontPauseWorkerGlobalScopeOnStart, |
| 124 nullptr, | 126 nullptr, |
| 125 m_securityOrigin.get(), | 127 m_securityOrigin.get(), |
| 126 clients, | 128 clients, |
| 127 WebAddressSpaceLocal, | 129 WebAddressSpaceLocal, |
| 128 nullptr, | 130 nullptr, |
| 129 V8CacheOptionsDefault)); | 131 V8CacheOptionsDefault)); |
| 130 return workerThread; | 132 return workerThread; |
| 131 } | 133 } |
| 132 | 134 |
| 133 // Attempts to run some simple script for |worker|. | 135 // Attempts to run some simple script for |worker|. |
| 134 void checkWorkerCanExecuteScript(WorkerThread* worker) | 136 void checkWorkerCanExecuteScript(WorkerThread* worker) |
| 135 { | 137 { |
| 136 OwnPtr<WaitableEvent> waitEvent = adoptPtr(new WaitableEvent()); | 138 std::unique_ptr<WaitableEvent> waitEvent = wrapUnique(new WaitableEvent(
)); |
| 137 worker->workerBackingThread().backingThread().postTask(BLINK_FROM_HERE,
threadSafeBind(&CompositorWorkerThreadTest::executeScriptInWorker, AllowCrossThr
eadAccess(this), | 139 worker->workerBackingThread().backingThread().postTask(BLINK_FROM_HERE,
threadSafeBind(&CompositorWorkerThreadTest::executeScriptInWorker, AllowCrossThr
eadAccess(this), |
| 138 AllowCrossThreadAccess(worker), AllowCrossThreadAccess(waitEvent.get
()))); | 140 AllowCrossThreadAccess(worker), AllowCrossThreadAccess(waitEvent.get
()))); |
| 139 waitEvent->wait(); | 141 waitEvent->wait(); |
| 140 } | 142 } |
| 141 | 143 |
| 142 private: | 144 private: |
| 143 void executeScriptInWorker(WorkerThread* worker, WaitableEvent* waitEvent) | 145 void executeScriptInWorker(WorkerThread* worker, WaitableEvent* waitEvent) |
| 144 { | 146 { |
| 145 EXPECT_GT(worker->workerBackingThread().workerScriptCount(), 0u); | 147 EXPECT_GT(worker->workerBackingThread().workerScriptCount(), 0u); |
| 146 WorkerOrWorkletScriptController* scriptController = worker->workerGlobal
Scope()->scriptController(); | 148 WorkerOrWorkletScriptController* scriptController = worker->workerGlobal
Scope()->scriptController(); |
| 147 bool evaluateResult = scriptController->evaluate(ScriptSourceCode("var c
ounter = 0; ++counter;")); | 149 bool evaluateResult = scriptController->evaluate(ScriptSourceCode("var c
ounter = 0; ++counter;")); |
| 148 ASSERT_UNUSED(evaluateResult, evaluateResult); | 150 ASSERT_UNUSED(evaluateResult, evaluateResult); |
| 149 waitEvent->signal(); | 151 waitEvent->signal(); |
| 150 } | 152 } |
| 151 | 153 |
| 152 OwnPtr<DummyPageHolder> m_page; | 154 std::unique_ptr<DummyPageHolder> m_page; |
| 153 RefPtr<SecurityOrigin> m_securityOrigin; | 155 RefPtr<SecurityOrigin> m_securityOrigin; |
| 154 OwnPtr<InProcessWorkerObjectProxy> m_objectProxy; | 156 std::unique_ptr<InProcessWorkerObjectProxy> m_objectProxy; |
| 155 CompositorWorkerTestPlatform m_testPlatform; | 157 CompositorWorkerTestPlatform m_testPlatform; |
| 156 }; | 158 }; |
| 157 | 159 |
| 158 TEST_F(CompositorWorkerThreadTest, Basic) | 160 TEST_F(CompositorWorkerThreadTest, Basic) |
| 159 { | 161 { |
| 160 OwnPtr<CompositorWorkerThread> compositorWorker = createCompositorWorker(); | 162 std::unique_ptr<CompositorWorkerThread> compositorWorker = createCompositorW
orker(); |
| 161 checkWorkerCanExecuteScript(compositorWorker.get()); | 163 checkWorkerCanExecuteScript(compositorWorker.get()); |
| 162 compositorWorker->terminateAndWait(); | 164 compositorWorker->terminateAndWait(); |
| 163 } | 165 } |
| 164 | 166 |
| 165 // Tests that the same WebThread is used for new workers if the WebThread is sti
ll alive. | 167 // Tests that the same WebThread is used for new workers if the WebThread is sti
ll alive. |
| 166 TEST_F(CompositorWorkerThreadTest, CreateSecondAndTerminateFirst) | 168 TEST_F(CompositorWorkerThreadTest, CreateSecondAndTerminateFirst) |
| 167 { | 169 { |
| 168 // Create the first worker and wait until it is initialized. | 170 // Create the first worker and wait until it is initialized. |
| 169 OwnPtr<CompositorWorkerThread> firstWorker = createCompositorWorker(); | 171 std::unique_ptr<CompositorWorkerThread> firstWorker = createCompositorWorker
(); |
| 170 WebThreadSupportingGC* firstThread = &firstWorker->workerBackingThread().bac
kingThread(); | 172 WebThreadSupportingGC* firstThread = &firstWorker->workerBackingThread().bac
kingThread(); |
| 171 checkWorkerCanExecuteScript(firstWorker.get()); | 173 checkWorkerCanExecuteScript(firstWorker.get()); |
| 172 v8::Isolate* firstIsolate = firstWorker->isolate(); | 174 v8::Isolate* firstIsolate = firstWorker->isolate(); |
| 173 ASSERT_TRUE(firstIsolate); | 175 ASSERT_TRUE(firstIsolate); |
| 174 | 176 |
| 175 // Create the second worker and immediately destroy the first worker. | 177 // Create the second worker and immediately destroy the first worker. |
| 176 OwnPtr<CompositorWorkerThread> secondWorker = createCompositorWorker(); | 178 std::unique_ptr<CompositorWorkerThread> secondWorker = createCompositorWorke
r(); |
| 177 firstWorker->terminateAndWait(); | 179 firstWorker->terminateAndWait(); |
| 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 WorkerBackingThread* workerBackingThread = &compositorWorker->workerBackingT
hread(); | 201 WorkerBackingThread* workerBackingThread = &compositorWorker->workerBackingT
hread(); |
| 200 WebThreadSupportingGC* firstThread = &compositorWorker->workerBackingThread(
).backingThread(); | 202 WebThreadSupportingGC* firstThread = &compositorWorker->workerBackingThread(
).backingThread(); |
| 201 checkWorkerCanExecuteScript(compositorWorker.get()); | 203 checkWorkerCanExecuteScript(compositorWorker.get()); |
| 202 | 204 |
| 203 ASSERT_EQ(2u, workerBackingThread->workerScriptCount()); | 205 ASSERT_EQ(2u, workerBackingThread->workerScriptCount()); |
| 204 compositorWorker->terminateAndWait(); | 206 compositorWorker->terminateAndWait(); |
| 205 | 207 |
| 206 ASSERT_EQ(1u, workerBackingThread->workerScriptCount()); | 208 ASSERT_EQ(1u, workerBackingThread->workerScriptCount()); |
| 207 | 209 |
| 208 // Create the second worker. The backing thread is same. | 210 // Create the second worker. The backing thread is same. |
| 209 compositorWorker = createCompositorWorker(); | 211 compositorWorker = createCompositorWorker(); |
| 210 WebThreadSupportingGC* secondThread = &compositorWorker->workerBackingThread
().backingThread(); | 212 WebThreadSupportingGC* secondThread = &compositorWorker->workerBackingThread
().backingThread(); |
| 211 EXPECT_EQ(firstThread, secondThread); | 213 EXPECT_EQ(firstThread, secondThread); |
| 212 checkWorkerCanExecuteScript(compositorWorker.get()); | 214 checkWorkerCanExecuteScript(compositorWorker.get()); |
| 213 ASSERT_EQ(2u, workerBackingThread->workerScriptCount()); | 215 ASSERT_EQ(2u, workerBackingThread->workerScriptCount()); |
| 214 | 216 |
| 215 compositorWorker->terminateAndWait(); | 217 compositorWorker->terminateAndWait(); |
| 216 } | 218 } |
| 217 | 219 |
| 218 // Tests that v8::Isolate and WebThread are correctly set-up if a worker is crea
ted while another is terminating. | 220 // Tests that v8::Isolate and WebThread are correctly set-up if a worker is crea
ted while another is terminating. |
| 219 TEST_F(CompositorWorkerThreadTest, CreatingSecondDuringTerminationOfFirst) | 221 TEST_F(CompositorWorkerThreadTest, CreatingSecondDuringTerminationOfFirst) |
| 220 { | 222 { |
| 221 OwnPtr<CompositorWorkerThread> firstWorker = createCompositorWorker(); | 223 std::unique_ptr<CompositorWorkerThread> firstWorker = createCompositorWorker
(); |
| 222 checkWorkerCanExecuteScript(firstWorker.get()); | 224 checkWorkerCanExecuteScript(firstWorker.get()); |
| 223 v8::Isolate* firstIsolate = firstWorker->isolate(); | 225 v8::Isolate* firstIsolate = firstWorker->isolate(); |
| 224 ASSERT_TRUE(firstIsolate); | 226 ASSERT_TRUE(firstIsolate); |
| 225 | 227 |
| 226 // Request termination of the first worker and create the second worker | 228 // Request termination of the first worker and create the second worker |
| 227 // as soon as possible. | 229 // as soon as possible. |
| 228 EXPECT_EQ(2u, firstWorker->workerBackingThread().workerScriptCount()); | 230 EXPECT_EQ(2u, firstWorker->workerBackingThread().workerScriptCount()); |
| 229 firstWorker->terminate(); | 231 firstWorker->terminate(); |
| 230 // We don't wait for its termination. | 232 // We don't wait for its termination. |
| 231 // Note: We rely on the assumption that the termination steps don't run | 233 // Note: We rely on the assumption that the termination steps don't run |
| 232 // on the worker thread so quickly. This could be a source of flakiness. | 234 // on the worker thread so quickly. This could be a source of flakiness. |
| 233 | 235 |
| 234 OwnPtr<CompositorWorkerThread> secondWorker = createCompositorWorker(); | 236 std::unique_ptr<CompositorWorkerThread> secondWorker = createCompositorWorke
r(); |
| 235 | 237 |
| 236 v8::Isolate* secondIsolate = secondWorker->isolate(); | 238 v8::Isolate* secondIsolate = secondWorker->isolate(); |
| 237 ASSERT_TRUE(secondIsolate); | 239 ASSERT_TRUE(secondIsolate); |
| 238 EXPECT_EQ(firstIsolate, secondIsolate); | 240 EXPECT_EQ(firstIsolate, secondIsolate); |
| 239 | 241 |
| 240 // Verify that the isolate can run some scripts correctly in the second work
er. | 242 // Verify that the isolate can run some scripts correctly in the second work
er. |
| 241 checkWorkerCanExecuteScript(secondWorker.get()); | 243 checkWorkerCanExecuteScript(secondWorker.get()); |
| 242 secondWorker->terminateAndWait(); | 244 secondWorker->terminateAndWait(); |
| 243 } | 245 } |
| 244 | 246 |
| 245 } // namespace blink | 247 } // namespace blink |
| OLD | NEW |