Bug 792992 - Switch URL usage to URI to prevent unnecessary network calls. r=rnewman, a=lmandel
authorEugen Sawin <esawin@mozilla.com>
Mon, 09 Feb 2015 22:06:22 +0100
changeset 250255 bd0696c04755
parent 250254 7113cd46019c
child 250256 e969067d440d
push id4527
push userryanvm@gmail.com
push date2015-03-05 16:09 +0000
treeherdermozilla-beta@00bad6e2ffbc [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersrnewman, lmandel
bugs792992
milestone37.0
Bug 792992 - Switch URL usage to URI to prevent unnecessary network calls. r=rnewman, a=lmandel
mobile/android/base/updater/UpdateService.java
mobile/android/base/updater/UpdateServiceHelper.java
--- a/mobile/android/base/updater/UpdateService.java
+++ b/mobile/android/base/updater/UpdateService.java
@@ -37,16 +37,17 @@ 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;
 
@@ -338,42 +339,42 @@ public class UpdateService extends Inten
             notification.setLatestEventInfo(this, getResources().getString(R.string.updater_apply_title),
                                             getResources().getString(R.string.updater_apply_select),
                                             contentIntent);
 
             mNotificationManager.notify(NOTIFICATION_ID, notification);
         }
     }
 
-    private URLConnection openConnectionWithProxy(URL url) throws java.net.URISyntaxException, java.io.IOException {
-        Log.i(LOGTAG, "opening connection with url: " + url);
+    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(url.toURI());
+            List<Proxy> proxies = ps.select(uri);
             if (proxies != null && !proxies.isEmpty()) {
                 proxy = proxies.get(0);
             }
         }
 
-        return url.openConnection(proxy);
+        return uri.toURL().openConnection(proxy);
     }
 
     private UpdateInfo findUpdate(boolean force) {
         try {
-            URL url = getUpdateUrl(force);
+            URI uri = getUpdateURI(force);
 
-            if (url == null) {
-              Log.e(LOGTAG, "failed to get update URL");
+            if (uri == null) {
+              Log.e(LOGTAG, "failed to get update URI");
               return null;
             }
 
             DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
-            Document dom = builder.parse(openConnectionWithProxy(url).getInputStream());
+            Document dom = builder.parse(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)
@@ -391,17 +392,17 @@ public class UpdateService extends Inten
 
             if (urlNode == null || hashFunctionNode == null ||
                 hashValueNode == null || sizeNode == null) {
                 return null;
             }
 
             // Fill in UpdateInfo from the XML data
             UpdateInfo info = new UpdateInfo();
-            info.url = new URL(urlNode.getTextContent());
+            info.uri = new URI(urlNode.getTextContent());
             info.buildID = buildIdNode.getTextContent();
             info.hashFunction = hashFunctionNode.getTextContent();
             info.hashValue = hashValueNode.getTextContent();
 
             try {
                 info.size = Integer.parseInt(sizeNode.getTextContent());
             } catch (NumberFormatException e) {
                 Log.e(LOGTAG, "Failed to find APK size: ", e);
@@ -495,19 +496,27 @@ public class UpdateService extends Inten
 
         pkg.delete();
         Log.i(LOGTAG, "deleted update package: " + path);
 
         return true;
     }
 
     private File downloadUpdatePackage(UpdateInfo info, boolean overwriteExisting) {
+        URL url = null;
+        try {
+            url = info.uri.toURL();
+        } catch (java.net.MalformedURLException e) {
+            Log.e(LOGTAG, "failed to read URL: ", e);
+            return null;
+        }
+
         File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
         path.mkdirs();
-        String fileName = new File(info.url.getFile()).getName();
+        String fileName = new File(url.getFile()).getName();
         File downloadFile = new File(path, fileName);
 
         if (!overwriteExisting && info.buildID.equals(getLastBuildID()) && downloadFile.exists()) {
             // The last saved buildID is the same as the one for the current update. We also have a file
             // already downloaded, so it's probably the package we want. Verify it to be sure and just
             // return that if it matches.
 
             if (verifyDownloadedPackage(downloadFile)) {
@@ -525,17 +534,17 @@ public class UpdateService extends Inten
         OutputStream output = null;
         InputStream input = null;
 
         mDownloading = true;
         mCancelDownload = false;
         showDownloadNotification(downloadFile);
 
         try {
-            URLConnection conn = openConnectionWithProxy(info.url);
+            URLConnection conn = 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;
 
@@ -684,18 +693,18 @@ public class UpdateService extends Inten
     }
 
     private void setAutoDownloadPolicy(AutoDownloadPolicy policy) {
         SharedPreferences.Editor editor = mPrefs.edit();
         editor.putInt(KEY_AUTODOWNLOAD_POLICY, policy.value);
         editor.commit();
     }
 
-    private URL getUpdateUrl(boolean force) {
-        return UpdateServiceHelper.expandUpdateUrl(this, mPrefs.getString(KEY_UPDATE_URL, null), force);
+    private URI getUpdateURI(boolean force) {
+        return UpdateServiceHelper.expandUpdateURI(this, mPrefs.getString(KEY_UPDATE_URL, null), force);
     }
 
     private void setUpdateUrl(String url) {
         SharedPreferences.Editor editor = mPrefs.edit();
         editor.putString(KEY_UPDATE_URL, url);
         editor.commit();
     }
 
@@ -704,29 +713,29 @@ public class UpdateService extends Inten
         editor.putString(KEY_LAST_BUILDID, info.buildID);
         editor.putString(KEY_LAST_HASH_FUNCTION, info.hashFunction);
         editor.putString(KEY_LAST_HASH_VALUE, info.hashValue);
         editor.putString(KEY_LAST_FILE_NAME, downloaded.toString());
         editor.commit();
     }
 
     private class UpdateInfo {
-        public URL url;
+        public URI uri;
         public String buildID;
         public String hashFunction;
         public String hashValue;
         public int size;
 
         private boolean isNonEmpty(String s) {
             return s != null && s.length() > 0;
         }
 
         public boolean isValid() {
-            return url != null && isNonEmpty(buildID) &&
+            return uri != null && isNonEmpty(buildID) &&
                 isNonEmpty(hashFunction) && isNonEmpty(hashValue) && size > 0;
         }
 
         @Override
         public String toString() {
-            return "url = " + url + ", buildID = " + buildID + ", hashFunction = " + hashFunction + ", hashValue = " + hashValue + ", size = " + size;
+            return "uri = " + uri + ", buildID = " + buildID + ", hashFunction = " + hashFunction + ", hashValue = " + hashValue + ", size = " + size;
         }
     }
 }
--- a/mobile/android/base/updater/UpdateServiceHelper.java
+++ b/mobile/android/base/updater/UpdateServiceHelper.java
@@ -12,17 +12,17 @@ import org.mozilla.gecko.util.GeckoJarRe
 
 import android.content.Context;
 import android.content.Intent;
 import android.content.pm.PackageManager;
 import android.content.pm.ApplicationInfo;
 import android.os.Build;
 import android.util.Log;
 
-import java.net.URL;
+import java.net.URI;
 import java.util.ArrayList;
 import java.util.HashMap;
 
 public class UpdateServiceHelper {
     public static final String ACTION_REGISTER_FOR_UPDATES = AppConstants.ANDROID_PACKAGE_NAME + ".REGISTER_FOR_UPDATES";
     public static final String ACTION_UNREGISTER_FOR_UPDATES = AppConstants.ANDROID_PACKAGE_NAME + ".UNREGISTER_FOR_UPDATES";
     public static final String ACTION_CHECK_FOR_UPDATE = AppConstants.ANDROID_PACKAGE_NAME + ".CHECK_FOR_UPDATE";
     public static final String ACTION_CHECK_UPDATE_RESULT = AppConstants.ANDROID_PACKAGE_NAME + ".CHECK_UPDATE_RESULT";
@@ -82,18 +82,18 @@ public class UpdateServiceHelper {
         }
     }
 
     @RobocopTarget
     public static void setEnabled(final boolean enabled) {
         isEnabled = enabled;
     }
 
-    public static URL expandUpdateUrl(Context context, String updateUrl, boolean force) {
-        if (updateUrl == null) {
+    public static URI expandUpdateURI(Context context, String updateUri, boolean force) {
+        if (updateUri == null) {
             return null;
         }
 
         PackageManager pm = context.getPackageManager();
 
         String pkgSpecial = AppConstants.MOZ_PKG_SPECIAL != null ?
                             "-" + AppConstants.MOZ_PKG_SPECIAL :
                             "";
@@ -107,30 +107,30 @@ public class UpdateServiceHelper {
             if (jarLocale != null) {
                 locale = jarLocale.trim();
             }
         } catch (android.content.pm.PackageManager.NameNotFoundException e) {
             // Shouldn't really be possible, but fallback to default locale
             Log.i(LOGTAG, "Failed to read update locale file, falling back to " + locale);
         }
 
-        String url = updateUrl.replace("%PRODUCT%", AppConstants.MOZ_APP_BASENAME)
+        String url = updateUri.replace("%PRODUCT%", AppConstants.MOZ_APP_BASENAME)
             .replace("%VERSION%", AppConstants.MOZ_APP_VERSION)
             .replace("%BUILD_ID%", force ? "0" : AppConstants.MOZ_APP_BUILDID)
             .replace("%BUILD_TARGET%", "Android_" + AppConstants.MOZ_APP_ABI + pkgSpecial)
             .replace("%LOCALE%", locale)
             .replace("%CHANNEL%", AppConstants.MOZ_UPDATE_CHANNEL)
             .replace("%OS_VERSION%", Build.VERSION.RELEASE)
             .replace("%DISTRIBUTION%", "default")
             .replace("%DISTRIBUTION_VERSION%", "default")
             .replace("%MOZ_VERSION%", AppConstants.MOZILLA_VERSION);
 
         try {
-            return new URL(url);
-        } catch (java.net.MalformedURLException e) {
+            return new URI(url);
+        } catch (java.net.URISyntaxException e) {
             Log.e(LOGTAG, "Failed to create update url: ", e);
             return null;
         }
     }
 
     public static boolean isUpdaterEnabled() {
         return AppConstants.MOZ_UPDATER && isEnabled;
     }