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

Side by Side Diff: components/subresource_filter/content/browser/async_document_subresource_filter.cc

Issue 2683413003: Introduce AsyncDocumentSubresourceFilter. (Closed)
Patch Set: Add first_disallowed_load_callback to ADSF. 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 unified diff | Download patch
OLDNEW
(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(
26 GURL document_url,
27 ActivationLevel parent_activation_level)
28 : document_url(document_url),
29 parent_activation_state(parent_activation_level) {}
30
31 InitializationParams::~InitializationParams() = default;
32 InitializationParams::InitializationParams(InitializationParams&&) = default;
33 InitializationParams& InitializationParams::operator=(InitializationParams&&) =
34 default;
35
36 // AsyncDocumentSubresourceFilter ----------------------------------------------
37
38 AsyncDocumentSubresourceFilter::AsyncDocumentSubresourceFilter(
39 VerifiedRuleset::Handle* ruleset_handle,
40 InitializationParams params,
41 base::Callback<void(ActivationState)> activation_state_callback,
42 base::OnceClosure first_disallowed_load_callback)
43 : task_runner_(ruleset_handle->task_runner()),
44 core_(new Core(), base::OnTaskRunnerDeleter(task_runner_)),
45 first_disallowed_load_callback_(
46 std::move(first_disallowed_load_callback)) {
47 // Note: It is safe to post |ruleset_handle|'s VerifiedRuleset pointer,
48 // because a task to delete it can only be posted to (and, therefore,
49 // processed by) |task_runner| after this method returns, hence after the
50 // below task is posted.
51 base::PostTaskAndReplyWithResult(
52 task_runner_, FROM_HERE,
53 base::Bind(&Core::Initialize, base::Unretained(core_.get()),
54 base::Passed(&params), ruleset_handle->ruleset_.get()),
55 std::move(activation_state_callback));
56 }
57
58 AsyncDocumentSubresourceFilter::~AsyncDocumentSubresourceFilter() {
59 DCHECK(thread_checker_.CalledOnValidThread());
60 }
61
62 void AsyncDocumentSubresourceFilter::GetLoadPolicyForSubdocument(
63 const GURL& subdocument_url,
64 LoadPolicyCallback result_callback) {
65 DCHECK(thread_checker_.CalledOnValidThread());
66
67 auto compute_load_policy = [](AsyncDocumentSubresourceFilter::Core* core,
68 const GURL& subdocument_url) {
69 DCHECK(core);
70 DocumentSubresourceFilter* filter = core->filter();
71 return filter ? filter->GetLoadPolicyForSubdocument(subdocument_url)
72 : LoadPolicy::Allow;
73 };
74
75 // TODO(pkalinnikov): Think about avoiding copy of |subdocument_url| if it is
76 // too big and won't be allowed anyway (e.g., it's a data: URI).
77 base::PostTaskAndReplyWithResult(
78 task_runner_, FROM_HERE,
79 base::Bind(compute_load_policy, core_.get(), subdocument_url),
80 std::move(result_callback));
81 }
82
83 void AsyncDocumentSubresourceFilter::ReportDisallowedLoad() {
84 if (!first_disallowed_load_callback_.is_null())
85 std::move(first_disallowed_load_callback_).Run();
86
87 auto report_disallowed_load = [](AsyncDocumentSubresourceFilter::Core* core) {
88 DCHECK(core);
89 DocumentSubresourceFilter* filter = core->filter();
90 if (filter)
91 filter->reportDisallowedLoad();
92 };
93 task_runner_->PostTask(FROM_HERE,
94 base::Bind(report_disallowed_load, core_.get()));
95 }
96
97 // AsyncDocumentSubresourceFilter::Core ----------------------------------------
98
99 AsyncDocumentSubresourceFilter::Core::Core() {
100 thread_checker_.DetachFromThread();
101 }
102
103 AsyncDocumentSubresourceFilter::Core::~Core() {
104 DCHECK(thread_checker_.CalledOnValidThread());
105 }
106
107 ActivationState AsyncDocumentSubresourceFilter::Core::Initialize(
108 InitializationParams params,
109 VerifiedRuleset* verified_ruleset) {
110 DCHECK(thread_checker_.CalledOnValidThread());
111 DCHECK(verified_ruleset);
112
113 const MemoryMappedRuleset* ruleset = verified_ruleset->Get();
114 if (!ruleset)
115 return ActivationState(ActivationLevel::DISABLED);
116
117 ActivationState activation_state =
118 ComputeActivationState(params.document_url, params.parent_document_origin,
119 params.parent_activation_state, ruleset);
120
121 filter_.emplace(url::Origin(params.document_url), activation_state, ruleset,
122 base::OnceClosure());
123
124 return activation_state;
125 }
126
127 } // namespace subresource_filter
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698