metrics_helper.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. #ifndef COMPONENTS_SECURITY_INTERSTITIALS_CORE_METRICS_HELPER_H_
  5. #define COMPONENTS_SECURITY_INTERSTITIALS_CORE_METRICS_HELPER_H_
  6. #include <string>
  7. #include "base/memory/weak_ptr.h"
  8. #include "base/task/cancelable_task_tracker.h"
  9. #include "url/gurl.h"
  10. namespace history {
  11. class HistoryService;
  12. struct VisibleVisitCountToHostResult;
  13. }
  14. namespace security_interstitials {
  15. // MetricsHelper records user warning interactions in a common way via METRICS
  16. // histograms. The class will generate the following histograms:
  17. // METRICS: interstitial.<metric_prefix>.decision[.repeat_visit]
  18. // METRICS: interstitial.<metric_prefix>.interaction[.repeat_visit]
  19. // wherein |metric_prefix| is specified via ReportDetails.
  20. // repeat_visit is also generated if the user has seen the page before.
  21. //
  22. // If |extra_suffix| is not empty, MetricsHelper will append ".<extra_suffix>"
  23. // to generate an additional 2 or 4 more metrics.
  24. class MetricsHelper {
  25. public:
  26. // These enums are used for histograms. Don't reorder, delete, or insert
  27. // elements. New elements should be added at the end (right before the max).
  28. enum Decision {
  29. SHOW,
  30. PROCEED,
  31. DONT_PROCEED,
  32. PROCEEDING_DISABLED,
  33. MAX_DECISION
  34. };
  35. enum Interaction {
  36. TOTAL_VISITS,
  37. SHOW_ADVANCED,
  38. SHOW_PRIVACY_POLICY,
  39. SHOW_DIAGNOSTIC,
  40. SHOW_LEARN_MORE,
  41. RELOAD,
  42. OPEN_TIME_SETTINGS,
  43. SET_EXTENDED_REPORTING_ENABLED,
  44. SET_EXTENDED_REPORTING_DISABLED,
  45. EXTENDED_REPORTING_IS_ENABLED,
  46. REPORT_PHISHING_ERROR,
  47. SHOW_WHITEPAPER,
  48. SHOW_ENHANCED_PROTECTION,
  49. OPEN_ENHANCED_PROTECTION,
  50. MAX_INTERACTION
  51. };
  52. // metric_prefix: Histogram prefix for UMA.
  53. // examples: "phishing", "ssl_overridable"
  54. // extra_suffix: If not-empty, will generate second set of metrics by
  55. // placing at the end of the metric name. Examples:
  56. // "from_datasaver", "from_device"
  57. struct ReportDetails {
  58. ReportDetails();
  59. ReportDetails(const ReportDetails& other);
  60. ~ReportDetails();
  61. std::string metric_prefix;
  62. std::string extra_suffix;
  63. };
  64. // Args:
  65. // url: URL of page that triggered the interstitial. Only origin is used.
  66. // history_service: Set this to record metrics based on whether the user
  67. // has visited this hostname before.
  68. // settings: Specify reporting details (prefixes and report types).
  69. // sampling_event_name: Event name for Experience Sampling.
  70. // e.g. "phishing_interstitial_"
  71. MetricsHelper(const GURL& url,
  72. const ReportDetails settings,
  73. history::HistoryService* history_service);
  74. MetricsHelper(const MetricsHelper&) = delete;
  75. MetricsHelper& operator=(const MetricsHelper&) = delete;
  76. virtual ~MetricsHelper();
  77. // Records a user decision or interaction to the appropriate UMA metrics
  78. // histogram.
  79. void RecordUserDecision(Decision decision);
  80. void RecordUserInteraction(Interaction interaction);
  81. void RecordShutdownMetrics();
  82. // Number of times user visited this origin before. -1 means not-yet-set.
  83. int NumVisits();
  84. protected:
  85. // Subclasses should implement any embedder-specific recording logic in these
  86. // methods. They'll be invoked from the matching Record methods.
  87. virtual void RecordExtraUserDecisionMetrics(Decision decision);
  88. virtual void RecordExtraUserInteractionMetrics(Interaction interaction);
  89. virtual void RecordExtraShutdownMetrics();
  90. private:
  91. // Used to query the HistoryService to see if the URL is in history. It will
  92. // only be invoked if the constructor received |history_service|.
  93. void OnGotHistoryCount(history::VisibleVisitCountToHostResult result);
  94. void RecordUserDecisionToMetrics(Decision decision,
  95. const std::string& histogram_name);
  96. const GURL request_url_;
  97. const ReportDetails settings_;
  98. int num_visits_;
  99. base::CancelableTaskTracker request_tracker_;
  100. };
  101. } // namespace security_interstitials
  102. #endif // COMPONENTS_SECURITY_INTERSTITIALS_CORE_METRICS_HELPER_H_