| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 "bindings/core/v8/ScriptCallStack.h" |
| 6 #include "bindings/core/v8/V8CacheOptions.h" |
| 7 #include "bindings/core/v8/V8GCController.h" |
| 8 #include "core/frame/csp/ContentSecurityPolicy.h" |
| 9 #include "core/inspector/ConsoleMessage.h" |
| 10 #include "core/workers/WorkerClients.h" |
| 11 #include "core/workers/WorkerLoaderProxy.h" |
| 12 #include "core/workers/WorkerReportingProxy.h" |
| 5 #include "core/workers/WorkerThread.h" | 13 #include "core/workers/WorkerThread.h" |
| 6 | |
| 7 #include "bindings/core/v8/ScriptCallStack.h" | |
| 8 #include "bindings/core/v8/V8GCController.h" | |
| 9 #include "core/inspector/ConsoleMessage.h" | |
| 10 #include "core/workers/WorkerReportingProxy.h" | |
| 11 #include "core/workers/WorkerThreadStartupData.h" | 14 #include "core/workers/WorkerThreadStartupData.h" |
| 12 #include "platform/NotImplemented.h" | 15 #include "platform/NotImplemented.h" |
| 13 #include "platform/Task.h" | |
| 14 #include "platform/ThreadSafeFunctional.h" | 16 #include "platform/ThreadSafeFunctional.h" |
| 15 #include "platform/WaitableEvent.h" | 17 #include "platform/WaitableEvent.h" |
| 18 #include "platform/WebThreadSupportingGC.h" |
| 19 #include "platform/heap/Handle.h" |
| 20 #include "platform/network/ContentSecurityPolicyParsers.h" |
| 21 #include "platform/weborigin/KURL.h" |
| 22 #include "platform/weborigin/SecurityOrigin.h" |
| 16 #include "public/platform/WebAddressSpace.h" | 23 #include "public/platform/WebAddressSpace.h" |
| 17 #include "public/platform/WebScheduler.h" | |
| 18 #include "testing/gmock/include/gmock/gmock.h" | 24 #include "testing/gmock/include/gmock/gmock.h" |
| 19 #include "testing/gtest/include/gtest/gtest.h" | 25 #include "wtf/CurrentTime.h" |
| 20 | 26 #include "wtf/Forward.h" |
| 21 using testing::_; | 27 #include "wtf/OwnPtr.h" |
| 22 using testing::AtMost; | 28 #include "wtf/PassOwnPtr.h" |
| 23 using testing::Invoke; | 29 #include "wtf/Vector.h" |
| 24 using testing::Return; | 30 #include <v8.h> |
| 25 using testing::Mock; | |
| 26 | 31 |
| 27 namespace blink { | 32 namespace blink { |
| 28 | 33 |
| 29 namespace { | |
| 30 | |
| 31 class MockWorkerLoaderProxyProvider : public WorkerLoaderProxyProvider { | 34 class MockWorkerLoaderProxyProvider : public WorkerLoaderProxyProvider { |
| 32 public: | 35 public: |
| 33 MockWorkerLoaderProxyProvider() { } | 36 MockWorkerLoaderProxyProvider() { } |
| 34 ~MockWorkerLoaderProxyProvider() override { } | 37 ~MockWorkerLoaderProxyProvider() override { } |
| 35 | 38 |
| 36 void postTaskToLoader(PassOwnPtr<ExecutionContextTask>) override | 39 void postTaskToLoader(PassOwnPtr<ExecutionContextTask>) override |
| 37 { | 40 { |
| 38 notImplemented(); | 41 notImplemented(); |
| 39 } | 42 } |
| 40 | 43 |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 98 | 101 |
| 99 class WorkerThreadForTest : public WorkerThread { | 102 class WorkerThreadForTest : public WorkerThread { |
| 100 public: | 103 public: |
| 101 WorkerThreadForTest( | 104 WorkerThreadForTest( |
| 102 WorkerLoaderProxyProvider* mockWorkerLoaderProxyProvider, | 105 WorkerLoaderProxyProvider* mockWorkerLoaderProxyProvider, |
| 103 WorkerReportingProxy& mockWorkerReportingProxy) | 106 WorkerReportingProxy& mockWorkerReportingProxy) |
| 104 : WorkerThread(WorkerLoaderProxy::create(mockWorkerLoaderProxyProvider),
mockWorkerReportingProxy) | 107 : WorkerThread(WorkerLoaderProxy::create(mockWorkerLoaderProxyProvider),
mockWorkerReportingProxy) |
| 105 , m_thread(WebThreadSupportingGC::create("Test thread")) | 108 , m_thread(WebThreadSupportingGC::create("Test thread")) |
| 106 , m_scriptLoadedEvent(adoptPtr(new WaitableEvent())) | 109 , m_scriptLoadedEvent(adoptPtr(new WaitableEvent())) |
| 107 { | 110 { |
| 111 ASSERT(m_thread); |
| 108 } | 112 } |
| 109 | 113 |
| 110 ~WorkerThreadForTest() override { } | 114 ~WorkerThreadForTest() override { } |
| 111 | 115 |
| 112 // WorkerThread implementation: | 116 // WorkerThread implementation: |
| 113 WebThreadSupportingGC& backingThread() override | 117 WebThreadSupportingGC& backingThread() override |
| 114 { | 118 { |
| 119 ASSERT(m_thread); |
| 115 return *m_thread; | 120 return *m_thread; |
| 116 } | 121 } |
| 117 void willDestroyIsolate() override | 122 void willDestroyIsolate() override |
| 118 { | 123 { |
| 119 V8GCController::collectAllGarbageForTesting(v8::Isolate::GetCurrent()); | 124 V8GCController::collectAllGarbageForTesting(v8::Isolate::GetCurrent()); |
| 120 WorkerThread::willDestroyIsolate(); | 125 WorkerThread::willDestroyIsolate(); |
| 121 } | 126 } |
| 122 | 127 |
| 123 PassRefPtrWillBeRawPtr<WorkerGlobalScope> createWorkerGlobalScope(PassOwnPtr
<WorkerThreadStartupData> startupData) override | 128 PassRefPtrWillBeRawPtr<WorkerGlobalScope> createWorkerGlobalScope(PassOwnPtr
<WorkerThreadStartupData> startupData) override |
| 124 { | 129 { |
| 125 return adoptRefWillBeNoop(new FakeWorkerGlobalScope(startupData->m_scrip
tURL, startupData->m_userAgent, this, startupData->m_starterOriginPrivilegeData.
release(), startupData->m_workerClients.release())); | 130 return adoptRefWillBeNoop(new FakeWorkerGlobalScope(startupData->m_scrip
tURL, startupData->m_userAgent, this, startupData->m_starterOriginPrivilegeData.
release(), startupData->m_workerClients.release())); |
| 126 } | 131 } |
| 127 | 132 |
| 128 void waitUntilScriptLoaded() | 133 void waitUntilScriptLoaded() |
| 129 { | 134 { |
| 130 m_scriptLoadedEvent->wait(); | 135 m_scriptLoadedEvent->wait(); |
| 131 } | 136 } |
| 132 | 137 |
| 133 void scriptLoaded() | 138 void scriptLoaded() |
| 134 { | 139 { |
| 135 m_scriptLoadedEvent->signal(); | 140 m_scriptLoadedEvent->signal(); |
| 136 } | 141 } |
| 137 | 142 |
| 138 private: | 143 void startWithSourceCode(SecurityOrigin* securityOrigin, const String& sourc
e) |
| 139 OwnPtr<WebThreadSupportingGC> m_thread; | |
| 140 OwnPtr<WaitableEvent> m_scriptLoadedEvent; | |
| 141 }; | |
| 142 | |
| 143 void notifyScriptLoadedEventToWorkerThreadForTest(WorkerThread* thread) | |
| 144 { | |
| 145 static_cast<WorkerThreadForTest*>(thread)->scriptLoaded(); | |
| 146 } | |
| 147 | |
| 148 } // namespace | |
| 149 | |
| 150 class WorkerThreadTest : public testing::Test { | |
| 151 public: | |
| 152 void SetUp() override | |
| 153 { | |
| 154 m_mockWorkerLoaderProxyProvider = adoptPtr(new MockWorkerLoaderProxyProv
ider()); | |
| 155 m_mockWorkerReportingProxy = adoptPtr(new MockWorkerReportingProxy()); | |
| 156 m_securityOrigin = SecurityOrigin::create(KURL(ParsedURLString, "http://
fake.url/")); | |
| 157 m_workerThread = adoptPtr(new WorkerThreadForTest( | |
| 158 m_mockWorkerLoaderProxyProvider.get(), | |
| 159 *m_mockWorkerReportingProxy)); | |
| 160 } | |
| 161 | |
| 162 void TearDown() override | |
| 163 { | |
| 164 m_workerThread->workerLoaderProxy()->detachProvider(m_mockWorkerLoaderPr
oxyProvider.get()); | |
| 165 } | |
| 166 | |
| 167 void start() | |
| 168 { | |
| 169 startWithSourceCode("//fake source code"); | |
| 170 } | |
| 171 | |
| 172 void startWithSourceCode(const String& source) | |
| 173 { | 144 { |
| 174 OwnPtr<Vector<CSPHeaderAndType>> headers = adoptPtr(new Vector<CSPHeader
AndType>()); | 145 OwnPtr<Vector<CSPHeaderAndType>> headers = adoptPtr(new Vector<CSPHeader
AndType>()); |
| 175 CSPHeaderAndType headerAndType("contentSecurityPolicy", ContentSecurityP
olicyHeaderTypeReport); | 146 CSPHeaderAndType headerAndType("contentSecurityPolicy", ContentSecurityP
olicyHeaderTypeReport); |
| 176 headers->append(headerAndType); | 147 headers->append(headerAndType); |
| 177 | 148 |
| 178 OwnPtrWillBeRawPtr<WorkerClients> clients = nullptr; | 149 OwnPtrWillBeRawPtr<WorkerClients> clients = nullptr; |
| 179 | 150 |
| 180 m_workerThread->start(WorkerThreadStartupData::create( | 151 start(WorkerThreadStartupData::create( |
| 181 KURL(ParsedURLString, "http://fake.url/"), | 152 KURL(ParsedURLString, "http://fake.url/"), |
| 182 "fake user agent", | 153 "fake user agent", |
| 183 source, | 154 source, |
| 184 nullptr, | 155 nullptr, |
| 185 DontPauseWorkerGlobalScopeOnStart, | 156 DontPauseWorkerGlobalScopeOnStart, |
| 186 headers.release(), | 157 headers.release(), |
| 187 m_securityOrigin.get(), | 158 securityOrigin, |
| 188 clients.release(), | 159 clients.release(), |
| 189 WebAddressSpaceLocal, | 160 WebAddressSpaceLocal, |
| 190 V8CacheOptionsDefault)); | 161 V8CacheOptionsDefault)); |
| 191 } | 162 } |
| 192 | 163 |
| 193 void waitForInit() | 164 void waitForInit() |
| 194 { | 165 { |
| 195 OwnPtr<WaitableEvent> completionEvent = adoptPtr(new WaitableEvent()); | 166 OwnPtr<WaitableEvent> completionEvent = adoptPtr(new WaitableEvent()); |
| 196 m_workerThread->backingThread().postTask(BLINK_FROM_HERE, threadSafeBind
(&WaitableEvent::signal, AllowCrossThreadAccess(completionEvent.get()))); | 167 backingThread().postTask(BLINK_FROM_HERE, threadSafeBind(&WaitableEvent:
:signal, AllowCrossThreadAccess(completionEvent.get()))); |
| 197 completionEvent->wait(); | 168 completionEvent->wait(); |
| 198 } | 169 } |
| 199 | 170 |
| 200 protected: | 171 private: |
| 201 void expectWorkerLifetimeReportingCalls() | 172 OwnPtr<WebThreadSupportingGC> m_thread; |
| 202 { | 173 OwnPtr<WaitableEvent> m_scriptLoadedEvent; |
| 203 EXPECT_CALL(*m_mockWorkerReportingProxy, workerGlobalScopeStarted(_)).Ti
mes(1); | |
| 204 EXPECT_CALL(*m_mockWorkerReportingProxy, didEvaluateWorkerScript(true)).
Times(1); | |
| 205 EXPECT_CALL(*m_mockWorkerReportingProxy, workerThreadTerminated()).Times
(1); | |
| 206 EXPECT_CALL(*m_mockWorkerReportingProxy, willDestroyWorkerGlobalScope())
.Times(1); | |
| 207 } | |
| 208 | |
| 209 RefPtr<SecurityOrigin> m_securityOrigin; | |
| 210 OwnPtr<MockWorkerLoaderProxyProvider> m_mockWorkerLoaderProxyProvider; | |
| 211 OwnPtr<MockWorkerReportingProxy> m_mockWorkerReportingProxy; | |
| 212 OwnPtr<WorkerThreadForTest> m_workerThread; | |
| 213 }; | 174 }; |
| 214 | 175 |
| 215 TEST_F(WorkerThreadTest, StartAndStop) | 176 inline void notifyScriptLoadedEventToWorkerThreadForTest(WorkerThread* thread) |
| 216 { | 177 { |
| 217 expectWorkerLifetimeReportingCalls(); | 178 static_cast<WorkerThreadForTest*>(thread)->scriptLoaded(); |
| 218 start(); | |
| 219 waitForInit(); | |
| 220 m_workerThread->terminateAndWait(); | |
| 221 } | |
| 222 | |
| 223 TEST_F(WorkerThreadTest, StartAndStopImmediately) | |
| 224 { | |
| 225 EXPECT_CALL(*m_mockWorkerReportingProxy, workerGlobalScopeStarted(_)) | |
| 226 .Times(AtMost(1)); | |
| 227 EXPECT_CALL(*m_mockWorkerReportingProxy, didEvaluateWorkerScript(_)) | |
| 228 .Times(AtMost(1)); | |
| 229 EXPECT_CALL(*m_mockWorkerReportingProxy, workerThreadTerminated()) | |
| 230 .Times(AtMost(1)); | |
| 231 EXPECT_CALL(*m_mockWorkerReportingProxy, willDestroyWorkerGlobalScope()) | |
| 232 .Times(AtMost(1)); | |
| 233 start(); | |
| 234 m_workerThread->terminateAndWait(); | |
| 235 } | |
| 236 | |
| 237 TEST_F(WorkerThreadTest, StartAndStopOnScriptLoaded) | |
| 238 { | |
| 239 // Use a JavaScript source code that makes an infinite loop so that we can | |
| 240 // catch some kind of issues as a timeout. | |
| 241 const String source("while(true) {}"); | |
| 242 | |
| 243 EXPECT_CALL(*m_mockWorkerReportingProxy, workerGlobalScopeStarted(_)) | |
| 244 .Times(AtMost(1)); | |
| 245 EXPECT_CALL(*m_mockWorkerReportingProxy, didEvaluateWorkerScript(_)) | |
| 246 .Times(AtMost(1)); | |
| 247 EXPECT_CALL(*m_mockWorkerReportingProxy, workerThreadTerminated()) | |
| 248 .Times(AtMost(1)); | |
| 249 EXPECT_CALL(*m_mockWorkerReportingProxy, willDestroyWorkerGlobalScope()) | |
| 250 .Times(AtMost(1)); | |
| 251 startWithSourceCode(source); | |
| 252 m_workerThread->waitUntilScriptLoaded(); | |
| 253 m_workerThread->terminateAndWait(); | |
| 254 } | 179 } |
| 255 | 180 |
| 256 } // namespace blink | 181 } // namespace blink |
| OLD | NEW |