Commit c1a13e6d authored by Alok Priyadarshi's avatar Alok Priyadarshi Committed by Commit Bot

Pass WorkerClients into WorkerThreadForTest::StartWithSourceCode.

Also rearranges code so that WorkerThreadForTest can be defined in one
place instead of having to declare FakeWorkerGlobalScope in between. No
functional change is done here. Purley moving the code around.

Split from
https://chromium-review.googlesource.com/c/chromium/src/+/636684

Bug: 750311
Change-Id: Ic79600567401604651ad579542ee5aabbeb5fa5f
Reviewed-on: https://chromium-review.googlesource.com/691301
Commit-Queue: Alok Priyadarshi <alokp@chromium.org>
Reviewed-by: default avatarHiroki Nakagawa <nhiroki@chromium.org>
Reviewed-by: default avatarYutaka Hirano <yhirano@chromium.org>
Cr-Commit-Position: refs/heads/master@{#505299}
parent 19a84daf
...@@ -52,6 +52,31 @@ class MockWorkerThreadLifecycleObserver final ...@@ -52,6 +52,31 @@ class MockWorkerThreadLifecycleObserver final
MOCK_METHOD1(ContextDestroyed, void(WorkerThreadLifecycleContext*)); MOCK_METHOD1(ContextDestroyed, void(WorkerThreadLifecycleContext*));
}; };
class FakeWorkerGlobalScope : public WorkerGlobalScope {
public:
FakeWorkerGlobalScope(const KURL& url,
const String& user_agent,
WorkerThread* thread,
std::unique_ptr<SecurityOrigin::PrivilegeData>
starter_origin_privilege_data,
WorkerClients* worker_clients)
: WorkerGlobalScope(url,
user_agent,
thread,
MonotonicallyIncreasingTime(),
std::move(starter_origin_privilege_data),
worker_clients) {}
~FakeWorkerGlobalScope() override {}
// EventTarget
const AtomicString& InterfaceName() const override {
return EventTargetNames::DedicatedWorkerGlobalScope;
}
void ExceptionThrown(ErrorEvent*) override {}
};
class WorkerThreadForTest : public WorkerThread { class WorkerThreadForTest : public WorkerThread {
public: public:
WorkerThreadForTest(ThreadableLoadingContext* loading_context, WorkerThreadForTest(ThreadableLoadingContext* loading_context,
...@@ -65,78 +90,50 @@ class WorkerThreadForTest : public WorkerThread { ...@@ -65,78 +90,50 @@ class WorkerThreadForTest : public WorkerThread {
WorkerBackingThread& GetWorkerBackingThread() override { WorkerBackingThread& GetWorkerBackingThread() override {
return *worker_backing_thread_; return *worker_backing_thread_;
} }
void ClearWorkerBackingThread() override { worker_backing_thread_ = nullptr; } void ClearWorkerBackingThread() override { worker_backing_thread_.reset(); }
WorkerOrWorkletGlobalScope* CreateWorkerGlobalScope(
std::unique_ptr<GlobalScopeCreationParams>) override;
void StartWithSourceCode(SecurityOrigin* security_origin, void StartWithSourceCode(SecurityOrigin* security_origin,
const String& source, const String& source,
ParentFrameTaskRunners* parent_frame_task_runners) { ParentFrameTaskRunners* parent_frame_task_runners,
std::unique_ptr<Vector<CSPHeaderAndType>> headers = WorkerClients* worker_clients = nullptr) {
WTF::MakeUnique<Vector<CSPHeaderAndType>>(); auto headers = WTF::MakeUnique<Vector<CSPHeaderAndType>>();
CSPHeaderAndType header_and_type("contentSecurityPolicy", CSPHeaderAndType header_and_type("contentSecurityPolicy",
kContentSecurityPolicyHeaderTypeReport); kContentSecurityPolicyHeaderTypeReport);
headers->push_back(header_and_type); headers->push_back(header_and_type);
WorkerClients* clients = nullptr; auto creation_params = WTF::MakeUnique<GlobalScopeCreationParams>(
KURL(kParsedURLString, "http://fake.url/"), "fake user agent", source,
nullptr, kDontPauseWorkerGlobalScopeOnStart, headers.get(), "",
security_origin, worker_clients, kWebAddressSpaceLocal, nullptr,
nullptr, kV8CacheOptionsDefault);
Start(WTF::MakeUnique<GlobalScopeCreationParams>( Start(std::move(creation_params),
KURL(kParsedURLString, "http://fake.url/"), "fake user agent",
source, nullptr, kDontPauseWorkerGlobalScopeOnStart,
headers.get(), "", security_origin, clients,
kWebAddressSpaceLocal, nullptr, nullptr, kV8CacheOptionsDefault),
WorkerBackingThreadStartupData::CreateDefault(), WorkerBackingThreadStartupData::CreateDefault(),
parent_frame_task_runners); parent_frame_task_runners);
} }
void WaitForInit() { void WaitForInit() {
std::unique_ptr<WaitableEvent> completion_event = WaitableEvent completion_event;
WTF::MakeUnique<WaitableEvent>();
GetWorkerBackingThread().BackingThread().PostTask( GetWorkerBackingThread().BackingThread().PostTask(
BLINK_FROM_HERE, BLINK_FROM_HERE,
CrossThreadBind(&WaitableEvent::Signal, CrossThreadBind(&WaitableEvent::Signal,
CrossThreadUnretained(completion_event.get()))); CrossThreadUnretained(&completion_event)));
completion_event->Wait(); completion_event.Wait();
} }
private: protected:
std::unique_ptr<WorkerBackingThread> worker_backing_thread_; WorkerOrWorkletGlobalScope* CreateWorkerGlobalScope(
}; std::unique_ptr<GlobalScopeCreationParams> creation_params) override {
return new FakeWorkerGlobalScope(
class FakeWorkerGlobalScope : public WorkerGlobalScope { creation_params->script_url, creation_params->user_agent, this,
public: std::move(creation_params->starter_origin_privilege_data),
FakeWorkerGlobalScope(const KURL& url, std::move(creation_params->worker_clients));
const String& user_agent,
WorkerThreadForTest* thread,
std::unique_ptr<SecurityOrigin::PrivilegeData>
starter_origin_privilege_data,
WorkerClients* worker_clients)
: WorkerGlobalScope(url,
user_agent,
thread,
MonotonicallyIncreasingTime(),
std::move(starter_origin_privilege_data),
worker_clients) {}
~FakeWorkerGlobalScope() override {}
// EventTarget
const AtomicString& InterfaceName() const override {
return EventTargetNames::DedicatedWorkerGlobalScope;
} }
void ExceptionThrown(ErrorEvent*) override {} private:
std::unique_ptr<WorkerBackingThread> worker_backing_thread_;
}; };
inline WorkerOrWorkletGlobalScope* WorkerThreadForTest::CreateWorkerGlobalScope(
std::unique_ptr<GlobalScopeCreationParams> creation_params) {
return new FakeWorkerGlobalScope(
creation_params->script_url, creation_params->user_agent, this,
std::move(creation_params->starter_origin_privilege_data),
std::move(creation_params->worker_clients));
}
} // namespace blink } // namespace blink
#endif // WorkerThreadTestHelper_h #endif // WorkerThreadTestHelper_h
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment