Skip to content

Commit 73d3574

Browse files
committed
feat: implement getAuthStatus for screen
1 parent 85c1bda commit 73d3574

File tree

4 files changed

+38
-3
lines changed

4 files changed

+38
-3
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,13 @@ This native Node.js module allows you to manage an app's access to:
1818
* Microphone
1919
* Accessibility
2020
* Location
21+
* Screen Capture
2122

2223
## API
2324

2425
## `permissions.getAuthStatus(type)`
2526

26-
* `type` String - The type of system component to which you are requesting access. Can be one of `accessibility`, `calendar`, `camera`, `contacts`, `full-disk-access`, `location`, `microphone`, `photos`, or `reminders`.
27+
* `type` String - The type of system component to which you are requesting access. Can be one of `accessibility`, `calendar`, `camera`, `contacts`, `full-disk-access`, `location`, `microphone`, `photos`, `screen`, or `reminders`.
2728

2829
Returns `String` - Can be one of `not determined`, `denied`, `authorized`, or `restricted`.
2930

index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ function getAuthStatus(type) {
99
'camera',
1010
'microphone',
1111
'accessibility',
12-
'location'
12+
'location',
13+
'screen'
1314
]
1415

1516
if (!validTypes.includes(type)) {

permissions.mm

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,36 @@
100100
return auth_status;
101101
}
102102

103+
// Returns a status indicating whether the user has authorized
104+
// Screen Recording access
105+
std::string ScreenAuthStatus() {
106+
std::string auth_status = "not determined";
107+
108+
// Screen Capture is considered allowed if the name of at least one normal
109+
// or dock window running on another process is visible.
110+
if (@available(macOS 10.15, *)) {
111+
CFArrayRef window_list = CGWindowListCopyWindowInfo(kCGWindowListOptionOnScreenOnly, kCGNullWindowID);
112+
int num_windows = (int)CFArrayGetCount(window_list);
113+
int num_named_windows = 0;
114+
for (int idx = 0; idx < num_windows; idx++) {
115+
NSDictionary* info = (NSDictionary*)CFArrayGetValueAtIndex(window_list, idx);
116+
NSString* window_name = info[(id)kCGWindowName];
117+
if (window_name) {
118+
num_named_windows++;
119+
} else {
120+
break;
121+
}
122+
}
123+
124+
CFRelease(window_list);
125+
auth_status = (num_windows == num_named_windows) ? "authorized" : "denied";
126+
} else {
127+
auth_status = "authorized";
128+
}
129+
130+
return auth_status;
131+
}
132+
103133
// Returns a status indicating whether the user has authorized
104134
// Camera/Microphone access
105135
std::string MediaAuthStatus(const std::string &type) {
@@ -165,6 +195,8 @@
165195
auth_status = AXIsProcessTrusted() ? "authorized" : "denied";
166196
} else if (type == "location") {
167197
auth_status = LocationAuthStatus();
198+
} else if (type == "screen") {
199+
auth_status = ScreenAuthStatus();
168200
}
169201

170202
return Napi::Value::From(env, auth_status);

test/module.spec.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ describe('node-mac-permissions', () => {
2121
'camera',
2222
'microphone',
2323
'accessibility',
24-
'location'
24+
'location',
25+
'screen'
2526
]
2627

2728
const statuses = ['not determined', 'denied', 'authorized', 'restricted']

0 commit comments

Comments
 (0)