-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSlBrowser.cpp
349 lines (277 loc) · 8.04 KB
/
SlBrowser.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
#include "SlBrowser.h"
#include "SlBrowserWidget.h"
#include "GrpcBrowser.h"
#include "CrashHandler.h"
#include <functional>
#include <sstream>
#include <thread>
#include <mutex>
#include <filesystem>
#include <fstream>
#include <QTimer>
#include <QPointer>
#include <QVBoxLayout>
#include <QDialog>
#include <QMainWindow>
#include <QAbstractButton>
#include <QPushButton>
#include <QApplication>
#include "browser-scheme.hpp"
#include "browser-version.h"
#include "json11/json11.hpp"
#include "cef-headers.hpp"
#include "ConsoleToggle.h"
#include <include/base/cef_callback.h>
#include <include/wrapper/cef_closure_task.h>
#include <dxgi.h>
#include <dxgi1_2.h>
#include <d3d11.h>
#include <ShlObj.h>
#include <iostream>
using namespace std;
using namespace json11;
SlBrowser::SlBrowser() {}
SlBrowser::~SlBrowser() {}
void SlBrowser::run(int argc, char *argv[])
{
QCoreApplication::addLibraryPath("../../bin/64bit/");
SpawnConsoleToggle();
if (argc < 4)
{
// todo: logging
printf("Not enough args.\n");
return;
}
m_obs64_PIDt = atoi(argv[1]);
int32_t parentListenPort = atoi(argv[2]);
int32_t myListenPort = atoi(argv[3]);
if (!GrpcBrowser::instance().startServer(myListenPort))
{
printf("sl-proxy: failed to start grpc server, GetLastError = %d\n", GetLastError());
return;
}
if (!GrpcBrowser::instance().connectToClient(parentListenPort))
{
printf("sl-proxy: failed to connected to plugin's grpc server, GetLastError = %d\n", GetLastError());
return;
}
QApplication a(argc, argv);
// Create CEF Browser
auto manager_thread = thread(&SlBrowser::browserManagerThread, this);
while (!m_cefInit)
::Sleep(1);
// Create Qt Widget
m_widget = new SlBrowserWidget{};
m_widget->setWindowTitle("Streamlabs");
m_widget->setMinimumSize(320, 240);
m_widget->resize(1280, 720);
// We have to show before creating CEF because it needs the HWND, and the HWND is not made until the QtWidget is shown at least once
m_widget->showMinimized();
CefPostTask(TID_UI, base::BindOnce(&CreateCefBrowser, 5));
std::thread(CheckForObsThread).detach();
std::thread(DebugInputThread).detach();
// Run Qt Application
int result = a.exec();
}
void SlBrowser::CreateCefBrowser(int arg)
{
auto &app = SlBrowser::instance();
CefWindowInfo window_info;
CefBrowserSettings browser_settings;
app.browserClient = new BrowserClient(false);
CefString url = SlBrowser::getDefaultUrl();
// Adjust for possible DPI
int realWidth = app.m_widget->width();
int realHeight = app.m_widget->height();
qreal scaleFactor = app.m_widget->devicePixelRatioF();
realWidth = static_cast<int>(realWidth * scaleFactor);
realHeight = static_cast<int>(realHeight * scaleFactor);
// Now set the parent of the CEF browser to the QWidget
window_info.SetAsChild((HWND)app.m_widget->winId(), CefRect(0, 0, realWidth, realHeight));
app.m_browser = CefBrowserHost::CreateBrowserSync(window_info, app.browserClient.get(), url, browser_settings, CefRefPtr<CefDictionaryValue>(), nullptr);
if (SlBrowser::instance().getSavedHiddenState())
{
SlBrowser::instance().m_widget->hide();
}
else
{
SlBrowser::instance().m_widget->showNormal();
auto bringToTop = []
{
// For the next second keep the window on top
for (int i = 0; i < 10; ++i)
{
::SetForegroundWindow((HWND)SlBrowser::instance().m_widget->winId());
::Sleep(100);
}
};
std::thread(bringToTop).detach();
}
}
void SlBrowser::browserInit()
{
std::string version;
std::string githubRevision;
std::string revision;
#ifdef SL_OBS_VERSION
version = SL_OBS_VERSION;
#else
version = "debug";
#endif
#ifdef GITHUB_REVISION
githubRevision = GITHUB_REVISION;
#else
githubRevision = "debug";
#endif
#ifdef SL_REVISION
revision = SL_REVISION;
#else
revision = "debug";
#endif
TCHAR moduleFileName[MAX_PATH]{};
GetModuleFileName(NULL, moduleFileName, MAX_PATH);
std::filesystem::path fsPath(moduleFileName);
std::string path = fsPath.remove_filename().string();
path += "sl-browser-page.exe";
CefMainArgs args;
CefSettings settings;
settings.log_severity = LOGSEVERITY_VERBOSE;
CefString(&settings.user_agent_product) = "Streamlabs";
CefString(&settings.locale) = "en-US";
CefString(&settings.accept_language_list) = "en-US,en";
// Value that will be inserted as the product portion of the default
// User-Agent string. If empty the Chromium product version will be used. If
// |userAgent| is specified this value will be ignored. Also configurable
// using the "user-agent-product" command-line switch.
std::stringstream prod_ver;
prod_ver << "Chrome/";
prod_ver << std::to_string(cef_version_info(4)) << "." << std::to_string(cef_version_info(5)) << "." << std::to_string(cef_version_info(6)) << "." << std::to_string(cef_version_info(7));
prod_ver << " SLABS/";
prod_ver << revision << "." << version << "." << githubRevision;
CefString(&settings.user_agent_product) = prod_ver.str();
settings.persist_user_preferences = 1;
char cache_path[MAX_PATH];
std::string cache_pathStdStr;
if (SUCCEEDED(SHGetFolderPathA(NULL, CSIDL_APPDATA, NULL, 0, cache_path)))
{
cache_pathStdStr = std::string(cache_path) + "\\StreamlabsOBS_CEF_Cache";
CefString(&settings.cache_path) = cache_pathStdStr;
}
CefString(&settings.browser_subprocess_path) = path.c_str();
if (!cache_pathStdStr.empty())
{
std::string logPathFile = cache_pathStdStr + "\\cef.log";
CefString(&settings.log_file) = logPathFile;
settings.log_severity = LOGSEVERITY_DEBUG;
CrashHandler::instance().addLogfilePath(logPathFile);
}
// Set the remote debugging port
settings.remote_debugging_port = 9123;
m_app = new BrowserApp();
CefExecuteProcess(args, m_app, nullptr);
CefInitialize(args, settings, m_app, nullptr);
// Register http://absolute/ scheme handler for older CEF builds which do not support file:// URLs
CefRegisterSchemeHandlerFactory("http", "absolute", new BrowserSchemeHandlerFactory());
}
void SlBrowser::browserShutdown()
{
CefClearSchemeHandlerFactories();
CefShutdown();
m_app = nullptr;
}
void SlBrowser::browserManagerThread()
{
browserInit();
m_cefInit = true;
CefRunMessageLoop();
browserShutdown();
}
/*static*/
void SlBrowser::CheckForObsThread()
{
while (true)
{
HANDLE hProcess = OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, FALSE, SlBrowser::instance().m_obs64_PIDt);
if (hProcess == NULL)
{
// If OpenProcess fails, it might mean the process does not exist
DWORD error = GetLastError();
if (error == ERROR_INVALID_PARAMETER)
{
abort();
}
}
else
{
DWORD exitCode;
if (GetExitCodeProcess(hProcess, &exitCode))
{
if (exitCode != STILL_ACTIVE)
{
// The process exists but is no longer active
CloseHandle(hProcess);
abort();
}
}
CloseHandle(hProcess);
}
// Sleep for 10ms before checking again
std::this_thread::sleep_for(std::chrono::milliseconds(10));
}
}
bool SlBrowser::getSavedHiddenState() const
{
std::wstring filePath = getCacheDir() + L"\\window_state.txt";
std::wifstream file(filePath);
if (!file.is_open())
return false;
wchar_t ch;
file >> ch;
return ch == L'1';
}
void SlBrowser::saveHiddenState(const bool b) const
{
namespace fs = std::filesystem;
std::wstring cacheDir = getCacheDir();
if (!fs::exists(cacheDir))
fs::create_directories(cacheDir);
std::wstring filePath = cacheDir + L"\\window_state.txt";
std::wofstream file(filePath, std::ios::trunc);
if (file.is_open())
file << (b ? L'1' : L'0');
}
std::wstring SlBrowser::getCacheDir() const
{
wchar_t path[MAX_PATH];
if (SUCCEEDED(SHGetFolderPathW(NULL, CSIDL_APPDATA, NULL, 0, path)))
return std::wstring(path) + L"\\StreamlabsOBS";
return L"";
}
/*static*/
// todo: remove this?
void SlBrowser::DebugInputThread()
{
::Sleep(2000);
printf("\n\n>>>>BROWSER CONSOLE:\n\n");
std::string url;
while (true)
{
if (GetConsoleWindow() == NULL)
{
::Sleep(1);
continue;
}
std::cout << "Enter URL: ";
std::cin >> url;
if (!url.empty())
{
std::cout << ";" << std::endl;
SlBrowser::instance().m_browser->GetMainFrame()->LoadURL(url);
}
else
{
std::cout << "URL cannot be empty. Please try again." << std::endl;
}
::Sleep(1000);
}
}