Bug 1254089 - LoadFaviconTask: Add proxy support. r=rnewman
authorSebastian Kaspari <s.kaspari@gmail.com>
Mon, 07 Mar 2016 19:09:05 +0100
changeset 324926 c8d1f83532a7fe4289a473e3528c04b8b2f85a64
parent 324925 d8c0657ff5d34dd1a62d63830f967e0d215b9e2b
child 324927 815bc777e53b1678ed0b1f1841305ece830c5857
push id6048
push userkmoir@mozilla.com
push dateMon, 06 Jun 2016 19:02:08 +0000
treeherdermozilla-beta@46d72a56c57d [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersrnewman
bugs1254089
milestone47.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1254089 - LoadFaviconTask: Add proxy support. r=rnewman MozReview-Commit-ID: lc8ufeTQ1Z
mobile/android/base/java/org/mozilla/gecko/favicons/LoadFaviconTask.java
mobile/android/base/java/org/mozilla/gecko/updater/UpdateService.java
mobile/android/base/java/org/mozilla/gecko/util/ProxySelector.java
--- a/mobile/android/base/java/org/mozilla/gecko/favicons/LoadFaviconTask.java
+++ b/mobile/android/base/java/org/mozilla/gecko/favicons/LoadFaviconTask.java
@@ -11,16 +11,17 @@ import android.text.TextUtils;
 import android.util.Log;
 import org.mozilla.gecko.GeckoAppShell;
 import org.mozilla.gecko.GeckoProfile;
 import org.mozilla.gecko.db.BrowserDB;
 import org.mozilla.gecko.favicons.decoders.FaviconDecoder;
 import org.mozilla.gecko.favicons.decoders.LoadFaviconResult;
 import org.mozilla.gecko.util.GeckoJarReader;
 import org.mozilla.gecko.util.IOUtils;
+import org.mozilla.gecko.util.ProxySelector;
 import org.mozilla.gecko.util.ThreadUtils;
 
 import java.io.IOException;
 import java.io.InputStream;
 import java.net.HttpURLConnection;
 import java.net.URI;
 import java.net.URISyntaxException;
 import java.util.ArrayList;
@@ -132,17 +133,17 @@ public class LoadFaviconTask {
         visitedLinkSet.add(faviconURI.toString());
         return tryDownloadRecurse(faviconURI, visitedLinkSet);
     }
     private Response tryDownloadRecurse(URI faviconURI, HashSet<String> visited) throws URISyntaxException, IOException {
         if (visited.size() == MAX_REDIRECTS_TO_FOLLOW) {
             return null;
         }
 
-        HttpURLConnection connection = (HttpURLConnection) faviconURI.toURL().openConnection();
+        HttpURLConnection connection = (HttpURLConnection) ProxySelector.openConnectionWithProxy(faviconURI);
         connection.setRequestProperty("User-Agent", GeckoAppShell.getGeckoInterface().getDefaultUAString());
 
         connection.connect();
 
         // Was the response a failure?
         int status = connection.getResponseCode();
 
         // Handle HTTP status codes requesting a redirect.
--- a/mobile/android/base/java/org/mozilla/gecko/updater/UpdateService.java
+++ b/mobile/android/base/java/org/mozilla/gecko/updater/UpdateService.java
@@ -7,16 +7,17 @@ package org.mozilla.gecko.updater;
 
 import org.mozilla.gecko.AppConstants;
 import org.mozilla.gecko.CrashHandler;
 import org.mozilla.gecko.R;
 
 import org.mozilla.apache.commons.codec.binary.Hex;
 
 import org.mozilla.gecko.permissions.Permissions;
+import org.mozilla.gecko.util.ProxySelector;
 import org.w3c.dom.Document;
 import org.w3c.dom.Node;
 import org.w3c.dom.NodeList;
 
 import android.Manifest;
 import android.app.AlarmManager;
 import android.app.IntentService;
 import android.app.Notification;
@@ -42,18 +43,16 @@ import android.util.Log;
 
 import java.io.BufferedInputStream;
 import java.io.BufferedOutputStream;
 import java.io.File;
 import java.io.FileInputStream;
 import java.io.FileOutputStream;
 import java.io.InputStream;
 import java.io.OutputStream;
-import java.net.Proxy;
-import java.net.ProxySelector;
 import java.net.URI;
 import java.net.URL;
 import java.net.URLConnection;
 import java.security.MessageDigest;
 import java.util.Calendar;
 import java.util.GregorianCalendar;
 import java.util.List;
 import java.util.TimeZone;
@@ -368,42 +367,27 @@ public class UpdateService extends Inten
             builder.setContentTitle(getString(R.string.updater_apply_title));
             builder.setContentText(getString(R.string.updater_apply_select));
             builder.setContentIntent(contentIntent);
 
             mNotificationManager.notify(NOTIFICATION_ID, builder.build());
         }
     }
 
-    private URLConnection openConnectionWithProxy(URI uri) throws java.net.MalformedURLException, java.io.IOException {
-        Log.i(LOGTAG, "opening connection with URI: " + uri);
-
-        ProxySelector ps = ProxySelector.getDefault();
-        Proxy proxy = Proxy.NO_PROXY;
-        if (ps != null) {
-            List<Proxy> proxies = ps.select(uri);
-            if (proxies != null && !proxies.isEmpty()) {
-                proxy = proxies.get(0);
-            }
-        }
-
-        return uri.toURL().openConnection(proxy);
-    }
-
     private UpdateInfo findUpdate(boolean force) {
         try {
             URI uri = getUpdateURI(force);
 
             if (uri == null) {
               Log.e(LOGTAG, "failed to get update URI");
               return null;
             }
 
             DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
-            Document dom = builder.parse(openConnectionWithProxy(uri).getInputStream());
+            Document dom = builder.parse(ProxySelector.openConnectionWithProxy(uri).getInputStream());
 
             NodeList nodes = dom.getElementsByTagName("update");
             if (nodes == null || nodes.getLength() == 0)
                 return null;
 
             Node updateNode = nodes.item(0);
             Node buildIdNode = updateNode.getAttributes().getNamedItem("buildID");
             if (buildIdNode == null)
@@ -574,17 +558,17 @@ public class UpdateService extends Inten
 
         try {
             NetworkInfo netInfo = mConnectivityManager.getActiveNetworkInfo();
             if (netInfo != null && netInfo.isConnected() &&
                 netInfo.getType() == ConnectivityManager.TYPE_WIFI) {
                 mWifiLock.acquire();
             }
 
-            URLConnection conn = openConnectionWithProxy(info.uri);
+            URLConnection conn = ProxySelector.openConnectionWithProxy(info.uri);
             int length = conn.getContentLength();
 
             output = new BufferedOutputStream(new FileOutputStream(downloadFile));
             input = new BufferedInputStream(conn.getInputStream());
 
             byte[] buf = new byte[BUFSIZE];
             int len = 0;
 
--- a/mobile/android/base/java/org/mozilla/gecko/util/ProxySelector.java
+++ b/mobile/android/base/java/org/mozilla/gecko/util/ProxySelector.java
@@ -18,18 +18,34 @@
 
 package org.mozilla.gecko.util;
 
 import android.text.TextUtils;
 
 import java.io.IOException;
 import java.net.InetSocketAddress;
 import java.net.Proxy;
+import java.net.URI;
+import java.net.URLConnection;
+import java.util.List;
 
 public class ProxySelector {
+    public static URLConnection openConnectionWithProxy(URI uri) throws IOException {
+        java.net.ProxySelector ps = java.net.ProxySelector.getDefault();
+        Proxy proxy = Proxy.NO_PROXY;
+        if (ps != null) {
+            List<Proxy> proxies = ps.select(uri);
+            if (proxies != null && !proxies.isEmpty()) {
+                proxy = proxies.get(0);
+            }
+        }
+
+        return uri.toURL().openConnection(proxy);
+    }
+
     public ProxySelector() {
     }
 
     public Proxy select(String scheme, String host) {
         int port = -1;
         Proxy proxy = null;
         String nonProxyHostsKey = null;
         boolean httpProxyOkay = true;