S13nSW: GetControllerServiceWorkerPtr() doesn't ensure the controller is running

The method is similar to EnsureControllerServiceWorker but in some cases we
can't make sure that the controller service worker is running (as existing
comment described). Update the description of the method to state that this
method doesn't guarantee that the controller is running.

The method will be called during navigation when S13nSW is enabled. During
navigation, we need to make sure that the controller is running before
calling this method. Currently we make sure this by calling
ServiceWorkerFetchDispatcher::StartWorker() in the following sequence:

ServiceWorkerControlleeRequestHandler::MaybeCreateLoader()
-> ServiceWorkerURLJobWrapper::ForwardToServiceWorker()
-> ServiceWorkerNavigationLoader::ForwardToServiceWorker()
-> ServiceWorkerFetchDispatcher::Run()
-> ServiceWorkerFetchDispatcher::StartWorker()

After creating a loader, we call GetControllerServiceWorkerPtr() in
ServiceWorkerControlleeReuqestHandler::MaybeCreateSubresourceLoaderParams().

Bug: 797222
Change-Id: If7ef6dbe2e59c15e2f737f16d68f0c851d889556
Reviewed-on: https://chromium-review.googlesource.com/987832
Reviewed-by: Kinuko Yasuda <[email protected]>
Reviewed-by: Matt Falkenhagen <[email protected]>
Commit-Queue: Kenichi Ishibashi <[email protected]>
Cr-Commit-Position: refs/heads/master@{#547634}
diff --git a/content/browser/service_worker/service_worker_provider_host.cc b/content/browser/service_worker/service_worker_provider_host.cc
index 9cfd493..79c58d9 100644
--- a/content/browser/service_worker/service_worker_provider_host.cc
+++ b/content/browser/service_worker/service_worker_provider_host.cc
@@ -319,8 +319,6 @@
       ServiceWorkerVersion::FetchHandlerExistence::DOES_NOT_EXIST) {
     return nullptr;
   }
-  // TODO(bashi): Make sure the worker is running by calling
-  // controller_->RunAfterStartWorker().
   mojom::ControllerServiceWorkerPtr controller_ptr;
   controller_->controller()->Clone(mojo::MakeRequest(&controller_ptr));
   return controller_ptr;
diff --git a/content/browser/service_worker/service_worker_provider_host.h b/content/browser/service_worker/service_worker_provider_host.h
index 0bb4b7dd..846bd70e 100644
--- a/content/browser/service_worker/service_worker_provider_host.h
+++ b/content/browser/service_worker/service_worker_provider_host.h
@@ -212,8 +212,8 @@
   }
 
   // S13nServiceWorker:
-  // For service worker clients. Similar to GetControllerServiceWorker, but this
-  // returns a bound Mojo ptr which is supposed to be sent to clients. The
+  // For service worker clients. Similar to EnsureControllerServiceWorker, but
+  // this returns a bound Mojo ptr which is supposed to be sent to clients. The
   // controller ptr passed to the clients will be used to intercept requests
   // from them.
   // It is invalid to call this when controller_ is null.
@@ -229,6 +229,21 @@
   //
   // This may return nullptr if the controller service worker does not have a
   // fetch handler, i.e. when the renderer does not need the controller ptr.
+  //
+  // WARNING:
+  // Unlike EnsureControllerServiceWorker, this method doesn't guarantee that
+  // the controller worker is running because this method can be called in some
+  // situations where the worker isn't running yet. When the returned ptr is
+  // stored somewhere and intended to use later, clients need to make sure
+  // that the worker is eventually started to use the ptr.
+  // Currently all the callsites do this, i.e. they start the worker before
+  // or after calling this, but there's no mechanism to prevent future breakage.
+  // TODO(crbug.com/827935): Figure out a way to prevent misuse of this method.
+  // TODO(crbug.com/827935): Make sure the connection error handler fires in
+  // ControllerServiceWorkerConnector (so that it can correctly call
+  // EnsureControllerServiceWorker later) if the worker gets killed before
+  // events are dispatched.
+  //
   // TODO(kinuko): revisit this if we start to use the ControllerServiceWorker
   // for posting messages.
   mojom::ControllerServiceWorkerPtr GetControllerServiceWorkerPtr();