Skip to content

Commit 92aa39f

Browse files
committed
Add ability to stop print dialog canceler
1 parent 4b02677 commit 92aa39f

4 files changed

Lines changed: 109 additions & 10 deletions

File tree

PrintCancelerTalk/cb_query.c

Lines changed: 85 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include <Windows.h>
22
#include <stdio.h>
33
#include <stdlib.h>
4+
#include <tlhelp32.h>
45
#include "internal.h"
56

67
static 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+
}

PrintCancelerTalk/main.c

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -143,15 +143,20 @@ int main(int argc, char *argv[])
143143
*
144144
* "C edge"
145145
* ... Read and return PrintCanceler.ini.
146-
* "Q edge
147-
* ... Open PrintCanceler for edge.
146+
* "B edge
147+
* ... Start (begin) PrintCanceler for edge.
148+
* "E edge
149+
* ... Stop (end) PrintCanceler for edge.
148150
*/
149151
switch (cmd[0]) {
150152
case 'C':
151153
ret = cb_config(cmd);
152154
break;
153-
case 'Q':
154-
ret = cb_query(cmd);
155+
case 'B':
156+
ret = cb_query_start(cmd);
157+
break;
158+
case 'E':
159+
ret = cb_query_stop(cmd);
155160
break;
156161
default:
157162
fprintf(stderr, "unknown command '%s'", cmd);

webextensions/edge/background.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,16 @@ chrome.scripting.registerContentScripts([{
2323

2424
chrome.runtime.onMessage.addListener((message, _sender, _sendResponse) => {
2525
console.log('accept message.');
26-
if (message === 'cancel') {
27-
const query = new String('Q ' + BROWSER);
28-
chrome.runtime.sendNativeMessage(SERVER_NAME, query);
26+
switch (message?.type) {
27+
case 'deny': {
28+
const query = new String('B ' + BROWSER);
29+
chrome.runtime.sendNativeMessage(SERVER_NAME, query);
30+
}; break;
31+
32+
case 'allow': {
33+
const query = new String('E ' + BROWSER);
34+
chrome.runtime.sendNativeMessage(SERVER_NAME, query);
35+
}; break;
2936
}
3037
return true;
3138
});

webextensions/edge/content-isolated.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@ window.addEventListener('beforeprint', _event => {
44
const isPrintEnabled = window.isPrintEnabled;
55
window.isPrintEnabled = false;
66
if (!isPrintEnabled) {
7-
chrome.runtime.sendMessage({ type: 'cancel' }, _response => {
7+
chrome.runtime.sendMessage({ type: 'deny' }, _response => {
8+
});
9+
}
10+
else {
11+
chrome.runtime.sendMessage({ type: 'allow' }, _response => {
812
});
913
}
1014
});

0 commit comments

Comments
 (0)