From f95d490b787f88dbd848a15f2a24b9ea929874db Mon Sep 17 00:00:00 2001 From: Steve Block Date: Thu, 9 Jun 2011 11:53:26 +0100 Subject: [PATCH] Fixes WebView.loadData() to avoid constructing invalid URLs. Also updates the documentation to make clear how the encoding parameter should be used. Bug:4541798 Change-Id: I9283513fbd8bcc3dc54056cfa2d0ab2425681824 --- core/java/android/webkit/WebView.java | 35 +++++++++++++++++---------- 1 file changed, 22 insertions(+), 13 deletions(-) diff --git a/core/java/android/webkit/WebView.java b/core/java/android/webkit/WebView.java index 4861642013717..371ed80be5928 100644 --- a/core/java/android/webkit/WebView.java +++ b/core/java/android/webkit/WebView.java @@ -44,7 +44,6 @@ import android.graphics.Point; import android.graphics.Rect; import android.graphics.RectF; import android.graphics.Region; -import android.graphics.SurfaceTexture; import android.graphics.Shader; import android.graphics.drawable.Drawable; import android.net.Proxy; @@ -99,6 +98,8 @@ import android.widget.ListView; import android.widget.OverScroller; import android.widget.Toast; +import junit.framework.Assert; + import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; @@ -117,8 +118,6 @@ import java.util.Vector; import java.util.regex.Matcher; import java.util.regex.Pattern; -import junit.framework.Assert; - /** *

A View that displays web pages. This class is the basis upon which you * can roll your own web browser or simply display some online content within your Activity. @@ -173,7 +172,7 @@ import junit.framework.Assert; * * // OR, you can also load from an HTML string: * String summary = "<html><body>You scored <b>192</b> points.</body></html>"; - * webview.loadData(summary, "text/html", "utf-8"); + * webview.loadData(summary, "text/html", null); * // ... although note that there are restrictions on what this HTML can do. * // See the JavaDocs for {@link #loadData(String,String,String) loadData()} and {@link * #loadDataWithBaseURL(String,String,String,String,String) loadDataWithBaseURL()} for more info. @@ -1965,14 +1964,17 @@ public class WebView extends AbsoluteLayout } /** - * Load the given data into the WebView. This will load the data into - * WebView using the data: scheme. Content loaded through this mechanism - * does not have the ability to load content from the network. - * @param data A String of data in the given encoding. The date must - * be URI-escaped -- '#', '%', '\', '?' should be replaced by %23, %25, - * %27, %3f respectively. - * @param mimeType The MIMEType of the data. i.e. text/html, image/jpeg - * @param encoding The encoding of the data. i.e. utf-8, base64 + * Load the given data into the WebView using a 'data' scheme URL. Content + * loaded in this way does not have the ability to load content from the + * network. + *

+ * If the value of the encoding parameter is 'base64', then the data must + * be encoded as base64. Otherwise, the data must use ASCII encoding for + * octets inside the range of safe URL characters and use the standard %xx + * hex encoding of URLs for octets outside that range. + * @param data A String of data in the given encoding. + * @param mimeType The MIMEType of the data, e.g. 'text/html'. + * @param encoding The encoding of the data. */ public void loadData(String data, String mimeType, String encoding) { checkThread(); @@ -1980,7 +1982,14 @@ public class WebView extends AbsoluteLayout } private void loadDataImpl(String data, String mimeType, String encoding) { - loadUrlImpl("data:" + mimeType + ";" + encoding + "," + data); + StringBuilder dataUrl = new StringBuilder("data:"); + dataUrl.append(mimeType); + if ("base64".equals(encoding)) { + dataUrl.append(";base64"); + } + dataUrl.append(","); + dataUrl.append(data); + loadUrlImpl(dataUrl.toString()); } /**