1 // Copyright 2020 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 5 #ifndef COMPONENTS_JS_INJECTION_COMMON_ORIGIN_MATCHER_INTERNAL_H_ 6 #define COMPONENTS_JS_INJECTION_COMMON_ORIGIN_MATCHER_INTERNAL_H_ 7 8 #include <string> 9 10 #include "net/base/scheme_host_port_matcher.h" 11 12 // NOTE: this file is an implementation detail and only used by code in 13 // js_injection. 14 15 namespace js_injection { 16 17 enum class OriginMatcherRuleType { kAny, kSubdomain }; 18 19 // Common superclass that includes the type of matcher. 20 class OriginMatcherRule : public net::SchemeHostPortMatcherRule { 21 public: 22 explicit OriginMatcherRule(OriginMatcherRuleType type); 23 ~OriginMatcherRule() override; 24 type()25 OriginMatcherRuleType type() const { return type_; } 26 27 private: 28 const OriginMatcherRuleType type_; 29 }; 30 31 // Matches *all* urls. 32 class MatchAllOriginsRule : public OriginMatcherRule { 33 public: 34 MatchAllOriginsRule(); 35 MatchAllOriginsRule(const MatchAllOriginsRule&) = delete; 36 MatchAllOriginsRule& operator=(const MatchAllOriginsRule&) = delete; 37 ~MatchAllOriginsRule() override; 38 39 // OriginMatcherRule: 40 net::SchemeHostPortMatcherResult Evaluate(const GURL& url) const override; 41 std::string ToString() const override; 42 }; 43 44 // Matches against a specific scheme, optional host (potentially with 45 // wild-cards) and an optional port. 46 class SubdomainMatchingRule : public OriginMatcherRule { 47 public: 48 SubdomainMatchingRule(const std::string& scheme, 49 const std::string& optional_host, 50 int optional_port); 51 // This constructor is implemented only in tests, it does no checking of 52 // args. 53 SubdomainMatchingRule(const std::string& scheme, 54 const std::string& optional_host, 55 int optional_port, 56 bool for_test); 57 SubdomainMatchingRule(const SubdomainMatchingRule&) = delete; 58 SubdomainMatchingRule& operator=(const SubdomainMatchingRule&) = delete; 59 ~SubdomainMatchingRule() override; 60 61 // Returns true if |scheme| is a valid scheme identifier. 62 static bool IsValidScheme(const std::string& scheme); 63 64 // Returns true if the |scheme| is allowed to have a host and port part. 65 static bool CanSchemeHaveHost(const std::string& scheme); 66 67 // Returns true if |scheme| and |host| are valid. 68 static bool IsValidSchemeAndHost(const std::string& scheme, 69 const std::string& host); 70 scheme()71 const std::string& scheme() const { return scheme_; } optional_host()72 const std::string& optional_host() const { return optional_host_; } optional_port()73 int optional_port() const { return optional_port_; } 74 75 // OriginMatcherRule: 76 net::SchemeHostPortMatcherResult Evaluate(const GURL& url) const override; 77 std::string ToString() const override; 78 79 private: 80 const std::string scheme_; 81 // Empty string means no host provided. 82 const std::string optional_host_; 83 // -1 means no port provided. 84 const int optional_port_; 85 }; 86 87 } // namespace js_injection 88 89 #endif // COMPONENTS_JS_INJECTION_COMMON_ORIGIN_MATCHER_INTERNAL_H_ 90