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

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

Issue 2683413003: Introduce AsyncDocumentSubresourceFilter. (Closed)
Patch Set: Address comments from csharrison@. 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 #ifndef COMPONENTS_SUBRESOURCE_FILTER_CONTENT_BROWSER_ASYNC_DOCUMENT_SUBRESOURCE _FILTER_H_
6 #define COMPONENTS_SUBRESOURCE_FILTER_CONTENT_BROWSER_ASYNC_DOCUMENT_SUBRESOURCE _FILTER_H_
7
8 #include <memory>
9
10 #include "base/callback_forward.h"
11 #include "base/macros.h"
12 #include "base/optional.h"
13 #include "base/sequenced_task_runner.h"
14 #include "base/threading/thread_checker.h"
15 #include "components/subresource_filter/content/browser/verified_ruleset_dealer. h"
16 #include "components/subresource_filter/content/common/document_subresource_filt er.h"
17 #include "components/subresource_filter/core/common/activation_level.h"
18 #include "components/subresource_filter/core/common/activation_state.h"
19 #include "third_party/WebKit/public/platform/WebDocumentSubresourceFilter.h"
20
21 namespace subresource_filter {
22
23 // An asynchronous wrapper around DocumentSubresourceFilter.
24 //
25 // It is accessed on the UI thread and owns an ADSF::Core living on a dedicated
engedy 2017/02/14 15:13:13 nit: Given that the existence of Core is an implem
pkalinnikov 2017/02/14 18:47:14 Done.
26 // sequenced |task_runner|. Provides asynchronous access to the Core, and
27 // destroys it asynchronously.
28 class AsyncDocumentSubresourceFilter {
29 public:
30 using LoadPolicy = blink::WebDocumentSubresourceFilter::LoadPolicy;
31 using LoadPolicyCallback = base::Callback<void(LoadPolicy)>;
32
33 class Core;
34
35 // Parameters posted to ADSF::Core during its initialization. Needed for
36 // computing ActivationState for a (sub-)document, and for creating a
engedy 2017/02/14 15:13:14 nit: I'd suggest swapping the two parts, so that t
pkalinnikov 2017/02/14 18:47:14 Done.
37 // corresponding DocumentSubresourceFilter.
38 struct InitializationParams {
39 InitializationParams();
40
41 // Creates parameters for a main-frame |document|. |activation_level| should
engedy 2017/02/14 15:13:14 nit: How about phrasing this an its sibling like s
pkalinnikov 2017/02/14 18:47:14 Done.
42 // not be DISABLED.
43 InitializationParams(GURL document_url,
44 ActivationLevel activation_level,
45 bool measure_performance);
46
47 // Creates parameters for a subframe |document|. Parent's |activation_state|
48 // should not be DISABLED.
49 InitializationParams(GURL document_url,
50 url::Origin parent_document_origin,
51 ActivationState parent_activation_state);
52
53 ~InitializationParams();
54
55 InitializationParams(InitializationParams&& other);
56 InitializationParams& operator=(InitializationParams&& other);
57
58 // Parameters used to compute ActivationState for the |document| before
59 // creating a DocumentSubresourceFilter.
60 GURL document_url;
engedy 2017/02/14 15:13:14 nit: #include "url/gurl.h"
pkalinnikov 2017/02/14 18:47:14 Done.
61 url::Origin parent_document_origin;
62 ActivationState parent_activation_state;
63
64 private:
65 DISALLOW_COPY_AND_ASSIGN(InitializationParams);
66 };
67
68 // Creates a Core and initializes it asynchronously on a |task_runner| using
69 // the supplied initialization |params| and a VerifiedRuleset taken from the
70 // |ruleset_handle|. The instance remains owned by |this| object, but lives on
engedy 2017/02/14 15:13:14 nit: s/instance/core/
pkalinnikov 2017/02/14 18:47:14 Done.
71 // (and is accessed on) the |task_runner|.
72 //
73 // Once the ActivationState for the current frame is calculated, it is
74 // reported back via |activation_state_callback| on the task runner associated
75 // with the current thread. If MemoryMappedRuleset is not present or
76 // malformed, then a default ActivationState is reported (with ActivationLevel
77 // equal to DISABLED).
78 //
79 // The |first_disallowed_load_callback|, if it is non-null, is invoked on the
80 // first ReportDisallowedLoad() call.
81 AsyncDocumentSubresourceFilter(
82 VerifiedRuleset::Handle* ruleset_handle,
83 InitializationParams params,
84 base::Callback<void(ActivationState)> activation_state_callback,
85 base::OnceClosure first_disallowed_load_callback);
86
87 ~AsyncDocumentSubresourceFilter();
88
89 // Computes LoadPolicy on a |task_runner| and returns it back to the calling
90 // thread via |result_callback|. If MemoryMappedRuleset is not present or
91 // malformed, then a LoadPolicy::Allow is returned.
92 void GetLoadPolicyForSubdocument(const GURL& subdocument_url,
93 LoadPolicyCallback result_callback);
94
95 // Invokes |first_disallowed_load_callback|, if necessary, and posts a task to
96 // call DocumentSubresourceFilter::reportDisallowedCallback() on the
97 // |task_runner|.
98 void ReportDisallowedLoad();
99
100 private:
101 // Note: Raw pointer, |core_| already holds a reference to |task_runner_|.
102 base::SequencedTaskRunner* task_runner_;
103 std::unique_ptr<Core, base::OnTaskRunnerDeleter> core_;
104 base::OnceClosure first_disallowed_load_callback_;
105
106 base::ThreadChecker thread_checker_;
107
108 DISALLOW_COPY_AND_ASSIGN(AsyncDocumentSubresourceFilter);
109 };
110
111 // Holds a DocumentSubresourceFilter (after Initialization, in case there is a
engedy 2017/02/14 15:13:14 Comment nit: // Holds a DocumentSubresourceFilte
pkalinnikov 2017/02/14 18:47:14 Done.
112 // valid MemoryMappedRuleset), and provides access to it.
113 //
114 // Initially holds an empty filter, allowing this object to be created on the
engedy 2017/02/14 15:13:14 nit: What do you think about moving this paragraph
pkalinnikov 2017/02/14 18:47:14 Done.
115 // UI thread synchronously, hence letting the ADSF to be created synchronously
116 // and be immediately used by clients on the UI thread, while the DSF is
117 // retrieved on the |task_runner| in a deferred manner.
118 class AsyncDocumentSubresourceFilter::Core {
119 public:
120 Core();
121 ~Core();
122
123 // Can return nullptr even after initialization in case MemoryMappedRuleset
124 // was not present, or was malformed during it.
125 DocumentSubresourceFilter* filter() {
126 DCHECK(thread_checker_.CalledOnValidThread());
127 return filter_ ? &filter_.value() : nullptr;
128 }
129
130 private:
131 friend class AsyncDocumentSubresourceFilter;
132
133 // Computes ActivationState from |params| and initializes a DSF using it.
134 // Returns the computed activation state.
135 ActivationState Initialize(InitializationParams params,
136 VerifiedRuleset* verified_ruleset);
137
138 base::Optional<DocumentSubresourceFilter> filter_;
139 base::ThreadChecker thread_checker_;
140
141 DISALLOW_COPY_AND_ASSIGN(Core);
142 };
143
144 } // namespace subresource_filter
145
146 #endif // COMPONENTS_SUBRESOURCE_FILTER_CONTENT_BROWSER_ASYNC_DOCUMENT_SUBRESOU RCE_FILTER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698