Commit 21fa2d81 authored by Makoto Shimazu's avatar Makoto Shimazu Committed by Commit Bot

Add an unit test for ServiceWorkerContextClient

SWContextClient is getting complicated rather than just passing through the
events, especially after adding renderer-side timers. This CL is for adding unit
tests for SWContextClient.

Bug: 774374
Change-Id: Iaaf3944d0a11d812c9f9f3e2f9bfe2f334fcba19
Reviewed-on: https://chromium-review.googlesource.com/799518
Commit-Queue: Makoto Shimazu <shimazu@chromium.org>
Reviewed-by: default avatarMatt Falkenhagen <falken@chromium.org>
Cr-Commit-Position: refs/heads/master@{#522355}
parent 85ae39e9
...@@ -9,6 +9,7 @@ ...@@ -9,6 +9,7 @@
#include "base/memory/ptr_util.h" #include "base/memory/ptr_util.h"
#include "base/strings/utf_string_conversions.h" #include "base/strings/utf_string_conversions.h"
#include "content/child/scoped_child_process_reference.h" #include "content/child/scoped_child_process_reference.h"
#include "content/child/thread_safe_sender.h"
#include "content/common/service_worker/embedded_worker_messages.h" #include "content/common/service_worker/embedded_worker_messages.h"
#include "content/common/service_worker/service_worker_utils.h" #include "content/common/service_worker/service_worker_utils.h"
#include "content/public/common/content_client.h" #include "content/public/common/content_client.h"
...@@ -76,7 +77,8 @@ void EmbeddedWorkerInstanceClientImpl::StartWorker( ...@@ -76,7 +77,8 @@ void EmbeddedWorkerInstanceClientImpl::StartWorker(
ServiceWorkerUtils::IsScriptStreamingEnabled() && installed_scripts_info, ServiceWorkerUtils::IsScriptStreamingEnabled() && installed_scripts_info,
std::move(dispatcher_request), std::move(controller_request), std::move(dispatcher_request), std::move(controller_request),
std::move(service_worker_host), std::move(instance_host), std::move(service_worker_host), std::move(instance_host),
std::move(provider_info), std::move(temporal_self_)); std::move(provider_info), std::move(temporal_self_),
ChildThreadImpl::current()->thread_safe_sender(), io_thread_runner_);
client->set_blink_initialized_time(blink_initialized_time_); client->set_blink_initialized_time(blink_initialized_time_);
client->set_start_worker_received_time(base::TimeTicks::Now()); client->set_start_worker_received_time(base::TimeTicks::Now());
wrapper_ = StartWorkerContext( wrapper_ = StartWorkerContext(
......
...@@ -61,6 +61,7 @@ ...@@ -61,6 +61,7 @@
#include "storage/common/blob_storage/blob_handle.h" #include "storage/common/blob_storage/blob_handle.h"
#include "third_party/WebKit/common/blob/blob.mojom.h" #include "third_party/WebKit/common/blob/blob.mojom.h"
#include "third_party/WebKit/common/blob/blob_registry.mojom.h" #include "third_party/WebKit/common/blob/blob_registry.mojom.h"
#include "third_party/WebKit/common/message_port/message_port_channel.h"
#include "third_party/WebKit/common/service_worker/service_worker_error_type.mojom.h" #include "third_party/WebKit/common/service_worker/service_worker_error_type.mojom.h"
#include "third_party/WebKit/common/service_worker/service_worker_event_status.mojom.h" #include "third_party/WebKit/common/service_worker/service_worker_event_status.mojom.h"
#include "third_party/WebKit/common/service_worker/service_worker_object.mojom.h" #include "third_party/WebKit/common/service_worker/service_worker_object.mojom.h"
...@@ -623,14 +624,16 @@ ServiceWorkerContextClient::ServiceWorkerContextClient( ...@@ -623,14 +624,16 @@ ServiceWorkerContextClient::ServiceWorkerContextClient(
blink::mojom::ServiceWorkerHostAssociatedPtrInfo service_worker_host, blink::mojom::ServiceWorkerHostAssociatedPtrInfo service_worker_host,
mojom::EmbeddedWorkerInstanceHostAssociatedPtrInfo instance_host, mojom::EmbeddedWorkerInstanceHostAssociatedPtrInfo instance_host,
mojom::ServiceWorkerProviderInfoForStartWorkerPtr provider_info, mojom::ServiceWorkerProviderInfoForStartWorkerPtr provider_info,
std::unique_ptr<EmbeddedWorkerInstanceClientImpl> embedded_worker_client) std::unique_ptr<EmbeddedWorkerInstanceClientImpl> embedded_worker_client,
scoped_refptr<ThreadSafeSender> sender,
scoped_refptr<base::SingleThreadTaskRunner> io_thread_task_runner)
: embedded_worker_id_(embedded_worker_id), : embedded_worker_id_(embedded_worker_id),
service_worker_version_id_(service_worker_version_id), service_worker_version_id_(service_worker_version_id),
service_worker_scope_(service_worker_scope), service_worker_scope_(service_worker_scope),
script_url_(script_url), script_url_(script_url),
sender_(ChildThreadImpl::current()->thread_safe_sender()), sender_(sender),
main_thread_task_runner_(base::ThreadTaskRunnerHandle::Get()), main_thread_task_runner_(base::ThreadTaskRunnerHandle::Get()),
io_thread_task_runner_(ChildThreadImpl::current()->GetIOTaskRunner()), io_thread_task_runner_(io_thread_task_runner),
proxy_(nullptr), proxy_(nullptr),
pending_dispatcher_request_(std::move(dispatcher_request)), pending_dispatcher_request_(std::move(dispatcher_request)),
pending_controller_request_(std::move(controller_request)), pending_controller_request_(std::move(controller_request)),
...@@ -643,7 +646,7 @@ ServiceWorkerContextClient::ServiceWorkerContextClient( ...@@ -643,7 +646,7 @@ ServiceWorkerContextClient::ServiceWorkerContextClient(
// Create a content::ServiceWorkerNetworkProvider for this data source so // Create a content::ServiceWorkerNetworkProvider for this data source so
// we can observe its requests. // we can observe its requests.
pending_network_provider_ = ServiceWorkerNetworkProvider::CreateForController( pending_network_provider_ = ServiceWorkerNetworkProvider::CreateForController(
std::move(provider_info)); std::move(provider_info), sender_);
provider_context_ = pending_network_provider_->context(); provider_context_ = pending_network_provider_->context();
TRACE_EVENT_NESTABLE_ASYNC_BEGIN1("ServiceWorker", TRACE_EVENT_NESTABLE_ASYNC_BEGIN1("ServiceWorker",
...@@ -1840,4 +1843,9 @@ ServiceWorkerContextClient::GetWeakPtr() { ...@@ -1840,4 +1843,9 @@ ServiceWorkerContextClient::GetWeakPtr() {
return context_->weak_factory.GetWeakPtr(); return context_->weak_factory.GetWeakPtr();
} }
// static
void ServiceWorkerContextClient::ResetThreadSpecificInstanceForTesting() {
g_worker_client_tls.Pointer()->Set(nullptr);
}
} // namespace content } // namespace content
...@@ -75,8 +75,9 @@ class WebWorkerFetchContext; ...@@ -75,8 +75,9 @@ class WebWorkerFetchContext;
// //
// Unless otherwise noted (here or in base class documentation), all methods are // Unless otherwise noted (here or in base class documentation), all methods are
// called on the worker thread. // called on the worker thread.
class ServiceWorkerContextClient : public blink::WebServiceWorkerContextClient, class CONTENT_EXPORT ServiceWorkerContextClient
public mojom::ServiceWorkerEventDispatcher { : public blink::WebServiceWorkerContextClient,
public mojom::ServiceWorkerEventDispatcher {
public: public:
// Returns a thread-specific client instance. This does NOT create a // Returns a thread-specific client instance. This does NOT create a
// new instance. // new instance.
...@@ -96,7 +97,9 @@ class ServiceWorkerContextClient : public blink::WebServiceWorkerContextClient, ...@@ -96,7 +97,9 @@ class ServiceWorkerContextClient : public blink::WebServiceWorkerContextClient,
blink::mojom::ServiceWorkerHostAssociatedPtrInfo service_worker_host, blink::mojom::ServiceWorkerHostAssociatedPtrInfo service_worker_host,
mojom::EmbeddedWorkerInstanceHostAssociatedPtrInfo instance_host, mojom::EmbeddedWorkerInstanceHostAssociatedPtrInfo instance_host,
mojom::ServiceWorkerProviderInfoForStartWorkerPtr provider_info, mojom::ServiceWorkerProviderInfoForStartWorkerPtr provider_info,
std::unique_ptr<EmbeddedWorkerInstanceClientImpl> embedded_worker_client); std::unique_ptr<EmbeddedWorkerInstanceClientImpl> embedded_worker_client,
scoped_refptr<ThreadSafeSender> sender,
scoped_refptr<base::SingleThreadTaskRunner> io_thread_task_runner);
~ServiceWorkerContextClient() override; ~ServiceWorkerContextClient() override;
// Called on the main thread. // Called on the main thread.
...@@ -253,6 +256,7 @@ class ServiceWorkerContextClient : public blink::WebServiceWorkerContextClient, ...@@ -253,6 +256,7 @@ class ServiceWorkerContextClient : public blink::WebServiceWorkerContextClient,
struct WorkerContextData; struct WorkerContextData;
class NavigationPreloadRequest; class NavigationPreloadRequest;
friend class ControllerServiceWorkerImpl; friend class ControllerServiceWorkerImpl;
friend class ServiceWorkerContextClientTest;
// Get routing_id for sending message to the ServiceWorkerVersion // Get routing_id for sending message to the ServiceWorkerVersion
// in the browser process. // in the browser process.
...@@ -381,6 +385,8 @@ class ServiceWorkerContextClient : public blink::WebServiceWorkerContextClient, ...@@ -381,6 +385,8 @@ class ServiceWorkerContextClient : public blink::WebServiceWorkerContextClient,
base::WeakPtr<ServiceWorkerContextClient> GetWeakPtr(); base::WeakPtr<ServiceWorkerContextClient> GetWeakPtr();
static void ResetThreadSpecificInstanceForTesting();
const int embedded_worker_id_; const int embedded_worker_id_;
const int64_t service_worker_version_id_; const int64_t service_worker_version_id_;
const GURL service_worker_scope_; const GURL service_worker_scope_;
......
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "content/renderer/service_worker/service_worker_context_client.h"
#include "base/message_loop/message_loop.h"
#include "base/run_loop.h"
#include "content/child/thread_safe_sender.h"
#include "content/renderer/service_worker/embedded_worker_instance_client_impl.h"
#include "content/renderer/service_worker/service_worker_dispatcher.h"
#include "content/renderer/worker_thread_registry.h"
#include "ipc/ipc_sync_message_filter.h"
#include "ipc/ipc_test_sink.h"
#include "mojo/public/cpp/bindings/associated_interface_ptr.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/WebKit/common/message_port/message_port_channel.h"
#include "third_party/WebKit/common/service_worker/service_worker_registration.mojom.h"
#include "third_party/WebKit/public/platform/WebDataConsumerHandle.h"
#include "third_party/WebKit/public/platform/WebString.h"
#include "third_party/WebKit/public/platform/WebURLResponse.h"
#include "third_party/WebKit/public/platform/modules/background_fetch/WebBackgroundFetchSettledFetch.h"
#include "third_party/WebKit/public/platform/modules/notifications/WebNotificationData.h"
#include "third_party/WebKit/public/platform/modules/payments/WebPaymentRequestEventData.h"
#include "third_party/WebKit/public/platform/modules/serviceworker/WebServiceWorkerClientsInfo.h"
#include "third_party/WebKit/public/platform/modules/serviceworker/WebServiceWorkerError.h"
#include "third_party/WebKit/public/platform/modules/serviceworker/WebServiceWorkerRequest.h"
#include "third_party/WebKit/public/web/modules/serviceworker/WebServiceWorkerContextProxy.h"
namespace content {
namespace {
// Pipes connected to the context client.
struct ContextClientPipes {
// From the browser to ServiceWorkerContextClient.
mojom::ServiceWorkerEventDispatcherPtr event_dispatcher;
mojom::ControllerServiceWorkerPtr controller;
blink::mojom::ServiceWorkerRegistrationObjectAssociatedPtr registration;
// From ServiceWorkerContextClient to the browser.
blink::mojom::ServiceWorkerHostAssociatedRequest service_worker_host_request;
mojom::EmbeddedWorkerInstanceHostAssociatedRequest
embedded_worker_host_request;
blink::mojom::ServiceWorkerRegistrationObjectHostAssociatedRequest
registration_host_request;
};
class TestSender : public ThreadSafeSender {
public:
explicit TestSender(IPC::TestSink* ipc_sink)
: ThreadSafeSender(nullptr, nullptr), ipc_sink_(ipc_sink) {}
bool Send(IPC::Message* message) override { return ipc_sink_->Send(message); }
private:
~TestSender() override = default;
IPC::TestSink* ipc_sink_;
DISALLOW_COPY_AND_ASSIGN(TestSender);
};
class MockWebServiceWorkerContextProxy
: public blink::WebServiceWorkerContextProxy {
public:
~MockWebServiceWorkerContextProxy() override = default;
void SetRegistration(
std::unique_ptr<blink::WebServiceWorkerRegistration::Handle> handle)
override {
registration_handle_ = std::move(handle);
}
bool HasFetchEventHandler() override { return false; }
void DispatchActivateEvent(int event_id) override { NOTREACHED(); }
void DispatchBackgroundFetchAbortEvent(
int event_id,
const blink::WebString& developer_id) override {
NOTREACHED();
}
void DispatchBackgroundFetchClickEvent(int event_id,
const blink::WebString& developer_id,
BackgroundFetchState status) override {
NOTREACHED();
}
void DispatchBackgroundFetchFailEvent(
int event_id,
const blink::WebString& developer_id,
const blink::WebVector<blink::WebBackgroundFetchSettledFetch>& fetches)
override {
NOTREACHED();
}
void DispatchBackgroundFetchedEvent(
int event_id,
const blink::WebString& developer_id,
const blink::WebString& unique_id,
const blink::WebVector<blink::WebBackgroundFetchSettledFetch>& fetches)
override {
NOTREACHED();
}
void DispatchExtendableMessageEvent(
int event_id,
const blink::WebString& message,
const blink::WebSecurityOrigin& source_origin,
blink::WebVector<blink::MessagePortChannel>,
const blink::WebServiceWorkerClientInfo&) override {
NOTREACHED();
}
void DispatchExtendableMessageEvent(
int event_id,
const blink::WebString& message,
const blink::WebSecurityOrigin& source_origin,
blink::WebVector<blink::MessagePortChannel>,
std::unique_ptr<blink::WebServiceWorker::Handle>) override {
NOTREACHED();
}
void DispatchInstallEvent(int event_id) override { NOTREACHED(); }
void DispatchFetchEvent(int fetch_event_id,
const blink::WebServiceWorkerRequest& web_request,
bool navigation_preload_sent) override {
NOTREACHED();
}
void DispatchNotificationClickEvent(int event_id,
const blink::WebString& notification_id,
const blink::WebNotificationData&,
int action_index,
const blink::WebString& reply) override {
NOTREACHED();
}
void DispatchNotificationCloseEvent(
int event_id,
const blink::WebString& notification_id,
const blink::WebNotificationData&) override {
NOTREACHED();
}
void DispatchPushEvent(int event_id, const blink::WebString& data) override {
NOTREACHED();
}
void DispatchSyncEvent(int sync_event_id,
const blink::WebString& tag,
LastChanceOption) override {
NOTREACHED();
}
void DispatchAbortPaymentEvent(int event_id) override { NOTREACHED(); }
void DispatchCanMakePaymentEvent(
int event_id,
const blink::WebCanMakePaymentEventData&) override {
NOTREACHED();
}
void DispatchPaymentRequestEvent(
int event_id,
const blink::WebPaymentRequestEventData&) override {
NOTREACHED();
}
void OnNavigationPreloadResponse(
int fetch_event_id,
std::unique_ptr<blink::WebURLResponse>,
std::unique_ptr<blink::WebDataConsumerHandle>) override {
NOTREACHED();
}
void OnNavigationPreloadError(
int fetch_event_id,
std::unique_ptr<blink::WebServiceWorkerError>) override {
NOTREACHED();
}
void OnNavigationPreloadComplete(int fetch_event_id,
double completion_time,
int64_t encoded_data_length,
int64_t encoded_body_length,
int64_t decoded_body_length) override {
NOTREACHED();
}
private:
std::unique_ptr<blink::WebServiceWorkerRegistration::Handle>
registration_handle_;
};
} // namespace
class ServiceWorkerContextClientTest : public testing::Test {
public:
ServiceWorkerContextClientTest() = default;
protected:
void SetUp() override {
sender_ = base::MakeRefCounted<TestSender>(&ipc_sink_);
// Use this thread as the worker thread.
WorkerThreadRegistry::Instance()->DidStartCurrentWorkerThread();
}
void TearDown() override {
ServiceWorkerContextClient::ResetThreadSpecificInstanceForTesting();
ServiceWorkerDispatcher::GetOrCreateThreadSpecificInstance(
sender_, main_task_runner())
->AllowReinstantiationForTesting();
// Unregister this thread from worker threads.
WorkerThreadRegistry::Instance()->WillStopCurrentWorkerThread();
}
// Creates an empty struct to initialize ServiceWorkerProviderContext.
mojom::ServiceWorkerProviderInfoForStartWorkerPtr CreateProviderInfo(
blink::mojom::ServiceWorkerRegistrationObjectHostAssociatedRequest*
out_request,
blink::mojom::ServiceWorkerRegistrationObjectAssociatedPtr* out_ptr) {
auto info = mojom::ServiceWorkerProviderInfoForStartWorker::New();
info->registration =
blink::mojom::ServiceWorkerRegistrationObjectInfo::New();
blink::mojom::ServiceWorkerRegistrationObjectHostAssociatedPtr host_ptr;
*out_request = mojo::MakeRequestAssociatedWithDedicatedPipe(&host_ptr);
info->registration->host_ptr_info = host_ptr.PassInterface();
info->registration->request =
mojo::MakeRequestAssociatedWithDedicatedPipe(out_ptr);
info->registration->installing =
blink::mojom::ServiceWorkerObjectInfo::New();
info->registration->registration_id = 100; // dummy
info->registration->waiting = blink::mojom::ServiceWorkerObjectInfo::New();
info->registration->active = blink::mojom::ServiceWorkerObjectInfo::New();
return info;
}
// Creates an ContextClient, whose pipes are connected to |out_pipes|.
std::unique_ptr<ServiceWorkerContextClient> CreateContextClient(
ContextClientPipes* out_pipes) {
auto event_dispatcher_request =
mojo::MakeRequest(&out_pipes->event_dispatcher);
auto controller_request = mojo::MakeRequest(&out_pipes->controller);
blink::mojom::ServiceWorkerHostAssociatedPtr sw_host_ptr;
out_pipes->service_worker_host_request =
mojo::MakeRequestAssociatedWithDedicatedPipe(&sw_host_ptr);
mojom::EmbeddedWorkerInstanceHostAssociatedPtr embedded_worker_host_ptr;
out_pipes->embedded_worker_host_request =
mojo::MakeRequestAssociatedWithDedicatedPipe(&embedded_worker_host_ptr);
EXPECT_TRUE(sender_);
return std::make_unique<ServiceWorkerContextClient>(
1 /* embeded_worker_id */, 1 /* service_worker_version_id */,
GURL("https://example.com") /* scope */,
GURL("https://example.com/SW.js") /* script_URL */,
false /* is_script_streaming */, std::move(event_dispatcher_request),
std::move(controller_request), sw_host_ptr.PassInterface(),
embedded_worker_host_ptr.PassInterface(),
CreateProviderInfo(&out_pipes->registration_host_request,
&out_pipes->registration),
nullptr /* embedded_worker_client */, sender_, io_task_runner());
}
private:
scoped_refptr<base::SingleThreadTaskRunner> main_task_runner() {
// Use this thread as the main thread.
return message_loop_.task_runner();
}
scoped_refptr<base::SingleThreadTaskRunner> io_task_runner() {
// Use this thread as the IO thread.
return message_loop_.task_runner();
}
base::MessageLoop message_loop_;
IPC::TestSink ipc_sink_;
scoped_refptr<TestSender> sender_;
};
TEST_F(ServiceWorkerContextClientTest, Ping) {
ContextClientPipes pipes;
std::unique_ptr<ServiceWorkerContextClient> context_client =
CreateContextClient(&pipes);
MockWebServiceWorkerContextProxy mock_proxy;
context_client->WorkerContextStarted(&mock_proxy);
// Ping() should invoke the passed callback.
base::RunLoop loop;
pipes.event_dispatcher->Ping(loop.QuitClosure());
loop.Run();
}
} // namespace content
...@@ -43,14 +43,18 @@ void* const kHasBeenDeleted = reinterpret_cast<void*>(0x1); ...@@ -43,14 +43,18 @@ void* const kHasBeenDeleted = reinterpret_cast<void*>(0x1);
} // namespace } // namespace
ServiceWorkerDispatcher::ServiceWorkerDispatcher( ServiceWorkerDispatcher::ServiceWorkerDispatcher(
ThreadSafeSender* thread_safe_sender, scoped_refptr<ThreadSafeSender> thread_safe_sender,
base::SingleThreadTaskRunner* main_thread_task_runner) scoped_refptr<base::SingleThreadTaskRunner> main_thread_task_runner)
: thread_safe_sender_(thread_safe_sender), : thread_safe_sender_(std::move(thread_safe_sender)),
main_thread_task_runner_(main_thread_task_runner) { main_thread_task_runner_(std::move(main_thread_task_runner)) {
g_dispatcher_tls.Pointer()->Set(static_cast<void*>(this)); g_dispatcher_tls.Pointer()->Set(static_cast<void*>(this));
} }
ServiceWorkerDispatcher::~ServiceWorkerDispatcher() { ServiceWorkerDispatcher::~ServiceWorkerDispatcher() {
if (allow_reinstantiation_) {
g_dispatcher_tls.Pointer()->Set(nullptr);
return;
}
g_dispatcher_tls.Pointer()->Set(kHasBeenDeleted); g_dispatcher_tls.Pointer()->Set(kHasBeenDeleted);
} }
...@@ -70,8 +74,8 @@ void ServiceWorkerDispatcher::OnMessageReceived(const IPC::Message& msg) { ...@@ -70,8 +74,8 @@ void ServiceWorkerDispatcher::OnMessageReceived(const IPC::Message& msg) {
ServiceWorkerDispatcher* ServiceWorkerDispatcher*
ServiceWorkerDispatcher::GetOrCreateThreadSpecificInstance( ServiceWorkerDispatcher::GetOrCreateThreadSpecificInstance(
ThreadSafeSender* thread_safe_sender, scoped_refptr<ThreadSafeSender> thread_safe_sender,
base::SingleThreadTaskRunner* main_thread_task_runner) { scoped_refptr<base::SingleThreadTaskRunner> main_thread_task_runner) {
if (g_dispatcher_tls.Pointer()->Get() == kHasBeenDeleted) { if (g_dispatcher_tls.Pointer()->Get() == kHasBeenDeleted) {
NOTREACHED() << "Re-instantiating TLS ServiceWorkerDispatcher."; NOTREACHED() << "Re-instantiating TLS ServiceWorkerDispatcher.";
g_dispatcher_tls.Pointer()->Set(nullptr); g_dispatcher_tls.Pointer()->Set(nullptr);
...@@ -80,8 +84,8 @@ ServiceWorkerDispatcher::GetOrCreateThreadSpecificInstance( ...@@ -80,8 +84,8 @@ ServiceWorkerDispatcher::GetOrCreateThreadSpecificInstance(
return static_cast<ServiceWorkerDispatcher*>( return static_cast<ServiceWorkerDispatcher*>(
g_dispatcher_tls.Pointer()->Get()); g_dispatcher_tls.Pointer()->Get());
ServiceWorkerDispatcher* dispatcher = ServiceWorkerDispatcher* dispatcher = new ServiceWorkerDispatcher(
new ServiceWorkerDispatcher(thread_safe_sender, main_thread_task_runner); std::move(thread_safe_sender), std::move(main_thread_task_runner));
if (WorkerThread::GetCurrentId()) if (WorkerThread::GetCurrentId())
WorkerThread::AddObserver(dispatcher); WorkerThread::AddObserver(dispatcher);
return dispatcher; return dispatcher;
...@@ -94,6 +98,10 @@ ServiceWorkerDispatcher* ServiceWorkerDispatcher::GetThreadSpecificInstance() { ...@@ -94,6 +98,10 @@ ServiceWorkerDispatcher* ServiceWorkerDispatcher::GetThreadSpecificInstance() {
g_dispatcher_tls.Pointer()->Get()); g_dispatcher_tls.Pointer()->Get());
} }
void ServiceWorkerDispatcher::AllowReinstantiationForTesting() {
allow_reinstantiation_ = true;
}
void ServiceWorkerDispatcher::WillStopCurrentWorkerThread() { void ServiceWorkerDispatcher::WillStopCurrentWorkerThread() {
delete this; delete this;
} }
......
...@@ -47,8 +47,8 @@ class WebServiceWorkerImpl; ...@@ -47,8 +47,8 @@ class WebServiceWorkerImpl;
class CONTENT_EXPORT ServiceWorkerDispatcher : public WorkerThread::Observer { class CONTENT_EXPORT ServiceWorkerDispatcher : public WorkerThread::Observer {
public: public:
ServiceWorkerDispatcher( ServiceWorkerDispatcher(
ThreadSafeSender* thread_safe_sender, scoped_refptr<ThreadSafeSender> thread_safe_sender,
base::SingleThreadTaskRunner* main_thread_task_runner); scoped_refptr<base::SingleThreadTaskRunner> main_thread_task_runner);
~ServiceWorkerDispatcher() override; ~ServiceWorkerDispatcher() override;
void OnMessageReceived(const IPC::Message& msg); void OnMessageReceived(const IPC::Message& msg);
...@@ -59,8 +59,8 @@ class CONTENT_EXPORT ServiceWorkerDispatcher : public WorkerThread::Observer { ...@@ -59,8 +59,8 @@ class CONTENT_EXPORT ServiceWorkerDispatcher : public WorkerThread::Observer {
std::unique_ptr<ServiceWorkerHandleReference> handle_ref); std::unique_ptr<ServiceWorkerHandleReference> handle_ref);
static ServiceWorkerDispatcher* GetOrCreateThreadSpecificInstance( static ServiceWorkerDispatcher* GetOrCreateThreadSpecificInstance(
ThreadSafeSender* thread_safe_sender, scoped_refptr<ThreadSafeSender> thread_safe_sender,
base::SingleThreadTaskRunner* main_thread_task_runner); scoped_refptr<base::SingleThreadTaskRunner> main_thread_task_runner);
// Unlike GetOrCreateThreadSpecificInstance() this doesn't create a new // Unlike GetOrCreateThreadSpecificInstance() this doesn't create a new
// instance if thread-local instance doesn't exist. // instance if thread-local instance doesn't exist.
...@@ -77,9 +77,12 @@ class CONTENT_EXPORT ServiceWorkerDispatcher : public WorkerThread::Observer { ...@@ -77,9 +77,12 @@ class CONTENT_EXPORT ServiceWorkerDispatcher : public WorkerThread::Observer {
private: private:
using WorkerObjectMap = std::map<int, WebServiceWorkerImpl*>; using WorkerObjectMap = std::map<int, WebServiceWorkerImpl*>;
friend class ServiceWorkerContextClientTest;
friend class ServiceWorkerDispatcherTest; friend class ServiceWorkerDispatcherTest;
friend class WebServiceWorkerImpl; friend class WebServiceWorkerImpl;
void AllowReinstantiationForTesting();
// WorkerThread::Observer implementation. // WorkerThread::Observer implementation.
void WillStopCurrentWorkerThread() override; void WillStopCurrentWorkerThread() override;
...@@ -91,6 +94,10 @@ class CONTENT_EXPORT ServiceWorkerDispatcher : public WorkerThread::Observer { ...@@ -91,6 +94,10 @@ class CONTENT_EXPORT ServiceWorkerDispatcher : public WorkerThread::Observer {
void AddServiceWorker(int handle_id, WebServiceWorkerImpl* worker); void AddServiceWorker(int handle_id, WebServiceWorkerImpl* worker);
void RemoveServiceWorker(int handle_id); void RemoveServiceWorker(int handle_id);
// True if another dispatcher is allowed to be created on the same thread
// after this instance is destructed.
bool allow_reinstantiation_ = false;
WorkerObjectMap service_workers_; WorkerObjectMap service_workers_;
scoped_refptr<ThreadSafeSender> thread_safe_sender_; scoped_refptr<ThreadSafeSender> thread_safe_sender_;
......
...@@ -219,8 +219,10 @@ ServiceWorkerNetworkProvider::CreateForSharedWorker(int route_id) { ...@@ -219,8 +219,10 @@ ServiceWorkerNetworkProvider::CreateForSharedWorker(int route_id) {
// static // static
std::unique_ptr<ServiceWorkerNetworkProvider> std::unique_ptr<ServiceWorkerNetworkProvider>
ServiceWorkerNetworkProvider::CreateForController( ServiceWorkerNetworkProvider::CreateForController(
mojom::ServiceWorkerProviderInfoForStartWorkerPtr info) { mojom::ServiceWorkerProviderInfoForStartWorkerPtr info,
return base::WrapUnique(new ServiceWorkerNetworkProvider(std::move(info))); scoped_refptr<ThreadSafeSender> sender) {
return base::WrapUnique(
new ServiceWorkerNetworkProvider(std::move(info), std::move(sender)));
} }
// static // static
...@@ -301,12 +303,12 @@ ServiceWorkerNetworkProvider::ServiceWorkerNetworkProvider( ...@@ -301,12 +303,12 @@ ServiceWorkerNetworkProvider::ServiceWorkerNetworkProvider(
// Constructor for service worker execution contexts. // Constructor for service worker execution contexts.
ServiceWorkerNetworkProvider::ServiceWorkerNetworkProvider( ServiceWorkerNetworkProvider::ServiceWorkerNetworkProvider(
mojom::ServiceWorkerProviderInfoForStartWorkerPtr info) { mojom::ServiceWorkerProviderInfoForStartWorkerPtr info,
scoped_refptr<ThreadSafeSender> sender) {
// Initialize the provider context with info for // Initialize the provider context with info for
// ServiceWorkerGlobalScope#registration. // ServiceWorkerGlobalScope#registration.
ThreadSafeSender* sender = ChildThreadImpl::current()->thread_safe_sender();
ServiceWorkerDispatcher::GetOrCreateThreadSpecificInstance( ServiceWorkerDispatcher::GetOrCreateThreadSpecificInstance(
sender, base::ThreadTaskRunnerHandle::Get().get()); sender, base::ThreadTaskRunnerHandle::Get());
// TODO(kinuko): Split ServiceWorkerProviderContext ctor for // TODO(kinuko): Split ServiceWorkerProviderContext ctor for
// controller and controllee. // controller and controllee.
context_ = base::MakeRefCounted<ServiceWorkerProviderContext>( context_ = base::MakeRefCounted<ServiceWorkerProviderContext>(
......
...@@ -33,6 +33,7 @@ class URLLoaderFactory; ...@@ -33,6 +33,7 @@ class URLLoaderFactory;
struct RequestNavigationParams; struct RequestNavigationParams;
class ServiceWorkerProviderContext; class ServiceWorkerProviderContext;
class ChildURLLoaderFactoryGetter; class ChildURLLoaderFactoryGetter;
class ThreadSafeSender;
// ServiceWorkerNetworkProvider enables the browser process to recognize // ServiceWorkerNetworkProvider enables the browser process to recognize
// resource requests from Blink that should be handled by service worker // resource requests from Blink that should be handled by service worker
...@@ -79,7 +80,8 @@ class CONTENT_EXPORT ServiceWorkerNetworkProvider { ...@@ -79,7 +80,8 @@ class CONTENT_EXPORT ServiceWorkerNetworkProvider {
// Creates a ServiceWorkerNetworkProvider for a "controller" (i.e. // Creates a ServiceWorkerNetworkProvider for a "controller" (i.e.
// a service worker execution context). // a service worker execution context).
static std::unique_ptr<ServiceWorkerNetworkProvider> CreateForController( static std::unique_ptr<ServiceWorkerNetworkProvider> CreateForController(
mojom::ServiceWorkerProviderInfoForStartWorkerPtr info); mojom::ServiceWorkerProviderInfoForStartWorkerPtr info,
scoped_refptr<ThreadSafeSender> sender);
// Valid only for WebServiceWorkerNetworkProvider created by // Valid only for WebServiceWorkerNetworkProvider created by
// CreateForNavigation. // CreateForNavigation.
...@@ -122,7 +124,8 @@ class CONTENT_EXPORT ServiceWorkerNetworkProvider { ...@@ -122,7 +124,8 @@ class CONTENT_EXPORT ServiceWorkerNetworkProvider {
// This is for controllers, used in CreateForController. // This is for controllers, used in CreateForController.
explicit ServiceWorkerNetworkProvider( explicit ServiceWorkerNetworkProvider(
mojom::ServiceWorkerProviderInfoForStartWorkerPtr info); mojom::ServiceWorkerProviderInfoForStartWorkerPtr info,
scoped_refptr<ThreadSafeSender> sender);
scoped_refptr<ServiceWorkerProviderContext> context_; scoped_refptr<ServiceWorkerProviderContext> context_;
mojom::ServiceWorkerDispatcherHostAssociatedPtr dispatcher_host_; mojom::ServiceWorkerDispatcherHostAssociatedPtr dispatcher_host_;
......
...@@ -1573,6 +1573,7 @@ test("content_unittests") { ...@@ -1573,6 +1573,7 @@ test("content_unittests") {
"../renderer/render_thread_impl_unittest.cc", "../renderer/render_thread_impl_unittest.cc",
"../renderer/render_widget_unittest.cc", "../renderer/render_widget_unittest.cc",
"../renderer/scheduler/resource_dispatch_throttler_unittest.cc", "../renderer/scheduler/resource_dispatch_throttler_unittest.cc",
"../renderer/service_worker/service_worker_context_client_unittest.cc",
"../renderer/service_worker/service_worker_dispatcher_unittest.cc", "../renderer/service_worker/service_worker_dispatcher_unittest.cc",
"../renderer/service_worker/service_worker_provider_context_unittest.cc", "../renderer/service_worker/service_worker_provider_context_unittest.cc",
"../renderer/service_worker/service_worker_subresource_loader_unittest.cc", "../renderer/service_worker/service_worker_subresource_loader_unittest.cc",
......
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