Chromium Code Reviews| Index: components/subresource_filter/content/browser/async_document_subresource_filter.h |
| diff --git a/components/subresource_filter/content/browser/async_document_subresource_filter.h b/components/subresource_filter/content/browser/async_document_subresource_filter.h |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..4103206a59e78f99a53f5c8bc74fb36e0411058e |
| --- /dev/null |
| +++ b/components/subresource_filter/content/browser/async_document_subresource_filter.h |
| @@ -0,0 +1,121 @@ |
| +// Copyright 2017 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#ifndef COMPONENTS_SUBRESOURCE_FILTER_CONTENT_BROWSER_ASYNC_DOCUMENT_SUBRESOURCE_FILTER_H_ |
| +#define COMPONENTS_SUBRESOURCE_FILTER_CONTENT_BROWSER_ASYNC_DOCUMENT_SUBRESOURCE_FILTER_H_ |
| + |
| +#include <memory> |
| + |
| +#include "base/callback_forward.h" |
| +#include "base/macros.h" |
| +#include "base/optional.h" |
| +#include "base/sequenced_task_runner.h" |
| +#include "base/threading/thread_checker.h" |
| +#include "components/subresource_filter/content/browser/verified_ruleset_dealer.h" |
| +#include "components/subresource_filter/content/common/document_subresource_filter.h" |
| +#include "third_party/WebKit/public/platform/WebDocumentSubresourceFilter.h" |
| + |
| +namespace subresource_filter { |
| + |
| +// 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.
|
| +// belongs to UI thread and owns an ADSF::Core living on a dedicated sequenced |
| +// |task_runner|. Provides asynchronous access to the Core, and destroys it |
| +// asynchronously. |
| +class AsyncDocumentSubresourceFilter { |
| + public: |
| + class Core; |
| + |
| + // Parameters posted to ADSF::Core during its initialization. Needed for |
| + // 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.
|
| + // DocumentSubresourceFilter. |
| + struct InitializationParams { |
| + InitializationParams(); |
| + ~InitializationParams(); |
| + |
| + InitializationParams(InitializationParams&& other); |
| + InitializationParams& operator=(InitializationParams&& other); |
| + |
| + // Parameters used to compute ActivationState for the |document| before |
| + // creating a DocumentSubresourceFilter. |
| + GURL document_url; |
| + url::Origin parent_document_origin; |
| + ActivationState parent_activation_state; |
| + |
| + // A callback passed to DocumentSubresourceFilter, and used by it to report |
| + // 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
|
| + base::OnceClosure first_disallowed_load_callback; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(InitializationParams); |
| + }; |
| + |
| + using LoadPolicy = blink::WebDocumentSubresourceFilter::LoadPolicy; |
| + using LoadPolicyCallback = base::Callback<void(LoadPolicy)>; |
| + |
| + // Creates a Core and initializes it asynchronously on a |task_runner| using |
| + // the supplied initialization |params| and a VerifiedRuleset taken from the |
| + // |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.
|
| + // and accessed on the |task_runner|. |
| + // |
| + // 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.
|
| + // thread. If MemoryMappedRuleset is not present or malformed, then a default |
| + // created ActivationState is reported (with ActivationLevel == DISABLED). |
| + AsyncDocumentSubresourceFilter( |
| + VerifiedRuleset::Handle* ruleset_handle, |
| + InitializationParams params, |
| + base::Callback<void(ActivationState)> activation_state_callback); |
| + |
| + ~AsyncDocumentSubresourceFilter(); |
| + |
| + // 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.
|
| + // thread via |result_callback|. If MemoryMappedRuleset is not present or |
| + // malformed, then a LoadPolicy::Allow is returned. |
| + void GetLoadPolicyForSubdocument(const GURL& subdocument_url, |
| + LoadPolicyCallback result_callback); |
| + |
| + private: |
| + // Note: Raw pointer, |core_| already holds a reference to |task_runner_|. |
| + base::SequencedTaskRunner* task_runner_; |
| + std::unique_ptr<Core, base::OnTaskRunnerDeleter> core_; |
| + |
| + base::ThreadChecker thread_checker_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(AsyncDocumentSubresourceFilter); |
| +}; |
| + |
| +// Holds a DocumentSubresourceFilter (after Initialization, in case there is a |
| +// valid MemoryMappedRuleset), and provides access to it. |
| +// |
| +// Initially holds an empty filter, allowing this object to be created on the |
| +// UI thread synchronously, hence letting the ADSF to be created synchronously |
| +// and be immediately used by clients on the UI thread, while the DSF is |
| +// retrieved on the |task_runner| in a deferred manner. |
| +class AsyncDocumentSubresourceFilter::Core { |
| + public: |
| + Core(); |
| + ~Core(); |
| + |
| + // 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?
|
| + // created because MemoryMappedRuleset is not present or malformed. |
| + DocumentSubresourceFilter* filter() { |
| + DCHECK(thread_checker_.CalledOnValidThread()); |
| + return filter_ ? &filter_.value() : nullptr; |
| + } |
| + |
| + private: |
| + friend class AsyncDocumentSubresourceFilter; |
| + |
| + // Computes ActivationState from |params| and initializes a DSF using it. |
| + // Returns the computed activation state. |
| + ActivationState Initialize(InitializationParams params, |
| + VerifiedRuleset* verified_ruleset); |
| + |
| + base::Optional<DocumentSubresourceFilter> filter_; |
| + base::ThreadChecker thread_checker_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(Core); |
| +}; |
| + |
| +} // namespace subresource_filter |
| + |
| +#endif // COMPONENTS_SUBRESOURCE_FILTER_CONTENT_BROWSER_ASYNC_DOCUMENT_SUBRESOURCE_FILTER_H_ |