From 5be4d2a1044b0eaac8dba8fdf060ce1c4b4381e9 Mon Sep 17 00:00:00 2001 From: Constantin Piber <59023762+cpiber@users.noreply.github.com> Date: Fri, 17 Oct 2025 18:07:18 +0200 Subject: [PATCH] Minor bug fixes for Webview, Permission request support (#1723) * fix: Match URLs with trailing / * Handle permission requests and attempt to enable Widevine * Tie CEF loglevel to server debug logs * Lint * Add missing file Forgot to add in previous commits * Provide WebResourceResponse * Fix NullException if headers are not set * fix: Don't allow interception for initial page load fixes #1713 * Lint --- .../android/webkit/PermissionRequest.java | 98 +++++++ .../android/webkit/WebResourceResponse.java | 249 ++++++++++++++++++ .../webkit/KcefWebViewProvider.kt | 69 ++++- .../suwayomi/tachidesk/server/ServerSetup.kt | 4 +- 4 files changed, 411 insertions(+), 9 deletions(-) create mode 100644 AndroidCompat/src/main/java/android/webkit/PermissionRequest.java create mode 100644 AndroidCompat/src/main/java/android/webkit/WebResourceResponse.java diff --git a/AndroidCompat/src/main/java/android/webkit/PermissionRequest.java b/AndroidCompat/src/main/java/android/webkit/PermissionRequest.java new file mode 100644 index 000000000..ac145b1d8 --- /dev/null +++ b/AndroidCompat/src/main/java/android/webkit/PermissionRequest.java @@ -0,0 +1,98 @@ +/* + * 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; + +/** + * This class defines a permission request and is used when web content + * requests access to protected resources. The permission request related events + * are delivered via {@link WebChromeClient#onPermissionRequest} and + * {@link WebChromeClient#onPermissionRequestCanceled}. + * + * Either {@link #grant(String[]) grant()} or {@link #deny()} must be called in UI + * thread to respond to the request. + * + * New protected resources whose names are not defined here may be requested in + * future versions of WebView, even when running on an older Android release. To + * avoid unintentionally granting requests for new permissions, you should pass the + * specific permissions you intend to grant to {@link #grant(String[]) grant()}, + * and avoid writing code like this example: + *
+ * permissionRequest.grant(permissionRequest.getResources()) // This is wrong!!! + *+ * See the WebView's release notes for information about new protected resources. + */ +public abstract class PermissionRequest { + /** + * Resource belongs to video capture device, like camera. + */ + public final static String RESOURCE_VIDEO_CAPTURE = "android.webkit.resource.VIDEO_CAPTURE"; + /** + * Resource belongs to audio capture device, like microphone. + */ + public final static String RESOURCE_AUDIO_CAPTURE = "android.webkit.resource.AUDIO_CAPTURE"; + /** + * Resource belongs to protected media identifier. + * After the user grants this resource, the origin can use EME APIs to generate the license + * requests. + */ + public final static String RESOURCE_PROTECTED_MEDIA_ID = + "android.webkit.resource.PROTECTED_MEDIA_ID"; + /** + * Resource will allow sysex messages to be sent to or received from MIDI devices. These + * messages are privileged operations, e.g. modifying sound libraries and sampling data, or + * even updating the MIDI device's firmware. + * + * Permission may be requested for this resource in API levels 21 and above, if the Android + * device has been updated to WebView 45 or above. + */ + public final static String RESOURCE_MIDI_SYSEX = "android.webkit.resource.MIDI_SYSEX"; + + /** + * Call this method to get the origin of the web page which is trying to access + * the restricted resources. + * + * @return the origin of web content which attempt to access the restricted + * resources. + */ + public abstract Uri getOrigin(); + + /** + * Call this method to get the resources the web page is trying to access. + * + * @return the array of resources the web content wants to access. + */ + public abstract String[] getResources(); + + /** + * Call this method to grant origin the permission to access the given resources. + * The granted permission is only valid for this WebView. + * + * @param resources the resources granted to be accessed by origin, to grant + * request, the requested resources returned by {@link #getResources()} + * must be equals or a subset of granted resources. + * This parameter is designed to avoid granting permission by accident + * especially when new resources are requested by web content. + */ + public abstract void grant(String[] resources); + + /** + * Call this method to deny the request. + */ + public abstract void deny(); +} diff --git a/AndroidCompat/src/main/java/android/webkit/WebResourceResponse.java b/AndroidCompat/src/main/java/android/webkit/WebResourceResponse.java new file mode 100644 index 000000000..1d374118c --- /dev/null +++ b/AndroidCompat/src/main/java/android/webkit/WebResourceResponse.java @@ -0,0 +1,249 @@ +/* + * Copyright (C) 2010 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.annotation.SystemApi; +import android.os.Build; +import java.io.InputStream; +import java.io.StringBufferInputStream; +import java.util.Map; +import android.annotation.NonNull; + + +/** + * Encapsulates a resource response. Applications can return an instance of this + * class from {@link WebViewClient#shouldInterceptRequest} to provide a custom + * response when the WebView requests a particular resource. + */ +public class WebResourceResponse { + private boolean mImmutable; + private String mMimeType; + private String mEncoding; + private int mStatusCode; + private String mReasonPhrase; + private Map
Note: The MIME type and character encoding must + * be specified as separate parameters (for example {@code "text/html"} and + * {@code "utf-8"}), not a single value like the {@code "text/html; charset=utf-8"} + * format used in the HTTP Content-Type header. Do not use the value of a HTTP + * Content-Encoding header for {@code encoding}, as that header does not specify a + * character encoding. Content without a defined character encoding (for example + * image resources) should pass {@code null} for {@code encoding}. + * + * @param mimeType the resource response's MIME type, for example {@code "text/html"}. + * @param encoding the resource response's character encoding, for example {@code "utf-8"}. + * @param data the input stream that provides the resource response's data. Must not be a + * StringBufferInputStream. + */ + public WebResourceResponse(String mimeType, String encoding, + InputStream data) { + mMimeType = mimeType; + mEncoding = encoding; + setData(data); + } + + /** + * Constructs a resource response with the given parameters. Callers must implement + * {@link InputStream#read(byte[])} for the input stream. {@link InputStream#close()} will be + * called after the WebView has finished with the response. + * + * + *
Note: See {@link #WebResourceResponse(String,String,InputStream)}
+ * for details on what should be specified for {@code mimeType} and {@code encoding}.
+ *
+ * @param mimeType the resource response's MIME type, for example {@code "text/html"}.
+ * @param encoding the resource response's character encoding, for example {@code "utf-8"}.
+ * @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-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. Must not be a
+ * StringBufferInputStream.
+ */
+ public WebResourceResponse(String mimeType, String encoding, int statusCode,
+ @NonNull String reasonPhrase, Map