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