Skip to content

Commit

Permalink
[wpe] Add public WPESettings API
Browse files Browse the repository at this point in the history
  • Loading branch information
Jani Hautakangas committed Sep 15, 2024
1 parent 5cdbeb8 commit d3db335
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 71 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
29 changes: 21 additions & 8 deletions wpe/src/main/cpp/Runtime/WKSettings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<WKWebView*>(wkWebViewPtr); // NOLINT(performance-no-int-to-ptr)
if (wkWebView != nullptr) {
Expand All @@ -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<WKWebView*>(wkWebViewPtr); // NOLINT(performance-no-int-to-ptr)
Expand All @@ -41,22 +41,35 @@ 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<WKWebView*>(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<gboolean>(allow));
webkit_settings_set_allow_universal_access_from_file_urls(settings, static_cast<gboolean>(allow));
webkit_settings_set_allow_file_access_from_file_urls(settings, static_cast<gboolean>(flag));
}
}

void nativeSetAllowUniversalAccessFromFileURLs(
JNIEnv* /*env*/, jobject /*obj*/, jlong wkWebViewPtr, jboolean flag) noexcept
{
auto* wkWebView = reinterpret_cast<WKWebView*>(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<gboolean>(flag));
}
}
} // namespace

void WKSettings::configureJNIMappings()
{
JNI::Class("org/wpewebkit/wpe/WKSettings")
.registerNativeMethods(JNI::NativeMethod<void(jlong, jstring)>("setNativeUserAgent", setNativeUserAgent),
.registerNativeMethods(
JNI::NativeMethod<void(jlong, jstring)>("nativeSetUserAgentString", nativeSetNativeUserAgentString),
JNI::NativeMethod<void(jlong, jboolean)>(
"nativeSetMediaPlaybackRequiresUserGesture", nativeSetMediaPlaybackRequiresUserGesture),
JNI::NativeMethod<void(jlong, jboolean)>(
"nativeSetAllowFileAccessFromFileURLs", nativeSetAllowFileAccessFromFileURLs),
JNI::NativeMethod<void(jlong, jboolean)>(
"setNativeMediaPlaybackRequiresUserGesture", setNativeMediaPlaybackRequiresUserGesture),
JNI::NativeMethod<void(jlong, jboolean)>("setNativeAllowFileUrls", setNativeAllowFileUrls));
"nativeSetAllowUniversalAccessFromFileURLs", nativeSetAllowUniversalAccessFromFileURLs));
}
82 changes: 30 additions & 52 deletions wpe/src/main/java/org/wpewebkit/wpe/WKSettings.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
4 changes: 2 additions & 2 deletions wpe/src/main/java/org/wpewebkit/wpe/WKWebView.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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; }
Expand Down
25 changes: 17 additions & 8 deletions wpe/src/main/java/org/wpewebkit/wpeview/WPEView.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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) {
Expand All @@ -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);
Expand All @@ -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.
Expand Down Expand Up @@ -250,6 +244,14 @@ public void evaluateJavascript(@NonNull String script, @Nullable WPECallback<Str
wkWebView.evaluateJavascript(script, WKCallback.fromWPECallback(resultCallback));
}

/**
* Return the WPESettings object used to control the settings for this
* WPEView.
* @return A WPESettings object that can be used to control this WPEView's
* settings.
*/
public @NonNull WPESettings getSettings() { return wpeSettings; }

@Override
public boolean onKeyUp(int keyCode, @NonNull KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_DEL) {
Expand All @@ -261,4 +263,11 @@ public boolean onKeyUp(int keyCode, @NonNull KeyEvent event) {
wkWebView.setInputMethodContent(map.get(keyCode, event.getMetaState()));
return true;
}

// Internal API

@NonNull
WKWebView getWKWebView() {
return wkWebView;
}
}

0 comments on commit d3db335

Please sign in to comment.