[Tab Search] Add metrics for tabs with same domains.

Logging the number of tabs in a particular domain along with a rough
estimate of the total number of tabs across all windows.

(cherry picked from commit 22ab811a74c5eb906d8b1132d8710c73008c8c58)

Bug: 1136644
Change-Id: I7c88cc6693b41d34b671342697b20772ef377f88
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2441285
Commit-Queue: Charlene Yan <[email protected]>
Reviewed-by: Alexei Svitkine <[email protected]>
Reviewed-by: Peter Boström <[email protected]>
Cr-Original-Commit-Position: refs/heads/master@{#813252}
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2463988
Reviewed-by: Charlene Yan <[email protected]>
Cr-Commit-Position: refs/branch-heads/4280@{#213}
Cr-Branched-From: ea420fb963f9658c9969b6513c56b8f47efa1a2a-refs/heads/master@{#812852}
diff --git a/chrome/browser/ui/uma_browsing_activity_observer.cc b/chrome/browser/ui/uma_browsing_activity_observer.cc
index 4e251cf..ff0b0256 100644
--- a/chrome/browser/ui/uma_browsing_activity_observer.cc
+++ b/chrome/browser/ui/uma_browsing_activity_observer.cc
@@ -4,8 +4,10 @@
 
 #include "chrome/browser/ui/uma_browsing_activity_observer.h"
 
+#include "base/metrics/histogram_functions.h"
 #include "base/metrics/histogram_macros.h"
 #include "base/metrics/user_metrics.h"
+#include "base/numerics/ranges.h"
 #include "base/time/time.h"
 #include "chrome/browser/chrome_notification_types.h"
 #include "chrome/browser/profiles/profile.h"
@@ -115,6 +117,8 @@
   int app_window_count = 0;
   int popup_window_count = 0;
   int tabbed_window_count = 0;
+  std::map<base::StringPiece, int> unique_domain;
+
   for (auto* browser : *BrowserList::GetInstance()) {
     // Record how many tabs each window has open.
     UMA_HISTOGRAM_CUSTOM_COUNTS("Tabs.TabCountPerWindow",
@@ -123,6 +127,13 @@
     TabStripModel* const tab_strip_model = browser->tab_strip_model();
     tab_count += tab_strip_model->count();
 
+    for (int i = 0; i < tab_strip_model->count(); ++i) {
+      base::StringPiece domain = tab_strip_model->GetWebContentsAt(i)
+                                     ->GetLastCommittedURL()
+                                     .host_piece();
+      unique_domain[domain] += 1;
+    }
+
     const std::vector<tab_groups::TabGroupId>& groups =
         tab_strip_model->group_model()->ListTabGroups();
     tab_group_count += groups.size();
@@ -152,6 +163,15 @@
     else if (browser->is_type_normal())
       tabbed_window_count++;
   }
+
+  // Record how many tabs share a domain based on the total number of tabs open.
+  const std::string tab_count_per_domain_histogram_name =
+      AppendTabBucketCountToHistogramName(tab_count);
+  for (auto domain : unique_domain) {
+    base::UmaHistogramSparse(tab_count_per_domain_histogram_name,
+                             base::ClampToRange(domain.second, 0, 200));
+  }
+
   // Record how many tabs total are open (across all windows).
   UMA_HISTOGRAM_CUSTOM_COUNTS("Tabs.TabCountPerLoad", tab_count, 1, 200, 50);
 
@@ -192,4 +212,42 @@
                            tabbed_window_count);
 }
 
+std::string UMABrowsingActivityObserver::AppendTabBucketCountToHistogramName(
+    int total_tab_count) const {
+  const char* bucket = nullptr;
+  if (total_tab_count < 6) {
+    bucket = "0to5";
+  } else if (total_tab_count < 11) {
+    bucket = "6to10";
+  } else if (total_tab_count < 16) {
+    bucket = "10to15";
+  } else if (total_tab_count < 21) {
+    bucket = "16to20";
+  } else if (total_tab_count < 31) {
+    bucket = "21to30";
+  } else if (total_tab_count < 41) {
+    bucket = "31to40";
+  } else if (total_tab_count < 61) {
+    bucket = "41to60";
+  } else if (total_tab_count < 81) {
+    bucket = "61to80";
+  } else if (total_tab_count < 101) {
+    bucket = "81to100";
+  } else if (total_tab_count < 151) {
+    bucket = "101to150";
+  } else if (total_tab_count < 201) {
+    bucket = "151to200";
+  } else if (total_tab_count < 301) {
+    bucket = "201to300";
+  } else if (total_tab_count < 401) {
+    bucket = "301to400";
+  } else if (total_tab_count < 501) {
+    bucket = "401to500";
+  } else {
+    bucket = "501+";
+  }
+  const char kHistogramBaseName[] = "Tabs.TabCountPerDomainPerLoad";
+  return base::StringPrintf("%s.%s", kHistogramBaseName, bucket);
+}
+
 }  // namespace chrome
diff --git a/chrome/browser/ui/uma_browsing_activity_observer.h b/chrome/browser/ui/uma_browsing_activity_observer.h
index 9dfed9e..8390eca 100644
--- a/chrome/browser/ui/uma_browsing_activity_observer.h
+++ b/chrome/browser/ui/uma_browsing_activity_observer.h
@@ -41,6 +41,10 @@
   // tabs here.
   void LogBrowserTabCount() const;
 
+  // Maps |total_tab_count| to the corresponding histogram bucket with the
+  // proper name suffix.
+  std::string AppendTabBucketCountToHistogramName(int total_tab_count) const;
+
   content::NotificationRegistrar registrar_;
   TabStripModelStatsRecorder tab_recorder_;
 
diff --git a/tools/metrics/histograms/histograms_xml/tab/histograms.xml b/tools/metrics/histograms/histograms_xml/tab/histograms.xml
index ce11bc2..90a14534 100644
--- a/tools/metrics/histograms/histograms_xml/tab/histograms.xml
+++ b/tools/metrics/histograms/histograms_xml/tab/histograms.xml
@@ -1672,6 +1672,35 @@
   </details>
 </histogram>
 
+<histogram name="Tabs.TabCountPerDomainPerLoad.{TotalTabCountBucket}"
+    units="tabs" expires_after="2021-03-07">
+  <owner>[email protected]</owner>
+  <owner>[email protected]</owner>
+  <owner>[email protected]</owner>
+  <summary>
+    The number of tabs across all browsers (counting app-mode windows) with the
+    same host piece when a load completes. {TotalTabCountBucket} is inclusive
+    and describes the total number of tabs when logging.
+  </summary>
+  <token key="TotalTabCountBucket">
+    <variant name="0to5" summary=""/>
+    <variant name="6to10" summary=""/>
+    <variant name="10to15" summary=""/>
+    <variant name="16to20" summary=""/>
+    <variant name="21to30" summary=""/>
+    <variant name="31to40" summary=""/>
+    <variant name="41to60" summary=""/>
+    <variant name="61to80" summary=""/>
+    <variant name="81to100" summary=""/>
+    <variant name="101to150" summary=""/>
+    <variant name="151to200" summary=""/>
+    <variant name="201to300" summary=""/>
+    <variant name="301to400" summary=""/>
+    <variant name="401to500" summary=""/>
+    <variant name="500+" summary=""/>
+  </token>
+</histogram>
+
 <histogram name="Tabs.TabCountPerLoad" units="tabs" expires_after="2021-07-31">
   <owner>[email protected]</owner>
   <owner>[email protected]</owner>