AuroraRuntime/Source/Processes/AuOpen.Win32.cpp
Jamie Reece Wilson 83f34b0c47 [*] I was right. String views are [mostly] pointless (*)
03:28:55:638  17>2 of 53388 functions (<0.1%) were compiled, the rest were copied from previous compilation.
03:28:55:638  17>  0 functions were new in current compilation
03:28:55:638  17>  65 functions had inline decision re-evaluated but remain unchanged
03:28:56:749  17>Finished generating code

the header of const AuString & is the same as std::string_view therefore nothing changes. in fact, we still need to alloc strings a bunch of times for a zero terminated string. worse, <c++20 always allocs each time we want to access a hashmap with o(1) lookup, making small hashmaps kinda pointless when we always have to alloc+copy (thx std)

perhaps this will help some language binders
2024-04-19 05:58:08 +01:00

158 lines
4.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 <Source/IO/FS/FS.hpp>
#define COINIT_APARTMENTTHREADED 0x2
#define COINIT_DISABLE_OLE1DDE 0x4
namespace Aurora::Processes
{
static AuList<AuPair<AuString, bool>> gOpenItems;
static AuConditionMutex gCondMutex;
static AuConditionVariable gCondVariable(AuUnsafeRaiiToShared(gCondMutex.AsPointer()));
static AuThreads::ThreadUnique_t gOpenerThread;
static void RunTasks()
{
AU_LOCK_GUARD(gCondMutex);
while (AuIsThreadRunning())
{
try
{
for (const auto &[uri, type] : gOpenItems)
{
bool bDirExists {};
bool bFileExists {};
if (uri.empty())
{
continue;
}
if (!pShellExecuteW)
{
SysPushErrorUninitialized("Cannot open URIs yet");
continue;
}
bFileExists = AuIOFS::FileExists(uri);
if (!type)
{
bDirExists = AuIOFS::DirExists(uri);
if (!bFileExists &&
!bDirExists)
{
SysPushErrorGeneric("Exploit attempt? Attempted to open non-existent file/directory. (request: {})", uri);
continue;
}
if (bFileExists)
{
if (AuFS::IsFileBlocked(uri))
{
SysPushErrorGeneric("Exploit attempt? Attempted to open untrusted file/directory. (request: {})", uri);
continue;
}
}
}
else
{
if (bFileExists)
{
SysPushErrorGeneric("Exploit attempt? Attempted to open existing file/directory via URI ({})", uri);
continue;
}
}
pShellExecuteW(nullptr,
bDirExists ? L"explore" : L"open",
Locale::ConvertFromUTF8(uri).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()
{
if (pCoInitializeEx)
{
(void)pCoInitializeEx(nullptr, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE);
}
RunTasks();
if (pCoUninitialize)
{
pCoUninitialize();
}
}
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()
{
if (!gOpenerThread)
{
return;
}
gOpenerThread->SendExitSignal();
gCondVariable->Signal();
gOpenerThread.reset();
}
AUKN_SYM void OpenUri(const AuROString &uri)
{
AU_LOCK_GUARD(gCondMutex);
AuTryInsert(gOpenItems, AuMakePair(AuString(uri), true));
gCondVariable->Signal();
}
AUKN_SYM void OpenFile(const AuROString &file)
{
auto path = AuIOFS::NormalizePathRet(file);
if (path.empty())
{
SysPushErrorMemory();
return;
}
{
AU_LOCK_GUARD(gCondMutex);
AuTryInsert(gOpenItems, AuMove(AuMakePair(AuMove(path), false)));
gCondVariable->Signal();
}
}
}