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