Skip to content

Commit 2c95b16

Browse files
committed
Unify backend instance creation.
1 parent cc8f41d commit 2c95b16

13 files changed

+390
-247
lines changed

webview/platform/linux/webview_linux_interface.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@
2222
<arg type='b' name='accepted' direction='out'/>
2323
<arg type='s' name='text' direction='out'/>
2424
</method>
25+
<method name='NavigationStateUpdate'>
26+
<arg type='s' name='url' direction='in'/>
27+
<arg type='s' name='title' direction='in'/>
28+
<arg type='b' name='canGoBack' direction='in'/>
29+
<arg type='b' name='canGoForward' direction='in'/>
30+
</method>
2531
</interface>
2632
<interface name='org.desktop_app.GtkIntegration.Webview.Helper'>
2733
<method name='Create'>

webview/platform/linux/webview_linux_webkitgtk.cpp

Lines changed: 62 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424
#include <webview/webview.hpp>
2525
#include <crl/crl.h>
26-
#include <rpl/range.h>
26+
#include <rpl/rpl.h>
2727
#include <regex>
2828

2929
namespace Webview::WebKitGTK {
@@ -67,22 +67,18 @@ class Instance final : public Interface, public ::base::has_weak_ptr {
6767

6868
ResolveResult resolve();
6969

70-
bool finishEmbedding() override;
71-
7270
void navigate(std::string url) override;
7371
void navigateToData(std::string id) override;
7472
void reload() override;
7573

76-
void resizeToWindow() override;
77-
7874
void init(std::string js) override;
7975
void eval(std::string js) override;
8076

8177
void focus() override;
8278

8379
QWidget *widget() override;
84-
void *winId() override;
8580

81+
void refreshNavigationHistoryState() override;
8682
auto navigationHistoryState()
8783
-> rpl::producer<NavigationHistoryState> override;
8884

@@ -108,10 +104,13 @@ class Instance final : public Interface, public ::base::has_weak_ptr {
108104

109105
void startProcess();
110106
void stopProcess();
107+
void updateHistoryStates();
111108

112109
void registerMasterMethodHandlers();
113110
void registerHelperMethodHandlers();
114111

112+
void *winId();
113+
115114
bool _remoting = false;
116115
bool _connected = false;
117116
Master _master;
@@ -134,6 +133,7 @@ class Instance final : public Interface, public ::base::has_weak_ptr {
134133
std::function<bool(std::string,bool)> _navigationStartHandler;
135134
std::function<void(bool)> _navigationDoneHandler;
136135
std::function<DialogResult(DialogArgs)> _dialogHandler;
136+
rpl::variable<NavigationHistoryState> _navigationHistoryState;
137137
bool _loadFailed = false;
138138

139139
};
@@ -350,6 +350,26 @@ bool Instance::create(Config config) {
350350
instance->loadChanged(loadEvent);
351351
}),
352352
this);
353+
g_signal_connect_swapped(
354+
_webview,
355+
"notify::uri",
356+
G_CALLBACK(+[](
357+
Instance *instance,
358+
GParamSpec *pspec) -> gboolean {
359+
instance->updateHistoryStates();
360+
return true;
361+
}),
362+
this);
363+
g_signal_connect_swapped(
364+
_webview,
365+
"notify::title",
366+
G_CALLBACK(+[](
367+
Instance *instance,
368+
GParamSpec *pspec) -> gboolean {
369+
instance->updateHistoryStates();
370+
return true;
371+
}),
372+
this);
353373
g_signal_connect_swapped(
354374
_webview,
355375
"decide-policy",
@@ -447,6 +467,7 @@ void Instance::loadChanged(WebKitLoadEvent loadEvent) {
447467
_master.call_navigation_done(!_loadFailed, nullptr);
448468
}
449469
}
470+
updateHistoryStates();
450471
}
451472

452473
bool Instance::decidePolicy(
@@ -564,10 +585,6 @@ ResolveResult Instance::resolve() {
564585
return Resolve(_wayland);
565586
}
566587

567-
bool Instance::finishEmbedding() {
568-
return true;
569-
}
570-
571588
void Instance::navigate(std::string url) {
572589
if (_remoting) {
573590
if (!_helper) {
@@ -651,6 +668,9 @@ void Instance::eval(std::string js) {
651668
}
652669

653670
void Instance::focus() {
671+
if (const auto widget = _widget.get()) {
672+
widget->activateWindow();
673+
}
654674
}
655675

656676
QWidget *Instance::widget() {
@@ -689,9 +709,13 @@ void *Instance::winId() {
689709
return reinterpret_cast<void*>(gtk_plug_get_id(GTK_PLUG(_window)));
690710
}
691711

712+
void Instance::refreshNavigationHistoryState() {
713+
// Not needed here, there are events.
714+
}
715+
692716
auto Instance::navigationHistoryState()
693717
-> rpl::producer<NavigationHistoryState> {
694-
return rpl::single(NavigationHistoryState());
718+
return _navigationHistoryState.value();
695719
}
696720

697721
void Instance::setOpaqueBg(QColor opaqueBg) {
@@ -728,9 +752,6 @@ void Instance::setOpaqueBg(QColor opaqueBg) {
728752
}
729753
}
730754

731-
void Instance::resizeToWindow() {
732-
}
733-
734755
void Instance::startProcess() {
735756
auto loop = GLib::MainLoop::new_();
736757

@@ -864,6 +885,17 @@ void Instance::stopProcess() {
864885
}
865886
}
866887

888+
void Instance::updateHistoryStates() {
889+
const auto url = webkit_web_view_get_uri(_webview);
890+
const auto title = webkit_web_view_get_title(_webview);
891+
_master.call_navigation_state_update(
892+
url ? url : "",
893+
title ? title : "",
894+
webkit_web_view_can_go_back(_webview),
895+
webkit_web_view_can_go_forward(_webview),
896+
nullptr);
897+
}
898+
867899
void Instance::registerMasterMethodHandlers() {
868900
if (!_master) {
869901
return;
@@ -970,6 +1002,22 @@ void Instance::registerMasterMethodHandlers() {
9701002

9711003
return true;
9721004
});
1005+
1006+
_master.signal_handle_navigation_state_update().connect([=](
1007+
Master,
1008+
Gio::DBusMethodInvocation invocation,
1009+
const std::string &url,
1010+
const std::string &title,
1011+
bool canGoBack,
1012+
bool canGoForward) {
1013+
_navigationHistoryState = NavigationHistoryState{
1014+
.url = url,
1015+
.title = title,
1016+
.canGoBack = canGoBack,
1017+
.canGoForward = canGoForward,
1018+
};
1019+
return true;
1020+
});
9731021
}
9741022

9751023
int Instance::exec() {

webview/platform/linux/webview_linux_webkitgtk_library.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@ ResolveResult Resolve(bool wayland) {
5656
&& LOAD_LIBRARY_SYMBOL(lib, webkit_script_dialog_prompt_set_text)
5757
&& LOAD_LIBRARY_SYMBOL(lib, webkit_web_view_get_type)
5858
&& LOAD_LIBRARY_SYMBOL(lib, webkit_web_view_get_user_content_manager)
59+
&& LOAD_LIBRARY_SYMBOL(lib, webkit_web_view_get_uri)
60+
&& LOAD_LIBRARY_SYMBOL(lib, webkit_web_view_get_title)
61+
&& LOAD_LIBRARY_SYMBOL(lib, webkit_web_view_can_go_back)
62+
&& LOAD_LIBRARY_SYMBOL(lib, webkit_web_view_can_go_forward)
5963
&& LOAD_LIBRARY_SYMBOL(lib, webkit_user_content_manager_register_script_message_handler)
6064
&& LOAD_LIBRARY_SYMBOL(lib, webkit_web_view_get_settings)
6165
&& LOAD_LIBRARY_SYMBOL(lib, webkit_settings_set_enable_developer_extras)

webview/platform/linux/webview_linux_webkitgtk_library.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,10 @@ inline GtkWidget *(*webkit_web_view_new_with_context)(WebKitWebContext *context)
185185
inline GType (*webkit_web_view_get_type)(void);
186186
inline WebKitUserContentManager *(*webkit_web_view_get_user_content_manager)(
187187
WebKitWebView *web_view);
188+
inline const gchar *(*webkit_web_view_get_uri)(WebKitWebView *web_view);
189+
inline const gchar *(*webkit_web_view_get_title)(WebKitWebView *web_view);
190+
inline gboolean (*webkit_web_view_can_go_back)(WebKitWebView *web_view);
191+
inline gboolean (*webkit_web_view_can_go_forward)(WebKitWebView *web_view);
188192
inline gboolean (*webkit_user_content_manager_register_script_message_handler)(
189193
WebKitUserContentManager *manager,
190194
const gchar *name,

0 commit comments

Comments
 (0)