Chromium Code Reviews| 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/logging.h" | |
| 12 #include "base/task_runner_util.h" | |
| 13 #include "base/threading/sequenced_task_runner_handle.h" | |
| 14 #include "components/subresource_filter/core/common/memory_mapped_ruleset.h" | |
| 15 | |
| 16 namespace subresource_filter { | |
| 17 | |
| 18 // AsyncDocumentSubresourceFilter::InitializationParams ------------------------ | |
| 19 | |
| 20 using InitializationParams = | |
| 21 AsyncDocumentSubresourceFilter::InitializationParams; | |
| 22 | |
| 23 InitializationParams::InitializationParams() = default; | |
| 24 | |
| 25 InitializationParams::InitializationParams(GURL document_url, | |
| 26 ActivationLevel activation_level, | |
| 27 bool measure_performance) | |
| 28 : document_url(std::move(document_url)), | |
| 29 parent_activation_state(activation_level) { | |
| 30 parent_activation_state.measure_performance = measure_performance; | |
| 31 } | |
| 32 | |
| 33 InitializationParams::InitializationParams( | |
| 34 GURL document_url, | |
| 35 url::Origin parent_document_origin, | |
| 36 ActivationState parent_activation_state) | |
| 37 : document_url(std::move(document_url)), | |
| 38 parent_document_origin(std::move(parent_document_origin)), | |
| 39 parent_activation_state(parent_activation_state) {} | |
| 40 | |
| 41 InitializationParams::~InitializationParams() = default; | |
| 42 InitializationParams::InitializationParams(InitializationParams&&) = default; | |
| 43 InitializationParams& InitializationParams::operator=(InitializationParams&&) = | |
| 44 default; | |
| 45 | |
| 46 // AsyncDocumentSubresourceFilter ---------------------------------------------- | |
| 47 | |
| 48 AsyncDocumentSubresourceFilter::AsyncDocumentSubresourceFilter( | |
| 49 VerifiedRuleset::Handle* ruleset_handle, | |
| 50 InitializationParams params, | |
| 51 base::Callback<void(ActivationState)> activation_state_callback, | |
| 52 base::OnceClosure first_disallowed_load_callback) | |
| 53 : task_runner_(ruleset_handle->task_runner()), | |
| 54 core_(new Core(), base::OnTaskRunnerDeleter(task_runner_)), | |
| 55 first_disallowed_load_callback_( | |
| 56 std::move(first_disallowed_load_callback)) { | |
| 57 // Note: It is safe to post |ruleset_handle|'s VerifiedRuleset pointer, | |
| 58 // because a task to delete it can only be posted to (and, therefore, | |
| 59 // processed by) |task_runner| after this method returns, hence after the | |
| 60 // below task is posted. | |
| 61 base::PostTaskAndReplyWithResult( | |
| 62 task_runner_, FROM_HERE, | |
| 63 base::Bind(&Core::Initialize, base::Unretained(core_.get()), | |
| 64 base::Passed(¶ms), ruleset_handle->ruleset_.get()), | |
| 65 std::move(activation_state_callback)); | |
| 66 } | |
| 67 | |
| 68 AsyncDocumentSubresourceFilter::~AsyncDocumentSubresourceFilter() { | |
| 69 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 70 } | |
| 71 | |
| 72 void AsyncDocumentSubresourceFilter::GetLoadPolicyForSubdocument( | |
| 73 const GURL& subdocument_url, | |
| 74 LoadPolicyCallback result_callback) { | |
| 75 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 76 | |
| 77 auto compute_load_policy = [](AsyncDocumentSubresourceFilter::Core* core, | |
| 78 const GURL& subdocument_url) { | |
| 79 DCHECK(core); | |
| 80 DocumentSubresourceFilter* filter = core->filter(); | |
| 81 return filter ? filter->GetLoadPolicyForSubdocument(subdocument_url) | |
| 82 : LoadPolicy::Allow; | |
| 83 }; | |
| 84 | |
| 85 // TODO(pkalinnikov): Think about avoiding copy of |subdocument_url| if it is | |
| 86 // too big and won't be allowed anyway (e.g., it's a data: URI). | |
| 87 base::PostTaskAndReplyWithResult( | |
| 88 task_runner_, FROM_HERE, | |
| 89 base::Bind(compute_load_policy, core_.get(), subdocument_url), | |
| 90 std::move(result_callback)); | |
| 91 } | |
| 92 | |
| 93 void AsyncDocumentSubresourceFilter::ReportDisallowedLoad() { | |
| 94 if (!first_disallowed_load_callback_.is_null()) | |
| 95 std::move(first_disallowed_load_callback_).Run(); | |
| 96 | |
| 97 auto report_disallowed_load = [](AsyncDocumentSubresourceFilter::Core* core) { | |
| 98 DCHECK(core); | |
| 99 DocumentSubresourceFilter* filter = core->filter(); | |
| 100 if (filter) | |
| 101 filter->reportDisallowedLoad(); | |
| 102 }; | |
| 103 task_runner_->PostTask(FROM_HERE, | |
| 104 base::Bind(report_disallowed_load, core_.get())); | |
| 105 } | |
| 106 | |
| 107 // AsyncDocumentSubresourceFilter::Core ---------------------------------------- | |
| 108 | |
| 109 AsyncDocumentSubresourceFilter::Core::Core() { | |
| 110 thread_checker_.DetachFromThread(); | |
| 111 } | |
| 112 | |
| 113 AsyncDocumentSubresourceFilter::Core::~Core() { | |
| 114 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 115 } | |
| 116 | |
| 117 ActivationState AsyncDocumentSubresourceFilter::Core::Initialize( | |
| 118 InitializationParams params, | |
| 119 VerifiedRuleset* verified_ruleset) { | |
| 120 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 121 DCHECK(verified_ruleset); | |
| 122 | |
| 123 const MemoryMappedRuleset* ruleset = verified_ruleset->Get(); | |
| 124 if (!ruleset) | |
| 125 return ActivationState(ActivationLevel::DISABLED); | |
| 126 | |
| 127 ActivationState activation_state = | |
| 128 ComputeActivationState(params.document_url, params.parent_document_origin, | |
| 129 params.parent_activation_state, ruleset); | |
| 130 | |
| 131 filter_.emplace(url::Origin(params.document_url), activation_state, ruleset, | |
|
Charlie Harrison
2017/02/13 20:07:50
Can you wrap this in a if (activation_state.activa
pkalinnikov
2017/02/14 09:31:07
Just like with DocumentSubresourceFilter, let's no
Charlie Harrison
2017/02/14 13:07:40
I prefer just DCHECKing the activation state is no
| |
| 132 base::OnceClosure()); | |
| 133 | |
| 134 return activation_state; | |
| 135 } | |
| 136 | |
| 137 } // namespace subresource_filter | |
| OLD | NEW |