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 "third_party/WebKit/public/platform/WebDocumentSubresourceFilter.h" | |
18 | |
19 namespace subresource_filter { | |
20 | |
21 // ADSF is an asynchronous version of DocumentSubresourceFilter. Normally it | |
engedy
2017/02/10 14:31:59
Phrasing nit:
An asynchronous wrapper around Docu
pkalinnikov
2017/02/10 15:15:14
Done.
| |
22 // belongs to UI thread and owns an ADSF::Core living on a dedicated sequenced | |
23 // |task_runner|. Provides asynchronous access to the Core, and destroys it | |
24 // asynchronously. | |
25 class AsyncDocumentSubresourceFilter { | |
26 public: | |
27 class Core; | |
28 | |
29 // Parameters posted to ADSF::Core during its initialization. Needed for | |
30 // computing ActivationState for a document, and creating a corresponding | |
engedy
2017/02/10 14:31:58
nit: and for
pkalinnikov
2017/02/10 15:15:14
Done.
| |
31 // DocumentSubresourceFilter. | |
32 struct InitializationParams { | |
33 InitializationParams(); | |
34 ~InitializationParams(); | |
35 | |
36 InitializationParams(InitializationParams&& other); | |
37 InitializationParams& operator=(InitializationParams&& other); | |
38 | |
39 // Parameters used to compute ActivationState for the |document| before | |
40 // creating a DocumentSubresourceFilter. | |
41 GURL document_url; | |
42 url::Origin parent_document_origin; | |
43 ActivationState parent_activation_state; | |
44 | |
45 // A callback passed to DocumentSubresourceFilter, and used by it to report | |
46 // the first disallowed load. Called at most once, on |task_runner|. | |
engedy
2017/02/10 14:31:59
Hang on, calling this on the |task_runner| doesn't
pkalinnikov
2017/02/10 15:15:14
Now the call is redirected to the current thread t
| |
47 base::OnceClosure first_disallowed_load_callback; | |
48 | |
49 DISALLOW_COPY_AND_ASSIGN(InitializationParams); | |
50 }; | |
51 | |
52 using LoadPolicy = blink::WebDocumentSubresourceFilter::LoadPolicy; | |
53 using LoadPolicyCallback = base::Callback<void(LoadPolicy)>; | |
54 | |
55 // Creates a Core and initializes it asynchronously on a |task_runner| using | |
56 // the supplied initialization |params| and a VerifiedRuleset taken from the | |
57 // |ruleset_handle|. The instance remains owned by |this| object, but living | |
engedy
2017/02/10 14:31:58
nit: but lives on (and is accessed on)
pkalinnikov
2017/02/10 15:15:14
Done.
| |
58 // and accessed on the |task_runner|. | |
59 // | |
60 // Reports ActivationState via |activation_state_callback| on the current | |
engedy
2017/02/10 14:31:59
nit: Once the ActivationState for the current fram
pkalinnikov
2017/02/10 15:15:14
Done.
| |
61 // thread. If MemoryMappedRuleset is not present or malformed, then a default | |
62 // created ActivationState is reported (with ActivationLevel == DISABLED). | |
63 AsyncDocumentSubresourceFilter( | |
64 VerifiedRuleset::Handle* ruleset_handle, | |
65 InitializationParams params, | |
66 base::Callback<void(ActivationState)> activation_state_callback); | |
67 | |
68 ~AsyncDocumentSubresourceFilter(); | |
69 | |
70 // Computes LoadPolicy on a |task_runner| and returns it back to the calling | |
engedy
2017/02/10 14:31:59
nit: Determines the LoadPolicy for the given |subd
pkalinnikov
2017/02/10 15:15:14
Done.
| |
71 // thread via |result_callback|. If MemoryMappedRuleset is not present or | |
72 // malformed, then a LoadPolicy::Allow is returned. | |
73 void GetLoadPolicyForSubdocument(const GURL& subdocument_url, | |
74 LoadPolicyCallback result_callback); | |
75 | |
76 private: | |
77 // Note: Raw pointer, |core_| already holds a reference to |task_runner_|. | |
78 base::SequencedTaskRunner* task_runner_; | |
79 std::unique_ptr<Core, base::OnTaskRunnerDeleter> core_; | |
80 | |
81 base::ThreadChecker thread_checker_; | |
82 | |
83 DISALLOW_COPY_AND_ASSIGN(AsyncDocumentSubresourceFilter); | |
84 }; | |
85 | |
86 // Holds a DocumentSubresourceFilter (after Initialization, in case there is a | |
87 // valid MemoryMappedRuleset), and provides access to it. | |
88 // | |
89 // Initially holds an empty filter, allowing this object to be created on the | |
90 // UI thread synchronously, hence letting the ADSF to be created synchronously | |
91 // and be immediately used by clients on the UI thread, while the DSF is | |
92 // retrieved on the |task_runner| in a deferred manner. | |
93 class AsyncDocumentSubresourceFilter::Core { | |
94 public: | |
95 Core(); | |
96 ~Core(); | |
97 | |
98 // Can return nullptr even after initialization in case DSF could not be | |
engedy
2017/02/10 14:31:58
nit: Let's try to avoid these non-standard abbrevi
pkalinnikov
2017/02/10 15:15:14
How about this?
| |
99 // created because MemoryMappedRuleset is not present or malformed. | |
100 DocumentSubresourceFilter* filter() { | |
101 DCHECK(thread_checker_.CalledOnValidThread()); | |
102 return filter_ ? &filter_.value() : nullptr; | |
103 } | |
104 | |
105 private: | |
106 friend class AsyncDocumentSubresourceFilter; | |
107 | |
108 // Computes ActivationState from |params| and initializes a DSF using it. | |
109 // Returns the computed activation state. | |
110 ActivationState Initialize(InitializationParams params, | |
111 VerifiedRuleset* verified_ruleset); | |
112 | |
113 base::Optional<DocumentSubresourceFilter> filter_; | |
114 base::ThreadChecker thread_checker_; | |
115 | |
116 DISALLOW_COPY_AND_ASSIGN(Core); | |
117 }; | |
118 | |
119 } // namespace subresource_filter | |
120 | |
121 #endif // COMPONENTS_SUBRESOURCE_FILTER_CONTENT_BROWSER_ASYNC_DOCUMENT_SUBRESOU RCE_FILTER_H_ | |
OLD | NEW |