breadcrumb_manager_tab_helper.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. // Copyright 2021 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_BREADCRUMBS_CORE_BREADCRUMB_MANAGER_TAB_HELPER_H_
  5. #define COMPONENTS_BREADCRUMBS_CORE_BREADCRUMB_MANAGER_TAB_HELPER_H_
  6. #include <string>
  7. #include "base/memory/raw_ptr.h"
  8. #include "base/scoped_observation.h"
  9. #include "components/infobars/core/infobar_manager.h"
  10. #include "ui/base/page_transition_types.h"
  11. namespace breadcrumbs {
  12. // Name of DidStartNavigation event (see
  13. // WebStateObserver/WebContentsObserver::DidStartNavigation).
  14. extern const char kBreadcrumbDidStartNavigation[];
  15. // Name of DidStartNavigation event (see
  16. // WebStateObserver/WebContentsObserver::DidFinishNavigation).
  17. extern const char kBreadcrumbDidFinishNavigation[];
  18. // Name of PageLoaded event (see WebStateObserver::PageLoaded and
  19. // WebContentsObserver::DidFinishLoad).
  20. extern const char kBreadcrumbPageLoaded[];
  21. // Name of DidChangeVisibleSecurityState event
  22. // (see WebStateObserver/WebContentsObserver::DidChangeVisibleSecurityState).
  23. extern const char kBreadcrumbDidChangeVisibleSecurityState[];
  24. // Name of OnInfoBarAdded event
  25. // (see infobars::InfoBarManager::Observer::OnInfoBarAdded).
  26. extern const char kBreadcrumbInfobarAdded[];
  27. // Name of OnInfoBarRemoved event
  28. // (see infobars::InfoBarManager::Observer::OnInfoBarRemoved).
  29. extern const char kBreadcrumbInfobarRemoved[];
  30. // Name of OnInfoBarReplaced event
  31. // (see infobars::InfoBarManager::Observer::OnInfoBarReplaced).
  32. extern const char kBreadcrumbInfobarReplaced[];
  33. // Name of Scroll event, logged when web contents scroll view finishes
  34. // scrolling.
  35. extern const char kBreadcrumbScroll[];
  36. // Name of Zoom event, logged when web contents scroll view finishes zooming.
  37. extern const char kBreadcrumbZoom[];
  38. // Constants below represent metadata for breadcrumb events.
  39. // Appended to |kBreadcrumbDidChangeVisibleSecurityState| event if page has bad
  40. // SSL cert.
  41. extern const char kBreadcrumbAuthenticationBroken[];
  42. // Appended to |kBreadcrumbDidFinishNavigation| event if
  43. // navigation is a download.
  44. extern const char kBreadcrumbDownload[];
  45. // Appended to |kBreadcrumbInfobarRemoved| if infobar removal is not animated.
  46. extern const char kBreadcrumbInfobarNotAnimated[];
  47. // Appended to |kBreadcrumbDidChangeVisibleSecurityState| event if page has
  48. // passive mixed content (f.e. an http served image on https served page).
  49. extern const char kBreadcrumbMixedContent[];
  50. // Appended to |kBreadcrumbPageLoaded| event if page load has
  51. // failed.
  52. extern const char kBreadcrumbPageLoadFailure[];
  53. // Appended to |kBreadcrumbDidStartNavigation| and
  54. // |kBreadcrumbPageLoaded| event if the navigation url was Chrome's New Tab
  55. // Page.
  56. extern const char kBreadcrumbNtpNavigation[];
  57. // Appended to |kBreadcrumbDidStartNavigation| and
  58. // |kBreadcrumbPageLoaded| events if the navigation url had google.com host.
  59. // Users tend to search and then navigate back and forth between search results
  60. // page and landing page. And these back-forward navigations can cause crashes.
  61. extern const char kBreadcrumbGoogleNavigation[];
  62. // Appended to |kBreadcrumbPageLoaded| event if content is PDF.
  63. extern const char kBreadcrumbPdfLoad[];
  64. // Appended to |kBreadcrumbDidStartNavigation| event if navigation
  65. // was a client side redirect (f.e. window.open without user gesture).
  66. extern const char kBreadcrumbRendererInitiatedByScript[];
  67. // Appended to |kBreadcrumbDidStartNavigation| event if navigation
  68. // was renderer-initiated navigation with user gesture (f.e. link tap or
  69. // widow.open with user gesture).
  70. extern const char kBreadcrumbRendererInitiatedByUser[];
  71. // Handles logging of Breadcrumb events associated with a tab (WebContents on
  72. // desktop, WebState on iOS).
  73. class BreadcrumbManagerTabHelper : public infobars::InfoBarManager::Observer {
  74. public:
  75. ~BreadcrumbManagerTabHelper() override;
  76. BreadcrumbManagerTabHelper(const BreadcrumbManagerTabHelper&) = delete;
  77. BreadcrumbManagerTabHelper& operator=(const BreadcrumbManagerTabHelper&) =
  78. delete;
  79. // Returns a unique identifier to be used in breadcrumb event logs to identify
  80. // events associated with the underlying tab. This value is unique across this
  81. // application run only, is NOT persisted, and will change across launches.
  82. int GetUniqueId() const { return unique_id_; }
  83. protected:
  84. explicit BreadcrumbManagerTabHelper(
  85. infobars::InfoBarManager* infobar_manager);
  86. // Logs the breadcrumb event for a started navigation with |navigation_id|.
  87. void LogDidStartNavigation(int64_t navigation_id,
  88. GURL url,
  89. bool is_ntp_url,
  90. bool is_renderer_initiated,
  91. bool has_user_gesture,
  92. ui::PageTransition page_transition);
  93. // Logs the breadcrumb event for a finished navigation with |navigation_id|.
  94. void LogDidFinishNavigation(int64_t navigation_id,
  95. bool is_download,
  96. int error_code);
  97. // Logs the breadcrumb event for loading the page at |url|.
  98. void LogPageLoaded(bool is_ntp_url,
  99. GURL url,
  100. bool page_load_success,
  101. const std::string& contents_mime_type);
  102. // Logs the breadcrumb event for changing the visible security state.
  103. void LogDidChangeVisibleSecurityState(
  104. bool displayed_mixed_content,
  105. bool security_style_authentication_broken);
  106. // Logs the breadcrumb event for a renderer process being terminated.
  107. void LogRenderProcessGone();
  108. // Logs the given |event| for the associated tab.
  109. void LogEvent(const std::string& event);
  110. // Returns true if event that was sequentially emitted |count| times should be
  111. // logged. Some events (e.g., infobars replacements or scrolling) are emitted
  112. // sequentially multiple times. Logging each event will pollute breadcrumbs,
  113. // so this throttling function decides if event should be logged.
  114. bool ShouldLogRepeatedEvent(int count);
  115. private:
  116. // Logs the given |event| for the associated tab by retrieving the breadcrumb
  117. // manager from WebState (iOS) or WebContents (desktop). This should not be
  118. // used directly to log events; use LogEvent() instead.
  119. virtual void PlatformLogEvent(const std::string& event) = 0;
  120. // infobars::InfoBarManager::Observer:
  121. void OnInfoBarAdded(infobars::InfoBar* infobar) override;
  122. void OnInfoBarRemoved(infobars::InfoBar* infobar, bool animate) override;
  123. void OnInfoBarReplaced(infobars::InfoBar* old_infobar,
  124. infobars::InfoBar* new_infobar) override;
  125. void OnManagerShuttingDown(infobars::InfoBarManager* manager) override;
  126. // A unique identifier for this tab helper, used in breadcrumb event logs to
  127. // identify events associated with the underlying tab.
  128. int unique_id_ = -1;
  129. raw_ptr<infobars::InfoBarManager> infobar_manager_ = nullptr;
  130. // A counter which is incremented for each |OnInfoBarReplaced| call. This
  131. // value is reset when any other infobars::InfoBarManager::Observer callback
  132. // is received.
  133. int sequentially_replaced_infobars_ = 0;
  134. // Manages this object as an observer of infobars.
  135. base::ScopedObservation<infobars::InfoBarManager,
  136. infobars::InfoBarManager::Observer>
  137. infobar_observation_{this};
  138. };
  139. } // namespace breadcrumbs
  140. #endif // COMPONENTS_BREADCRUMBS_CORE_BREADCRUMB_MANAGER_TAB_HELPER_H_