From 51574b7396bc9bb917a1d442c3b3f7ff1cd31c2c Mon Sep 17 00:00:00 2001 From: Aqua-sama Date: Mon, 27 Jan 2020 18:43:21 +0200 Subject: Refactor Browser::addPlugin to ::loadPlugins --- Kconfig | 14 +++++++------- src/browser.cpp | 38 +++++++++++++++++-------------------- src/browser.h | 8 ++++---- src/main.cpp | 47 ++++++++++++---------------------------------- src/mainwindow/menubar.cpp | 2 +- 5 files changed, 41 insertions(+), 68 deletions(-) diff --git a/Kconfig b/Kconfig index 62309b0..1564f19 100644 --- a/Kconfig +++ b/Kconfig @@ -144,20 +144,20 @@ config USEPLUGINS menu "Plugin Settings" depends on USEPLUGINS - config plugins.path + config PLUGINS_PATH string "Plugin load location" default "~/.config/smolbote/plugins.d" - choice PLUGIN_SIGNATURE_CHECK + choice PLUGINS_SIGNATURE_CHECK bool "Plugin Signature enforcement" - default PLUGIN_SIGNATURE_CHECKED - config PLUGIN_SIGNATURE_IGNORED + default PLUGINS_SIGNATURE_CHECKED + config PLUGINS_SIGNATURE_IGNORED bool "Don't check plugin signatures" - config PLUGIN_SIGNATURE_CHECKED + config PLUGINS_SIGNATURE_CHECKED bool "Don't load plugins with invalid signatures" - config PLUGIN_SIGNATURE_ENFORCED + config PLUGINS_SIGNATURE_ENFORCED bool "Only load plugins with valid signatures" endchoice - config PLUGIN_SIGNATURE_HASH + config PLUGINS_SIGNATURE_HASH string "Hashing algorithm used by the signature" default "SHA256" endmenu diff --git a/src/browser.cpp b/src/browser.cpp index 86d8f13..20367aa 100644 --- a/src/browser.cpp +++ b/src/browser.cpp @@ -125,10 +125,10 @@ void Browser::loadProfiles(const QStringList &profilePaths) } } -QPluginLoader *Browser::addPlugin(const QString &path) +void Browser::loadPlugins(const QStringList &paths, const std::function &callback) { - if(path.isEmpty()) - return nullptr; + if(paths.isEmpty()) + return; Configuration conf; const PluginLoader::SignatureState state( @@ -136,33 +136,29 @@ QPluginLoader *Browser::addPlugin(const QString &path) conf.value("plugins.signature.checked").value(), conf.value("plugins.signature.enforced").value()); - auto *loader = new PluginLoader(path, state, this); - const bool loaded = loader->load(); - spdlog::info("Loading plugin [{}]: {}", qUtf8Printable(path), loaded ? "passed" : "failed"); + for(const auto &path : paths) { + auto *loader = new PluginLoader(path, state, this); + const bool loaded = loader->load(); - if(!loaded) { - delete loader; - return nullptr; - } + callback(loader); - auto *info = new PluginInfo(loader); - m_plugins.append(info); + if(!loaded) { + delete loader; + } else { + auto *info = new PluginInfo(loader); + m_plugins.append(info); - for(MainWindow *window : qAsConst(m_windows)) { - addPluginTo(info, window); + for(MainWindow *window : qAsConst(m_windows)) { + addPluginTo(info, window); + } + } } - - return loader; } -void Browser::setup(QVector plugins) +void Browser::setup() { Configuration conf; - for(QPluginLoader *loader : plugins) { - m_plugins.append(new PluginInfo(loader)); - } - if(auto stylesheet = conf.value("browser.stylesheet")) { QFile f(stylesheet.value()); if(f.open(QIODevice::ReadOnly)) { diff --git a/src/browser.h b/src/browser.h index d4117ba..ec6bb57 100644 --- a/src/browser.h +++ b/src/browser.h @@ -40,10 +40,10 @@ public: // interface [[deprecated]] const QList> profileList() const; void loadProfiles(const QStringList &profilePaths); - - QPluginLoader *addPlugin(const QString &path = QString()); - - void setup(QVector plugins); + void loadPlugins( + const QStringList &paths, + const std::function &callback = [](const auto) {}); + void setup(); const QVector windows() const { diff --git a/src/main.cpp b/src/main.cpp index 3394a84..e04f58a 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -98,35 +98,6 @@ int main(int argc, char **argv) return -1; } - QVector plugins; - CommandHash_t pluginCommands; - - // Load plugins - [&]() { - Configuration conf; - spdlog::debug("plugins.path={}", conf.value("plugins.path").value()); - - const PluginLoader::SignatureState state( - conf.value("plugins.signature.ignored").value(), - conf.value("plugins.signature.checked").value(), - conf.value("plugins.signature.enforced").value()); - - for(const QString &path : Util::files(conf.value("plugins.path").value(), { "*.so", "*.dll" })) { - auto *loader = new PluginLoader(path, state); - const bool loaded = loader->load(); - spdlog::info("{} plugin {}", loaded ? "Loaded" : "Failed to load", qUtf8Printable(path)); - - if(loaded) { - plugins.append(loader); - auto *plugin = qobject_cast(loader->instance()); - pluginCommands.unite(plugin->commands()); - } else { - spdlog::warn("{}", qUtf8Printable(loader->errorString())); - delete loader; - } - } - }(); - // argc, argv, allowSecondary Browser app(argc, argv); // set this, otherwise the webview becomes black when using a stylesheet @@ -134,6 +105,7 @@ int main(int argc, char **argv) { Configuration conf; + if(conf.value("usebreakpad").value()) { CrashHandler::Context ctx( conf.value("path.crashdump").value(), @@ -145,6 +117,15 @@ int main(int argc, char **argv) spdlog::warn("Failed to install breakpad crash handler: {}", ctx.dumppath); } } + + // load plugins + app.loadPlugins(Util::files(conf.value("plugins.path").value(), { "*.so", "*.dll" }), + [](const auto *loader) { + if(loader->isLoaded()) + spdlog::info("Loaded plugin [{}]", qUtf8Printable(loader->fileName())); + else + spdlog::warn("Loading plugin [{}] failed: {}", qUtf8Printable(loader->fileName()), qUtf8Printable(loader->errorString())); + }); } const auto profile = []() { @@ -152,16 +133,12 @@ int main(int argc, char **argv) return c.value("profile.default").value(); }(); - app.setup(plugins); + app.setup(); QStringList urls; for(const auto &u : args::get(cmd_args)) { - if(pluginCommands.contains(QString::fromStdString(u))) { - return pluginCommands.value(QString::fromStdString(u))(); - } else { - urls.append(QString::fromStdString(u)); - } + urls.append(QString::fromStdString(u)); } if(urls.isEmpty()) urls.append(QString()); diff --git a/src/mainwindow/menubar.cpp b/src/mainwindow/menubar.cpp index eecb61a..27a8561 100644 --- a/src/mainwindow/menubar.cpp +++ b/src/mainwindow/menubar.cpp @@ -137,7 +137,7 @@ MenuBar::MenuBar(MainWindow *parent) smolbote->addSeparator(); smolbote->addAction(tr("Load Plugin"), browser, [browser]() { const QString path = QFileDialog::getOpenFileName(nullptr, tr("Select Plugin"), QDir::homePath(), tr("Plugins (*.so)")); - browser->addPlugin(path); + browser->loadPlugins(QStringList(path)); }); pluginInsertLocation = smolbote->addSeparator(); -- cgit v1.2.1