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

Commit 25e89545 authored by Mikhail Naganov's avatar Mikhail Naganov
Browse files

Provide better error reporting for WebView

Add new callbacks into WebViewClient that will provide notifications
about subresource loading errors and HTTP errors received from servers.

Bug: 19313118
Change-Id: Idb47f53ef7e72cb95f3e9b89d1e238d69e896b8b
parent a92d6e28
Loading
Loading
Loading
Loading
+21 −2
Original line number Diff line number Diff line
@@ -36927,6 +36927,12 @@ package android.webkit {
    method public abstract void onReceivedIcon(java.lang.String, android.graphics.Bitmap);
  }
  public abstract class WebResourceError {
    ctor public WebResourceError();
    method public abstract java.lang.String getDescription();
    method public abstract int getErrorCode();
  }
  public abstract interface WebResourceRequest {
    method public abstract java.lang.String getMethod();
    method public abstract java.util.Map<java.lang.String, java.lang.String> getRequestHeaders();
@@ -36935,7 +36941,7 @@ package android.webkit {
    method public abstract boolean isForMainFrame();
  }
  public class WebResourceResponse {
  public class WebResourceResponse extends android.webkit.WebResourceResponseBase {
    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();
@@ -36951,6 +36957,16 @@ package android.webkit {
    method public void setStatusCodeAndReasonPhrase(int, java.lang.String);
  }
  public abstract class WebResourceResponseBase {
    ctor public WebResourceResponseBase();
    method public abstract java.io.InputStream getData();
    method public abstract java.lang.String getEncoding();
    method public abstract java.lang.String getMimeType();
    method public abstract java.lang.String getReasonPhrase();
    method public abstract java.util.Map<java.lang.String, java.lang.String> getResponseHeaders();
    method public abstract int getStatusCode();
  }
  public abstract class WebSettings {
    ctor public WebSettings();
    method public abstract deprecated boolean enableSmoothTransition();
@@ -37265,8 +37281,10 @@ package android.webkit {
    method public void onPageFinished(android.webkit.WebView, java.lang.String);
    method public void onPageStarted(android.webkit.WebView, java.lang.String, android.graphics.Bitmap);
    method public void onReceivedClientCertRequest(android.webkit.WebView, android.webkit.ClientCertRequest);
    method public void onReceivedError(android.webkit.WebView, int, java.lang.String, java.lang.String);
    method public deprecated void onReceivedError(android.webkit.WebView, int, java.lang.String, java.lang.String);
    method public void onReceivedError(android.webkit.WebView, android.webkit.WebResourceRequest, android.webkit.WebResourceError);
    method public void onReceivedHttpAuthRequest(android.webkit.WebView, android.webkit.HttpAuthHandler, java.lang.String, java.lang.String);
    method public void onReceivedHttpError(android.webkit.WebView, android.webkit.WebResourceRequest, android.webkit.WebResourceResponseBase);
    method public void onReceivedLoginRequest(android.webkit.WebView, java.lang.String, java.lang.String, java.lang.String);
    method public void onReceivedSslError(android.webkit.WebView, android.webkit.SslErrorHandler, android.net.http.SslError);
    method public void onScaleChanged(android.webkit.WebView, float, float);
@@ -37279,6 +37297,7 @@ package android.webkit {
    method public boolean shouldOverrideUrlLoading(android.webkit.WebView, java.lang.String);
    field public static final int ERROR_AUTHENTICATION = -4; // 0xfffffffc
    field public static final int ERROR_BAD_URL = -12; // 0xfffffff4
    field public static final int ERROR_BLOCKED = -16; // 0xfffffff0
    field public static final int ERROR_CONNECT = -6; // 0xfffffffa
    field public static final int ERROR_FAILED_SSL_HANDSHAKE = -11; // 0xfffffff5
    field public static final int ERROR_FILE = -13; // 0xfffffff3
+21 −2
Original line number Diff line number Diff line
@@ -39151,6 +39151,12 @@ package android.webkit {
    method public abstract void onReceivedIcon(java.lang.String, android.graphics.Bitmap);
  }
  public abstract class WebResourceError {
    ctor public WebResourceError();
    method public abstract java.lang.String getDescription();
    method public abstract int getErrorCode();
  }
  public abstract interface WebResourceRequest {
    method public abstract java.lang.String getMethod();
    method public abstract java.util.Map<java.lang.String, java.lang.String> getRequestHeaders();
@@ -39159,7 +39165,7 @@ package android.webkit {
    method public abstract boolean isForMainFrame();
  }
  public class WebResourceResponse {
  public class WebResourceResponse extends android.webkit.WebResourceResponseBase {
    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();
@@ -39175,6 +39181,16 @@ package android.webkit {
    method public void setStatusCodeAndReasonPhrase(int, java.lang.String);
  }
  public abstract class WebResourceResponseBase {
    ctor public WebResourceResponseBase();
    method public abstract java.io.InputStream getData();
    method public abstract java.lang.String getEncoding();
    method public abstract java.lang.String getMimeType();
    method public abstract java.lang.String getReasonPhrase();
    method public abstract java.util.Map<java.lang.String, java.lang.String> getResponseHeaders();
    method public abstract int getStatusCode();
  }
  public abstract class WebSettings {
    ctor public WebSettings();
    method public abstract deprecated boolean enableSmoothTransition();
@@ -39534,8 +39550,10 @@ package android.webkit {
    method public void onPageFinished(android.webkit.WebView, java.lang.String);
    method public void onPageStarted(android.webkit.WebView, java.lang.String, android.graphics.Bitmap);
    method public void onReceivedClientCertRequest(android.webkit.WebView, android.webkit.ClientCertRequest);
    method public void onReceivedError(android.webkit.WebView, int, java.lang.String, java.lang.String);
    method public deprecated void onReceivedError(android.webkit.WebView, int, java.lang.String, java.lang.String);
    method public void onReceivedError(android.webkit.WebView, android.webkit.WebResourceRequest, android.webkit.WebResourceError);
    method public void onReceivedHttpAuthRequest(android.webkit.WebView, android.webkit.HttpAuthHandler, java.lang.String, java.lang.String);
    method public void onReceivedHttpError(android.webkit.WebView, android.webkit.WebResourceRequest, android.webkit.WebResourceResponseBase);
    method public void onReceivedLoginRequest(android.webkit.WebView, java.lang.String, java.lang.String, java.lang.String);
    method public void onReceivedSslError(android.webkit.WebView, android.webkit.SslErrorHandler, android.net.http.SslError);
    method public void onScaleChanged(android.webkit.WebView, float, float);
@@ -39548,6 +39566,7 @@ package android.webkit {
    method public boolean shouldOverrideUrlLoading(android.webkit.WebView, java.lang.String);
    field public static final int ERROR_AUTHENTICATION = -4; // 0xfffffffc
    field public static final int ERROR_BAD_URL = -12; // 0xfffffff4
    field public static final int ERROR_BLOCKED = -16; // 0xfffffff0
    field public static final int ERROR_CONNECT = -6; // 0xfffffffa
    field public static final int ERROR_FAILED_SSL_HANDSHAKE = -11; // 0xfffffff5
    field public static final int ERROR_FILE = -13; // 0xfffffff3
+39 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2015 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;

/**
 * Encapsulates information about errors occured during loading of web resources. See
 * {@link WebViewClient#onReceivedError(WebView, WebResourceRequest, WebResourceError) WebViewClient.onReceivedError(WebView, WebResourceRequest, WebResourceError)}
 */
public abstract class WebResourceError {
    /**
     * Gets the error code of the error. The code corresponds to one
     * of the ERROR_* constants in {@link WebViewClient}.
     *
     * @return The error code of the error
     */
    public abstract int getErrorCode();

    /**
     * Gets the string describing the error. Descriptions are localized,
     * and thus can be used for communicating the problem to the user.
     *
     * @return The description of the error
     */
    public abstract String getDescription();
}
+18 −25
Original line number Diff line number Diff line
@@ -25,7 +25,7 @@ import java.util.Map;
 * class from {@link WebViewClient#shouldInterceptRequest} to provide a custom
 * response when the WebView requests a particular resource.
 */
public class WebResourceResponse {
public class WebResourceResponse extends WebResourceResponseBase {
    private String mMimeType;
    private String mEncoding;
    private int mStatusCode;
@@ -75,38 +75,36 @@ public class WebResourceResponse {
    }

    /**
     * Sets the resource response's MIME type, for example text/html.
     * Sets the resource response's MIME type, for example &quot;text/html&quot;.
     *
     * @param mimeType the resource response's MIME type
     * @param mimeType The resource response's MIME type
     */
    public void setMimeType(String mimeType) {
        mMimeType = mimeType;
    }

    /**
     * Gets the resource response's MIME type.
     *
     * @return the resource response's MIME type
     * {@inheritDoc}
     */
    @Override
    public String getMimeType() {
        return mMimeType;
    }

    /**
     * Sets the resource response's encoding, for example UTF-8. This is used
     * Sets the resource response's encoding, for example &quot;UTF-8&quot;. This is used
     * to decode the data from the input stream.
     *
     * @param encoding the resource response's encoding
     * @param encoding The resource response's encoding
     */
    public void setEncoding(String encoding) {
        mEncoding = encoding;
    }

    /**
     * Gets the resource response's encoding.
     *
     * @return the resource response's encoding
     * {@inheritDoc}
     */
    @Override
    public String getEncoding() {
        return mEncoding;
    }
@@ -142,19 +140,17 @@ public class WebResourceResponse {
    }

    /**
     * Gets the resource response's status code.
     *
     * @return the resource response's status code.
     * {@inheritDoc}
     */
    @Override
    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.
     * {@inheritDoc}
     */
    @Override
    public String getReasonPhrase() {
        return mReasonPhrase;
    }
@@ -162,17 +158,16 @@ public class WebResourceResponse {
    /**
     * Sets the headers for the resource response.
     *
     * @param headers mapping of header name -> header value.
     * @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.
     * {@inheritDoc}
     */
    @Override
    public Map<String, String> getResponseHeaders() {
        return mResponseHeaders;
    }
@@ -190,15 +185,13 @@ public class WebResourceResponse {
            throw new IllegalArgumentException("StringBufferInputStream is deprecated and must " +
                "not be passed to a WebResourceResponse");
        }

        mInputStream = data;
    }

    /**
     * Gets the input stream that provides the resource response's data.
     *
     * @return the input stream that provides the resource response's data
     * {@inheritDoc}
     */
    @Override
    public InputStream getData() {
        return mInputStream;
    }
+68 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2015 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 java.io.InputStream;
import java.util.Map;

/**
 * Encapsulates a resource response received from the server.
 * This is an abstract class used by WebView callbacks.
 */
public abstract class WebResourceResponseBase {
    /**
     * Gets the resource response's MIME type.
     *
     * @return The resource response's MIME type
     */
    public abstract String getMimeType();

    /**
     * Gets the resource response's encoding.
     *
     * @return The resource response's encoding
     */
    public abstract String getEncoding();

    /**
     * Gets the resource response's status code.
     *
     * @return The resource response's status code.
     */
    public abstract int getStatusCode();

    /**
     * Gets the description of the resource response's status code.
     *
     * @return The description of the resource response's status code.
     */
    public abstract String getReasonPhrase();

    /**
     * Gets the headers for the resource response.
     *
     * @return The headers for the resource response.
     */
    public abstract Map<String, String> getResponseHeaders();

    /**
     * Gets the input stream that provides the resource response's data.
     *
     * @return The input stream that provides the resource response's data
     */
    public abstract InputStream getData();
}
Loading