2020-10-14 08:37:03 +08:00
|
|
|
#include "devtools.h"
|
2021-01-15 21:07:23 +08:00
|
|
|
#include <QWebSocket>
|
|
|
|
#include <QMetaEnum>
|
|
|
|
#include <ppltasks.h>
|
|
|
|
|
|
|
|
namespace
|
|
|
|
{
|
|
|
|
std::function<void(QString)> OnStatusChanged = Swallow;
|
|
|
|
PROCESS_INFORMATION processInfo = {};
|
2021-01-16 00:32:23 +08:00
|
|
|
std::atomic<int> idCounter = 0;
|
2021-01-15 21:07:23 +08:00
|
|
|
std::mutex devToolsMutex;
|
|
|
|
QWebSocket webSocket;
|
|
|
|
std::unordered_map<int, concurrency::task_completion_event<JSON::Value<wchar_t>>> mapQueue;
|
|
|
|
auto _ = ([]
|
|
|
|
{
|
|
|
|
QObject::connect(&webSocket, &QWebSocket::stateChanged,
|
|
|
|
[](QAbstractSocket::SocketState state) { OnStatusChanged(QMetaEnum::fromType<QAbstractSocket::SocketState>().valueToKey(state)); }
|
|
|
|
);
|
|
|
|
QObject::connect(&webSocket, &QWebSocket::textMessageReceived, [](QString message)
|
|
|
|
{
|
|
|
|
auto result = JSON::Parse(S(message));
|
|
|
|
std::scoped_lock lock(devToolsMutex);
|
2021-01-16 00:32:23 +08:00
|
|
|
if (auto id = result[L"id"].Number()) if (auto request = mapQueue.find((int)*id); request != mapQueue.end())
|
|
|
|
{
|
|
|
|
request->second.set(result);
|
|
|
|
mapQueue.erase(request);
|
|
|
|
}
|
2021-01-15 21:07:23 +08:00
|
|
|
});
|
|
|
|
}(), 0);
|
2020-10-14 08:37:03 +08:00
|
|
|
}
|
|
|
|
|
2021-01-15 21:07:23 +08:00
|
|
|
namespace DevTools
|
2020-10-14 08:37:03 +08:00
|
|
|
{
|
2021-01-16 00:32:23 +08:00
|
|
|
void Start(const std::wstring& path, std::function<void(QString)> statusChanged, bool headless)
|
2020-10-14 08:37:03 +08:00
|
|
|
{
|
2021-01-15 21:07:23 +08:00
|
|
|
OnStatusChanged = statusChanged;
|
|
|
|
DWORD exitCode = 0;
|
|
|
|
auto args = FormatString(
|
2021-01-16 00:32:23 +08:00
|
|
|
L"%s --proxy-server=direct:// --disable-extensions --disable-gpu --user-data-dir=%s\\devtoolscache --remote-debugging-port=9222",
|
2021-01-15 21:07:23 +08:00
|
|
|
path,
|
2021-01-16 00:32:23 +08:00
|
|
|
std::filesystem::current_path().wstring()
|
2021-01-15 21:07:23 +08:00
|
|
|
);
|
2021-01-16 00:32:23 +08:00
|
|
|
if (headless) args += L" --headless";
|
2021-01-15 21:07:23 +08:00
|
|
|
STARTUPINFOW DUMMY = { sizeof(DUMMY) };
|
|
|
|
if ((GetExitCodeProcess(processInfo.hProcess, &exitCode) && exitCode == STILL_ACTIVE) ||
|
|
|
|
CreateProcessW(NULL, args.data(), nullptr, nullptr, FALSE, 0, nullptr, nullptr, &DUMMY, &processInfo)
|
|
|
|
)
|
2020-10-14 08:37:03 +08:00
|
|
|
{
|
2021-01-15 21:07:23 +08:00
|
|
|
if (HttpRequest httpRequest{
|
|
|
|
L"Mozilla/5.0 Textractor",
|
|
|
|
L"127.0.0.1",
|
|
|
|
L"POST",
|
|
|
|
L"/json/list",
|
|
|
|
"",
|
|
|
|
NULL,
|
2021-01-16 00:32:23 +08:00
|
|
|
9222,
|
2021-01-15 21:07:23 +08:00
|
|
|
NULL,
|
|
|
|
WINHTTP_FLAG_ESCAPE_DISABLE
|
|
|
|
})
|
2020-10-18 21:16:06 +08:00
|
|
|
{
|
2021-01-15 21:07:23 +08:00
|
|
|
if (auto list = Copy(JSON::Parse(httpRequest.response).Array())) if (auto it = std::find_if(
|
|
|
|
list->begin(),
|
|
|
|
list->end(),
|
|
|
|
[](const JSON::Value<wchar_t>& object) { return object[L"type"].String() && *object[L"type"].String() == L"page" && object[L"webSocketDebuggerUrl"].String(); }
|
|
|
|
); it != list->end())
|
2020-10-18 21:16:06 +08:00
|
|
|
{
|
2021-01-15 21:07:23 +08:00
|
|
|
std::scoped_lock lock(devToolsMutex);
|
|
|
|
webSocket.open(S(*(*it)[L"webSocketDebuggerUrl"].String()));
|
|
|
|
return;
|
|
|
|
}
|
2020-10-18 21:16:06 +08:00
|
|
|
}
|
2021-01-15 21:07:23 +08:00
|
|
|
OnStatusChanged("Failed Connection");
|
2020-10-14 08:37:03 +08:00
|
|
|
}
|
2021-01-16 00:32:23 +08:00
|
|
|
else OnStatusChanged("Failed Startup");
|
2020-10-14 08:37:03 +08:00
|
|
|
}
|
|
|
|
|
2021-01-15 21:07:23 +08:00
|
|
|
void Close()
|
2020-10-15 07:32:58 +08:00
|
|
|
{
|
2021-01-15 21:07:23 +08:00
|
|
|
std::scoped_lock lock(devToolsMutex);
|
|
|
|
for (const auto& [_, task] : mapQueue) task.set_exception(std::runtime_error("closed"));
|
|
|
|
webSocket.close();
|
|
|
|
mapQueue.clear();
|
|
|
|
DWORD exitCode = 0;
|
|
|
|
if (GetExitCodeProcess(processInfo.hProcess, &exitCode) && exitCode == STILL_ACTIVE)
|
2020-10-16 04:23:41 +08:00
|
|
|
{
|
2021-01-15 21:07:23 +08:00
|
|
|
TerminateProcess(processInfo.hProcess, 0);
|
|
|
|
WaitForSingleObject(processInfo.hProcess, 100);
|
|
|
|
CloseHandle(processInfo.hProcess);
|
|
|
|
CloseHandle(processInfo.hThread);
|
2020-10-16 04:23:41 +08:00
|
|
|
}
|
2021-01-15 21:07:23 +08:00
|
|
|
try { std::filesystem::remove_all(L"devtoolscache"); } catch (std::filesystem::filesystem_error) {}
|
|
|
|
OnStatusChanged("Stopped");
|
2020-10-15 07:32:58 +08:00
|
|
|
}
|
2021-01-15 21:07:23 +08:00
|
|
|
|
|
|
|
bool Connected()
|
2020-11-04 06:23:20 +08:00
|
|
|
{
|
2021-01-15 21:07:23 +08:00
|
|
|
std::scoped_lock lock(devToolsMutex);
|
|
|
|
return webSocket.state() == QAbstractSocket::ConnectedState;
|
2020-11-04 06:23:20 +08:00
|
|
|
}
|
2020-10-15 07:32:58 +08:00
|
|
|
|
2021-01-15 21:07:23 +08:00
|
|
|
JSON::Value<wchar_t> SendRequest(const std::wstring& method, const std::wstring& params)
|
2020-10-14 08:37:03 +08:00
|
|
|
{
|
2021-01-15 21:07:23 +08:00
|
|
|
concurrency::task_completion_event<JSON::Value<wchar_t>> response;
|
2021-01-16 00:32:23 +08:00
|
|
|
int id = idCounter += 1;
|
2021-01-15 21:07:23 +08:00
|
|
|
auto message = FormatString(LR"({"id":%d,"method":"%s","params":%s})", id, method, params);
|
2020-10-14 08:37:03 +08:00
|
|
|
{
|
2021-01-15 21:07:23 +08:00
|
|
|
std::scoped_lock lock(devToolsMutex);
|
2021-01-16 00:32:23 +08:00
|
|
|
if (webSocket.state() != QAbstractSocket::ConnectedState) return {};
|
2021-01-15 21:07:23 +08:00
|
|
|
mapQueue.try_emplace(id, response);
|
|
|
|
webSocket.sendTextMessage(S(message));
|
|
|
|
webSocket.flush();
|
2020-10-14 08:37:03 +08:00
|
|
|
}
|
2021-01-15 21:07:23 +08:00
|
|
|
try { if (auto result = create_task(response).get()[L"result"]) return result; } catch (...) {}
|
|
|
|
return {};
|
2020-10-16 04:23:41 +08:00
|
|
|
}
|
2021-01-15 21:07:23 +08:00
|
|
|
}
|