201 lines
6.6 KiB
C++
201 lines
6.6 KiB
C++
/***
|
|
Copyright (C) 2021 Jamie 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, int>> gOpenItems;
|
|
static AuConditionMutex gCondMutex;
|
|
static AuConditionVariable gCondVariable(AuUnsafeRaiiToShared(gCondMutex.AsPointer()));
|
|
static AuThreads::ThreadUnique_t gOpenerThread;
|
|
|
|
static void RunTasks()
|
|
{
|
|
AU_LOCK_GUARD(gCondMutex);
|
|
|
|
while (AuIsThreadRunning() || gOpenItems.size())
|
|
{
|
|
try
|
|
{
|
|
while (gOpenItems.size())
|
|
{
|
|
auto &[uri, type] = gOpenItems[0];
|
|
|
|
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 && !gRuntimeConfig.fio.bBypassInternetBlockCheckOpenFile)
|
|
{
|
|
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;
|
|
}
|
|
}
|
|
|
|
auto u16OpenURI = Locale::ConvertFromUTF8(uri);
|
|
auto pOpenType = bDirExists ? L"explore" : L"open";
|
|
bool bReveal = type == 2 &&
|
|
pILCreateFromPathW &&
|
|
pSHOpenFolderAndSelectItems &&
|
|
pILFree;
|
|
|
|
gCondMutex->Unlock();
|
|
if (bReveal)
|
|
{
|
|
if (auto pIL = pILCreateFromPathW(u16OpenURI.c_str()))
|
|
{
|
|
pSHOpenFolderAndSelectItems(pIL, 0, nullptr, 0);
|
|
pILFree(pIL);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
pShellExecuteW(nullptr,
|
|
pOpenType,
|
|
u16OpenURI.c_str(),
|
|
nullptr,
|
|
nullptr,
|
|
SW_SHOWNORMAL);
|
|
}
|
|
gCondMutex->Lock();
|
|
|
|
// Work mostly in-place and move all N-1 elements for each N, vs do all in place and clear all later (requires uninterrupted mutex lock?), vs making a copy of the work queue (requires another alloc unless AuExchanged in-place, + needs another nested indentation for a for itr loop).
|
|
// The former is pretty much the same as the latter with an AuExchange, except we use .erase(itr) moves to single step through the queue instead of batching & erasing all at once.
|
|
// We can just do ShellExecuteW outside of the lock and access a locally owned U16-translated string container, without bothering the write side mutex or mutable work container.
|
|
// A work queue buffer could solve this, but eh.
|
|
gOpenItems.erase(gOpenItems.begin());
|
|
}
|
|
|
|
gCondVariable->WaitForSignal();
|
|
}
|
|
catch (...)
|
|
{
|
|
SysPushErrorCatch("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_GLOBAL_GUARD(gCondMutex);
|
|
AuTryInsert(gOpenItems, AuMakePair(AuString(uri), 0));
|
|
gCondVariable->Signal();
|
|
}
|
|
|
|
AUKN_SYM void OpenFile(const AuROString &file)
|
|
{
|
|
auto path = AuIOFS::NormalizePathRet(file);
|
|
|
|
if (path.empty())
|
|
{
|
|
SysPushErrorMemory();
|
|
return;
|
|
}
|
|
|
|
{
|
|
AU_LOCK_GLOBAL_GUARD(gCondMutex);
|
|
AuTryInsert(gOpenItems, AuMove(AuMakePair(AuMove(path), 1)));
|
|
gCondVariable->Signal();
|
|
}
|
|
}
|
|
|
|
AUKN_SYM void RevealInDirectory(const AuROString &file)
|
|
{
|
|
auto path = AuIOFS::NormalizePathRet(file);
|
|
|
|
if (path.empty())
|
|
{
|
|
SysPushErrorMemory();
|
|
return;
|
|
}
|
|
|
|
{
|
|
AU_LOCK_GLOBAL_GUARD(gCondMutex);
|
|
AuTryInsert(gOpenItems, AuMove(AuMakePair(AuMove(path), 2)));
|
|
gCondVariable->Signal();
|
|
}
|
|
}
|
|
} |