Skip to content

Commit

Permalink
[wpe] Switch to libsoup3 based wpewebkit
Browse files Browse the repository at this point in the history
This also implies using 2022_GLIB_API from wpewebkit
  • Loading branch information
zhani committed Jun 16, 2024
1 parent c061216 commit 0960759
Show file tree
Hide file tree
Showing 21 changed files with 268 additions and 177 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import com.wpe.wpeview.WPEChromeClient
import com.wpe.wpeview.WPEView
import com.wpe.wpeview.WPEViewClient


const val INITIAL_URL = "https://igalia.com"
const val SEARCH_URI_BASE = "https://duckduckgo.com/?q="

Expand Down
14 changes: 7 additions & 7 deletions tools/scripts/bootstrap.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,21 +100,21 @@ class Bootstrap:
"libgobject-2.0.so",
"libwpe-1.0.so",
"libWPEBackend-android.so",
"libWPEWebKit-1.0_3.so"
"libWPEWebKit-2.0_1.so"
]
_build_includes = [
("glib-2.0", "glib-2.0"),
("libsoup-2.4", "libsoup-2.4"),
("libsoup-3.0", "libsoup-3.0"),
("wpe-1.0", "wpe"),
("wpe-android", "wpe-android"),
("wpe-webkit-1.0", "wpe-webkit"),
("wpe-webkit-2.0", "wpe-webkit"),
("xkbcommon", "xkbcommon")
]
_soname_replacements = [
("libnettle.so.8", "libnettle_8.so"), # This entry is not retrievable from the packaged libnettle.so
("libWPEWebKit-1.0.so.3", "libWPEWebKit-1.0_3.so") # This is for libWPEInjectedBundle.so
("libWPEWebKit-2.0.so.1", "libWPEWebKit-2.0_1.so") # This is for libWPEInjectedBundle.so
]
_base_needed = ["libWPEWebKit-1.0_3.so"]
_base_needed = ["libWPEWebKit-2.0_1.so"]

def __init__(self, args=None):
args = args or {}
Expand Down Expand Up @@ -352,8 +352,8 @@ def _copy_system_libs(self, target_dir):
sysroot_lib_dir = os.path.join(self._sysroot_dir, "lib")

libs_paths = list(Path(sysroot_lib_dir).glob("*.so"))
libs_paths.extend(list(Path(os.path.join(sysroot_lib_dir, "wpe-webkit-1.0")).glob("*.so")))
libs_paths.extend(list(Path(os.path.join(sysroot_lib_dir, "wpe-webkit-1.0", "injected-bundle")).glob("*.so")))
libs_paths.extend(list(Path(os.path.join(sysroot_lib_dir, "wpe-webkit-2.0")).glob("*.so")))
libs_paths.extend(list(Path(os.path.join(sysroot_lib_dir, "wpe-webkit-2.0", "injected-bundle")).glob("*.so")))

self._soname_replacements = Bootstrap._soname_replacements.copy()

Expand Down
2 changes: 2 additions & 0 deletions wpe/src/main/cpp/Browser/EntryPoint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "PageSettings.h"
#include "WKCallback.h"
#include "WKCookieManager.h"
#include "WKNetworkSession.h"
#include "WKWebContext.h"
#include "WKWebsiteDataManager.h"

Expand All @@ -33,6 +34,7 @@ extern "C" JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM* javaVM, void* /*reserved*/)
Browser::configureJNIMappings();
WKCallback::configureJNIMappings();
WKCookieManager::configureJNIMappings();
WKNetworkSession::configureJNIMappings();
WKWebContext::configureJNIMappings();
WKWebsiteDataManager::configureJNIMappings();
Page::configureJNIMappings();
Expand Down
30 changes: 3 additions & 27 deletions wpe/src/main/cpp/Browser/WKCallback.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,46 +29,22 @@ class JNIWKCallbackCache final : public JNI::TypedClass<JNIWKCallback> {
public:
JNIWKCallbackCache()
: JNI::TypedClass<JNIWKCallback>(true)
, m_onStringResult(getStaticMethod<void(JNIWKCallback, jstring)>("onStringResult"))
, m_onStringResult(getStaticMethod<void(jstring, JNIWKCallback)>("onStringResult"))
{
}

void onStringResult(JNIWKCallback callback, jstring result) const noexcept
{
try {
m_onStringResult.invoke(callback, result);
m_onStringResult.invoke(result, callback);
} catch (const std::exception& ex) {
Logging::logError("cannot call WKCallback callback result (%s)", ex.what());
}
/*
try {
JNIEnv* env = JNI::getCurrentThreadJNIEnv();
//env->DeleteGlobalRef(callback);
} catch (const std::exception& ex) {
Logging::logError("Failed to release WKCallback reference (%s)", ex.what());
}
*/
}
/*
void onResult(JNIWKWebsiteDataManagerCallbackHolder callbackHolder, jboolean result) const noexcept
{
try {
m_commitResult.invoke(callbackHolder, result);
} catch (const std::exception& ex) {
Logging::logError("cannot call WKWebsiteDataManager callback result (%s)", ex.what());
}

try {
JNIEnv* env = JNI::getCurrentThreadJNIEnv();
env->DeleteGlobalRef(callbackHolder);
} catch (const std::exception& ex) {
Logging::logError("Failed to release WKWebsiteDataManager.CallbackHolder reference (%s)", ex.what());
}
}
*/
private:
// NOLINTBEGIN(cppcoreguidelines-avoid-const-or-ref-data-members)
const JNI::StaticMethod<void(JNIWKCallback, jstring)> m_onStringResult;
const JNI::StaticMethod<void(jstring, JNIWKCallback)> m_onStringResult;
// NOLINTEND(cppcoreguidelines-avoid-const-or-ref-data-members)
};

Expand Down
6 changes: 5 additions & 1 deletion wpe/src/main/cpp/Browser/WKCookieManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,8 @@ JNIWKCookieManagerCache::JNIWKCookieManagerCache()
* Native WKCookieManager class implementation
**********************************************************************************************************************/

void WKCookieManager::configureJNIMappings() { JNIWKCookieManagerCache(); }
namespace WKCookieManager {

void configureJNIMappings() { getJNIWKCookieManagerCache(); }

} // namespace WKCookieManager
9 changes: 5 additions & 4 deletions wpe/src/main/cpp/Browser/WKCookieManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@

DECLARE_JNI_CLASS_SIGNATURE(JNIWKCookieManager, "com/wpe/wpe/WKCookieManager");

class WKCookieManager final {
public:
static void configureJNIMappings();
};
namespace WKCookieManager {

void configureJNIMappings();

} // namespace WKCookieManager
96 changes: 96 additions & 0 deletions wpe/src/main/cpp/Browser/WKNetworkSession.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
#include "WKNetworkSession.h"

#include "Logging.h"
#include "WKWebContext.h"

#include <unistd.h>

/***********************************************************************************************************************
* JNI mapping with Java WKNetworkSession class
**********************************************************************************************************************/

class JNIWKNetworkSessionCache;
const JNIWKNetworkSessionCache& getJNIWKNetworkSessionCache();

class JNIWKNetworkSessionCache final : public JNI::TypedClass<JNIWKNetworkSession> {
public:
JNIWKNetworkSessionCache();

private:
static jlong nativeInit(
JNIEnv* env, jobject obj, jlong wkWebContextPtr, jboolean automationMode, jstring dataDir, jstring cacheDir);
static void nativeDestroy(JNIEnv* env, jobject obj, jlong networkSessionPtr) noexcept;
static jlong nativeCookieManager(JNIEnv* env, jobject obj, jlong networkSessionPtr) noexcept;
static jlong nativeWebsiteDataManager(JNIEnv* env, jobject obj, jlong networkSessionPtr) noexcept;
};

const JNIWKNetworkSessionCache& getJNIWKNetworkSessionCache()
{
static const JNIWKNetworkSessionCache s_singleton;
return s_singleton;
}

JNIWKNetworkSessionCache::JNIWKNetworkSessionCache()
: JNI::TypedClass<JNIWKNetworkSession>(true)
{
registerNativeMethods(
JNI::NativeMethod<jlong(jlong, jboolean, jstring, jstring)>("nativeInit", JNIWKNetworkSessionCache::nativeInit),
JNI::NativeMethod<void(jlong)>("nativeDestroy", JNIWKNetworkSessionCache::nativeDestroy),
JNI::NativeMethod<jlong(jlong)>("nativeCookieManager", JNIWKNetworkSessionCache::nativeCookieManager),
JNI::NativeMethod<jlong(jlong)>(
"nativeWebsiteDataManager", JNIWKNetworkSessionCache::nativeWebsiteDataManager));
}

jlong JNIWKNetworkSessionCache::nativeInit(
JNIEnv* env, jobject obj, jlong wkWebContextPtr, jboolean automationMode, jstring dataDir, jstring cacheDir)
{
Logging::logDebug("JNIWKNetworkSessionCache::nativeInit(%p, %d, %d) [tid %d]", obj, gettid());
auto* wkWebContext = reinterpret_cast<WKWebContext*>(wkWebContextPtr); // NOLINT(performance-no-int-to-ptr)
// NOLINTNEXTLINE(cppcoreguidelines-owning-memory)
auto* wkNetworkSession = new WKNetworkSession(env, reinterpret_cast<JNIWKNetworkSession>(obj), wkWebContext,
static_cast<unsigned int>(automationMode) != 0U, JNI::String(dataDir).getContent().get(),
JNI::String(cacheDir).getContent().get());
return reinterpret_cast<jlong>(wkNetworkSession);
}

void JNIWKNetworkSessionCache::nativeDestroy(JNIEnv* /*env*/, jobject /*obj*/, jlong networkSessionPtr) noexcept
{
Logging::logDebug("JNIWKNetworkSessionCache::nativeDestroy() [tid %d]", gettid());
auto* wkNetworkSession
= reinterpret_cast<WKNetworkSession*>(networkSessionPtr); // NOLINT(performance-no-int-to-ptr)
delete wkNetworkSession; // NOLINT(cppcoreguidelines-owning-memory)
}

jlong JNIWKNetworkSessionCache::nativeCookieManager(JNIEnv* /*env*/, jobject /*obj*/, jlong networkSessionPtr) noexcept
{
auto* wkNetworkSession
= reinterpret_cast<WKNetworkSession*>(networkSessionPtr); // NOLINT(performance-no-int-to-ptr)
return reinterpret_cast<jlong>(webkit_network_session_get_cookie_manager(wkNetworkSession->networkSession()));
}

jlong JNIWKNetworkSessionCache::nativeWebsiteDataManager(
JNIEnv* /*env*/, jobject /*obj*/, jlong networkSessionPtr) noexcept
{
auto* wkNetworkSession
= reinterpret_cast<WKNetworkSession*>(networkSessionPtr); // NOLINT(performance-no-int-to-ptr)
return reinterpret_cast<jlong>(webkit_network_session_get_website_data_manager(wkNetworkSession->networkSession()));
}

/***********************************************************************************************************************
* Native WKWebsiteDataManager class implementation
**********************************************************************************************************************/

void WKNetworkSession::configureJNIMappings() { getJNIWKNetworkSessionCache(); }

WKNetworkSession::WKNetworkSession(JNIEnv* env, JNIWKNetworkSession jniWKNetworkSession, WKWebContext* wkWebContext,
bool automationMode, const char* dataDir, const char* cacheDir)
: m_networkSessionJavaInstance(JNI::createTypedProtectedRef(env, jniWKNetworkSession, true))
{
if (automationMode) {
m_networkSession
= {g_object_ref(webkit_web_context_get_network_session_for_automation(wkWebContext->webContext())),
[](auto* ptr) { g_object_unref(ptr); }};
} else {
m_networkSession = {webkit_network_session_new(dataDir, cacheDir), [](auto* ptr) { g_object_unref(ptr); }};
}
}
54 changes: 54 additions & 0 deletions wpe/src/main/cpp/Browser/WKNetworkSession.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/**
* Copyright (C) 2024 Igalia S.L. <[email protected]>
* Author: Jani Hautakangas <[email protected]>
*
* 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

#include "JNI/JNI.h"

#include <wpe/webkit.h>

DECLARE_JNI_CLASS_SIGNATURE(JNIWKNetworkSession, "com/wpe/wpe/WKNetworkSession");

class WKWebContext;

class WKNetworkSession final {
public:
static void configureJNIMappings();

WKNetworkSession(WKNetworkSession&&) = delete;
WKNetworkSession& operator=(WKNetworkSession&&) = delete;
WKNetworkSession(const WKNetworkSession&) = delete;
WKNetworkSession& operator=(const WKNetworkSession&) = delete;

~WKNetworkSession() = default;

WebKitNetworkSession* networkSession() const noexcept { return m_networkSession.get(); }

private:
friend class JNIWKNetworkSessionCache;

WKNetworkSession(JNIEnv* env, JNIWKNetworkSession jniWKNetworkSession, WKWebContext* wkWebContext,
bool automationMode, const char* dataDir, const char* cacheDir);

template <typename T> using ProtectedUniquePointer = std::unique_ptr<T, std::function<void(T*)>>;

JNI::ProtectedType<JNIWKNetworkSession> m_networkSessionJavaInstance;

ProtectedUniquePointer<WebKitNetworkSession> m_networkSession {};
};
19 changes: 7 additions & 12 deletions wpe/src/main/cpp/Browser/WKWebContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class JNIWKWebContextCache final : public JNI::TypedClass<JNIWKWebContext> {
const JNI::Method<jlong()> m_createPageForAutomation;
// NOLINTEND(cppcoreguidelines-avoid-const-or-ref-data-members)

static jlong nativeInit(JNIEnv* env, jobject obj, jlong nativeWebsiteDataManagerPtr, jboolean automationMode);
static jlong nativeInit(JNIEnv* env, jobject obj, jboolean automationMode);
static void nativeDestroy(JNIEnv* env, jobject obj, jlong wkWebContextPtr) noexcept;
};

Expand All @@ -65,19 +65,16 @@ JNIWKWebContextCache::JNIWKWebContextCache()
: JNI::TypedClass<JNIWKWebContext>(true)
, m_createPageForAutomation(getMethod<jlong()>("createPageForAutomation"))
{
registerNativeMethods(JNI::NativeMethod<jlong(jlong, jboolean)>("nativeInit", JNIWKWebContextCache::nativeInit),
registerNativeMethods(JNI::NativeMethod<jlong(jboolean)>("nativeInit", JNIWKWebContextCache::nativeInit),
JNI::NativeMethod<void(jlong)>("nativeDestroy", JNIWKWebContextCache::nativeDestroy));
}

jlong JNIWKWebContextCache::nativeInit(
JNIEnv* env, jobject obj, jlong nativeWebsiteDataManagerPtr, jboolean automationMode)
jlong JNIWKWebContextCache::nativeInit(JNIEnv* env, jobject obj, jboolean automationMode)
{
Logging::logDebug("WKWebContext::nativeInit(%p, %d, %d) [tid %d]", obj, gettid());
auto* wkWebsiteDataManager = reinterpret_cast<WKWebsiteDataManager*>(nativeWebsiteDataManagerPtr); // NOLINT
// (performance-no-int-to-ptr)
// NOLINTNEXTLINE(cppcoreguidelines-owning-memory)
auto* wkWebContext = new WKWebContext(env, reinterpret_cast<JNIWKWebContext>(obj), wkWebsiteDataManager,
static_cast<unsigned int>(automationMode) != 0U);
auto* wkWebContext = new WKWebContext(
env, reinterpret_cast<JNIWKWebContext>(obj), static_cast<unsigned int>(automationMode) != 0U);
return reinterpret_cast<jlong>(wkWebContext);
}

Expand All @@ -94,14 +91,12 @@ void JNIWKWebContextCache::nativeDestroy(JNIEnv* /*env*/, jobject /*obj*/, jlong

void WKWebContext::configureJNIMappings() { getJNIWKWebContextCache(); }

WKWebContext::WKWebContext(
JNIEnv* env, JNIWKWebContext jniWKWebContext, WKWebsiteDataManager* wkWebsiteDataManager, bool automationMode)
WKWebContext::WKWebContext(JNIEnv* env, JNIWKWebContext jniWKWebContext, bool automationMode)
: m_webContextJavaInstance(JNI::createTypedProtectedRef(env, jniWKWebContext, true))
{
m_automationMode = automationMode;

m_webContext = {webkit_web_context_new_with_website_data_manager(wkWebsiteDataManager->websiteDataManager()),
[](auto* ptr) { g_object_unref(ptr); }};
m_webContext = {webkit_web_context_new(), [](auto* ptr) { g_object_unref(ptr); }};
webkit_web_context_set_automation_allowed(m_webContext.get(), automationMode ? TRUE : FALSE);
if (automationMode) {
g_signal_connect_swapped(
Expand Down
5 changes: 1 addition & 4 deletions wpe/src/main/cpp/Browser/WKWebContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@

DECLARE_JNI_CLASS_SIGNATURE(JNIWKWebContext, "com/wpe/wpe/WKWebContext");

class WKWebsiteDataManager;

class WKWebContext final {
public:
static void configureJNIMappings();
Expand All @@ -45,8 +43,7 @@ class WKWebContext final {
private:
friend class JNIWKWebContextCache;

WKWebContext(
JNIEnv* env, JNIWKWebContext jniWKWebContext, WKWebsiteDataManager* wkWebsiteDataManager, bool automationMode);
WKWebContext(JNIEnv* env, JNIWKWebContext jniWKWebContext, bool automationMode);

template <typename T> using ProtectedUniquePointer = std::unique_ptr<T, std::function<void(T*)>>;

Expand Down
Loading

0 comments on commit 0960759

Please sign in to comment.