Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 5 additions & 8 deletions src/app/getcommandhandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,15 @@

CommandHandler::ptr getCommandHandler(const CommandWithArguments& cmd)
{
auto cmdType = cmd.first;
auto cmdCopy = CommandWithArguments {cmdType, cmd.second};

switch (cmdType) {
switch (cmd.first) {
case CommandType::GET_SIGNING_CERTIFICATE:
return std::make_unique<GetCertificate>(cmdCopy);
return std::make_unique<GetCertificate>(cmd);
case CommandType::AUTHENTICATE:
return std::make_unique<Authenticate>(cmdCopy);
return std::make_unique<Authenticate>(cmd);
case CommandType::SIGN:
return std::make_unique<Sign>(cmdCopy);
return std::make_unique<Sign>(cmd);
default:
throw std::invalid_argument("Unknown command '" + std::string(cmdType)
throw std::invalid_argument("Unknown command '" + std::string(cmd.first)
+ "' in getCommandHandler()");
}
}
11 changes: 5 additions & 6 deletions src/controller/application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ void Application::loadTranslations(const QString& lang)
void(translator->load(locale, QStringLiteral(":/translations/")));
}

CommandWithArgumentsPtr Application::parseArgs()
CommandWithArguments Application::parseArgs()
{
// On Windows Chrome, the native messaging host is also passed a command line argument with a
// handle to the calling Chrome native window: --parent-window=<decimal handle value>.
Expand Down Expand Up @@ -167,8 +167,7 @@ CommandWithArgumentsPtr Application::parseArgs()
if (command == CMDLINE_GET_SIGNING_CERTIFICATE || command == CMDLINE_AUTHENTICATE
|| command == CMDLINE_SIGN) {
// TODO: add command-specific argument validation
return std::make_unique<CommandWithArguments>(CommandType(command),
parseArgumentJson(arguments));
return {CommandType(command), parseArgumentJson(arguments)};
}
throw ArgumentError("The command has to be one of " + COMMANDS.toStdString());
}
Expand All @@ -177,18 +176,18 @@ CommandWithArgumentsPtr Application::parseArgs()
qDebug() << "Parent window handle is unused" << parser.value(parentWindow);
}
if (parser.isSet(aboutArgument)) {
return std::make_unique<CommandWithArguments>(CommandType::ABOUT, QVariantMap());
return {CommandType::ABOUT, QVariantMap()};
}
// In Linux, when the application is launched via xdg-desktop-portal from a browser that runs
// inside a Snap/Flatpack/other container, it doesn't receive the extension origin command-line
// argument. Hence, a special case is required to run the application in input-output mode
// instead of opening the about window in Linux.
#ifndef Q_OS_LINUX
if (arguments().size() == 1) {
return std::make_unique<CommandWithArguments>(CommandType::ABOUT, QVariantMap());
return {CommandType::ABOUT, QVariantMap()};
}
#endif
return nullptr;
return {CommandType::NONE, QVariantMap()};
}

void Application::registerMetatypes()
Expand Down
6 changes: 3 additions & 3 deletions src/controller/application.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,17 +47,17 @@ class Application : public QApplication

static bool isDarkTheme();
void loadTranslations(const QString& lang = {});
static CommandWithArgumentsPtr parseArgs();
static CommandWithArguments parseArgs();
static void registerMetatypes();

// Methods specific to Safari web extension's containing app,
// see class SafariApplication in src/mac/main.mm and WebEidDialog::showAboutPage().
virtual bool isSafariExtensionContainingApp() { return false; }
virtual void requestSafariExtensionState() {}
virtual void requestSafariExtensionState() { }
#ifdef Q_OS_MAC
void showAbout();
#endif
virtual void showSafariSettings() {}
virtual void showSafariSettings() { }

signals:
void safariExtensionEnabled(bool value);
Expand Down
2 changes: 1 addition & 1 deletion src/controller/commandhandler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class CommandHandler : public QObject
void verifyPinFailed(electronic_id::VerifyPinFailed::Status status, qint8 retriesLeft);

protected:
CommandHandler(const CommandWithArguments& cmd) : command(cmd) {}
CommandHandler(const CommandWithArguments& cmd) : command(cmd) { }
CommandWithArguments command;
};

Expand Down
3 changes: 0 additions & 3 deletions src/controller/commands.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@

#pragma once

#include <memory>

#include <QMetaType>
#include <QVariantMap>

Expand Down Expand Up @@ -59,4 +57,3 @@ extern const QString CMDLINE_AUTHENTICATE;
extern const QString CMDLINE_SIGN;

using CommandWithArguments = std::pair<CommandType, QVariantMap>;
using CommandWithArgumentsPtr = std::unique_ptr<CommandWithArguments>;
9 changes: 4 additions & 5 deletions src/controller/controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,7 @@ QVariantMap makeErrorObject(const QString& errorCode, const QString& errorMessag
void Controller::run() noexcept
try {
// If a command is passed, the application is in command-line mode, else in stdin/stdout mode.
const bool isInCommandLineMode = bool(command);
isInStdinMode = !isInCommandLineMode;
isInStdinMode = command.first == CommandType::NONE;

qInfo() << QCoreApplication::applicationName() << "app"
<< QCoreApplication::applicationVersion() << "running in"
Expand All @@ -74,8 +73,8 @@ try {
command = readCommandFromStdin();
}

REQUIRE_NON_NULL(command)
switch (command->first) {
switch (command.first) {
case CommandType::NONE:
case CommandType::ABOUT:
WebEidUI::showAboutPage();
return;
Expand All @@ -89,7 +88,7 @@ try {
break;
}

commandHandler = getCommandHandler(*command);
commandHandler = getCommandHandler(command);

startCommandExecution();

Expand Down
4 changes: 2 additions & 2 deletions src/controller/controller.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class Controller : public QObject
Q_OBJECT

public:
explicit Controller(CommandWithArgumentsPtr cmd) : command(std::move(cmd)) {}
explicit Controller(CommandWithArguments&& cmd) : command(std::move(cmd)) { }

const QVariantMap& result() const { return _result; }

Expand Down Expand Up @@ -77,7 +77,7 @@ class Controller : public QObject
void waitForChildThreads() noexcept;
CommandType commandType() const noexcept;

CommandWithArgumentsPtr command;
CommandWithArguments command;
CommandHandler::ptr commandHandler;
// As the Qt::WA_DeleteOnClose flag is set, the dialog is deleted automatically.
observer_ptr<WebEidUI> window = nullptr;
Expand Down
9 changes: 4 additions & 5 deletions src/controller/inputoutputmode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ void writeResponseLength(std::ostream& stream, const uint32_t responseLength)
stream.write(reinterpret_cast<const char*>(&responseLength), sizeof(responseLength));
}

CommandWithArgumentsPtr readCommandFromStdin()
CommandWithArguments readCommandFromStdin()
{

#ifdef Q_OS_WIN
Expand Down Expand Up @@ -93,14 +93,13 @@ CommandWithArgumentsPtr readCommandFromStdin()
}

const auto jsonObject = json.object();
const auto command = jsonObject["command"];
const auto arguments = jsonObject["arguments"];
const auto& command = jsonObject[QStringLiteral("command")];
const auto& arguments = jsonObject[QStringLiteral("arguments")];

if (!command.isString() || !arguments.isObject()) {
throw std::invalid_argument("readCommandFromStdin: Invalid JSON, the main object does not "
"contain a 'command' string and 'arguments' object");
}

return std::make_unique<CommandWithArguments>(CommandType(command.toString()),
arguments.toObject().toVariantMap());
return {CommandType(command.toString()), arguments.toObject().toVariantMap()};
}
2 changes: 1 addition & 1 deletion src/controller/inputoutputmode.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,6 @@

#include "pcsc-cpp/pcsc-cpp.hpp"

CommandWithArgumentsPtr readCommandFromStdin();
CommandWithArguments readCommandFromStdin();

void writeResponseLength(std::ostream& stream, const uint32_t responseLength);
18 changes: 8 additions & 10 deletions src/mac/main.mm
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,9 @@ - (void)notificationEvent:(NSNotification*)notification
try {
const auto argumentJson =
QJsonDocument::fromJson(QByteArray::fromNSData(req[@"arguments"]));
Controller controller(std::make_unique<CommandWithArguments>(
Controller controller({
CommandType(QString::fromNSString(req[@"command"])),
argumentJson.object().toVariantMap()));
argumentJson.object().toVariantMap()});
controller.run();
QEventLoop e;
QObject::connect(&controller, &Controller::quit, &e, &QEventLoop::quit);
Expand Down Expand Up @@ -187,17 +187,15 @@ int main(int argc, char* argv[])
}

try {
if (auto args = app.parseArgs()) {
NSLog(@"web-eid-safari: running with arguments");
NSLog(@"web-eid-safari: running with arguments");

Controller controller(std::move(args));
Controller controller(app.parseArgs());

QObject::connect(&controller, &Controller::quit, &app, &QApplication::quit);
// Pass control to Controller::run() when the event loop starts.
QTimer::singleShot(0, &controller, &Controller::run);
QObject::connect(&controller, &Controller::quit, &app, &QApplication::quit);
// Pass control to Controller::run() when the event loop starts.
QTimer::singleShot(0, &controller, &Controller::run);

return QApplication::exec();
}
return QApplication::exec();

} catch (const ArgumentError& error) {
// This error must go directly to cerr to avoid extra info from the logging system.
Expand Down
3 changes: 2 additions & 1 deletion tests/tests/getcommandhandler-mock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ CommandHandler::ptr getCommandHandler(const CommandWithArguments& cmd)
// The cached global unique_ptr will be nullptr after return.
if (cmd.first == CommandType::GET_SIGNING_CERTIFICATE) {
return std::move(g_cached_GetCertificate);
} else if (cmd.first == CommandType::AUTHENTICATE) {
}
if (cmd.first == CommandType::AUTHENTICATE) {
return std::move(g_cached_Authenticate);
}
throw std::logic_error("Programming error in "s + __FILE__ + ":"s + __FUNCTION__
Expand Down
14 changes: 6 additions & 8 deletions tests/tests/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -200,8 +200,7 @@ void WebEidTests::authenticate_validArgumentsResultInValidToken()
void WebEidTests::quit_exits()
{
try {
auto quitCmd = std::make_unique<CommandWithArguments>(CommandType::QUIT, QVariantMap {});
controller = std::make_unique<Controller>(std::move(quitCmd));
controller = std::make_unique<Controller>(CommandWithArguments {CommandType::QUIT, {}});

QSignalSpy quitSpy(controller.get(), &Controller::quit);
QTimer::singleShot(0, controller.get(), &Controller::run);
Expand Down Expand Up @@ -232,10 +231,10 @@ void WebEidTests::runEventLoopVerifySignalsEmitted(QSignalSpy& actionSpy, bool w
void WebEidTests::initGetCert()
{
try {
auto getCertCmd = std::make_unique<CommandWithArguments>(
CommandType::GET_SIGNING_CERTIFICATE, GET_CERTIFICATE_COMMAND_ARGUMENT);
CommandWithArguments getCertCmd {CommandType::GET_SIGNING_CERTIFICATE,
GET_CERTIFICATE_COMMAND_ARGUMENT};
// GetCertificate will make an internal copy of getCertCmd.
g_cached_GetCertificate = std::make_unique<GetCertificate>(*getCertCmd);
g_cached_GetCertificate = std::make_unique<GetCertificate>(getCertCmd);
// Controller will take ownership of getCertCmd, it will be null after this line.
controller = std::make_unique<Controller>(std::move(getCertCmd));

Expand All @@ -249,10 +248,9 @@ void WebEidTests::initGetCert()
void WebEidTests::initAuthenticate()
{
try {
auto authCmd = std::make_unique<CommandWithArguments>(CommandType::AUTHENTICATE,
AUTHENTICATE_COMMAND_ARGUMENT);
CommandWithArguments authCmd {CommandType::AUTHENTICATE, AUTHENTICATE_COMMAND_ARGUMENT};
// See comments in initGetCert() regarding authCmd lifetime.
g_cached_Authenticate = std::make_unique<Authenticate>(*authCmd);
g_cached_Authenticate = std::make_unique<Authenticate>(authCmd);
controller = std::make_unique<Controller>(std::move(authCmd));

} catch (const std::exception& e) {
Expand Down
Loading