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 DCHECK_NE(ActivationLevel::DISABLED, activation_level); | |
31 parent_activation_state.measure_performance = measure_performance; | |
32 } | |
33 | |
34 InitializationParams::InitializationParams( | |
35 GURL document_url, | |
36 url::Origin parent_document_origin, | |
37 ActivationState parent_activation_state) | |
38 : document_url(std::move(document_url)), | |
39 parent_document_origin(std::move(parent_document_origin)), | |
40 parent_activation_state(parent_activation_state) { | |
41 DCHECK_NE(ActivationLevel::DISABLED, | |
42 parent_activation_state.activation_level); | |
43 } | |
44 | |
45 InitializationParams::~InitializationParams() = default; | |
46 InitializationParams::InitializationParams(InitializationParams&&) = default; | |
47 InitializationParams& InitializationParams::operator=(InitializationParams&&) = | |
48 default; | |
49 | |
50 // AsyncDocumentSubresourceFilter ---------------------------------------------- | |
51 | |
52 AsyncDocumentSubresourceFilter::AsyncDocumentSubresourceFilter( | |
53 VerifiedRuleset::Handle* ruleset_handle, | |
engedy
2017/02/14 15:13:13
Based on how Charles is planning to use this, have
pkalinnikov
2017/02/14 18:47:13
Charles, can you please tell how you are going to
Charlie Harrison
2017/02/14 19:31:06
Your solution sounds good to me. I was thinking th
engedy
2017/02/14 20:33:31
Currently the verification state is persisted on t
| |
54 InitializationParams params, | |
55 base::Callback<void(ActivationState)> activation_state_callback, | |
56 base::OnceClosure first_disallowed_load_callback) | |
57 : task_runner_(ruleset_handle->task_runner()), | |
58 core_(new Core(), base::OnTaskRunnerDeleter(task_runner_)), | |
59 first_disallowed_load_callback_( | |
60 std::move(first_disallowed_load_callback)) { | |
engedy
2017/02/14 15:13:13
nit: #include <utility> for std::move.
pkalinnikov
2017/02/14 18:47:13
Done.
| |
61 DCHECK_NE(ActivationLevel::DISABLED, | |
62 params.parent_activation_state.activation_level); | |
63 | |
64 // Note: It is safe to post |ruleset_handle|'s VerifiedRuleset pointer, | |
65 // because a task to delete it can only be posted to (and, therefore, | |
66 // processed by) |task_runner| after this method returns, hence after the | |
67 // below task is posted. | |
68 base::PostTaskAndReplyWithResult( | |
69 task_runner_, FROM_HERE, | |
70 base::Bind(&Core::Initialize, base::Unretained(core_.get()), | |
71 base::Passed(¶ms), ruleset_handle->ruleset_.get()), | |
72 std::move(activation_state_callback)); | |
73 } | |
74 | |
75 AsyncDocumentSubresourceFilter::~AsyncDocumentSubresourceFilter() { | |
76 DCHECK(thread_checker_.CalledOnValidThread()); | |
77 } | |
78 | |
79 void AsyncDocumentSubresourceFilter::GetLoadPolicyForSubdocument( | |
80 const GURL& subdocument_url, | |
81 LoadPolicyCallback result_callback) { | |
82 DCHECK(thread_checker_.CalledOnValidThread()); | |
83 | |
84 auto compute_load_policy = [](AsyncDocumentSubresourceFilter::Core* core, | |
85 const GURL& subdocument_url) { | |
86 DCHECK(core); | |
87 DocumentSubresourceFilter* filter = core->filter(); | |
engedy
2017/02/14 15:13:13
nit: Do you expect it to have a measurable perform
pkalinnikov
2017/02/14 18:47:13
Funny thing is that it doesn't save the line, beca
engedy
2017/02/14 20:33:31
Fair enough. ;-)
| |
88 return filter ? filter->GetLoadPolicyForSubdocument(subdocument_url) | |
89 : LoadPolicy::Allow; | |
90 }; | |
91 | |
92 // TODO(pkalinnikov): Think about avoiding copy of |subdocument_url| if it is | |
93 // too big and won't be allowed anyway (e.g., it's a data: URI). | |
94 base::PostTaskAndReplyWithResult( | |
95 task_runner_, FROM_HERE, | |
96 base::Bind(compute_load_policy, core_.get(), subdocument_url), | |
97 std::move(result_callback)); | |
98 } | |
99 | |
100 void AsyncDocumentSubresourceFilter::ReportDisallowedLoad() { | |
101 if (!first_disallowed_load_callback_.is_null()) | |
102 std::move(first_disallowed_load_callback_).Run(); | |
103 | |
104 auto report_disallowed_load = [](AsyncDocumentSubresourceFilter::Core* core) { | |
engedy
2017/02/14 15:13:13
nit: How would it look if we inlined this lambda (
pkalinnikov
2017/02/14 18:47:14
Okay, this doesn't look too bad :)
| |
105 DCHECK(core); | |
106 DocumentSubresourceFilter* filter = core->filter(); | |
107 if (filter) | |
108 filter->reportDisallowedLoad(); | |
109 }; | |
110 task_runner_->PostTask(FROM_HERE, | |
111 base::Bind(report_disallowed_load, core_.get())); | |
112 } | |
113 | |
114 // AsyncDocumentSubresourceFilter::Core ---------------------------------------- | |
115 | |
116 AsyncDocumentSubresourceFilter::Core::Core() { | |
117 thread_checker_.DetachFromThread(); | |
118 } | |
119 | |
120 AsyncDocumentSubresourceFilter::Core::~Core() { | |
121 DCHECK(thread_checker_.CalledOnValidThread()); | |
122 } | |
123 | |
124 ActivationState AsyncDocumentSubresourceFilter::Core::Initialize( | |
125 InitializationParams params, | |
126 VerifiedRuleset* verified_ruleset) { | |
127 DCHECK(thread_checker_.CalledOnValidThread()); | |
128 DCHECK(verified_ruleset); | |
129 | |
130 const MemoryMappedRuleset* ruleset = verified_ruleset->Get(); | |
engedy
2017/02/14 15:13:13
nit: Same here?
pkalinnikov
2017/02/14 18:47:13
Done.
| |
131 if (!ruleset) | |
132 return ActivationState(ActivationLevel::DISABLED); | |
133 | |
134 ActivationState activation_state = | |
135 ComputeActivationState(params.document_url, params.parent_document_origin, | |
136 params.parent_activation_state, ruleset); | |
137 | |
138 DCHECK_NE(ActivationLevel::DISABLED, activation_state.activation_level); | |
139 filter_.emplace(url::Origin(params.document_url), activation_state, ruleset, | |
140 base::OnceClosure()); | |
engedy
2017/02/14 15:13:13
Let's add a comment to explain why this callback i
pkalinnikov
2017/02/14 18:47:14
Done.
| |
141 | |
142 return activation_state; | |
143 } | |
144 | |
145 } // namespace subresource_filter | |
OLD | NEW |