Re-subimission of https://codereview.chromium.org/1041213003/
[chromium-blink-merge.git] / media / base / key_systems_support_uma.cc
blobf0cdf8495a1865bf624b1bf47d3557315ba179c6
1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "media/base/key_systems_support_uma.h"
7 #include <string>
9 #include "base/metrics/histogram.h"
10 #include "media/base/key_systems.h"
12 namespace media {
14 namespace {
16 const char kKeySystemSupportUMAPrefix[] = "Media.EME.KeySystemSupport.";
18 // These values are reported to UMA. Do not change the existing values!
19 enum KeySystemSupportStatus {
20 KEY_SYSTEM_QUERIED = 0,
21 KEY_SYSTEM_SUPPORTED = 1,
22 KEY_SYSTEM_WITH_TYPE_QUERIED = 2,
23 KEY_SYSTEM_WITH_TYPE_SUPPORTED = 3,
24 KEY_SYSTEM_SUPPORT_STATUS_COUNT
27 // Reports an event only once.
28 class OneTimeReporter {
29 public:
30 OneTimeReporter(const std::string& key_system, KeySystemSupportStatus status);
31 ~OneTimeReporter();
33 void Report();
35 private:
36 bool is_reported_;
37 const std::string key_system_;
38 const KeySystemSupportStatus status_;
41 OneTimeReporter::OneTimeReporter(const std::string& key_system,
42 KeySystemSupportStatus status)
43 : is_reported_(false), key_system_(key_system), status_(status) {
46 OneTimeReporter::~OneTimeReporter() {}
48 void OneTimeReporter::Report() {
49 if (is_reported_)
50 return;
52 // Not using UMA_HISTOGRAM_ENUMERATION directly because UMA_* macros require
53 // the names to be constant throughout the process' lifetime.
54 base::LinearHistogram::FactoryGet(
55 kKeySystemSupportUMAPrefix + GetKeySystemNameForUMA(key_system_), 1,
56 KEY_SYSTEM_SUPPORT_STATUS_COUNT, KEY_SYSTEM_SUPPORT_STATUS_COUNT + 1,
57 base::Histogram::kUmaTargetedHistogramFlag)->Add(status_);
59 is_reported_ = true;
62 } // namespace
64 class KeySystemsSupportUMA::Reporter {
65 public:
66 explicit Reporter(const std::string& key_system);
67 ~Reporter();
69 void Report(bool has_type, bool is_supported);
71 private:
72 const std::string key_system_;
74 OneTimeReporter call_reporter_;
75 OneTimeReporter call_with_type_reporter_;
76 OneTimeReporter support_reporter_;
77 OneTimeReporter support_with_type_reporter_;
80 KeySystemsSupportUMA::Reporter::Reporter(const std::string& key_system)
81 : key_system_(key_system),
82 call_reporter_(key_system, KEY_SYSTEM_QUERIED),
83 call_with_type_reporter_(key_system, KEY_SYSTEM_WITH_TYPE_QUERIED),
84 support_reporter_(key_system, KEY_SYSTEM_SUPPORTED),
85 support_with_type_reporter_(key_system, KEY_SYSTEM_WITH_TYPE_SUPPORTED) {}
87 KeySystemsSupportUMA::Reporter::~Reporter() {}
89 void KeySystemsSupportUMA::Reporter::Report(bool has_type, bool is_supported) {
90 call_reporter_.Report();
91 if (has_type)
92 call_with_type_reporter_.Report();
94 if (!is_supported)
95 return;
97 support_reporter_.Report();
98 if (has_type)
99 support_with_type_reporter_.Report();
102 KeySystemsSupportUMA::KeySystemsSupportUMA() {}
104 KeySystemsSupportUMA::~KeySystemsSupportUMA() {}
106 void KeySystemsSupportUMA::AddKeySystemToReport(const std::string& key_system) {
107 DCHECK(!GetReporter(key_system));
108 reporters_.set(key_system, scoped_ptr<Reporter>(new Reporter(key_system)));
111 void KeySystemsSupportUMA::ReportKeySystemQuery(const std::string& key_system,
112 bool has_type) {
113 Reporter* reporter = GetReporter(key_system);
114 if (!reporter)
115 return;
116 reporter->Report(has_type, false);
119 void KeySystemsSupportUMA::ReportKeySystemSupport(const std::string& key_system,
120 bool has_type) {
121 Reporter* reporter = GetReporter(key_system);
122 if (!reporter)
123 return;
124 reporter->Report(has_type, true);
127 KeySystemsSupportUMA::Reporter* KeySystemsSupportUMA::GetReporter(
128 const std::string& key_system) {
129 Reporters::iterator reporter = reporters_.find(key_system);
130 if (reporter == reporters_.end())
131 return NULL;
132 return reporter->second;
135 } // namespace media