aboutsummaryrefslogtreecommitdiff
path: root/src/events
diff options
context:
space:
mode:
authorAkshay Nair <phenax5@gmail.com>2025-05-01 22:38:12 +0530
committerAkshay Nair <phenax5@gmail.com>2025-05-02 16:21:23 +0530
commitf0faa92246c1f5fc58546051ace2d047869447df (patch)
tree68ac05f65ca59915d5459b9957fe874e616fc2be /src/events
parent86496d0ce96f2e57f3f301c08488b6421321b3c9 (diff)
downloadnull-browser-f0faa92246c1f5fc58546051ace2d047869447df.tar.gz
null-browser-f0faa92246c1f5fc58546051ace2d047869447df.zip
Refactor events into separate files
Diffstat (limited to 'src/events')
-rw-r--r--src/events/Event.hpp19
-rw-r--r--src/events/EventHandlerRequest.hpp12
-rw-r--r--src/events/PermissionRequestedEvent.hpp104
-rw-r--r--src/events/UrlChangedEvent.hpp28
4 files changed, 163 insertions, 0 deletions
diff --git a/src/events/Event.hpp b/src/events/Event.hpp
new file mode 100644
index 0000000..a0853d6
--- /dev/null
+++ b/src/events/Event.hpp
@@ -0,0 +1,19 @@
+#pragma once
+
+#include <QtCore>
+#include <lua.hpp>
+
+#define SET_FIELD(NAME, TYPE, VALUE) \
+ lua_pushstring(state, NAME); \
+ lua_push##TYPE(state, VALUE); \
+ lua_settable(state, -3);
+
+class Event {
+public:
+ QString kind = "-";
+
+ virtual void lua_push(lua_State *state) const {
+ lua_newtable(state);
+ SET_FIELD("type", string, kind.toStdString().c_str())
+ };
+};
diff --git a/src/events/EventHandlerRequest.hpp b/src/events/EventHandlerRequest.hpp
new file mode 100644
index 0000000..94f4afc
--- /dev/null
+++ b/src/events/EventHandlerRequest.hpp
@@ -0,0 +1,12 @@
+#pragma once
+
+#include <QtCore>
+
+#include "events/Event.hpp"
+
+struct EventHandlerRequest {
+ std::vector<QString> event_names;
+ std::vector<QString> patterns;
+ std::function<void(Event *)> handler;
+ int function_ref;
+};
diff --git a/src/events/PermissionRequestedEvent.hpp b/src/events/PermissionRequestedEvent.hpp
new file mode 100644
index 0000000..089cc99
--- /dev/null
+++ b/src/events/PermissionRequestedEvent.hpp
@@ -0,0 +1,104 @@
+#pragma once
+
+#include <QWebEnginePermission>
+#include <QtCore>
+#include <lua.hpp>
+
+#include "events/Event.hpp"
+#include "widgets/BrowserWindow.hpp"
+#include "widgets/WebViewStack.hpp"
+
+class PermissionRequestedEvent : public Event {
+public:
+ const QWebEnginePermission::PermissionType premission_type;
+ const WebViewId webview_id;
+ const WindowId win_id;
+ std::function<void()> accept_request;
+ std::function<void()> reject_request;
+
+ PermissionRequestedEvent(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 PermissionRequestedEvent *
+ 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 PermissionRequestedEvent(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<PermissionRequestedEvent *>(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<PermissionRequestedEvent *>(userdata);
+ event->reject_request();
+ lua_pushnil(state);
+ return 1;
+ },
+ 1);
+ lua_settable(state, -3);
+ }
+};
diff --git a/src/events/UrlChangedEvent.hpp b/src/events/UrlChangedEvent.hpp
new file mode 100644
index 0000000..1685c89
--- /dev/null
+++ b/src/events/UrlChangedEvent.hpp
@@ -0,0 +1,28 @@
+#pragma once
+
+#include <QtCore>
+#include <lua.hpp>
+
+#include "events/Event.hpp"
+#include "widgets/BrowserWindow.hpp"
+#include "widgets/WebViewStack.hpp"
+
+class UrlChangedEvent : public Event {
+public:
+ const QString &url;
+ const WebViewId webview_id;
+ const WindowId 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())
+ }
+};