| 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..86542884e11c5e0a8bda4a6da2e57bf092b9b2c3
|
| --- /dev/null
|
| +++ b/components/subresource_filter/content/browser/async_document_subresource_filter.h
|
| @@ -0,0 +1,144 @@
|
| +// 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 "components/subresource_filter/core/common/activation_level.h"
|
| +#include "components/subresource_filter/core/common/activation_state.h"
|
| +#include "third_party/WebKit/public/platform/WebDocumentSubresourceFilter.h"
|
| +
|
| +namespace subresource_filter {
|
| +
|
| +// An asynchronous wrapper around DocumentSubresourceFilter.
|
| +//
|
| +// It is accessed on the 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:
|
| + using LoadPolicy = blink::WebDocumentSubresourceFilter::LoadPolicy;
|
| + using LoadPolicyCallback = base::Callback<void(LoadPolicy)>;
|
| +
|
| + class Core;
|
| +
|
| + // Parameters posted to ADSF::Core during its initialization. Needed for
|
| + // computing ActivationState for a (sub-)document, and for creating a
|
| + // corresponding DocumentSubresourceFilter.
|
| + struct InitializationParams {
|
| + InitializationParams();
|
| +
|
| + // Creates parameters for a main-frame |document|.
|
| + InitializationParams(GURL document_url,
|
| + ActivationLevel activation_level,
|
| + bool measure_performance);
|
| +
|
| + // Creates parameters for a subframe |document|.
|
| + InitializationParams(GURL document_url,
|
| + url::Origin parent_document_origin,
|
| + ActivationState parent_activation_state);
|
| +
|
| + ~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;
|
| +
|
| + private:
|
| + DISALLOW_COPY_AND_ASSIGN(InitializationParams);
|
| + };
|
| +
|
| + // 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 lives on
|
| + // (and is accessed on) the |task_runner|.
|
| + //
|
| + // Once the ActivationState for the current frame is calculated, it is
|
| + // reported back via |activation_state_callback| on the task runner associated
|
| + // with the current thread. If MemoryMappedRuleset is not present or
|
| + // malformed, then a default ActivationState is reported (with ActivationLevel
|
| + // equal to DISABLED).
|
| + //
|
| + // The |first_disallowed_load_callback|, if it is non-null, is invoked on the
|
| + // first ReportDisallowedLoad() call.
|
| + AsyncDocumentSubresourceFilter(
|
| + VerifiedRuleset::Handle* ruleset_handle,
|
| + InitializationParams params,
|
| + base::Callback<void(ActivationState)> activation_state_callback,
|
| + base::OnceClosure first_disallowed_load_callback);
|
| +
|
| + ~AsyncDocumentSubresourceFilter();
|
| +
|
| + // Computes LoadPolicy on a |task_runner| and returns it back to the calling
|
| + // 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);
|
| +
|
| + // Invokes |first_disallowed_load_callback|, if necessary, and posts a task to
|
| + // call DocumentSubresourceFilter::reportDisallowedCallback() on the
|
| + // |task_runner|.
|
| + void ReportDisallowedLoad();
|
| +
|
| + private:
|
| + // Note: Raw pointer, |core_| already holds a reference to |task_runner_|.
|
| + base::SequencedTaskRunner* task_runner_;
|
| + std::unique_ptr<Core, base::OnTaskRunnerDeleter> core_;
|
| + base::OnceClosure first_disallowed_load_callback_;
|
| +
|
| + 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 MemoryMappedRuleset
|
| + // was not present, or was malformed during it.
|
| + 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_
|
|
|