[Chromecast] Introduce CastWebService with new CastWebView lifetime logic

Updates the ownership model of CastWebView so that users "borrow" the
CastWebView, returning ownership to CastWebService when the user goes
out of scope. Web page lifetime can be extended beyond the lifetime of
the user, facilitating smooth transitions between pages (no intermediate
black screen) and simplifying logic for extended shutdown.

Merge-With: eureka-internal/313200

Bug: internal b/77879457
Bug: internal b/130320237
Test: Start multiple apps in sequence, verify successful transition with
      no crashes.

Change-Id: Ic03217ed6d3abb8a74d012c9d6b11bd1a8d92c68
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1895504
Commit-Queue: Sean Topping <[email protected]>
Reviewed-by: Simeon Anfinrud <[email protected]>
Reviewed-by: Daniel Nicoara <[email protected]>
Cr-Commit-Position: refs/heads/master@{#713636}
diff --git a/chromecast/browser/cast_web_contents.h b/chromecast/browser/cast_web_contents.h
index cb61f16..6f08898 100644
--- a/chromecast/browser/cast_web_contents.h
+++ b/chromecast/browser/cast_web_contents.h
@@ -56,7 +56,7 @@
 // We consider the CastWebContents to be in a LOADED state when the content of
 // the main frame is fully loaded and running (all resources fetched, JS is
 // running). Iframes might still be loading in this case, but in general we
-// consider the page to be in a presentable state at this stage. It is
+// consider the page to be in a presentable state at this stage, so it is
 // appropriate to display the WebContents to the user.
 //
 // During or after the page is loaded, there are multiple error conditions that
@@ -184,8 +184,10 @@
 
   // Initialization parameters for CastWebContents.
   struct InitParams {
-    // Delegate for CastWebContents. This can be null for an inner WebContents.
-    Delegate* delegate = nullptr;
+    // The delegate for the CastWebContents. Must be non-null. If the delegate
+    // is destroyed before CastWebContents, the WeakPtr will be invalidated on
+    // the main UI thread.
+    base::WeakPtr<Delegate> delegate = nullptr;
     // Enable development mode for this CastWebCastWebContents. Whitelists
     // certain functionality for the WebContents, like remote debugging and
     // debugging interfaces.
@@ -204,6 +206,10 @@
     // Background color for the WebContents view. If not provided, the color
     // will fall back to the platform default.
     BackgroundColor background_color = BackgroundColor::NONE;
+
+    InitParams();
+    InitParams(const InitParams& other);
+    ~InitParams();
   };
 
   // Page state for the main frame.
@@ -234,9 +240,6 @@
   // Initialization and Setup
   // ===========================================================================
 
-  // Set the delegate. SetDelegate(nullptr) can be used to stop notifications.
-  virtual void SetDelegate(Delegate* delegate) = 0;
-
   // Add a set of features for all renderers in the WebContents. Features are
   // configured when `CastWebContents::RenderFrameCreated` is invoked.
   virtual void AddRendererFeatures(std::vector<RendererFeature> features) = 0;