From 78da6543bfa311596260e7dc1cde051286d751f9 Mon Sep 17 00:00:00 2001
From: droger <droger@chromium.org>
Date: Mon, 2 Nov 2015 03:25:59 -0800
Subject: [PATCH] Componentize ProfileMetrics::Counts

ProfileMetrics::Counts and related histograms are used by all platforms
including iOS, and it is important that iOS remains consistent with the
other platforms.
This CL componentizes these metrics to be able to share them cleanly on
iOS.

TBR=jochen

Review URL: https://codereview.chromium.org/1417903006

Cr-Commit-Position: refs/heads/master@{#357333}
---
 chrome/browser/BUILD.gn                    |  1 +
 chrome/browser/DEPS                        |  1 +
 chrome/browser/profiles/profile_metrics.cc | 23 ++++-----------
 chrome/browser/profiles/profile_metrics.h  | 22 ++++----------
 chrome/chrome_browser.gypi                 |  1 +
 components/OWNERS                          |  4 +++
 components/components.gyp                  |  1 +
 components/profile_metrics.gypi            | 24 +++++++++++++++
 components/profile_metrics/BUILD.gn        | 15 ++++++++++
 components/profile_metrics/OWNERS          |  3 ++
 components/profile_metrics/counts.cc       | 30 +++++++++++++++++++
 components/profile_metrics/counts.h        | 34 ++++++++++++++++++++++
 12 files changed, 125 insertions(+), 34 deletions(-)
 create mode 100644 components/profile_metrics.gypi
 create mode 100644 components/profile_metrics/BUILD.gn
 create mode 100644 components/profile_metrics/OWNERS
 create mode 100644 components/profile_metrics/counts.cc
 create mode 100644 components/profile_metrics/counts.h

diff --git a/chrome/browser/BUILD.gn b/chrome/browser/BUILD.gn
index 653714d858eb2..556c489c8890c 100644
--- a/chrome/browser/BUILD.gn
+++ b/chrome/browser/BUILD.gn
@@ -297,6 +297,7 @@ source_set("browser") {
       "//components/password_manager/content/browser",
       "//components/password_manager/sync/browser",
       "//components/plugins/common",
+      "//components/profile_metrics",
       "//components/proxy_config",
       "//components/resources",
       "//components/safe_browsing_db",
diff --git a/chrome/browser/DEPS b/chrome/browser/DEPS
index cd6a091864347..cfe67c6cd2528 100644
--- a/chrome/browser/DEPS
+++ b/chrome/browser/DEPS
@@ -76,6 +76,7 @@ include_rules = [
   "+components/pref_registry",
   "+components/printing/browser",
   "+components/printing/common",
+  "+components/profile_metrics",
   "+components/proximity_auth",
   "+components/proxy_config",
   "+components/query_parser",
diff --git a/chrome/browser/profiles/profile_metrics.cc b/chrome/browser/profiles/profile_metrics.cc
index a0143d58da485..1825c95bd804d 100644
--- a/chrome/browser/profiles/profile_metrics.cc
+++ b/chrome/browser/profiles/profile_metrics.cc
@@ -15,6 +15,7 @@
 #include "chrome/browser/ui/browser_finder.h"
 #include "chrome/common/chrome_constants.h"
 #include "chrome/installer/util/google_update_settings.h"
+#include "components/profile_metrics/counts.h"
 #include "content/public/browser/browser_thread.h"
 #include "content/public/browser/user_metrics.h"
 
@@ -143,7 +144,7 @@ enum ProfileAvatar {
 };
 
 bool ProfileMetrics::CountProfileInformation(ProfileManager* manager,
-                                             ProfileCounts* counts) {
+                                             profile_metrics::Counts* counts) {
   const ProfileInfoCache& info_cache = manager->GetProfileInfoCache();
   size_t number_of_profiles = info_cache.GetNumberOfProfiles();
   counts->total = number_of_profiles;
@@ -176,7 +177,7 @@ bool ProfileMetrics::CountProfileInformation(ProfileManager* manager,
 
 void ProfileMetrics::UpdateReportedProfilesStatistics(ProfileManager* manager) {
 #if defined(OS_WIN) || defined(OS_MACOSX)
-  ProfileCounts counts;
+  profile_metrics::Counts counts;
   if (CountProfileInformation(manager, &counts)) {
     size_t limited_total = counts.total;
     size_t limited_signedin = counts.signedin;
@@ -207,25 +208,13 @@ void ProfileMetrics::UpdateReportedOSProfileStatistics(
 #endif
 
 void ProfileMetrics::LogNumberOfProfiles(ProfileManager* manager) {
-  ProfileCounts counts;
+  profile_metrics::Counts counts;
   bool success = CountProfileInformation(manager, &counts);
-  UMA_HISTOGRAM_COUNTS_100("Profile.NumberOfProfiles", counts.total);
+
+  profile_metrics::LogProfileMetricsCounts(counts);
 
   // Ignore other metrics if we have no profiles.
   if (success) {
-    UMA_HISTOGRAM_COUNTS_100("Profile.NumberOfManagedProfiles",
-                             counts.supervised);
-    UMA_HISTOGRAM_COUNTS_100("Profile.PercentageOfManagedProfiles",
-                             100 * counts.supervised / counts.total);
-    UMA_HISTOGRAM_COUNTS_100("Profile.NumberOfSignedInProfiles",
-                             counts.signedin);
-    UMA_HISTOGRAM_COUNTS_100("Profile.NumberOfUnusedProfiles",
-                             counts.unused);
-    UMA_HISTOGRAM_COUNTS_100("Profile.NumberOfSignedInProfilesWithGAIAIcons",
-                             counts.gaia_icon);
-    UMA_HISTOGRAM_COUNTS_100("Profile.NumberOfProfilesWithAuthErrors",
-                             counts.auth_errors);
-
     LogLockedProfileInformation(manager);
 
 #if defined(OS_WIN) || defined(OS_MACOSX)
diff --git a/chrome/browser/profiles/profile_metrics.h b/chrome/browser/profiles/profile_metrics.h
index 0b3c71fc4bb7f..c34635fbf5f87 100644
--- a/chrome/browser/profiles/profile_metrics.h
+++ b/chrome/browser/profiles/profile_metrics.h
@@ -19,24 +19,12 @@ namespace base {
 class FilePath;
 }
 
+namespace profile_metrics {
+struct Counts;
+}
+
 class ProfileMetrics {
  public:
-  struct ProfileCounts {
-    size_t total;
-    size_t signedin;
-    size_t supervised;
-    size_t unused;
-    size_t gaia_icon;
-    size_t auth_errors;
-
-    ProfileCounts()
-        : total(0),
-          signedin(0),
-          supervised(0),
-          unused(0),
-          gaia_icon(0),
-          auth_errors(0) {}
-  };
 
   // Enum for counting the ways users were added.
   enum ProfileAdd {
@@ -213,7 +201,7 @@ class ProfileMetrics {
   // Count and return summary information about the profiles currently in the
   // |manager|. This information is returned in the output variable |counts|.
   static bool CountProfileInformation(ProfileManager* manager,
-                                      ProfileCounts* counts);
+                                      profile_metrics::Counts* counts);
 
 #if !defined(OS_ANDROID) && !defined(OS_IOS)
   static void LogNumberOfProfileSwitches();
diff --git a/chrome/chrome_browser.gypi b/chrome/chrome_browser.gypi
index 19dac934b3f31..9392b0feba2f8 100644
--- a/chrome/chrome_browser.gypi
+++ b/chrome/chrome_browser.gypi
@@ -3234,6 +3234,7 @@
             '../components/components.gyp:password_manager_sync_browser',
             '../components/components.gyp:plugins_common',
             '../components/components.gyp:power',
+            '../components/components.gyp:profile_metrics',
             '../components/components.gyp:proxy_config',
             '../components/components.gyp:safe_browsing_db',
             '../components/components.gyp:safe_json',
diff --git a/components/OWNERS b/components/OWNERS
index 2c5c12e72ff3b..7de4ca976ea15 100644
--- a/components/OWNERS
+++ b/components/OWNERS
@@ -232,6 +232,10 @@ per-file policy*=bartfab@chromium.org
 per-file policy*=atwilson@chromium.org
 per-file policy*=cschuet@chromium.org
 
+per-file profile_metrics*=anthonyvd@chromium.org
+per-file profile_metrics*=erg@chromium.org
+per-file profile_metrics*=mlerman@chromium.org
+
 per-file proximity_auth*=isherman@chromium.org
 per-file proximity_auth*=tbarzic@chromium.org
 per-file proximity_auth*=tengs@chromium.org
diff --git a/components/components.gyp b/components/components.gyp
index 3a29ea2a3bbbb..21ae71fa8be63 100644
--- a/components/components.gyp
+++ b/components/components.gyp
@@ -66,6 +66,7 @@
     'policy.gypi',
     'precache.gypi',
     'pref_registry.gypi',
+    'profile_metrics.gypi',
     'proxy_config.gypi',
     'query_parser.gypi',
     'rappor.gypi',
diff --git a/components/profile_metrics.gypi b/components/profile_metrics.gypi
new file mode 100644
index 0000000000000..49e713ada4ce3
--- /dev/null
+++ b/components/profile_metrics.gypi
@@ -0,0 +1,24 @@
+# Copyright 2015 The Chromium Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+{
+  'targets': [
+    {
+      # GN version: //components/profile_metrics
+      'target_name': 'profile_metrics',
+      'type': 'static_library',
+      'include_dirs': [
+        '..',
+      ],
+      'dependencies': [
+        '../base/base.gyp:base',
+      ],
+      'sources': [
+        # Note: sources list duplicated in GN build.
+        'profile_metrics/counts.cc',
+        'profile_metrics/counts.h',
+      ],
+    },
+  ],
+}
diff --git a/components/profile_metrics/BUILD.gn b/components/profile_metrics/BUILD.gn
new file mode 100644
index 0000000000000..8adf08bb883a0
--- /dev/null
+++ b/components/profile_metrics/BUILD.gn
@@ -0,0 +1,15 @@
+# Copyright 2015 The Chromium Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+# GYP version: components/profile_metrics.gypi:profile_metrics
+static_library("profile_metrics") {
+  sources = [
+    "counts.cc",
+    "counts.h",
+  ]
+
+  deps = [
+    "//base",
+  ]
+}
diff --git a/components/profile_metrics/OWNERS b/components/profile_metrics/OWNERS
new file mode 100644
index 0000000000000..367bb85da5f4c
--- /dev/null
+++ b/components/profile_metrics/OWNERS
@@ -0,0 +1,3 @@
+anthonyvd@chromium.org
+erg@chromium.org
+mlerman@chromium.org
diff --git a/components/profile_metrics/counts.cc b/components/profile_metrics/counts.cc
new file mode 100644
index 0000000000000..9186d042a6655
--- /dev/null
+++ b/components/profile_metrics/counts.cc
@@ -0,0 +1,30 @@
+// Copyright 2015 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "components/profile_metrics/counts.h"
+
+#include "base/metrics/histogram.h"
+
+namespace profile_metrics {
+
+void LogProfileMetricsCounts(const Counts& counts) {
+  UMA_HISTOGRAM_COUNTS_100("Profile.NumberOfProfiles", counts.total);
+
+  // Ignore other metrics if we have no profiles.
+  if (counts.total > 0) {
+    UMA_HISTOGRAM_COUNTS_100("Profile.NumberOfManagedProfiles",
+                             counts.supervised);
+    UMA_HISTOGRAM_COUNTS_100("Profile.PercentageOfManagedProfiles",
+                             100 * counts.supervised / counts.total);
+    UMA_HISTOGRAM_COUNTS_100("Profile.NumberOfSignedInProfiles",
+                             counts.signedin);
+    UMA_HISTOGRAM_COUNTS_100("Profile.NumberOfUnusedProfiles", counts.unused);
+    UMA_HISTOGRAM_COUNTS_100("Profile.NumberOfSignedInProfilesWithGAIAIcons",
+                             counts.gaia_icon);
+    UMA_HISTOGRAM_COUNTS_100("Profile.NumberOfProfilesWithAuthErrors",
+                             counts.auth_errors);
+  }
+}
+
+}  // namespace profile_metrics
diff --git a/components/profile_metrics/counts.h b/components/profile_metrics/counts.h
new file mode 100644
index 0000000000000..a02a9725a0fe6
--- /dev/null
+++ b/components/profile_metrics/counts.h
@@ -0,0 +1,34 @@
+// Copyright 2015 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef COMPONENTS_PROFILE_METRICS_COUNTS_H_
+#define COMPONENTS_PROFILE_METRICS_COUNTS_H_
+
+#include "base/metrics/histogram_base.h"
+
+namespace profile_metrics {
+
+struct Counts {
+  base::HistogramBase::Sample total;
+  base::HistogramBase::Sample signedin;
+  base::HistogramBase::Sample supervised;
+  base::HistogramBase::Sample unused;
+  base::HistogramBase::Sample gaia_icon;
+  base::HistogramBase::Sample auth_errors;
+
+  Counts()
+      : total(0),
+        signedin(0),
+        supervised(0),
+        unused(0),
+        gaia_icon(0),
+        auth_errors(0) {}
+};
+
+// Logs metrics related to |counts|.
+void LogProfileMetricsCounts(const Counts& counts);
+
+}  // namespace profile_metrics
+
+#endif  // COMPONENTS_PROFILE_METRICS_COUNTS_H_