Fix crash downloading posters for HTML5 video.

We use the apache HTTP stack to download the poster images
for HTML5 video. This will crash if there is no host to use
as the "Host" header when making the request. Limit the java
poster downloader to just http or https for now. WebKit seems
able to display posters over other schemes like file://, but
doesn't always get the dimensions right. This fix just stops
us crashing.

Bug: 3180037
Change-Id: Idf51efda5b9ca1f2fe373c1fdb9c6bb7d5e254c8
diff --git a/core/java/android/webkit/HTML5VideoViewProxy.java b/core/java/android/webkit/HTML5VideoViewProxy.java
index d8f34e0..3caf345 100644
--- a/core/java/android/webkit/HTML5VideoViewProxy.java
+++ b/core/java/android/webkit/HTML5VideoViewProxy.java
@@ -46,6 +46,8 @@
 
 import java.io.ByteArrayOutputStream;
 import java.io.IOException;
+import java.net.MalformedURLException;
+import java.net.URL;
 import java.util.HashMap;
 import java.util.Map;
 import java.util.Timer;
@@ -344,7 +346,7 @@
         private static RequestQueue mRequestQueue;
         private static int mQueueRefCount = 0;
         // The poster URL
-        private String mUrl;
+        private URL mUrl;
         // The proxy we're doing this for.
         private final HTML5VideoViewProxy mProxy;
         // The poster bytes. We only touch this on the network thread.
@@ -359,14 +361,30 @@
         private Handler mHandler;
 
         public PosterDownloader(String url, HTML5VideoViewProxy proxy) {
-            mUrl = url;
+            try {
+                mUrl = new URL(url);
+            } catch (MalformedURLException e) {
+                mUrl = null;
+            }
             mProxy = proxy;
             mHandler = new Handler();
         }
         // Start the download. Called on WebCore thread.
         public void start() {
             retainQueue();
-            mRequestHandle = mRequestQueue.queueRequest(mUrl, "GET", null, this, null, 0);
+
+            if (mUrl == null) {
+                return;
+            }
+
+            // Only support downloading posters over http/https.
+            // FIXME: Add support for other schemes. WebKit seems able to load
+            // posters over other schemes e.g. file://, but gets the dimensions wrong.
+            String protocol = mUrl.getProtocol();
+            if ("http".equals(protocol) || "https".equals(protocol)) {
+                mRequestHandle = mRequestQueue.queueRequest(mUrl.toString(), "GET", null,
+                        this, null, 0);
+            }
         }
         // Cancel the download if active and release the queue. Called on WebCore thread.
         public void cancelAndReleaseQueue() {
@@ -405,12 +423,16 @@
                 cleanup();
             } else if (mStatusCode >= 300 && mStatusCode < 400) {
                 // We have a redirect.
-                mUrl = mHeaders.getLocation();
+                try {
+                    mUrl = new URL(mHeaders.getLocation());
+                } catch (MalformedURLException e) {
+                    mUrl = null;
+                }
                 if (mUrl != null) {
                     mHandler.post(new Runnable() {
                        public void run() {
                            if (mRequestHandle != null) {
-                               mRequestHandle.setupRedirect(mUrl, mStatusCode,
+                               mRequestHandle.setupRedirect(mUrl.toString(), mStatusCode,
                                        new HashMap<String, String>());
                            }
                        }