Skip to content

Commit 0212787

Browse files
committed
Internal – WebUI now properly closes the browser process
1 parent 252780b commit 0212787

File tree

1 file changed

+82
-65
lines changed

1 file changed

+82
-65
lines changed

src/webui.c

Lines changed: 82 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@
106106
#define WEBUI_PROFILE_NAME "WebUI" // Default browser profile name (Used only for Firefox)
107107
#define WEBUI_COOKIES_LEN (32) // Authentification cookies len
108108
#define WEBUI_COOKIES_BUF (64) // Authentification cookies buffer size
109+
#define WEBUI_NATIVE_BROWSER (99) // Internal ID used to avoid terminating the user's native browser on exit
109110

110111
#ifdef WEBUI_TLS
111112
#define WEBUI_SECURE "TLS-Encryption"
@@ -371,7 +372,6 @@ typedef struct _webui_window_t {
371372
int x;
372373
int y;
373374
bool position_set;
374-
size_t process_id;
375375
const void*(*files_handler)(const char* filename, int* length);
376376
const void*(*files_handler_window)(size_t window, const char* filename, int* length);
377377
const void* file_handler_async_response;
@@ -634,6 +634,7 @@ static int _webui_external_file_handler(_webui_window_t* win, struct mg_connecti
634634
static int _webui_interpret_file(_webui_window_t* win, struct mg_connection* client, char* index, size_t client_id);
635635
static void _webui_webview_update(_webui_window_t* win);
636636
static const char* _webui_get_local_ip(void);
637+
static size_t _webui_get_child_process_id(_webui_window_t* win);
637638
// WebView
638639
#ifdef _WIN32
639640
// Microsoft Windows
@@ -2344,7 +2345,11 @@ size_t webui_get_parent_process_id(size_t window) {
23442345
return 0;
23452346
_webui_window_t* win = _webui.wins[window];
23462347

2347-
return win->process_id;
2348+
#if defined(_WIN32)
2349+
return (size_t)GetCurrentProcessId();
2350+
#else
2351+
return (size_t)getpid();
2352+
#endif
23482353
}
23492354

23502355
const char* webui_get_mime_type(const char* file) {
@@ -2668,53 +2673,7 @@ size_t webui_get_child_process_id(size_t window) {
26682673
return 0;
26692674
_webui_window_t* win = _webui.wins[window];
26702675

2671-
// Get PID
2672-
if (_webui.is_webview) {
2673-
// WebView Mode
2674-
#if defined(_WIN32)
2675-
return (size_t)GetCurrentProcessId();
2676-
#else
2677-
return (size_t)getpid();
2678-
#endif
2679-
} else {
2680-
// Web Browser Mode
2681-
// Filter process by WebUI's web server URL in CLI
2682-
char cmd[1024] = {0};
2683-
FILE* fp;
2684-
#if defined(_WIN32)
2685-
#define popen _popen
2686-
#define pclose _pclose
2687-
snprintf(
2688-
cmd, sizeof(cmd),
2689-
"wmic process get ProcessId,CommandLine /FORMAT:CSV | findstr /c:\"%s\"",
2690-
win->url
2691-
);
2692-
fp = popen(cmd, "r");
2693-
#else
2694-
snprintf(cmd, sizeof(cmd), "pgrep -f \"%s\"", win->url);
2695-
fp = popen(cmd, "r");
2696-
#endif
2697-
if (!fp) return 0;
2698-
char line[4096] = {0};
2699-
while (fgets(line, sizeof(line), fp)) {
2700-
int pid = 0;
2701-
#if defined(_WIN32)
2702-
char* last_comma = strrchr(line, ',');
2703-
if (last_comma) {
2704-
pid = atoi(last_comma + 1);
2705-
}
2706-
#else
2707-
pid = atoi(line);
2708-
#endif
2709-
if (pid > 0) {
2710-
pclose(fp);
2711-
return pid;
2712-
}
2713-
}
2714-
pclose(fp);
2715-
}
2716-
2717-
return 0;
2676+
return _webui_get_child_process_id(win);
27182677
}
27192678

27202679
void* webui_win32_get_hwnd(size_t window) {
@@ -2739,7 +2698,7 @@ void* webui_win32_get_hwnd(size_t window) {
27392698
}
27402699
} else {
27412700
// Web Browser Window
2742-
size_t child_pid = webui_get_child_process_id(window);
2701+
size_t child_pid = _webui_get_child_process_id(win);
27432702
if (child_pid == 0) {
27442703
return NULL;
27452704
}
@@ -7452,6 +7411,58 @@ static bool _webui_browser_start(_webui_window_t* win, const char* address, size
74527411
return true;
74537412
}
74547413

7414+
static size_t _webui_get_child_process_id(_webui_window_t* win) {
7415+
if (win) {
7416+
// Get PID
7417+
if (_webui.is_webview) {
7418+
// WebView Mode
7419+
#if defined(_WIN32)
7420+
return (size_t)GetCurrentProcessId();
7421+
#else
7422+
return (size_t)getpid();
7423+
#endif
7424+
} else {
7425+
// Web Browser Mode
7426+
// Filter process by WebUI's web server URL in process CLI
7427+
char cmd[1024] = {0};
7428+
FILE* fp;
7429+
#if defined(_WIN32)
7430+
#define popen _popen
7431+
#define pclose _pclose
7432+
snprintf(
7433+
cmd, sizeof(cmd),
7434+
"wmic process get ProcessId,CommandLine /FORMAT:CSV | findstr /c:\"%s\"",
7435+
win->url
7436+
);
7437+
fp = popen(cmd, "r");
7438+
#else
7439+
snprintf(cmd, sizeof(cmd), "pgrep -f \"%s\"", win->url);
7440+
fp = popen(cmd, "r");
7441+
#endif
7442+
if (!fp) return 0;
7443+
char line[4096] = {0};
7444+
while (fgets(line, sizeof(line), fp)) {
7445+
int pid = 0;
7446+
#if defined(_WIN32)
7447+
char* last_comma = strrchr(line, ',');
7448+
if (last_comma) {
7449+
pid = atoi(last_comma + 1);
7450+
}
7451+
#else
7452+
pid = atoi(line);
7453+
#endif
7454+
if (pid > 0) {
7455+
pclose(fp);
7456+
return (size_t)pid;
7457+
}
7458+
}
7459+
pclose(fp);
7460+
}
7461+
}
7462+
7463+
return 0;
7464+
}
7465+
74557466
static bool _webui_is_process_running(const char* process_name) {
74567467

74577468
#ifdef WEBUI_LOG
@@ -8192,6 +8203,8 @@ static bool _webui_show_window(_webui_window_t* win, struct mg_connection* clien
81928203
printf("[Core]\t\t_webui_show_window() -> Native browser succeeded\n");
81938204
#endif
81948205
runBrowser = true;
8206+
// To avoid terminating the user's native browser on exit
8207+
_webui.current_browser = WEBUI_NATIVE_BROWSER;
81958208
}
81968209
else {
81978210
#ifdef WEBUI_LOG
@@ -9745,6 +9758,16 @@ static WEBUI_THREAD_SERVER_START {
97459758

97469759
_webui_mutex_is_connected(win, WEBUI_MUTEX_SET_FALSE);
97479760

9761+
// Kill Process
9762+
#ifndef WEBUI_LOG
9763+
if (!_webui.is_webview) {
9764+
if (_webui.current_browser != WEBUI_NATIVE_BROWSER) {
9765+
// Terminating the web browser window process
9766+
_webui_kill_pid(_webui_get_child_process_id(win));
9767+
}
9768+
}
9769+
#endif
9770+
97489771
// Clean WebView
97499772
if (win->webView) {
97509773
win->webView->stop = true;
@@ -9758,10 +9781,6 @@ static WEBUI_THREAD_SERVER_START {
97589781
_webui_free_mem((void*)server_port);
97599782
// _webui_client_cookies_free_all(win);
97609783

9761-
// Kill Process
9762-
// _webui_kill_pid(win->process_id);
9763-
// win->process_id = 0;
9764-
97659784
#ifdef WEBUI_LOG
97669785
printf("[Core]\t\t_webui_server_thread([%zu]) -> Server stopped.\n", win->num);
97679786
#endif
@@ -10079,20 +10098,22 @@ static bool _webui_connection_get_id(_webui_window_t* win, struct mg_connection*
1007910098
#endif
1008010099

1008110100
// Find a ws client
10082-
_webui_mutex_lock(&_webui.mutex_client);
10083-
for (size_t i = 0; i < WEBUI_MAX_IDS; i++) {
10084-
if (_webui.clients[i] == client) {
10085-
*connection_id = i;
10086-
_webui_mutex_unlock(&_webui.mutex_client);
10087-
return true;
10101+
if (client) {
10102+
_webui_mutex_lock(&_webui.mutex_client);
10103+
for (size_t i = 0; i < WEBUI_MAX_IDS; i++) {
10104+
if (_webui.clients[i] == client) {
10105+
*connection_id = i;
10106+
_webui_mutex_unlock(&_webui.mutex_client);
10107+
return true;
10108+
}
1008810109
}
10110+
_webui_mutex_unlock(&_webui.mutex_client);
1008910111
}
1009010112

1009110113
// Client not found
1009210114
#ifdef WEBUI_LOG
1009310115
printf("[Core]\t\t_webui_connection_get_id() -> Client not found\n");
1009410116
#endif
10095-
_webui_mutex_unlock(&_webui.mutex_client);
1009610117
return false;
1009710118
}
1009810119

@@ -11101,10 +11122,6 @@ static int _webui_system_win32(_webui_window_t* win, char* cmd, bool show) {
1110111122
_webui_free_mem((void*)wcmd);
1110211123
return -1;
1110311124
}
11104-
11105-
if (win) {
11106-
win->process_id = (size_t)pi.dwProcessId;
11107-
}
1110811125

1110911126
SetFocus(pi.hProcess);
1111011127
// EnumWindows(_webui_enum_windows_proc_win32, (LPARAM)(pi.dwProcessId));

0 commit comments

Comments
 (0)