From d797063ae13856497560195cc6a7cf5dcd7c2667 Mon Sep 17 00:00:00 2001 From: Pavel Kalinnikov Date: Tue, 20 Jun 2017 11:07:34 +0200 Subject: [PATCH] Create a separate component for UrlPatternIndex. Bug: 713774 Change-Id: I12a70bc0b5caa37470ecf53568d90304c05095eb Reviewed-on: https://chromium-review.googlesource.com/527445 Commit-Queue: Pavel Kalinnikov Reviewed-by: Jochen Eisinger Reviewed-by: Charlie Harrison Cr-Commit-Position: refs/heads/master@{#481360} --- .../subresource_filter_browsertest.cc | 3 + components/BUILD.gn | 1 + components/subresource_filter/DEPS | 1 + ..._computing_navigation_throttle_unittest.cc | 4 +- .../async_document_subresource_filter.cc | 10 +-- ...nc_document_subresource_filter_unittest.cc | 4 +- ...source_filter_throttle_manager_unittest.cc | 4 +- ...e_browsing_activation_throttle_unittest.cc | 3 +- .../web_document_subresource_filter_impl.cc | 2 + .../web_document_subresource_filter_impl.h | 7 +- .../subresource_filter/core/browser/BUILD.gn | 4 +- .../core/browser/ruleset_service.cc | 15 ++-- .../core/browser/ruleset_service_unittest.cc | 4 +- .../subresource_filter/core/common/BUILD.gn | 30 ++------ .../core/common/PRESUBMIT.py | 19 ++--- .../common/document_subresource_filter.cc | 2 +- .../core/common/document_subresource_filter.h | 7 +- .../document_subresource_filter_unittest.cc | 2 + .../core/common/flat/BUILD.gn | 6 +- .../core/common/flat/indexed_ruleset.fbs | 8 +-- .../core/common/indexed_ruleset.cc | 4 +- .../core/common/indexed_ruleset.h | 37 +++++----- .../core/common/indexed_ruleset_unittest.cc | 37 ++++++---- .../core/common/test_ruleset_creator.cc | 8 ++- .../core/common/test_ruleset_creator.h | 12 ++-- .../core/common/test_ruleset_utils.cc | 2 + .../core/common/test_ruleset_utils.h | 9 +-- components/url_pattern_index/BUILD.gn | 70 +++++++++++++++++++ components/url_pattern_index/DEPS | 5 ++ components/url_pattern_index/OWNERS | 3 + components/url_pattern_index/README | 12 ++++ .../closed_hash_map.h | 25 +++---- .../closed_hash_map_unittest.cc | 8 +-- .../copying_file_stream.cc | 6 +- .../copying_file_stream.h | 10 +-- components/url_pattern_index/flat/BUILD.gn | 11 +++ .../flat/url_pattern_index.fbs | 10 +-- .../fuzzy_pattern_matching.cc | 6 +- .../fuzzy_pattern_matching.h | 10 +-- .../fuzzy_pattern_matching_unittest.cc | 6 +- .../ngram_extractor.h | 10 +-- .../ngram_extractor_unittest.cc | 6 +- .../proto/BUILD.gn | 2 +- .../proto/rules.proto | 4 +- .../string_splitter.h | 10 +-- .../string_splitter_unittest.cc | 6 +- .../uint64_hasher.h | 10 +-- .../unindexed_ruleset.cc | 6 +- .../unindexed_ruleset.h | 12 ++-- .../unindexed_ruleset_unittest.cc | 19 +++-- .../url_pattern.cc | 12 ++-- .../url_pattern.h | 12 ++-- .../url_pattern_index.cc | 10 +-- .../url_pattern_index.h | 18 ++--- .../url_pattern_index_unittest.cc | 10 +-- .../url_pattern_unittest.cc | 6 +- .../url_rule_test_support.cc | 6 +- .../url_rule_test_support.h | 14 ++-- third_party/flatbuffers/flatbuffer.gni | 6 ++ 59 files changed, 368 insertions(+), 238 deletions(-) create mode 100644 components/url_pattern_index/BUILD.gn create mode 100644 components/url_pattern_index/DEPS create mode 100644 components/url_pattern_index/OWNERS create mode 100644 components/url_pattern_index/README rename components/{subresource_filter/core/common => url_pattern_index}/closed_hash_map.h (94%) rename components/{subresource_filter/core/common => url_pattern_index}/closed_hash_map_unittest.cc (95%) rename components/{subresource_filter/core/common => url_pattern_index}/copying_file_stream.cc (87%) rename components/{subresource_filter/core/common => url_pattern_index}/copying_file_stream.h (83%) create mode 100644 components/url_pattern_index/flat/BUILD.gn rename components/{subresource_filter/core/common => url_pattern_index}/flat/url_pattern_index.fbs (91%) rename components/{subresource_filter/core/common => url_pattern_index}/fuzzy_pattern_matching.cc (92%) rename components/{subresource_filter/core/common => url_pattern_index}/fuzzy_pattern_matching.h (87%) rename components/{subresource_filter/core/common => url_pattern_index}/fuzzy_pattern_matching_unittest.cc (96%) rename components/{subresource_filter/core/common => url_pattern_index}/ngram_extractor.h (94%) rename components/{subresource_filter/core/common => url_pattern_index}/ngram_extractor_unittest.cc (95%) rename components/{subresource_filter/core/common => url_pattern_index}/proto/BUILD.gn (87%) rename components/{subresource_filter/core/common => url_pattern_index}/proto/rules.proto (98%) rename components/{subresource_filter/core/common => url_pattern_index}/string_splitter.h (91%) rename components/{subresource_filter/core/common => url_pattern_index}/string_splitter_unittest.cc (94%) rename components/{subresource_filter/core/common => url_pattern_index}/uint64_hasher.h (84%) rename components/{subresource_filter/core/common => url_pattern_index}/unindexed_ruleset.cc (94%) rename components/{subresource_filter/core/common => url_pattern_index}/unindexed_ruleset.h (91%) rename components/{subresource_filter/core/common => url_pattern_index}/unindexed_ruleset_unittest.cc (92%) rename components/{subresource_filter/core/common => url_pattern_index}/url_pattern.cc (96%) rename components/{subresource_filter/core/common => url_pattern_index}/url_pattern.h (88%) rename components/{subresource_filter/core/common => url_pattern_index}/url_pattern_index.cc (98%) rename components/{subresource_filter/core/common => url_pattern_index}/url_pattern_index.h (90%) rename components/{subresource_filter/core/common => url_pattern_index}/url_pattern_index_unittest.cc (98%) rename components/{subresource_filter/core/common => url_pattern_index}/url_pattern_unittest.cc (97%) rename components/{subresource_filter/core/common => url_pattern_index}/url_rule_test_support.cc (92%) rename components/{subresource_filter/core/common => url_pattern_index}/url_rule_test_support.h (86%) diff --git a/chrome/browser/subresource_filter/subresource_filter_browsertest.cc b/chrome/browser/subresource_filter/subresource_filter_browsertest.cc index 8c3fa76e074969..4f3d6cef2c02fe 100644 --- a/chrome/browser/subresource_filter/subresource_filter_browsertest.cc +++ b/chrome/browser/subresource_filter/subresource_filter_browsertest.cc @@ -61,6 +61,7 @@ #include "components/subresource_filter/core/common/scoped_timers.h" #include "components/subresource_filter/core/common/test_ruleset_creator.h" #include "components/subresource_filter/core/common/test_ruleset_utils.h" +#include "components/url_pattern_index/proto/rules.pb.h" #include "content/public/browser/browser_thread.h" #include "content/public/browser/notification_service.h" #include "content/public/browser/notification_types.h" @@ -81,6 +82,8 @@ namespace { +namespace proto = url_pattern_index::proto; + // The path to a multi-frame document used for tests. static constexpr const char kTestFrameSetPath[] = "/subresource_filter/frame_set.html"; diff --git a/components/BUILD.gn b/components/BUILD.gn index 6189588440057d..97bb414d0e1cd2 100644 --- a/components/BUILD.gn +++ b/components/BUILD.gn @@ -151,6 +151,7 @@ test("components_unittests") { "//components/upload_list:unit_tests", "//components/url_formatter:unit_tests", "//components/url_matcher:unit_tests", + "//components/url_pattern_index:unit_tests", "//components/variations:unit_tests", "//components/variations/service:unit_tests", "//components/web_resource:unit_tests", diff --git a/components/subresource_filter/DEPS b/components/subresource_filter/DEPS index 39b3659501153c..1194dc13f3d83b 100644 --- a/components/subresource_filter/DEPS +++ b/components/subresource_filter/DEPS @@ -1,5 +1,6 @@ include_rules = [ "+components/prefs", + "+components/url_pattern_index", "+components/variations", # subresource_filter is a layered component; subdirectories must explicitly # introduce the ability to use non-core layers as appropriate. diff --git a/components/subresource_filter/content/browser/activation_state_computing_navigation_throttle_unittest.cc b/components/subresource_filter/content/browser/activation_state_computing_navigation_throttle_unittest.cc index 9b06df81a451a0..bd31786c028a73 100644 --- a/components/subresource_filter/content/browser/activation_state_computing_navigation_throttle_unittest.cc +++ b/components/subresource_filter/content/browser/activation_state_computing_navigation_throttle_unittest.cc @@ -20,9 +20,9 @@ #include "components/subresource_filter/content/browser/async_document_subresource_filter_test_utils.h" #include "components/subresource_filter/core/common/activation_level.h" #include "components/subresource_filter/core/common/activation_state.h" -#include "components/subresource_filter/core/common/proto/rules.pb.h" #include "components/subresource_filter/core/common/test_ruleset_creator.h" #include "components/subresource_filter/core/common/test_ruleset_utils.h" +#include "components/url_pattern_index/proto/rules.pb.h" #include "content/public/browser/navigation_handle.h" #include "content/public/browser/web_contents_observer.h" #include "content/public/test/navigation_simulator.h" @@ -31,6 +31,8 @@ namespace subresource_filter { +namespace proto = url_pattern_index::proto; + class ActivationStateComputingNavigationThrottleTest : public content::RenderViewHostTestHarness, public content::WebContentsObserver { diff --git a/components/subresource_filter/content/browser/async_document_subresource_filter.cc b/components/subresource_filter/content/browser/async_document_subresource_filter.cc index 3b0b7ccfd80ed8..8d1887fc7a9068 100644 --- a/components/subresource_filter/content/browser/async_document_subresource_filter.cc +++ b/components/subresource_filter/content/browser/async_document_subresource_filter.cc @@ -14,6 +14,7 @@ #include "base/threading/sequenced_task_runner_handle.h" #include "components/subresource_filter/core/common/memory_mapped_ruleset.h" #include "components/subresource_filter/core/common/time_measurements.h" +#include "components/url_pattern_index/proto/rules.pb.h" namespace subresource_filter { @@ -42,12 +43,12 @@ ActivationState ComputeActivationState( // TODO(pkalinnikov): Match several activation types in a batch. if (matcher.ShouldDisableFilteringForDocument( document_url, parent_document_origin, - proto::ACTIVATION_TYPE_DOCUMENT)) { + url_pattern_index::proto::ACTIVATION_TYPE_DOCUMENT)) { activation_state.filtering_disabled_for_document = true; } else if (!activation_state.generic_blocking_rules_disabled && matcher.ShouldDisableFilteringForDocument( document_url, parent_document_origin, - proto::ACTIVATION_TYPE_GENERICBLOCK)) { + url_pattern_index::proto::ACTIVATION_TYPE_GENERICBLOCK)) { activation_state.generic_blocking_rules_disabled = true; } return activation_state; @@ -136,8 +137,9 @@ void AsyncDocumentSubresourceFilter::GetLoadPolicyForSubdocument( DCHECK(core); DocumentSubresourceFilter* filter = core->filter(); return filter - ? filter->GetLoadPolicy(subdocument_url, - proto::ELEMENT_TYPE_SUBDOCUMENT) + ? filter->GetLoadPolicy( + subdocument_url, + url_pattern_index::proto::ELEMENT_TYPE_SUBDOCUMENT) : LoadPolicy::ALLOW; }, core_.get(), subdocument_url), diff --git a/components/subresource_filter/content/browser/async_document_subresource_filter_unittest.cc b/components/subresource_filter/content/browser/async_document_subresource_filter_unittest.cc index de949b4285aef2..6b9282d99808bd 100644 --- a/components/subresource_filter/content/browser/async_document_subresource_filter_unittest.cc +++ b/components/subresource_filter/content/browser/async_document_subresource_filter_unittest.cc @@ -18,13 +18,15 @@ #include "components/subresource_filter/content/browser/async_document_subresource_filter_test_utils.h" #include "components/subresource_filter/core/common/load_policy.h" #include "components/subresource_filter/core/common/memory_mapped_ruleset.h" -#include "components/subresource_filter/core/common/proto/rules.pb.h" #include "components/subresource_filter/core/common/test_ruleset_creator.h" #include "components/subresource_filter/core/common/test_ruleset_utils.h" +#include "components/url_pattern_index/proto/rules.pb.h" #include "testing/gtest/include/gtest/gtest.h" namespace subresource_filter { +namespace proto = url_pattern_index::proto; + class AsyncDocumentSubresourceFilterTest : public ::testing::Test { public: AsyncDocumentSubresourceFilterTest() = default; diff --git a/components/subresource_filter/content/browser/content_subresource_filter_throttle_manager_unittest.cc b/components/subresource_filter/content/browser/content_subresource_filter_throttle_manager_unittest.cc index de032cd9c77092..f415322bc3f659 100644 --- a/components/subresource_filter/content/browser/content_subresource_filter_throttle_manager_unittest.cc +++ b/components/subresource_filter/content/browser/content_subresource_filter_throttle_manager_unittest.cc @@ -21,9 +21,9 @@ #include "components/subresource_filter/content/common/subresource_filter_messages.h" #include "components/subresource_filter/core/common/activation_level.h" #include "components/subresource_filter/core/common/activation_state.h" -#include "components/subresource_filter/core/common/proto/rules.pb.h" #include "components/subresource_filter/core/common/test_ruleset_creator.h" #include "components/subresource_filter/core/common/test_ruleset_utils.h" +#include "components/url_pattern_index/proto/rules.pb.h" #include "content/public/browser/navigation_handle.h" #include "content/public/browser/navigation_throttle.h" #include "content/public/browser/web_contents.h" @@ -37,6 +37,8 @@ namespace subresource_filter { +namespace proto = url_pattern_index::proto; + const char kTestURLWithActivation[] = "https://www.page-with-activation.com/"; const char kTestURLWithActivation2[] = "https://www.page-with-activation-2.com/"; diff --git a/components/subresource_filter/content/browser/subresource_filter_safe_browsing_activation_throttle_unittest.cc b/components/subresource_filter/content/browser/subresource_filter_safe_browsing_activation_throttle_unittest.cc index 635b9fd3de5572..38c8ffec3bd021 100644 --- a/components/subresource_filter/content/browser/subresource_filter_safe_browsing_activation_throttle_unittest.cc +++ b/components/subresource_filter/content/browser/subresource_filter_safe_browsing_activation_throttle_unittest.cc @@ -31,6 +31,7 @@ #include "components/subresource_filter/core/common/activation_state.h" #include "components/subresource_filter/core/common/test_ruleset_creator.h" #include "components/subresource_filter/core/common/test_ruleset_utils.h" +#include "components/url_pattern_index/proto/rules.pb.h" #include "content/public/browser/browser_thread.h" #include "content/public/browser/navigation_handle.h" #include "content/public/browser/web_contents_observer.h" @@ -166,7 +167,7 @@ class SubresourceFilterSafeBrowsingActivationThrottleTest Configure(); test_io_task_runner_ = new base::TestMockTimeTaskRunner(); // Note: Using NiceMock to allow uninteresting calls and suppress warnings. - std::vector rules; + std::vector rules; rules.push_back(testing::CreateSuffixRule("disallowed.html")); ASSERT_NO_FATAL_FAILURE(test_ruleset_creator_.CreateRulesetWithRules( rules, &test_ruleset_pair_)); diff --git a/components/subresource_filter/content/renderer/web_document_subresource_filter_impl.cc b/components/subresource_filter/content/renderer/web_document_subresource_filter_impl.cc index efb514b72d38f5..88d5f1c4881ed4 100644 --- a/components/subresource_filter/content/renderer/web_document_subresource_filter_impl.cc +++ b/components/subresource_filter/content/renderer/web_document_subresource_filter_impl.cc @@ -17,6 +17,8 @@ namespace subresource_filter { +namespace proto = url_pattern_index::proto; + namespace { using WebLoadPolicy = blink::WebDocumentSubresourceFilter::LoadPolicy; diff --git a/components/subresource_filter/content/renderer/web_document_subresource_filter_impl.h b/components/subresource_filter/content/renderer/web_document_subresource_filter_impl.h index 9722aa46814251..179b0de64d718d 100644 --- a/components/subresource_filter/content/renderer/web_document_subresource_filter_impl.h +++ b/components/subresource_filter/content/renderer/web_document_subresource_filter_impl.h @@ -9,7 +9,7 @@ #include "base/macros.h" #include "base/memory/weak_ptr.h" #include "components/subresource_filter/core/common/document_subresource_filter.h" -#include "components/subresource_filter/core/common/proto/rules.pb.h" +#include "components/url_pattern_index/proto/rules.pb.h" #include "third_party/WebKit/public/platform/WebDocumentSubresourceFilter.h" namespace subresource_filter { @@ -44,8 +44,9 @@ class WebDocumentSubresourceFilterImpl bool ShouldLogToConsole() override; private: - LoadPolicy getLoadPolicyImpl(const blink::WebURL& url, - proto::ElementType element_type); + LoadPolicy getLoadPolicyImpl( + const blink::WebURL& url, + url_pattern_index::proto::ElementType element_type); DocumentSubresourceFilter filter_; base::OnceClosure first_disallowed_load_callback_; diff --git a/components/subresource_filter/core/browser/BUILD.gn b/components/subresource_filter/core/browser/BUILD.gn index 2e3e756b118034..a8536dc0694b45 100644 --- a/components/subresource_filter/core/browser/BUILD.gn +++ b/components/subresource_filter/core/browser/BUILD.gn @@ -14,7 +14,7 @@ static_library("browser") { ] deps = [ "//base", - "//components/prefs:prefs", + "//components/prefs", "//components/subresource_filter/core/common", "//components/variations", "//third_party/protobuf:protobuf_lite", @@ -50,7 +50,7 @@ source_set("unit_tests") { "//base/test:test_support", "//components/prefs:test_support", "//components/subresource_filter/core/common:test_support", - "//components/subresource_filter/core/common/proto", + "//components/url_pattern_index/proto:url_pattern_index", "//components/variations", "//testing/gmock", "//testing/gtest", diff --git a/components/subresource_filter/core/browser/ruleset_service.cc b/components/subresource_filter/core/browser/ruleset_service.cc index 46256729f5c002..ece8d8bf0a50a0 100644 --- a/components/subresource_filter/core/browser/ruleset_service.cc +++ b/components/subresource_filter/core/browser/ruleset_service.cc @@ -25,11 +25,11 @@ #include "components/prefs/pref_service.h" #include "components/subresource_filter/core/browser/ruleset_service_delegate.h" #include "components/subresource_filter/core/browser/subresource_filter_constants.h" -#include "components/subresource_filter/core/common/copying_file_stream.h" #include "components/subresource_filter/core/common/indexed_ruleset.h" -#include "components/subresource_filter/core/common/proto/rules.pb.h" #include "components/subresource_filter/core/common/time_measurements.h" -#include "components/subresource_filter/core/common/unindexed_ruleset.h" +#include "components/url_pattern_index/copying_file_stream.h" +#include "components/url_pattern_index/proto/rules.pb.h" +#include "components/url_pattern_index/unindexed_ruleset.h" #include "third_party/protobuf/src/google/protobuf/io/zero_copy_stream_impl_lite.h" namespace subresource_filter { @@ -351,15 +351,16 @@ bool RulesetService::IndexRuleset(base::File unindexed_ruleset_file, int64_t unindexed_ruleset_size = unindexed_ruleset_file.GetLength(); if (unindexed_ruleset_size < 0) return false; - CopyingFileInputStream copying_stream(std::move(unindexed_ruleset_file)); + url_pattern_index::CopyingFileInputStream copying_stream( + std::move(unindexed_ruleset_file)); google::protobuf::io::CopyingInputStreamAdaptor zero_copy_stream_adaptor( ©ing_stream, 4096 /* buffer_size */); - UnindexedRulesetReader reader(&zero_copy_stream_adaptor); + url_pattern_index::UnindexedRulesetReader reader(&zero_copy_stream_adaptor); size_t num_unsupported_rules = 0; - proto::FilteringRules ruleset_chunk; + url_pattern_index::proto::FilteringRules ruleset_chunk; while (reader.ReadNextChunk(&ruleset_chunk)) { - for (const proto::UrlRule& rule : ruleset_chunk.url_rules()) { + for (const auto& rule : ruleset_chunk.url_rules()) { if (!indexer->AddUrlRule(rule)) ++num_unsupported_rules; } diff --git a/components/subresource_filter/core/browser/ruleset_service_unittest.cc b/components/subresource_filter/core/browser/ruleset_service_unittest.cc index fa7d2fb159db21..56444462517480 100644 --- a/components/subresource_filter/core/browser/ruleset_service_unittest.cc +++ b/components/subresource_filter/core/browser/ruleset_service_unittest.cc @@ -27,8 +27,8 @@ #include "build/build_config.h" #include "components/prefs/testing_pref_service.h" #include "components/subresource_filter/core/browser/ruleset_service_delegate.h" -#include "components/subresource_filter/core/common/proto/rules.pb.h" #include "components/subresource_filter/core/common/test_ruleset_creator.h" +#include "components/url_pattern_index/proto/rules.pb.h" #include "testing/gmock/include/gmock/gmock.h" #include "testing/gtest/include/gtest/gtest.h" @@ -661,7 +661,7 @@ TEST_F(SubresourceFilteringRulesetServiceTest, mock_delegate()->SimulateStartupCompleted(); // The default field values are considered unsupported. - proto::UrlRule unfilled_rule; + url_pattern_index::proto::UrlRule unfilled_rule; TestRulesetPair ruleset_with_unsupported_rule; ASSERT_NO_FATAL_FAILURE( diff --git a/components/subresource_filter/core/common/BUILD.gn b/components/subresource_filter/core/common/BUILD.gn index ea52296ae289b6..55111b6107ffd7 100644 --- a/components/subresource_filter/core/common/BUILD.gn +++ b/components/subresource_filter/core/common/BUILD.gn @@ -13,43 +13,29 @@ static_library("common") { "activation_scope.h", "activation_state.cc", "activation_state.h", - "closed_hash_map.h", - "copying_file_stream.cc", - "copying_file_stream.h", "document_load_statistics.h", "document_subresource_filter.cc", "document_subresource_filter.h", "first_party_origin.cc", "first_party_origin.h", - "fuzzy_pattern_matching.cc", - "fuzzy_pattern_matching.h", "indexed_ruleset.cc", "indexed_ruleset.h", "load_policy.h", "memory_mapped_ruleset.cc", "memory_mapped_ruleset.h", - "ngram_extractor.h", "scoped_timers.h", - "string_splitter.h", "time_measurements.h", - "uint64_hasher.h", - "unindexed_ruleset.cc", - "unindexed_ruleset.h", - "url_pattern.cc", - "url_pattern.h", - "url_pattern_index.cc", - "url_pattern_index.h", ] public_deps = [ - "//components/subresource_filter/core/common/flat:flatbuffer", - "//components/subresource_filter/core/common/proto:proto", + "//components/subresource_filter/core/common/flat:indexed_ruleset", + "//components/url_pattern_index", ] deps = [ "//base", "//net", - "//third_party/flatbuffers:flatbuffers", + "//third_party/flatbuffers", "//third_party/protobuf:protobuf_lite", "//url", ] @@ -62,8 +48,6 @@ static_library("test_support") { "test_ruleset_creator.h", "test_ruleset_utils.cc", "test_ruleset_utils.h", - "url_rule_test_support.cc", - "url_rule_test_support.h", ] deps = [ ":common", @@ -77,23 +61,17 @@ static_library("test_support") { source_set("unit_tests") { testonly = true sources = [ - "closed_hash_map_unittest.cc", "document_subresource_filter_unittest.cc", "first_party_origin_unittest.cc", - "fuzzy_pattern_matching_unittest.cc", "indexed_ruleset_unittest.cc", - "ngram_extractor_unittest.cc", "scoped_timers_unittest.cc", - "string_splitter_unittest.cc", - "unindexed_ruleset_unittest.cc", - "url_pattern_index_unittest.cc", - "url_pattern_unittest.cc", ] deps = [ ":common", ":test_support", "//base", "//base/test:test_support", + "//components/url_pattern_index:test_support", "//testing/gtest", "//third_party/protobuf:protobuf_lite", "//url", diff --git a/components/subresource_filter/core/common/PRESUBMIT.py b/components/subresource_filter/core/common/PRESUBMIT.py index 7e054081adb2a8..79908dc38b3209 100644 --- a/components/subresource_filter/core/common/PRESUBMIT.py +++ b/components/subresource_filter/core/common/PRESUBMIT.py @@ -11,10 +11,12 @@ def CheckIndexedRulesetVersion(input_api, output_api): """ Checks that IndexedRuleset format version is modified when necessary. - Whenever a *.fbs or indexed_ruleset.cc file is touched in - components/subresource_filter/core/common and kIndexedFormatVersion constant - is not changed, this check returns a presubmit warning to make sure the value - should not be updated. + Whenever any of the following files is changed: + - components/subresource_filter/core/common/indexed_ruleset.cc + - components/url_pattern_index/flat/*.fbs + - components/url_pattern_index/url_pattern_index.cc + and kIndexedFormatVersion constant stays intact, this check returns a + presubmit warning to make sure the value should not be updated. """ indexed_ruleset_changed = False @@ -22,7 +24,8 @@ def CheckIndexedRulesetVersion(input_api, output_api): for affected_file in input_api.AffectedFiles(): path = affected_file.LocalPath() - if not 'components/subresource_filter/core/common' in path: + if (not 'components/subresource_filter/core/common' in path and + not 'components/url_pattern_index/flat' in path): continue basename = input_api.basename(path) @@ -37,9 +40,9 @@ def CheckIndexedRulesetVersion(input_api, output_api): if indexed_ruleset_changed and not indexed_ruleset_version_changed: return [output_api.PresubmitPromptWarning( - 'Please make sure that IndexedRuleset modifications in *.fbs and ' - 'indexed_ruleset.cc do not require updating ' - 'RulesetIndexer::kIndexedFormatVersion.')] + 'Please make sure that UrlPatternIndex/IndexedRuleset modifications in ' + '*.fbs and url_pattern_index.cc/indexed_ruleset.cc do not require ' + 'updating RulesetIndexer::kIndexedFormatVersion.')] return [] def CheckChangeOnUpload(input_api, output_api): diff --git a/components/subresource_filter/core/common/document_subresource_filter.cc b/components/subresource_filter/core/common/document_subresource_filter.cc index bbf1574e50f84d..2dcbaaa52925d4 100644 --- a/components/subresource_filter/core/common/document_subresource_filter.cc +++ b/components/subresource_filter/core/common/document_subresource_filter.cc @@ -33,7 +33,7 @@ DocumentSubresourceFilter::~DocumentSubresourceFilter() = default; LoadPolicy DocumentSubresourceFilter::GetLoadPolicy( const GURL& subresource_url, - proto::ElementType subresource_type) { + url_pattern_index::proto::ElementType subresource_type) { TRACE_EVENT1("loader", "DocumentSubresourceFilter::GetLoadPolicy", "url", subresource_url.spec()); diff --git a/components/subresource_filter/core/common/document_subresource_filter.h b/components/subresource_filter/core/common/document_subresource_filter.h index a10ba1d78f0c8a..02550c6d38dbf3 100644 --- a/components/subresource_filter/core/common/document_subresource_filter.h +++ b/components/subresource_filter/core/common/document_subresource_filter.h @@ -17,7 +17,7 @@ #include "components/subresource_filter/core/common/document_load_statistics.h" #include "components/subresource_filter/core/common/indexed_ruleset.h" #include "components/subresource_filter/core/common/load_policy.h" -#include "components/subresource_filter/core/common/proto/rules.pb.h" +#include "components/url_pattern_index/proto/rules.pb.h" class GURL; @@ -52,8 +52,9 @@ class DocumentSubresourceFilter { // TODO(pkalinnikov): Find a better way to achieve this. DocumentLoadStatistics& statistics() { return statistics_; } - LoadPolicy GetLoadPolicy(const GURL& subresource_url, - proto::ElementType subresource_type); + LoadPolicy GetLoadPolicy( + const GURL& subresource_url, + url_pattern_index::proto::ElementType subresource_type); private: const ActivationState activation_state_; diff --git a/components/subresource_filter/core/common/document_subresource_filter_unittest.cc b/components/subresource_filter/core/common/document_subresource_filter_unittest.cc index f20c5521bad5bb..2ecfcf54f3d0f6 100644 --- a/components/subresource_filter/core/common/document_subresource_filter_unittest.cc +++ b/components/subresource_filter/core/common/document_subresource_filter_unittest.cc @@ -16,6 +16,8 @@ namespace subresource_filter { +namespace proto = url_pattern_index::proto; + namespace { constexpr auto kDryRun = ActivationLevel::DRYRUN; diff --git a/components/subresource_filter/core/common/flat/BUILD.gn b/components/subresource_filter/core/common/flat/BUILD.gn index d554b35a338c5b..d46e71cc83d718 100644 --- a/components/subresource_filter/core/common/flat/BUILD.gn +++ b/components/subresource_filter/core/common/flat/BUILD.gn @@ -4,9 +4,11 @@ import("//third_party/flatbuffers/flatbuffer.gni") -flatbuffer("flatbuffer") { +flatbuffer("indexed_ruleset") { sources = [ "indexed_ruleset.fbs", - "url_pattern_index.fbs", + ] + public_deps = [ + "//components/url_pattern_index/flat:url_pattern_index", ] } diff --git a/components/subresource_filter/core/common/flat/indexed_ruleset.fbs b/components/subresource_filter/core/common/flat/indexed_ruleset.fbs index e8e351efb5808d..6401486d99f10e 100644 --- a/components/subresource_filter/core/common/flat/indexed_ruleset.fbs +++ b/components/subresource_filter/core/common/flat/indexed_ruleset.fbs @@ -2,20 +2,20 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -include "components/subresource_filter/core/common/flat/url_pattern_index.fbs"; +include "components/url_pattern_index/flat/url_pattern_index.fbs"; namespace subresource_filter.flat; // The top-level data structure used to store URL rules. table IndexedRuleset { // The index of all blacklist URL rules. - blacklist_index : UrlPatternIndex; + blacklist_index : url_pattern_index.flat.UrlPatternIndex; // The index of all whitelist URL rules, except pure deactivation rules. - whitelist_index : UrlPatternIndex; + whitelist_index : url_pattern_index.flat.UrlPatternIndex; // The index of all whitelist URL rules with activation options. - deactivation_index : UrlPatternIndex; + deactivation_index : url_pattern_index.flat.UrlPatternIndex; } root_type IndexedRuleset; diff --git a/components/subresource_filter/core/common/indexed_ruleset.cc b/components/subresource_filter/core/common/indexed_ruleset.cc index edbbee88c9d128..d24fde53e43ca2 100644 --- a/components/subresource_filter/core/common/indexed_ruleset.cc +++ b/components/subresource_filter/core/common/indexed_ruleset.cc @@ -11,6 +11,8 @@ namespace subresource_filter { +namespace proto = url_pattern_index::proto; + // RulesetIndexer -------------------------------------------------------------- // static @@ -22,7 +24,7 @@ RulesetIndexer::RulesetIndexer() RulesetIndexer::~RulesetIndexer() = default; bool RulesetIndexer::AddUrlRule(const proto::UrlRule& rule) { - const UrlRuleOffset offset = SerializeUrlRule(rule, &builder_); + const auto offset = url_pattern_index::SerializeUrlRule(rule, &builder_); // Note: A zero offset.o means a "nullptr" offset. It is returned when the // rule has not been serialized. if (!offset.o) diff --git a/components/subresource_filter/core/common/indexed_ruleset.h b/components/subresource_filter/core/common/indexed_ruleset.h index 00588f8a0a7651..defd8023d16d74 100644 --- a/components/subresource_filter/core/common/indexed_ruleset.h +++ b/components/subresource_filter/core/common/indexed_ruleset.h @@ -11,7 +11,7 @@ #include "base/macros.h" #include "base/numerics/safe_conversions.h" #include "components/subresource_filter/core/common/flat/indexed_ruleset_generated.h" -#include "components/subresource_filter/core/common/url_pattern_index.h" +#include "components/url_pattern_index/url_pattern_index.h" #include "third_party/flatbuffers/src/include/flatbuffers/flatbuffers.h" class GURL; @@ -20,13 +20,15 @@ namespace url { class Origin; } -namespace subresource_filter { - -class FirstPartyOrigin; - +namespace url_pattern_index { namespace proto { class UrlRule; } +} + +namespace subresource_filter { + +class FirstPartyOrigin; // The class used to construct flat data structures representing the set of URL // filtering rules, as well as the index of those. Internally owns a @@ -48,7 +50,7 @@ class RulesetIndexer { // Adds |rule| to the ruleset and the index unless the |rule| has unsupported // filter options, in which case the data structures remain unmodified. // Returns whether the |rule| has been serialized and added to the index. - bool AddUrlRule(const proto::UrlRule& rule); + bool AddUrlRule(const url_pattern_index::proto::UrlRule& rule); // Finalizes construction of the data structures. void Finish(); @@ -63,9 +65,9 @@ class RulesetIndexer { private: flatbuffers::FlatBufferBuilder builder_; - UrlPatternIndexBuilder blacklist_; - UrlPatternIndexBuilder whitelist_; - UrlPatternIndexBuilder deactivation_; + url_pattern_index::UrlPatternIndexBuilder blacklist_; + url_pattern_index::UrlPatternIndexBuilder whitelist_; + url_pattern_index::UrlPatternIndexBuilder deactivation_; DISALLOW_COPY_AND_ASSIGN(RulesetIndexer); }; @@ -91,22 +93,23 @@ class IndexedRulesetMatcher { bool ShouldDisableFilteringForDocument( const GURL& document_url, const url::Origin& parent_document_origin, - proto::ActivationType activation_type) const; + url_pattern_index::proto::ActivationType activation_type) const; // Returns whether the network request to |url| of |element_type| initiated by // |document_origin| is not allowed to proceed. Always returns false if the // |url| is not valid or |element_type| == ELEMENT_TYPE_UNSPECIFIED. - bool ShouldDisallowResourceLoad(const GURL& url, - const FirstPartyOrigin& first_party, - proto::ElementType element_type, - bool disable_generic_rules) const; + bool ShouldDisallowResourceLoad( + const GURL& url, + const FirstPartyOrigin& first_party, + url_pattern_index::proto::ElementType element_type, + bool disable_generic_rules) const; private: const flat::IndexedRuleset* root_; - UrlPatternIndexMatcher blacklist_; - UrlPatternIndexMatcher whitelist_; - UrlPatternIndexMatcher deactivation_; + url_pattern_index::UrlPatternIndexMatcher blacklist_; + url_pattern_index::UrlPatternIndexMatcher whitelist_; + url_pattern_index::UrlPatternIndexMatcher deactivation_; DISALLOW_COPY_AND_ASSIGN(IndexedRulesetMatcher); }; diff --git a/components/subresource_filter/core/common/indexed_ruleset_unittest.cc b/components/subresource_filter/core/common/indexed_ruleset_unittest.cc index bf94498f8e2593..e51a14048916a5 100644 --- a/components/subresource_filter/core/common/indexed_ruleset_unittest.cc +++ b/components/subresource_filter/core/common/indexed_ruleset_unittest.cc @@ -10,16 +10,19 @@ #include "base/macros.h" #include "base/strings/string_piece.h" #include "components/subresource_filter/core/common/first_party_origin.h" -#include "components/subresource_filter/core/common/proto/rules.pb.h" -#include "components/subresource_filter/core/common/url_pattern.h" -#include "components/subresource_filter/core/common/url_rule_test_support.h" +#include "components/url_pattern_index/proto/rules.pb.h" +#include "components/url_pattern_index/url_pattern.h" +#include "components/url_pattern_index/url_rule_test_support.h" #include "testing/gtest/include/gtest/gtest.h" #include "url/gurl.h" #include "url/origin.h" namespace subresource_filter { -using namespace testing; +namespace proto = url_pattern_index::proto; +namespace testing = url_pattern_index::testing; +using testing::MakeUrlRule; +using url_pattern_index::UrlPattern; class SubresourceFilterIndexedRulesetTest : public ::testing::Test { public: @@ -28,21 +31,22 @@ class SubresourceFilterIndexedRulesetTest : public ::testing::Test { protected: bool ShouldAllow(base::StringPiece url, base::StringPiece document_origin = nullptr, - proto::ElementType element_type = kOther, + proto::ElementType element_type = testing::kOther, bool disable_generic_rules = false) const { DCHECK(matcher_); return !matcher_->ShouldDisallowResourceLoad( - GURL(url), FirstPartyOrigin(GetOrigin(document_origin)), element_type, - disable_generic_rules); + GURL(url), FirstPartyOrigin(testing::GetOrigin(document_origin)), + element_type, disable_generic_rules); } bool ShouldDeactivate( base::StringPiece document_url, base::StringPiece parent_document_origin = nullptr, - proto::ActivationType activation_type = kNoActivation) const { + proto::ActivationType activation_type = testing::kNoActivation) const { DCHECK(matcher_); return matcher_->ShouldDisableFilteringForDocument( - GURL(document_url), GetOrigin(parent_document_origin), activation_type); + GURL(document_url), testing::GetOrigin(parent_document_origin), + activation_type); } bool AddUrlRule(const proto::UrlRule& rule) { @@ -50,18 +54,19 @@ class SubresourceFilterIndexedRulesetTest : public ::testing::Test { } bool AddSimpleRule(base::StringPiece url_pattern) { - return AddUrlRule(MakeUrlRule(UrlPattern(url_pattern, kSubstring))); + return AddUrlRule( + MakeUrlRule(UrlPattern(url_pattern, testing::kSubstring))); } bool AddSimpleWhitelistRule(base::StringPiece url_pattern) { - auto rule = MakeUrlRule(UrlPattern(url_pattern, kSubstring)); + auto rule = MakeUrlRule(UrlPattern(url_pattern, testing::kSubstring)); rule.set_semantics(proto::RULE_SEMANTICS_WHITELIST); return AddUrlRule(rule); } bool AddSimpleWhitelistRule(base::StringPiece url_pattern, int32_t activation_types) { - auto rule = MakeUrlRule(UrlPattern(url_pattern, kSubstring)); + auto rule = MakeUrlRule(UrlPattern(url_pattern, testing::kSubstring)); rule.set_semantics(proto::RULE_SEMANTICS_WHITELIST); rule.clear_element_types(); rule.set_activation_types(activation_types); @@ -130,11 +135,13 @@ TEST_F(SubresourceFilterIndexedRulesetTest, SimpleBlacklistAndWhitelist) { TEST_F(SubresourceFilterIndexedRulesetTest, OneBlacklistAndOneDeactivationRule) { ASSERT_TRUE(AddSimpleRule("example.com")); - ASSERT_TRUE(AddSimpleWhitelistRule("example.com", kDocument)); + ASSERT_TRUE(AddSimpleWhitelistRule("example.com", testing::kDocument)); Finish(); - EXPECT_TRUE(ShouldDeactivate("https://example.com", nullptr, kDocument)); - EXPECT_FALSE(ShouldDeactivate("https://xample.com", nullptr, kDocument)); + EXPECT_TRUE( + ShouldDeactivate("https://example.com", nullptr, testing::kDocument)); + EXPECT_FALSE( + ShouldDeactivate("https://xample.com", nullptr, testing::kDocument)); EXPECT_FALSE(ShouldAllow("https://example.com")); EXPECT_TRUE(ShouldAllow("https://xample.com")); } diff --git a/components/subresource_filter/core/common/test_ruleset_creator.cc b/components/subresource_filter/core/common/test_ruleset_creator.cc index 939dff3085e960..ba02d32f0481dd 100644 --- a/components/subresource_filter/core/common/test_ruleset_creator.cc +++ b/components/subresource_filter/core/common/test_ruleset_creator.cc @@ -12,14 +12,16 @@ #include "base/strings/string_number_conversions.h" #include "base/threading/thread_restrictions.h" #include "components/subresource_filter/core/common/indexed_ruleset.h" -#include "components/subresource_filter/core/common/proto/rules.pb.h" #include "components/subresource_filter/core/common/test_ruleset_utils.h" -#include "components/subresource_filter/core/common/unindexed_ruleset.h" +#include "components/url_pattern_index/proto/rules.pb.h" +#include "components/url_pattern_index/unindexed_ruleset.h" #include "testing/gtest/include/gtest/gtest.h" #include "third_party/protobuf/src/google/protobuf/io/zero_copy_stream_impl_lite.h" namespace subresource_filter { +namespace proto = url_pattern_index::proto; + namespace { // The methods below assume that char and uint8_t are interchangeable. @@ -39,7 +41,7 @@ std::vector SerializeUnindexedRulesetWithMultipleRules( const std::vector& rules) { std::string ruleset_contents; google::protobuf::io::StringOutputStream output(&ruleset_contents); - UnindexedRulesetWriter ruleset_writer(&output); + url_pattern_index::UnindexedRulesetWriter ruleset_writer(&output); for (const auto& rule : rules) ruleset_writer.AddUrlRule(rule); ruleset_writer.Finish(); diff --git a/components/subresource_filter/core/common/test_ruleset_creator.h b/components/subresource_filter/core/common/test_ruleset_creator.h index cb07526e7878cf..7d1d5cab66717f 100644 --- a/components/subresource_filter/core/common/test_ruleset_creator.h +++ b/components/subresource_filter/core/common/test_ruleset_creator.h @@ -14,7 +14,7 @@ #include "base/files/scoped_temp_dir.h" #include "base/macros.h" #include "base/strings/string_piece.h" -#include "components/subresource_filter/core/common/proto/rules.pb.h" +#include "components/url_pattern_index/proto/rules.pb.h" namespace subresource_filter { namespace testing { @@ -83,10 +83,12 @@ class TestRulesetCreator { int num_of_suffixes, TestRulesetPair* test_ruleset_pair); - void CreateRulesetWithRules(const std::vector& rules, - TestRulesetPair* test_ruleset_pair); - void CreateUnindexedRulesetWithRules(const std::vector& rules, - TestRuleset* test_unindexed_ruleset); + void CreateRulesetWithRules( + const std::vector& rules, + TestRulesetPair* test_ruleset_pair); + void CreateUnindexedRulesetWithRules( + const std::vector& rules, + TestRuleset* test_unindexed_ruleset); // Returns a unique |path| that is valid for the lifetime of this instance. // No file at |path| will be automatically created. diff --git a/components/subresource_filter/core/common/test_ruleset_utils.cc b/components/subresource_filter/core/common/test_ruleset_utils.cc index e97548eced7e19..9ea5f663371668 100644 --- a/components/subresource_filter/core/common/test_ruleset_utils.cc +++ b/components/subresource_filter/core/common/test_ruleset_utils.cc @@ -7,6 +7,8 @@ namespace subresource_filter { namespace testing { +namespace proto = url_pattern_index::proto; + proto::UrlRule CreateSuffixRule(base::StringPiece suffix) { proto::UrlRule rule; rule.set_semantics(proto::RULE_SEMANTICS_BLACKLIST); diff --git a/components/subresource_filter/core/common/test_ruleset_utils.h b/components/subresource_filter/core/common/test_ruleset_utils.h index d7023f86b056d1..5c001c90b55c57 100644 --- a/components/subresource_filter/core/common/test_ruleset_utils.h +++ b/components/subresource_filter/core/common/test_ruleset_utils.h @@ -10,21 +10,22 @@ #include #include "base/strings/string_piece.h" -#include "components/subresource_filter/core/common/proto/rules.pb.h" +#include "components/url_pattern_index/proto/rules.pb.h" namespace subresource_filter { namespace testing { // Creates a blacklist URL rule which targets subresources of any type such that // the resource URL ends with |suffix|. -proto::UrlRule CreateSuffixRule(base::StringPiece suffix); +url_pattern_index::proto::UrlRule CreateSuffixRule(base::StringPiece suffix); // Same as CreateUrlRule(pattern, proto::URL_PATTERN_TYPE_WILDCARDED), but the // rule applies to the specified |activation_types|, and to no element types. // Additionally, it is restricted to a set of |domains| (if provided). -proto::UrlRule CreateWhitelistRuleForDocument( +url_pattern_index::proto::UrlRule CreateWhitelistRuleForDocument( base::StringPiece pattern, - int32_t activation_types = proto::ACTIVATION_TYPE_DOCUMENT, + int32_t activation_types = + url_pattern_index::proto::ACTIVATION_TYPE_DOCUMENT, std::vector domains = std::vector()); } // namespace testing diff --git a/components/url_pattern_index/BUILD.gn b/components/url_pattern_index/BUILD.gn new file mode 100644 index 00000000000000..410252b0dec348 --- /dev/null +++ b/components/url_pattern_index/BUILD.gn @@ -0,0 +1,70 @@ +# 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. + +static_library("url_pattern_index") { + sources = [ + "closed_hash_map.h", + "copying_file_stream.cc", + "copying_file_stream.h", + "fuzzy_pattern_matching.cc", + "fuzzy_pattern_matching.h", + "ngram_extractor.h", + "string_splitter.h", + "uint64_hasher.h", + "unindexed_ruleset.cc", + "unindexed_ruleset.h", + "url_pattern.cc", + "url_pattern.h", + "url_pattern_index.cc", + "url_pattern_index.h", + ] + + public_deps = [ + "//components/url_pattern_index/flat:url_pattern_index", + "//components/url_pattern_index/proto:url_pattern_index", + ] + + deps = [ + "//base", + "//third_party/flatbuffers:flatbuffers", + "//third_party/protobuf:protobuf_lite", + "//url", + ] +} + +static_library("test_support") { + testonly = true + sources = [ + "url_rule_test_support.cc", + "url_rule_test_support.h", + ] + deps = [ + ":url_pattern_index", + "//base", + "//net", + "//third_party/protobuf:protobuf_lite", + "//url", + ] +} + +source_set("unit_tests") { + testonly = true + sources = [ + "closed_hash_map_unittest.cc", + "fuzzy_pattern_matching_unittest.cc", + "ngram_extractor_unittest.cc", + "string_splitter_unittest.cc", + "unindexed_ruleset_unittest.cc", + "url_pattern_index_unittest.cc", + "url_pattern_unittest.cc", + ] + deps = [ + ":test_support", + ":url_pattern_index", + "//base", + "//testing/gtest", + "//third_party/protobuf:protobuf_lite", + "//url", + ] +} diff --git a/components/url_pattern_index/DEPS b/components/url_pattern_index/DEPS new file mode 100644 index 00000000000000..55dda8afb39a92 --- /dev/null +++ b/components/url_pattern_index/DEPS @@ -0,0 +1,5 @@ +include_rules = [ + "+net/base/registry_controlled_domains", + "+third_party/flatbuffers", + "+third_party/protobuf", +] diff --git a/components/url_pattern_index/OWNERS b/components/url_pattern_index/OWNERS new file mode 100644 index 00000000000000..8e8ba099fc7f43 --- /dev/null +++ b/components/url_pattern_index/OWNERS @@ -0,0 +1,3 @@ +csharrison@chromium.org +engedy@chromium.org +pkalinnikov@chromium.org diff --git a/components/url_pattern_index/README b/components/url_pattern_index/README new file mode 100644 index 00000000000000..755e9df34a3cd9 --- /dev/null +++ b/components/url_pattern_index/README @@ -0,0 +1,12 @@ +UrlPatternIndex component can be used to build an index over a set of URL rules, +and speed up matching network requests against these rules. + +A URL rule (see flat::UrlRule structure) describes a subset of network requests +that it targets. The essential element of the rule is its URL pattern, which is +a simplified regular expression (a string with wildcards). UrlPatternIndex is +mainly based on text fragments extracted from the patterns. + +The component uses FlatBuffers serialization library to represent the rules and +the index. The key advantage of the format is that it does not require +deserialization. Once built, the data structure can be stored on disk or +transferred, then copied/loaded/memory-mapped and used directly. diff --git a/components/subresource_filter/core/common/closed_hash_map.h b/components/url_pattern_index/closed_hash_map.h similarity index 94% rename from components/subresource_filter/core/common/closed_hash_map.h rename to components/url_pattern_index/closed_hash_map.h index 7fca880dee5531..a91cfb2dd2af9e 100644 --- a/components/subresource_filter/core/common/closed_hash_map.h +++ b/components/url_pattern_index/closed_hash_map.h @@ -2,8 +2,8 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#ifndef COMPONENTS_SUBRESOURCE_FILTER_CORE_COMMON_CLOSED_HASH_MAP_H_ -#define COMPONENTS_SUBRESOURCE_FILTER_CORE_COMMON_CLOSED_HASH_MAP_H_ +#ifndef COMPONENTS_URL_PATTERN_INDEX_CLOSED_HASH_MAP_H_ +#define COMPONENTS_URL_PATTERN_INDEX_CLOSED_HASH_MAP_H_ #include #include @@ -19,7 +19,7 @@ #include "base/macros.h" #include "base/numerics/safe_conversions.h" -namespace subresource_filter { +namespace url_pattern_index { template class SimpleQuadraticProber; @@ -215,13 +215,14 @@ class ClosedHashMap { // Finds a slot such that it's either empty (indicating that the |key| is not // stored) or contains the |key|. size_t FindSlotForKey(const KeyType& key) const { - return prober_.FindSlot(key, hash_table_.size(), [this](const KeyType& key, - size_t slot_index) { - DCHECK_LT(slot_index, hash_table_.size()); - const uint32_t entry_index = hash_table_[slot_index]; - DCHECK(entry_index == EmptySlot() || entry_index < entries_.size()); - return entry_index == EmptySlot() || entries_[entry_index].first == key; - }); + return prober_.FindSlot( + key, hash_table_.size(), [this](const KeyType& key, size_t slot_index) { + DCHECK_LT(slot_index, hash_table_.size()); + const uint32_t entry_index = hash_table_[slot_index]; + DCHECK(entry_index == EmptySlot() || entry_index < entries_.size()); + return entry_index == EmptySlot() || + entries_[entry_index].first == key; + }); } // Contains indices into |entries_|, or EmptySlot() for free table slots. @@ -288,6 +289,6 @@ class SimpleQuadraticProber { Hasher hasher_; }; -} // namespace subresource_filter +} // namespace url_pattern_index -#endif // COMPONENTS_SUBRESOURCE_FILTER_CORE_COMMON_CLOSED_HASH_MAP_H_ +#endif // COMPONENTS_URL_PATTERN_INDEX_CLOSED_HASH_MAP_H_ diff --git a/components/subresource_filter/core/common/closed_hash_map_unittest.cc b/components/url_pattern_index/closed_hash_map_unittest.cc similarity index 95% rename from components/subresource_filter/core/common/closed_hash_map_unittest.cc rename to components/url_pattern_index/closed_hash_map_unittest.cc index 1987c568fee4d2..122e9125c7c08f 100644 --- a/components/subresource_filter/core/common/closed_hash_map_unittest.cc +++ b/components/url_pattern_index/closed_hash_map_unittest.cc @@ -2,15 +2,15 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#include "components/subresource_filter/core/common/closed_hash_map.h" +#include "components/url_pattern_index/closed_hash_map.h" #include #include -#include "components/subresource_filter/core/common/uint64_hasher.h" +#include "components/url_pattern_index/uint64_hasher.h" #include "testing/gtest/include/gtest/gtest.h" -namespace subresource_filter { +namespace url_pattern_index { namespace { @@ -163,4 +163,4 @@ TEST(ClosedHashMapTest, ManualRehash) { } } -} // namespace subresource_filter +} // namespace url_pattern_index diff --git a/components/subresource_filter/core/common/copying_file_stream.cc b/components/url_pattern_index/copying_file_stream.cc similarity index 87% rename from components/subresource_filter/core/common/copying_file_stream.cc rename to components/url_pattern_index/copying_file_stream.cc index 5d4b82bfbf85be..7f6d6d1d838a70 100644 --- a/components/subresource_filter/core/common/copying_file_stream.cc +++ b/components/url_pattern_index/copying_file_stream.cc @@ -2,9 +2,9 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#include "components/subresource_filter/core/common/copying_file_stream.h" +#include "components/url_pattern_index/copying_file_stream.h" -namespace subresource_filter { +namespace url_pattern_index { // CopyingFileInputStream ------------------------------------------------------ @@ -28,4 +28,4 @@ bool CopyingFileOutputStream::Write(const void* buffer, int size) { size; } -} // namespace subresource_filter +} // namespace url_pattern_index diff --git a/components/subresource_filter/core/common/copying_file_stream.h b/components/url_pattern_index/copying_file_stream.h similarity index 83% rename from components/subresource_filter/core/common/copying_file_stream.h rename to components/url_pattern_index/copying_file_stream.h index f8318163da47d5..f8e3009327fa38 100644 --- a/components/subresource_filter/core/common/copying_file_stream.h +++ b/components/url_pattern_index/copying_file_stream.h @@ -2,14 +2,14 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#ifndef COMPONENTS_SUBRESOURCE_FILTER_CORE_COMMON_COPYING_FILE_STREAM_H_ -#define COMPONENTS_SUBRESOURCE_FILTER_CORE_COMMON_COPYING_FILE_STREAM_H_ +#ifndef COMPONENTS_URL_PATTERN_INDEX_COPYING_FILE_STREAM_H_ +#define COMPONENTS_URL_PATTERN_INDEX_COPYING_FILE_STREAM_H_ #include "base/files/file.h" #include "base/macros.h" #include "third_party/protobuf/src/google/protobuf/io/zero_copy_stream_impl_lite.h" -namespace subresource_filter { +namespace url_pattern_index { // Implements a CopyingInputStream that reads from a base::File. Can be used in // combination with CopyingInputStreamAdaptor for reading from that file through @@ -46,6 +46,6 @@ class CopyingFileOutputStream DISALLOW_COPY_AND_ASSIGN(CopyingFileOutputStream); }; -} // namespace subresource_filter +} // namespace url_pattern_index -#endif // COMPONENTS_SUBRESOURCE_FILTER_CORE_COMMON_COPYING_FILE_STREAM_H_ +#endif // COMPONENTS_URL_PATTERN_INDEX_COPYING_FILE_STREAM_H_ diff --git a/components/url_pattern_index/flat/BUILD.gn b/components/url_pattern_index/flat/BUILD.gn new file mode 100644 index 00000000000000..0b20b05a5e04a7 --- /dev/null +++ b/components/url_pattern_index/flat/BUILD.gn @@ -0,0 +1,11 @@ +# Copyright 2016 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. + +import("//third_party/flatbuffers/flatbuffer.gni") + +flatbuffer("url_pattern_index") { + sources = [ + "url_pattern_index.fbs", + ] +} diff --git a/components/subresource_filter/core/common/flat/url_pattern_index.fbs b/components/url_pattern_index/flat/url_pattern_index.fbs similarity index 91% rename from components/subresource_filter/core/common/flat/url_pattern_index.fbs rename to components/url_pattern_index/flat/url_pattern_index.fbs index ace4a0566dca5f..3a3cc7da756714 100644 --- a/components/subresource_filter/core/common/flat/url_pattern_index.fbs +++ b/components/url_pattern_index/flat/url_pattern_index.fbs @@ -2,16 +2,16 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -namespace subresource_filter.flat; +namespace url_pattern_index.flat; -// Corresponds to subresource_filter::proto::UrlPatternType. +// Corresponds to url_pattern_index::proto::UrlPatternType. enum UrlPatternType : ubyte { SUBSTRING, WILDCARDED, REGEXP, } -// Corresponds to subresource_filter::proto::AnchorType. +// Corresponds to url_pattern_index::proto::AnchorType. enum AnchorType : ubyte { NONE, BOUNDARY, @@ -19,7 +19,7 @@ enum AnchorType : ubyte { } // URL rule matching options. These correspond to multiple fields of -// subresource_filter::proto::UrlRule, but here, they are represented as flags +// url_pattern_index::proto::UrlRule, but here, they are represented as flags // of the same bitmask to allow for compact storage. enum OptionFlag : ubyte (bit_flags) { IS_WHITELIST, @@ -29,7 +29,7 @@ enum OptionFlag : ubyte (bit_flags) { } // The flat representation of a single URL rule. For more details regarding the -// fields please see the comments to subresource_filter::proto::UrlRule. +// fields please see the comments to url_pattern_index::proto::UrlRule. table UrlRule { // Rule matching options, a bitmask consisting of OptionFlags. options : ubyte; diff --git a/components/subresource_filter/core/common/fuzzy_pattern_matching.cc b/components/url_pattern_index/fuzzy_pattern_matching.cc similarity index 92% rename from components/subresource_filter/core/common/fuzzy_pattern_matching.cc rename to components/url_pattern_index/fuzzy_pattern_matching.cc index f0a4dfd50aa9c8..92b6032c17f0e5 100644 --- a/components/subresource_filter/core/common/fuzzy_pattern_matching.cc +++ b/components/url_pattern_index/fuzzy_pattern_matching.cc @@ -2,11 +2,11 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#include "components/subresource_filter/core/common/fuzzy_pattern_matching.h" +#include "components/url_pattern_index/fuzzy_pattern_matching.h" #include -namespace subresource_filter { +namespace url_pattern_index { namespace { @@ -56,4 +56,4 @@ size_t FindFuzzy(base::StringPiece text, return found == text.end() ? base::StringPiece::npos : found - text.begin(); } -} // namespace subresource_filter +} // namespace url_pattern_index diff --git a/components/subresource_filter/core/common/fuzzy_pattern_matching.h b/components/url_pattern_index/fuzzy_pattern_matching.h similarity index 87% rename from components/subresource_filter/core/common/fuzzy_pattern_matching.h rename to components/url_pattern_index/fuzzy_pattern_matching.h index 68472e76ae6d85..52dc75b4069d16 100644 --- a/components/subresource_filter/core/common/fuzzy_pattern_matching.h +++ b/components/url_pattern_index/fuzzy_pattern_matching.h @@ -14,14 +14,14 @@ // characters of the |text|, whereas each '^' placeholder can correspond to any // type of separator in |text|. -#ifndef COMPONENTS_SUBRESOURCE_FILTER_CORE_COMMON_FUZZY_PATTERN_MATCHING_H_ -#define COMPONENTS_SUBRESOURCE_FILTER_CORE_COMMON_FUZZY_PATTERN_MATCHING_H_ +#ifndef COMPONENTS_URL_PATTERN_INDEX_FUZZY_PATTERN_MATCHING_H_ +#define COMPONENTS_URL_PATTERN_INDEX_FUZZY_PATTERN_MATCHING_H_ #include #include "base/strings/string_piece.h" -namespace subresource_filter { +namespace url_pattern_index { constexpr char kSeparatorPlaceholder = '^'; @@ -63,6 +63,6 @@ size_t FindFuzzy(base::StringPiece text, base::StringPiece subpattern, size_t from = 0); -} // namespace subresource_filter +} // namespace url_pattern_index -#endif // COMPONENTS_SUBRESOURCE_FILTER_CORE_COMMON_FUZZY_PATTERN_MATCHING_H_ +#endif // COMPONENTS_URL_PATTERN_INDEX_FUZZY_PATTERN_MATCHING_H_ diff --git a/components/subresource_filter/core/common/fuzzy_pattern_matching_unittest.cc b/components/url_pattern_index/fuzzy_pattern_matching_unittest.cc similarity index 96% rename from components/subresource_filter/core/common/fuzzy_pattern_matching_unittest.cc rename to components/url_pattern_index/fuzzy_pattern_matching_unittest.cc index 0aab4832d2ceaf..6bbaf90a90b5dc 100644 --- a/components/subresource_filter/core/common/fuzzy_pattern_matching_unittest.cc +++ b/components/url_pattern_index/fuzzy_pattern_matching_unittest.cc @@ -2,13 +2,13 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#include "components/subresource_filter/core/common/fuzzy_pattern_matching.h" +#include "components/url_pattern_index/fuzzy_pattern_matching.h" #include #include "testing/gtest/include/gtest/gtest.h" -namespace subresource_filter { +namespace url_pattern_index { TEST(SubresourceFilterFuzzyPatternMatchingTest, StartsWithFuzzy) { const struct { @@ -105,4 +105,4 @@ TEST(SubresourceFilterFuzzyPatternMatchingTest, FindFuzzy) { } } -} // namespace subresource_filter +} // namespace url_pattern_index diff --git a/components/subresource_filter/core/common/ngram_extractor.h b/components/url_pattern_index/ngram_extractor.h similarity index 94% rename from components/subresource_filter/core/common/ngram_extractor.h rename to components/url_pattern_index/ngram_extractor.h index 110c64b04d1b72..d91a04674b39c5 100644 --- a/components/subresource_filter/core/common/ngram_extractor.h +++ b/components/url_pattern_index/ngram_extractor.h @@ -2,8 +2,8 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#ifndef COMPONENTS_SUBRESOURCE_FILTER_CORE_COMMON_NGRAM_EXTRACTOR_H_ -#define COMPONENTS_SUBRESOURCE_FILTER_CORE_COMMON_NGRAM_EXTRACTOR_H_ +#ifndef COMPONENTS_URL_PATTERN_INDEX_NGRAM_EXTRACTOR_H_ +#define COMPONENTS_URL_PATTERN_INDEX_NGRAM_EXTRACTOR_H_ #include @@ -13,7 +13,7 @@ #include "base/logging.h" #include "base/strings/string_piece.h" -namespace subresource_filter { +namespace url_pattern_index { // The class used to iteratively extract N-grams from strings. An N-gram is a // string consisting of N (up to 8) non-special characters, which are stored in @@ -128,6 +128,6 @@ NGramExtractor CreateNGramExtractor( return NGramExtractor(string, is_separator); } -} // namespace subresource_filter +} // namespace url_pattern_index -#endif // COMPONENTS_SUBRESOURCE_FILTER_CORE_COMMON_NGRAM_EXTRACTOR_H_ +#endif // COMPONENTS_URL_PATTERN_INDEX_NGRAM_EXTRACTOR_H_ diff --git a/components/subresource_filter/core/common/ngram_extractor_unittest.cc b/components/url_pattern_index/ngram_extractor_unittest.cc similarity index 95% rename from components/subresource_filter/core/common/ngram_extractor_unittest.cc rename to components/url_pattern_index/ngram_extractor_unittest.cc index bea40150ab508d..c758f2a9aee317 100644 --- a/components/subresource_filter/core/common/ngram_extractor_unittest.cc +++ b/components/url_pattern_index/ngram_extractor_unittest.cc @@ -2,7 +2,7 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#include "components/subresource_filter/core/common/ngram_extractor.h" +#include "components/url_pattern_index/ngram_extractor.h" #include @@ -11,7 +11,7 @@ #include "testing/gtest/include/gtest/gtest.h" -namespace subresource_filter { +namespace url_pattern_index { namespace { @@ -108,4 +108,4 @@ TEST(NGramExtractorTest, NGramsArePresent) { } } -} // namespace subresource_filter +} // namespace url_pattern_index diff --git a/components/subresource_filter/core/common/proto/BUILD.gn b/components/url_pattern_index/proto/BUILD.gn similarity index 87% rename from components/subresource_filter/core/common/proto/BUILD.gn rename to components/url_pattern_index/proto/BUILD.gn index b0ccabe19c3f36..035744e2a2b6fa 100644 --- a/components/subresource_filter/core/common/proto/BUILD.gn +++ b/components/url_pattern_index/proto/BUILD.gn @@ -4,7 +4,7 @@ import("//third_party/protobuf/proto_library.gni") -proto_library("proto") { +proto_library("url_pattern_index") { sources = [ "rules.proto", ] diff --git a/components/subresource_filter/core/common/proto/rules.proto b/components/url_pattern_index/proto/rules.proto similarity index 98% rename from components/subresource_filter/core/common/proto/rules.proto rename to components/url_pattern_index/proto/rules.proto index b918dd95a9960f..086dfd341795a6 100644 --- a/components/subresource_filter/core/common/proto/rules.proto +++ b/components/url_pattern_index/proto/rules.proto @@ -5,8 +5,8 @@ syntax = "proto2"; option optimize_for = LITE_RUNTIME; -package subresource_filter.proto; -option java_package = "org.chromium.components.subresource_filter.proto"; +package url_pattern_index.proto; +option java_package = "org.chromium.components.url_pattern_index.proto"; // The type of a subresource filtering rule. enum RuleType { diff --git a/components/subresource_filter/core/common/string_splitter.h b/components/url_pattern_index/string_splitter.h similarity index 91% rename from components/subresource_filter/core/common/string_splitter.h rename to components/url_pattern_index/string_splitter.h index f3b4e85b06b41b..b51a0e87fd8a12 100644 --- a/components/subresource_filter/core/common/string_splitter.h +++ b/components/url_pattern_index/string_splitter.h @@ -2,15 +2,15 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#ifndef COMPONENTS_SUBRESOURCE_FILTER_CORE_COMMON_STRING_SPLITTER_H_ -#define COMPONENTS_SUBRESOURCE_FILTER_CORE_COMMON_STRING_SPLITTER_H_ +#ifndef COMPONENTS_URL_PATTERN_INDEX_STRING_SPLITTER_H_ +#define COMPONENTS_URL_PATTERN_INDEX_STRING_SPLITTER_H_ #include #include "base/logging.h" #include "base/strings/string_piece.h" -namespace subresource_filter { +namespace url_pattern_index { // A zero-allocation string splitter. Splits a string into non-empty tokens // divided by separator characters as defined by the IsSeparator predicate. @@ -96,6 +96,6 @@ StringSplitter CreateStringSplitter(base::StringPiece text, return StringSplitter(text, is_separator); } -} // namespace subresource_filter +} // namespace url_pattern_index -#endif // COMPONENTS_SUBRESOURCE_FILTER_CORE_COMMON_STRING_SPLITTER_H_ +#endif // COMPONENTS_URL_PATTERN_INDEX_STRING_SPLITTER_H_ diff --git a/components/subresource_filter/core/common/string_splitter_unittest.cc b/components/url_pattern_index/string_splitter_unittest.cc similarity index 94% rename from components/subresource_filter/core/common/string_splitter_unittest.cc rename to components/url_pattern_index/string_splitter_unittest.cc index 5e638405b2dfec..d2d2938c8821e0 100644 --- a/components/subresource_filter/core/common/string_splitter_unittest.cc +++ b/components/url_pattern_index/string_splitter_unittest.cc @@ -2,7 +2,7 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#include "components/subresource_filter/core/common/string_splitter.h" +#include "components/url_pattern_index/string_splitter.h" #include #include @@ -10,7 +10,7 @@ #include "base/strings/string_piece.h" #include "testing/gtest/include/gtest/gtest.h" -namespace subresource_filter { +namespace url_pattern_index { namespace { bool IsTestSeparator(char c) { @@ -79,4 +79,4 @@ TEST(StringSplitterTest, SplitThreeWords) { } } -} // namespace subresource_filter +} // namespace url_pattern_index diff --git a/components/subresource_filter/core/common/uint64_hasher.h b/components/url_pattern_index/uint64_hasher.h similarity index 84% rename from components/subresource_filter/core/common/uint64_hasher.h rename to components/url_pattern_index/uint64_hasher.h index 349d9d73585d67..7fddbe649af01b 100644 --- a/components/subresource_filter/core/common/uint64_hasher.h +++ b/components/url_pattern_index/uint64_hasher.h @@ -7,15 +7,15 @@ // https://gist.github.com/badboy/6267743 // TODO(pkalinnikov): Consider moving the implementation into base/. -#ifndef COMPONENTS_SUBRESOURCE_FILTER_CORE_COMMON_HASH_H_ -#define COMPONENTS_SUBRESOURCE_FILTER_CORE_COMMON_HASH_H_ +#ifndef COMPONENTS_URL_PATTERN_INDEX_HASH_H_ +#define COMPONENTS_URL_PATTERN_INDEX_HASH_H_ #include #include #include -namespace subresource_filter { +namespace url_pattern_index { template typename std::enable_if::type Uint64Hash(uint64_t v) { @@ -47,6 +47,6 @@ class Uint64Hasher { size_t operator()(uint64_t v) const { return Uint64Hash(v); } }; -} // subresource_filter +} // namespace url_pattern_index -#endif // COMPONENTS_SUBRESOURCE_FILTER_CORE_COMMON_HASH_H_ +#endif // COMPONENTS_URL_PATTERN_INDEX_HASH_H_ diff --git a/components/subresource_filter/core/common/unindexed_ruleset.cc b/components/url_pattern_index/unindexed_ruleset.cc similarity index 94% rename from components/subresource_filter/core/common/unindexed_ruleset.cc rename to components/url_pattern_index/unindexed_ruleset.cc index 12a4f5d446bb58..d9f4c2df037d26 100644 --- a/components/subresource_filter/core/common/unindexed_ruleset.cc +++ b/components/url_pattern_index/unindexed_ruleset.cc @@ -2,12 +2,12 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#include "components/subresource_filter/core/common/unindexed_ruleset.h" +#include "components/url_pattern_index/unindexed_ruleset.h" #include "base/logging.h" #include "base/numerics/safe_conversions.h" -namespace subresource_filter { +namespace url_pattern_index { // UnindexedRulesetReader ------------------------------------------------------ @@ -68,4 +68,4 @@ bool UnindexedRulesetWriter::WritePendingChunk() { return !had_error() && chunk.SerializeToCodedStream(&coded_stream_); } -} // namespace subresource_filter +} // namespace url_pattern_index diff --git a/components/subresource_filter/core/common/unindexed_ruleset.h b/components/url_pattern_index/unindexed_ruleset.h similarity index 91% rename from components/subresource_filter/core/common/unindexed_ruleset.h rename to components/url_pattern_index/unindexed_ruleset.h index af847ea8a06a6d..520f5a1efcc282 100644 --- a/components/subresource_filter/core/common/unindexed_ruleset.h +++ b/components/url_pattern_index/unindexed_ruleset.h @@ -19,15 +19,15 @@ // before reading the next one. In practice, this should not be an issue as // indexing of the ruleset is expected to be performed in an on-line fashion. -#ifndef COMPONENTS_SUBRESOURCE_FILTER_CORE_COMMON_UNINDEXED_RULESET_H_ -#define COMPONENTS_SUBRESOURCE_FILTER_CORE_COMMON_UNINDEXED_RULESET_H_ +#ifndef COMPONENTS_URL_PATTERN_INDEX_UNINDEXED_RULESET_H_ +#define COMPONENTS_URL_PATTERN_INDEX_UNINDEXED_RULESET_H_ #include "base/macros.h" -#include "components/subresource_filter/core/common/proto/rules.pb.h" +#include "components/url_pattern_index/proto/rules.pb.h" #include "third_party/protobuf/src/google/protobuf/io/coded_stream.h" #include "third_party/protobuf/src/google/protobuf/io/zero_copy_stream.h" -namespace subresource_filter { +namespace url_pattern_index { // Reads an unindexed ruleset from |stream| one chunk at a time. class UnindexedRulesetReader { @@ -95,6 +95,6 @@ class UnindexedRulesetWriter { DISALLOW_COPY_AND_ASSIGN(UnindexedRulesetWriter); }; -} // namespace subresource_filter +} // namespace url_pattern_index -#endif // COMPONENTS_SUBRESOURCE_FILTER_CORE_COMMON_UNINDEXED_RULESET_H_ +#endif // COMPONENTS_URL_PATTERN_INDEX_UNINDEXED_RULESET_H_ diff --git a/components/subresource_filter/core/common/unindexed_ruleset_unittest.cc b/components/url_pattern_index/unindexed_ruleset_unittest.cc similarity index 92% rename from components/subresource_filter/core/common/unindexed_ruleset_unittest.cc rename to components/url_pattern_index/unindexed_ruleset_unittest.cc index f193b5ed46e42d..318855e013aa55 100644 --- a/components/subresource_filter/core/common/unindexed_ruleset_unittest.cc +++ b/components/url_pattern_index/unindexed_ruleset_unittest.cc @@ -2,7 +2,7 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#include "components/subresource_filter/core/common/unindexed_ruleset.h" +#include "components/url_pattern_index/unindexed_ruleset.h" #include #include @@ -11,18 +11,16 @@ #include "base/macros.h" #include "base/numerics/safe_conversions.h" #include "base/strings/string_number_conversions.h" -#include "components/subresource_filter/core/common/url_pattern.h" -#include "components/subresource_filter/core/common/url_rule_test_support.h" +#include "components/url_pattern_index/url_pattern.h" +#include "components/url_pattern_index/url_rule_test_support.h" #include "testing/gtest/include/gtest/gtest.h" #include "third_party/protobuf/src/google/protobuf/io/zero_copy_stream.h" #include "third_party/protobuf/src/google/protobuf/io/zero_copy_stream_impl_lite.h" -namespace subresource_filter { +namespace url_pattern_index { namespace { -using namespace testing; - bool IsEqual(const proto::UrlRule& lhs, const proto::UrlRule& rhs) { return lhs.SerializeAsString() == rhs.SerializeAsString(); } @@ -52,7 +50,7 @@ class UnindexedRulesetTestBuilder { bool AddUrlRule(const UrlPattern& url_pattern, proto::SourceType source_type, bool is_whitelist = false) { - auto rule = MakeUrlRule(url_pattern); + auto rule = testing::MakeUrlRule(url_pattern); if (is_whitelist) rule.set_semantics(proto::RULE_SEMANTICS_WHITELIST); rule.set_source_type(source_type); @@ -65,7 +63,7 @@ class UnindexedRulesetTestBuilder { bool AddUrlRules(int number_of_rules) { for (int i = 0; i < number_of_rules; ++i) { std::string url_pattern = "example" + base::IntToString(i) + ".com"; - if (!AddUrlRule(UrlPattern(url_pattern), kAnyParty, i & 1)) + if (!AddUrlRule(UrlPattern(url_pattern), testing::kAnyParty, i & 1)) return false; } return true; @@ -127,7 +125,8 @@ TEST(UnindexedRulesetTest, EmptyRuleset) { TEST(UnindexedRulesetTest, OneUrlRule) { UnindexedRulesetTestBuilder builder; - EXPECT_TRUE(builder.AddUrlRule(UrlPattern("example.com"), kThirdParty)); + EXPECT_TRUE( + builder.AddUrlRule(UrlPattern("example.com"), testing::kThirdParty)); EXPECT_TRUE(builder.Finish()); EXPECT_TRUE(IsRulesetValid(builder.ruleset_contents(), builder.url_rules())); } @@ -179,4 +178,4 @@ TEST(UnindexedRulesetTest, ReadCorruptedInput) { } } -} // namespace subresource_filter +} // namespace url_pattern_index diff --git a/components/subresource_filter/core/common/url_pattern.cc b/components/url_pattern_index/url_pattern.cc similarity index 96% rename from components/subresource_filter/core/common/url_pattern.cc rename to components/url_pattern_index/url_pattern.cc index 381def4b45e4e2..83584d4fd2ecfc 100644 --- a/components/subresource_filter/core/common/url_pattern.cc +++ b/components/url_pattern_index/url_pattern.cc @@ -13,20 +13,20 @@ // the following: '_', '-', '.', '%'. Note that the separator placeholder // character '^' is itself a separator, as well as '\0'. -#include "components/subresource_filter/core/common/url_pattern.h" +#include "components/url_pattern_index/url_pattern.h" #include #include #include "base/logging.h" -#include "components/subresource_filter/core/common/flat/url_pattern_index_generated.h" -#include "components/subresource_filter/core/common/fuzzy_pattern_matching.h" -#include "components/subresource_filter/core/common/string_splitter.h" +#include "components/url_pattern_index/flat/url_pattern_index_generated.h" +#include "components/url_pattern_index/fuzzy_pattern_matching.h" +#include "components/url_pattern_index/string_splitter.h" #include "url/gurl.h" #include "url/third_party/mozilla/url_parse.h" -namespace subresource_filter { +namespace url_pattern_index { namespace { @@ -253,4 +253,4 @@ std::ostream& operator<<(std::ostream& out, const UrlPattern& pattern) { return out; } -} // namespace subresource_filter +} // namespace url_pattern_index diff --git a/components/subresource_filter/core/common/url_pattern.h b/components/url_pattern_index/url_pattern.h similarity index 88% rename from components/subresource_filter/core/common/url_pattern.h rename to components/url_pattern_index/url_pattern.h index edbef99a6d45ca..0994803ac6337e 100644 --- a/components/subresource_filter/core/common/url_pattern.h +++ b/components/url_pattern_index/url_pattern.h @@ -2,18 +2,18 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#ifndef COMPONENTS_SUBRESOURCE_FILTER_CORE_COMMON_URL_PATTERN_H_ -#define COMPONENTS_SUBRESOURCE_FILTER_CORE_COMMON_URL_PATTERN_H_ +#ifndef COMPONENTS_URL_PATTERN_INDEX_URL_PATTERN_H_ +#define COMPONENTS_URL_PATTERN_INDEX_URL_PATTERN_H_ #include #include "base/macros.h" #include "base/strings/string_piece.h" -#include "components/subresource_filter/core/common/proto/rules.pb.h" +#include "components/url_pattern_index/proto/rules.pb.h" class GURL; -namespace subresource_filter { +namespace url_pattern_index { namespace flat { struct UrlRule; // The FlatBuffers version of UrlRule. @@ -74,6 +74,6 @@ class UrlPattern { // Allow pretty-printing URLPatterns when they are used in GTest assertions. std::ostream& operator<<(std::ostream& out, const UrlPattern& pattern); -} // namespace subresource_filter +} // namespace url_pattern_index -#endif // COMPONENTS_SUBRESOURCE_FILTER_CORE_COMMON_URL_PATTERN_H_ +#endif // COMPONENTS_URL_PATTERN_INDEX_URL_PATTERN_H_ diff --git a/components/subresource_filter/core/common/url_pattern_index.cc b/components/url_pattern_index/url_pattern_index.cc similarity index 98% rename from components/subresource_filter/core/common/url_pattern_index.cc rename to components/url_pattern_index/url_pattern_index.cc index 574f577a32533c..c18091aea8f5ed 100644 --- a/components/subresource_filter/core/common/url_pattern_index.cc +++ b/components/url_pattern_index/url_pattern_index.cc @@ -2,7 +2,7 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#include "components/subresource_filter/core/common/url_pattern_index.h" +#include "components/url_pattern_index/url_pattern_index.h" #include #include @@ -12,12 +12,12 @@ #include "base/numerics/safe_conversions.h" #include "base/strings/string_piece.h" #include "base/strings/string_util.h" -#include "components/subresource_filter/core/common/ngram_extractor.h" -#include "components/subresource_filter/core/common/url_pattern.h" +#include "components/url_pattern_index/ngram_extractor.h" +#include "components/url_pattern_index/url_pattern.h" #include "url/gurl.h" #include "url/origin.h" -namespace subresource_filter { +namespace url_pattern_index { namespace { @@ -564,4 +564,4 @@ const flat::UrlRule* UrlPatternIndexMatcher::FindMatch( is_third_party, disable_generic_rules); } -} // namespace subresource_filter +} // namespace url_pattern_index diff --git a/components/subresource_filter/core/common/url_pattern_index.h b/components/url_pattern_index/url_pattern_index.h similarity index 90% rename from components/subresource_filter/core/common/url_pattern_index.h rename to components/url_pattern_index/url_pattern_index.h index 30224dce7680cf..22e84b127ba91b 100644 --- a/components/subresource_filter/core/common/url_pattern_index.h +++ b/components/url_pattern_index/url_pattern_index.h @@ -2,8 +2,8 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#ifndef COMPONENTS_SUBRESOURCE_FILTER_CORE_COMMON_URL_PATTERN_INDEX_H_ -#define COMPONENTS_SUBRESOURCE_FILTER_CORE_COMMON_URL_PATTERN_INDEX_H_ +#ifndef COMPONENTS_URL_PATTERN_INDEX_URL_PATTERN_INDEX_H_ +#define COMPONENTS_URL_PATTERN_INDEX_URL_PATTERN_INDEX_H_ #include #include @@ -12,10 +12,10 @@ #include "base/macros.h" #include "base/strings/string_piece_forward.h" -#include "components/subresource_filter/core/common/closed_hash_map.h" -#include "components/subresource_filter/core/common/flat/url_pattern_index_generated.h" -#include "components/subresource_filter/core/common/proto/rules.pb.h" -#include "components/subresource_filter/core/common/uint64_hasher.h" +#include "components/url_pattern_index/closed_hash_map.h" +#include "components/url_pattern_index/flat/url_pattern_index_generated.h" +#include "components/url_pattern_index/proto/rules.pb.h" +#include "components/url_pattern_index/uint64_hasher.h" #include "third_party/flatbuffers/src/include/flatbuffers/flatbuffers.h" class GURL; @@ -24,7 +24,7 @@ namespace url { class Origin; } -namespace subresource_filter { +namespace url_pattern_index { // The integer type used to represent N-grams. using NGram = uint64_t; @@ -130,6 +130,6 @@ class UrlPatternIndexMatcher { DISALLOW_COPY_AND_ASSIGN(UrlPatternIndexMatcher); }; -} // namespace subresource_filter +} // namespace url_pattern_index -#endif // COMPONENTS_SUBRESOURCE_FILTER_CORE_COMMON_URL_PATTERN_INDEX_H_ +#endif // COMPONENTS_URL_PATTERN_INDEX_URL_PATTERN_INDEX_H_ diff --git a/components/subresource_filter/core/common/url_pattern_index_unittest.cc b/components/url_pattern_index/url_pattern_index_unittest.cc similarity index 98% rename from components/subresource_filter/core/common/url_pattern_index_unittest.cc rename to components/url_pattern_index/url_pattern_index_unittest.cc index fcdb9da54c4e4b..53d19677929f7f 100644 --- a/components/subresource_filter/core/common/url_pattern_index_unittest.cc +++ b/components/url_pattern_index/url_pattern_index_unittest.cc @@ -2,7 +2,7 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#include "components/subresource_filter/core/common/url_pattern_index.h" +#include "components/url_pattern_index/url_pattern_index.h" #include #include @@ -11,13 +11,13 @@ #include "base/macros.h" #include "base/strings/string_piece.h" -#include "components/subresource_filter/core/common/url_pattern.h" -#include "components/subresource_filter/core/common/url_rule_test_support.h" +#include "components/url_pattern_index/url_pattern.h" +#include "components/url_pattern_index/url_rule_test_support.h" #include "testing/gtest/include/gtest/gtest.h" #include "url/gurl.h" #include "url/origin.h" -namespace subresource_filter { +namespace url_pattern_index { using namespace testing; @@ -704,4 +704,4 @@ TEST_F(UrlPatternIndexTest, FindMatchReturnsCorrectRules) { FindMatch("http://example." + std::to_string(kNumOfPatterns) + ".com")); } -} // namespace subresource_filter +} // namespace url_pattern_index diff --git a/components/subresource_filter/core/common/url_pattern_unittest.cc b/components/url_pattern_index/url_pattern_unittest.cc similarity index 97% rename from components/subresource_filter/core/common/url_pattern_unittest.cc rename to components/url_pattern_index/url_pattern_unittest.cc index 91ba4fa7d6f233..09434567eb837b 100644 --- a/components/subresource_filter/core/common/url_pattern_unittest.cc +++ b/components/url_pattern_index/url_pattern_unittest.cc @@ -2,12 +2,12 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#include "components/subresource_filter/core/common/url_pattern.h" +#include "components/url_pattern_index/url_pattern.h" #include "testing/gtest/include/gtest/gtest.h" #include "url/gurl.h" -namespace subresource_filter { +namespace url_pattern_index { namespace { @@ -121,4 +121,4 @@ TEST(SubresourceFilterUrlPatternTest, MatchesUrl) { } } -} // namespace subresource_filter +} // namespace url_pattern_index diff --git a/components/subresource_filter/core/common/url_rule_test_support.cc b/components/url_pattern_index/url_rule_test_support.cc similarity index 92% rename from components/subresource_filter/core/common/url_rule_test_support.cc rename to components/url_pattern_index/url_rule_test_support.cc index ab6f7f1bfe7163..9f2a462d372f37 100644 --- a/components/subresource_filter/core/common/url_rule_test_support.cc +++ b/components/url_pattern_index/url_rule_test_support.cc @@ -2,14 +2,14 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#include "components/subresource_filter/core/common/url_rule_test_support.h" +#include "components/url_pattern_index/url_rule_test_support.h" #include "base/logging.h" #include "net/base/registry_controlled_domains/registry_controlled_domain.h" #include "url/gurl.h" #include "url/origin.h" -namespace subresource_filter { +namespace url_pattern_index { namespace testing { proto::UrlRule MakeUrlRule(const UrlPattern& url_pattern) { @@ -53,4 +53,4 @@ bool IsThirdParty(const GURL& url, const url::Origin& first_party_origin) { } } // namespace testing -} // namespace subresource_filter +} // namespace url_pattern_index diff --git a/components/subresource_filter/core/common/url_rule_test_support.h b/components/url_pattern_index/url_rule_test_support.h similarity index 86% rename from components/subresource_filter/core/common/url_rule_test_support.h rename to components/url_pattern_index/url_rule_test_support.h index fef73b9456e8bf..895f0f8446ae33 100644 --- a/components/subresource_filter/core/common/url_rule_test_support.h +++ b/components/url_pattern_index/url_rule_test_support.h @@ -2,15 +2,15 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#ifndef COMPONENTS_SUBRESOURCE_FILTER_CORE_COMMON_URL_RULE_TEST_SUPPORT_H_ -#define COMPONENTS_SUBRESOURCE_FILTER_CORE_COMMON_URL_RULE_TEST_SUPPORT_H_ +#ifndef COMPONENTS_URL_PATTERN_INDEX_URL_RULE_TEST_SUPPORT_H_ +#define COMPONENTS_URL_PATTERN_INDEX_URL_RULE_TEST_SUPPORT_H_ #include #include #include "base/strings/string_piece.h" -#include "components/subresource_filter/core/common/proto/rules.pb.h" -#include "components/subresource_filter/core/common/url_pattern.h" +#include "components/url_pattern_index/proto/rules.pb.h" +#include "components/url_pattern_index/url_pattern.h" class GURL; @@ -18,7 +18,7 @@ namespace url { class Origin; } -namespace subresource_filter { +namespace url_pattern_index { namespace testing { // Constants ------------------------------------------------------------------- @@ -69,6 +69,6 @@ url::Origin GetOrigin(base::StringPiece origin_string); bool IsThirdParty(const GURL& url, const url::Origin& first_party_origin); } // namespace testing -} // namespace subresource_filter +} // namespace url_pattern_index -#endif // COMPONENTS_SUBRESOURCE_FILTER_CORE_COMMON_URL_RULE_TEST_SUPPORT_H_ +#endif // COMPONENTS_URL_PATTERN_INDEX_URL_RULE_TEST_SUPPORT_H_ diff --git a/third_party/flatbuffers/flatbuffer.gni b/third_party/flatbuffers/flatbuffer.gni index cc28b1bd885d43..11aae5ed74d4ce 100644 --- a/third_party/flatbuffers/flatbuffer.gni +++ b/third_party/flatbuffers/flatbuffer.gni @@ -68,6 +68,7 @@ template("flatbuffer") { tool = "//third_party/flatbuffers:flatc" sources = invoker.sources + deps = [] if (defined(invoker.flatc_out_dir)) { out_dir = "$root_gen_dir/" + invoker.flatc_out_dir @@ -138,5 +139,10 @@ template("flatbuffer") { if (defined(invoker.deps)) { deps += invoker.deps } + + # Same for public_deps. + if (defined(invoker.public_deps)) { + public_deps += invoker.public_deps + } } }