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

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

Issue 2683413003: Introduce AsyncDocumentSubresourceFilter. (Closed)
Patch Set: Distinguish main-frame and subframe ADSF initialization params. 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
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
37 // corresponding DocumentSubresourceFilter.
38 struct InitializationParams {
39 InitializationParams();
40
41 // Creates parameters for a main-frame |document|.
42 InitializationParams(GURL document_url,
43 ActivationLevel activation_level,
44 bool measure_performance);
45
46 // Creates parameters for a subframe |document|.
47 InitializationParams(GURL document_url,
48 url::Origin parent_document_origin,
49 ActivationState parent_activation_state);
50
51 ~InitializationParams();
52
53 InitializationParams(InitializationParams&& other);
54 InitializationParams& operator=(InitializationParams&& other);
55
56 // Parameters used to compute ActivationState for the |document| before
57 // creating a DocumentSubresourceFilter.
58 GURL document_url;
59 url::Origin parent_document_origin;
60 ActivationState parent_activation_state;
61
62 private:
63 DISALLOW_COPY_AND_ASSIGN(InitializationParams);
64 };
65
66 // Creates a Core and initializes it asynchronously on a |task_runner| using
67 // the supplied initialization |params| and a VerifiedRuleset taken from the
68 // |ruleset_handle|. The instance remains owned by |this| object, but lives on
69 // (and is accessed on) the |task_runner|.
70 //
71 // Once the ActivationState for the current frame is calculated, it is
72 // reported back via |activation_state_callback| on the task runner associated
73 // with the current thread. If MemoryMappedRuleset is not present or
74 // malformed, then a default ActivationState is reported (with ActivationLevel
75 // equal to DISABLED).
76 //
77 // The |first_disallowed_load_callback|, if it is non-null, is invoked on the
78 // first ReportDisallowedLoad() call.
79 AsyncDocumentSubresourceFilter(
80 VerifiedRuleset::Handle* ruleset_handle,
81 InitializationParams params,
82 base::Callback<void(ActivationState)> activation_state_callback,
83 base::OnceClosure first_disallowed_load_callback);
84
85 ~AsyncDocumentSubresourceFilter();
86
87 // Computes LoadPolicy on a |task_runner| and returns it back to the calling
88 // thread via |result_callback|. If MemoryMappedRuleset is not present or
89 // malformed, then a LoadPolicy::Allow is returned.
90 void GetLoadPolicyForSubdocument(const GURL& subdocument_url,
91 LoadPolicyCallback result_callback);
92
93 // Invokes |first_disallowed_load_callback|, if necessary, and posts a task to
94 // call DocumentSubresourceFilter::reportDisallowedCallback() on the
95 // |task_runner|.
96 void ReportDisallowedLoad();
97
98 private:
99 // Note: Raw pointer, |core_| already holds a reference to |task_runner_|.
100 base::SequencedTaskRunner* task_runner_;
101 std::unique_ptr<Core, base::OnTaskRunnerDeleter> core_;
102 base::OnceClosure first_disallowed_load_callback_;
103
104 base::ThreadChecker thread_checker_;
105
106 DISALLOW_COPY_AND_ASSIGN(AsyncDocumentSubresourceFilter);
107 };
108
109 // Holds a DocumentSubresourceFilter (after Initialization, in case there is a
110 // valid MemoryMappedRuleset), and provides access to it.
111 //
112 // Initially holds an empty filter, allowing this object to be created on the
113 // UI thread synchronously, hence letting the ADSF to be created synchronously
114 // and be immediately used by clients on the UI thread, while the DSF is
115 // retrieved on the |task_runner| in a deferred manner.
116 class AsyncDocumentSubresourceFilter::Core {
117 public:
118 Core();
119 ~Core();
120
121 // Can return nullptr even after initialization in case MemoryMappedRuleset
122 // was not present, or was malformed during it.
123 DocumentSubresourceFilter* filter() {
124 DCHECK(thread_checker_.CalledOnValidThread());
125 return filter_ ? &filter_.value() : nullptr;
126 }
127
128 private:
129 friend class AsyncDocumentSubresourceFilter;
130
131 // Computes ActivationState from |params| and initializes a DSF using it.
132 // Returns the computed activation state.
133 ActivationState Initialize(InitializationParams params,
134 VerifiedRuleset* verified_ruleset);
135
136 base::Optional<DocumentSubresourceFilter> filter_;
137 base::ThreadChecker thread_checker_;
138
139 DISALLOW_COPY_AND_ASSIGN(Core);
140 };
141
142 } // namespace subresource_filter
143
144 #endif // COMPONENTS_SUBRESOURCE_FILTER_CONTENT_BROWSER_ASYNC_DOCUMENT_SUBRESOU RCE_FILTER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698