aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/events.hpp110
-rw-r--r--src/widgets/WebViewStack.cpp24
-rw-r--r--src/widgets/WebViewStack.hpp1
3 files changed, 120 insertions, 15 deletions
diff --git a/src/events.hpp b/src/events.hpp
index ffa4f8b..f16c2a1 100644
--- a/src/events.hpp
+++ b/src/events.hpp
@@ -1,8 +1,12 @@
#pragma once
#include <QtCore>
+#include <functional>
#include <lua.hpp>
+#include <memory>
+#include <qwebenginepermission.h>
+#include "LuaRuntime.hpp"
#include "lua.h"
#include "widgets/BrowserWindow.hpp"
#include "widgets/WebViewStack.hpp"
@@ -15,30 +19,126 @@
class BrowserEvent {
public:
QString kind = "-";
- virtual ~BrowserEvent() = default;
- virtual void lua_push(lua_State *state) const { lua_newtable(state); };
+ virtual void lua_push(lua_State *state) const {
+ lua_newtable(state);
+ SET_FIELD("type", string, kind.toStdString().c_str())
+ };
};
class UrlChangedEvent : public BrowserEvent {
public:
- const QString url;
+ const QString &url;
const WebViewId webview_id;
const WindowId win_id;
- UrlChangedEvent(QString url, WebViewId webview_id, WindowId win_id)
- : url(std::move(url)), webview_id(webview_id), win_id(win_id) {
+ UrlChangedEvent(const QString &url, WebViewId webview_id, WindowId win_id)
+ : url(url), webview_id(webview_id), win_id(win_id) {
kind = "UrlChanged";
}
void lua_push(lua_State *state) const override {
lua_newtable(state);
+ SET_FIELD("type", string, kind.toStdString().c_str())
SET_FIELD("view_id", integer, webview_id)
SET_FIELD("win_id", integer, win_id)
SET_FIELD("url", string, url.toStdString().c_str())
}
};
+class PermissionRequestEvent : public BrowserEvent {
+public:
+ const QWebEnginePermission::PermissionType premission_type;
+ const WebViewId webview_id;
+ const WindowId win_id;
+ std::function<void()> accept_request;
+ std::function<void()> reject_request;
+
+ PermissionRequestEvent(QWebEnginePermission::PermissionType type,
+ std::function<void()> accept_request, std::function<void()> reject_request,
+ WebViewId webview_id, WindowId win_id)
+ : premission_type(type), webview_id(webview_id), win_id(win_id),
+ accept_request(std::move(accept_request)), reject_request(std::move(reject_request)) {
+ kind = "PermissionRequested";
+ }
+
+ static PermissionRequestEvent *from_permission(std::shared_ptr<QWebEnginePermission> &permission,
+ WebViewId webview_id, WindowId win_id) {
+ auto accept = [permission]() {
+ QMetaObject::invokeMethod(qApp, [=]() { permission->grant(); }, Qt::QueuedConnection);
+ };
+ auto reject = [permission]() {
+ QMetaObject::invokeMethod(qApp, [=]() { permission->deny(); }, Qt::QueuedConnection);
+ };
+ // TODO: Manage delete for permission object
+ return new PermissionRequestEvent(permission->permissionType(), accept, reject, webview_id,
+ win_id);
+ }
+
+ [[nodiscard]] const char *permission_type() const {
+ switch (premission_type) {
+ case QWebEnginePermission::PermissionType::MediaAudioCapture:
+ return "MediaAudioCapture";
+ case QWebEnginePermission::PermissionType::MediaVideoCapture:
+ return "MediaVideoCapture";
+ case QWebEnginePermission::PermissionType::MediaAudioVideoCapture:
+ return "MediaAudioVideoCapture";
+ case QWebEnginePermission::PermissionType::DesktopVideoCapture:
+ return "DesktopVideoCapture";
+ case QWebEnginePermission::PermissionType::DesktopAudioVideoCapture:
+ return "DesktopAudioVideoCapture";
+ case QWebEnginePermission::PermissionType::MouseLock:
+ return "MouseLock";
+ case QWebEnginePermission::PermissionType::Notifications:
+ return "Notifications";
+ case QWebEnginePermission::PermissionType::Geolocation:
+ return "Geolocation";
+ case QWebEnginePermission::PermissionType::ClipboardReadWrite:
+ return "ClipboardReadWrite";
+ case QWebEnginePermission::PermissionType::LocalFontsAccess:
+ return "LocalFontsAccess";
+ default:
+ return "Unsupported";
+ }
+ }
+
+ void lua_push(lua_State *state) const override {
+ lua_newtable(state);
+ SET_FIELD("type", string, kind.toStdString().c_str())
+ SET_FIELD("permission_type", string, permission_type())
+ SET_FIELD("view_id", integer, webview_id)
+ SET_FIELD("win_id", integer, win_id)
+
+ lua_pushstring(state, "accept");
+ lua_pushlightuserdata(state, (void *)this);
+ lua_pushcclosure(
+ state,
+ [](lua_State *state) {
+ void *userdata = lua_touserdata(state, lua_upvalueindex(1));
+ auto *event = static_cast<PermissionRequestEvent *>(userdata);
+ event->accept_request();
+ lua_pushnil(state);
+ return 1;
+ },
+ 1);
+ lua_settable(state, -3);
+
+ lua_pushstring(state, "reject");
+ lua_pushlightuserdata(state, (void *)this);
+ lua_pushcclosure(
+ state,
+ [](lua_State *state) {
+ void *userdata = lua_touserdata(state, lua_upvalueindex(1));
+ auto *event = static_cast<PermissionRequestEvent *>(userdata);
+ event->reject_request();
+ lua_pushnil(state);
+ return 1;
+ },
+ 1);
+ lua_settable(state, -3);
+ }
+};
+
struct EventHandlerRequest {
std::vector<QString> event_names;
std::vector<QString> patterns;
diff --git a/src/widgets/WebViewStack.cpp b/src/widgets/WebViewStack.cpp
index c48a363..7fe9218 100644
--- a/src/widgets/WebViewStack.cpp
+++ b/src/widgets/WebViewStack.cpp
@@ -5,6 +5,7 @@
#include <QWebEngineProfile>
#include <cstdint>
#include <cstdlib>
+#include <memory>
#include <vector>
#include "WindowActionRouter.hpp"
@@ -48,17 +49,22 @@ WebView *WebViewStack::create_new_webview(const QUrl &url, bool focus) {
layout->addWidget(webview);
webview_list.append(webview);
- connect(webview->page(), &QWebEnginePage::newWindowRequested, this,
- &WebViewStack::on_new_webview_request);
- connect(webview->page(), &QWebEnginePage::urlChanged, this, [webview](const QUrl &url) {
+ auto *page = webview->page();
+ connect(page, &QWebEnginePage::newWindowRequested, this, &WebViewStack::on_new_webview_request);
+ connect(page, &QWebEnginePage::urlChanged, this, [webview](const QUrl &url) {
// TODO: Add window id
- WindowActionRouter::instance().dispatch_event(
- new UrlChangedEvent(url.toString(), webview->get_id(), 0));
+ auto *event = new UrlChangedEvent(url.toString(), webview->get_id(), 0);
+ WindowActionRouter::instance().dispatch_event(event);
+ });
+ connect(page, &QWebEnginePage::titleChanged, this, [this](const QString & /* title */) {
+ emit current_webview_title_changed(layout->currentIndex());
+ });
+ connect(page, &QWebEnginePage::permissionRequested, this, [webview](QWebEnginePermission perm) {
+ auto permission = std::make_shared<QWebEnginePermission>(std::move(perm));
+ // TODO: Add windown id
+ auto *event = PermissionRequestEvent::from_permission(permission, webview->get_id(), 0);
+ WindowActionRouter::instance().dispatch_event(event);
});
- connect(webview->page(), &QWebEnginePage::titleChanged, this,
- [this](const QString & /* title */) {
- emit current_webview_title_changed(layout->currentIndex());
- });
if (focus)
focus_webview(webview->get_id());
diff --git a/src/widgets/WebViewStack.hpp b/src/widgets/WebViewStack.hpp
index 1b8b288..cee0593 100644
--- a/src/widgets/WebViewStack.hpp
+++ b/src/widgets/WebViewStack.hpp
@@ -3,7 +3,6 @@
#include <QStackedLayout>
#include <QWebEngineProfile>
#include <cstdint>
-#include <functional>
#include <qwebengineprofile.h>
#include <vector>