Bug 1330454 - Remove deprecated subdomain/suffix stripping code draft
authorAndrzej Hunt <ahunt@mozilla.com>
Tue, 31 Jan 2017 14:25:00 -0800
changeset 468634 f8c269b4c2b088db6c10a9e342ba5d750ffe8ef0
parent 468633 469af524a326eeadf40e2644e5719a9872510b76
child 468635 7cb7f1d931609010a62b7f471ef3451aff5dddd1
push id43542
push userahunt@mozilla.com
push dateTue, 31 Jan 2017 22:38:41 +0000
bugs1330454
milestone54.0a1
Bug 1330454 - Remove deprecated subdomain/suffix stripping code This is now obsolete thanks to DomainProcessor. MozReview-Commit-ID: 2Jd7gj98XsN
mobile/android/base/moz.build
mobile/android/geckoview/src/main/java/org/mozilla/gecko/util/StringUtils.java
mobile/android/geckoview/src/main/java/org/mozilla/gecko/util/publicsuffix/PublicSuffix.java
mobile/android/tests/background/junit4/src/org/mozilla/gecko/util/TestStringUtils.java
mobile/android/tests/background/junit4/src/org/mozilla/gecko/util/publicsuffix/TestPublicSuffix.java
--- a/mobile/android/base/moz.build
+++ b/mobile/android/base/moz.build
@@ -158,17 +158,16 @@ gujar.sources += [geckoview_source_dir +
     'util/NativeEventListener.java',
     'util/NativeJSContainer.java',
     'util/NativeJSObject.java',
     'util/NetworkUtils.java',
     'util/NonEvictingLruCache.java',
     'util/PrefUtils.java',
     'util/ProxySelector.java',
     'util/publicsuffix/DomainProcessor.java',
-    'util/publicsuffix/PublicSuffix.java',
     'util/publicsuffix/PublicSuffixPatterns.java',
     'util/RawResource.java',
     'util/StringUtils.java',
     'util/ThreadUtils.java',
     'util/UIAsyncTask.java',
     'util/UUIDUtil.java',
     'util/WeakReferenceHandler.java',
     'util/WindowUtils.java',
--- a/mobile/android/geckoview/src/main/java/org/mozilla/gecko/util/StringUtils.java
+++ b/mobile/android/geckoview/src/main/java/org/mozilla/gecko/util/StringUtils.java
@@ -4,29 +4,27 @@
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 package org.mozilla.gecko.util;
 
 import android.net.Uri;
 import android.support.annotation.NonNull;
 import android.text.TextUtils;
 
+
 import java.nio.charset.Charset;
-import java.util.Collections;
-import java.util.LinkedHashSet;
 import java.util.List;
 import java.util.Set;
 
 public class StringUtils {
     private static final String LOGTAG = "GeckoStringUtils";
 
     private static final String FILTER_URL_PREFIX = "filter://";
     private static final String USER_ENTERED_URL_PREFIX = "user-entered:";
 
-
     /**
      * The UTF-8 charset.
      */
     public static final Charset UTF_8 = Charset.forName("UTF-8");
 
     /*
      * This method tries to guess if the given string could be a search query or URL,
      * and returns a previous result if there is ambiguity
@@ -121,36 +119,16 @@ public class StringUtils {
     public static boolean isHttpOrHttps(String url) {
         if (TextUtils.isEmpty(url)) {
             return false;
         }
 
         return url.startsWith("http://") || url.startsWith("https://");
     }
 
-    public static String stripCommonSubdomains(String host) {
-        if (host == null) {
-            return host;
-        }
-
-        // In contrast to desktop, we also strip mobile subdomains,
-        // since its unlikely users are intentionally typing them
-        int start = 0;
-
-        if (host.startsWith("www.")) {
-            start = 4;
-        } else if (host.startsWith("mobile.")) {
-            start = 7;
-        } else if (host.startsWith("m.")) {
-            start = 2;
-        }
-
-        return host.substring(start);
-    }
-
     /**
      * Searches the url query string for the first value with the given key.
      */
     public static String getQueryParameter(String url, String desiredKey) {
         if (TextUtils.isEmpty(url) || TextUtils.isEmpty(desiredKey)) {
             return null;
         }
 
deleted file mode 100644
--- a/mobile/android/geckoview/src/main/java/org/mozilla/gecko/util/publicsuffix/PublicSuffix.java
+++ /dev/null
@@ -1,121 +0,0 @@
-/* This Source Code Form is subject to the terms of the Mozilla Public
- * License, v. 2.0. If a copy of the MPL was not distributed with this file,
- * You can obtain one at http://mozilla.org/MPL/2.0/. */
-
-package org.mozilla.gecko.util.publicsuffix;
-
-import android.content.Context;
-import android.support.annotation.NonNull;
-import android.support.annotation.WorkerThread;
-
-import org.mozilla.gecko.util.StringUtils;
-
-import java.util.ArrayList;
-import java.util.Collections;
-import java.util.List;
-import java.util.Set;
-
-/**
- * Helper methods for the public suffix part of a domain.
- *
- * A "public suffix" is one under which Internet users can (or historically could) directly register
- * names. Some examples of public suffixes are .com, .co.uk and pvt.k12.ma.us.
- *
- * https://publicsuffix.org/
- *
- * Some parts of the implementation of this class are based on InternetDomainName class of the Guava
- * project: https://github.com/google/guava
- */
-public class PublicSuffix {
-    /**
-     * Strip the public suffix from the domain. Returns the original domain if no public suffix
-     * could be found.
-     *
-     * www.mozilla.org -> www.mozilla
-     * independent.co.uk -> independent
-     */
-    @NonNull
-    @WorkerThread // This method might need to load data from disk
-    public static String stripPublicSuffix(Context context, @NonNull String domain) {
-        if (domain.length() == 0) {
-            return domain;
-        }
-
-        final int index = findPublicSuffixIndex(context, domain);
-        if (index == -1) {
-            return domain;
-        }
-
-        return domain.substring(0, index);
-    }
-
-    /**
-     * Returns the index of the leftmost part of the public suffix, or -1 if not found.
-     */
-    @WorkerThread
-    private static int findPublicSuffixIndex(Context context, String domain) {
-        final List<String> parts = normalizeAndSplit(domain);
-        final int partsSize = parts.size();
-        final Set<String> exact = PublicSuffixPatterns.getExactSet(context);
-
-        for (int i = 0; i < partsSize; i++) {
-            String ancestorName = StringUtils.join(".", parts.subList(i, partsSize));
-
-            if (exact.contains(ancestorName)) {
-                return joinIndex(parts, i);
-            }
-
-            // Excluded domains (e.g. !nhs.uk) use the next highest
-            // domain as the effective public suffix (e.g. uk).
-            if (PublicSuffixPatterns.EXCLUDED.contains(ancestorName)) {
-                return joinIndex(parts, i + 1);
-            }
-
-            if (matchesWildcardPublicSuffix(ancestorName)) {
-                return joinIndex(parts, i);
-            }
-        }
-
-        return -1;
-    }
-
-    /**
-     * Normalize domain and split into domain parts (www.mozilla.org -> [www, mozilla, org]).
-     */
-    private static List<String> normalizeAndSplit(String domain) {
-        domain = domain.replaceAll("[.\u3002\uFF0E\uFF61]", "."); // All dot-like characters to '.'
-        domain = domain.toLowerCase();
-
-        if (domain.endsWith(".")) {
-            domain = domain.substring(0, domain.length() - 1); // Strip trailing '.'
-        }
-
-        List<String> parts = new ArrayList<>();
-        Collections.addAll(parts, domain.split("\\."));
-
-        return parts;
-    }
-
-    /**
-     * Translate the index of the leftmost part of the public suffix to the index of the domain string.
-     *
-     * [www, mozilla, org] and 2 => 12 (www.mozilla)
-     */
-    private static int joinIndex(List<String> parts, int index) {
-        int actualIndex = parts.get(0).length();
-
-        for (int i = 1; i < index; i++) {
-            actualIndex += parts.get(i).length() + 1; // Add one for the "." that is not part of the list elements
-        }
-
-        return actualIndex;
-    }
-
-    /**
-     * Does the domain name match one of the "wildcard" patterns (e.g. {@code "*.ar"})?
-     */
-    private static boolean matchesWildcardPublicSuffix(String domain) {
-        final String[] pieces = domain.split("\\.", 2);
-        return pieces.length == 2 && PublicSuffixPatterns.UNDER.contains(pieces[1]);
-    }
-}
--- a/mobile/android/tests/background/junit4/src/org/mozilla/gecko/util/TestStringUtils.java
+++ b/mobile/android/tests/background/junit4/src/org/mozilla/gecko/util/TestStringUtils.java
@@ -1,28 +1,33 @@
 /* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*-
  * This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 package org.mozilla.gecko.util;
 
+import android.content.Context;
+
 import org.junit.Test;
 import org.junit.runner.RunWith;
 import org.mozilla.gecko.background.testhelpers.TestRunner;
+import org.robolectric.RuntimeEnvironment;
 
 import java.util.Arrays;
 import java.util.Collections;
 
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertTrue;
 
 @RunWith(TestRunner.class)
 public class TestStringUtils {
+    final Context context = RuntimeEnvironment.application;
+
     @Test
     public void testIsHttpOrHttps() {
         // No value
         assertFalse(StringUtils.isHttpOrHttps(null));
         assertFalse(StringUtils.isHttpOrHttps(""));
 
         // Garbage
         assertFalse(StringUtils.isHttpOrHttps("lksdjflasuf"));
deleted file mode 100644
--- a/mobile/android/tests/background/junit4/src/org/mozilla/gecko/util/publicsuffix/TestPublicSuffix.java
+++ /dev/null
@@ -1,62 +0,0 @@
-package org.mozilla.gecko.util.publicsuffix;
-
-import org.junit.Assert;
-import org.junit.Test;
-import org.junit.runner.RunWith;
-import org.mozilla.gecko.background.testhelpers.TestRunner;
-import org.robolectric.RuntimeEnvironment;
-
-@RunWith(TestRunner.class)
-public class TestPublicSuffix {
-    @Test
-    public void testStripPublicSuffix() {
-        // Test empty value
-        Assert.assertEquals("",
-                PublicSuffix.stripPublicSuffix(RuntimeEnvironment.application, ""));
-
-        // Test domains with public suffix
-        Assert.assertEquals("www.mozilla",
-                PublicSuffix.stripPublicSuffix(RuntimeEnvironment.application, "www.mozilla.org"));
-        Assert.assertEquals("www.google",
-                PublicSuffix.stripPublicSuffix(RuntimeEnvironment.application, "www.google.com"));
-        Assert.assertEquals("foobar",
-                PublicSuffix.stripPublicSuffix(RuntimeEnvironment.application, "foobar.blogspot.com"));
-        Assert.assertEquals("independent",
-                PublicSuffix.stripPublicSuffix(RuntimeEnvironment.application, "independent.co.uk"));
-        Assert.assertEquals("biz",
-                PublicSuffix.stripPublicSuffix(RuntimeEnvironment.application, "biz.com.ua"));
-        Assert.assertEquals("example",
-                PublicSuffix.stripPublicSuffix(RuntimeEnvironment.application, "example.org"));
-        Assert.assertEquals("example",
-                PublicSuffix.stripPublicSuffix(RuntimeEnvironment.application, "example.pvt.k12.ma.us"));
-
-        // Test domain without public suffix
-        Assert.assertEquals("localhost",
-                PublicSuffix.stripPublicSuffix(RuntimeEnvironment.application, "localhost"));
-        Assert.assertEquals("firefox.mozilla",
-                PublicSuffix.stripPublicSuffix(RuntimeEnvironment.application, "firefox.mozilla"));
-
-        // IDN domains
-        Assert.assertEquals("ουτοπία.δπθ",
-                PublicSuffix.stripPublicSuffix(RuntimeEnvironment.application, "ουτοπία.δπθ.gr"));
-        Assert.assertEquals("a网络A",
-                PublicSuffix.stripPublicSuffix(RuntimeEnvironment.application, "a网络A.网络.Cn"));
-
-        // Other non-domain values
-        Assert.assertEquals("192.168.0.1",
-                PublicSuffix.stripPublicSuffix(RuntimeEnvironment.application, "192.168.0.1"));
-        Assert.assertEquals("asdflkj9uahsd",
-                PublicSuffix.stripPublicSuffix(RuntimeEnvironment.application, "asdflkj9uahsd"));
-
-        // Other trailing and other types of dots
-        Assert.assertEquals("www.mozilla。home.example",
-                PublicSuffix.stripPublicSuffix(RuntimeEnvironment.application, "www.mozilla。home.example。org"));
-        Assert.assertEquals("example",
-                PublicSuffix.stripPublicSuffix(RuntimeEnvironment.application, "example.org"));
-    }
-
-    @Test(expected = NullPointerException.class)
-    public void testStripPublicSuffixThrowsException() {
-        PublicSuffix.stripPublicSuffix(RuntimeEnvironment.application, null);
-    }
-}