Skip to content

armgit / cra

Public Imported from GitHub
0

The open-source Arc replacement for macOS. Spaces, pinned tabs, split view, Little Windows and Arc's shortcuts on Chromium, with your Arc sidebar imported in one go.

The permission sheet names what a page asks for, in Chrome's words, in tabs and Little Windows (#137) (#147)

Victor Kildahl895ba587fe47
diff --git a/Packages/CraCore/Sources/CraCore/PermissionPrompt.swift b/Packages/CraCore/Sources/CraCore/PermissionPrompt.swift
index 059adce..aaf3152 100644
--- a/Packages/CraCore/Sources/CraCore/PermissionPrompt.swift
+++ b/Packages/CraCore/Sources/CraCore/PermissionPrompt.swift
@@ -1,16 +1,54 @@
/// The question Cra's permission sheet asks for a page's request. `kinds` is CraCEF's bit set:
-/// 1 camera, 2 microphone, 4 location, 8 notifications, 16 anything else. Notifications ask
-/// like the others (Arc asks too; Messenger and the like need them), up to 0.1.27 they were
-/// denied without a sheet.
+/// 1 camera, 2 microphone, 4 location, 8 notifications, 16 a type CraCEF can't name, and from
+/// 1 << 5 Chrome's other types. Notifications ask like the others (Arc asks too; Messenger and
+/// the like need them), up to 0.1.27 they were denied without a sheet. Every type has Chrome's
+/// own words, so the sheet says what Allow gives (#137: Calendar's sheet said only "a browser
+/// permission").
public enum PermissionPrompt {
+ /// Chrome's wording (its *_PERMISSION_FRAGMENT strings), in the order the sheet lists them.
+ public static let requests: [(kind: UInt, wants: String)] = [
+ (8, "show notifications"),
+ (1 << 5, "see text and images copied to the clipboard"),
+ (1 << 6, "manage windows on all your displays"),
+ (1 << 7, "access other devices on your local network"),
+ (1 << 8, "access other apps and services on this device"),
+ (1 << 9, "use its cookies and site data on this page"),
+ (1 << 10, "let the sites it embeds use their cookies and site data"),
+ (1 << 11, "use the fonts on your computer"),
+ (1 << 12, "know when you're actively using this device"),
+ (1 << 13, "control and reprogram your MIDI devices"),
+ (1 << 14, "download multiple files"),
+ (1 << 15, "override some keys on your keyboard, like Esc"),
+ (1 << 16, "lock and use your mouse"),
+ (1 << 17, "play protected content"),
+ (1 << 18, "open a kind of link, like email or calendar links"),
+ (1 << 19, "create a 3D map of your surroundings and track camera position"),
+ (1 << 20, "use virtual reality devices and data"),
+ (1 << 21, "track your hands"),
+ (1 << 22, "use your motion sensors"),
+ (1 << 23, "store files on this device"),
+ (1 << 24, "use your accounts to log in to websites"),
+ (1 << 25, "scroll and zoom the contents of your shared tab"),
+ (1 << 26, "install web apps"),
+ (1 << 27, "edit files on this device"),
+ (1 << 28, "sign you in automatically"),
+ ]
+
public static func message(origin: String, kinds: UInt) -> String {
var devices: [String] = []
if kinds & 1 != 0 { devices.append("camera") }
if kinds & 2 != 0 { devices.append("microphone") }
if kinds & 4 != 0 { devices.append("location") }
- var wants = devices.isEmpty ? [] : ["use your \(devices.joined(separator: " and "))"]
- if kinds & 8 != 0 { wants.append("show notifications") }
+ var wants = devices.isEmpty ? [] : ["use your \(list(devices))"]
+ wants += requests.filter { kinds & $0.kind != 0 }.map(\.wants)
+ if kinds & 16 != 0, !wants.isEmpty { wants.append("use another browser permission") }
return wants.isEmpty ? "\(origin) is asking for a browser permission"
- : "\(origin) wants to \(wants.joined(separator: " and "))"
+ : "\(origin) wants to \(list(wants))"
+ }
+
+ /// "a", "a and b", "a, b and c".
+ private static func list(_ items: [String]) -> String {
+ guard let last = items.last, items.count > 1 else { return items.first ?? "" }
+ return items.dropLast().joined(separator: ", ") + " and " + last
}
}
diff --git a/Packages/CraCore/Tests/CraCoreTests/PermissionPromptTests.swift b/Packages/CraCore/Tests/CraCoreTests/PermissionPromptTests.swift
index 1e923cc..13c28a3 100644
--- a/Packages/CraCore/Tests/CraCoreTests/PermissionPromptTests.swift
+++ b/Packages/CraCore/Tests/CraCoreTests/PermissionPromptTests.swift
@@ -12,14 +12,35 @@ struct PermissionPromptTests {
@Test func devicesAreUsed() {
#expect(PermissionPrompt.message(origin: site, kinds: 1 | 2) == "https://www.messenger.com/ wants to use your camera and microphone")
#expect(PermissionPrompt.message(origin: site, kinds: 4) == "https://www.messenger.com/ wants to use your location")
+ #expect(PermissionPrompt.message(origin: site, kinds: 1 | 2 | 4) == "https://www.messenger.com/ wants to use your camera, microphone and location")
}
@Test func devicesAndNotificationsTogether() {
#expect(PermissionPrompt.message(origin: site, kinds: 1 | 8) == "https://www.messenger.com/ wants to use your camera and show notifications")
}
+ // #137: Calendar's sheet said "is asking for a browser permission" for a type CraCEF
+ // didn't map. Every type CraCEF maps is named.
+ @Test func chromesOtherTypesAreNamed() {
+ let calendar = "https://calendar.google.com/"
+ #expect(PermissionPrompt.message(origin: calendar, kinds: 1 << 18) == "https://calendar.google.com/ wants to open a kind of link, like email or calendar links")
+ #expect(PermissionPrompt.message(origin: calendar, kinds: 1 << 7) == "https://calendar.google.com/ wants to access other devices on your local network")
+ #expect(PermissionPrompt.message(origin: calendar, kinds: 1 << 6) == "https://calendar.google.com/ wants to manage windows on all your displays")
+ #expect(PermissionPrompt.message(origin: calendar, kinds: 1 << 5 | 8 | 2) == "https://calendar.google.com/ wants to use your microphone, show notifications and see text and images copied to the clipboard")
+ for request in PermissionPrompt.requests {
+ #expect(!PermissionPrompt.message(origin: calendar, kinds: request.kind).contains("browser permission"))
+ }
+ }
+
+ @Test func eachTypeHasItsOwnBit() {
+ let bits = PermissionPrompt.requests.map(\.kind)
+ #expect(Set(bits).count == bits.count)
+ #expect(bits.allSatisfy { $0.nonzeroBitCount == 1 && $0 & (1 | 2 | 4 | 16) == 0 })
+ }
+
@Test func anythingElseIsAGenericPermission() {
#expect(PermissionPrompt.message(origin: site, kinds: 16) == "https://www.messenger.com/ is asking for a browser permission")
#expect(PermissionPrompt.message(origin: site, kinds: 0) == "https://www.messenger.com/ is asking for a browser permission")
+ #expect(PermissionPrompt.message(origin: site, kinds: 8 | 16) == "https://www.messenger.com/ wants to show notifications and use another browser permission")
}
}
diff --git a/Sources/Cra/UI/Windows/LittleWindowController.swift b/Sources/Cra/UI/Windows/LittleWindowController.swift
index f2c1fcc..e936cda 100644
--- a/Sources/Cra/UI/Windows/LittleWindowController.swift
+++ b/Sources/Cra/UI/Windows/LittleWindowController.swift
@@ -786,14 +786,10 @@ final class LittleWindowController: NSWindowController, NSWindowDelegate, @preco
func browser(_ browser: CraBrowser, didUpdateDownload download: CraDownloadInfo) { app.main.browserManager(download: download) }
func browser(_ browser: CraBrowser, requestsPermissionForOrigin origin: String, kinds: UInt, completion: @escaping (Bool) -> Void) {
- if kinds == 8 { completion(false); return }
- var what: [String] = []
- if kinds & 1 != 0 { what.append("camera") }
- if kinds & 2 != 0 { what.append("microphone") }
- if kinds & 4 != 0 { what.append("location") }
+ // The main window's sheet. A quiet notification deny here would save a block for the
+ // site that its tabs then hit too (#137).
let alert = NSAlert()
- alert.messageText = what.isEmpty ? "\(origin) is asking for a browser permission"
- : "\(origin) wants to use your \(what.joined(separator: " and "))"
+ alert.messageText = PermissionPrompt.message(origin: origin, kinds: kinds)
alert.addButton(withTitle: "Allow")
alert.addButton(withTitle: "Block")
guard let window else { completion(false); return }
diff --git a/Sources/CraCEF/CraCEF.mm b/Sources/CraCEF/CraCEF.mm
index 4b44253..98c6a80 100644
--- a/Sources/CraCEF/CraCEF.mm
+++ b/Sources/CraCEF/CraCEF.mm
@@ -1091,6 +1091,48 @@ private:
// MARK: - Browser client
+// Chrome's permission request types as CraCEF's `kinds` bits (CraCEF.h), so the sheet can say
+// what a page asks for. A type CEF adds later, not in this table, comes through as 16.
+static const struct { uint32_t cef; NSUInteger kind; } kPermissionKinds[] = {
+ {CEF_PERMISSION_TYPE_CAMERA_STREAM | CEF_PERMISSION_TYPE_CAMERA_PAN_TILT_ZOOM, 1},
+ {CEF_PERMISSION_TYPE_MIC_STREAM, 2},
+ {CEF_PERMISSION_TYPE_GEOLOCATION, 4},
+ {CEF_PERMISSION_TYPE_NOTIFICATIONS, 8},
+ {CEF_PERMISSION_TYPE_CLIPBOARD, 1 << 5},
+ {CEF_PERMISSION_TYPE_WINDOW_MANAGEMENT, 1 << 6},
+ {CEF_PERMISSION_TYPE_LOCAL_NETWORK_ACCESS_DEPRECATED | CEF_PERMISSION_TYPE_LOCAL_NETWORK, 1 << 7},
+ {CEF_PERMISSION_TYPE_LOOPBACK_NETWORK, 1 << 8},
+ {CEF_PERMISSION_TYPE_STORAGE_ACCESS, 1 << 9},
+ {CEF_PERMISSION_TYPE_TOP_LEVEL_STORAGE_ACCESS, 1 << 10},
+ {CEF_PERMISSION_TYPE_LOCAL_FONTS, 1 << 11},
+ {CEF_PERMISSION_TYPE_IDLE_DETECTION, 1 << 12},
+ {CEF_PERMISSION_TYPE_MIDI_SYSEX, 1 << 13},
+ {CEF_PERMISSION_TYPE_MULTIPLE_DOWNLOADS, 1 << 14},
+ {CEF_PERMISSION_TYPE_KEYBOARD_LOCK, 1 << 15},
+ {CEF_PERMISSION_TYPE_POINTER_LOCK, 1 << 16},
+ {CEF_PERMISSION_TYPE_PROTECTED_MEDIA_IDENTIFIER, 1 << 17},
+ {CEF_PERMISSION_TYPE_REGISTER_PROTOCOL_HANDLER, 1 << 18},
+ {CEF_PERMISSION_TYPE_AR_SESSION, 1 << 19},
+ {CEF_PERMISSION_TYPE_VR_SESSION, 1 << 20},
+ {CEF_PERMISSION_TYPE_HAND_TRACKING, 1 << 21},
+ {CEF_PERMISSION_TYPE_SENSORS, 1 << 22},
+ {CEF_PERMISSION_TYPE_DISK_QUOTA, 1 << 23},
+ {CEF_PERMISSION_TYPE_IDENTITY_PROVIDER, 1 << 24},
+ {CEF_PERMISSION_TYPE_CAPTURED_SURFACE_CONTROL, 1 << 25},
+ {CEF_PERMISSION_TYPE_WEB_APP_INSTALLATION, 1 << 26},
+ {CEF_PERMISSION_TYPE_FILE_SYSTEM_ACCESS, 1 << 27},
+ {CEF_PERMISSION_TYPE_AMBIENT_LOGIN, 1 << 28},
+};
+
+static NSUInteger CraPermissionKinds(uint32_t requested) {
+ NSUInteger kinds = 0;
+ for (const auto& k : kPermissionKinds) {
+ if (requested & k.cef) kinds |= k.kind;
+ requested &= ~k.cef;
+ }
+ return requested ? kinds | 16 : kinds;
+}
+
class CraClient : public CefClient,
public CefLifeSpanHandler,
public CefLoadHandler,
@@ -1701,14 +1743,9 @@ public:
callback->Continue(CEF_PERMISSION_RESULT_DISMISS);
return true;
}
- NSUInteger kinds = 0;
- if (requested_permissions & (CEF_PERMISSION_TYPE_CAMERA_STREAM | CEF_PERMISSION_TYPE_CAMERA_PAN_TILT_ZOOM)) kinds |= 1;
- if (requested_permissions & CEF_PERMISSION_TYPE_MIC_STREAM) kinds |= 2;
- if (requested_permissions & CEF_PERMISSION_TYPE_GEOLOCATION) kinds |= 4;
- if (requested_permissions & CEF_PERMISSION_TYPE_NOTIFICATIONS) kinds |= 8;
- if (kinds == 0) kinds = 16;
CefRefPtr<CefPermissionPromptCallback> cb = callback;
- [o.delegate browser:o requestsPermissionForOrigin:NSStringFromCef(requesting_origin) kinds:kinds
+ [o.delegate browser:o requestsPermissionForOrigin:NSStringFromCef(requesting_origin)
+ kinds:CraPermissionKinds(requested_permissions)
completion:^(BOOL allowed) {
answering_prompt_ = true;
cb->Continue(allowed ? CEF_PERMISSION_RESULT_ACCEPT : CEF_PERMISSION_RESULT_DENY);
diff --git a/Sources/CraCEF/include/CraCEF.h b/Sources/CraCEF/include/CraCEF.h
index 42441be..15203d3 100644
--- a/Sources/CraCEF/include/CraCEF.h
+++ b/Sources/CraCEF/include/CraCEF.h
@@ -74,7 +74,9 @@ typedef NS_ENUM(NSInteger, CraDownloadState) {
- (BOOL)browser:(CraBrowser *)browser handleUnhandledKeyEvent:(NSEvent *)event;
- (void)browser:(CraBrowser *)browser didUpdateDownload:(CraDownloadInfo *)download;
/// Ask the user. `kinds` is a bitmask: 1 = camera, 2 = microphone,
-/// 4 = location, 8 = notifications, 16 = other.
+/// 4 = location, 8 = notifications, 16 = a type CraCEF has no bit for, and from
+/// 1 << 5 Chrome's other types (kPermissionKinds in CraCEF.mm, worded by CraCore's
+/// PermissionPrompt).
- (void)browser:(CraBrowser *)browser requestsPermissionForOrigin:(NSString *)origin kinds:(NSUInteger)kinds completion:(void (^)(BOOL allowed))completion;
- (void)browserDidReceiveFocus:(CraBrowser *)browser;
- (void)browserDidClose:(CraBrowser *)browser;