popup_tracker.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. // Copyright 2017 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_BLOCKED_CONTENT_POPUP_TRACKER_H_
  5. #define COMPONENTS_BLOCKED_CONTENT_POPUP_TRACKER_H_
  6. #include "base/scoped_observation.h"
  7. #include "base/time/time.h"
  8. #include "components/subresource_filter/content/browser/subresource_filter_observer.h"
  9. #include "components/subresource_filter/content/browser/subresource_filter_observer_manager.h"
  10. #include "content/public/browser/web_contents_observer.h"
  11. #include "content/public/browser/web_contents_user_data.h"
  12. #include "services/metrics/public/cpp/ukm_source_id.h"
  13. #include "third_party/abseil-cpp/absl/types/optional.h"
  14. #include "ui/base/scoped_visibility_tracker.h"
  15. #include "ui/base/window_open_disposition.h"
  16. namespace content {
  17. class WebContents;
  18. }
  19. namespace blocked_content {
  20. // This class tracks new popups, and is used to log metrics on the visibility
  21. // time of the first document in the popup.
  22. // TODO(csharrison): Consider adding more metrics like total visibility for the
  23. // lifetime of the WebContents.
  24. class PopupTracker : public content::WebContentsObserver,
  25. public content::WebContentsUserData<PopupTracker>,
  26. public subresource_filter::SubresourceFilterObserver {
  27. public:
  28. // These values are persisted to logs. Entries should not be renumbered and
  29. // numeric values should never be reused.
  30. enum class PopupSafeBrowsingStatus {
  31. kNoValue = 0,
  32. kSafe = 1,
  33. kUnsafe = 2,
  34. kMaxValue = kUnsafe,
  35. };
  36. static PopupTracker* CreateForWebContents(content::WebContents* contents,
  37. content::WebContents* opener,
  38. WindowOpenDisposition disposition);
  39. PopupTracker(const PopupTracker&) = delete;
  40. PopupTracker& operator=(const PopupTracker&) = delete;
  41. ~PopupTracker() override;
  42. void set_is_trusted(bool is_trusted) { is_trusted_ = is_trusted; }
  43. bool has_first_load_visible_time_for_testing() const {
  44. return first_load_visible_time_.has_value();
  45. }
  46. private:
  47. friend class content::WebContentsUserData<PopupTracker>;
  48. PopupTracker(content::WebContents* contents,
  49. content::WebContents* opener,
  50. WindowOpenDisposition disposition);
  51. // content::WebContentsObserver:
  52. void WebContentsDestroyed() override;
  53. void DidFinishNavigation(
  54. content::NavigationHandle* navigation_handle) override;
  55. void OnVisibilityChanged(content::Visibility visibility) override;
  56. void DidGetUserInteraction(const blink::WebInputEvent& event) override;
  57. // subresource_filter::SubresourceFilterObserver:
  58. void OnSafeBrowsingChecksComplete(
  59. content::NavigationHandle* navigation_handle,
  60. const subresource_filter::SubresourceFilterSafeBrowsingClient::
  61. CheckResult& result) override;
  62. void OnSubresourceFilterGoingAway() override;
  63. base::ScopedObservation<subresource_filter::SubresourceFilterObserverManager,
  64. subresource_filter::SubresourceFilterObserver>
  65. scoped_observation_{this};
  66. // Will be unset until the first navigation commits. Will be set to the total
  67. // time the contents was visible at commit time.
  68. absl::optional<base::TimeDelta> first_load_visible_time_start_;
  69. // Will be unset until the second navigation commits. Is the total time the
  70. // contents is visible while the first document is loading (after commit).
  71. absl::optional<base::TimeDelta> first_load_visible_time_;
  72. ui::ScopedVisibilityTracker visibility_tracker_;
  73. // The number of user interactions occurring in this popup tab.
  74. int num_interactions_ = 0;
  75. // The number of user interacitons in a popup tab broken down into
  76. // user activation and gesture scroll begin events.
  77. int num_activation_events_ = 0;
  78. int num_gesture_scroll_begin_events_ = 0;
  79. // Number of redirects taken by the pop-up during navigation.
  80. int num_redirects_ = 0;
  81. bool first_navigation_committed_ = false;
  82. // The id of the web contents that created the popup at the time of creation.
  83. // SourceIds are permanent so it's okay to use at any point so long as it's
  84. // not invalid.
  85. const ukm::SourceId opener_source_id_;
  86. bool is_trusted_ = false;
  87. // Whether the pop-up navigated to a site on the safe browsing list. Set when
  88. // the safe browsing checks complete.
  89. PopupSafeBrowsingStatus safe_browsing_status_ =
  90. PopupSafeBrowsingStatus::kNoValue;
  91. // The window open disposition used when creating the popup.
  92. const WindowOpenDisposition window_open_disposition_;
  93. WEB_CONTENTS_USER_DATA_KEY_DECL();
  94. };
  95. } // namespace blocked_content
  96. #endif // COMPONENTS_BLOCKED_CONTENT_POPUP_TRACKER_H_