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 #include "components/subresource_filter/content/browser/content_subresource_filt er_throttle_manager.h" | |
6 | |
7 #include "base/bind.h" | |
8 #include "base/memory/ptr_util.h" | |
9 #include "components/subresource_filter/content/browser/activation_state_computi ng_navigation_throttle.h" | |
10 #include "components/subresource_filter/content/browser/async_document_subresour ce_filter.h" | |
11 #include "components/subresource_filter/content/browser/subframe_navigation_filt ering_throttle.h" | |
12 #include "content/public/browser/navigation_handle.h" | |
13 #include "content/public/browser/render_frame_host.h" | |
14 #include "content/public/browser/web_contents.h" | |
15 | |
16 namespace subresource_filter { | |
17 | |
18 FrameActivationInfo::FrameActivationInfo() {} | |
19 FrameActivationInfo::~FrameActivationInfo() {} | |
20 | |
21 bool ContentSubresourceFilterThrottleManager::Delegate::ShouldVetoActivation( | |
22 content::NavigationHandle* navigation_handle) { | |
23 return false; | |
24 } | |
25 | |
26 ContentSubresourceFilterThrottleManager:: | |
27 ContentSubresourceFilterThrottleManager( | |
28 Delegate* delegate, | |
29 VerifiedRulesetDealer::Handle* dealer_handle, | |
30 content::WebContents* web_contents) | |
31 : content::WebContentsObserver(web_contents), | |
32 dealer_handle_(dealer_handle), | |
33 delegate_(delegate) {} | |
34 | |
35 ContentSubresourceFilterThrottleManager:: | |
36 ~ContentSubresourceFilterThrottleManager() {} | |
37 | |
38 void ContentSubresourceFilterThrottleManager::OnPageStateActivationComputed( | |
engedy
2017/02/17 20:44:44
nit: For consistency, what do you think about call
Charlie Harrison
2017/02/17 22:12:31
Sure, renamed to NotifyPageActivationComputed.
| |
39 content::NavigationHandle* activated_navigation, | |
40 const ActivationState& state) { | |
engedy
2017/02/17 20:44:44
nit: %s/ state/ activation_state/g
Charlie Harrison
2017/02/17 22:12:31
Done, here and throughout the file.
| |
41 DCHECK(activated_navigation->IsInMainFrame()); | |
42 DCHECK(!activated_navigation->HasCommitted()); | |
43 auto it = ongoing_activation_throttles_.find(activated_navigation); | |
44 if (it != ongoing_activation_throttles_.end()) { | |
45 it->second->NotifyPageActivationWithRuleset( | |
46 EnsureRulesetHandle(), state, | |
47 base::Bind(&ContentSubresourceFilterThrottleManager::Delegate:: | |
48 OnFirstSubresourceLoadDisallowed, | |
engedy
2017/02/17 20:44:44
Note that, later, we will need to clean up this si
Charlie Harrison
2017/02/17 22:12:31
I think the coalescing should be done in this clas
engedy
2017/02/20 15:58:13
Sounds good, let's do that in a follow-up CL.
Chr
| |
49 delegate_->AsWeakPtr())); | |
50 } | |
51 } | |
52 | |
53 void ContentSubresourceFilterThrottleManager::RenderFrameDeleted( | |
54 content::RenderFrameHost* frame_host) { | |
55 activated_frame_hosts_.erase(frame_host); | |
56 if (activated_frame_hosts_.size() == 0) | |
57 ruleset_handle_.reset(); | |
58 } | |
59 | |
60 // Pull the AsyncDocumentSubresourceFilter and its associated ActivationState | |
61 // out of the activation state computing throttle. Store it for later filtering | |
62 // of subframe navigations. | |
63 void ContentSubresourceFilterThrottleManager::ReadyToCommitNavigation( | |
64 content::NavigationHandle* navigation_handle) { | |
65 content::RenderFrameHost* frame_host = | |
66 navigation_handle->GetRenderFrameHost(); | |
67 // Make sure that if the committed navigation is in a RenderFrameHost that is | |
68 // already activated, the activation state is reset. This is important in the | |
69 // case where this navigation does *not* trigger activation. | |
70 activated_frame_hosts_.erase(frame_host); | |
engedy
2017/02/17 20:44:44
nit: It's a bit hard to argue about |ruleset_handl
Charlie Harrison
2017/02/17 22:12:31
You're right, this is hard to reason about. Essent
| |
71 | |
72 auto throttle = ongoing_activation_throttles_.find(navigation_handle); | |
73 if (throttle == ongoing_activation_throttles_.end()) | |
74 return; | |
75 | |
76 const ActivationState& state = throttle->second->GetActivationState(); | |
77 if (state.activation_level == ActivationLevel::DISABLED || | |
78 delegate_->ShouldVetoActivation(navigation_handle)) | |
79 return; | |
80 | |
81 auto info = base::MakeUnique<FrameActivationInfo>(); | |
82 info->subresource_filter = throttle->second->ReleaseFilter(); | |
83 info->activation_state = state; | |
84 DCHECK(info->subresource_filter); | |
85 activated_frame_hosts_[navigation_handle->GetRenderFrameHost()] = | |
engedy
2017/02/17 20:44:44
nit: activated_frame_hosts_[frame_host]
Charlie Harrison
2017/02/17 22:12:31
Done.
| |
86 std::move(info); | |
87 // TODO(csharrison): Send an IPC to the renderer. | |
88 } | |
89 | |
90 void ContentSubresourceFilterThrottleManager::DidFinishNavigation( | |
91 content::NavigationHandle* navigation_handle) { | |
92 ongoing_activation_throttles_.erase(navigation_handle); | |
93 } | |
94 | |
95 void ContentSubresourceFilterThrottleManager::MaybeInsertNavigationThrottles( | |
engedy
2017/02/17 20:44:44
nit: MaybeAppendNavigationThrottles
Charlie Harrison
2017/02/17 22:12:31
Done.
| |
96 content::NavigationHandle* navigation_handle, | |
97 std::vector<std::unique_ptr<content::NavigationThrottle>>* throttles) { | |
98 if (std::unique_ptr<content::NavigationThrottle> filtering_throttle = | |
99 MaybeCreateSubframeNavigationFilteringThrottle(navigation_handle)) { | |
100 throttles->push_back(std::move(filtering_throttle)); | |
101 } | |
102 if (std::unique_ptr<content::NavigationThrottle> activation_throttle = | |
103 MaybeCreateActivationStateComputingThrottle(navigation_handle)) { | |
104 throttles->push_back(std::move(activation_throttle)); | |
105 } | |
106 } | |
107 | |
108 std::unique_ptr<content::NavigationThrottle> | |
109 ContentSubresourceFilterThrottleManager:: | |
110 MaybeCreateSubframeNavigationFilteringThrottle( | |
111 content::NavigationHandle* navigation_handle) { | |
112 if (navigation_handle->IsInMainFrame() || navigation_handle->IsSamePage()) | |
engedy
2017/02/17 20:44:44
Could you please verify what happens here for navi
Charlie Harrison
2017/02/17 22:12:31
Yes I think same-page navigations and all synchron
engedy
2017/02/20 15:58:13
Awesome, thanks for checking!
engedy
2017/03/10 17:36:20
To quote a popular meme:
-- Tell me you turned th
Charlie Harrison
2017/03/14 23:18:31
LOL. Done.
| |
113 return nullptr; | |
114 FrameActivationInfo* info = | |
115 GetFrameActivationInfoForChildNavigation(navigation_handle); | |
116 return info ? base::MakeUnique<SubframeNavigationFilteringThrottle>( | |
117 navigation_handle, info->subresource_filter.get()) | |
118 : nullptr; | |
119 } | |
120 | |
121 std::unique_ptr<content::NavigationThrottle> | |
122 ContentSubresourceFilterThrottleManager:: | |
123 MaybeCreateActivationStateComputingThrottle( | |
124 content::NavigationHandle* navigation_handle) { | |
125 if (navigation_handle->IsSamePage()) | |
126 return nullptr; | |
127 | |
128 // Main frames: create unconditionally. | |
129 if (navigation_handle->IsInMainFrame()) | |
130 return ActivationStateComputingNavigationThrottle::CreateForMainFrame( | |
131 navigation_handle); | |
132 | |
133 // Subframes: create only for frames with activated parents. | |
134 FrameActivationInfo* info = | |
135 GetFrameActivationInfoForChildNavigation(navigation_handle); | |
136 if (!info) | |
137 return nullptr; | |
138 DCHECK(ruleset_handle_); | |
139 return ActivationStateComputingNavigationThrottle::CreateForSubframe( | |
140 navigation_handle, ruleset_handle_.get(), info->activation_state, | |
141 base::Bind(&Delegate::OnFirstSubresourceLoadDisallowed, | |
142 delegate_->AsWeakPtr())); | |
143 } | |
144 | |
145 FrameActivationInfo* ContentSubresourceFilterThrottleManager:: | |
146 GetFrameActivationInfoForChildNavigation( | |
147 content::NavigationHandle* child_frame_navigation) { | |
148 DCHECK(!child_frame_navigation->IsInMainFrame()); | |
149 content::RenderFrameHost* parent = web_contents()->FindFrameByFrameTreeNodeId( | |
150 child_frame_navigation->GetParentFrameTreeNodeId()); | |
151 DCHECK(parent); | |
152 auto it = activated_frame_hosts_.find(parent); | |
153 return it == activated_frame_hosts_.end() ? nullptr : it->second.get(); | |
154 } | |
155 | |
156 VerifiedRuleset::Handle* | |
157 ContentSubresourceFilterThrottleManager::EnsureRulesetHandle() { | |
158 if (!ruleset_handle_) | |
159 ruleset_handle_ = base::MakeUnique<VerifiedRuleset::Handle>(dealer_handle_); | |
160 return ruleset_handle_.get(); | |
161 } | |
162 | |
163 } // namespace subresource_filter | |
OLD | NEW |