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/async_document_subresour
ce_filter.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/bind_helpers.h" |
| 9 #include "base/callback.h" |
| 10 #include "base/location.h" |
| 11 #include "base/task_runner_util.h" |
| 12 #include "components/subresource_filter/core/common/memory_mapped_ruleset.h" |
| 13 |
| 14 namespace subresource_filter { |
| 15 |
| 16 using InitializationParams = |
| 17 AsyncDocumentSubresourceFilter::InitializationParams; |
| 18 |
| 19 InitializationParams::InitializationParams() = default; |
| 20 InitializationParams::~InitializationParams() = default; |
| 21 InitializationParams::InitializationParams(InitializationParams&&) = default; |
| 22 InitializationParams& InitializationParams::operator=(InitializationParams&&) = |
| 23 default; |
| 24 |
| 25 AsyncDocumentSubresourceFilter::AsyncDocumentSubresourceFilter( |
| 26 VerifiedRuleset::Handle* ruleset_handle, |
| 27 InitializationParams params, |
| 28 base::Callback<void(ActivationState)> activation_state_callback) |
| 29 : task_runner_(ruleset_handle->task_runner()), |
| 30 core_(new Core(), base::OnTaskRunnerDeleter(task_runner_)) { |
| 31 // Note: It is safe to post |ruleset_handle|'s VerifiedRuleset pointer, |
| 32 // because a task to delete it can only be posted to (and, therefore, |
| 33 // processed by) |task_runner| after this method returns, hence after the |
| 34 // below task is posted. |
| 35 base::PostTaskAndReplyWithResult( |
| 36 task_runner_, FROM_HERE, |
| 37 base::Bind(&Core::Initialize, base::Unretained(core_.get()), |
| 38 base::Passed(¶ms), ruleset_handle->ruleset_.get()), |
| 39 std::move(activation_state_callback)); |
| 40 } |
| 41 |
| 42 AsyncDocumentSubresourceFilter::~AsyncDocumentSubresourceFilter() { |
| 43 DCHECK(thread_checker_.CalledOnValidThread()); |
| 44 } |
| 45 |
| 46 void AsyncDocumentSubresourceFilter::GetLoadPolicyForSubdocument( |
| 47 const GURL& subdocument_url, |
| 48 LoadPolicyCallback result_callback) { |
| 49 DCHECK(thread_checker_.CalledOnValidThread()); |
| 50 |
| 51 auto compute_load_policy = [](AsyncDocumentSubresourceFilter::Core* core, |
| 52 const GURL& subdocument_url) { |
| 53 DCHECK(core); |
| 54 DocumentSubresourceFilter* filter = core->filter(); |
| 55 return filter ? filter->GetLoadPolicyForSubdocument(subdocument_url) |
| 56 : LoadPolicy::Allow; |
| 57 }; |
| 58 |
| 59 // TODO(pkalinnikov): Think about avoiding copy of |subdocument_url| if it is |
| 60 // too big and won't be allowed anyway (e.g., it's a data: URI). |
| 61 base::PostTaskAndReplyWithResult( |
| 62 task_runner_, FROM_HERE, |
| 63 base::Bind(compute_load_policy, core_.get(), subdocument_url), |
| 64 std::move(result_callback)); |
| 65 } |
| 66 |
| 67 AsyncDocumentSubresourceFilter::Core::Core() { |
| 68 thread_checker_.DetachFromThread(); |
| 69 } |
| 70 |
| 71 AsyncDocumentSubresourceFilter::Core::~Core() { |
| 72 DCHECK(thread_checker_.CalledOnValidThread()); |
| 73 } |
| 74 |
| 75 ActivationState AsyncDocumentSubresourceFilter::Core::Initialize( |
| 76 InitializationParams params, |
| 77 VerifiedRuleset* verified_ruleset) { |
| 78 DCHECK(thread_checker_.CalledOnValidThread()); |
| 79 DCHECK(verified_ruleset); |
| 80 |
| 81 const MemoryMappedRuleset* ruleset = verified_ruleset->Get(); |
| 82 if (!ruleset) |
| 83 return ActivationState(ActivationLevel::DISABLED); |
| 84 |
| 85 ActivationState activation_state = |
| 86 ComputeActivationState(params.document_url, params.parent_document_origin, |
| 87 params.parent_activation_state, ruleset); |
| 88 |
| 89 filter_.emplace(url::Origin(params.document_url), activation_state, ruleset, |
| 90 std::move(params.first_disallowed_load_callback)); |
| 91 |
| 92 return activation_state; |
| 93 } |
| 94 |
| 95 } // namespace subresource_filter |
OLD | NEW |