[HaTS] Listen to onSurveyCompleted events in HaTS webui

(cherry picked from commit 613424c2bffa9350717e5b6f3172b023ff70d928)

Bug: 335782514
Change-Id: Iad6acec294f52b5e174339f5e43d9a44585ba0eb
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/5463956
Reviewed-by: Mike West <mkwst@chromium.org>
Reviewed-by: Theodore Olsauskas-Warren <sauski@google.com>
Commit-Queue: Ravjit Uppal <ravjit@chromium.org>
Cr-Original-Commit-Position: refs/heads/main@{#1291814}
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/5487037
Bot-Commit: Rubber Stamper <rubber-stamper@appspot.gserviceaccount.com>
Cr-Commit-Position: refs/branch-heads/6422@{#512}
Cr-Branched-From: 9012208d0ce02e0cf0adb9b62558627c356f3278-refs/heads/main@{#1287751}
diff --git a/chrome/browser/resources/hats/hats.ts b/chrome/browser/resources/hats/hats.ts
index ff9c0f7..9631ef4 100644
--- a/chrome/browser/resources/hats/hats.ts
+++ b/chrome/browser/resources/hats/hats.ts
@@ -47,11 +47,14 @@
   let loadedSent = false;
 
   const surveyListener = {
-    // Provide survey state to the WebContentsObserver via URL fragments.
     surveyClosed: function() {
       browserProxy!.handler.onSurveyClosed();
     },
 
+    surveyCompleted: function() {
+      browserProxy!.handler.onSurveyCompleted();
+    },
+
     surveyPositioning: function(_: any, size: any, animationSpec: any) {
       // Delay sending a loaded signal to the browser until the initial
       // animation has completed.
diff --git a/chrome/browser/ui/views/hats/hats_next_web_dialog.cc b/chrome/browser/ui/views/hats/hats_next_web_dialog.cc
index cc3c689..c8048b9fb5 100644
--- a/chrome/browser/ui/views/hats/hats_next_web_dialog.cc
+++ b/chrome/browser/ui/views/hats/hats_next_web_dialog.cc
@@ -210,6 +210,10 @@
   std::move(success_callback_).Run();
 }
 
+void HatsNextWebDialog::OnSurveyCompleted() {
+  base::UmaHistogramBoolean(kHatsSurveyCompletedHistogram, true);
+}
+
 void HatsNextWebDialog::OnSurveyClosed() {
   loading_timer_.Stop();
   if (!received_survey_loaded_) {
@@ -365,7 +369,7 @@
   } else if (state == "close") {
     OnSurveyClosed();
   } else if (state == "completed") {
-    base::UmaHistogramBoolean(kHatsSurveyCompletedHistogram, true);
+    OnSurveyCompleted();
   } else {
     LOG(ERROR) << "Unknown state provided in URL fragment by HaTS survey:"
                << state;
diff --git a/chrome/browser/ui/views/hats/hats_next_web_dialog.h b/chrome/browser/ui/views/hats/hats_next_web_dialog.h
index 87b7c70..2de92ff 100644
--- a/chrome/browser/ui/views/hats/hats_next_web_dialog.h
+++ b/chrome/browser/ui/views/hats/hats_next_web_dialog.h
@@ -61,6 +61,7 @@
   std::vector<std::string> GetLanguageList() override;
   base::Value::Dict GetProductSpecificDataJson() override;
   void OnSurveyLoaded() override;
+  void OnSurveyCompleted() override;
   void OnSurveyClosed() override;
 
  protected:
diff --git a/chrome/browser/ui/webui/hats/hats.mojom b/chrome/browser/ui/webui/hats/hats.mojom
index a662d1a..752f63c 100644
--- a/chrome/browser/ui/webui/hats/hats.mojom
+++ b/chrome/browser/ui/webui/hats/hats.mojom
@@ -17,6 +17,9 @@
   // Notifies the browser that a survey has been successfully loaded and is
   // being displayed within the WebContents.
   OnSurveyLoaded();
+  // This event is triggered when the user finishes the entire survey. This will
+  // be triggered before the 'thank you' card is displayed.
+  OnSurveyCompleted();
   // Notifies the browser that the survey has been closed and is not being
   // displayed.
   OnSurveyClosed();
diff --git a/chrome/browser/ui/webui/hats/hats_page_handler.cc b/chrome/browser/ui/webui/hats/hats_page_handler.cc
index e4717b1..aaaf5f4 100644
--- a/chrome/browser/ui/webui/hats/hats_page_handler.cc
+++ b/chrome/browser/ui/webui/hats/hats_page_handler.cc
@@ -30,6 +30,10 @@
 void HatsPageHandler::OnSurveyLoaded() {
   delegate_->OnSurveyLoaded();
 }
+// Triggered by onSurveyCompleted() call in TS.
+void HatsPageHandler::OnSurveyCompleted() {
+  delegate_->OnSurveyCompleted();
+}
 
 // Triggered by onSurveyClosed() call in TS.
 void HatsPageHandler::OnSurveyClosed() {
diff --git a/chrome/browser/ui/webui/hats/hats_page_handler.h b/chrome/browser/ui/webui/hats/hats_page_handler.h
index c58d249..cfb2fc19 100644
--- a/chrome/browser/ui/webui/hats/hats_page_handler.h
+++ b/chrome/browser/ui/webui/hats/hats_page_handler.h
@@ -22,6 +22,7 @@
   // Returns the product specific data associated with this survey.
   virtual base::Value::Dict GetProductSpecificDataJson() = 0;
   virtual void OnSurveyLoaded() = 0;
+  virtual void OnSurveyCompleted() = 0;
   virtual void OnSurveyClosed() = 0;
 };
 
@@ -38,6 +39,7 @@
 
   // hats::mojom::PageHandler:
   void OnSurveyLoaded() override;
+  void OnSurveyCompleted() override;
   void OnSurveyClosed() override;
 
  private: