| 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 16 matching lines...) Expand all Loading... |
| 66 public: | 68 public: |
| 67 TestCompositorProxyClient() {} | 69 TestCompositorProxyClient() {} |
| 68 | 70 |
| 69 void setGlobalScope(WorkerGlobalScope*) override {} | 71 void setGlobalScope(WorkerGlobalScope*) override {} |
| 70 void requestAnimationFrame() override {} | 72 void requestAnimationFrame() override {} |
| 71 }; | 73 }; |
| 72 | 74 |
| 73 class CompositorWorkerTestPlatform : public TestingPlatformSupport { | 75 class CompositorWorkerTestPlatform : public TestingPlatformSupport { |
| 74 public: | 76 public: |
| 75 CompositorWorkerTestPlatform() | 77 CompositorWorkerTestPlatform() |
| 76 : m_thread(adoptPtr(m_oldPlatform->createThread("Compositor"))) | 78 : m_thread(wrapUnique(m_oldPlatform->createThread("Compositor"))) |
| 77 { | 79 { |
| 78 } | 80 } |
| 79 | 81 |
| 80 WebThread* compositorThread() const override | 82 WebThread* compositorThread() const override |
| 81 { | 83 { |
| 82 return m_thread.get(); | 84 return m_thread.get(); |
| 83 } | 85 } |
| 84 | 86 |
| 85 WebCompositorSupport* compositorSupport() override { return &m_compositorSup
port; } | 87 WebCompositorSupport* compositorSupport() override { return &m_compositorSup
port; } |
| 86 | 88 |
| 87 private: | 89 private: |
| 88 OwnPtr<WebThread> m_thread; | 90 std::unique_ptr<WebThread> m_thread; |
| 89 TestingCompositorSupport m_compositorSupport; | 91 TestingCompositorSupport m_compositorSupport; |
| 90 }; | 92 }; |
| 91 | 93 |
| 92 } // namespace | 94 } // namespace |
| 93 | 95 |
| 94 class CompositorWorkerThreadTest : public ::testing::Test { | 96 class CompositorWorkerThreadTest : public ::testing::Test { |
| 95 public: | 97 public: |
| 96 void SetUp() override | 98 void SetUp() override |
| 97 { | 99 { |
| 98 CompositorWorkerThread::createSharedBackingThreadForTest(); | 100 CompositorWorkerThread::createSharedBackingThreadForTest(); |
| 99 m_page = DummyPageHolder::create(); | 101 m_page = DummyPageHolder::create(); |
| 100 m_objectProxy = TestCompositorWorkerObjectProxy::create(&m_page->documen
t()); | 102 m_objectProxy = TestCompositorWorkerObjectProxy::create(&m_page->documen
t()); |
| 101 m_securityOrigin = SecurityOrigin::create(KURL(ParsedURLString, "http://
fake.url/")); | 103 m_securityOrigin = SecurityOrigin::create(KURL(ParsedURLString, "http://
fake.url/")); |
| 102 } | 104 } |
| 103 | 105 |
| 104 void TearDown() override | 106 void TearDown() override |
| 105 { | 107 { |
| 106 m_page.reset(); | 108 m_page.reset(); |
| 107 CompositorWorkerThread::terminateExecution(); | 109 CompositorWorkerThread::terminateExecution(); |
| 108 CompositorWorkerThread::clearSharedBackingThread(); | 110 CompositorWorkerThread::clearSharedBackingThread(); |
| 109 } | 111 } |
| 110 | 112 |
| 111 PassOwnPtr<CompositorWorkerThread> createCompositorWorker() | 113 std::unique_ptr<CompositorWorkerThread> createCompositorWorker() |
| 112 { | 114 { |
| 113 OwnPtr<CompositorWorkerThread> workerThread = CompositorWorkerThread::cr
eate(nullptr, *m_objectProxy, 0); | 115 std::unique_ptr<CompositorWorkerThread> workerThread = CompositorWorkerT
hread::create(nullptr, *m_objectProxy, 0); |
| 114 WorkerClients* clients = WorkerClients::create(); | 116 WorkerClients* clients = WorkerClients::create(); |
| 115 provideCompositorProxyClientTo(clients, new TestCompositorProxyClient); | 117 provideCompositorProxyClientTo(clients, new TestCompositorProxyClient); |
| 116 workerThread->start(WorkerThreadStartupData::create( | 118 workerThread->start(WorkerThreadStartupData::create( |
| 117 KURL(ParsedURLString, "http://fake.url/"), | 119 KURL(ParsedURLString, "http://fake.url/"), |
| 118 "fake user agent", | 120 "fake user agent", |
| 119 "//fake source code", | 121 "//fake source code", |
| 120 nullptr, | 122 nullptr, |
| 121 DontPauseWorkerGlobalScopeOnStart, | 123 DontPauseWorkerGlobalScopeOnStart, |
| 122 nullptr, | 124 nullptr, |
| 123 m_securityOrigin.get(), | 125 m_securityOrigin.get(), |
| 124 clients, | 126 clients, |
| 125 WebAddressSpaceLocal, | 127 WebAddressSpaceLocal, |
| 126 nullptr, | 128 nullptr, |
| 127 V8CacheOptionsDefault)); | 129 V8CacheOptionsDefault)); |
| 128 return workerThread; | 130 return workerThread; |
| 129 } | 131 } |
| 130 | 132 |
| 131 // Attempts to run some simple script for |worker|. | 133 // Attempts to run some simple script for |worker|. |
| 132 void checkWorkerCanExecuteScript(WorkerThread* worker) | 134 void checkWorkerCanExecuteScript(WorkerThread* worker) |
| 133 { | 135 { |
| 134 OwnPtr<WaitableEvent> waitEvent = adoptPtr(new WaitableEvent()); | 136 std::unique_ptr<WaitableEvent> waitEvent = wrapUnique(new WaitableEvent(
)); |
| 135 worker->workerBackingThread().backingThread().postTask(BLINK_FROM_HERE,
threadSafeBind(&CompositorWorkerThreadTest::executeScriptInWorker, AllowCrossThr
eadAccess(this), | 137 worker->workerBackingThread().backingThread().postTask(BLINK_FROM_HERE,
threadSafeBind(&CompositorWorkerThreadTest::executeScriptInWorker, AllowCrossThr
eadAccess(this), |
| 136 AllowCrossThreadAccess(worker), AllowCrossThreadAccess(waitEvent.get
()))); | 138 AllowCrossThreadAccess(worker), AllowCrossThreadAccess(waitEvent.get
()))); |
| 137 waitEvent->wait(); | 139 waitEvent->wait(); |
| 138 } | 140 } |
| 139 | 141 |
| 140 private: | 142 private: |
| 141 void executeScriptInWorker(WorkerThread* worker, WaitableEvent* waitEvent) | 143 void executeScriptInWorker(WorkerThread* worker, WaitableEvent* waitEvent) |
| 142 { | 144 { |
| 143 EXPECT_GT(worker->workerBackingThread().workerScriptCount(), 0u); | 145 EXPECT_GT(worker->workerBackingThread().workerScriptCount(), 0u); |
| 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 firstWorker->terminateAndWait(); | 177 firstWorker->terminateAndWait(); |
| 176 | 178 |
| 177 // Wait until the second worker is initialized. Verify that the second worke
r is using the same | 179 // Wait until the second worker is initialized. Verify that the second worke
r is using the same |
| 178 // thread and Isolate as the first worker. | 180 // thread and Isolate as the first worker. |
| 179 WebThreadSupportingGC* secondThread = &secondWorker->workerBackingThread().b
ackingThread(); | 181 WebThreadSupportingGC* secondThread = &secondWorker->workerBackingThread().b
ackingThread(); |
| 180 ASSERT_EQ(firstThread, secondThread); | 182 ASSERT_EQ(firstThread, secondThread); |
| 181 | 183 |
| 182 v8::Isolate* secondIsolate = secondWorker->isolate(); | 184 v8::Isolate* secondIsolate = secondWorker->isolate(); |
| 183 ASSERT_TRUE(secondIsolate); | 185 ASSERT_TRUE(secondIsolate); |
| 184 EXPECT_EQ(firstIsolate, secondIsolate); | 186 EXPECT_EQ(firstIsolate, secondIsolate); |
| 185 | 187 |
| 186 // Verify that the worker can still successfully execute script. | 188 // Verify that the worker can still successfully execute script. |
| 187 checkWorkerCanExecuteScript(secondWorker.get()); | 189 checkWorkerCanExecuteScript(secondWorker.get()); |
| 188 | 190 |
| 189 secondWorker->terminateAndWait(); | 191 secondWorker->terminateAndWait(); |
| 190 } | 192 } |
| 191 | 193 |
| 192 // Tests that a new WebThread is created if all existing workers are terminated
before a new worker is created. | 194 // Tests that a new WebThread is created if all existing workers are terminated
before a new worker is created. |
| 193 TEST_F(CompositorWorkerThreadTest, TerminateFirstAndCreateSecond) | 195 TEST_F(CompositorWorkerThreadTest, TerminateFirstAndCreateSecond) |
| 194 { | 196 { |
| 195 // Create the first worker, wait until it is initialized, and terminate it. | 197 // Create the first worker, wait until it is initialized, and terminate it. |
| 196 OwnPtr<CompositorWorkerThread> compositorWorker = createCompositorWorker(); | 198 std::unique_ptr<CompositorWorkerThread> compositorWorker = createCompositorW
orker(); |
| 197 WorkerBackingThread* workerBackingThread = &compositorWorker->workerBackingT
hread(); | 199 WorkerBackingThread* workerBackingThread = &compositorWorker->workerBackingT
hread(); |
| 198 WebThreadSupportingGC* firstThread = &compositorWorker->workerBackingThread(
).backingThread(); | 200 WebThreadSupportingGC* firstThread = &compositorWorker->workerBackingThread(
).backingThread(); |
| 199 checkWorkerCanExecuteScript(compositorWorker.get()); | 201 checkWorkerCanExecuteScript(compositorWorker.get()); |
| 200 | 202 |
| 201 ASSERT_EQ(2u, workerBackingThread->workerScriptCount()); | 203 ASSERT_EQ(2u, workerBackingThread->workerScriptCount()); |
| 202 compositorWorker->terminateAndWait(); | 204 compositorWorker->terminateAndWait(); |
| 203 | 205 |
| 204 ASSERT_EQ(1u, workerBackingThread->workerScriptCount()); | 206 ASSERT_EQ(1u, workerBackingThread->workerScriptCount()); |
| 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 ASSERT_EQ(2u, workerBackingThread->workerScriptCount()); | 213 ASSERT_EQ(2u, workerBackingThread->workerScriptCount()); |
| 212 | 214 |
| 213 compositorWorker->terminateAndWait(); | 215 compositorWorker->terminateAndWait(); |
| 214 } | 216 } |
| 215 | 217 |
| 216 // Tests that v8::Isolate and WebThread are correctly set-up if a worker is crea
ted while another is terminating. | 218 // Tests that v8::Isolate and WebThread are correctly set-up if a worker is crea
ted while another is terminating. |
| 217 TEST_F(CompositorWorkerThreadTest, CreatingSecondDuringTerminationOfFirst) | 219 TEST_F(CompositorWorkerThreadTest, CreatingSecondDuringTerminationOfFirst) |
| 218 { | 220 { |
| 219 OwnPtr<CompositorWorkerThread> firstWorker = createCompositorWorker(); | 221 std::unique_ptr<CompositorWorkerThread> firstWorker = createCompositorWorker
(); |
| 220 checkWorkerCanExecuteScript(firstWorker.get()); | 222 checkWorkerCanExecuteScript(firstWorker.get()); |
| 221 v8::Isolate* firstIsolate = firstWorker->isolate(); | 223 v8::Isolate* firstIsolate = firstWorker->isolate(); |
| 222 ASSERT_TRUE(firstIsolate); | 224 ASSERT_TRUE(firstIsolate); |
| 223 | 225 |
| 224 // Request termination of the first worker and create the second worker | 226 // Request termination of the first worker and create the second worker |
| 225 // as soon as possible. | 227 // as soon as possible. |
| 226 EXPECT_EQ(2u, firstWorker->workerBackingThread().workerScriptCount()); | 228 EXPECT_EQ(2u, firstWorker->workerBackingThread().workerScriptCount()); |
| 227 firstWorker->terminate(); | 229 firstWorker->terminate(); |
| 228 // We don't wait for its termination. | 230 // We don't wait for its termination. |
| 229 // Note: We rely on the assumption that the termination steps don't run | 231 // Note: We rely on the assumption that the termination steps don't run |
| 230 // on the worker thread so quickly. This could be a source of flakiness. | 232 // on the worker thread so quickly. This could be a source of flakiness. |
| 231 | 233 |
| 232 OwnPtr<CompositorWorkerThread> secondWorker = createCompositorWorker(); | 234 std::unique_ptr<CompositorWorkerThread> secondWorker = createCompositorWorke
r(); |
| 233 | 235 |
| 234 v8::Isolate* secondIsolate = secondWorker->isolate(); | 236 v8::Isolate* secondIsolate = secondWorker->isolate(); |
| 235 ASSERT_TRUE(secondIsolate); | 237 ASSERT_TRUE(secondIsolate); |
| 236 EXPECT_EQ(firstIsolate, secondIsolate); | 238 EXPECT_EQ(firstIsolate, secondIsolate); |
| 237 | 239 |
| 238 // Verify that the isolate can run some scripts correctly in the second work
er. | 240 // Verify that the isolate can run some scripts correctly in the second work
er. |
| 239 checkWorkerCanExecuteScript(secondWorker.get()); | 241 checkWorkerCanExecuteScript(secondWorker.get()); |
| 240 secondWorker->terminateAndWait(); | 242 secondWorker->terminateAndWait(); |
| 241 } | 243 } |
| 242 | 244 |
| 243 } // namespace blink | 245 } // namespace blink |
| OLD | NEW |