Skip to content

Commit ad794b1

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

13 files changed

Lines changed: 354 additions & 253 deletions

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: 57 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,21 +67,16 @@ 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

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

109104
void startProcess();
110105
void stopProcess();
106+
void updateHistoryStates();
111107

112108
void registerMasterMethodHandlers();
113109
void registerHelperMethodHandlers();
114110

111+
void *winId();
112+
115113
bool _remoting = false;
116114
bool _connected = false;
117115
Master _master;
@@ -134,6 +132,7 @@ class Instance final : public Interface, public ::base::has_weak_ptr {
134132
std::function<bool(std::string,bool)> _navigationStartHandler;
135133
std::function<void(bool)> _navigationDoneHandler;
136134
std::function<DialogResult(DialogArgs)> _dialogHandler;
135+
rpl::variable<NavigationHistoryState> _navigationHistoryState;
137136
bool _loadFailed = false;
138137

139138
};
@@ -350,6 +349,26 @@ bool Instance::create(Config config) {
350349
instance->loadChanged(loadEvent);
351350
}),
352351
this);
352+
g_signal_connect_swapped(
353+
_webview,
354+
"notify::uri",
355+
G_CALLBACK(+[](
356+
Instance *instance,
357+
GParamSpec *pspec) -> gboolean {
358+
instance->updateHistoryStates();
359+
return true;
360+
}),
361+
this);
362+
g_signal_connect_swapped(
363+
_webview,
364+
"notify::title",
365+
G_CALLBACK(+[](
366+
Instance *instance,
367+
GParamSpec *pspec) -> gboolean {
368+
instance->updateHistoryStates();
369+
return true;
370+
}),
371+
this);
353372
g_signal_connect_swapped(
354373
_webview,
355374
"decide-policy",
@@ -447,6 +466,7 @@ void Instance::loadChanged(WebKitLoadEvent loadEvent) {
447466
_master.call_navigation_done(!_loadFailed, nullptr);
448467
}
449468
}
469+
updateHistoryStates();
450470
}
451471

452472
bool Instance::decidePolicy(
@@ -564,10 +584,6 @@ ResolveResult Instance::resolve() {
564584
return Resolve(_wayland);
565585
}
566586

567-
bool Instance::finishEmbedding() {
568-
return true;
569-
}
570-
571587
void Instance::navigate(std::string url) {
572588
if (_remoting) {
573589
if (!_helper) {
@@ -651,6 +667,9 @@ void Instance::eval(std::string js) {
651667
}
652668

653669
void Instance::focus() {
670+
if (const auto widget = _widget.get()) {
671+
widget->activateWindow();
672+
}
654673
}
655674

656675
QWidget *Instance::widget() {
@@ -691,7 +710,7 @@ void *Instance::winId() {
691710

692711
auto Instance::navigationHistoryState()
693712
-> rpl::producer<NavigationHistoryState> {
694-
return rpl::single(NavigationHistoryState());
713+
return _navigationHistoryState.value();
695714
}
696715

697716
void Instance::setOpaqueBg(QColor opaqueBg) {
@@ -728,9 +747,6 @@ void Instance::setOpaqueBg(QColor opaqueBg) {
728747
}
729748
}
730749

731-
void Instance::resizeToWindow() {
732-
}
733-
734750
void Instance::startProcess() {
735751
auto loop = GLib::MainLoop::new_();
736752

@@ -864,6 +880,17 @@ void Instance::stopProcess() {
864880
}
865881
}
866882

883+
void Instance::updateHistoryStates() {
884+
const auto url = webkit_web_view_get_uri(_webview);
885+
const auto title = webkit_web_view_get_title(_webview);
886+
_master.call_navigation_state_update(
887+
url ? url : "",
888+
title ? title : "",
889+
webkit_web_view_can_go_back(_webview),
890+
webkit_web_view_can_go_forward(_webview),
891+
nullptr);
892+
}
893+
867894
void Instance::registerMasterMethodHandlers() {
868895
if (!_master) {
869896
return;
@@ -970,6 +997,22 @@ void Instance::registerMasterMethodHandlers() {
970997

971998
return true;
972999
});
1000+
1001+
_master.signal_handle_navigation_state_update().connect([=](
1002+
Master,
1003+
Gio::DBusMethodInvocation invocation,
1004+
const std::string &url,
1005+
const std::string &title,
1006+
bool canGoBack,
1007+
bool canGoForward) {
1008+
_navigationHistoryState = NavigationHistoryState{
1009+
.url = url,
1010+
.title = title,
1011+
.canGoBack = canGoBack,
1012+
.canGoForward = canGoForward,
1013+
};
1014+
return true;
1015+
});
9731016
}
9741017

9751018
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)