PageRenderTime 24ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/components/subresource_filter/content/browser/content_subresource_filter_web_contents_helper.h

https://github.com/chromium/chromium
C Header | 214 lines | 124 code | 36 blank | 54 comment | 0 complexity | 8c90493e4e74e9243fb0b3137f8e5fa2 MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, Apache-2.0, BSD-3-Clause
  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_SUBRESOURCE_FILTER_CONTENT_BROWSER_CONTENT_SUBRESOURCE_FILTER_WEB_CONTENTS_HELPER_H_
  5. #define COMPONENTS_SUBRESOURCE_FILTER_CONTENT_BROWSER_CONTENT_SUBRESOURCE_FILTER_WEB_CONTENTS_HELPER_H_
  6. #include <map>
  7. #include <set>
  8. #include "base/containers/flat_set.h"
  9. #include "base/memory/raw_ptr.h"
  10. #include "base/memory/scoped_refptr.h"
  11. #include "base/scoped_observation.h"
  12. #include "components/crash/core/common/crash_key.h"
  13. #include "components/subresource_filter/content/browser/subresource_filter_observer.h"
  14. #include "components/subresource_filter/content/browser/subresource_filter_observer_manager.h"
  15. #include "components/subresource_filter/content/browser/verified_ruleset_dealer.h"
  16. #include "content/public/browser/web_contents_observer.h"
  17. #include "content/public/browser/web_contents_user_data.h"
  18. namespace content {
  19. class NavigationHandle;
  20. class Page;
  21. class RenderFrameHost;
  22. class WebContents;
  23. } // namespace content
  24. namespace safe_browsing {
  25. class SafeBrowsingDatabaseManager;
  26. } // namespace safe_browsing
  27. namespace subresource_filter {
  28. class ContentSubresourceFilterThrottleManager;
  29. class SubresourceFilterProfileContext;
  30. // TODO(bokan): Temporary to help debug crash in crbug.com/1264667. Tracks
  31. // main frame RFHs and observes navigations/events relating to them, recording
  32. // some potentially interesting data that'll be stored as a crash key at the
  33. // crash site in
  34. // ContentSubresourceFilterWebContentsHelper::DidFinishNavigation.
  35. class DebugCrashWebContentsObserver : public content::WebContentsObserver {
  36. struct CrashKeyData {
  37. CrashKeyData();
  38. ~CrashKeyData();
  39. CrashKeyData(const CrashKeyData& other);
  40. int num_commits = 0;
  41. bool last_nav_had_manager = false;
  42. bool last_nav_manager_moved_to_page = false;
  43. bool entered_bf_cache = false;
  44. bool is_bfcache_restore = false;
  45. base::WeakPtr<content::Page> page_at_last_nav_finish;
  46. base::WeakPtr<content::Page> page_at_bf_cache_entry;
  47. base::WeakPtr<content::Page> page_at_restore;
  48. };
  49. public:
  50. explicit DebugCrashWebContentsObserver(content::WebContents* web_contents);
  51. ~DebugCrashWebContentsObserver() override;
  52. void RenderFrameCreated(content::RenderFrameHost* render_frame_host) override;
  53. void RenderFrameDeleted(content::RenderFrameHost* render_frame_host) override;
  54. void ReadyToCommitNavigation(content::NavigationHandle* handle) override;
  55. // Not an override since it relies on running after the WebContentsHelper's
  56. // DidFinishNavigation.
  57. void DidFinishNonActivatingNavigation(content::NavigationHandle* handle,
  58. content::Page* page);
  59. void RenderFrameHostStateChanged(
  60. content::RenderFrameHost* render_frame_host,
  61. content::RenderFrameHost::LifecycleState old_state,
  62. content::RenderFrameHost::LifecycleState new_state) override;
  63. // Not an override since it relies on running before the crash site in the
  64. // WebContentsHelper's DidFinishNavigation.
  65. void DidFinishPageActivatingNavigation(content::NavigationHandle* handle);
  66. void GetScopedCrashKeyStrings(
  67. content::RenderFrameHost* rfh,
  68. std::vector<std::unique_ptr<crash_reporter::ScopedCrashKeyString>>& keys);
  69. private:
  70. // Tracks useful data for each main frame RFH that's passed through
  71. // ReadyToCommit/DidFinishNavigation.
  72. std::map<content::RenderFrameHost*, CrashKeyData> rfh_crash_data_;
  73. };
  74. // This class manages the lifetime and storage of
  75. // ContentSubresourceFilterThrottleManager instances. This helper is attached
  76. // to each WebContents and listens to navigations to ensure certain Page(s) in
  77. // the WebContents have an associated throttle manager. A throttle manager is
  78. // created for outermost pages and for portal pages. Fenced frames are treated
  79. // as subframes and don't create a throttle manager; they use the throttle
  80. // manager of their embedding page.
  81. //
  82. // This class also listens to events occurring in the WebContents and
  83. // SubresourceFilter and, based on their context, routes the event to the
  84. // throttle manager of the target page.
  85. // TODO(bokan): This seems like a common pattern for a feature to want to
  86. // observe events and track state on a per-Page basis. The WebContentsHelper
  87. // pattern or something like it should be wrapped up into a common and reusable
  88. // //content API. See:
  89. // https://docs.google.com/document/d/1p-IXk8hI5ucWRf5vJEi9K_YvJXsTr8kbvzGrjMcALDE/edit?usp=sharing
  90. class ContentSubresourceFilterWebContentsHelper
  91. : public content::WebContentsUserData<
  92. ContentSubresourceFilterWebContentsHelper>,
  93. public content::WebContentsObserver,
  94. public SubresourceFilterObserver {
  95. public:
  96. static void CreateForWebContents(
  97. content::WebContents* web_contents,
  98. SubresourceFilterProfileContext* profile_context,
  99. scoped_refptr<safe_browsing::SafeBrowsingDatabaseManager>
  100. database_manager,
  101. VerifiedRulesetDealer::Handle* dealer_handle);
  102. // Will get the helper from the given `page`'s WebContents.
  103. static ContentSubresourceFilterWebContentsHelper* FromPage(
  104. content::Page& page);
  105. explicit ContentSubresourceFilterWebContentsHelper(
  106. content::WebContents* web_contents,
  107. SubresourceFilterProfileContext* profile_context,
  108. scoped_refptr<safe_browsing::SafeBrowsingDatabaseManager>
  109. database_manager,
  110. VerifiedRulesetDealer::Handle* dealer_handle);
  111. ~ContentSubresourceFilterWebContentsHelper() override;
  112. WEB_CONTENTS_USER_DATA_KEY_DECL();
  113. // Prefer to use the static methods on
  114. // ContentSubresourceFilterThrottleManager. See comments there.
  115. static ContentSubresourceFilterThrottleManager* GetThrottleManager(
  116. content::NavigationHandle& handle);
  117. static ContentSubresourceFilterThrottleManager* GetThrottleManager(
  118. content::Page& page);
  119. // Sets the SafeBrowsingDatabaseManager instance to use on new throttle
  120. // managers. Note, this will not update the database_manager_ value on
  121. // existing ContentSubresourceFilterThrottleManagers.
  122. void SetDatabaseManagerForTesting(
  123. scoped_refptr<safe_browsing::SafeBrowsingDatabaseManager>
  124. database_manager);
  125. void WillDestroyThrottleManager(
  126. ContentSubresourceFilterThrottleManager* throttle_manager);
  127. protected:
  128. // content::WebContentsObserver:
  129. void RenderFrameDeleted(content::RenderFrameHost* frame_host) override;
  130. void FrameDeleted(int frame_tree_node_id) override;
  131. void DidStartNavigation(
  132. content::NavigationHandle* navigation_handle) override;
  133. void ReadyToCommitNavigation(
  134. content::NavigationHandle* navigation_handle) override;
  135. void DidFinishNavigation(
  136. content::NavigationHandle* navigation_handle) override;
  137. void DidFinishLoad(content::RenderFrameHost* render_frame_host,
  138. const GURL& validated_url) override;
  139. // SubresourceFilterObserver:
  140. void OnSubresourceFilterGoingAway() override;
  141. void OnPageActivationComputed(
  142. content::NavigationHandle* navigation_handle,
  143. const mojom::ActivationState& activation_state) override;
  144. void OnSubframeNavigationEvaluated(
  145. content::NavigationHandle* navigation_handle,
  146. LoadPolicy load_policy) override;
  147. private:
  148. DebugCrashWebContentsObserver debug_crash_observer_;
  149. raw_ptr<SubresourceFilterProfileContext> profile_context_;
  150. scoped_refptr<safe_browsing::SafeBrowsingDatabaseManager> database_manager_;
  151. raw_ptr<VerifiedRulesetDealer::Handle> dealer_handle_;
  152. // Set of frames across all pages in this WebContents that have had at least
  153. // one committed or aborted navigation. Keyed by FrameTreeNode ID.
  154. // TODO(bokan): Make this a strongly typed ID.
  155. std::set<int> navigated_frames_;
  156. base::ScopedObservation<SubresourceFilterObserverManager,
  157. SubresourceFilterObserver>
  158. scoped_observation_{this};
  159. // Keep track of all active throttle managers. Unowned as a throttle manager
  160. // will notify this class when it's destroyed so we can remove it from this
  161. // set.
  162. base::flat_set<ContentSubresourceFilterThrottleManager*> throttle_managers_;
  163. };
  164. // Returns true if the navigation is happening in the main frame of a page
  165. // considered a subresource filter root (i.e. one that may create a new
  166. // ThrottleManager). These navigations are not themselves able to be filtered
  167. // by the subresource filter.
  168. bool IsInSubresourceFilterRoot(content::NavigationHandle* navigation_handle);
  169. // Same as above but for RenderFrameHosts, returns true if the given
  170. // RenderFrameHost is a subresource filter root.
  171. bool IsSubresourceFilterRoot(content::RenderFrameHost* rfh);
  172. // Gets the closest ancestor Page which is a subresource filter root, i.e. one
  173. // for which we have created a throttle manager. Note: This crosses the fenced
  174. // frame boundary (as they are considered a subresource filter child), but does
  175. // not cross a portal boundary (which is a subresource filter root).
  176. content::Page& GetSubresourceFilterRootPage(content::RenderFrameHost* rfh);
  177. } // namespace subresource_filter
  178. #endif // COMPONENTS_SUBRESOURCE_FILTER_CONTENT_BROWSER_CONTENT_SUBRESOURCE_FILTER_WEB_CONTENTS_HELPER_H_