summaryrefslogtreecommitdiff
path: root/core/java/android/webkit/WebResourceResponse.java
diff options
context:
space:
mode:
authorMarcin Kosiba <mkosiba@google.com>2014-07-15 17:33:47 +0100
committerMarcin Kosiba <mkosiba@google.com>2014-07-15 21:31:44 -0700
commitd72e7ba1c04b2f7b128c5710607a72867b73bf1c (patch)
tree0c904ff6fa5e988d6ca5d6c44213e58b7236fecc /core/java/android/webkit/WebResourceResponse.java
parent795903e83ddca05445607074fa7fa6369e37dd6d (diff)
Add more params to WebViewClient.shouldInterceptRequest.
This changes shouldInterceptRequest to take an object containing the following new parameters: isMainFrame, hasUserGesture, method, headers. This also lets the embedder specify the following additional properties of the response: status code, response phrase, headers. BUG: 7589347 Change-Id: Id922c5e7023eb067db871e6f782f599492a2428f
Diffstat (limited to 'core/java/android/webkit/WebResourceResponse.java')
-rw-r--r--core/java/android/webkit/WebResourceResponse.java97
1 files changed, 94 insertions, 3 deletions
diff --git a/core/java/android/webkit/WebResourceResponse.java b/core/java/android/webkit/WebResourceResponse.java
index f21e2b42a102..ad6e9aa31382 100644
--- a/core/java/android/webkit/WebResourceResponse.java
+++ b/core/java/android/webkit/WebResourceResponse.java
@@ -17,6 +17,7 @@
package android.webkit;
import java.io.InputStream;
+import java.util.Map;
/**
* Encapsulates a resource response. Applications can return an instance of this
@@ -24,9 +25,11 @@ import java.io.InputStream;
* response when the WebView requests a particular resource.
*/
public class WebResourceResponse {
- // Accessed by jni, do not rename without modifying the jni code.
private String mMimeType;
private String mEncoding;
+ private int mStatusCode;
+ private String mReasonPhrase;
+ private Map<String, String> mResponseHeaders;
private InputStream mInputStream;
/**
@@ -47,6 +50,28 @@ public class WebResourceResponse {
}
/**
+ * Constructs a resource response with the given parameters. Callers must
+ * implement {@link InputStream#read(byte[]) InputStream.read(byte[])} for
+ * the input stream.
+ *
+ * @param mimeType the resource response's MIME type, for example text/html
+ * @param encoding the resource response's encoding
+ * @param statusCode the status code needs to be in the ranges [100, 299], [400, 599].
+ * Causing a redirect by specifying a 3xx code is not supported.
+ * @param reasonPhrase the phrase describing the status code, for example "OK". Must be non-null
+ * and not empty.
+ * @param responseHeaders the resource response's headers represented as a mapping of header
+ * name -> header value.
+ * @param data the input stream that provides the resource response's data
+ */
+ public WebResourceResponse(String mimeType, String encoding, int statusCode,
+ String reasonPhrase, Map<String, String> responseHeaders, InputStream data) {
+ this(mimeType, encoding, data);
+ setStatusCodeAndReasonPhrase(statusCode, reasonPhrase);
+ setResponseHeaders(responseHeaders);
+ }
+
+ /**
* Sets the resource response's MIME type, for example text/html.
*
* @param mimeType the resource response's MIME type
@@ -84,7 +109,73 @@ public class WebResourceResponse {
}
/**
- * Sets the input stream that provides the resource respone's data. Callers
+ * Sets the resource response's status code and reason phrase.
+ *
+ * @param statusCode the status code needs to be in the ranges [100, 299], [400, 599].
+ * Causing a redirect by specifying a 3xx code is not supported.
+ * @param reasonPhrase the phrase describing the status code, for example "OK". Must be non-null
+ * and not empty.
+ */
+ public void setStatusCodeAndReasonPhrase(int statusCode, String reasonPhrase) {
+ if (statusCode < 100)
+ throw new IllegalArgumentException("statusCode can't be less than 100.");
+ if (statusCode > 599)
+ throw new IllegalArgumentException("statusCode can't be greater than 599.");
+ if (statusCode > 299 && statusCode < 400)
+ throw new IllegalArgumentException("statusCode can't be in the [300, 399] range.");
+ if (reasonPhrase == null)
+ throw new IllegalArgumentException("reasonPhrase can't be null.");
+ if (reasonPhrase.trim().isEmpty())
+ throw new IllegalArgumentException("reasonPhrase can't be empty.");
+ for (int i = 0; i < reasonPhrase.length(); i++) {
+ int c = reasonPhrase.charAt(i);
+ if (c > 0x7F) {
+ throw new IllegalArgumentException(
+ "reasonPhrase can't contain non-ASCII characters.");
+ }
+ }
+ mStatusCode = statusCode;
+ mReasonPhrase = reasonPhrase;
+ }
+
+ /**
+ * Gets the resource response's status code.
+ *
+ * @return the resource response's status code.
+ */
+ public int getStatusCode() {
+ return mStatusCode;
+ }
+
+ /**
+ * Gets the description of the resource response's status code.
+ *
+ * @return the description of the resource response's status code.
+ */
+ public String getReasonPhrase() {
+ return mReasonPhrase;
+ }
+
+ /**
+ * Sets the headers for the resource response.
+ *
+ * @param headers mapping of header name -> header value.
+ */
+ public void setResponseHeaders(Map<String, String> headers) {
+ mResponseHeaders = headers;
+ }
+
+ /**
+ * Gets the headers for the resource response.
+ *
+ * @return the headers for the resource response.
+ */
+ public Map<String, String> getResponseHeaders() {
+ return mResponseHeaders;
+ }
+
+ /**
+ * Sets the input stream that provides the resource response's data. Callers
* must implement {@link InputStream#read(byte[]) InputStream.read(byte[])}.
*
* @param data the input stream that provides the resource response's data
@@ -94,7 +185,7 @@ public class WebResourceResponse {
}
/**
- * Gets the input stream that provides the resource respone's data.
+ * Gets the input stream that provides the resource response's data.
*
* @return the input stream that provides the resource response's data
*/