Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions wpeview/src/main/cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -141,9 +141,20 @@ add_library(
Runtime/WKWebContext.cpp
Runtime/WKSettings.cpp
Runtime/WKWebsiteDataManager.cpp
Runtime/WKWebView.cpp)
Runtime/WKWebView.cpp
capi/JNIMappings.cpp
capi/WebKitCookieManager.cpp
capi/WebKitNetworkSession.cpp
capi/WebKitSettings.cpp
capi/WebKitWebContext.cpp
capi/WebKitWebView.cpp
capi/WebKitWebsiteDataManager.cpp
capi/WPEDisplay.cpp
capi/WPEScreen.cpp
capi/WPEToplevel.cpp
capi/WPEView.cpp)
target_configure_quality(WPEAndroidRuntime)
target_include_directories(WPEAndroidRuntime PRIVATE Platform Runtime)
target_include_directories(WPEAndroidRuntime PRIVATE Platform Runtime capi)
target_compile_definitions(WPEAndroidRuntime PRIVATE WPE_ENABLE_PROCESS)
target_link_libraries(
WPEAndroidRuntime
Expand Down
1 change: 1 addition & 0 deletions wpeview/src/main/cpp/Common/JNI/JNI.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#pragma once

#include "JNIClass.h"
#include "JNIEnv.h"
#include "JNIObjectArray.h"
#include "JNIScalarArray.h"
#include "JNIString.h"
65 changes: 65 additions & 0 deletions wpeview/src/main/cpp/Common/JNI/JNIEnv.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,69 @@ inline EnableIfObjectType<T, ProtectedType<T>> createTypedProtectedRef(JNIEnv* e
env, std::move(std::forward<T>(obj)), useGlobalRef)); // NOLINT(bugprone-move-forwarding-reference)
}

// Move-only RAII owner of a single Java global reference.
// Analogous to Chromium's ScopedJavaGlobalRef and preferred for async callback
// holders plus native-to-Java back-references that outlive the current JNI call.
template <typename T, typename = EnableIfObjectType<T>> class GlobalRef final {
public:
GlobalRef() noexcept
: m_ref(nullptr)
{
}
GlobalRef(JNIEnv* env, T obj)
: m_ref(obj ? env->NewGlobalRef(obj) : nullptr)
{
}
GlobalRef(JNIEnv* env, jobject obj)
: m_ref(obj ? env->NewGlobalRef(obj) : nullptr)
{
}
~GlobalRef()
{
reset();
}

GlobalRef(GlobalRef&& other) noexcept
: m_ref(other.release())
{
}
GlobalRef& operator=(GlobalRef&& other) noexcept
{
if (this != &other) {
reset();
m_ref = other.release();
}
return *this;
}
GlobalRef(const GlobalRef&) = delete;
GlobalRef& operator=(const GlobalRef&) = delete;

T get() const noexcept
{
return static_cast<T>(m_ref);
}
explicit operator bool() const noexcept
{
return m_ref != nullptr;
}

T release() noexcept
{
auto* ref = static_cast<T>(m_ref);
m_ref = nullptr;
return ref;
}

void reset() noexcept
{
if (m_ref) {
getCurrentThreadJNIEnv()->DeleteGlobalRef(m_ref);
m_ref = nullptr;
}
}

private:
jobject m_ref;
};

} // namespace JNI
5 changes: 5 additions & 0 deletions wpeview/src/main/cpp/Common/JNI/JNITypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -139,4 +139,9 @@ template <typename Ret, typename... Params> struct FunctionSignature<Ret(Params.
closeParenthesis, TypeSignature<Ret>::value>::value;
};

template <typename T> inline T* from_jlong(jlong ptr)
{
return reinterpret_cast<T*>(ptr);
}

} // namespace JNI
13 changes: 10 additions & 3 deletions wpeview/src/main/cpp/Platform/WPEToplevelAndroid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,18 @@ WPEToplevel* wpe_toplevel_android_new(WPEDisplay* display, ANativeWindow* window
return WPE_TOPLEVEL(toplevel);
}

// NOLINTNEXTLINE(readability-function-cognitive-complexity)
void wpe_toplevel_android_set_window(WPEToplevelAndroid* toplevel, ANativeWindow* window)
{
g_return_if_fail(WPE_IS_TOPLEVEL_ANDROID(toplevel));

if (toplevel->window == window && window) {
int physicalWidth = ANativeWindow_getWidth(window);
int physicalHeight = ANativeWindow_getHeight(window);
wpeToplevelAndroidSyncScaleAndSize(toplevel, physicalWidth, physicalHeight);
return;
}

if (window)
ANativeWindow_acquire(window);

Expand All @@ -135,11 +143,10 @@ void wpe_toplevel_android_set_window(WPEToplevelAndroid* toplevel, ANativeWindow
ASurfaceControl* newRoot = ASurfaceControl_createFromWindow(window, "WPEToplevelRoot");

if (newRoot) {
if (!toplevel->surfaceControl) {
if (!toplevel->surfaceControl)
toplevel->surfaceControl = ASurfaceControl_create(newRoot, "WPEWebLayer");
} else {
else
ASurfaceTransaction_reparent(transaction, toplevel->surfaceControl, newRoot);
}
if (toplevel->surfaceControl) {
ASurfaceTransaction_setVisibility(
transaction, toplevel->surfaceControl, ASURFACE_TRANSACTION_VISIBILITY_SHOW);
Expand Down
3 changes: 3 additions & 0 deletions wpeview/src/main/cpp/Runtime/EntryPoint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/

#include "../capi/JNIMappings.h"
#include "Init.h"
#include "Logging.h"
#include "WKCallback.h"
Expand All @@ -42,6 +43,8 @@ extern "C" JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM* javaVM, void* /*reserved*/)
WKWebView::configureJNIMappings();
WKSettings::configureJNIMappings();

WebKit::configureJNIMappings();

return JNI::VERSION;
} catch (const std::exception& e) {
Logging::logError("Runtime: JNI_OnLoad: %s", e.what());
Expand Down
54 changes: 54 additions & 0 deletions wpeview/src/main/cpp/capi/JNIMappings.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/**
* Copyright (C) 2026 Igalia S.L.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/

#include "JNIMappings.h"

namespace WebKit {

void configureWebKitCookieManagerJNIMappings();
void configureWebKitCookieManagerAcceptPolicyCallbackHolderJNIMappings();
void configureWebKitNetworkSessionJNIMappings();
void configureWebKitSettingsJNIMappings();
void configureWebKitWebContextJNIMappings();
void configureWebKitWebViewJNIMappings();
void configureWebKitWebViewEvalCallbackHolderJNIMappings();
void configureWebKitWebsiteDataManagerJNIMappings();
void configureWebKitWebsiteDataManagerCallbackHolderJNIMappings();
void configureWPEDisplayJNIMappings();
void configureWPEScreenJNIMappings();
void configureWPEToplevelJNIMappings();
void configureWPEViewJNIMappings();

void configureJNIMappings()
{
configureWebKitCookieManagerJNIMappings();
configureWebKitCookieManagerAcceptPolicyCallbackHolderJNIMappings();
configureWebKitNetworkSessionJNIMappings();
configureWebKitSettingsJNIMappings();
configureWebKitWebContextJNIMappings();
configureWebKitWebViewJNIMappings();
configureWebKitWebViewEvalCallbackHolderJNIMappings();
configureWebKitWebsiteDataManagerJNIMappings();
configureWebKitWebsiteDataManagerCallbackHolderJNIMappings();
configureWPEDisplayJNIMappings();
configureWPEScreenJNIMappings();
configureWPEToplevelJNIMappings();
configureWPEViewJNIMappings();
}

} // namespace WebKit
25 changes: 25 additions & 0 deletions wpeview/src/main/cpp/capi/JNIMappings.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* Copyright (C) 2026 Igalia S.L.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/

#pragma once

namespace WebKit {

void configureJNIMappings();

} // namespace WebKit
75 changes: 75 additions & 0 deletions wpeview/src/main/cpp/capi/WPEDisplay.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/**
* Copyright (C) 2026 Igalia S.L.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/

#include "JNI/JNI.h"
#include "Logging.h"
#include "WPEDisplayAndroid.h"

#include <wpe/wpe-platform.h>

DECLARE_JNI_CLASS_SIGNATURE(JNIWPEDisplay, "org/wpewebkit/wpe/WPEDisplay");

namespace WebKit {

class JNIWPEDisplayCache final : public JNI::TypedClass<JNIWPEDisplay> {
public:
JNIWPEDisplayCache()
: JNI::TypedClass<JNIWPEDisplay>(true)
{
registerNativeMethods(JNI::NativeMethod<jlong()>("nativeInit", nativeInit),
JNI::NativeMethod<void(jlong)>("nativeDestroy", nativeDestroy),
JNI::NativeMethod<jlong(jlong)>("nativeGetScreen", nativeGetScreen));
}

private:
static jlong nativeInit(JNIEnv*, jobject)
{
auto* display = wpe_display_android_new();

g_autoptr(GError) error = nullptr;
if (!wpe_display_connect(WPE_DISPLAY(display), &error)) {
Logging::logError("WPEDisplay: failed to connect: %s", error ? error->message : "unknown");
g_object_unref(display);
return 0;
}

return reinterpret_cast<jlong>(display);
}

static void nativeDestroy(JNIEnv*, jobject, jlong displayPtr)
{
g_object_unref(JNI::from_jlong<WPEDisplay>(displayPtr));
}

static jlong nativeGetScreen(JNIEnv*, jobject, jlong displayPtr)
{
return reinterpret_cast<jlong>(wpe_display_get_screen(JNI::from_jlong<WPEDisplay>(displayPtr), 0));
}
};

const JNIWPEDisplayCache& getJNIWPEDisplayCache()
{
static const JNIWPEDisplayCache s_singleton;
return s_singleton;
}

void configureWPEDisplayJNIMappings()
{
getJNIWPEDisplayCache();
}
} // namespace WebKit
59 changes: 59 additions & 0 deletions wpeview/src/main/cpp/capi/WPEScreen.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/**
* Copyright (C) 2026 Igalia S.L.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/

#include "JNI/JNI.h"
#include "Logging.h"

#include <wpe/wpe-platform.h>

DECLARE_JNI_CLASS_SIGNATURE(JNIWPEScreen, "org/wpewebkit/wpe/WPEScreen");

namespace WebKit {

class JNIWPEScreenCache final : public JNI::TypedClass<JNIWPEScreen> {
public:
JNIWPEScreenCache()
: JNI::TypedClass<JNIWPEScreen>(true)
{
registerNativeMethods(JNI::NativeMethod<jfloat(jlong)>("nativeGetScale", nativeGetScale),
JNI::NativeMethod<void(jlong, jfloat)>("nativeSetScale", nativeSetScale));
}

private:
static jfloat nativeGetScale(JNIEnv*, jobject, jlong nativePtr)
{
return static_cast<jfloat>(wpe_screen_get_scale(JNI::from_jlong<WPEScreen>(nativePtr)));
}

static void nativeSetScale(JNIEnv*, jobject, jlong nativePtr, jfloat scale)
{
wpe_screen_set_scale(JNI::from_jlong<WPEScreen>(nativePtr), scale);
}
};

const JNIWPEScreenCache& getJNIWPEScreenCache()
{
static const JNIWPEScreenCache s_singleton;
return s_singleton;
}

void configureWPEScreenJNIMappings()
{
getJNIWPEScreenCache();
}
} // namespace WebKit
Loading
Loading