11#include <Windows.h>
22#include <stdio.h>
33#include <stdlib.h>
4+ #include <tlhelp32.h>
45#include "internal.h"
56
67static int get_PrintCancelerExtensionExecfile (char * buf , DWORD size )
@@ -52,7 +53,7 @@ static int start_monitoring(char *browser, char *path)
5253 return 0 ;
5354}
5455
55- int cb_query (char * cmd )
56+ int cb_query_start (char * cmd )
5657{
5758 char path [MAX_PATH ];
5859 char * browser ;
@@ -63,7 +64,7 @@ int cb_query(char *cmd)
6364 }
6465
6566 /*
66- * Q edge
67+ * B edge
6768 * ----
6869 */
6970 browser = cmd + 2 ;
@@ -77,3 +78,85 @@ int cb_query(char *cmd)
7778 talk_response ("{\"status\":\"OK\"}" );
7879 return 0 ;
7980}
81+
82+ static int stop_monitoring (char * browser , char * path )
83+ {
84+ HANDLE hSnap ;
85+ PROCESSENTRY32 pe32 ;
86+
87+ hSnap = CreateToolhelp32Snapshot (TH32CS_SNAPPROCESS , 0 );
88+ if (hSnap == INVALID_HANDLE_VALUE ) {
89+ fprintf (stderr , "CreateToolhelp32Snapshot failed (%lu)\n" , GetLastError ());
90+ return -1 ;
91+ }
92+
93+ pe32 .dwSize = sizeof (PROCESSENTRY32 );
94+
95+ if (!Process32First (hSnap , & pe32 )) {
96+ fprintf (stderr , "Process32First failed (%lu)\n" , GetLastError ());
97+ CloseHandle (hSnap );
98+ return -1 ;
99+ }
100+
101+ int stopped = 0 ;
102+
103+ do {
104+ HANDLE hProc = OpenProcess (PROCESS_QUERY_LIMITED_INFORMATION | PROCESS_TERMINATE ,
105+ FALSE,
106+ pe32 .th32ProcessID );
107+ if (!hProc ) {
108+ continue ;
109+ }
110+
111+ char procPath [MAX_PATH ];
112+ DWORD size = sizeof (procPath );
113+
114+ if (QueryFullProcessImageNameA (hProc , 0 , procPath , & size )) {
115+ if (_stricmp (procPath , path ) == 0 ) {
116+ if (TerminateProcess (hProc , 0 )) {
117+ stopped = 1 ;
118+ } else {
119+ fprintf (stderr , "TerminateProcess failed (%lu)\n" , GetLastError ());
120+ }
121+ }
122+ }
123+
124+ CloseHandle (hProc );
125+
126+ } while (Process32Next (hSnap , & pe32 ));
127+
128+ CloseHandle (hSnap );
129+
130+ if (!stopped ) {
131+ fprintf (stderr , "No matching process for '%s'\n" , path );
132+ return -1 ;
133+ }
134+
135+ return 0 ;
136+ }
137+
138+ int cb_query_stop (char * cmd )
139+ {
140+ char path [MAX_PATH ];
141+ char * browser ;
142+
143+ if (strlen (cmd ) < 3 ) {
144+ fprintf (stderr , "command too short '%s'" , cmd );
145+ return -1 ;
146+ }
147+
148+ /*
149+ * E edge
150+ * ----
151+ */
152+ browser = cmd + 2 ;
153+
154+ if (get_PrintCancelerExtensionExecfile (path , MAX_PATH ) < 0 )
155+ return -1 ;
156+
157+ if (stop_monitoring (browser , path ) < 0 )
158+ return -1 ;
159+
160+ talk_response ("{\"status\":\"OK\"}" );
161+ return 0 ;
162+ }
0 commit comments