metrics_helper.cc 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. // Copyright 2015 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.
  4. #include "components/security_interstitials/core/metrics_helper.h"
  5. #include <memory>
  6. #include <utility>
  7. #include "base/bind.h"
  8. #include "base/metrics/histogram.h"
  9. #include "base/metrics/histogram_functions.h"
  10. #include "base/metrics/user_metrics.h"
  11. #include "base/metrics/user_metrics_action.h"
  12. #include "components/history/core/browser/history_service.h"
  13. #include "net/base/registry_controlled_domains/registry_controlled_domain.h"
  14. using base::RecordAction;
  15. using base::UserMetricsAction;
  16. namespace security_interstitials {
  17. namespace {
  18. void RecordSingleDecisionToMetrics(MetricsHelper::Decision decision,
  19. const std::string& histogram_name) {
  20. base::UmaHistogramExactLinear(histogram_name, decision,
  21. MetricsHelper::MAX_DECISION);
  22. }
  23. void RecordSingleInteractionToMetrics(MetricsHelper::Interaction interaction,
  24. const std::string& histogram_name) {
  25. base::UmaHistogramExactLinear(histogram_name, interaction,
  26. MetricsHelper::MAX_INTERACTION);
  27. }
  28. void MaybeRecordDecisionAsAction(MetricsHelper::Decision decision,
  29. const std::string& metric_name) {
  30. if (decision == MetricsHelper::PROCEED) {
  31. if (metric_name == "malware")
  32. RecordAction(UserMetricsAction("MalwareInterstitial.Proceed"));
  33. else if (metric_name == "harmful")
  34. RecordAction(UserMetricsAction("HarmfulInterstitial.Proceed"));
  35. else if (metric_name == "ssl_overridable")
  36. RecordAction(UserMetricsAction("SSLOverridableInterstitial.Proceed"));
  37. } else if (decision == MetricsHelper::DONT_PROCEED) {
  38. if (metric_name == "malware")
  39. RecordAction(UserMetricsAction("MalwareInterstitial.Back"));
  40. else if (metric_name == "harmful")
  41. RecordAction(UserMetricsAction("HarmfulInterstitial.Back"));
  42. else if (metric_name == "ssl_overridable")
  43. RecordAction(UserMetricsAction("SSLOverridableInterstitial.Back"));
  44. else if (metric_name == "ssl_nonoverridable")
  45. RecordAction(UserMetricsAction("SSLNonOverridableInsterstitial.Back"));
  46. else if (metric_name == "bad_clock")
  47. RecordAction(UserMetricsAction("BadClockInterstitial.Back"));
  48. }
  49. }
  50. void MaybeRecordInteractionAsAction(MetricsHelper::Interaction interaction,
  51. const std::string& metric_name) {
  52. if (interaction == MetricsHelper::TOTAL_VISITS) {
  53. if (metric_name == "malware")
  54. RecordAction(UserMetricsAction("MalwareInterstitial.Show"));
  55. else if (metric_name == "harmful")
  56. RecordAction(UserMetricsAction("HarmfulInterstitial.Show"));
  57. else if (metric_name == "ssl_overridable")
  58. RecordAction(UserMetricsAction("SSLOverridableInterstitial.Show"));
  59. else if (metric_name == "ssl_nonoverridable")
  60. RecordAction(UserMetricsAction("SSLNonOverridableInterstitial.Show"));
  61. else if (metric_name == "bad_clock")
  62. RecordAction(UserMetricsAction("BadClockInterstitial.Show"));
  63. } else if (interaction == MetricsHelper::SHOW_ADVANCED) {
  64. if (metric_name == "malware") {
  65. RecordAction(UserMetricsAction("MalwareInterstitial.Advanced"));
  66. } else if (metric_name == "harmful") {
  67. RecordAction(UserMetricsAction("HarmfulInterstitial.Advanced"));
  68. } else if (metric_name == "ssl_overridable" ||
  69. metric_name == "ssl_nonoverridable") {
  70. RecordAction(UserMetricsAction("SSLInterstitial.Advanced"));
  71. }
  72. } else if (interaction == MetricsHelper::RELOAD) {
  73. if (metric_name == "ssl_nonoverridable")
  74. RecordAction(UserMetricsAction("SSLInterstitial.Reload"));
  75. } else if (interaction == MetricsHelper::OPEN_TIME_SETTINGS) {
  76. if (metric_name == "bad_clock")
  77. RecordAction(UserMetricsAction("BadClockInterstitial.Settings"));
  78. }
  79. }
  80. } // namespace
  81. MetricsHelper::~MetricsHelper() {}
  82. MetricsHelper::ReportDetails::ReportDetails() {}
  83. MetricsHelper::ReportDetails::ReportDetails(const ReportDetails& other) =
  84. default;
  85. MetricsHelper::ReportDetails::~ReportDetails() {}
  86. MetricsHelper::MetricsHelper(const GURL& request_url,
  87. const ReportDetails settings,
  88. history::HistoryService* history_service)
  89. : request_url_(request_url), settings_(settings), num_visits_(-1) {
  90. DCHECK(!settings_.metric_prefix.empty());
  91. if (history_service) {
  92. history_service->GetVisibleVisitCountToHost(
  93. request_url_,
  94. base::BindOnce(&MetricsHelper::OnGotHistoryCount,
  95. base::Unretained(this)),
  96. &request_tracker_);
  97. }
  98. }
  99. void MetricsHelper::RecordUserDecision(Decision decision) {
  100. const std::string histogram_name(
  101. "interstitial." + settings_.metric_prefix + ".decision");
  102. RecordUserDecisionToMetrics(decision, histogram_name);
  103. // Record additional information about sites that users have visited before.
  104. // Report |decision| and SHOW together, filtered by the same history state
  105. // so they they are paired regardless of when if num_visits_ is populated.
  106. if (num_visits_ > 0 && (decision == PROCEED || decision == DONT_PROCEED)) {
  107. RecordUserDecisionToMetrics(SHOW, histogram_name + ".repeat_visit");
  108. RecordUserDecisionToMetrics(decision, histogram_name + ".repeat_visit");
  109. }
  110. MaybeRecordDecisionAsAction(decision, settings_.metric_prefix);
  111. }
  112. void MetricsHelper::RecordUserDecisionToMetrics(
  113. Decision decision,
  114. const std::string& histogram_name) {
  115. // Record the decision, and additionally |with extra_suffix|.
  116. RecordSingleDecisionToMetrics(decision, histogram_name);
  117. if (!settings_.extra_suffix.empty()) {
  118. RecordSingleDecisionToMetrics(
  119. decision, histogram_name + "." + settings_.extra_suffix);
  120. }
  121. }
  122. void MetricsHelper::RecordUserInteraction(Interaction interaction) {
  123. const std::string histogram_name(
  124. "interstitial." + settings_.metric_prefix + ".interaction");
  125. RecordSingleInteractionToMetrics(interaction, histogram_name);
  126. if (!settings_.extra_suffix.empty()) {
  127. RecordSingleInteractionToMetrics(
  128. interaction, histogram_name + "." + settings_.extra_suffix);
  129. }
  130. MaybeRecordInteractionAsAction(interaction, settings_.metric_prefix);
  131. }
  132. void MetricsHelper::RecordShutdownMetrics() {
  133. RecordExtraShutdownMetrics();
  134. }
  135. int MetricsHelper::NumVisits() {
  136. return num_visits_;
  137. }
  138. void MetricsHelper::RecordExtraUserDecisionMetrics(Decision decision) {}
  139. void MetricsHelper::RecordExtraUserInteractionMetrics(Interaction interaction) {
  140. }
  141. void MetricsHelper::RecordExtraShutdownMetrics() {}
  142. void MetricsHelper::OnGotHistoryCount(
  143. history::VisibleVisitCountToHostResult result) {
  144. if (result.success)
  145. num_visits_ = result.count;
  146. }
  147. } // namespace security_interstitials