Bug 920855 - Replace java.net.URL with java.net.URI wherever possible. r=rnewman
authorClemens Wilding <clemens.wilding+firefox@gmail.com>
Wed, 16 Apr 2014 08:42:15 -0700
changeset 179328 1ebef0fc77de07dd28b8db8cdcd65871d45211ad
parent 179327 e066aad9ede3930313510eda9e2ac8b0cd2d0994
child 179329 566c01fe0d987d9a0115f206401307999a717601
push id272
push userpvanderbeken@mozilla.com
push dateMon, 05 May 2014 16:31:18 +0000
reviewersrnewman
bugs920855
milestone31.0a1
Bug 920855 - Replace java.net.URL with java.net.URI wherever possible. r=rnewman
mobile/android/base/GeckoAppShell.java
mobile/android/base/WebappImpl.java
mobile/android/base/util/GeckoJarReader.java
mobile/android/base/util/JSONUtils.java
mobile/android/base/webapp/InstallListener.java
mobile/android/base/webapp/WebappImpl.java
--- a/mobile/android/base/GeckoAppShell.java
+++ b/mobile/android/base/GeckoAppShell.java
@@ -3,26 +3,22 @@
  * 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;
 
 import java.io.BufferedReader;
 import java.io.Closeable;
 import java.io.File;
-import java.io.FileOutputStream;
 import java.io.FileReader;
 import java.io.IOException;
-import java.io.InputStream;
 import java.io.InputStreamReader;
-import java.io.OutputStream;
 import java.io.PrintWriter;
 import java.io.StringWriter;
 import java.net.Proxy;
-import java.net.URL;
 import java.nio.ByteBuffer;
 import java.util.ArrayList;
 import java.util.Iterator;
 import java.util.List;
 import java.util.Locale;
 import java.util.Map;
 import java.util.NoSuchElementException;
 import java.util.Queue;
@@ -105,17 +101,16 @@ import android.view.ContextThemeWrapper;
 import android.view.HapticFeedbackConstants;
 import android.view.Surface;
 import android.view.SurfaceView;
 import android.view.TextureView;
 import android.view.View;
 import android.view.inputmethod.InputMethodManager;
 import android.webkit.MimeTypeMap;
 import android.widget.AbsoluteLayout;
-import android.widget.Toast;
 
 public class GeckoAppShell
 {
     private static final String LOGTAG = "GeckoAppShell";
     private static final boolean LOGGING = false;
 
     // We have static members only.
     private GeckoAppShell() { }
--- a/mobile/android/base/WebappImpl.java
+++ b/mobile/android/base/WebappImpl.java
@@ -18,23 +18,23 @@ import android.graphics.Bitmap;
 import android.graphics.Color;
 import android.graphics.drawable.Drawable;
 import android.graphics.drawable.GradientDrawable;
 import android.view.animation.AnimationUtils;
 import android.view.animation.Animation;
 import android.widget.ImageView;
 import android.view.Display;
 
-import java.net.URL;
 import java.io.File;
+import java.net.URI;
 
 public class WebappImpl extends GeckoApp {
     private static final String LOGTAG = "GeckoWebappImpl";
 
-    private URL mOrigin;
+    private URI mOrigin;
     private TextView mTitlebarText = null;
     private View mTitlebar = null;
 
     private View mSplashscreen;
 
     protected int getIndex() { return 0; }
 
     @Override
@@ -64,28 +64,28 @@ public class WebappImpl extends GeckoApp
         if (!action.startsWith(ACTION_WEBAPP_PREFIX)) {
             Log.e(LOGTAG, "Webapp launch, but intent action is " + action + "!");
             return;
         }
 
         // Try to use the origin stored in the WebappAllocator first
         String origin = WebappAllocator.getInstance(this).getAppForIndex(getIndex());
         try {
-            mOrigin = new URL(origin);
-        } catch (java.net.MalformedURLException ex) {
+            mOrigin = new URI(origin);
+        } catch (java.net.URISyntaxException ex) {
             // If we can't parse the this is an app protocol, just settle for not having an origin
             if (!origin.startsWith("app://")) {
                 return;
             }
 
             // If that failed fall back to the origin stored in the shortcut
             Log.i(LOGTAG, "Webapp is not registered with allocator");
             try {
-                mOrigin = new URL(getIntent().getData().toString());
-            } catch (java.net.MalformedURLException ex2) {
+                mOrigin = new URI(getIntent().getData().toString());
+            } catch (java.net.URISyntaxException ex2) {
                 Log.e(LOGTAG, "Unable to parse intent url: ", ex);
             }
         }
     }
 
     @Override
     protected void loadStartupTab(String uri) {
         String action = getIntent().getAction();
@@ -159,38 +159,38 @@ public class WebappImpl extends GeckoApp
 
     @Override
     public void onTabChanged(Tab tab, Tabs.TabEvents msg, Object data) {
         switch(msg) {
             case SELECTED:
             case LOCATION_CHANGE:
                 if (Tabs.getInstance().isSelectedTab(tab)) {
                     final String urlString = tab.getURL();
-                    final URL url;
+                    final URI uri;
 
                     try {
-                        url = new URL(urlString);
-                    } catch (java.net.MalformedURLException ex) {
+                        uri = new URI(urlString);
+                    } catch (java.net.URISyntaxException ex) {
                         mTitlebarText.setText(urlString);
 
                         // If we can't parse the url, and its an app protocol hide
                         // the titlebar and return, otherwise show the titlebar
                         // and the full url
                         if (!urlString.startsWith("app://")) {
                             mTitlebar.setVisibility(View.VISIBLE);
                         } else {
                             mTitlebar.setVisibility(View.GONE);
                         }
                         return;
                     }
 
-                    if (mOrigin != null && mOrigin.getHost().equals(url.getHost())) {
+                    if (mOrigin != null && mOrigin.getHost().equals(uri.getHost())) {
                         mTitlebar.setVisibility(View.GONE);
                     } else {
-                        mTitlebarText.setText(url.getProtocol() + "://" + url.getHost());
+                        mTitlebarText.setText(uri.getScheme() + "://" + uri.getHost());
                         mTitlebar.setVisibility(View.VISIBLE);
                     }
                 }
                 break;
             case LOADED:
                 if (mSplashscreen != null && mSplashscreen.getVisibility() == View.VISIBLE) {
                     Animation fadeout = AnimationUtils.loadAnimation(this, android.R.anim.fade_out);
                     fadeout.setAnimationListener(new Animation.AnimationListener() {
--- a/mobile/android/base/util/GeckoJarReader.java
+++ b/mobile/android/base/util/GeckoJarReader.java
@@ -11,17 +11,18 @@ import android.graphics.Bitmap;
 import android.graphics.drawable.BitmapDrawable;
 import android.util.Log;
 import org.mozilla.gecko.mozglue.RobocopTarget;
 
 import java.io.BufferedReader;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.InputStreamReader;
-import java.net.URL;
+import java.net.URI;
+import java.net.URISyntaxException;
 import java.util.Stack;
 
 /* Reads out of a multiple level deep jar file such as
  *  jar:jar:file:///data/app/org.mozilla.fennec.apk!/omni.ja!/chrome/chrome/content/branding/favicon32.png
  */
 public final class GeckoJarReader {
     private static final String LOGTAG = "GeckoJarReader";
 
@@ -42,16 +43,18 @@ public final class GeckoJarReader {
             // Load the initial jar file as a zip
             zip = getZipFile(jarUrls.pop());
             inputStream = getStream(zip, jarUrls, url);
             if (inputStream != null) {
                 bitmap = new BitmapDrawable(resources, inputStream);
             }
         } catch (IOException ex) {
             Log.e(LOGTAG, "Exception ", ex);
+        } catch (URISyntaxException ex) {
+            Log.e(LOGTAG, "Exception ", ex);
         } finally {
             if (inputStream != null) {
                 try {
                     inputStream.close();
                 } catch(IOException ex) {
                     Log.e(LOGTAG, "Error closing stream", ex);
                 }
             }
@@ -73,34 +76,36 @@ public final class GeckoJarReader {
             zip = getZipFile(jarUrls.pop());
             InputStream input = getStream(zip, jarUrls, url);
             if (input != null) {
                 reader = new BufferedReader(new InputStreamReader(input));
                 text = reader.readLine();
             }
         } catch (IOException ex) {
             Log.e(LOGTAG, "Exception ", ex);
+        } catch (URISyntaxException ex) {
+            Log.e(LOGTAG, "Exception ", ex);
         } finally {
             if (reader != null) {
                 try {
                     reader.close();
                 } catch(IOException ex) {
                     Log.e(LOGTAG, "Error closing reader", ex);
                 }
             }
             if (zip != null) {
                 zip.close();
             }
         }
 
         return text;
     }
 
-    private static NativeZip getZipFile(String url) throws IOException {
-        URL fileUrl = new URL(url);
+    private static NativeZip getZipFile(String url) throws IOException, URISyntaxException {
+        URI fileUrl = new URI(url);
         return new NativeZip(fileUrl.getPath());
     }
 
     @RobocopTarget
     public static InputStream getStream(String url) {
         Stack<String> jarUrls = parseUrl(url);
         try {
             NativeZip zip = getZipFile(jarUrls.pop());
--- a/mobile/android/base/util/JSONUtils.java
+++ b/mobile/android/base/util/JSONUtils.java
@@ -1,51 +1,22 @@
 /* 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 java.util.UUID;
+
 import org.json.JSONException;
 import org.json.JSONObject;
 
-import android.util.Log;
-
-import java.net.MalformedURLException;
-import java.net.URL;
-import java.util.UUID;
-
 public final class JSONUtils {
-    private static final String LOGTAG = "JSONUtils";
-
     private JSONUtils() {}
 
-    public static URL getURL(String name, JSONObject json) {
-        String url = json.optString(name, null);
-        if (url == null) {
-            return null;
-        }
-
-        try {
-            return new URL(url);
-        } catch (MalformedURLException e) {
-            Log.e(LOGTAG, "", new IllegalStateException(name + "=" + url, e));
-            return null;
-        }
-    }
-
-    public static void putURL(String name, URL url, JSONObject json) {
-        String urlString = url.toString();
-        try {
-            json.put(name, urlString);
-        } catch (JSONException e) {
-            throw new IllegalArgumentException(name + "=" + urlString, e);
-        }
-    }
-
     public static UUID getUUID(String name, JSONObject json) {
         String uuid = json.optString(name, null);
         return (uuid != null) ? UUID.fromString(uuid) : null;
     }
 
     public static void putUUID(String name, UUID uuid, JSONObject json) {
         String uuidString = uuid.toString();
         try {
--- a/mobile/android/base/webapp/InstallListener.java
+++ b/mobile/android/base/webapp/InstallListener.java
@@ -2,18 +2,16 @@
  * 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.webapp;
 
 import java.io.File;
 import java.io.IOException;
-import java.net.MalformedURLException;
-import java.net.URL;
 
 import org.json.JSONException;
 import org.json.JSONObject;
 import org.mozilla.gecko.GeckoThread;
 
 import android.content.BroadcastReceiver;
 import android.content.Context;
 import android.content.Intent;
--- a/mobile/android/base/webapp/WebappImpl.java
+++ b/mobile/android/base/webapp/WebappImpl.java
@@ -2,55 +2,48 @@
  * 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.webapp;
 
 import java.io.File;
 import java.io.IOException;
-import java.net.MalformedURLException;
-import java.net.URL;
+import java.net.URI;
 
 import org.json.JSONException;
 import org.json.JSONObject;
 import org.mozilla.gecko.GeckoApp;
 import org.mozilla.gecko.GeckoAppShell;
 import org.mozilla.gecko.GeckoEvent;
 import org.mozilla.gecko.GeckoThread;
 import org.mozilla.gecko.R;
 import org.mozilla.gecko.Tab;
 import org.mozilla.gecko.Tabs;
-import org.mozilla.gecko.webapp.ApkResources;
-import org.mozilla.gecko.webapp.InstallHelper;
 import org.mozilla.gecko.webapp.InstallHelper.InstallCallback;
 
-import android.content.Context;
 import android.content.Intent;
-import android.content.SharedPreferences;
 import android.content.pm.PackageManager.NameNotFoundException;
-import android.graphics.Bitmap;
 import android.graphics.Color;
-import android.graphics.drawable.BitmapDrawable;
 import android.graphics.drawable.Drawable;
 import android.graphics.drawable.GradientDrawable;
 import android.net.Uri;
 import android.os.Bundle;
 import android.util.Log;
 import android.view.Display;
 import android.view.View;
 import android.view.animation.Animation;
 import android.view.animation.AnimationUtils;
 import android.widget.ImageView;
 import android.widget.TextView;
 
 public class WebappImpl extends GeckoApp implements InstallCallback {
     private static final String LOGTAG = "GeckoWebappImpl";
 
-    private URL mOrigin;
+    private URI mOrigin;
     private TextView mTitlebarText = null;
     private View mTitlebar = null;
 
     private View mSplashscreen;
 
     private boolean mIsApk = true;
     private ApkResources mApkResources;
     private String mManifestUrl;
@@ -267,38 +260,38 @@ public class WebappImpl extends GeckoApp
                     // Don't show the titlebar for about:blank, which we load
                     // into the initial tab we create while waiting for the app
                     // to load.
                     if (urlString != null && urlString.equals("about:blank")) {
                         mTitlebar.setVisibility(View.GONE);
                         return;
                     }
 
-                    final URL url;
+                    final URI uri;
 
                     try {
-                        url = new URL(urlString);
-                    } catch (java.net.MalformedURLException ex) {
+                        uri = new URI(urlString);
+                    } catch (java.net.URISyntaxException ex) {
                         mTitlebarText.setText(urlString);
 
                         // If we can't parse the url, and its an app protocol hide
                         // the titlebar and return, otherwise show the titlebar
                         // and the full url
                         if (urlString != null && !urlString.startsWith("app://")) {
                             mTitlebar.setVisibility(View.VISIBLE);
                         } else {
                             mTitlebar.setVisibility(View.GONE);
                         }
                         return;
                     }
 
-                    if (mOrigin != null && mOrigin.getHost().equals(url.getHost())) {
+                    if (mOrigin != null && mOrigin.getHost().equals(uri.getHost())) {
                         mTitlebar.setVisibility(View.GONE);
                     } else {
-                        mTitlebarText.setText(url.getProtocol() + "://" + url.getHost());
+                        mTitlebarText.setText(uri.getScheme() + "://" + uri.getHost());
                         mTitlebar.setVisibility(View.VISIBLE);
                     }
                 }
                 break;
             case LOADED:
                 hideSplash();
                 break;
             case START:
@@ -345,31 +338,31 @@ public class WebappImpl extends GeckoApp
 
     @Override
     public void installErrored(InstallHelper installHelper, Exception exception) {
         Log.e(LOGTAG, "Install errored", exception);
     }
 
     private void setOrigin(String origin) {
         try {
-            mOrigin = new URL(origin);
-        } catch (java.net.MalformedURLException ex) {
+            mOrigin = new URI(origin);
+        } catch (java.net.URISyntaxException ex) {
             // If this isn't an app: URL, just settle for not having an origin.
             if (!origin.startsWith("app://")) {
                 return;
             }
 
             // If that failed fall back to the origin stored in the shortcut.
             if (!mIsApk) {
                 Log.i(LOGTAG, "Origin is app: URL; falling back to intent URL");
                 Uri data = getIntent().getData();
                 if (data != null) {
                     try {
-                        mOrigin = new URL(data.toString());
-                    } catch (java.net.MalformedURLException ex2) {
+                        mOrigin = new URI(data.toString());
+                    } catch (java.net.URISyntaxException ex2) {
                         Log.e(LOGTAG, "Unable to parse intent URL: ", ex);
                     }
                 }
             }
         }
     }
 
     public void launchWebapp(String origin) {