AuroraRuntime/Source/Processes/AuOpen.Win32.cpp
2023-09-12 20:49:02 +01:00

128 lines
3.6 KiB
C++

/***
Copyright (C) 2021 J Reece Wilson (a/k/a "Reece"). All rights reserved.
File: AuOpen.Win32.cpp
Date: 2021-6-12
Author: Reece
***/
#include <Source/RuntimeInternal.hpp>
#include "AuProcesses.hpp"
#include "AuOpen.Win32.hpp"
#include <shellapi.h>
#include <tlhelp32.h>
#include <Source/IO/FS/FS.hpp>
#include "Objbase.h"
#include "shellapi.h"
namespace Aurora::Processes
{
static AuList<AuString> gOpenItems;
static AuThreadPrimitives::ConditionMutex gCondMutex;
static AuThreadPrimitives::ConditionVariable gCondVariable(AuUnsafeRaiiToShared(gCondMutex.AsPointer()));
static AuThreads::ThreadUnique_t gOpenerThread;
static void RunTasks()
{
AU_LOCK_GUARD(gCondMutex);
while (AuIsThreadRunning())
{
try
{
for (const auto &open : gOpenItems)
{
if (open.empty()) continue; // [*1]
ShellExecuteW(nullptr,
AuIOFS::DirExists(open) ? L"explore" : L"open",
Locale::ConvertFromUTF8(open).c_str(),
nullptr,
nullptr,
SW_SHOWNORMAL);
}
gOpenItems.clear();
gCondVariable->WaitForSignal();
}
catch (...)
{
Debug::PrintError();
AuLogWarn("An error occurred while dispatching a ShellExecute runner frame");
}
}
}
static void OpenerThread()
{
(void)CoInitializeEx(nullptr, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE);
RunTasks();
CoUninitialize();
}
void InitWin32Opener()
{
gOpenerThread = AuThreads::ThreadUnique(AuThreads::ThreadInfo(
AuMakeShared<AuThreads::IThreadVectorsFunctional>(AuThreads::IThreadVectorsFunctional::OnEntry_t(std::bind(OpenerThread)),
AuThreads::IThreadVectorsFunctional::OnExit_t{}),
"COM ShellExecute Runner"
));
SysAssert(gOpenerThread);
gOpenerThread->Run();
}
void DeinitWin32Opener()
{
gOpenerThread->SendExitSignal();
gCondVariable->Signal();
gOpenerThread.reset();
}
AUKN_SYM void OpenUri(const AuString &uri)
{
if (AuFS::FileExists(uri))
{
SysPushErrorGeneric("Exploit attempt? Attempted to open existing file/directory via URI ({})", uri);
return;
}
AU_LOCK_GUARD(gCondMutex);
AuTryInsert(gOpenItems, uri);
gCondVariable->Signal();
}
AUKN_SYM void OpenFile(const AuString &file)
{
auto path = AuIOFS::NormalizePathRet(file);
bool bFileExists {};
if (path.empty())
{
SysPushErrorMemory();
return;
}
if (!(bFileExists = AuFS::FileExists(path)) &&
!AuFS::DirExists(path))
{
SysPushErrorGeneric("Exploit attempt? Attempted to open non-existent file/directory. (request: {})", file);
return;
}
if (bFileExists)
{
if (!AuFS::IsFileBlocked(path))
{
SysPushErrorGeneric("Exploit attempt? Attempted to open untrusted file/directory. (request: {})", file);
return;
}
}
{
AU_LOCK_GUARD(gCondMutex);
AuTryInsert(gOpenItems, AuMove(path));
gCondVariable->Signal();
}
}
}