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..22d11c4be5109631fd951b70f0838663155ee981 |
| --- /dev/null |
| +++ b/components/subresource_filter/content/browser/content_subresource_filter_throttle_manager.cc |
| @@ -0,0 +1,181 @@ |
| +// 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/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 { |
| + |
| +FrameActivationInfo::FrameActivationInfo() {} |
| +FrameActivationInfo::~FrameActivationInfo() {} |
| + |
| +bool ContentSubresourceFilterThrottleManager::Delegate::ShouldVetoActivation( |
| + 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* activated_navigation, |
| + const ActivationState& activation_state) { |
| + DCHECK(activated_navigation->IsInMainFrame()); |
|
engedy
2017/02/20 15:58:13
For DCHECK: #include "base/logging.h"
Charlie Harrison
2017/03/01 00:02:56
Done.
|
| + DCHECK(!activated_navigation->HasCommitted()); |
| + auto it = ongoing_activation_throttles_.find(activated_navigation); |
| + 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); |
| + DestroyRulesetHandleIfNecessary(); |
| +} |
| + |
| +// 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 |
| + // 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()) { |
| + DestroyRulesetHandleIfNecessary(); |
| + return; |
| + } |
| + |
| + const ActivationState& activation_state = |
| + throttle->second->GetActivationState(); |
| + if (activation_state.activation_level == ActivationLevel::DISABLED || |
| + delegate_->ShouldVetoActivation(navigation_handle)) { |
| + DestroyRulesetHandleIfNecessary(); |
| + return; |
| + } |
| + |
| + auto info = base::MakeUnique<FrameActivationInfo>(); |
| + info->subresource_filter = throttle->second->ReleaseFilter(); |
| + info->activation_state = activation_state; |
| + DCHECK(info->subresource_filter); |
| + activated_frame_hosts_[frame_host] = std::move(info); |
| + // 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()) |
|
engedy
2017/02/20 15:58:13
Do we need to check non-same-page here?
Charlie Harrison
2017/03/01 00:02:56
Yep, great catch. Adding this to the testing TODO.
|
| + current_committed_load_has_notified_disallowed_load_ = false; |
| +} |
| + |
| +void ContentSubresourceFilterThrottleManager::MaybeAppendNavigationThrottles( |
| + content::NavigationHandle* navigation_handle, |
| + std::vector<std::unique_ptr<content::NavigationThrottle>>* throttles) { |
| + DCHECK(!navigation_handle->IsSamePage()); |
| + if (std::unique_ptr<content::NavigationThrottle> filtering_throttle = |
| + MaybeCreateSubframeNavigationFilteringThrottle(navigation_handle)) { |
| + throttles->push_back(std::move(filtering_throttle)); |
| + } |
| + if (std::unique_ptr<content::NavigationThrottle> activation_throttle = |
| + MaybeCreateActivationStateComputingThrottle(navigation_handle)) { |
| + throttles->push_back(std::move(activation_throttle)); |
| + } |
| +} |
| + |
| +std::unique_ptr<content::NavigationThrottle> |
| +ContentSubresourceFilterThrottleManager:: |
| + MaybeCreateSubframeNavigationFilteringThrottle( |
| + content::NavigationHandle* navigation_handle) { |
| + if (navigation_handle->IsInMainFrame()) |
| + return nullptr; |
| + FrameActivationInfo* info = |
| + GetFrameActivationInfoForChildNavigation(navigation_handle); |
|
engedy
2017/02/20 15:58:13
nit: What do you think about:
FrameActivationInfo
Charlie Harrison
2017/03/01 00:02:56
I like it (modified slightly for the removal of Fr
|
| + return info ? base::MakeUnique<SubframeNavigationFilteringThrottle>( |
| + navigation_handle, info->subresource_filter.get()) |
| + : nullptr; |
| +} |
| + |
| +std::unique_ptr<content::NavigationThrottle> |
| +ContentSubresourceFilterThrottleManager:: |
| + MaybeCreateActivationStateComputingThrottle( |
| + content::NavigationHandle* navigation_handle) { |
| + // Main frames: create unconditionally. |
| + if (navigation_handle->IsInMainFrame()) |
|
engedy
2017/02/20 15:58:13
style nit: {}
Charlie Harrison
2017/03/01 00:02:56
Done.
|
| + return ActivationStateComputingNavigationThrottle::CreateForMainFrame( |
| + navigation_handle); |
| + |
| + // Subframes: create only for frames with activated parents. |
| + FrameActivationInfo* info = |
|
engedy
2017/02/20 15:58:13
nit: s/info/parent_info/
Charlie Harrison
2017/03/01 00:02:56
Done.
|
| + GetFrameActivationInfoForChildNavigation(navigation_handle); |
| + if (!info) |
| + return nullptr; |
| + DCHECK(ruleset_handle_); |
| + return ActivationStateComputingNavigationThrottle::CreateForSubframe( |
| + navigation_handle, ruleset_handle_.get(), info->activation_state, |
| + base::Bind(&ContentSubresourceFilterThrottleManager:: |
| + MaybeCallFirstDisallowedLoad, |
| + weak_ptr_factory_.GetWeakPtr())); |
| +} |
| + |
| +FrameActivationInfo* ContentSubresourceFilterThrottleManager:: |
| + GetFrameActivationInfoForChildNavigation( |
| + 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:: |
| + DestroyRulesetHandleIfNecessary() { |
| + if (activated_frame_hosts_.size() == 0) |
| + ruleset_handle_.reset(); |
| +} |
| + |
| +} // namespace subresource_filter |