2020-10-14 08:37:03 +08:00
|
|
|
#include "devtools.h"
|
2022-01-30 05:20:57 +08:00
|
|
|
#include "module.h"
|
2021-07-02 13:50:47 +08:00
|
|
|
#include <ppltasks.h>
|
|
|
|
#include <ShlObj.h>
|
2021-01-15 21:07:23 +08:00
|
|
|
#include <QWebSocket>
|
|
|
|
#include <QMetaEnum>
|
2021-06-05 22:25:46 +08:00
|
|
|
#include <QFileDialog>
|
|
|
|
#include <QMouseEvent>
|
|
|
|
|
|
|
|
extern const char* CHROME_LOCATION;
|
|
|
|
extern const char* START_DEVTOOLS;
|
|
|
|
extern const char* STOP_DEVTOOLS;
|
2022-01-31 15:48:50 +08:00
|
|
|
extern const char* HIDE_CHROME;
|
2021-06-05 22:25:46 +08:00
|
|
|
extern const char* DEVTOOLS_STATUS;
|
|
|
|
extern const char* AUTO_START;
|
|
|
|
|
|
|
|
extern const char* TRANSLATION_PROVIDER;
|
|
|
|
|
|
|
|
extern QFormLayout* display;
|
|
|
|
extern Settings settings;
|
2021-01-15 21:07:23 +08:00
|
|
|
|
|
|
|
namespace
|
|
|
|
{
|
2021-06-07 12:43:40 +08:00
|
|
|
QLabel* statusLabel;
|
2021-06-07 14:05:45 +08:00
|
|
|
AutoHandle<> process = NULL;
|
2021-01-15 21:07:23 +08:00
|
|
|
QWebSocket webSocket;
|
2021-06-07 12:43:40 +08:00
|
|
|
std::atomic<int> idCounter = 0;
|
|
|
|
Synchronized<std::unordered_map<int, concurrency::task_completion_event<JSON::Value<wchar_t>>>> mapQueue;
|
2021-06-05 22:25:46 +08:00
|
|
|
|
|
|
|
void StatusChanged(QString status)
|
|
|
|
{
|
|
|
|
QMetaObject::invokeMethod(statusLabel, std::bind(&QLabel::setText, statusLabel, status));
|
|
|
|
}
|
2021-06-07 12:43:40 +08:00
|
|
|
void Start(std::wstring chromePath, bool headless)
|
|
|
|
{
|
|
|
|
if (process) DevTools::Close();
|
|
|
|
|
|
|
|
auto args = FormatString(
|
2022-04-30 16:13:24 +08:00
|
|
|
L"%s --proxy-server=direct:// --disable-extensions --disable-gpu --no-first-run --user-data-dir=\"%s\\devtoolscache\" --remote-debugging-port=9222",
|
2021-06-07 12:43:40 +08:00
|
|
|
chromePath,
|
|
|
|
std::filesystem::current_path().wstring()
|
|
|
|
);
|
2022-04-30 16:13:24 +08:00
|
|
|
args += headless ? L" --window-size=1920,1080 --headless" : L" --window-size=850,900";
|
2021-06-07 12:43:40 +08:00
|
|
|
DWORD exitCode = 0;
|
|
|
|
STARTUPINFOW DUMMY = { sizeof(DUMMY) };
|
|
|
|
PROCESS_INFORMATION processInfo = {};
|
|
|
|
if (!CreateProcessW(NULL, args.data(), nullptr, nullptr, FALSE, 0, nullptr, nullptr, &DUMMY, &processInfo)) return StatusChanged("StartupFailed");
|
|
|
|
CloseHandle(processInfo.hThread);
|
|
|
|
process = processInfo.hProcess;
|
|
|
|
|
|
|
|
if (HttpRequest httpRequest{
|
|
|
|
L"Mozilla/5.0 Textractor",
|
|
|
|
L"127.0.0.1",
|
|
|
|
L"POST",
|
|
|
|
L"/json/list",
|
|
|
|
"",
|
|
|
|
NULL,
|
|
|
|
9222,
|
|
|
|
NULL,
|
|
|
|
WINHTTP_FLAG_ESCAPE_DISABLE
|
|
|
|
})
|
|
|
|
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()) return webSocket.open(S(*(*it)[L"webSocketDebuggerUrl"].String()));
|
|
|
|
|
|
|
|
StatusChanged("ConnectingFailed");
|
|
|
|
}
|
|
|
|
|
2021-01-15 21:07:23 +08:00
|
|
|
auto _ = ([]
|
|
|
|
{
|
|
|
|
QObject::connect(&webSocket, &QWebSocket::stateChanged,
|
2021-06-05 22:25:46 +08:00
|
|
|
[](QAbstractSocket::SocketState state) { StatusChanged(QMetaEnum::fromType<QAbstractSocket::SocketState>().valueToKey(state)); });
|
2021-01-15 21:07:23 +08:00
|
|
|
QObject::connect(&webSocket, &QWebSocket::textMessageReceived, [](QString message)
|
|
|
|
{
|
|
|
|
auto result = JSON::Parse(S(message));
|
2021-06-07 12:43:40 +08:00
|
|
|
auto mapQueue = ::mapQueue.Acquire();
|
|
|
|
if (auto id = result[L"id"].Number()) if (auto request = mapQueue->find((int)*id); request != mapQueue->end())
|
2021-01-16 00:32:23 +08:00
|
|
|
{
|
|
|
|
request->second.set(result);
|
2021-06-07 12:43:40 +08:00
|
|
|
mapQueue->erase(request);
|
2021-01-16 00:32:23 +08:00
|
|
|
}
|
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-06-07 12:43:40 +08:00
|
|
|
void Initialize()
|
2021-06-05 22:25:46 +08:00
|
|
|
{
|
|
|
|
QString chromePath = settings.value(CHROME_LOCATION).toString();
|
2022-01-30 05:20:57 +08:00
|
|
|
if (chromePath.isEmpty())
|
2020-10-14 08:37:03 +08:00
|
|
|
{
|
2022-01-30 05:20:57 +08:00
|
|
|
for (auto [_, process] : GetAllProcesses())
|
2023-04-22 04:35:19 +08:00
|
|
|
if (process && (process->find(L"\\chrome.exe") != std::string::npos || process->find(L"\\msedge.exe") != std::string::npos)) chromePath = S(process.value());
|
2022-01-30 05:20:57 +08:00
|
|
|
wchar_t programFiles[MAX_PATH + 100] = {};
|
|
|
|
for (auto folder : { CSIDL_PROGRAM_FILESX86, CSIDL_PROGRAM_FILES, CSIDL_LOCAL_APPDATA })
|
|
|
|
{
|
|
|
|
SHGetFolderPathW(NULL, folder, NULL, SHGFP_TYPE_CURRENT, programFiles);
|
|
|
|
wcscat_s(programFiles, L"/Google/Chrome/Application/chrome.exe");
|
|
|
|
if (std::filesystem::exists(programFiles)) chromePath = S(programFiles);
|
|
|
|
}
|
2021-06-05 22:25:46 +08:00
|
|
|
}
|
|
|
|
auto chromePathEdit = new QLineEdit(chromePath);
|
|
|
|
static struct : QObject
|
|
|
|
{
|
|
|
|
bool eventFilter(QObject* object, QEvent* event)
|
2020-10-18 21:16:06 +08:00
|
|
|
{
|
2021-06-05 22:25:46 +08:00
|
|
|
if (auto mouseEvent = dynamic_cast<QMouseEvent*>(event))
|
|
|
|
if (mouseEvent->button() == Qt::LeftButton)
|
|
|
|
if (QString chromePath = QFileDialog::getOpenFileName(nullptr, TRANSLATION_PROVIDER, "/", "Google Chrome (*.exe)"); !chromePath.isEmpty())
|
|
|
|
((QLineEdit*)object)->setText(chromePath);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} chromeSelector;
|
|
|
|
chromePathEdit->installEventFilter(&chromeSelector);
|
|
|
|
QObject::connect(chromePathEdit, &QLineEdit::textChanged, [chromePathEdit](QString path) { settings.setValue(CHROME_LOCATION, path); });
|
|
|
|
display->addRow(CHROME_LOCATION, chromePathEdit);
|
|
|
|
auto headlessCheck = new QCheckBox();
|
|
|
|
auto startButton = new QPushButton(START_DEVTOOLS), stopButton = new QPushButton(STOP_DEVTOOLS);
|
2022-01-31 15:48:50 +08:00
|
|
|
headlessCheck->setChecked(settings.value(HIDE_CHROME, true).toBool());
|
|
|
|
QObject::connect(headlessCheck, &QCheckBox::clicked, [](bool headless) { settings.setValue(HIDE_CHROME, headless); });
|
2021-06-07 12:43:40 +08:00
|
|
|
QObject::connect(startButton, &QPushButton::clicked, [chromePathEdit, headlessCheck] { Start(S(chromePathEdit->text()), headlessCheck->isChecked()); });
|
2021-06-05 22:25:46 +08:00
|
|
|
QObject::connect(stopButton, &QPushButton::clicked, &Close);
|
|
|
|
auto buttons = new QHBoxLayout();
|
|
|
|
buttons->addWidget(startButton);
|
|
|
|
buttons->addWidget(stopButton);
|
2022-01-31 15:48:50 +08:00
|
|
|
display->addRow(HIDE_CHROME, headlessCheck);
|
2021-06-05 22:25:46 +08:00
|
|
|
auto autoStartCheck = new QCheckBox();
|
|
|
|
autoStartCheck->setChecked(settings.value(AUTO_START, false).toBool());
|
|
|
|
QObject::connect(autoStartCheck, &QCheckBox::clicked, [](bool autoStart) { settings.setValue(AUTO_START, autoStart); });
|
|
|
|
display->addRow(AUTO_START, autoStartCheck);
|
|
|
|
display->addRow(buttons);
|
2021-06-07 12:43:40 +08:00
|
|
|
statusLabel = new QLabel("Stopped");
|
2021-06-05 22:25:46 +08:00
|
|
|
statusLabel->setFrameStyle(QFrame::Panel | QFrame::Sunken);
|
|
|
|
display->addRow(DEVTOOLS_STATUS, statusLabel);
|
|
|
|
if (autoStartCheck->isChecked()) QMetaObject::invokeMethod(startButton, &QPushButton::click, Qt::QueuedConnection);
|
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
|
|
|
webSocket.close();
|
2021-06-07 12:43:40 +08:00
|
|
|
for (const auto& [_, task] : mapQueue.Acquire().contents) task.set_exception(std::runtime_error("closed"));
|
|
|
|
mapQueue->clear();
|
|
|
|
|
|
|
|
if (process)
|
2021-06-05 22:50:00 +08:00
|
|
|
{
|
2021-06-07 12:43:40 +08:00
|
|
|
TerminateProcess(process, 0);
|
|
|
|
WaitForSingleObject(process, 1000);
|
|
|
|
for (int retry = 0; ++retry < 20; Sleep(100))
|
|
|
|
try { std::filesystem::remove_all(L"devtoolscache"); break; }
|
|
|
|
catch (std::filesystem::filesystem_error) { continue; }
|
2021-06-05 22:50:00 +08:00
|
|
|
}
|
2021-06-07 12:43:40 +08:00
|
|
|
process = NULL;
|
2021-06-05 22:25:46 +08:00
|
|
|
StatusChanged("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
|
|
|
return webSocket.state() == QAbstractSocket::ConnectedState;
|
2020-11-04 06:23:20 +08:00
|
|
|
}
|
2020-10-15 07:32:58 +08:00
|
|
|
|
2021-03-10 12:32:56 +08:00
|
|
|
JSON::Value<wchar_t> SendRequest(const char* 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-06-07 12:43:40 +08:00
|
|
|
if (!Connected()) return {};
|
|
|
|
mapQueue->try_emplace(id, response);
|
2021-06-07 14:05:45 +08:00
|
|
|
QMetaObject::invokeMethod(&webSocket, std::bind(&QWebSocket::sendTextMessage, &webSocket, S(FormatString(LR"({"id":%d,"method":"%S","params":%s})", id, method, params))));
|
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
|
|
|
}
|