Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit d72e7ba1 authored by Marcin Kosiba's avatar Marcin Kosiba
Browse files

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
parent 795903e8
Loading
Loading
Loading
Loading
+16 −1
Original line number Diff line number Diff line
@@ -36171,14 +36171,28 @@ package android.webkit {
    method public abstract void onReceivedIcon(java.lang.String, android.graphics.Bitmap);
  }
  public abstract interface WebResourceRequest {
    method public abstract java.lang.String getMethod();
    method public abstract java.util.Map<java.lang.String, java.lang.String> getRequestHeaders();
    method public abstract android.net.Uri getUrl();
    method public abstract boolean hasUserGestureInsecure();
    method public abstract boolean isForMainFrame();
  }
  public class WebResourceResponse {
    ctor public WebResourceResponse(java.lang.String, java.lang.String, java.io.InputStream);
    ctor public WebResourceResponse(java.lang.String, java.lang.String, int, java.lang.String, java.util.Map<java.lang.String, java.lang.String>, java.io.InputStream);
    method public java.io.InputStream getData();
    method public java.lang.String getEncoding();
    method public java.lang.String getMimeType();
    method public java.lang.String getReasonPhrase();
    method public java.util.Map<java.lang.String, java.lang.String> getResponseHeaders();
    method public int getStatusCode();
    method public void setData(java.io.InputStream);
    method public void setEncoding(java.lang.String);
    method public void setMimeType(java.lang.String);
    method public void setResponseHeaders(java.util.Map<java.lang.String, java.lang.String>);
    method public void setStatusCodeAndReasonPhrase(int, java.lang.String);
  }
  public abstract class WebSettings {
@@ -36502,7 +36516,8 @@ package android.webkit {
    method public deprecated void onTooManyRedirects(android.webkit.WebView, android.os.Message, android.os.Message);
    method public void onUnhandledInputEvent(android.webkit.WebView, android.view.InputEvent);
    method public deprecated void onUnhandledKeyEvent(android.webkit.WebView, android.view.KeyEvent);
    method public android.webkit.WebResourceResponse shouldInterceptRequest(android.webkit.WebView, java.lang.String);
    method public deprecated android.webkit.WebResourceResponse shouldInterceptRequest(android.webkit.WebView, java.lang.String);
    method public android.webkit.WebResourceResponse shouldInterceptRequest(android.webkit.WebView, android.webkit.WebResourceRequest);
    method public boolean shouldOverrideKeyEvent(android.webkit.WebView, android.view.KeyEvent);
    method public boolean shouldOverrideUrlLoading(android.webkit.WebView, java.lang.String);
    field public static final int ERROR_AUTHENTICATION = -4; // 0xfffffffc
+68 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2014 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package android.webkit;

import android.net.Uri;

import java.io.InputStream;
import java.util.Map;

/**
 * Encompasses parameters to the {@link WebViewClient#shouldInterceptRequest} method.
 */
public interface WebResourceRequest {
    /**
     * Gets the URL for which the resource request was made.
     *
     * @return the URL for which the resource request was made.
     */
    Uri getUrl();

    /**
     * Gets whether the request was made for the main frame.
     *
     * @return whether the request was made for the main frame. Will be false for iframes,
     *         for example.
     */
    boolean isForMainFrame();

    /**
     * Gets whether a gesture was associated with the request.
     * <p>
     * <strong>IMPORTANT:</strong>
     * This should not be used to implement any form of security. It is possible for the content
     * to spoof this.
     *
     * @return whether a gesture was associated with the request.
     */
    boolean hasUserGestureInsecure();

    /**
     * Gets the method associated with the request, for example "GET".
     *
     * @return the method associated with the request.
     */
    String getMethod();

    /**
     * Gets the headers associated with the request. These are represented as a mapping of header
     * name to header value.
     *
     * @return the headers associated with the request.
     */
    Map<String, String> getRequestHeaders();
}
+94 −3
Original line number Diff line number Diff line
@@ -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;

    /**
@@ -46,6 +49,28 @@ public class WebResourceResponse {
        mInputStream = data;
    }

    /**
     * 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.
     *
@@ -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
     */
+23 −0
Original line number Diff line number Diff line
@@ -96,12 +96,35 @@ public class WebViewClient {
     * @return A {@link android.webkit.WebResourceResponse} containing the
     *         response information or null if the WebView should load the
     *         resource itself.
     * @deprecated Use {@link #shouldInterceptRequest(WebView, WebResourceRequest)
     *             shouldInterceptRequest(WebView, WebResourceRequest)} instead.
     */
    @Deprecated
    public WebResourceResponse shouldInterceptRequest(WebView view,
            String url) {
        return null;
    }

    /**
     * Notify the host application of a resource request and allow the
     * application to return the data.  If the return value is null, the WebView
     * will continue to load the resource as usual.  Otherwise, the return
     * response and data will be used.  NOTE: This method is called on a thread
     * other than the UI thread so clients should exercise caution
     * when accessing private data or the view system.
     *
     * @param view The {@link android.webkit.WebView} that is requesting the
     *             resource.
     * @param request Object containing the details of the request.
     * @return A {@link android.webkit.WebResourceResponse} containing the
     *         response information or null if the WebView should load the
     *         resource itself.
     */
    public WebResourceResponse shouldInterceptRequest(WebView view,
            WebResourceRequest request) {
        return shouldInterceptRequest(view, request.getUrl().toString());
    }

    /**
     * Notify the host application that there have been an excessive number of
     * HTTP redirects. As the host application if it would like to continue