Index: components/subresource_filter/content/browser/async_document_subresource_filter.cc |
diff --git a/components/subresource_filter/content/browser/async_document_subresource_filter.cc b/components/subresource_filter/content/browser/async_document_subresource_filter.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..7c8e85a14093f321cc1b3cc549747ccfc064ee9a |
--- /dev/null |
+++ b/components/subresource_filter/content/browser/async_document_subresource_filter.cc |
@@ -0,0 +1,95 @@ |
+// 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/async_document_subresource_filter.h" |
+ |
+#include "base/bind.h" |
+#include "base/bind_helpers.h" |
+#include "base/callback.h" |
+#include "base/location.h" |
+#include "base/task_runner_util.h" |
+#include "components/subresource_filter/core/common/memory_mapped_ruleset.h" |
+ |
+namespace subresource_filter { |
+ |
+using InitializationParams = |
+ AsyncDocumentSubresourceFilter::InitializationParams; |
+ |
+InitializationParams::InitializationParams() = default; |
+InitializationParams::~InitializationParams() = default; |
+InitializationParams::InitializationParams(InitializationParams&&) = default; |
+InitializationParams& InitializationParams::operator=(InitializationParams&&) = |
+ default; |
+ |
+AsyncDocumentSubresourceFilter::AsyncDocumentSubresourceFilter( |
+ VerifiedRuleset::Handle* ruleset_handle, |
+ InitializationParams params, |
+ base::Callback<void(ActivationState)> activation_state_callback) |
+ : task_runner_(ruleset_handle->task_runner()), |
+ core_(new Core(), base::OnTaskRunnerDeleter(task_runner_)) { |
+ // Note: It is safe to post |ruleset_handle|'s VerifiedRuleset pointer, |
+ // because a task to delete it can only be posted to (and, therefore, |
+ // processed by) |task_runner| after this method returns, hence after the |
+ // below task is posted. |
+ base::PostTaskAndReplyWithResult( |
+ task_runner_, FROM_HERE, |
+ base::Bind(&Core::Initialize, base::Unretained(core_.get()), |
+ base::Passed(¶ms), ruleset_handle->ruleset_.get()), |
+ std::move(activation_state_callback)); |
+} |
+ |
+AsyncDocumentSubresourceFilter::~AsyncDocumentSubresourceFilter() { |
+ DCHECK(thread_checker_.CalledOnValidThread()); |
+} |
+ |
+void AsyncDocumentSubresourceFilter::GetLoadPolicyForSubdocument( |
+ const GURL& subdocument_url, |
+ LoadPolicyCallback result_callback) { |
+ DCHECK(thread_checker_.CalledOnValidThread()); |
+ |
+ auto compute_load_policy = [](AsyncDocumentSubresourceFilter::Core* core, |
+ const GURL& subdocument_url) { |
+ DCHECK(core); |
+ DocumentSubresourceFilter* filter = core->filter(); |
+ return filter ? filter->GetLoadPolicyForSubdocument(subdocument_url) |
+ : LoadPolicy::Allow; |
+ }; |
+ |
+ // TODO(pkalinnikov): Think about avoiding copy of |subdocument_url| if it is |
+ // too big and won't be allowed anyway (e.g., it's a data: URI). |
+ base::PostTaskAndReplyWithResult( |
+ task_runner_, FROM_HERE, |
+ base::Bind(compute_load_policy, core_.get(), subdocument_url), |
+ std::move(result_callback)); |
+} |
+ |
+AsyncDocumentSubresourceFilter::Core::Core() { |
+ thread_checker_.DetachFromThread(); |
+} |
+ |
+AsyncDocumentSubresourceFilter::Core::~Core() { |
+ DCHECK(thread_checker_.CalledOnValidThread()); |
+} |
+ |
+ActivationState AsyncDocumentSubresourceFilter::Core::Initialize( |
+ InitializationParams params, |
+ VerifiedRuleset* verified_ruleset) { |
+ DCHECK(thread_checker_.CalledOnValidThread()); |
+ DCHECK(verified_ruleset); |
+ |
+ const MemoryMappedRuleset* ruleset = verified_ruleset->Get(); |
+ if (!ruleset) |
+ return ActivationState(ActivationLevel::DISABLED); |
+ |
+ ActivationState activation_state = |
+ ComputeActivationState(params.document_url, params.parent_document_origin, |
+ params.parent_activation_state, ruleset); |
+ |
+ filter_.emplace(url::Origin(params.document_url), activation_state, ruleset, |
+ std::move(params.first_disallowed_load_callback)); |
+ |
+ return activation_state; |
+} |
+ |
+} // namespace subresource_filter |