Introduce subdomain wild matching for legacy tech event

With the patch, example.com will now match www.example.com or
s.example.com.

www.example.com will still match www.example.com exactly.

Also add tests for ip address.

Bug: 1501132
Change-Id: I51b1f1519d7f935965c8c3bd77a5c8b2de6c7e8b
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/5018881
Reviewed-by: Parastoo Geranmayeh <parastoog@google.com>
Code-Coverage: findit-for-me@appspot.gserviceaccount.com <findit-for-me@appspot.gserviceaccount.com>
Auto-Submit: Owen Min <zmin@chromium.org>
Commit-Queue: Owen Min <zmin@chromium.org>
Commit-Queue: Parastoo Geranmayeh <parastoog@google.com>
Cr-Commit-Position: refs/heads/main@{#1222610}
diff --git a/chrome/browser/enterprise/reporting/legacy_tech/legacy_tech_url_matcher.cc b/chrome/browser/enterprise/reporting/legacy_tech/legacy_tech_url_matcher.cc
index b093fbe..1f8eb78 100644
--- a/chrome/browser/enterprise/reporting/legacy_tech/legacy_tech_url_matcher.cc
+++ b/chrome/browser/enterprise/reporting/legacy_tech/legacy_tech_url_matcher.cc
@@ -40,7 +40,6 @@
     // Scheme, port and query in the pattern will be ignored while subdomains
     // must be fully specified.
     components.scheme = "";
-    components.match_subdomains = false;
     components.port = 0;
     components.query = "";
 
diff --git a/chrome/browser/enterprise/reporting/legacy_tech/legacy_tech_url_matcher_unittest.cc b/chrome/browser/enterprise/reporting/legacy_tech/legacy_tech_url_matcher_unittest.cc
index 2bf8ee0..97f82342 100644
--- a/chrome/browser/enterprise/reporting/legacy_tech/legacy_tech_url_matcher_unittest.cc
+++ b/chrome/browser/enterprise/reporting/legacy_tech/legacy_tech_url_matcher_unittest.cc
@@ -63,6 +63,26 @@
   EXPECT_FALSE(matcher.GetMatchedURL(GURL("https://chat.example2.com")));
 }
 
+TEST_F(LegacyURLMatcherTest, SubDomain) {
+  LegacyTechURLMatcher matcher{profile()};
+  SetPolicy({"www.example.com", "example2.com", ".example3.com"});
+
+  // Only subdomain www is matched
+  EXPECT_FALSE(matcher.GetMatchedURL(GURL("https://example.com")));
+  EXPECT_FALSE(matcher.GetMatchedURL(GURL("https://chat.example.com")));
+  EXPECT_TRUE(matcher.GetMatchedURL(GURL("https://www.example.com")));
+
+  // All subdomains are matched.
+  EXPECT_TRUE(matcher.GetMatchedURL(GURL("https://example2.com")));
+  EXPECT_TRUE(matcher.GetMatchedURL(GURL("https://chat.example2.com")));
+  EXPECT_TRUE(matcher.GetMatchedURL(GURL("https://www.example2.com")));
+  EXPECT_TRUE(matcher.GetMatchedURL(GURL("https://s1.s2.example2.com")));
+
+  // Subdomain wildcard matching is disabled with prefix dot.
+  EXPECT_TRUE(matcher.GetMatchedURL(GURL("https://example3.com")));
+  EXPECT_FALSE(matcher.GetMatchedURL(GURL("https://chat.example3.com")));
+}
+
 TEST_F(LegacyURLMatcherTest, PathPrecedence) {
   LegacyTechURLMatcher matcher{profile()};
   SetPolicy({"www.example.com", "www.example.com/p1", "www.example.com/p1/p2"});
@@ -104,6 +124,15 @@
             *matcher.GetMatchedURL(GURL("https://localhost/path2")));
 }
 
+TEST_F(LegacyURLMatcherTest, IP) {
+  LegacyTechURLMatcher matcher{profile()};
+  SetPolicy({"192.168.1.1", "192.168.1./path"});
+
+  EXPECT_EQ("192.168.1.1",
+            *matcher.GetMatchedURL(GURL("https://192.168.1.1/path2")));
+  EXPECT_FALSE(matcher.GetMatchedURL(GURL("https://192.168.1.2/path")));
+}
+
 TEST_F(LegacyURLMatcherTest, File) {
   LegacyTechURLMatcher matcher{profile()};