Support [Get/Set]BrowserProfileType on iOS.

Profile type on other platforms is assigned and retrieved by
|profile_metrics::[Get/Set]BrowserProfileType| functions.
This is now supported on iOS and all use cases of |GetBrowserStateType|
are replaced with |GetBrowserProfileType|.

Bug: 1169142
Change-Id: Ic81948e6d5e33a3f5f349d7f617be583c764afce
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2840463
Reviewed-by: Sylvain Defresne <sdefresne@chromium.org>
Commit-Queue: Ramin Halavati <rhalavati@chromium.org>
Cr-Commit-Position: refs/heads/master@{#875124}
GitOrigin-RevId: f87d28cc1821e4ec54e104045243a24bbc799440
diff --git a/chrome/browser/browser_state/BUILD.gn b/chrome/browser/browser_state/BUILD.gn
index ba35b9b..18420c1 100644
--- a/chrome/browser/browser_state/BUILD.gn
+++ b/chrome/browser/browser_state/BUILD.gn
@@ -82,6 +82,7 @@
     "//components/policy/core/common",
     "//components/pref_registry",
     "//components/prefs",
+    "//components/profile_metrics",
     "//components/proxy_config",
     "//components/proxy_config/ios",
     "//components/signin/ios/browser:active_state_manager",
@@ -203,6 +204,7 @@
     "//components/history/ios/browser",
     "//components/keyed_service/core",
     "//components/keyed_service/ios",
+    "//components/profile_metrics",
     "//components/sync_preferences",
     "//components/sync_preferences:test_support",
     "//components/undo",
diff --git a/chrome/browser/browser_state/chrome_browser_state_impl.cc b/chrome/browser/browser_state/chrome_browser_state_impl.cc
index dddb369..6dc3a5f 100644
--- a/chrome/browser/browser_state/chrome_browser_state_impl.cc
+++ b/chrome/browser/browser_state/chrome_browser_state_impl.cc
@@ -17,6 +17,7 @@
 #include "components/pref_registry/pref_registry_syncable.h"
 #include "components/prefs/json_pref_store.h"
 #include "components/prefs/pref_service.h"
+#include "components/profile_metrics/browser_profile_type.h"
 #include "components/proxy_config/ios/proxy_service_factory.h"
 #include "components/proxy_config/pref_proxy_config_tracker.h"
 #include "components/sync_preferences/pref_service_syncable.h"
@@ -83,6 +84,9 @@
       io_data_(new ChromeBrowserStateImplIOData::Handle(this)) {
   otr_state_path_ = state_path_.Append(FILE_PATH_LITERAL("OTR"));
 
+  profile_metrics::SetBrowserProfileType(
+      this, profile_metrics::BrowserProfileType::kRegular);
+
   // It would be nice to use PathService for fetching this directory, but
   // the cache directory depends on the browser state stash directory, which
   // isn't available to PathService.
diff --git a/chrome/browser/browser_state/off_the_record_chrome_browser_state_impl.cc b/chrome/browser/browser_state/off_the_record_chrome_browser_state_impl.cc
index 7628bf0..f853247 100644
--- a/chrome/browser/browser_state/off_the_record_chrome_browser_state_impl.cc
+++ b/chrome/browser/browser_state/off_the_record_chrome_browser_state_impl.cc
@@ -9,6 +9,7 @@
 #include "base/sequenced_task_runner.h"
 #include "base/task/post_task.h"
 #include "components/keyed_service/ios/browser_state_dependency_manager.h"
+#include "components/profile_metrics/browser_profile_type.h"
 #include "components/proxy_config/ios/proxy_service_factory.h"
 #include "components/proxy_config/pref_proxy_config_tracker.h"
 #include "components/sync_preferences/pref_service_syncable.h"
@@ -34,6 +35,8 @@
   io_data_.reset(new OffTheRecordChromeBrowserStateIOData::Handle(this));
   BrowserStateDependencyManager::GetInstance()->CreateBrowserStateServices(
       this);
+  profile_metrics::SetBrowserProfileType(
+      this, profile_metrics::BrowserProfileType::kIncognito);
 }
 
 OffTheRecordChromeBrowserStateImpl::~OffTheRecordChromeBrowserStateImpl() {
diff --git a/chrome/browser/browser_state/test_chrome_browser_state.mm b/chrome/browser/browser_state/test_chrome_browser_state.mm
index a4bfae5..dfad28c 100644
--- a/chrome/browser/browser_state/test_chrome_browser_state.mm
+++ b/chrome/browser/browser_state/test_chrome_browser_state.mm
@@ -27,6 +27,7 @@
 #include "components/history/ios/browser/history_database_helper.h"
 #include "components/keyed_service/core/service_access_type.h"
 #include "components/keyed_service/ios/browser_state_dependency_manager.h"
+#include "components/profile_metrics/browser_profile_type.h"
 #include "components/sync_preferences/pref_service_syncable.h"
 #include "components/sync_preferences/testing_pref_service_syncable.h"
 #include "components/undo/bookmark_undo_service.h"
@@ -99,6 +100,9 @@
   // off-the-record TestChromeBrowserState must be established before this
   // method can be called.
   DCHECK(original_browser_state_);
+
+  profile_metrics::SetBrowserProfileType(
+      this, profile_metrics::BrowserProfileType::kIncognito);
 }
 
 TestChromeBrowserState::TestChromeBrowserState(
@@ -123,6 +127,9 @@
     pair.first->SetTestingFactory(this, std::move(pair.second));
   }
 
+  profile_metrics::SetBrowserProfileType(
+      this, profile_metrics::BrowserProfileType::kRegular);
+
   Init();
 }
 
diff --git a/chrome/browser/browser_state_metrics/browser_state_metrics.cc b/chrome/browser/browser_state_metrics/browser_state_metrics.cc
index 9f31f2a..233346e 100644
--- a/chrome/browser/browser_state_metrics/browser_state_metrics.cc
+++ b/chrome/browser/browser_state_metrics/browser_state_metrics.cc
@@ -33,11 +33,4 @@
   profile_metrics::Counts counts;
   CountBrowserStateInformation(manager, &counts);
   profile_metrics::LogProfileMetricsCounts(counts);
-}
-
-profile_metrics::BrowserProfileType GetBrowserStateType(
-    web::BrowserState* browser_state) {
-  return browser_state->IsOffTheRecord()
-             ? profile_metrics::BrowserProfileType::kIncognito
-             : profile_metrics::BrowserProfileType::kRegular;
-}
+}
\ No newline at end of file
diff --git a/chrome/browser/browser_state_metrics/browser_state_metrics.h b/chrome/browser/browser_state_metrics/browser_state_metrics.h
index 95d36b5..d54fb22 100644
--- a/chrome/browser/browser_state_metrics/browser_state_metrics.h
+++ b/chrome/browser/browser_state_metrics/browser_state_metrics.h
@@ -11,13 +11,8 @@
 
 namespace profile_metrics {
 struct Counts;
-enum class BrowserProfileType;
 }  // namespace profile_metrics
 
-namespace web {
-class BrowserState;
-}
-
 // Counts and returns summary information about the browser states currently in
 // the |manager|. This information is returned in the output variable
 // |counts|. Assumes that all field of |counts| are set to zero before the call.
@@ -26,7 +21,4 @@
 
 void LogNumberOfBrowserStates(ios::ChromeBrowserStateManager* manager);
 
-profile_metrics::BrowserProfileType GetBrowserStateType(
-    web::BrowserState* browser_state);
-
 #endif  // IOS_CHROME_BROWSER_BROWSER_STATE_METRICS_BROWSER_STATE_METRICS_H_
diff --git a/chrome/browser/ui/browser_view/browser_coordinator.mm b/chrome/browser/ui/browser_view/browser_coordinator.mm
index dc42990..0a7503c 100644
--- a/chrome/browser/ui/browser_view/browser_coordinator.mm
+++ b/chrome/browser/ui/browser_view/browser_coordinator.mm
@@ -13,7 +13,6 @@
 #import "ios/chrome/browser/app_launcher/app_launcher_tab_helper.h"
 #import "ios/chrome/browser/autofill/autofill_tab_helper.h"
 #include "ios/chrome/browser/browser_state/chrome_browser_state.h"
-#include "ios/chrome/browser/browser_state_metrics/browser_state_metrics.h"
 #include "ios/chrome/browser/chrome_url_constants.h"
 #include "ios/chrome/browser/download/download_directory_util.h"
 #import "ios/chrome/browser/download/external_app_util.h"
@@ -613,7 +612,7 @@
 
   base::UmaHistogramEnumeration(
       "Download.OpenDownloads.PerProfileType",
-      GetBrowserStateType(self.browser->GetBrowserState()));
+      profile_metrics::GetBrowserProfileType(self.browser->GetBrowserState()));
 }
 
 - (void)showRecentTabs {
diff --git a/chrome/browser/ui/location_bar/location_bar_coordinator.mm b/chrome/browser/ui/location_bar/location_bar_coordinator.mm
index 86cebe5..36c3ad7 100644
--- a/chrome/browser/ui/location_bar/location_bar_coordinator.mm
+++ b/chrome/browser/ui/location_bar/location_bar_coordinator.mm
@@ -398,8 +398,9 @@
     return;
   }
 
-  base::UmaHistogramEnumeration("iOS.LocationBar.ShareButton.PerProfileType",
-                                GetBrowserStateType(self.browserState));
+  base::UmaHistogramEnumeration(
+      "iOS.LocationBar.ShareButton.PerProfileType",
+      profile_metrics::GetBrowserProfileType(self.browserState));
 }
 
 - (void)locationBarVisitCopyLinkTapped {
diff --git a/chrome/browser/web_state_list/web_state_list_metrics_browser_agent.mm b/chrome/browser/web_state_list/web_state_list_metrics_browser_agent.mm
index bc61828..906a8b2 100644
--- a/chrome/browser/web_state_list/web_state_list_metrics_browser_agent.mm
+++ b/chrome/browser/web_state_list/web_state_list_metrics_browser_agent.mm
@@ -10,7 +10,6 @@
 #include "components/navigation_metrics/navigation_metrics.h"
 #include "components/profile_metrics/browser_profile_type.h"
 #include "ios/chrome/browser/browser_state/chrome_browser_state.h"
-#include "ios/chrome/browser/browser_state_metrics/browser_state_metrics.h"
 #include "ios/chrome/browser/chrome_url_constants.h"
 #include "ios/chrome/browser/crash_report/crash_loop_detection_util.h"
 #import "ios/chrome/browser/sessions/session_restoration_browser_agent.h"
@@ -152,7 +151,7 @@
       item ? item->GetVirtualURL() : GURL::EmptyGURL(),
       navigation_context->IsSameDocument(),
       web_state->GetBrowserState()->IsOffTheRecord(),
-      GetBrowserStateType(web_state->GetBrowserState()));
+      profile_metrics::GetBrowserProfileType(web_state->GetBrowserState()));
 }
 
 void WebStateListMetricsBrowserAgent::PageLoaded(
diff --git a/web_view/BUILD.gn b/web_view/BUILD.gn
index 85c8c30..e0725b3 100644
--- a/web_view/BUILD.gn
+++ b/web_view/BUILD.gn
@@ -279,6 +279,7 @@
     "//components/password_manager/ios:password_manager_feature",
     "//components/pref_registry",
     "//components/prefs",
+    "//components/profile_metrics",
     "//components/proxy_config",
     "//components/safe_browsing/core/common:safe_browsing_prefs",
     "//components/security_state/ios",
diff --git a/web_view/internal/DEPS b/web_view/internal/DEPS
index f5e40da..fe5f733 100644
--- a/web_view/internal/DEPS
+++ b/web_view/internal/DEPS
@@ -20,6 +20,7 @@
   "+components/password_manager/core",
   "+components/password_manager/ios",
   "+components/pref_registry",
+  "+components/profile_metrics",
   "+components/prefs",
   "+components/proxy_config",
   "+components/safe_browsing/core/common",
diff --git a/web_view/internal/web_view_browser_state.mm b/web_view/internal/web_view_browser_state.mm
index 95dbe31..76d6e1d 100644
--- a/web_view/internal/web_view_browser_state.mm
+++ b/web_view/internal/web_view_browser_state.mm
@@ -23,6 +23,7 @@
 #include "components/prefs/json_pref_store.h"
 #include "components/prefs/pref_filter.h"
 #include "components/prefs/pref_service_factory.h"
+#include "components/profile_metrics/browser_profile_type.h"
 #include "components/safe_browsing/core/common/safe_browsing_prefs.h"
 #include "components/sync/base/pref_names.h"
 #include "components/sync/base/sync_prefs.h"
@@ -82,6 +83,10 @@
           !recording_browser_state->IsOffTheRecord()));
   recording_browser_state_ = recording_browser_state;
 
+  profile_metrics::SetBrowserProfileType(
+      this, off_the_record ? profile_metrics::BrowserProfileType::kIncognito
+                           : profile_metrics::BrowserProfileType::kRegular);
+
   // IO access is required to setup the browser state. In Chrome, this is
   // already allowed during thread startup. However, startup time of
   // ChromeWebView is not predetermined, so IO access is temporarily allowed.