aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAkshay Nair <phenax5@gmail.com>2025-08-08 13:08:44 +0530
committerAkshay Nair <phenax5@gmail.com>2025-08-08 13:08:44 +0530
commit8ec50e55a45af1706d40eff292db4541b6fcc261 (patch)
tree6b4cb54d638765cc0c3178984a6ec18af4a3a7e4 /src
parentb955a603c27ffb3869901b1899303b936816afcf (diff)
downloadnull-browser-8ec50e55a45af1706d40eff292db4541b6fcc261.tar.gz
null-browser-8ec50e55a45af1706d40eff292db4541b6fcc261.zip
Embed docs as null://docs + web.help api
Diffstat (limited to 'src')
-rw-r--r--src/schemes/NullSchemeHandler.cpp46
-rw-r--r--src/schemes/NullSchemeHandler.hpp30
-rw-r--r--src/schemes/schemes.hpp13
-rw-r--r--src/widgets/BrowserApp.cpp2
4 files changed, 90 insertions, 1 deletions
diff --git a/src/schemes/NullSchemeHandler.cpp b/src/schemes/NullSchemeHandler.cpp
new file mode 100644
index 0000000..203e673
--- /dev/null
+++ b/src/schemes/NullSchemeHandler.cpp
@@ -0,0 +1,46 @@
+#include "NullSchemeHandler.hpp"
+#include <qstringview.h>
+#include <qwebengineurlrequestjob.h>
+
+void NullSchemeHandler::requestStarted(QWebEngineUrlRequestJob *job) {
+ auto url = job->requestUrl();
+
+ if (url.host() == "docs") {
+ QBuffer *buffer = new QBuffer(job);
+ buffer->setData(read_static_docs_file(url.path()));
+ buffer->open(QIODevice::ReadOnly);
+ job->reply(get_content_type(url.path()), buffer);
+ } else {
+ job->reply("text/html", new QBuffer(job));
+ }
+}
+
+QByteArray NullSchemeHandler::read_static_docs_file(const QString &path) {
+ QFile file(NULL_DOCS_DIR + path);
+ if (!file.exists())
+ return read_index_html();
+
+ if (file.open(QIODevice::ReadOnly)) {
+ auto contents = file.readAll();
+ file.close();
+ return contents;
+ }
+ return QByteArray{};
+}
+
+QByteArray NullSchemeHandler::read_index_html() {
+ QFile file(NULL_DOCS_DIR + QString("/index.html"));
+ qDebug() << ":::" << file.exists();
+ if (file.open(QIODevice::ReadOnly)) {
+ auto contents = file.readAll();
+ file.close();
+ return contents;
+ }
+ return QByteArray{};
+}
+
+QByteArray NullSchemeHandler::get_content_type(const QString &path) {
+ if (path.endsWith(".css"))
+ return "text/css";
+ return "text/html";
+}
diff --git a/src/schemes/NullSchemeHandler.hpp b/src/schemes/NullSchemeHandler.hpp
new file mode 100644
index 0000000..bc05076
--- /dev/null
+++ b/src/schemes/NullSchemeHandler.hpp
@@ -0,0 +1,30 @@
+#pragma once
+
+#include <QWebEngineUrlRequestJob>
+#include <QWebEngineUrlSchemeHandler>
+#include <QtCore>
+#include <qcontainerfwd.h>
+#include <qdebug.h>
+#include <qurlquery.h>
+
+// TODO: Install doc dir and pass this path during build
+#define NULL_DOCS_DIR "./doc"
+
+class NullSchemeHandler : public QWebEngineUrlSchemeHandler {
+ Q_OBJECT
+
+public:
+ static NullSchemeHandler &instance() {
+ static NullSchemeHandler handler;
+ return handler;
+ }
+
+ void requestStarted(QWebEngineUrlRequestJob *job) override;
+
+private:
+ NullSchemeHandler() = default;
+
+ QByteArray read_static_docs_file(const QString &path);
+ QByteArray read_index_html();
+ QByteArray get_content_type(const QString &path);
+};
diff --git a/src/schemes/schemes.hpp b/src/schemes/schemes.hpp
index f33d74b..439519c 100644
--- a/src/schemes/schemes.hpp
+++ b/src/schemes/schemes.hpp
@@ -13,4 +13,15 @@ void register_nullrpc_scheme() {
QWebEngineUrlScheme::registerScheme(scheme);
}
-void register_all_schemes() { register_nullrpc_scheme(); }
+void register_null_scheme() {
+ QWebEngineUrlScheme scheme("null");
+ scheme.setSyntax(QWebEngineUrlScheme::Syntax::Host);
+ scheme.setFlags(QWebEngineUrlScheme::SecureScheme | QWebEngineUrlScheme::LocalScheme |
+ QWebEngineUrlScheme::LocalAccessAllowed);
+ QWebEngineUrlScheme::registerScheme(scheme);
+}
+
+void register_all_schemes() {
+ register_nullrpc_scheme();
+ register_null_scheme();
+}
diff --git a/src/widgets/BrowserApp.cpp b/src/widgets/BrowserApp.cpp
index c3c2313..e57b923 100644
--- a/src/widgets/BrowserApp.cpp
+++ b/src/widgets/BrowserApp.cpp
@@ -8,6 +8,7 @@
#include "events/NotificationReceivedEvent.hpp"
#include "events/WinCreatedEvent.hpp"
#include "schemes/NullRpcSchemeHandler.hpp"
+#include "schemes/NullSchemeHandler.hpp"
#include "widgets/BrowserWindow.hpp"
#include "widgets/BrowserApp.hpp"
@@ -59,6 +60,7 @@ void BrowserApp::setup_profile(QWebEngineProfile *profile) {
});
profile->setPersistentPermissionsPolicy(configuration.permission_persistance_policy());
profile->installUrlSchemeHandler("nullrpc", &NullRPCSchemeHandler::instance());
+ profile->installUrlSchemeHandler("null", &NullSchemeHandler::instance());
}
BrowserWindow *BrowserApp::create_window(const QStringList &urls) {