Skip to content

Commit

Permalink
Unify backend instance creation.
Browse files Browse the repository at this point in the history
  • Loading branch information
john-preston committed Jul 30, 2024
1 parent cc8f41d commit ad794b1
Show file tree
Hide file tree
Showing 13 changed files with 354 additions and 253 deletions.
6 changes: 6 additions & 0 deletions webview/platform/linux/webview_linux_interface.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@
<arg type='b' name='accepted' direction='out'/>
<arg type='s' name='text' direction='out'/>
</method>
<method name='NavigationStateUpdate'>
<arg type='s' name='url' direction='in'/>
<arg type='s' name='title' direction='in'/>
<arg type='b' name='canGoBack' direction='in'/>
<arg type='b' name='canGoForward' direction='in'/>
</method>
</interface>
<interface name='org.desktop_app.GtkIntegration.Webview.Helper'>
<method name='Create'>
Expand Down
71 changes: 57 additions & 14 deletions webview/platform/linux/webview_linux_webkitgtk.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

#include <webview/webview.hpp>
#include <crl/crl.h>
#include <rpl/range.h>
#include <rpl/rpl.h>
#include <regex>

namespace Webview::WebKitGTK {
Expand Down Expand Up @@ -67,21 +67,16 @@ class Instance final : public Interface, public ::base::has_weak_ptr {

ResolveResult resolve();

bool finishEmbedding() override;

void navigate(std::string url) override;
void navigateToData(std::string id) override;
void reload() override;

void resizeToWindow() override;

void init(std::string js) override;
void eval(std::string js) override;

void focus() override;

QWidget *widget() override;
void *winId() override;

auto navigationHistoryState()
-> rpl::producer<NavigationHistoryState> override;
Expand All @@ -108,10 +103,13 @@ class Instance final : public Interface, public ::base::has_weak_ptr {

void startProcess();
void stopProcess();
void updateHistoryStates();

void registerMasterMethodHandlers();
void registerHelperMethodHandlers();

void *winId();

bool _remoting = false;
bool _connected = false;
Master _master;
Expand All @@ -134,6 +132,7 @@ class Instance final : public Interface, public ::base::has_weak_ptr {
std::function<bool(std::string,bool)> _navigationStartHandler;
std::function<void(bool)> _navigationDoneHandler;
std::function<DialogResult(DialogArgs)> _dialogHandler;
rpl::variable<NavigationHistoryState> _navigationHistoryState;
bool _loadFailed = false;

};
Expand Down Expand Up @@ -350,6 +349,26 @@ bool Instance::create(Config config) {
instance->loadChanged(loadEvent);
}),
this);
g_signal_connect_swapped(
_webview,
"notify::uri",
G_CALLBACK(+[](
Instance *instance,
GParamSpec *pspec) -> gboolean {
instance->updateHistoryStates();
return true;
}),
this);
g_signal_connect_swapped(
_webview,
"notify::title",
G_CALLBACK(+[](
Instance *instance,
GParamSpec *pspec) -> gboolean {
instance->updateHistoryStates();
return true;
}),
this);
g_signal_connect_swapped(
_webview,
"decide-policy",
Expand Down Expand Up @@ -447,6 +466,7 @@ void Instance::loadChanged(WebKitLoadEvent loadEvent) {
_master.call_navigation_done(!_loadFailed, nullptr);
}
}
updateHistoryStates();
}

bool Instance::decidePolicy(
Expand Down Expand Up @@ -564,10 +584,6 @@ ResolveResult Instance::resolve() {
return Resolve(_wayland);
}

bool Instance::finishEmbedding() {
return true;
}

void Instance::navigate(std::string url) {
if (_remoting) {
if (!_helper) {
Expand Down Expand Up @@ -651,6 +667,9 @@ void Instance::eval(std::string js) {
}

void Instance::focus() {
if (const auto widget = _widget.get()) {
widget->activateWindow();
}
}

QWidget *Instance::widget() {
Expand Down Expand Up @@ -691,7 +710,7 @@ void *Instance::winId() {

auto Instance::navigationHistoryState()
-> rpl::producer<NavigationHistoryState> {
return rpl::single(NavigationHistoryState());
return _navigationHistoryState.value();
}

void Instance::setOpaqueBg(QColor opaqueBg) {
Expand Down Expand Up @@ -728,9 +747,6 @@ void Instance::setOpaqueBg(QColor opaqueBg) {
}
}

void Instance::resizeToWindow() {
}

void Instance::startProcess() {
auto loop = GLib::MainLoop::new_();

Expand Down Expand Up @@ -864,6 +880,17 @@ void Instance::stopProcess() {
}
}

void Instance::updateHistoryStates() {
const auto url = webkit_web_view_get_uri(_webview);
const auto title = webkit_web_view_get_title(_webview);
_master.call_navigation_state_update(
url ? url : "",
title ? title : "",
webkit_web_view_can_go_back(_webview),
webkit_web_view_can_go_forward(_webview),
nullptr);
}

void Instance::registerMasterMethodHandlers() {
if (!_master) {
return;
Expand Down Expand Up @@ -970,6 +997,22 @@ void Instance::registerMasterMethodHandlers() {

return true;
});

_master.signal_handle_navigation_state_update().connect([=](
Master,
Gio::DBusMethodInvocation invocation,
const std::string &url,
const std::string &title,
bool canGoBack,
bool canGoForward) {
_navigationHistoryState = NavigationHistoryState{
.url = url,
.title = title,
.canGoBack = canGoBack,
.canGoForward = canGoForward,
};
return true;
});
}

int Instance::exec() {
Expand Down
4 changes: 4 additions & 0 deletions webview/platform/linux/webview_linux_webkitgtk_library.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ ResolveResult Resolve(bool wayland) {
&& LOAD_LIBRARY_SYMBOL(lib, webkit_script_dialog_prompt_set_text)
&& LOAD_LIBRARY_SYMBOL(lib, webkit_web_view_get_type)
&& LOAD_LIBRARY_SYMBOL(lib, webkit_web_view_get_user_content_manager)
&& LOAD_LIBRARY_SYMBOL(lib, webkit_web_view_get_uri)
&& LOAD_LIBRARY_SYMBOL(lib, webkit_web_view_get_title)
&& LOAD_LIBRARY_SYMBOL(lib, webkit_web_view_can_go_back)
&& LOAD_LIBRARY_SYMBOL(lib, webkit_web_view_can_go_forward)
&& LOAD_LIBRARY_SYMBOL(lib, webkit_user_content_manager_register_script_message_handler)
&& LOAD_LIBRARY_SYMBOL(lib, webkit_web_view_get_settings)
&& LOAD_LIBRARY_SYMBOL(lib, webkit_settings_set_enable_developer_extras)
Expand Down
4 changes: 4 additions & 0 deletions webview/platform/linux/webview_linux_webkitgtk_library.h
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,10 @@ inline GtkWidget *(*webkit_web_view_new_with_context)(WebKitWebContext *context)
inline GType (*webkit_web_view_get_type)(void);
inline WebKitUserContentManager *(*webkit_web_view_get_user_content_manager)(
WebKitWebView *web_view);
inline const gchar *(*webkit_web_view_get_uri)(WebKitWebView *web_view);
inline const gchar *(*webkit_web_view_get_title)(WebKitWebView *web_view);
inline gboolean (*webkit_web_view_can_go_back)(WebKitWebView *web_view);
inline gboolean (*webkit_web_view_can_go_forward)(WebKitWebView *web_view);
inline gboolean (*webkit_user_content_manager_register_script_message_handler)(
WebKitUserContentManager *manager,
const gchar *name,
Expand Down
Loading

0 comments on commit ad794b1

Please sign in to comment.