url_rule_test_support.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. // Copyright 2017 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_URL_PATTERN_INDEX_URL_RULE_TEST_SUPPORT_H_
  5. #define COMPONENTS_URL_PATTERN_INDEX_URL_RULE_TEST_SUPPORT_H_
  6. #include <string>
  7. #include <vector>
  8. #include "base/strings/string_piece.h"
  9. #include "components/url_pattern_index/proto/rules.pb.h"
  10. #include "components/url_pattern_index/url_pattern.h"
  11. class GURL;
  12. namespace url {
  13. class Origin;
  14. }
  15. namespace url_pattern_index {
  16. namespace testing {
  17. // Constants -------------------------------------------------------------------
  18. constexpr proto::UrlPatternType kSubstring = proto::URL_PATTERN_TYPE_SUBSTRING;
  19. constexpr proto::AnchorType kAnchorNone = proto::ANCHOR_TYPE_NONE;
  20. constexpr proto::AnchorType kBoundary = proto::ANCHOR_TYPE_BOUNDARY;
  21. constexpr proto::AnchorType kSubdomain = proto::ANCHOR_TYPE_SUBDOMAIN;
  22. constexpr proto::ElementType kNoElement = proto::ELEMENT_TYPE_UNSPECIFIED;
  23. constexpr proto::ElementType kOther = proto::ELEMENT_TYPE_OTHER;
  24. constexpr proto::ElementType kScript = proto::ELEMENT_TYPE_SCRIPT;
  25. constexpr proto::ElementType kImage = proto::ELEMENT_TYPE_IMAGE;
  26. constexpr proto::ElementType kSubdocument = proto::ELEMENT_TYPE_SUBDOCUMENT;
  27. constexpr proto::ElementType kFont = proto::ELEMENT_TYPE_FONT;
  28. constexpr proto::ElementType kPopup = proto::ELEMENT_TYPE_POPUP;
  29. constexpr proto::ElementType kWebSocket = proto::ELEMENT_TYPE_WEBSOCKET;
  30. constexpr proto::ElementType kAllElementTypes = proto::ELEMENT_TYPE_ALL;
  31. constexpr proto::ActivationType kNoActivation =
  32. proto::ACTIVATION_TYPE_UNSPECIFIED;
  33. constexpr proto::ActivationType kDocument = proto::ACTIVATION_TYPE_DOCUMENT;
  34. constexpr proto::ActivationType kGenericBlock =
  35. proto::ACTIVATION_TYPE_GENERICBLOCK;
  36. constexpr proto::SourceType kAnyParty = proto::SOURCE_TYPE_ANY;
  37. constexpr proto::SourceType kThirdParty = proto::SOURCE_TYPE_THIRD_PARTY;
  38. constexpr proto::SourceType kFirstParty = proto::SOURCE_TYPE_FIRST_PARTY;
  39. // Helpers ---------------------------------------------------------------------
  40. // Creates a UrlRule with the given |url_pattern|, and all necessary fields
  41. // initialized to defaults.
  42. proto::UrlRule MakeUrlRule(const UrlPattern& url_pattern = UrlPattern());
  43. // Parses `initiator_domains` and adds them to the initiator domain list of the
  44. // `rule`.
  45. //
  46. // The `initiator_domains` vector should contain non-empty strings. If a string
  47. // starts with '~' then the following part of the string is an exception domain.
  48. void AddInitiatorDomains(const std::vector<std::string>& initiator_domains,
  49. proto::UrlRule* rule);
  50. // Parses `request_domains` and adds them to the request domain list of the
  51. // `rule`. See `AddInitiatorDomains`.
  52. void AddRequestDomains(const std::vector<std::string>& request_domains,
  53. proto::UrlRule* rule);
  54. // Returns the url::Origin parsed from |origin_string|, or the unique origin if
  55. // the string is empty.
  56. url::Origin GetOrigin(base::StringPiece origin_string);
  57. // Returns whether |url| is third-party resource w.r.t. |first_party_origin|.
  58. bool IsThirdParty(const GURL& url, const url::Origin& first_party_origin);
  59. } // namespace testing
  60. } // namespace url_pattern_index
  61. #endif // COMPONENTS_URL_PATTERN_INDEX_URL_RULE_TEST_SUPPORT_H_