Cherry-pick: [NTP][Enterprise] Add histogram to record when an auth attempt is made

(cherry picked from commit af026a957b013c33c9b2d86eb3a7453662756e28)

Bug: 395182365
Fixed: 399129429
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/6244483
Reviewed-by: Paul Adedeji <pauladedeji@google.com>
Reviewed-by: Roman Arora <romanarora@chromium.org>
Commit-Queue: Riley Tatum <rtatum@google.com>
Reviewed-by: Matthew Denton <mpdenton@chromium.org>
Cr-Original-Commit-Position: refs/heads/main@{#1419503}
Change-Id: I15285a6a96ebdca8e187b5bb1520ad8d02563c95
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/6334077
Cr-Commit-Position: refs/branch-heads/6998@{#1898}
Cr-Branched-From: de9c6fafd8ae5c6ea0438764076ca7d04a0b165d-refs/heads/main@{#1415337}
diff --git a/chrome/browser/resources/new_tab_page/modules/v2/authentication/microsoft_auth_module.ts b/chrome/browser/resources/new_tab_page/modules/v2/authentication/microsoft_auth_module.ts
index 418ac17c..91c08987 100644
--- a/chrome/browser/resources/new_tab_page/modules/v2/authentication/microsoft_auth_module.ts
+++ b/chrome/browser/resources/new_tab_page/modules/v2/authentication/microsoft_auth_module.ts
@@ -8,6 +8,7 @@
 
 import {I18nMixinLit, loadTimeData} from '../../../i18n_setup.js';
 import type {MicrosoftAuthPageHandlerRemote} from '../../../microsoft_auth.mojom-webui.js';
+import {AuthType} from '../../../ntp_microsoft_auth_shared_ui.mojom-webui.js';
 import {ParentTrustedDocumentProxy} from '../../microsoft_auth_frame_connector.js';
 import {ModuleDescriptor} from '../../module_descriptor.js';
 import type {MenuItem, ModuleHeaderElement} from '../module_header.js';
@@ -106,6 +107,9 @@
     const proxyInstance = ParentTrustedDocumentProxy.getInstance();
     if (proxyInstance) {
       proxyInstance.getChildDocument().acquireTokenPopup();
+      chrome.metricsPrivate.recordEnumerationValue(
+          `NewTabPage.MicrosoftAuth.AuthStarted`, AuthType.kPopup,
+          AuthType.MAX_VALUE + 1);
     }
   }
 }
diff --git a/chrome/browser/ui/webui/ntp_microsoft_auth/ntp_microsoft_auth_page_handler.cc b/chrome/browser/ui/webui/ntp_microsoft_auth/ntp_microsoft_auth_page_handler.cc
index 7afe6ba..94f5edda 100644
--- a/chrome/browser/ui/webui/ntp_microsoft_auth/ntp_microsoft_auth_page_handler.cc
+++ b/chrome/browser/ui/webui/ntp_microsoft_auth/ntp_microsoft_auth_page_handler.cc
@@ -6,6 +6,7 @@
 
 #include <utility>
 
+#include "base/metrics/histogram_functions.h"
 #include "chrome/browser/new_tab_page/microsoft_auth/microsoft_auth_service.h"
 #include "chrome/browser/new_tab_page/microsoft_auth/microsoft_auth_service_factory.h"
 #include "chrome/browser/profiles/profile.h"
@@ -32,9 +33,17 @@
   auth_service_->ClearAuthData();
 }
 
+// TODO(crbug.com/396144770): Update logic to something that simply calls
+// `OnAuthStateUpdated` after merges for M134 are complete, instead of the
+// renderer calling this and deciding for itself based on the response.
 void MicrosoftAuthUntrustedPageHandler::GetAuthState(
     GetAuthStateCallback callback) {
-  std::move(callback).Run(auth_service_->GetAuthState());
+  auto state = auth_service_->GetAuthState();
+  if (state == new_tab_page::mojom::AuthState::kNone) {
+    base::UmaHistogramEnumeration("NewTabPage.MicrosoftAuth.AuthStarted",
+                                  new_tab_page::mojom::AuthType::kSilent);
+  }
+  std::move(callback).Run(std::move(state));
 }
 
 void MicrosoftAuthUntrustedPageHandler::SetAccessToken(
@@ -50,5 +59,7 @@
   new_tab_page::mojom::AuthState auth_state = auth_service_->GetAuthState();
   if (auth_state == new_tab_page::mojom::AuthState::kNone) {
     document_->AcquireTokenSilent();
+    base::UmaHistogramEnumeration("NewTabPage.MicrosoftAuth.AuthStarted",
+                                  new_tab_page::mojom::AuthType::kSilent);
   }
 }
diff --git a/chrome/browser/ui/webui/ntp_microsoft_auth/ntp_microsoft_auth_page_handler_unittest.cc b/chrome/browser/ui/webui/ntp_microsoft_auth/ntp_microsoft_auth_page_handler_unittest.cc
index 2dec04b..14179a6 100644
--- a/chrome/browser/ui/webui/ntp_microsoft_auth/ntp_microsoft_auth_page_handler_unittest.cc
+++ b/chrome/browser/ui/webui/ntp_microsoft_auth/ntp_microsoft_auth_page_handler_unittest.cc
@@ -7,6 +7,7 @@
 #include <memory>
 
 #include "base/test/gmock_move_support.h"
+#include "base/test/metrics/histogram_tester.h"
 #include "base/test/mock_callback.h"
 #include "chrome/browser/new_tab_page/microsoft_auth/microsoft_auth_service.h"
 #include "chrome/browser/new_tab_page/microsoft_auth/microsoft_auth_service_factory.h"
@@ -90,15 +91,17 @@
         mock_document_.BindAndGetRemote(), profile_.get());
   }
 
-  MicrosoftAuthUntrustedPageHandler& handler() { return *handler_; }
-  MockMicrosoftAuthService& mock_auth_service() { return *mock_auth_service_; }
-  MockDocument& mock_document() { return mock_document_; }
   MicrosoftAuthServiceObserver& auth_service_observer() {
     return *auth_service_observer_;
   }
+  MicrosoftAuthUntrustedPageHandler& handler() { return *handler_; }
+  base::HistogramTester& histogram_tester() { return histogram_tester_; }
+  MockMicrosoftAuthService& mock_auth_service() { return *mock_auth_service_; }
+  MockDocument& mock_document() { return mock_document_; }
 
  private:
   testing::NiceMock<MockDocument> mock_document_;
+  base::HistogramTester histogram_tester_;
   // NOTE: The initialization order of these members matters.
   content::BrowserTaskEnvironment task_environment_{
       base::test::TaskEnvironment::TimeSource::MOCK_TIME};
@@ -125,6 +128,24 @@
 
   handler().GetAuthState(callback.Get());
   EXPECT_EQ(state, new_tab_page::mojom::AuthState::kSuccess);
+  histogram_tester().ExpectBucketCount("NewTabPage.MicrosoftAuth.AuthStarted",
+                                       new_tab_page::mojom::AuthType::kSilent,
+                                       0);
+}
+
+TEST_F(NtpMicrosoftAuthUntrustedPageHandlerTest, GetAuthStateNone) {
+  base::MockCallback<MicrosoftAuthUntrustedPageHandler::GetAuthStateCallback>
+      callback;
+  new_tab_page::mojom::AuthState state;
+  EXPECT_CALL(callback, Run).WillOnce(MoveArg<0>(&state));
+  ON_CALL(mock_auth_service(), GetAuthState)
+      .WillByDefault(testing::Return(new_tab_page::mojom::AuthState::kNone));
+
+  handler().GetAuthState(callback.Get());
+  EXPECT_EQ(state, new_tab_page::mojom::AuthState::kNone);
+  histogram_tester().ExpectBucketCount("NewTabPage.MicrosoftAuth.AuthStarted",
+                                       new_tab_page::mojom::AuthType::kSilent,
+                                       1);
 }
 
 TEST_F(NtpMicrosoftAuthUntrustedPageHandlerTest, SetAccessToken) {
@@ -155,4 +176,7 @@
   EXPECT_CALL(mock_document(), AcquireTokenSilent);
 
   auth_service_observer().OnAuthStateUpdated();
+  histogram_tester().ExpectBucketCount("NewTabPage.MicrosoftAuth.AuthStarted",
+                                       new_tab_page::mojom::AuthType::kSilent,
+                                       1);
 }
diff --git a/chrome/browser/ui/webui/ntp_microsoft_auth/ntp_microsoft_auth_shared_ui.mojom b/chrome/browser/ui/webui/ntp_microsoft_auth/ntp_microsoft_auth_shared_ui.mojom
index 3f08720..5be54bc8 100644
--- a/chrome/browser/ui/webui/ntp_microsoft_auth/ntp_microsoft_auth_shared_ui.mojom
+++ b/chrome/browser/ui/webui/ntp_microsoft_auth/ntp_microsoft_auth_shared_ui.mojom
@@ -4,6 +4,13 @@
 
 module new_tab_page.mojom;
 
+// Enum for use in NewTabPage.MicrosoftAuth.AuthStarted histogram.
+// Must match the NTPMicrosoftAuthType enum.
+enum AuthType {
+  kSilent,
+  kPopup,
+};
+
 // Interfaces for inter-frame communication between chrome:// and its embedded
 // chrome-untrusted:// child document.
 
diff --git a/chrome/test/data/webui/new_tab_page/modules/v2/authentication/microsoft_auth_module_test.ts b/chrome/test/data/webui/new_tab_page/modules/v2/authentication/microsoft_auth_module_test.ts
index 18d4f462..3e32d878f 100644
--- a/chrome/test/data/webui/new_tab_page/modules/v2/authentication/microsoft_auth_module_test.ts
+++ b/chrome/test/data/webui/new_tab_page/modules/v2/authentication/microsoft_auth_module_test.ts
@@ -5,9 +5,10 @@
 import type {DisableModuleEvent, DismissModuleInstanceEvent, MicrosoftAuthModuleElement} from 'chrome://new-tab-page/lazy_load.js';
 import {microsoftAuthModuleDescriptor, MicrosoftAuthProxyImpl, ParentTrustedDocumentProxy} from 'chrome://new-tab-page/lazy_load.js';
 import {MicrosoftAuthPageHandlerRemote} from 'chrome://new-tab-page/microsoft_auth.mojom-webui.js';
-import {MicrosoftAuthUntrustedDocumentRemote} from 'chrome://new-tab-page/ntp_microsoft_auth_shared_ui.mojom-webui.js';
+import {AuthType, MicrosoftAuthUntrustedDocumentRemote} from 'chrome://new-tab-page/ntp_microsoft_auth_shared_ui.mojom-webui.js';
 import {loadTimeData} from 'chrome://resources/js/load_time_data.js';
 import {assertEquals, assertFalse, assertTrue} from 'chrome://webui-test/chai_assert.js';
+import type {MetricsTracker} from 'chrome://webui-test/metrics_test_support.js';
 import {fakeMetricsPrivate} from 'chrome://webui-test/metrics_test_support.js';
 import type {TestMock} from 'chrome://webui-test/test_mock.js';
 import {eventToPromise, microtasksFinished} from 'chrome://webui-test/test_util.js';
@@ -17,6 +18,7 @@
 suite('MicrosoftAuthModule', () => {
   let handler: TestMock<MicrosoftAuthPageHandlerRemote>;
   let childDocument: TestMock<MicrosoftAuthUntrustedDocumentRemote>;
+  let metrics: MetricsTracker;
   let microsoftAuthModule: MicrosoftAuthModuleElement;
   const modulesMicrosoftAuthName = 'Microsoft Authentication';
 
@@ -34,9 +36,7 @@
         MicrosoftAuthUntrustedDocumentRemote,
         mock => ParentTrustedDocumentProxy.setInstance(mock));
 
-    // Set fake metrics. Otherwise, tests fail when creating descriptor
-    // because of the mock |WindowProxy|.
-    fakeMetricsPrivate();
+    metrics = fakeMetricsPrivate();
   });
 
   async function createMicrosoftAuthElement() {
@@ -99,6 +99,9 @@
 
     // Assert.
     assertEquals(1, childDocument.getCallCount('acquireTokenPopup'));
+    assertEquals(
+        1,
+        metrics.count(`NewTabPage.MicrosoftAuth.AuthStarted`, AuthType.kPopup));
   });
 
   test('does not populate module if handler says not to', async () => {
diff --git a/tools/metrics/histograms/metadata/new_tab_page/enums.xml b/tools/metrics/histograms/metadata/new_tab_page/enums.xml
index 46f319e..1cea9d78 100644
--- a/tools/metrics/histograms/metadata/new_tab_page/enums.xml
+++ b/tools/metrics/histograms/metadata/new_tab_page/enums.xml
@@ -375,6 +375,11 @@
   <int value="2" label="Images in a specific NTP collection"/>
 </enum>
 
+<enum name="NTPMicrosoftAuthType">
+  <int value="0" label="Silent"/>
+  <int value="1" label="Popup"/>
+</enum>
+
 <enum name="NtpModules">
   <summary>
     Hash values for the IDs of NTP modules. Each of these values is computed by
diff --git a/tools/metrics/histograms/metadata/new_tab_page/histograms.xml b/tools/metrics/histograms/metadata/new_tab_page/histograms.xml
index af28611b..36edf45 100644
--- a/tools/metrics/histograms/metadata/new_tab_page/histograms.xml
+++ b/tools/metrics/histograms/metadata/new_tab_page/histograms.xml
@@ -621,6 +621,20 @@
   </summary>
 </histogram>
 
+<histogram name="NewTabPage.MicrosoftAuth.AuthStarted"
+    enum="NTPMicrosoftAuthType" expires_after="2026-02-06">
+  <owner>rtatum@google.com</owner>
+  <owner>pauladedeji@google.com</owner>
+  <owner>chrome-desktop-ntp@google.com</owner>
+  <summary>
+    Records the type of Microsoft Auth (Silent or Popup) whenever Microsoft
+    authentication is initiated by the NTP. This is only logged on the 1P NTP
+    when the Microsoft Authentication module and another Microsoft module are
+    enabled. It can be logged because of an initial login or because an access
+    token expires.
+  </summary>
+</histogram>
+
 <histogram name="NewTabPage.MicrosoftFiles.FileClick" units="index"
     expires_after="2026-01-20">
   <owner>jennserrano@google.com</owner>