Skip to content

Commit

Permalink
Android: Fix many WrongConstant errors
Browse files Browse the repository at this point in the history
Add WebviewErrorCode @IntDef.

Bug: 1116130
Change-Id: I740ad5b03eb2c03effd16413b57640d872b00a0a
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2401813
Reviewed-by: Theresa  <twellington@chromium.org>
Reviewed-by: Xi Han <hanxi@chromium.org>
Reviewed-by: Bo <boliu@chromium.org>
Reviewed-by: Andrew Grieve <agrieve@chromium.org>
Commit-Queue: Peter Wen <wnwen@chromium.org>
Auto-Submit: Peter Wen <wnwen@chromium.org>
Cr-Commit-Position: refs/heads/master@{#806706}
  • Loading branch information
Peter Wen authored and Commit Bot committed Sep 14, 2020
1 parent 0ebf5c2 commit 4f91a35
Show file tree
Hide file tree
Showing 31 changed files with 204 additions and 458 deletions.
1 change: 1 addition & 0 deletions android_webview/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,7 @@ android_library("browser_java") {
"java/src/org/chromium/android_webview/WebMessageListenerHolder.java",
"java/src/org/chromium/android_webview/WebMessageListenerInfo.java",
"java/src/org/chromium/android_webview/WebViewChromiumRunQueue.java",
"java/src/org/chromium/android_webview/WebviewErrorCode.java",
"java/src/org/chromium/android_webview/gfx/AwDrawFnImpl.java",
"java/src/org/chromium/android_webview/gfx/AwFunctor.java",
"java/src/org/chromium/android_webview/gfx/AwGLFunctor.java",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ public AwWebResourceRequest(String url, boolean isMainFrame, boolean hasUserGest
* Parameters for {@link AwContentsClient#onReceivedError} method.
*/
public static class AwWebResourceError {
public int errorCode = ErrorCodeConversionHelper.ERROR_UNKNOWN;
public @WebviewErrorCode int errorCode = WebviewErrorCode.ERROR_UNKNOWN;
public String description;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -304,14 +304,14 @@ private void onReceivedError(
AwContentsClient.AwWebResourceRequest request = new AwContentsClient.AwWebResourceRequest(
url, isMainFrame, hasUserGesture, method, requestHeaderNames, requestHeaderValues);
AwContentsClient.AwWebResourceError error = new AwContentsClient.AwWebResourceError();
error.errorCode = errorCode;
error.errorCode = ErrorCodeConversionHelper.convertErrorCode(errorCode);
error.description = description;

String unreachableWebDataUrl = AwContentsStatics.getUnreachableWebDataUrl();
boolean isErrorUrl =
unreachableWebDataUrl != null && unreachableWebDataUrl.equals(request.url);

if ((!isErrorUrl && error.errorCode != NetError.ERR_ABORTED) || safebrowsingHit) {
if ((!isErrorUrl && errorCode != NetError.ERR_ABORTED) || safebrowsingHit) {
// NetError.ERR_ABORTED error code is generated for the following reasons:
// - WebView.stopLoading is called;
// - the navigation is intercepted by the embedder via shouldOverrideUrlLoading;
Expand All @@ -326,10 +326,8 @@ private void onReceivedError(
// dismissed.
return;
} else {
error.errorCode = ErrorCodeConversionHelper.ERROR_UNSAFE_RESOURCE;
error.errorCode = WebviewErrorCode.ERROR_UNSAFE_RESOURCE;
}
} else {
error.errorCode = ErrorCodeConversionHelper.convertErrorCode(error.errorCode);
}
if (request.isMainFrame
&& AwFeatureList.pageStartedOnCommitEnabled(isRendererInitiated)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public void titleWasSet(String title) {
@Override
public void didFinishNavigation(NavigationHandle navigation) {
String url = navigation.getUrl();
if (navigation.errorCode() != 0 && !navigation.isDownload()) {
if (navigation.errorCode() != NetError.OK && !navigation.isDownload()) {
didFailLoad(navigation.isInMainFrame(), navigation.errorCode(), url);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,90 +4,52 @@

package org.chromium.android_webview;

import android.webkit.WebViewClient;

import org.chromium.net.NetError;

/**
* This is a helper class to map native error code about loading a page to Android specific ones.
*/
public final class ErrorCodeConversionHelper {
// Success
public static final int ERROR_OK = 0;
// Generic error
public static final int ERROR_UNKNOWN = WebViewClient.ERROR_UNKNOWN;
// Server or proxy hostname lookup failed
public static final int ERROR_HOST_LOOKUP = WebViewClient.ERROR_HOST_LOOKUP;
// Unsupported authentication scheme (not basic or digest)
public static final int ERROR_UNSUPPORTED_AUTH_SCHEME =
WebViewClient.ERROR_UNSUPPORTED_AUTH_SCHEME;
// User authentication failed on server
public static final int ERROR_AUTHENTICATION = WebViewClient.ERROR_AUTHENTICATION;
// User authentication failed on proxy
public static final int ERROR_PROXY_AUTHENTICATION = WebViewClient.ERROR_PROXY_AUTHENTICATION;
// Failed to connect to the server
public static final int ERROR_CONNECT = WebViewClient.ERROR_CONNECT;
// Failed to read or write to the server
public static final int ERROR_IO = WebViewClient.ERROR_IO;
// Connection timed out
public static final int ERROR_TIMEOUT = WebViewClient.ERROR_TIMEOUT;
// Too many redirects
public static final int ERROR_REDIRECT_LOOP = WebViewClient.ERROR_REDIRECT_LOOP;
// Unsupported URI scheme
public static final int ERROR_UNSUPPORTED_SCHEME = WebViewClient.ERROR_UNSUPPORTED_SCHEME;
// Failed to perform SSL handshake
public static final int ERROR_FAILED_SSL_HANDSHAKE = WebViewClient.ERROR_FAILED_SSL_HANDSHAKE;
// Malformed URL
public static final int ERROR_BAD_URL = WebViewClient.ERROR_BAD_URL;
// Generic file error
public static final int ERROR_FILE = WebViewClient.ERROR_FILE;
// File not found
public static final int ERROR_FILE_NOT_FOUND = WebViewClient.ERROR_FILE_NOT_FOUND;
// Too many requests during this load
public static final int ERROR_TOO_MANY_REQUESTS = WebViewClient.ERROR_TOO_MANY_REQUESTS;
// Request was identified as a bad url by safebrowsing.
public static final int ERROR_UNSAFE_RESOURCE = WebViewClient.ERROR_UNSAFE_RESOURCE;

static int convertErrorCode(@NetError int netError) {
static @WebviewErrorCode int convertErrorCode(@NetError int netError) {
// Note: many NetError.Error constants don't have an obvious mapping.
// These will be handled by the default case, ERROR_UNKNOWN.
switch (netError) {
case NetError.ERR_UNSUPPORTED_AUTH_SCHEME:
return ERROR_UNSUPPORTED_AUTH_SCHEME;
return WebviewErrorCode.ERROR_UNSUPPORTED_AUTH_SCHEME;

case NetError.ERR_INVALID_AUTH_CREDENTIALS:
case NetError.ERR_MISSING_AUTH_CREDENTIALS:
case NetError.ERR_MISCONFIGURED_AUTH_ENVIRONMENT:
return ERROR_AUTHENTICATION;
return WebviewErrorCode.ERROR_AUTHENTICATION;

case NetError.ERR_TOO_MANY_REDIRECTS:
return ERROR_REDIRECT_LOOP;
return WebviewErrorCode.ERROR_REDIRECT_LOOP;

case NetError.ERR_UPLOAD_FILE_CHANGED:
return ERROR_FILE_NOT_FOUND;
return WebviewErrorCode.ERROR_FILE_NOT_FOUND;

case NetError.ERR_INVALID_URL:
return ERROR_BAD_URL;
return WebviewErrorCode.ERROR_BAD_URL;

case NetError.ERR_DISALLOWED_URL_SCHEME:
case NetError.ERR_UNKNOWN_URL_SCHEME:
return ERROR_UNSUPPORTED_SCHEME;
return WebviewErrorCode.ERROR_UNSUPPORTED_SCHEME;

case NetError.ERR_IO_PENDING:
case NetError.ERR_NETWORK_IO_SUSPENDED:
return ERROR_IO;
return WebviewErrorCode.ERROR_IO;

case NetError.ERR_CONNECTION_TIMED_OUT:
case NetError.ERR_TIMED_OUT:
return ERROR_TIMEOUT;
return WebviewErrorCode.ERROR_TIMEOUT;

case NetError.ERR_FILE_TOO_BIG:
return ERROR_FILE;
return WebviewErrorCode.ERROR_FILE;

case NetError.ERR_HOST_RESOLVER_QUEUE_TOO_LARGE:
case NetError.ERR_INSUFFICIENT_RESOURCES:
case NetError.ERR_OUT_OF_MEMORY:
return ERROR_TOO_MANY_REQUESTS;
return WebviewErrorCode.ERROR_TOO_MANY_REQUESTS;

case NetError.ERR_BLOCKED_BY_ADMINISTRATOR:
case NetError.ERR_CONNECTION_CLOSED:
Expand All @@ -96,15 +58,15 @@ static int convertErrorCode(@NetError int netError) {
case NetError.ERR_CONNECTION_ABORTED:
case NetError.ERR_CONNECTION_FAILED:
case NetError.ERR_SOCKET_NOT_CONNECTED:
return ERROR_CONNECT;
return WebviewErrorCode.ERROR_CONNECT;

case NetError.ERR_INTERNET_DISCONNECTED:
case NetError.ERR_ADDRESS_INVALID:
case NetError.ERR_ADDRESS_UNREACHABLE:
case NetError.ERR_NAME_NOT_RESOLVED:
case NetError.ERR_NAME_RESOLUTION_FAILED:
case NetError.ERR_ICANN_NAME_COLLISION:
return ERROR_HOST_LOOKUP;
return WebviewErrorCode.ERROR_HOST_LOOKUP;

case NetError.ERR_SSL_PROTOCOL_ERROR:
case NetError.ERR_SSL_CLIENT_AUTH_CERT_NEEDED:
Expand All @@ -119,13 +81,13 @@ static int convertErrorCode(@NetError int netError) {
case NetError.ERR_SSL_BAD_RECORD_MAC_ALERT:
case NetError.ERR_SSL_CLIENT_AUTH_PRIVATE_KEY_ACCESS_DENIED:
case NetError.ERR_SSL_CLIENT_AUTH_CERT_NO_PRIVATE_KEY:
return ERROR_FAILED_SSL_HANDSHAKE;
return WebviewErrorCode.ERROR_FAILED_SSL_HANDSHAKE;

case NetError.ERR_PROXY_AUTH_UNSUPPORTED:
case NetError.ERR_PROXY_AUTH_REQUESTED:
case NetError.ERR_PROXY_CONNECTION_FAILED:
case NetError.ERR_UNEXPECTED_PROXY_AUTH:
return ERROR_PROXY_AUTHENTICATION;
return WebviewErrorCode.ERROR_PROXY_AUTHENTICATION;

// The certificate errors are handled by onReceivedSslError
// and don't need to be reported here.
Expand All @@ -140,10 +102,10 @@ static int convertErrorCode(@NetError int netError) {
case NetError.ERR_CERT_INVALID:
case NetError.ERR_CERT_WEAK_SIGNATURE_ALGORITHM:
case NetError.ERR_CERT_NON_UNIQUE_NAME:
return ERROR_OK;
return WebviewErrorCode.ERROR_OK;

default:
return ERROR_UNKNOWN;
return WebviewErrorCode.ERROR_UNKNOWN;
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

package org.chromium.android_webview;

import android.webkit.WebViewClient;

import androidx.annotation.IntDef;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

@Retention(RetentionPolicy.SOURCE)
@IntDef({WebviewErrorCode.ERROR_OK, WebviewErrorCode.ERROR_UNKNOWN,
WebviewErrorCode.ERROR_HOST_LOOKUP, WebviewErrorCode.ERROR_UNSUPPORTED_AUTH_SCHEME,
WebviewErrorCode.ERROR_AUTHENTICATION, WebviewErrorCode.ERROR_PROXY_AUTHENTICATION,
WebviewErrorCode.ERROR_CONNECT, WebviewErrorCode.ERROR_IO, WebviewErrorCode.ERROR_TIMEOUT,
WebviewErrorCode.ERROR_REDIRECT_LOOP, WebviewErrorCode.ERROR_UNSUPPORTED_SCHEME,
WebviewErrorCode.ERROR_FAILED_SSL_HANDSHAKE, WebviewErrorCode.ERROR_BAD_URL,
WebviewErrorCode.ERROR_FILE, WebviewErrorCode.ERROR_FILE_NOT_FOUND,
WebviewErrorCode.ERROR_TOO_MANY_REQUESTS, WebviewErrorCode.ERROR_UNSAFE_RESOURCE})
public @interface WebviewErrorCode {
// Success
int ERROR_OK = 0;
// Generic error
int ERROR_UNKNOWN = WebViewClient.ERROR_UNKNOWN;
// Server or proxy hostname lookup failed
int ERROR_HOST_LOOKUP = WebViewClient.ERROR_HOST_LOOKUP;
// Unsupported authentication scheme (not basic or digest)
int ERROR_UNSUPPORTED_AUTH_SCHEME = WebViewClient.ERROR_UNSUPPORTED_AUTH_SCHEME;
// User authentication failed on server
int ERROR_AUTHENTICATION = WebViewClient.ERROR_AUTHENTICATION;
// User authentication failed on proxy
int ERROR_PROXY_AUTHENTICATION = WebViewClient.ERROR_PROXY_AUTHENTICATION;
// Failed to connect to the server
int ERROR_CONNECT = WebViewClient.ERROR_CONNECT;
// Failed to read or write to the server
int ERROR_IO = WebViewClient.ERROR_IO;
// Connection timed out
int ERROR_TIMEOUT = WebViewClient.ERROR_TIMEOUT;
// Too many redirects
int ERROR_REDIRECT_LOOP = WebViewClient.ERROR_REDIRECT_LOOP;
// Unsupported URI scheme
int ERROR_UNSUPPORTED_SCHEME = WebViewClient.ERROR_UNSUPPORTED_SCHEME;
// Failed to perform SSL handshake
int ERROR_FAILED_SSL_HANDSHAKE = WebViewClient.ERROR_FAILED_SSL_HANDSHAKE;
// Malformed URL
int ERROR_BAD_URL = WebViewClient.ERROR_BAD_URL;
// Generic file error
int ERROR_FILE = WebViewClient.ERROR_FILE;
// File not found
int ERROR_FILE_NOT_FOUND = WebViewClient.ERROR_FILE_NOT_FOUND;
// Too many requests during this load
int ERROR_TOO_MANY_REQUESTS = WebViewClient.ERROR_TOO_MANY_REQUESTS;
// Request was identified as a bad url by safebrowsing.
int ERROR_UNSAFE_RESOURCE = WebViewClient.ERROR_UNSAFE_RESOURCE;
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

import org.chromium.android_webview.AwContents;
import org.chromium.android_webview.AwContentsStatics;
import org.chromium.android_webview.ErrorCodeConversionHelper;
import org.chromium.android_webview.WebviewErrorCode;
import org.chromium.android_webview.test.TestAwContentsClient.OnReceivedError2Helper;
import org.chromium.base.test.util.CallbackHelper;
import org.chromium.base.test.util.Feature;
Expand Down Expand Up @@ -95,8 +95,8 @@ public void testSetCheckClearTextPermittedTrue() throws Throwable {
mAwContents, mContentsClient.getOnPageFinishedHelper(), url);
Assert.assertEquals("onReceivedError should be called.", errorCount + 1,
errorHelper.getCallCount());
Assert.assertEquals("Incorrect network error code.",
ErrorCodeConversionHelper.ERROR_UNKNOWN, errorHelper.getError().errorCode);
Assert.assertEquals("Incorrect network error code.", WebviewErrorCode.ERROR_UNKNOWN,
errorHelper.getError().errorCode);
Assert.assertEquals("onReceivedError was called for the wrong URL.", url,
errorHelper.getRequest().url);
} finally {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import org.chromium.android_webview.AwContents;
import org.chromium.android_webview.AwContentsClient.AwWebResourceError;
import org.chromium.android_webview.AwContentsClient.AwWebResourceRequest;
import org.chromium.android_webview.ErrorCodeConversionHelper;
import org.chromium.android_webview.WebviewErrorCode;
import org.chromium.android_webview.test.util.AwTestTouchUtils;
import org.chromium.android_webview.test.util.CommonResources;
import org.chromium.base.test.util.Feature;
Expand Down Expand Up @@ -123,7 +123,7 @@ public void testMainFrame() throws Throwable {
AwWebResourceError error = onReceivedError2Helper.getError();
// The particular error code that is returned depends on the configuration of the device
// (such as existence of a proxy) so we don't test for it.
assertNotEquals(ErrorCodeConversionHelper.ERROR_UNKNOWN, error.errorCode);
assertNotEquals(WebviewErrorCode.ERROR_UNKNOWN, error.errorCode);
Assert.assertNotNull(error.description);
}

Expand Down Expand Up @@ -157,7 +157,7 @@ public void testUserGesture() throws Throwable {
AwWebResourceError error = onReceivedError2Helper.getError();
// The particular error code that is returned depends on the configuration of the device
// (such as existence of a proxy) so we don't test for it.
assertNotEquals(ErrorCodeConversionHelper.ERROR_UNKNOWN, error.errorCode);
assertNotEquals(WebviewErrorCode.ERROR_UNKNOWN, error.errorCode);
Assert.assertNotNull(error.description);
}

Expand All @@ -184,7 +184,7 @@ public void testIframeSubresource() throws Throwable {
AwWebResourceError error = onReceivedError2Helper.getError();
// The particular error code that is returned depends on the configuration of the device
// (such as existence of a proxy) so we don't test for it.
assertNotEquals(ErrorCodeConversionHelper.ERROR_UNKNOWN, error.errorCode);
assertNotEquals(WebviewErrorCode.ERROR_UNKNOWN, error.errorCode);
Assert.assertNotNull(error.description);
}

Expand Down Expand Up @@ -222,7 +222,7 @@ public void testUserGestureForIframeSubresource() throws Throwable {
AwWebResourceError error = onReceivedError2Helper.getError();
// The particular error code that is returned depends on the configuration of the device
// (such as existence of a proxy) so we don't test for it.
assertNotEquals(ErrorCodeConversionHelper.ERROR_UNKNOWN, error.errorCode);
assertNotEquals(WebviewErrorCode.ERROR_UNKNOWN, error.errorCode);
Assert.assertNotNull(error.description);
}

Expand Down Expand Up @@ -250,7 +250,7 @@ public void testImageSubresource() throws Throwable {
AwWebResourceError error = onReceivedError2Helper.getError();
// The particular error code that is returned depends on the configuration of the device
// (such as existence of a proxy) so we don't test for it.
assertNotEquals(ErrorCodeConversionHelper.ERROR_UNKNOWN, error.errorCode);
assertNotEquals(WebviewErrorCode.ERROR_UNKNOWN, error.errorCode);
Assert.assertNotNull(error.description);
}

Expand All @@ -275,7 +275,7 @@ public void testOnInvalidScheme() throws Throwable {
Assert.assertFalse(request.isMainFrame);
Assert.assertFalse(request.hasUserGesture);
AwWebResourceError error = onReceivedError2Helper.getError();
Assert.assertEquals(ErrorCodeConversionHelper.ERROR_UNSUPPORTED_SCHEME, error.errorCode);
Assert.assertEquals(WebviewErrorCode.ERROR_UNSUPPORTED_SCHEME, error.errorCode);
Assert.assertNotNull(error.description);
}

Expand All @@ -302,7 +302,7 @@ public void testOnNonExistentAssetUrl() throws Throwable {
Assert.assertFalse(request.isMainFrame);
Assert.assertFalse(request.hasUserGesture);
AwWebResourceError error = onReceivedError2Helper.getError();
Assert.assertEquals(ErrorCodeConversionHelper.ERROR_UNKNOWN, error.errorCode);
Assert.assertEquals(WebviewErrorCode.ERROR_UNKNOWN, error.errorCode);
Assert.assertNotNull(error.description);
}

Expand All @@ -329,7 +329,7 @@ public void testOnNonExistentResourceUrl() throws Throwable {
Assert.assertFalse(request.isMainFrame);
Assert.assertFalse(request.hasUserGesture);
AwWebResourceError error = onReceivedError2Helper.getError();
Assert.assertEquals(ErrorCodeConversionHelper.ERROR_UNKNOWN, error.errorCode);
Assert.assertEquals(WebviewErrorCode.ERROR_UNKNOWN, error.errorCode);
Assert.assertNotNull(error.description);
}

Expand All @@ -356,7 +356,7 @@ public void testOnCacheMiss() throws Throwable {
Assert.assertFalse(request.isMainFrame);
Assert.assertFalse(request.hasUserGesture);
AwWebResourceError error = onReceivedError2Helper.getError();
Assert.assertEquals(ErrorCodeConversionHelper.ERROR_UNKNOWN, error.errorCode);
Assert.assertEquals(WebviewErrorCode.ERROR_UNKNOWN, error.errorCode);
Assert.assertNotNull(error.description);
}

Expand Down
Loading

0 comments on commit 4f91a35

Please sign in to comment.