OLD | NEW |
(Empty) | |
| 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 |
| 5 #ifndef COMPONENTS_SUBRESOURCE_FILTER_CONTENT_BROWSER_CONTENT_SUBRESOURCE_FILTER
_THROTTLE_MANAGER_H_ |
| 6 #define COMPONENTS_SUBRESOURCE_FILTER_CONTENT_BROWSER_CONTENT_SUBRESOURCE_FILTER
_THROTTLE_MANAGER_H_ |
| 7 |
| 8 #include <memory> |
| 9 #include <unordered_map> |
| 10 #include <vector> |
| 11 |
| 12 #include "base/macros.h" |
| 13 #include "base/memory/weak_ptr.h" |
| 14 #include "components/subresource_filter/content/browser/verified_ruleset_dealer.
h" |
| 15 #include "components/subresource_filter/core/common/activation_state.h" |
| 16 #include "content/public/browser/web_contents_observer.h" |
| 17 |
| 18 namespace content { |
| 19 class NavigationHandle; |
| 20 class NavigationThrottle; |
| 21 class RenderFrameHost; |
| 22 } // namespace content |
| 23 |
| 24 namespace subresource_filter { |
| 25 |
| 26 class AsyncDocumentSubresourceFilter; |
| 27 class ActivationStateComputingNavigationThrottle; |
| 28 class SubframeNavigationFilteringThrottle; |
| 29 |
| 30 // The ContentSubresourceFilterThrottleManager manages NavigationThrottles in |
| 31 // order to calculate frame activation states and subframe navigation filtering, |
| 32 // within a given WebContents. It contains a mapping of all activated |
| 33 // RenderFrameHosts, along with their associated DocumentSubresourceFilters. |
| 34 // |
| 35 // The class is designed to be used by a Delegate, which shares lifetime with |
| 36 // this class (aka the typical lifetime of a WebContentsObserver). The delegate |
| 37 // will be notified of the first disallowed subresource load for a top level |
| 38 // navgation, and has veto power for frame activation. |
| 39 class ContentSubresourceFilterThrottleManager |
| 40 : public content::WebContentsObserver { |
| 41 public: |
| 42 // It is expected that the Delegate outlives |this|, and manages the lifetime |
| 43 // of this class. |
| 44 class Delegate { |
| 45 public: |
| 46 // The embedder may be interested in displaying UI to the user when the |
| 47 // first load is disallowed for a given page load. |
| 48 virtual void OnFirstSubresourceLoadDisallowed() {} |
| 49 |
| 50 // Let the delegate have the last word when it comes to activation. It might |
| 51 // have a specific whitelist. |
| 52 virtual bool ShouldSuppressActivation( |
| 53 content::NavigationHandle* navigation_handle); |
| 54 }; |
| 55 |
| 56 ContentSubresourceFilterThrottleManager( |
| 57 Delegate* delegate, |
| 58 VerifiedRulesetDealer::Handle* dealer_handle, |
| 59 content::WebContents* web_contents); |
| 60 ~ContentSubresourceFilterThrottleManager() override; |
| 61 |
| 62 // Sets the desired page-level |activation_state| for the currently ongoing |
| 63 // page load, identified by its main-frame |navigation_handle|. To be called |
| 64 // by the embedder at the latest in the WillProcessResponse stage from a |
| 65 // NavigationThrottle that was registered before the throttles created by this |
| 66 // manager in MaybeAppendNavigationThrottles(). If this method is not called |
| 67 // for a main-frame navigation, the default behavior is no activation for that |
| 68 // page load. |
| 69 void NotifyPageActivationComputed( |
| 70 content::NavigationHandle* navigation_handle, |
| 71 const ActivationState& activation_state); |
| 72 |
| 73 // This method inspects |navigation_handle| and attaches navigation throttles |
| 74 // appropriately, based on the current state of frame activation. |
| 75 // |
| 76 // 1. Subframe navigation filtering throttles are appended if the parent |
| 77 // frame is activated. |
| 78 // 2. Activation state computing throttles are appended if either the |
| 79 // navigation is a main frame navigation, or if the parent frame is activated. |
| 80 // |
| 81 // Note that there is currently no constraints on the ordering of throttles. |
| 82 void MaybeAppendNavigationThrottles( |
| 83 content::NavigationHandle* navigation_handle, |
| 84 std::vector<std::unique_ptr<content::NavigationThrottle>>* throttles); |
| 85 |
| 86 VerifiedRuleset::Handle* ruleset_handle_for_testing() { |
| 87 return ruleset_handle_.get(); |
| 88 } |
| 89 |
| 90 protected: |
| 91 // content::WebContentsObserver: |
| 92 void RenderFrameDeleted(content::RenderFrameHost* frame_host) override; |
| 93 void ReadyToCommitNavigation( |
| 94 content::NavigationHandle* navigation_handle) override; |
| 95 void DidFinishNavigation( |
| 96 content::NavigationHandle* navigation_handle) override; |
| 97 |
| 98 private: |
| 99 std::unique_ptr<SubframeNavigationFilteringThrottle> |
| 100 MaybeCreateSubframeNavigationFilteringThrottle( |
| 101 content::NavigationHandle* navigation_handle); |
| 102 std::unique_ptr<ActivationStateComputingNavigationThrottle> |
| 103 MaybeCreateActivationStateComputingThrottle( |
| 104 content::NavigationHandle* navigation_handle); |
| 105 |
| 106 // Will return nullptr if the parent frame of this navigation is not |
| 107 // activated (and therefore has no subresource filter). |
| 108 AsyncDocumentSubresourceFilter* GetParentFrameFilter( |
| 109 content::NavigationHandle* child_frame_navigation); |
| 110 |
| 111 // Calls OnFirstSubresourceLoadDisallowed on the Delegate at most once per |
| 112 // committed, non-same-page navigation in the main frame. |
| 113 // TODO(csharrison): Ensure IPCs from the renderer go through this path when |
| 114 // they disallow subresource loads. |
| 115 void MaybeCallFirstDisallowedLoad(); |
| 116 |
| 117 VerifiedRuleset::Handle* EnsureRulesetHandle(); |
| 118 void DestroyRulesetHandleIfNoLongerUsed(); |
| 119 |
| 120 // For each RenderFrameHost where the last committed load has subresource |
| 121 // filtering activated, owns the corresponding AsyncDocumentSubresourceFilter. |
| 122 std::unordered_map<content::RenderFrameHost*, |
| 123 std::unique_ptr<AsyncDocumentSubresourceFilter>> |
| 124 activated_frame_hosts_; |
| 125 |
| 126 // For each ongoing navigation that requires activation state computation, |
| 127 // keeps track of the throttle that is carrying out that computation, so that |
| 128 // the result can be retrieved when the navigation is ready to commit. |
| 129 std::unordered_map<content::NavigationHandle*, |
| 130 ActivationStateComputingNavigationThrottle*> |
| 131 ongoing_activation_throttles_; |
| 132 |
| 133 // Lazily instantiated in EnsureRulesetHandle when the first page level |
| 134 // activation is triggered. Will go away when there are no more activated |
| 135 // RenderFrameHosts (i.e. activated_frame_hosts_ is empty). |
| 136 std::unique_ptr<VerifiedRuleset::Handle> ruleset_handle_; |
| 137 |
| 138 // True if the current committed main frame load in this WebContents has |
| 139 // notified the delegate that a subresource was disallowed. The callback |
| 140 // should only be called at most once per main frame load. |
| 141 bool current_committed_load_has_notified_disallowed_load_ = false; |
| 142 |
| 143 // These members outlive this class. |
| 144 VerifiedRulesetDealer::Handle* dealer_handle_; |
| 145 Delegate* delegate_; |
| 146 |
| 147 base::WeakPtrFactory<ContentSubresourceFilterThrottleManager> |
| 148 weak_ptr_factory_; |
| 149 |
| 150 DISALLOW_COPY_AND_ASSIGN(ContentSubresourceFilterThrottleManager); |
| 151 }; |
| 152 |
| 153 } // namespace subresource_filter |
| 154 |
| 155 #endif // COMPONENTS_SUBRESOURCE_FILTER_CONTENT_BROWSER_CONTENT_SUBRESOURCE_FIL
TER_THROTTLE_MANAGER_H_ |
OLD | NEW |