Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(599)

Unified Diff: content/browser/service_worker/embedded_worker_instance.cc

Issue 2606593002: [ServiceWorker] Associate interfaces.
Patch Set: Fix unit tests Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: content/browser/service_worker/embedded_worker_instance.cc
diff --git a/content/browser/service_worker/embedded_worker_instance.cc b/content/browser/service_worker/embedded_worker_instance.cc
index 3fcf14181fbf474c6ee28799ae12d1d2858f2724..5649a11f370e21c5ba1702c312a4e07273fd30e4 100644
--- a/content/browser/service_worker/embedded_worker_instance.cc
+++ b/content/browser/service_worker/embedded_worker_instance.cc
@@ -26,6 +26,7 @@
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/content_browser_client.h"
#include "content/public/browser/render_process_host.h"
+#include "content/public/common/associated_interface_provider.h"
#include "content/public/common/child_process_host.h"
#include "content/public/common/content_switches.h"
#include "ipc/ipc_message.h"
@@ -78,12 +79,15 @@ void SetupOnUI(
const GURL& url,
const GURL& scope,
bool is_installed,
- mojom::EmbeddedWorkerInstanceClientRequest request,
- const base::Callback<void(int worker_devtools_agent_route_id,
- bool wait_for_debugger)>& callback) {
+ const base::Callback<
+ void(int worker_devtools_agent_route_id,
+ bool wait_for_debugger,
+ mojom::EmbeddedWorkerInstanceClientAssociatedPtrInfo ptr_info)>&
+ callback) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
int worker_devtools_agent_route_id = MSG_ROUTING_NONE;
bool wait_for_debugger = false;
+ mojom::EmbeddedWorkerInstanceClientAssociatedPtr client_ptr;
if (RenderProcessHost* rph = RenderProcessHost::FromID(process_id)) {
// |rph| may be NULL in unit tests.
worker_devtools_agent_route_id = rph->GetNextRoutingID();
@@ -94,13 +98,25 @@ void SetupOnUI(
service_worker_context, service_worker_context_weak,
service_worker_version_id, url, scope),
is_installed);
- if (request.is_pending())
- rph->GetRemoteInterfaces()->GetInterface(std::move(request));
+
+ if (rph->GetChannel()) {
+ rph->GetChannel()->GetRemoteAssociatedInterface(&client_ptr);
+ } else {
+ // In unit tests we establish associated interface connections via
+ // MockRenderProcessHost.
+ DCHECK(rph->GetAssociatedInterfaceProviderForTesting());
+ rph->GetAssociatedInterfaceProviderForTesting()->GetInterface(
+ &client_ptr);
+ }
+ } else {
+ // No render process host, just connect to a dummy implementation to ensure
+ // client_ptr to be bound.
+ mojo::GetDummyProxyForTesting(&client_ptr);
}
BrowserThread::PostTask(
- BrowserThread::IO,
- FROM_HERE,
- base::Bind(callback, worker_devtools_agent_route_id, wait_for_debugger));
+ BrowserThread::IO, FROM_HERE,
+ base::Bind(callback, worker_devtools_agent_route_id, wait_for_debugger,
+ base::Passed(client_ptr.PassInterface())));
}
void SetupEventDispatcherOnUIThread(
@@ -109,11 +125,18 @@ void SetupEventDispatcherOnUIThread(
mojom::ServiceWorkerEventDispatcherRequest request) {
DCHECK(!ServiceWorkerUtils::IsMojoForServiceWorkerEnabled());
RenderProcessHost* rph = RenderProcessHost::FromID(process_id);
- // |rph| or its InterfaceProvider may be NULL in unit tests.
- if (!rph || !rph->GetRemoteInterfaces())
+ // |rph| may be NULL in unit tests.
+ if (!rph)
return;
- mojom::EmbeddedWorkerSetupPtr setup;
- rph->GetRemoteInterfaces()->GetInterface(&setup);
+ mojom::EmbeddedWorkerSetupAssociatedPtr setup;
+ if (rph->GetChannel()) {
+ rph->GetChannel()->GetRemoteAssociatedInterface(&setup);
+ } else {
+ // In unit tests we establish associated interface connections via
+ // MockRenderProcessHost.
+ DCHECK(rph->GetAssociatedInterfaceProviderForTesting());
+ rph->GetAssociatedInterfaceProviderForTesting()->GetInterface(&setup);
+ }
setup->AttachServiceWorkerEventDispatcher(thread_id, std::move(request));
}
@@ -224,11 +247,8 @@ class EmbeddedWorkerInstance::StartTask {
public:
enum class ProcessAllocationState { NOT_ALLOCATED, ALLOCATING, ALLOCATED };
- StartTask(EmbeddedWorkerInstance* instance,
- const GURL& script_url,
- mojom::EmbeddedWorkerInstanceClientRequest request)
+ StartTask(EmbeddedWorkerInstance* instance, const GURL& script_url)
: instance_(instance),
- request_(std::move(request)),
state_(ProcessAllocationState::NOT_ALLOCATED),
is_installed_(false),
started_during_browser_startup_(false),
@@ -359,16 +379,18 @@ class EmbeddedWorkerInstance::StartTask {
BrowserThread::UI, FROM_HERE,
base::Bind(&SetupOnUI, process_id, instance_->context_.get(),
instance_->context_, service_worker_version_id, script_url,
- scope, is_installed_, base::Passed(&request_),
+ scope, is_installed_,
base::Bind(&StartTask::OnSetupOnUICompleted,
weak_factory_.GetWeakPtr(), base::Passed(&params),
is_new_process)));
}
- void OnSetupOnUICompleted(std::unique_ptr<EmbeddedWorkerStartParams> params,
- bool is_new_process,
- int worker_devtools_agent_route_id,
- bool wait_for_debugger) {
+ void OnSetupOnUICompleted(
+ std::unique_ptr<EmbeddedWorkerStartParams> params,
+ bool is_new_process,
+ int worker_devtools_agent_route_id,
+ bool wait_for_debugger,
+ mojom::EmbeddedWorkerInstanceClientAssociatedPtrInfo ptr_info) {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
TRACE_EVENT_ASYNC_STEP_PAST0("ServiceWorker",
"EmbeddedWorkerInstance::Start", this,
@@ -382,7 +404,7 @@ class EmbeddedWorkerInstance::StartTask {
params->wait_for_debugger = wait_for_debugger;
if (ServiceWorkerUtils::IsMojoForServiceWorkerEnabled())
- SendMojoStartWorker(std::move(params));
+ SendMojoStartWorker(std::move(params), std::move(ptr_info));
else
SendStartWorker(std::move(params));
}
@@ -407,9 +429,11 @@ class EmbeddedWorkerInstance::StartTask {
// is evaluated.
}
- void SendMojoStartWorker(std::unique_ptr<EmbeddedWorkerStartParams> params) {
+ void SendMojoStartWorker(
+ std::unique_ptr<EmbeddedWorkerStartParams> params,
+ mojom::EmbeddedWorkerInstanceClientAssociatedPtrInfo ptr_info) {
ServiceWorkerStatusCode status =
- instance_->SendMojoStartWorker(std::move(params));
+ instance_->SendMojoStartWorker(std::move(params), std::move(ptr_info));
if (status != SERVICE_WORKER_OK) {
StatusCallback callback = start_callback_;
start_callback_.Reset();
@@ -422,10 +446,6 @@ class EmbeddedWorkerInstance::StartTask {
// |instance_| must outlive |this|.
EmbeddedWorkerInstance* instance_;
- // Ownership is transferred by base::Passed() to a task after process
- // allocation.
- mojom::EmbeddedWorkerInstanceClientRequest request_;
-
StatusCallback start_callback_;
ProcessAllocationState state_;
@@ -478,17 +498,9 @@ void EmbeddedWorkerInstance::Start(
params->wait_for_debugger = false;
params->settings.v8_cache_options = GetV8CacheOptions();
- mojom::EmbeddedWorkerInstanceClientRequest request;
- if (ServiceWorkerUtils::IsMojoForServiceWorkerEnabled()) {
- request = mojo::MakeRequest(&client_);
- client_.set_connection_error_handler(
- base::Bind(&CallDetach, base::Unretained(this)));
- }
-
pending_dispatcher_request_ = std::move(dispatcher_request);
- inflight_start_task_.reset(
- new StartTask(this, params->script_url, std::move(request)));
+ inflight_start_task_.reset(new StartTask(this, params->script_url));
inflight_start_task_->Start(std::move(params), callback);
}
@@ -502,10 +514,13 @@ ServiceWorkerStatusCode EmbeddedWorkerInstance::Stop() {
ServiceWorkerStatusCode status = SERVICE_WORKER_ERROR_IPC_FAILED;
if (ServiceWorkerUtils::IsMojoForServiceWorkerEnabled()) {
- status = SERVICE_WORKER_OK;
- client_->StopWorker(base::Bind(&EmbeddedWorkerRegistry::OnWorkerStopped,
- base::Unretained(registry_.get()),
- process_id(), embedded_worker_id()));
+ // |client_| may be not bound yet, or has become broken.
+ if (client_ && !client_.encountered_error()) {
+ status = SERVICE_WORKER_OK;
+ client_->StopWorker(base::Bind(&EmbeddedWorkerRegistry::OnWorkerStopped,
+ base::Unretained(registry_.get()),
+ process_id(), embedded_worker_id()));
+ }
} else {
status = registry_->StopWorker(process_id(), embedded_worker_id_);
}
@@ -606,10 +621,16 @@ void EmbeddedWorkerInstance::OnRegisteredToDevToolsManager(
}
ServiceWorkerStatusCode EmbeddedWorkerInstance::SendMojoStartWorker(
- std::unique_ptr<EmbeddedWorkerStartParams> params) {
+ std::unique_ptr<EmbeddedWorkerStartParams> params,
+ mojom::EmbeddedWorkerInstanceClientAssociatedPtrInfo ptr_info) {
if (!context_)
return SERVICE_WORKER_ERROR_ABORT;
+ DCHECK(ServiceWorkerUtils::IsMojoForServiceWorkerEnabled());
DCHECK(pending_dispatcher_request_.is_pending());
+ DCHECK(ptr_info.is_valid());
+ client_.Bind(std::move(ptr_info));
+ client_.set_connection_error_handler(
+ base::Bind(&CallDetach, base::Unretained(this)));
client_->StartWorker(*params, std::move(pending_dispatcher_request_));
registry_->BindWorkerToProcess(process_id(), embedded_worker_id());
TRACE_EVENT_ASYNC_STEP_PAST1("ServiceWorker", "EmbeddedWorkerInstance::Start",

Powered by Google App Engine
This is Rietveld 408576698