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

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

Issue 2683413003: Introduce AsyncDocumentSubresourceFilter. (Closed)
Patch Set: Address more comments from engedy@. 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"
jochen (gone - plz use gerrit) 2017/02/15 10:32:11 you can only include POD and enum headers from the
pkalinnikov 2017/02/15 11:47:40 Done.
20 #include "url/gurl.h"
21
22 namespace subresource_filter {
23
24 // An asynchronous wrapper around DocumentSubresourceFilter (DSF).
25 //
26 // It is accessed on the UI thread and owns a DSF living on a dedicated
27 // sequenced |task_runner|. Provides asynchronous access to the DSF and destroys
28 // it asynchronously.
29 //
30 // Initially holds an empty filter in the synchronously created Core object, and
31 // initializes the filter on the |task_runner| asynchronously. This lets ADSF be
32 // created synchrously and be immediately used by clients on the UI thread,
33 // while the DSF is retrieved on the |task_runner| in a deferred manner.
34 class AsyncDocumentSubresourceFilter {
35 public:
36 using LoadPolicy = blink::WebDocumentSubresourceFilter::LoadPolicy;
37 using LoadPolicyCallback = base::Callback<void(LoadPolicy)>;
38
39 class Core;
40
41 // Encapsulates the parameters needed for computing the frame-level
42 // ActivationState appropriate for the frame this ADSF is created for. These
43 // parameters are posted to ADSF::Core during its initialization.
44 struct InitializationParams {
45 InitializationParams();
46
47 // Takes parameters needed for calculating the main-frame ActivationState,
48 // that is: the main-frame |document| URL, the page-level
49 // |activation_level|, and whether or not to |measure_performance|.
50 InitializationParams(GURL document_url,
51 ActivationLevel activation_level,
52 bool measure_performance);
53
54 // Takes parameters needed for calculating the sub-frame ActivationState,
55 // that is: the sub-frame |document| URL, the origin of its |parent|, as
56 // well as the parent's |activation_state|.
57 InitializationParams(GURL document_url,
58 url::Origin parent_document_origin,
59 ActivationState parent_activation_state);
60
61 ~InitializationParams();
62
63 InitializationParams(InitializationParams&& other);
64 InitializationParams& operator=(InitializationParams&& other);
65
66 // Parameters used to compute ActivationState for the |document| before
67 // creating a DocumentSubresourceFilter.
68 GURL document_url;
69 url::Origin parent_document_origin;
70 ActivationState parent_activation_state;
71
72 private:
73 DISALLOW_COPY_AND_ASSIGN(InitializationParams);
74 };
75
76 // Creates a Core and initializes it asynchronously on a |task_runner| using
77 // the supplied initialization |params| and a VerifiedRuleset taken from the
78 // |ruleset_handle|. The core remains owned by |this| object, but lives on
79 // (and is accessed on) the |task_runner|.
80 //
81 // Once the ActivationState for the current frame is calculated, it is
82 // reported back via |activation_state_callback| on the task runner associated
83 // with the current thread. If MemoryMappedRuleset is not present or
84 // malformed, then a default ActivationState is reported (with ActivationLevel
85 // equal to DISABLED).
86 //
87 // The |first_disallowed_load_callback|, if it is non-null, is invoked on the
88 // first ReportDisallowedLoad() call.
89 AsyncDocumentSubresourceFilter(
90 VerifiedRuleset::Handle* ruleset_handle,
91 InitializationParams params,
92 base::Callback<void(ActivationState)> activation_state_callback,
93 base::OnceClosure first_disallowed_load_callback);
94
95 ~AsyncDocumentSubresourceFilter();
96
97 // Computes LoadPolicy on a |task_runner| and returns it back to the calling
98 // thread via |result_callback|. If MemoryMappedRuleset is not present or
99 // malformed, then a LoadPolicy::Allow is returned.
100 void GetLoadPolicyForSubdocument(const GURL& subdocument_url,
101 LoadPolicyCallback result_callback);
102
103 // Invokes |first_disallowed_load_callback|, if necessary, and posts a task to
104 // call DocumentSubresourceFilter::reportDisallowedCallback() on the
105 // |task_runner|.
106 void ReportDisallowedLoad();
107
108 private:
109 // Note: Raw pointer, |core_| already holds a reference to |task_runner_|.
110 base::SequencedTaskRunner* task_runner_;
111 std::unique_ptr<Core, base::OnTaskRunnerDeleter> core_;
112 base::OnceClosure first_disallowed_load_callback_;
113
114 base::ThreadChecker thread_checker_;
115
116 DISALLOW_COPY_AND_ASSIGN(AsyncDocumentSubresourceFilter);
117 };
118
119 // Holds a DocumentSubresourceFilter that is created in a deferred manner in
120 // Initialize(), provided there is a valid ruleset to work with.
121 class AsyncDocumentSubresourceFilter::Core {
122 public:
123 Core();
124 ~Core();
125
126 // Can return nullptr even after initialization in case MemoryMappedRuleset
127 // was not present, or was malformed during it.
128 DocumentSubresourceFilter* filter() {
129 DCHECK(thread_checker_.CalledOnValidThread());
130 return filter_ ? &filter_.value() : nullptr;
131 }
132
133 private:
134 friend class AsyncDocumentSubresourceFilter;
135
136 // Computes ActivationState from |params| and initializes a DSF using it.
137 // Returns the computed activation state.
138 ActivationState Initialize(InitializationParams params,
139 VerifiedRuleset* verified_ruleset);
140
141 base::Optional<DocumentSubresourceFilter> filter_;
142 base::ThreadChecker thread_checker_;
143
144 DISALLOW_COPY_AND_ASSIGN(Core);
145 };
146
147 } // namespace subresource_filter
148
149 #endif // COMPONENTS_SUBRESOURCE_FILTER_CONTENT_BROWSER_ASYNC_DOCUMENT_SUBRESOU RCE_FILTER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698