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