[WebView Support Library] Add WebResourceRequestCompat.
WebResourceRequest.isRedirect was added after Lollipop so with this CL
we provide support for the method
WebResourceRequestCompat.isRedirect(WebResourceRequest).
Bug: 74939930
Test: manually ensure calling WebResourceRequestCompat.isRedirect
doesn't crash.
Change-Id: Id68e83c090486c23c7bd84767a1810da08cd879b
diff --git a/webkit/api/current.txt b/webkit/api/current.txt
index dfb6bfa..b335efa 100644
--- a/webkit/api/current.txt
+++ b/webkit/api/current.txt
@@ -27,6 +27,10 @@
method public abstract int getErrorCode();
}
+ public class WebResourceRequestCompat {
+ method public static boolean isRedirect(android.webkit.WebResourceRequest);
+ }
+
public class WebSettingsCompat {
method public static int getDisabledActionModeMenuItems(android.webkit.WebSettings);
method public static boolean getOffscreenPreRaster(android.webkit.WebSettings);
@@ -73,6 +77,7 @@
field public static final java.lang.String SHOULD_OVERRIDE_WITH_REDIRECTS = "SHOULD_OVERRIDE_WITH_REDIRECTS";
field public static final java.lang.String START_SAFE_BROWSING = "START_SAFE_BROWSING";
field public static final java.lang.String VISUAL_STATE_CALLBACK = "VISUAL_STATE_CALLBACK";
+ field public static final java.lang.String WEB_RESOURCE_REQUEST_IS_REDIRECT = "WEB_RESOURCE_REQUEST_IS_REDIRECT";
}
}
diff --git a/webkit/src/main/java/androidx/webkit/WebResourceRequestCompat.java b/webkit/src/main/java/androidx/webkit/WebResourceRequestCompat.java
new file mode 100644
index 0000000..783efca
--- /dev/null
+++ b/webkit/src/main/java/androidx/webkit/WebResourceRequestCompat.java
@@ -0,0 +1,60 @@
+/*
+ * Copyright 2018 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 androidx.webkit;
+
+import android.annotation.SuppressLint;
+import android.webkit.WebResourceRequest;
+
+import androidx.annotation.NonNull;
+import androidx.annotation.RequiresFeature;
+import androidx.webkit.internal.WebResourceRequestAdapter;
+import androidx.webkit.internal.WebViewFeatureInternal;
+import androidx.webkit.internal.WebViewGlueCommunicator;
+
+// TODO(gsennton) add a test for this class
+
+/**
+ * Compatibility version of {@link WebResourceRequest}.
+ */
+public class WebResourceRequestCompat {
+
+ // Developers should not be able to instantiate this class.
+ private WebResourceRequestCompat() {}
+
+ /**
+ * Gets whether the request was a result of a server-side redirect.
+ *
+ * @return whether the request was a result of a server-side redirect.
+ */
+ @SuppressLint("NewApi")
+ @RequiresFeature(name = WebViewFeature.WEB_RESOURCE_REQUEST_IS_REDIRECT,
+ enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
+ public static boolean isRedirect(@NonNull WebResourceRequest request) {
+ WebViewFeatureInternal feature = WebViewFeatureInternal.WEB_RESOURCE_REQUEST_IS_REDIRECT;
+ if (feature.isSupportedByFramework()) {
+ return request.isRedirect();
+ } else if (feature.isSupportedByWebView()) {
+ return getAdapter(request).isRedirect();
+ } else {
+ throw WebViewFeatureInternal.getUnsupportedOperationException();
+ }
+ }
+
+ private static WebResourceRequestAdapter getAdapter(WebResourceRequest request) {
+ return WebViewGlueCommunicator.getCompatConverter().convertWebResourceRequest(request);
+ }
+}
diff --git a/webkit/src/main/java/androidx/webkit/WebViewFeature.java b/webkit/src/main/java/androidx/webkit/WebViewFeature.java
index 4286880..76f876c 100644
--- a/webkit/src/main/java/androidx/webkit/WebViewFeature.java
+++ b/webkit/src/main/java/androidx/webkit/WebViewFeature.java
@@ -62,7 +62,8 @@
RECEIVE_WEB_RESOURCE_ERROR,
RECEIVE_HTTP_ERROR,
SHOULD_OVERRIDE_WITH_REDIRECTS,
- SAFE_BROWSING_HIT
+ SAFE_BROWSING_HIT,
+ WEB_RESOURCE_REQUEST_IS_REDIRECT
})
@Retention(RetentionPolicy.SOURCE)
@Target({ElementType.PARAMETER, ElementType.METHOD})
@@ -211,6 +212,14 @@
public static final String SAFE_BROWSING_HIT = Features.SAFE_BROWSING_HIT;
/**
+ * Feature for {@link #isFeatureSupported(String)}.
+ * This feature covers
+ * {@link WebResourceRequestCompat#isRedirect(WebResourceRequest)}.
+ */
+ public static final String WEB_RESOURCE_REQUEST_IS_REDIRECT =
+ Features.WEB_RESOURCE_REQUEST_IS_REDIRECT;
+
+ /**
* Return whether a feature is supported at run-time. This depends on the Android version of the
* device and the WebView APK on the device.
*/
diff --git a/webkit/src/main/java/androidx/webkit/internal/WebResourceRequestAdapter.java b/webkit/src/main/java/androidx/webkit/internal/WebResourceRequestAdapter.java
new file mode 100644
index 0000000..0d6a05d
--- /dev/null
+++ b/webkit/src/main/java/androidx/webkit/internal/WebResourceRequestAdapter.java
@@ -0,0 +1,41 @@
+/*
+ * Copyright 2018 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 androidx.webkit.internal;
+
+import android.webkit.WebResourceRequest;
+
+import org.chromium.support_lib_boundary.WebResourceRequestBoundaryInterface;
+
+/**
+ * Adapter between {@link androidx.webkit.WebResourceRequestCompat} and
+ * {@link org.chromium.support_lib_boundary.WebResourceRequestBoundaryInterface}.
+ */
+public class WebResourceRequestAdapter {
+ private final WebResourceRequestBoundaryInterface mBoundaryInterface;
+
+ public WebResourceRequestAdapter(WebResourceRequestBoundaryInterface boundaryInterface) {
+ mBoundaryInterface = boundaryInterface;
+ }
+
+ /**
+ * Adapter method for
+ * {@link androidx.webkit.WebResourceRequestCompat#isRedirect(WebResourceRequest)}.
+ */
+ public boolean isRedirect() {
+ return mBoundaryInterface.isRedirect();
+ }
+}
diff --git a/webkit/src/main/java/androidx/webkit/internal/WebViewFeatureInternal.java b/webkit/src/main/java/androidx/webkit/internal/WebViewFeatureInternal.java
index d804ee9..b648dc7 100644
--- a/webkit/src/main/java/androidx/webkit/internal/WebViewFeatureInternal.java
+++ b/webkit/src/main/java/androidx/webkit/internal/WebViewFeatureInternal.java
@@ -23,6 +23,7 @@
import android.webkit.WebSettings;
import androidx.webkit.ServiceWorkerClientCompat;
+import androidx.webkit.WebResourceRequestCompat;
import androidx.webkit.WebViewClientCompat;
import androidx.webkit.WebViewCompat;
import androidx.webkit.WebViewFeature;
@@ -157,7 +158,14 @@
* TODO(ntfschr): turn this into a javadoc link once the method is implemented in
* http://ag/3858246.
*/
- SAFE_BROWSING_HIT(WebViewFeature.SAFE_BROWSING_HIT, Build.VERSION_CODES.O_MR1);
+ SAFE_BROWSING_HIT(WebViewFeature.SAFE_BROWSING_HIT, Build.VERSION_CODES.O_MR1),
+
+ /**
+ * This feature covers
+ * {@link WebResourceRequestCompat#isRedirect(WebResourceRequest)}.
+ */
+ WEB_RESOURCE_REQUEST_IS_REDIRECT(WebViewFeature.WEB_RESOURCE_REQUEST_IS_REDIRECT,
+ Build.VERSION_CODES.N);
private final String mFeatureValue;
private final int mOsVersion;
diff --git a/webkit/src/main/java/androidx/webkit/internal/WebkitToCompatConverter.java b/webkit/src/main/java/androidx/webkit/internal/WebkitToCompatConverter.java
index 4d264a3..e333480 100644
--- a/webkit/src/main/java/androidx/webkit/internal/WebkitToCompatConverter.java
+++ b/webkit/src/main/java/androidx/webkit/internal/WebkitToCompatConverter.java
@@ -17,9 +17,11 @@
package androidx.webkit.internal;
import android.webkit.ServiceWorkerWebSettings;
+import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import org.chromium.support_lib_boundary.ServiceWorkerWebSettingsBoundaryInterface;
+import org.chromium.support_lib_boundary.WebResourceRequestBoundaryInterface;
import org.chromium.support_lib_boundary.WebSettingsBoundaryInterface;
import org.chromium.support_lib_boundary.WebkitToCompatConverterBoundaryInterface;
import org.chromium.support_lib_boundary.util.BoundaryInterfaceReflectionUtil;
@@ -56,4 +58,14 @@
ServiceWorkerWebSettingsBoundaryInterface.class,
mImpl.convertServiceWorkerSettings(settings));
}
+
+ /**
+ * Return a {@link WebResourceRequestAdapter} linked to the given {@link WebResourceRequest} so
+ * that calls on either of those objects affect the other object.
+ */
+ public WebResourceRequestAdapter convertWebResourceRequest(WebResourceRequest request) {
+ return new WebResourceRequestAdapter(BoundaryInterfaceReflectionUtil.castToSuppLibClass(
+ WebResourceRequestBoundaryInterface.class,
+ mImpl.convertWebResourceRequest(request)));
+ }
}