Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(599)

Unified Diff: components/subresource_filter/content/browser/content_subresource_filter_throttle_manager.cc

Issue 2691423006: Introduce the ThrottleManager (Closed)
Patch Set: Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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..c492fd04c00f518f990ad40514b974c143e0fe61
--- /dev/null
+++ b/components/subresource_filter/content/browser/content_subresource_filter_throttle_manager.cc
@@ -0,0 +1,149 @@
+// 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,
+ content::WebContents* web_contents)
+ : content::WebContentsObserver(web_contents), delegate_(delegate) {}
+
+ContentSubresourceFilterThrottleManager::
+ ~ContentSubresourceFilterThrottleManager() {}
+
+std::unique_ptr<content::NavigationThrottle>
+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
+ MaybeCreateSubframeNavigationFilteringThrottle(
+ content::NavigationHandle* navigation_handle) {
+ if (navigation_handle->IsInMainFrame() || navigation_handle->IsSamePage())
+ return nullptr;
+ FrameActivationInfo* info =
+ GetFrameActivationInfoForChildNavigation(navigation_handle);
+ return info ? base::MakeUnique<SubframeNavigationFilteringThrottle>(
+ navigation_handle, info->subresource_filter.get())
+ : nullptr;
+}
+
+std::unique_ptr<content::NavigationThrottle>
+ContentSubresourceFilterThrottleManager::
+ MaybeCreateActivationStateComputingThrottle(
+ content::NavigationHandle* navigation_handle) {
+ if (navigation_handle->IsSamePage())
+ return nullptr;
+
+ // Main frames: create unconditionally.
+ if (navigation_handle->IsInMainFrame())
+ return ActivationStateComputingNavigationThrottle::CreateForMainFrame(
+ navigation_handle);
+
+ // Subframes: create only for frames with activated parents.
+ FrameActivationInfo* info =
+ GetFrameActivationInfoForChildNavigation(navigation_handle);
+ if (!info)
+ return nullptr;
+ DCHECK(ruleset_handle_);
+ return ActivationStateComputingNavigationThrottle::CreateForSubframe(
+ navigation_handle, ruleset_handle_.get(), info->activation_state,
+ base::Bind(&Delegate::OnFirstSubresourceLoadDisallowed,
+ delegate_->AsWeakPtr()));
+}
+
+void ContentSubresourceFilterThrottleManager::OnPageStateActivationComputed(
+ content::NavigationHandle* activated_navigation,
+ const ActivationState& state) {
+ DCHECK(activated_navigation->IsInMainFrame());
+ DCHECK(!activated_navigation->HasCommitted());
+ auto it = ongoing_activation_throttles_.find(activated_navigation);
+ if (it != ongoing_activation_throttles_.end()) {
+ it->second->NotifyPageActivationWithRuleset(
+ EnsureRulesetHandle(), state,
+ base::Bind(&ContentSubresourceFilterThrottleManager::Delegate::
+ OnFirstSubresourceLoadDisallowed,
+ delegate_->AsWeakPtr()));
+ }
+}
+
+void ContentSubresourceFilterThrottleManager::RenderFrameDeleted(
+ content::RenderFrameHost* frame_host) {
+ activated_frame_hosts_.erase(frame_host);
+ if (activated_frame_hosts_.size() == 0)
+ ruleset_handle_.reset();
+}
+
+// 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())
+ return;
+
+ const ActivationState& state = throttle->second->GetActivationState();
+ if (state.activation_level == ActivationLevel::DISABLED ||
+ delegate_->ShouldVetoActivation(navigation_handle))
+ return;
+
+ auto info = base::MakeUnique<FrameActivationInfo>();
+ info->subresource_filter = throttle->second->ReleaseFilter();
+ info->activation_state = state;
+ DCHECK(info->subresource_filter);
+ activated_frame_hosts_[navigation_handle->GetRenderFrameHost()] =
+ std::move(info);
+ delegate_->OnActivationStateComputedForCommittingLoad(navigation_handle,
+ state);
+}
+
+void ContentSubresourceFilterThrottleManager::DidFinishNavigation(
+ content::NavigationHandle* navigation_handle) {
+ ongoing_activation_throttles_.erase(navigation_handle);
+}
+
+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();
+}
+
+VerifiedRuleset::Handle*
+ContentSubresourceFilterThrottleManager::EnsureRulesetHandle() {
+ if (!ruleset_handle_) {
+ ruleset_handle_ = base::MakeUnique<VerifiedRuleset::Handle>(
+ delegate_->GetRulesetDealerHandle());
+ }
+ return ruleset_handle_.get();
+}
+
+} // namespace subresource_filter

Powered by Google App Engine
This is Rietveld 408576698