diff --git a/tools/mediaplayer/src/main/java/org/wpewebkit/tools/mediaplayer/MainActivity.java b/tools/mediaplayer/src/main/java/org/wpewebkit/tools/mediaplayer/MainActivity.java index 22830daa7..594c5b47f 100644 --- a/tools/mediaplayer/src/main/java/org/wpewebkit/tools/mediaplayer/MainActivity.java +++ b/tools/mediaplayer/src/main/java/org/wpewebkit/tools/mediaplayer/MainActivity.java @@ -57,7 +57,8 @@ protected void onCreate(@Nullable Bundle savedInstanceState) { try { String content = getHtmlContent(); WPEView view = findViewById(R.id.wpe_view); - view.getWKWebView().getWKSettings().setAllowFileUrls(true); + view.getSettings().setAllowFileAccessFromFileURLs(true); + view.getSettings().setAllowUniversalAccessFromFileURLs(true); view.loadHtml(content, "file:///"); } catch (IOException ex) { String message = "Cannot initialize web application"; diff --git a/wpe/src/main/cpp/Runtime/WKSettings.cpp b/wpe/src/main/cpp/Runtime/WKSettings.cpp index 55a1aadc3..ee732d480 100644 --- a/wpe/src/main/cpp/Runtime/WKSettings.cpp +++ b/wpe/src/main/cpp/Runtime/WKSettings.cpp @@ -22,7 +22,7 @@ #include "WKWebView.h" namespace { -void setNativeUserAgent(JNIEnv* /*env*/, jobject /*obj*/, jlong wkWebViewPtr, jstring userAgent) noexcept +void nativeSetNativeUserAgentString(JNIEnv* /*env*/, jobject /*obj*/, jlong wkWebViewPtr, jstring userAgent) noexcept { auto* wkWebView = reinterpret_cast(wkWebViewPtr); // NOLINT(performance-no-int-to-ptr) if (wkWebView != nullptr) { @@ -31,7 +31,7 @@ void setNativeUserAgent(JNIEnv* /*env*/, jobject /*obj*/, jlong wkWebViewPtr, js } } -void setNativeMediaPlaybackRequiresUserGesture( +void nativeSetMediaPlaybackRequiresUserGesture( JNIEnv* /*env*/, jobject /*obj*/, jlong wkWebViewPtr, jboolean require) noexcept { auto* wkWebView = reinterpret_cast(wkWebViewPtr); // NOLINT(performance-no-int-to-ptr) @@ -41,13 +41,22 @@ void setNativeMediaPlaybackRequiresUserGesture( } } -void setNativeAllowFileUrls(JNIEnv* /*env*/, jobject /*obj*/, jlong wkWebViewPtr, jboolean allow) noexcept +void nativeSetAllowFileAccessFromFileURLs(JNIEnv* /*env*/, jobject /*obj*/, jlong wkWebViewPtr, jboolean flag) noexcept { auto* wkWebView = reinterpret_cast(wkWebViewPtr); // NOLINT(performance-no-int-to-ptr) if (wkWebView != nullptr) { WebKitSettings* settings = webkit_web_view_get_settings(wkWebView->webView()); - webkit_settings_set_allow_file_access_from_file_urls(settings, static_cast(allow)); - webkit_settings_set_allow_universal_access_from_file_urls(settings, static_cast(allow)); + webkit_settings_set_allow_file_access_from_file_urls(settings, static_cast(flag)); + } +} + +void nativeSetAllowUniversalAccessFromFileURLs( + JNIEnv* /*env*/, jobject /*obj*/, jlong wkWebViewPtr, jboolean flag) noexcept +{ + auto* wkWebView = reinterpret_cast(wkWebViewPtr); // NOLINT(performance-no-int-to-ptr) + if (wkWebView != nullptr) { + WebKitSettings* settings = webkit_web_view_get_settings(wkWebView->webView()); + webkit_settings_set_allow_universal_access_from_file_urls(settings, static_cast(flag)); } } } // namespace @@ -55,8 +64,12 @@ void setNativeAllowFileUrls(JNIEnv* /*env*/, jobject /*obj*/, jlong wkWebViewPtr void WKSettings::configureJNIMappings() { JNI::Class("org/wpewebkit/wpe/WKSettings") - .registerNativeMethods(JNI::NativeMethod("setNativeUserAgent", setNativeUserAgent), + .registerNativeMethods( + JNI::NativeMethod("nativeSetUserAgentString", nativeSetNativeUserAgentString), + JNI::NativeMethod( + "nativeSetMediaPlaybackRequiresUserGesture", nativeSetMediaPlaybackRequiresUserGesture), + JNI::NativeMethod( + "nativeSetAllowFileAccessFromFileURLs", nativeSetAllowFileAccessFromFileURLs), JNI::NativeMethod( - "setNativeMediaPlaybackRequiresUserGesture", setNativeMediaPlaybackRequiresUserGesture), - JNI::NativeMethod("setNativeAllowFileUrls", setNativeAllowFileUrls)); + "nativeSetAllowUniversalAccessFromFileURLs", nativeSetAllowUniversalAccessFromFileURLs)); } diff --git a/wpe/src/main/java/org/wpewebkit/wpe/WKSettings.java b/wpe/src/main/java/org/wpewebkit/wpe/WKSettings.java index 8f5b602a4..b02446715 100644 --- a/wpe/src/main/java/org/wpewebkit/wpe/WKSettings.java +++ b/wpe/src/main/java/org/wpewebkit/wpe/WKSettings.java @@ -54,81 +54,59 @@ private static String getOsVersion() { + "Mobile Safari/605.1.15"; private final WKWebView wkWebView; - public @NonNull WKWebView getWKWebView() { return wkWebView; } - - private native void setNativeUserAgent(long nativePtr, String userAgent); - private native void setNativeMediaPlaybackRequiresUserGesture(long nativePtr, boolean requires); - private native void setNativeAllowFileUrls(long nativePtr, boolean allow); + private String userAgent = ""; + private boolean mediaPlaybackRequiresUserGesture = false; + private boolean allowUniversalAccessFromFileUrls = false; + private boolean allowFileAccessFromFileUrls = false; public WKSettings(@NonNull WKWebView wkWebView) { this.wkWebView = wkWebView; - setUserAgent(null); + + setUserAgentString(null); setMediaPlaybackRequiresUserGesture(true); } - private String userAgent = ""; + public @NonNull String getUserAgentString() { return userAgent; } - /** - * Gets the user-agent string. - * @return the page user-agent string - * @see #setUserAgent - */ - public @NonNull String getUserAgent() { return userAgent; } - - /** - * Sets the user-agent string. If the string is {@code null} or empty, - * the system default value will be used. - * @param str the new user-agent string - */ - public void setUserAgent(@Nullable String str) { + public void setUserAgentString(@Nullable String str) { if ((str == null) || str.isEmpty()) str = DEFAUlT_USER_AGENT; if (!str.equals(userAgent)) { userAgent = str; - setNativeUserAgent(wkWebView.getNativePtr(), userAgent); + nativeSetUserAgentString(wkWebView.getNativePtr(), userAgent); } } - private boolean mediaPlaybackRequiresUserGesture = false; - - /** - * Gets whether the page requires a user gesture to play media. - * @return {@code true} if the page requires a user gesture to play media, or false otherwise. - * @see #setMediaPlaybackRequiresUserGesture - */ public boolean getMediaPlaybackRequiresUserGesture() { return mediaPlaybackRequiresUserGesture; } - /** - * Sets whether the page requires a user gesture to play media. - * The default is {@code true}. - * @param requires whether the page requires a user gesture to play media, or not. - */ public void setMediaPlaybackRequiresUserGesture(boolean requires) { if (requires != mediaPlaybackRequiresUserGesture) { mediaPlaybackRequiresUserGesture = requires; - setNativeMediaPlaybackRequiresUserGesture(wkWebView.getNativePtr(), mediaPlaybackRequiresUserGesture); + nativeSetMediaPlaybackRequiresUserGesture(wkWebView.getNativePtr(), mediaPlaybackRequiresUserGesture); } } - private boolean allowFileUrls = false; - - /** - * Gets whether the page allows file Urls. - * @return {@code true} if the page allows file Urls, or false otherwise. - * @see #setAllowFileUrls - */ - public boolean getAllowFileUrls() { return allowFileUrls; } - - /** - * Sets whether the page allows file Urls. - * The default is {@code false}. - * @param allow whether the page allows file Urls, or not. - */ - public void setAllowFileUrls(boolean allow) { - if (allow != allowFileUrls) { - allowFileUrls = allow; - setNativeAllowFileUrls(wkWebView.getNativePtr(), allowFileUrls); + public boolean getAllowUniversalAccessFromFileURLs() { return allowUniversalAccessFromFileUrls; } + + public void setAllowUniversalAccessFromFileURLs(boolean flag) { + if (flag != allowFileAccessFromFileUrls) { + allowFileAccessFromFileUrls = flag; + nativeSetAllowUniversalAccessFromFileURLs(wkWebView.getNativePtr(), flag); + } + } + + public boolean getAllowFileAccessFromFileURLs() { return allowFileAccessFromFileUrls; } + + public void setAllowFileAccessFromFileURLs(boolean flag) { + if (flag != allowFileAccessFromFileUrls) { + allowFileAccessFromFileUrls = flag; + nativeSetAllowFileAccessFromFileURLs(wkWebView.getNativePtr(), flag); } } + + private native void nativeSetUserAgentString(long nativePtr, String userAgent); + private native void nativeSetMediaPlaybackRequiresUserGesture(long nativePtr, boolean requires); + private native void nativeSetAllowFileAccessFromFileURLs(long nativePtr, boolean flag); + private native void nativeSetAllowUniversalAccessFromFileURLs(long nativePtr, boolean flag); } diff --git a/wpe/src/main/java/org/wpewebkit/wpe/WKWebView.java b/wpe/src/main/java/org/wpewebkit/wpe/WKWebView.java index db2d63acc..e3032a1b2 100644 --- a/wpe/src/main/java/org/wpewebkit/wpe/WKWebView.java +++ b/wpe/src/main/java/org/wpewebkit/wpe/WKWebView.java @@ -98,8 +98,6 @@ public final class WKWebView { private FrameLayout customView = null; - public @NonNull WKSettings getWKSettings() { return wkSettings; } - private String uri = "about:blank"; private String originalUrl = uri; private String title = uri; @@ -186,6 +184,8 @@ protected void finalize() throws Throwable { } } + public @NonNull WKSettings getSettings() { return wkSettings; } + public void setWPEViewClient(@NonNull WPEViewClient wpeViewClient) { this.wpeViewClient = wpeViewClient; } public @NonNull WPEViewClient getWPEViewClient() { return wpeViewClient; } diff --git a/wpe/src/main/java/org/wpewebkit/wpeview/WPEView.java b/wpe/src/main/java/org/wpewebkit/wpeview/WPEView.java index 156aee0ad..b83cefad8 100644 --- a/wpe/src/main/java/org/wpewebkit/wpeview/WPEView.java +++ b/wpe/src/main/java/org/wpewebkit/wpeview/WPEView.java @@ -25,10 +25,8 @@ import android.content.Context; import android.util.AttributeSet; -import android.util.Log; import android.view.KeyCharacterMap; import android.view.KeyEvent; -import android.view.SurfaceView; import android.view.View; import android.widget.FrameLayout; @@ -53,6 +51,7 @@ public class WPEView extends FrameLayout { private boolean ownsContext; private WKWebView wkWebView; + private WPESettings wpeSettings; private SurfaceClient surfaceClient = null; public WPEView(@NonNull Context context) { @@ -76,6 +75,7 @@ private void init(@NonNull WPEContext context, boolean ownsContext, boolean head wpeContext = context; this.ownsContext = ownsContext; wkWebView = new WKWebView(this, wpeContext.getWebContext(), false); + wpeSettings = new WPESettings(wkWebView.getSettings()); setFocusable(true); setFocusableInTouchMode(true); @@ -95,12 +95,6 @@ public void destroy() { } } - /** - * Gets the wkWebView associated with this WPEView. - * @return the associated page. - */ - public @NonNull WKWebView getWKWebView() { return wkWebView; } - /** * Loads the given URL. * @param url The URL of the resource to load. @@ -250,6 +244,14 @@ public void evaluateJavascript(@NonNull String script, @Nullable WPECallback