[Global Media Controls] Add UMA to HardwareKeyMediaController

This CL records key presses in HardwareKeyMediaController for each
supported MediaSessionAction.

Bug: 921281
Change-Id: I0e2cbecc3a25fa2b850ff51830ecd9fc71023dce
Reviewed-on: https://chromium-review.googlesource.com/c/1435905
Commit-Queue: Tommy Steimel <steimel@chromium.org>
Reviewed-by: Mounir Lamouri <mlamouri@chromium.org>
Reviewed-by: Steven Holte <holte@chromium.org>
Reviewed-by: Scott Violet <sky@chromium.org>
Cr-Original-Commit-Position: refs/heads/master@{#627732}(cherry picked from commit f36cc732d6568d9182516504715df9148a137573)
Reviewed-on: https://chromium-review.googlesource.com/c/1452461
Reviewed-by: Tommy Steimel <steimel@chromium.org>
Cr-Commit-Position: refs/branch-heads/3683@{#170}
Cr-Branched-From: e51029943e0a38dd794b73caaf6373d5496ae783-refs/heads/master@{#625896}
diff --git a/content/browser/media/hardware_key_media_controller.cc b/content/browser/media/hardware_key_media_controller.cc
index 26be8a4..f7cf052 100644
--- a/content/browser/media/hardware_key_media_controller.cc
+++ b/content/browser/media/hardware_key_media_controller.cc
@@ -8,6 +8,7 @@
 #include <utility>
 #include <vector>
 
+#include "base/metrics/histogram_macros.h"
 #include "content/public/browser/media_keys_listener_manager.h"
 #include "services/media_session/public/mojom/constants.mojom.h"
 #include "services/service_manager/public/cpp/connector.h"
@@ -112,18 +113,23 @@
   switch (action) {
     case MediaSessionAction::kPreviousTrack:
       media_controller_ptr_->PreviousTrack();
+      RecordAction(MediaHardwareKeyAction::kActionPreviousTrack);
       return;
     case MediaSessionAction::kPlay:
       media_controller_ptr_->Resume();
+      RecordAction(MediaHardwareKeyAction::kActionPlay);
       return;
     case MediaSessionAction::kPause:
       media_controller_ptr_->Suspend();
+      RecordAction(MediaHardwareKeyAction::kActionPause);
       return;
     case MediaSessionAction::kNextTrack:
       media_controller_ptr_->NextTrack();
+      RecordAction(MediaHardwareKeyAction::kActionNextTrack);
       return;
     case MediaSessionAction::kStop:
       media_controller_ptr_->Stop();
+      RecordAction(MediaHardwareKeyAction::kActionStop);
       return;
     case MediaSessionAction::kSeekBackward:
     case MediaSessionAction::kSeekForward:
@@ -133,6 +139,10 @@
   }
 }
 
+void HardwareKeyMediaController::RecordAction(MediaHardwareKeyAction action) {
+  UMA_HISTOGRAM_ENUMERATION("Media.HardwareKeyPressed", action);
+}
+
 MediaSessionAction HardwareKeyMediaController::KeyCodeToMediaSessionAction(
     ui::KeyboardCode key_code) const {
   switch (key_code) {
diff --git a/content/browser/media/hardware_key_media_controller.h b/content/browser/media/hardware_key_media_controller.h
index 1e5d40e..b9ecba2 100644
--- a/content/browser/media/hardware_key_media_controller.h
+++ b/content/browser/media/hardware_key_media_controller.h
@@ -50,6 +50,17 @@
   }
 
  private:
+  // These values are persisted to logs. Entries should not be renumbered and
+  // numeric values should never be reused.
+  enum class MediaHardwareKeyAction {
+    kActionPlay = 0,
+    kActionPause,
+    kActionStop,
+    kActionNextTrack,
+    kActionPreviousTrack,
+    kMaxValue = kActionPreviousTrack
+  };
+
   // Used for converting between MediaSessionAction and KeyboardCode.
   media_session::mojom::MediaSessionAction KeyCodeToMediaSessionAction(
       ui::KeyboardCode key_code) const;
@@ -61,6 +72,7 @@
 
   bool SupportsAction(media_session::mojom::MediaSessionAction action) const;
   void PerformAction(media_session::mojom::MediaSessionAction action);
+  void RecordAction(MediaHardwareKeyAction action);
 
   // Used to control the active session.
   media_session::mojom::MediaControllerPtr media_controller_ptr_;
diff --git a/tools/metrics/histograms/enums.xml b/tools/metrics/histograms/enums.xml
index de60370..cc6f84c 100644
--- a/tools/metrics/histograms/enums.xml
+++ b/tools/metrics/histograms/enums.xml
@@ -34596,6 +34596,14 @@
   <int value="27" label="iPhoto file system used this session (obsolete)"/>
 </enum>
 
+<enum name="MediaHardwareKeyAction">
+  <int value="0" label="Play"/>
+  <int value="1" label="Pause"/>
+  <int value="2" label="Stop"/>
+  <int value="3" label="Next Track"/>
+  <int value="4" label="Previous Track"/>
+</enum>
+
 <enum name="MediaKeyError">
   <int value="1" label="kUnknownError"/>
   <int value="2" label="kClientError"/>
diff --git a/tools/metrics/histograms/histograms.xml b/tools/metrics/histograms/histograms.xml
index 4b5c863..4db4711 100644
--- a/tools/metrics/histograms/histograms.xml
+++ b/tools/metrics/histograms/histograms.xml
@@ -48152,6 +48152,15 @@
   </summary>
 </histogram>
 
+<histogram name="Media.HardwareKeyPressed" enum="MediaHardwareKeyAction">
+  <owner>steimel@chromium.org</owner>
+  <owner>media-dev@chromium.org</owner>
+  <summary>
+    Records media key presses that are handled by the
+    HardwareKeyMediaController.
+  </summary>
+</histogram>
+
 <histogram name="Media.HasEverPlayed" enum="BooleanHasPlayed">
   <owner>dalecurtis@chromium.org</owner>
   <summary>