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(Delegate* delegate, | |
28 content::WebContents* web_contents) | |
29 : content::WebContentsObserver(web_contents), delegate_(delegate) {} | |
30 | |
31 ContentSubresourceFilterThrottleManager:: | |
32 ~ContentSubresourceFilterThrottleManager() {} | |
33 | |
34 std::unique_ptr<content::NavigationThrottle> | |
35 ContentSubresourceFilterThrottleManager:: | |
engedy
2017/02/17 15:34:27
Should we make these methods private, and expose o
Charlie Harrison
2017/02/17 18:04:35
Yeah I thought of this before and was nervous to e
| |
36 MaybeCreateSubframeNavigationFilteringThrottle( | |
37 content::NavigationHandle* navigation_handle) { | |
38 if (navigation_handle->IsInMainFrame() || navigation_handle->IsSamePage()) | |
39 return nullptr; | |
40 FrameActivationInfo* info = | |
41 GetFrameActivationInfoForChildNavigation(navigation_handle); | |
42 return info ? base::MakeUnique<SubframeNavigationFilteringThrottle>( | |
43 navigation_handle, info->subresource_filter.get()) | |
44 : nullptr; | |
45 } | |
46 | |
47 std::unique_ptr<content::NavigationThrottle> | |
48 ContentSubresourceFilterThrottleManager:: | |
49 MaybeCreateActivationStateComputingThrottle( | |
50 content::NavigationHandle* navigation_handle) { | |
51 if (navigation_handle->IsSamePage()) | |
52 return nullptr; | |
53 | |
54 // Main frames: create unconditionally. | |
55 if (navigation_handle->IsInMainFrame()) | |
56 return ActivationStateComputingNavigationThrottle::CreateForMainFrame( | |
57 navigation_handle); | |
58 | |
59 // Subframes: create only for frames with activated parents. | |
60 FrameActivationInfo* info = | |
61 GetFrameActivationInfoForChildNavigation(navigation_handle); | |
62 if (!info) | |
63 return nullptr; | |
64 DCHECK(ruleset_handle_); | |
65 return ActivationStateComputingNavigationThrottle::CreateForSubframe( | |
66 navigation_handle, ruleset_handle_.get(), info->activation_state, | |
67 base::Bind(&Delegate::OnFirstSubresourceLoadDisallowed, | |
68 delegate_->AsWeakPtr())); | |
69 } | |
70 | |
71 void ContentSubresourceFilterThrottleManager::OnPageStateActivationComputed( | |
72 content::NavigationHandle* activated_navigation, | |
73 const ActivationState& state) { | |
74 DCHECK(activated_navigation->IsInMainFrame()); | |
75 DCHECK(!activated_navigation->HasCommitted()); | |
76 auto it = ongoing_activation_throttles_.find(activated_navigation); | |
77 if (it != ongoing_activation_throttles_.end()) { | |
78 it->second->NotifyPageActivationWithRuleset( | |
79 EnsureRulesetHandle(), state, | |
80 base::Bind(&ContentSubresourceFilterThrottleManager::Delegate:: | |
81 OnFirstSubresourceLoadDisallowed, | |
82 delegate_->AsWeakPtr())); | |
83 } | |
84 } | |
85 | |
86 void ContentSubresourceFilterThrottleManager::RenderFrameDeleted( | |
87 content::RenderFrameHost* frame_host) { | |
88 activated_frame_hosts_.erase(frame_host); | |
89 if (activated_frame_hosts_.size() == 0) | |
90 ruleset_handle_.reset(); | |
91 } | |
92 | |
93 // Pull the AsyncDocumentSubresourceFilter and its associated ActivationState | |
94 // out of the activation state computing throttle. Store it for later filtering | |
95 // of subframe navigations. | |
96 void ContentSubresourceFilterThrottleManager::ReadyToCommitNavigation( | |
97 content::NavigationHandle* navigation_handle) { | |
98 content::RenderFrameHost* frame_host = | |
99 navigation_handle->GetRenderFrameHost(); | |
100 // Make sure that if the committed navigation is in a RenderFrameHost that is | |
101 // already activated, the activation state is reset. This is important in the | |
102 // case where this navigation does *not* trigger activation. | |
103 activated_frame_hosts_.erase(frame_host); | |
104 | |
105 auto throttle = ongoing_activation_throttles_.find(navigation_handle); | |
106 if (throttle == ongoing_activation_throttles_.end()) | |
107 return; | |
108 | |
109 const ActivationState& state = throttle->second->GetActivationState(); | |
110 if (state.activation_level == ActivationLevel::DISABLED || | |
111 delegate_->ShouldVetoActivation(navigation_handle)) | |
112 return; | |
113 | |
114 auto info = base::MakeUnique<FrameActivationInfo>(); | |
115 info->subresource_filter = throttle->second->ReleaseFilter(); | |
116 info->activation_state = state; | |
117 DCHECK(info->subresource_filter); | |
118 activated_frame_hosts_[navigation_handle->GetRenderFrameHost()] = | |
119 std::move(info); | |
120 delegate_->OnActivationStateComputedForCommittingLoad(navigation_handle, | |
121 state); | |
122 } | |
123 | |
124 void ContentSubresourceFilterThrottleManager::DidFinishNavigation( | |
125 content::NavigationHandle* navigation_handle) { | |
126 ongoing_activation_throttles_.erase(navigation_handle); | |
127 } | |
128 | |
129 FrameActivationInfo* ContentSubresourceFilterThrottleManager:: | |
130 GetFrameActivationInfoForChildNavigation( | |
131 content::NavigationHandle* child_frame_navigation) { | |
132 DCHECK(!child_frame_navigation->IsInMainFrame()); | |
133 content::RenderFrameHost* parent = web_contents()->FindFrameByFrameTreeNodeId( | |
134 child_frame_navigation->GetParentFrameTreeNodeId()); | |
135 DCHECK(parent); | |
136 auto it = activated_frame_hosts_.find(parent); | |
137 return it == activated_frame_hosts_.end() ? nullptr : it->second.get(); | |
138 } | |
139 | |
140 VerifiedRuleset::Handle* | |
141 ContentSubresourceFilterThrottleManager::EnsureRulesetHandle() { | |
142 if (!ruleset_handle_) { | |
143 ruleset_handle_ = base::MakeUnique<VerifiedRuleset::Handle>( | |
144 delegate_->GetRulesetDealerHandle()); | |
145 } | |
146 return ruleset_handle_.get(); | |
147 } | |
148 | |
149 } // namespace subresource_filter | |
OLD | NEW |