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