M65: Fix child process leaks in the Cloud Print Connector.

Before r526819, the utility process performing the PDF to EMF conversion
disconnects itself. Its ServiceUtilityProcessHost notices the
disconnection and deletes itself. With r526819, the utility process no
longer disconnects itself, and waits forever for its host to tear down
the connection.

Meet this new expectation and tear down the connection from the host
side.

BUG=814984
TBR=rockot@chromium.org

(cherry picked from commit 6064af4e953d2f127e87101878fe464d63082aa7)

Change-Id: I505673a82eb4748898819c6664802238738443bb
Reviewed-on: https://chromium-review.googlesource.com/935631
Reviewed-by: Ken Rockot <rockot@chromium.org>
Commit-Queue: Lei Zhang <thestig@chromium.org>
Cr-Original-Commit-Position: refs/heads/master@{#538959}
Reviewed-on: https://chromium-review.googlesource.com/938670
Reviewed-by: Lei Zhang <thestig@chromium.org>
Cr-Commit-Position: refs/branch-heads/3325@{#599}
Cr-Branched-From: bc084a8b5afa3744a74927344e304c02ae54189f-refs/heads/master@{#530369}
diff --git a/chrome/service/cloud_print/print_system_win.cc b/chrome/service/cloud_print/print_system_win.cc
index b89f96b..0be529a 100644
--- a/chrome/service/cloud_print/print_system_win.cc
+++ b/chrome/service/cloud_print/print_system_win.cc
@@ -435,8 +435,8 @@
         int render_dpi,
         const scoped_refptr<base::SingleThreadTaskRunner>& client_task_runner) {
       DCHECK(CurrentlyOnServiceIOThread());
-      std::unique_ptr<ServiceUtilityProcessHost> utility_host(
-          new ServiceUtilityProcessHost(this, client_task_runner.get()));
+      auto utility_host = std::make_unique<ServiceUtilityProcessHost>(
+          this, client_task_runner.get());
       // TODO(gene): For now we disabling autorotation for CloudPrinting.
       // Landscape/Portrait setting is passed in the print ticket and
       // server is generating portrait PDF always.
@@ -592,8 +592,8 @@
   void GetPrinterCapsAndDefaultsImpl(
       const scoped_refptr<base::SingleThreadTaskRunner>& client_task_runner) {
     DCHECK(CurrentlyOnServiceIOThread());
-    std::unique_ptr<ServiceUtilityProcessHost> utility_host(
-        new ServiceUtilityProcessHost(this, client_task_runner.get()));
+    auto utility_host = std::make_unique<ServiceUtilityProcessHost>(
+        this, client_task_runner.get());
     if (utility_host->StartGetPrinterCapsAndDefaults(printer_name_)) {
       // The object will self-destruct when the child process dies.
       ignore_result(utility_host.release());
@@ -606,8 +606,8 @@
   void GetPrinterSemanticCapsAndDefaultsImpl(
       const scoped_refptr<base::SingleThreadTaskRunner>& client_task_runner) {
     DCHECK(CurrentlyOnServiceIOThread());
-    std::unique_ptr<ServiceUtilityProcessHost> utility_host(
-        new ServiceUtilityProcessHost(this, client_task_runner.get()));
+    auto utility_host = std::make_unique<ServiceUtilityProcessHost>(
+        this, client_task_runner.get());
     if (utility_host->StartGetPrinterSemanticCapsAndDefaults(printer_name_)) {
       // The object will self-destruct when the child process dies.
       ignore_result(utility_host.release());
diff --git a/chrome/service/service_utility_process_host.cc b/chrome/service/service_utility_process_host.cc
index d9c29e3..5f884030 100644
--- a/chrome/service/service_utility_process_host.cc
+++ b/chrome/service/service_utility_process_host.cc
@@ -483,6 +483,8 @@
         FROM_HERE, base::Bind(&Client::OnChildDied, client_.get()));
     ReportUmaEvent(SERVICE_UTILITY_DISCONNECTED);
   }
+
+  // The child process has died for some reason. This host is no longer needed.
   delete this;
 }
 
@@ -559,6 +561,9 @@
       FROM_HERE, base::Bind(&Client::OnRenderPDFPagesToMetafileDone,
                             client_.get(), success));
   pdf_to_emf_state_.reset();
+
+  // The child process has finished at this point. This host is done as well.
+  delete this;
 }
 
 void ServiceUtilityProcessHost::OnGetPrinterCapsAndDefaultsSucceeded(
@@ -570,6 +575,8 @@
   client_task_runner_->PostTask(
       FROM_HERE, base::Bind(&Client::OnGetPrinterCapsAndDefaults, client_.get(),
                             true, printer_name, caps_and_defaults));
+  // The child process disconnects itself and this host deletes itself via
+  // OnChildDisconnected().
 }
 
 void ServiceUtilityProcessHost::OnGetPrinterSemanticCapsAndDefaultsSucceeded(
@@ -582,6 +589,8 @@
       FROM_HERE,
       base::Bind(&Client::OnGetPrinterSemanticCapsAndDefaults, client_.get(),
                  true, printer_name, caps_and_defaults));
+  // The child process disconnects itself and this host deletes itself via
+  // OnChildDisconnected().
 }
 
 void ServiceUtilityProcessHost::OnGetPrinterCapsAndDefaultsFailed(
@@ -593,6 +602,8 @@
       FROM_HERE,
       base::Bind(&Client::OnGetPrinterCapsAndDefaults, client_.get(), false,
                  printer_name, printing::PrinterCapsAndDefaults()));
+  // The child process disconnects itself and this host deletes itself via
+  // OnChildDisconnected().
 }
 
 void ServiceUtilityProcessHost::OnGetPrinterSemanticCapsAndDefaultsFailed(
@@ -604,6 +615,8 @@
       FROM_HERE, base::Bind(&Client::OnGetPrinterSemanticCapsAndDefaults,
                             client_.get(), false, printer_name,
                             printing::PrinterSemanticCapsAndDefaults()));
+  // The child process disconnects itself and this host deletes itself via
+  // OnChildDisconnected().
 }
 
 bool ServiceUtilityProcessHost::Client::MetafileAvailable(float scale_factor,
diff --git a/chrome/service/service_utility_process_host.h b/chrome/service/service_utility_process_host.h
index 07a8fa8..1b7c49d 100644
--- a/chrome/service/service_utility_process_host.h
+++ b/chrome/service/service_utility_process_host.h
@@ -42,6 +42,8 @@
 // Acts as the service-side host to a utility child process. A
 // utility process is a short-lived sandboxed process that is created to run
 // a specific task.
+// This class is expected to delete itself IFF one of its Start methods has been
+// called.
 class ServiceUtilityProcessHost : public content::ChildProcessHostDelegate {
  public:
   // Consumers of ServiceUtilityProcessHost must implement this interface to