optimization_filter.h 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. // Copyright 2018 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef COMPONENTS_OPTIMIZATION_GUIDE_CORE_OPTIMIZATION_FILTER_H_
  5. #define COMPONENTS_OPTIMIZATION_GUIDE_CORE_OPTIMIZATION_FILTER_H_
  6. #include <memory>
  7. #include <vector>
  8. #include "base/sequence_checker.h"
  9. #include "components/optimization_guide/core/bloom_filter.h"
  10. #include "third_party/re2/src/re2/re2.h"
  11. #include "url/gurl.h"
  12. namespace optimization_guide {
  13. typedef std::vector<std::unique_ptr<re2::RE2>> RegexpList;
  14. // OptimizationFilter represents a filter with two underlying implementations: a
  15. // Bloom filter and sets of regexps. This class has a 1:1 mapping with an
  16. // OptimizationFilter protobuf message where this is the logical implementation
  17. // of the proto data.
  18. class OptimizationFilter {
  19. public:
  20. explicit OptimizationFilter(std::unique_ptr<BloomFilter> bloom_filter,
  21. std::unique_ptr<RegexpList> regexps,
  22. std::unique_ptr<RegexpList> exclusion_regexps,
  23. bool skip_host_suffix_checking);
  24. OptimizationFilter(const OptimizationFilter&) = delete;
  25. OptimizationFilter& operator=(const OptimizationFilter&) = delete;
  26. ~OptimizationFilter();
  27. // Returns true if the given url is matched by this filter.
  28. bool Matches(const GURL& url) const;
  29. private:
  30. // Returns whether this filter contains a host suffix for the host part
  31. // of |url|. It will check at most 5 host suffixes and it may ignore simple
  32. // top level domain matches (such as "com" or "co.in").
  33. //
  34. // A host suffix is comprised of domain name level elements (vs. characters).
  35. // For example, "server1.www.company.co.in" has the following suffixes that
  36. // would be checked for membership:
  37. // "server1.www.company.co.in"
  38. // "www.company.co.in"
  39. // "company.co.in"
  40. // This method will return true if any of those suffixes are present.
  41. bool ContainsHostSuffix(const GURL& url) const;
  42. std::unique_ptr<BloomFilter> bloom_filter_;
  43. std::unique_ptr<RegexpList> regexps_;
  44. std::unique_ptr<RegexpList> exclusion_regexps_;
  45. bool skip_host_suffix_checking_ = false;
  46. SEQUENCE_CHECKER(sequence_checker_);
  47. };
  48. } // namespace optimization_guide
  49. #endif // COMPONENTS_OPTIMIZATION_GUIDE_CORE_OPTIMIZATION_FILTER_H_