Chromium Code Reviews| Index: components/subresource_filter/content/browser/content_subresource_filter_throttle_manager.cc |
| diff --git a/components/subresource_filter/content/browser/content_subresource_filter_throttle_manager.cc b/components/subresource_filter/content/browser/content_subresource_filter_throttle_manager.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..e3394e90a7d92841e2ce85a36e1ef8fefd54cad3 |
| --- /dev/null |
| +++ b/components/subresource_filter/content/browser/content_subresource_filter_throttle_manager.cc |
| @@ -0,0 +1,188 @@ |
| +// Copyright 2017 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "components/subresource_filter/content/browser/content_subresource_filter_throttle_manager.h" |
| + |
| +#include "base/bind.h" |
| +#include "base/logging.h" |
| +#include "base/memory/ptr_util.h" |
| +#include "components/subresource_filter/content/browser/activation_state_computing_navigation_throttle.h" |
| +#include "components/subresource_filter/content/browser/async_document_subresource_filter.h" |
| +#include "components/subresource_filter/content/browser/subframe_navigation_filtering_throttle.h" |
| +#include "content/public/browser/navigation_handle.h" |
| +#include "content/public/browser/render_frame_host.h" |
| +#include "content/public/browser/web_contents.h" |
| + |
| +namespace subresource_filter { |
| + |
| +bool ContentSubresourceFilterThrottleManager::Delegate:: |
| + ShouldSuppressActivation(content::NavigationHandle* navigation_handle) { |
| + return false; |
| +} |
| + |
| +ContentSubresourceFilterThrottleManager:: |
| + ContentSubresourceFilterThrottleManager( |
| + Delegate* delegate, |
| + VerifiedRulesetDealer::Handle* dealer_handle, |
| + content::WebContents* web_contents) |
| + : content::WebContentsObserver(web_contents), |
| + dealer_handle_(dealer_handle), |
| + delegate_(delegate), |
| + weak_ptr_factory_(this) {} |
| + |
| +ContentSubresourceFilterThrottleManager:: |
| + ~ContentSubresourceFilterThrottleManager() {} |
| + |
| +void ContentSubresourceFilterThrottleManager::NotifyPageActivationComputed( |
| + content::NavigationHandle* navigation_handle, |
| + const ActivationState& activation_state) { |
| + DCHECK(navigation_handle->IsInMainFrame()); |
| + DCHECK(!navigation_handle->HasCommitted()); |
| + auto it = ongoing_activation_throttles_.find(navigation_handle); |
| + if (it != ongoing_activation_throttles_.end()) { |
| + it->second->NotifyPageActivationWithRuleset( |
| + EnsureRulesetHandle(), activation_state, |
| + base::Bind(&ContentSubresourceFilterThrottleManager:: |
| + MaybeCallFirstDisallowedLoad, |
| + weak_ptr_factory_.GetWeakPtr())); |
| + } |
| +} |
| + |
| +void ContentSubresourceFilterThrottleManager::RenderFrameDeleted( |
| + content::RenderFrameHost* frame_host) { |
| + activated_frame_hosts_.erase(frame_host); |
| + DestroyRulesetHandleIfNoLongerUsed(); |
| +} |
| + |
| +// Pull the AsyncDocumentSubresourceFilter and its associated ActivationState |
| +// out of the activation state computing throttle. Store it for later filtering |
| +// of subframe navigations. |
| +void ContentSubresourceFilterThrottleManager::ReadyToCommitNavigation( |
| + content::NavigationHandle* navigation_handle) { |
| + content::RenderFrameHost* frame_host = |
| + navigation_handle->GetRenderFrameHost(); |
| + // Make sure that if the committed navigation is in a RenderFrameHost that is |
|
engedy
2017/03/10 17:36:20
Note that ReadyToCommitNavigation is not invoked f
engedy
2017/03/10 20:50:34
Could you please add a unit test to ensure the rul
Charlie Harrison
2017/03/14 23:18:31
Ah that is very unfortunate. To get around that an
Charlie Harrison
2017/03/14 23:18:31
Added tests for:
- Same site navigations to inacti
Charlie Harrison
2017/03/14 23:18:31
Done.
engedy
2017/03/20 18:58:14
Acknowledged, thanks!
|
| + // already activated, the activation state is reset. This is important in the |
| + // case where this navigation does *not* trigger activation. |
| + activated_frame_hosts_.erase(frame_host); |
| + |
| + auto throttle = ongoing_activation_throttles_.find(navigation_handle); |
| + if (throttle == ongoing_activation_throttles_.end()) { |
| + DestroyRulesetHandleIfNoLongerUsed(); |
| + return; |
| + } |
| + |
| + // Some navigations (like about:blank) do not go through WillProcessResponse, |
| + // and won't receive valid filters or activation states. |
|
engedy
2017/03/10 17:36:20
I am not sure but I think that in all these cases
engedy
2017/03/10 17:55:55
I checked and my previous statement is not correct
Charlie Harrison
2017/03/14 23:18:31
Yeah I noticed this in tests.
|
| + std::unique_ptr<AsyncDocumentSubresourceFilter> filter = |
| + throttle->second->ReleaseFilter(); |
| + if (!filter) |
| + return; |
| + |
| + const ActivationState& activation_state = |
| + throttle->second->GetActivationState(); |
| + DCHECK_NE(ActivationLevel::DISABLED, activation_state.activation_level); |
| + if (delegate_->ShouldSuppressActivation(navigation_handle)) { |
| + DestroyRulesetHandleIfNoLongerUsed(); |
| + return; |
| + } |
| + activated_frame_hosts_[frame_host] = std::move(filter); |
| + // TODO(csharrison): Send an IPC to the renderer. |
| +} |
| + |
| +void ContentSubresourceFilterThrottleManager::DidFinishNavigation( |
| + content::NavigationHandle* navigation_handle) { |
| + ongoing_activation_throttles_.erase(navigation_handle); |
| + if (navigation_handle->IsInMainFrame() && navigation_handle->HasCommitted() && |
| + !navigation_handle->IsSameDocument()) { |
| + current_committed_load_has_notified_disallowed_load_ = false; |
| + } |
| +} |
| + |
| +void ContentSubresourceFilterThrottleManager::MaybeAppendNavigationThrottles( |
| + content::NavigationHandle* navigation_handle, |
| + std::vector<std::unique_ptr<content::NavigationThrottle>>* throttles) { |
| + if (navigation_handle->IsSameDocument()) |
| + return; |
| + if (auto filtering_throttle = |
| + MaybeCreateSubframeNavigationFilteringThrottle(navigation_handle)) { |
| + throttles->push_back(std::move(filtering_throttle)); |
| + } |
| + if (auto activation_throttle = |
| + MaybeCreateActivationStateComputingThrottle(navigation_handle)) { |
| + ongoing_activation_throttles_[navigation_handle] = |
| + activation_throttle.get(); |
| + throttles->push_back(std::move(activation_throttle)); |
| + } |
| +} |
| + |
| +std::unique_ptr<SubframeNavigationFilteringThrottle> |
| +ContentSubresourceFilterThrottleManager:: |
| + MaybeCreateSubframeNavigationFilteringThrottle( |
| + content::NavigationHandle* navigation_handle) { |
| + if (navigation_handle->IsInMainFrame()) |
| + return nullptr; |
| + AsyncDocumentSubresourceFilter* parent_filter = |
| + GetParentFilter(navigation_handle); |
|
engedy
2017/03/10 17:36:20
nit: GetParentFrameFilter
Charlie Harrison
2017/03/14 23:18:31
Done.
|
| + return parent_filter ? base::MakeUnique<SubframeNavigationFilteringThrottle>( |
| + navigation_handle, parent_filter) |
| + : nullptr; |
| +} |
| + |
| +std::unique_ptr<ActivationStateComputingNavigationThrottle> |
| +ContentSubresourceFilterThrottleManager:: |
| + MaybeCreateActivationStateComputingThrottle( |
| + content::NavigationHandle* navigation_handle) { |
| + // Main frames: create unconditionally. |
| + if (navigation_handle->IsInMainFrame()) { |
| + return ActivationStateComputingNavigationThrottle::CreateForMainFrame( |
| + navigation_handle); |
| + } |
| + |
| + // Subframes: create only for frames with activated parents. |
| + AsyncDocumentSubresourceFilter* parent_filter = |
| + GetParentFilter(navigation_handle); |
| + if (!parent_filter) |
| + return nullptr; |
| + DCHECK(ruleset_handle_); |
| + return ActivationStateComputingNavigationThrottle::CreateForSubframe( |
| + navigation_handle, ruleset_handle_.get(), |
| + parent_filter->activation_state(), |
| + base::Bind(&ContentSubresourceFilterThrottleManager:: |
| + MaybeCallFirstDisallowedLoad, |
| + weak_ptr_factory_.GetWeakPtr())); |
| +} |
| + |
| +AsyncDocumentSubresourceFilter* |
| +ContentSubresourceFilterThrottleManager::GetParentFilter( |
| + content::NavigationHandle* child_frame_navigation) { |
| + DCHECK(!child_frame_navigation->IsInMainFrame()); |
| + content::RenderFrameHost* parent = web_contents()->FindFrameByFrameTreeNodeId( |
| + child_frame_navigation->GetParentFrameTreeNodeId()); |
| + DCHECK(parent); |
| + auto it = activated_frame_hosts_.find(parent); |
| + return it == activated_frame_hosts_.end() ? nullptr : it->second.get(); |
| +} |
| + |
| +void ContentSubresourceFilterThrottleManager::MaybeCallFirstDisallowedLoad() { |
| + if (current_committed_load_has_notified_disallowed_load_) |
| + return; |
| + delegate_->OnFirstSubresourceLoadDisallowed(); |
| + current_committed_load_has_notified_disallowed_load_ = true; |
| +} |
| + |
| +VerifiedRuleset::Handle* |
| +ContentSubresourceFilterThrottleManager::EnsureRulesetHandle() { |
| + if (!ruleset_handle_) |
| + ruleset_handle_ = base::MakeUnique<VerifiedRuleset::Handle>(dealer_handle_); |
| + return ruleset_handle_.get(); |
| +} |
| + |
| +void ContentSubresourceFilterThrottleManager:: |
| + DestroyRulesetHandleIfNoLongerUsed() { |
| + if (activated_frame_hosts_.size() == 0) |
| + ruleset_handle_.reset(); |
| +} |
| + |
| +} // namespace subresource_filter |