[Fuchsia] Log component start & stop in runners

Now runners will log when a new component is created or destroyed.

Bug: 1108487
Change-Id: I0327b43064f632b91129c5c16c94ca3a00fbcee5
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2872532
Auto-Submit: Sergey Ulanov <[email protected]>
Reviewed-by: Jochen Eisinger <[email protected]>
Reviewed-by: David Dorwin <[email protected]>
Commit-Queue: Jochen Eisinger <[email protected]>
Cr-Commit-Position: refs/heads/master@{#880871}
diff --git a/PRESUBMIT.py b/PRESUBMIT.py
index 5eafdc6..9857cd8 100644
--- a/PRESUBMIT.py
+++ b/PRESUBMIT.py
@@ -2336,6 +2336,7 @@
                     r"^extensions[\\/]renderer[\\/]logging_native_handler\.cc$",
                     r"^fuchsia[\\/]engine[\\/]browser[\\/]frame_impl.cc$",
                     r"^fuchsia[\\/]engine[\\/]context_provider_main.cc$",
+                    r"^fuchsia[\\/]runners[\\/]common[\\/]web_component.cc$",
                     r"^headless[\\/]app[\\/]headless_shell\.cc$",
                     r"^ipc[\\/]ipc_logging\.cc$",
                     r"^native_client_sdk[\\/]",
diff --git a/fuchsia/runners/common/web_component.cc b/fuchsia/runners/common/web_component.cc
index aca830e..fe1ffad 100644
--- a/fuchsia/runners/common/web_component.cc
+++ b/fuchsia/runners/common/web_component.cc
@@ -32,15 +32,18 @@
       module_context_(
           startup_context()->svc()->Connect<fuchsia::modular::ModuleContext>()),
       navigation_listener_binding_(this) {
+  DCHECK(!debug_name_.empty());
   DCHECK(runner);
 
+  LOG(INFO) << "Creating component " << debug_name_;
+
   // If the ComponentController request is valid then bind it, and configure it
   // to destroy this component on error.
   if (controller_request.is_valid()) {
     controller_binding_.Bind(std::move(controller_request));
     controller_binding_.set_error_handler([this](zx_status_t status) {
       ZX_LOG_IF(ERROR, status != ZX_ERR_PEER_CLOSED, status)
-          << " ComponentController disconnected";
+          << " ComponentController disconnected for component " << debug_name_;
       // Teardown the component with dummy values, since ComponentController
       // channel isn't there to receive them.
       DestroyComponent(0, fuchsia::sys::TerminationReason::UNKNOWN);
@@ -82,8 +85,10 @@
   // ZX_ERR_PEER_CLOSED will usually indicate a crash, reported elsewhere.
   // Therefore only log other, more unusual, |status| codes.
   frame_.set_error_handler([this](zx_status_t status) {
-    if (status != ZX_OK && status != ZX_ERR_PEER_CLOSED)
-      ZX_LOG(ERROR, status) << " Frame disconnected";
+    if (status != ZX_OK && status != ZX_ERR_PEER_CLOSED) {
+      ZX_LOG(ERROR, status)
+          << " component " << debug_name_ << ": Frame disconnected";
+    }
     DestroyComponent(status, fuchsia::sys::TerminationReason::EXITED);
   });
 
@@ -109,6 +114,7 @@
     const GURL& url,
     std::vector<fuchsia::net::http::Header> extra_headers) {
   DCHECK(url.is_valid());
+
   fuchsia::web::NavigationControllerPtr navigation_controller;
   frame()->GetNavigationController(navigation_controller.NewRequest());
 
@@ -184,6 +190,10 @@
 
 void WebComponent::DestroyComponent(int64_t exit_code,
                                     fuchsia::sys::TerminationReason reason) {
+  LOG(INFO) << "Component " << debug_name_
+            << " is shutting down. reason=" << static_cast<int>(reason)
+            << " exit_code=" << exit_code;
+
   termination_reason_ = reason;
   termination_exit_code_ = exit_code;
   runner_->DestroyComponent(this);
diff --git a/fuchsia/runners/common/web_content_runner.cc b/fuchsia/runners/common/web_content_runner.cc
index ccf16c73..d0f80851 100644
--- a/fuchsia/runners/common/web_content_runner.cc
+++ b/fuchsia/runners/common/web_content_runner.cc
@@ -18,6 +18,7 @@
 #include "base/fuchsia/scoped_service_binding.h"
 #include "base/fuchsia/startup_context.h"
 #include "base/logging.h"
+#include "base/strings/stringprintf.h"
 #include "fuchsia/runners/buildflags.h"
 #include "fuchsia/runners/common/web_component.h"
 #include "url/gurl.h"
@@ -42,6 +43,11 @@
   return status == ZX_OK;
 }
 
+std::string CreateUniqueComponentName() {
+  static int last_component_id_ = 0;
+  return base::StringPrintf("web-component:%d", ++last_component_id_);
+}
+
 }  // namespace
 
 WebContentRunner::WebContentRunner(
@@ -90,7 +96,7 @@
   }
 
   std::unique_ptr<WebComponent> component = std::make_unique<WebComponent>(
-      std::string(), this,
+      CreateUniqueComponentName(), this,
       std::make_unique<base::StartupContext>(std::move(startup_info)),
       std::move(controller_request));
 #if BUILDFLAG(WEB_RUNNER_REMOTE_DEBUGGING_PORT) != 0