popup_opener_tab_helper.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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_OPENER_TAB_HELPER_H_
  5. #define COMPONENTS_BLOCKED_CONTENT_POPUP_OPENER_TAB_HELPER_H_
  6. #include <memory>
  7. #include "base/memory/raw_ptr.h"
  8. #include "base/time/tick_clock.h"
  9. #include "base/time/time.h"
  10. #include "components/content_settings/core/browser/host_content_settings_map.h"
  11. #include "content/public/browser/web_contents_observer.h"
  12. #include "content/public/browser/web_contents_user_data.h"
  13. #include "third_party/abseil-cpp/absl/types/optional.h"
  14. namespace base {
  15. class TickClock;
  16. }
  17. namespace content {
  18. class WebContents;
  19. }
  20. namespace ui {
  21. class ScopedVisibilityTracker;
  22. }
  23. namespace blocked_content {
  24. class PopupTracker;
  25. // This class tracks WebContents for the purpose of logging metrics related to
  26. // popup openers.
  27. class PopupOpenerTabHelper
  28. : public content::WebContentsObserver,
  29. public content::WebContentsUserData<PopupOpenerTabHelper> {
  30. public:
  31. PopupOpenerTabHelper(const PopupOpenerTabHelper&) = delete;
  32. PopupOpenerTabHelper& operator=(const PopupOpenerTabHelper&) = delete;
  33. ~PopupOpenerTabHelper() override;
  34. void OnOpenedPopup(PopupTracker* popup_tracker);
  35. void OnDidTabUnder();
  36. bool has_opened_popup_since_last_user_gesture() const {
  37. return has_opened_popup_since_last_user_gesture_;
  38. }
  39. bool did_tab_under() const {
  40. return visible_time_before_tab_under_.has_value();
  41. }
  42. private:
  43. friend class content::WebContentsUserData<PopupOpenerTabHelper>;
  44. // |tick_clock| overrides the internal time for testing. This doesn't take
  45. // ownership of |tick_clock| or |settings_map|, and they both must outlive the
  46. // PopupOpenerTabHelper instance.
  47. PopupOpenerTabHelper(content::WebContents* web_contents,
  48. const base::TickClock* tick_clock,
  49. HostContentSettingsMap* settings_map);
  50. // content::WebContentsObserver:
  51. void OnVisibilityChanged(content::Visibility visibility) override;
  52. void DidStartNavigation(
  53. content::NavigationHandle* navigation_handle) override;
  54. void DidGetUserInteraction(const blink::WebInputEvent& event) override;
  55. // Logs user popup content settings if the last committed URL is valid and
  56. // we have not recorded the settings for the opener id of the helper's
  57. // web contents at the time the function is called.
  58. void MaybeLogPagePopupContentSettings();
  59. // Visible time for this tab until a tab-under is detected. At which point it
  60. // gets the visible time from the |visibility_tracker_|. Will be unset until a
  61. // tab-under is detected.
  62. absl::optional<base::TimeDelta> visible_time_before_tab_under_;
  63. // The clock which is used by the visibility trackers.
  64. raw_ptr<const base::TickClock> tick_clock_;
  65. // Keeps track of the total foreground time for this tab.
  66. std::unique_ptr<ui::ScopedVisibilityTracker> visibility_tracker_;
  67. // Measures the time this WebContents opened a popup.
  68. base::TimeTicks last_popup_open_time_;
  69. bool has_opened_popup_since_last_user_gesture_ = false;
  70. // The last source id used for logging Popup_Page.
  71. ukm::SourceId last_opener_source_id_ = ukm::kInvalidSourceId;
  72. // The settings map for the web contents this object is associated with.
  73. raw_ptr<HostContentSettingsMap> settings_map_ = nullptr;
  74. WEB_CONTENTS_USER_DATA_KEY_DECL();
  75. };
  76. } // namespace blocked_content
  77. #endif // COMPONENTS_BLOCKED_CONTENT_POPUP_OPENER_TAB_HELPER_H_