Commit 1eef7a03 authored by Karan Bhatia's avatar Karan Bhatia Committed by Commit Bot

DNR: Introduce RulesetMatcherInterface, ExtensionUrlPatternIndexMatcher.

This is a purely refactoring CL. We are adding support for regex rules to DNR.
Currently RulesetMatcher is tasked with evaluating a single ruleset source (An
extension can have upto two ruleset sources: static and dynamic). RulesetMatcher
implements matching of filter list style rules supported by url_pattern_index.

This CL does the following:
- Introduce an interface called RulesetMatcherInterface.
- Introduce ExtensionUrlPatternIndexMatcher: this will handle all filter list
  style rules within a ruleset.
- Move most of the implementation from RulesetMatcher to
  ExtensionUrlPatternIndexMatcher. After this CL RulesetMatcher is just a
  wrapper over ExtensionUrlPatternIndexMatcher.
- A subsequent CL will
   - Introduce RegexRulesMatcher which will handle all regex rules within a
     ruleset. It will also implement the RulesetMatcherInterface.
   - RulesetMatcher owns a ExtensionUrlPatternIndexMatcher currently.
     Subsequently it will also own a RegexRulesMatcher and combine the results
     from the two.

BUG=974391
Doc=https://docs.google.com/document/d/1mRErUMII_gSSPaHmxyn31UOYWUaZLj0xOaezekxD2-Y/edit?usp=sharing (Internal)

Change-Id: I580be59c8b19840622c2c673338c0f54087f5186
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1879565Reviewed-by: default avatarIstiaque Ahmed <lazyboy@chromium.org>
Commit-Queue: Karan Bhatia <karandeepb@chromium.org>
Cr-Commit-Position: refs/heads/master@{#710611}
parent 2ff4948f
......@@ -12,6 +12,8 @@ source_set("declarative_net_request") {
"constants.h",
"declarative_net_request_api.cc",
"declarative_net_request_api.h",
"extension_url_pattern_index_matcher.cc",
"extension_url_pattern_index_matcher.h",
"file_sequence_helper.cc",
"file_sequence_helper.h",
"flat_ruleset_indexer.cc",
......@@ -30,6 +32,7 @@ source_set("declarative_net_request") {
"ruleset_manager.h",
"ruleset_matcher.cc",
"ruleset_matcher.h",
"ruleset_matcher_interface.h",
"ruleset_source.cc",
"ruleset_source.h",
"utils.cc",
......
// Copyright 2019 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 EXTENSIONS_BROWSER_API_DECLARATIVE_NET_REQUEST_EXTENSION_URL_PATTERN_INDEX_MATCHER_H_
#define EXTENSIONS_BROWSER_API_DECLARATIVE_NET_REQUEST_EXTENSION_URL_PATTERN_INDEX_MATCHER_H_
#include <vector>
#include "components/url_pattern_index/url_pattern_index.h"
#include "extensions/browser/api/declarative_net_request/flat/extension_ruleset_generated.h"
#include "extensions/browser/api/declarative_net_request/ruleset_matcher_interface.h"
namespace extensions {
namespace declarative_net_request {
// ExtensionUrlPatternIndexMatcher is an implementation detail of
// RulesetMatcher. It deals with matching of filter list style rules. This uses
// the url_pattern_index component to achieve fast matching of network requests
// against declarative rules.
class ExtensionUrlPatternIndexMatcher final : public RulesetMatcherInterface {
public:
using UrlPatternIndexList = flatbuffers::Vector<
flatbuffers::Offset<url_pattern_index::flat::UrlPatternIndex>>;
using ExtensionMetadataList =
flatbuffers::Vector<flatbuffers::Offset<flat::UrlRuleMetadata>>;
ExtensionUrlPatternIndexMatcher(
const ExtensionId& extension_id,
api::declarative_net_request::SourceType source_type,
const UrlPatternIndexList* index_list,
const ExtensionMetadataList* metadata_list);
// RulesetMatcherInterface override:
~ExtensionUrlPatternIndexMatcher() override;
base::Optional<RequestAction> GetBlockOrCollapseAction(
const RequestParams& params) const override;
bool HasMatchingAllowRule(const RequestParams& params) const override;
base::Optional<RequestAction> GetRedirectAction(
const RequestParams& params) const override;
base::Optional<RequestAction> GetUpgradeAction(
const RequestParams& params) const override;
base::Optional<RequestAction> GetRedirectOrUpgradeActionByPriority(
const RequestParams& params) const override;
uint8_t GetRemoveHeadersMask(
const RequestParams& params,
uint8_t ignored_mask,
std::vector<RequestAction>* remove_headers_actions) const override;
bool IsExtraHeadersMatcher() const override {
return is_extra_headers_matcher_;
}
const ExtensionId& extension_id() const override { return extension_id_; }
api::declarative_net_request::SourceType source_type() const override {
return source_type_;
}
private:
using UrlPatternIndexMatcher = url_pattern_index::UrlPatternIndexMatcher;
// Returns the ruleset's matching redirect rule and populates
// |redirect_url| if there is a matching redirect rule, otherwise returns
// nullptr.
const url_pattern_index::flat::UrlRule* GetRedirectRule(
const RequestParams& params,
GURL* redirect_url) const;
// Returns the ruleset's matching upgrade scheme rule or nullptr if no
// matching rule is found or if the request's scheme is not upgradeable.
const url_pattern_index::flat::UrlRule* GetUpgradeRule(
const RequestParams& params) const;
const url_pattern_index::flat::UrlRule* GetMatchingRule(
const RequestParams& params,
flat::ActionIndex index,
UrlPatternIndexMatcher::FindRuleStrategy strategy =
UrlPatternIndexMatcher::FindRuleStrategy::kAny) const;
const ExtensionId extension_id_;
const api::declarative_net_request::SourceType source_type_;
const ExtensionMetadataList* const metadata_list_;
// UrlPatternIndexMatchers corresponding to entries in flat::ActionIndex.
const std::vector<UrlPatternIndexMatcher> matchers_;
const bool is_extra_headers_matcher_;
DISALLOW_COPY_AND_ASSIGN(ExtensionUrlPatternIndexMatcher);
};
} // namespace declarative_net_request
} // namespace extensions
#endif // EXTENSIONS_BROWSER_API_DECLARATIVE_NET_REQUEST_EXTENSION_URL_PATTERN_INDEX_MATCHER_H_
......@@ -8,21 +8,14 @@
#include <cstdint>
#include <memory>
#include <string>
#include <vector>
#include "base/macros.h"
#include "base/optional.h"
#include "components/url_pattern_index/url_pattern_index.h"
#include "extensions/browser/api/declarative_net_request/extension_url_pattern_index_matcher.h"
#include "extensions/browser/api/declarative_net_request/flat/extension_ruleset_generated.h"
#include "extensions/common/api/declarative_net_request.h"
#include "extensions/common/extension_id.h"
#include "extensions/browser/api/declarative_net_request/ruleset_matcher_interface.h"
class GURL;
namespace extensions {
namespace declarative_net_request {
struct RequestAction;
struct RequestParams;
class RulesetSource;
namespace flat {
......@@ -31,10 +24,9 @@ struct UrlRuleMetadata;
} // namespace flat
// RulesetMatcher encapsulates the Declarative Net Request API ruleset
// corresponding to a single RulesetSource. This uses the url_pattern_index
// component to achieve fast matching of network requests against declarative
// rules. Since this class is immutable, it is thread-safe.
class RulesetMatcher {
// corresponding to a single RulesetSource. Since this class is immutable, it is
// thread-safe.
class RulesetMatcher : public RulesetMatcherInterface {
public:
// Describes the result of creating a RulesetMatcher instance.
// This is logged as part of UMA. Hence existing values should not be re-
......@@ -71,45 +63,27 @@ class RulesetMatcher {
int expected_ruleset_checksum,
std::unique_ptr<RulesetMatcher>* matcher);
~RulesetMatcher();
// RulesetMatcherInterface overrides:
~RulesetMatcher() override;
// Returns the ruleset's matching RequestAction with type |BLOCK| or
// |COLLAPSE|, or base::nullopt if the ruleset has no matching blocking rule.
base::Optional<RequestAction> GetBlockOrCollapseAction(
const RequestParams& params) const;
// Returns whether the ruleset has a matching allow rule.
bool HasMatchingAllowRule(const RequestParams& params) const {
return GetMatchingRule(params, flat::ActionIndex_allow);
}
// Returns the ruleset's matching redirect RequestAction if there is a
// matching redirect rule, otherwise returns base::nullopt.
const RequestParams& params) const override;
bool HasMatchingAllowRule(const RequestParams& params) const override;
base::Optional<RequestAction> GetRedirectAction(
const RequestParams& params) const;
// Returns the ruleset's matching RequestAction with |params.url| upgraded to
// HTTPS as the redirect url, or base::nullopt if no matching rule is found or
// if the request's scheme is not upgradeable.
const RequestParams& params) const override;
base::Optional<RequestAction> GetUpgradeAction(
const RequestParams& params) const;
// Returns a RedirectAction constructed from the matching redirect or upgrade
// rule with the highest priority, or base::nullopt if no matching redirect or
// upgrade rules are found for this request.
const RequestParams& params) const override;
base::Optional<RequestAction> GetRedirectOrUpgradeActionByPriority(
const RequestParams& params) const;
// Returns the bitmask of headers to remove from the request. The bitmask
// corresponds to RemoveHeadersMask type. |ignored_mask| denotes the mask of
// headers to be skipped for evaluation and is excluded in the return value.
const RequestParams& params) const override;
uint8_t GetRemoveHeadersMask(
const RequestParams& params,
uint8_t ignored_mask,
std::vector<RequestAction>* remove_headers_actions) const;
// Returns whether this modifies "extraHeaders".
bool IsExtraHeadersMatcher() const { return is_extra_headers_matcher_; }
std::vector<RequestAction>* remove_headers_actions) const override;
bool IsExtraHeadersMatcher() const override;
const ExtensionId& extension_id() const override { return extension_id_; }
api::declarative_net_request::SourceType source_type() const override {
return source_type_;
}
// ID of the ruleset. Each extension can have multiple rulesets with
// their own unique ids.
......@@ -120,43 +94,16 @@ class RulesetMatcher {
size_t priority() const { return priority_; }
private:
using UrlPatternIndexMatcher = url_pattern_index::UrlPatternIndexMatcher;
using ExtensionMetadataList =
flatbuffers::Vector<flatbuffers::Offset<flat::UrlRuleMetadata>>;
explicit RulesetMatcher(std::string ruleset_data,
size_t id,
size_t priority,
api::declarative_net_request::SourceType source_type,
const ExtensionId& extension_id);
// Returns the ruleset's matching redirect rule and populates
// |redirect_url| if there is a matching redirect rule, otherwise returns
// nullptr.
const url_pattern_index::flat::UrlRule* GetRedirectRule(
const RequestParams& params,
GURL* redirect_url) const;
// Returns the ruleset's matching upgrade scheme rule or nullptr if no
// matching rule is found or if the request's scheme is not upgradeable.
const url_pattern_index::flat::UrlRule* GetUpgradeRule(
const RequestParams& params) const;
const url_pattern_index::flat::UrlRule* GetMatchingRule(
const RequestParams& params,
flat::ActionIndex index,
UrlPatternIndexMatcher::FindRuleStrategy strategy =
UrlPatternIndexMatcher::FindRuleStrategy::kAny) const;
const std::string ruleset_data_;
const flat::ExtensionIndexedRuleset* const root_;
// UrlPatternIndexMatchers corresponding to entries in flat::ActionIndex.
const std::vector<UrlPatternIndexMatcher> matchers_;
const ExtensionMetadataList* const metadata_list_;
const size_t id_;
const size_t priority_;
......@@ -165,7 +112,9 @@ class RulesetMatcher {
// The ID of the extension from which this matcher's ruleset originates from.
const ExtensionId extension_id_;
const bool is_extra_headers_matcher_;
// Underlying matcher for filter-list style rules supported using the
// |url_pattern_index| component.
const ExtensionUrlPatternIndexMatcher url_pattern_index_matcher_;
DISALLOW_COPY_AND_ASSIGN(RulesetMatcher);
};
......
// Copyright 2019 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 EXTENSIONS_BROWSER_API_DECLARATIVE_NET_REQUEST_RULESET_MATCHER_INTERFACE_H_
#define EXTENSIONS_BROWSER_API_DECLARATIVE_NET_REQUEST_RULESET_MATCHER_INTERFACE_H_
#include <vector>
#include "base/optional.h"
#include "extensions/common/api/declarative_net_request.h"
#include "extensions/common/extension_id.h"
namespace extensions {
namespace declarative_net_request {
struct RequestAction;
struct RequestParams;
// An abstract interface for rule matchers. Overridden by different kinds of
// matchers, e.g. filter lists and regex.
class RulesetMatcherInterface {
public:
virtual ~RulesetMatcherInterface() = default;
// Returns the ruleset's matching RequestAction with type |BLOCK| or
// |COLLAPSE|, or base::nullopt if the ruleset has no matching blocking rule.
virtual base::Optional<RequestAction> GetBlockOrCollapseAction(
const RequestParams& params) const = 0;
// Returns whether the ruleset has a matching allow rule.
virtual bool HasMatchingAllowRule(const RequestParams& params) const = 0;
// Returns the ruleset's matching redirect RequestAction if there is a
// matching redirect rule, otherwise returns base::nullopt.
virtual base::Optional<RequestAction> GetRedirectAction(
const RequestParams& params) const = 0;
// Returns the ruleset's matching RequestAction with |params.url| upgraded to
// HTTPS as the redirect url, or base::nullopt if no matching rule is found or
// if the request's scheme is not upgradeable.
virtual base::Optional<RequestAction> GetUpgradeAction(
const RequestParams& params) const = 0;
// Returns a RedirectAction constructed from the matching redirect or upgrade
// rule with the highest priority, or base::nullopt if no matching redirect or
// upgrade rules are found for this request.
virtual base::Optional<RequestAction> GetRedirectOrUpgradeActionByPriority(
const RequestParams& params) const = 0;
// Returns the bitmask of headers to remove from the request. The bitmask
// corresponds to RemoveHeadersMask type. |ignored_mask| denotes the mask of
// headers to be skipped for evaluation and is excluded in the return value.
virtual uint8_t GetRemoveHeadersMask(
const RequestParams& params,
uint8_t ignored_mask,
std::vector<RequestAction>* remove_headers_actions) const = 0;
// Returns whether this modifies "extraHeaders".
virtual bool IsExtraHeadersMatcher() const = 0;
// Returns the extension ID with which this matcher is associated.
virtual const ExtensionId& extension_id() const = 0;
// The source type of the matcher.
virtual api::declarative_net_request::SourceType source_type() const = 0;
};
} // namespace declarative_net_request
} // namespace extensions
#endif // EXTENSIONS_BROWSER_API_DECLARATIVE_NET_REQUEST_RULESET_MATCHER_INTERFACE_H_
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment