diff --git a/Packages/CraCore/Sources/CraCore/DialogActions.swift b/Packages/CraCore/Sources/CraCore/DialogActions.swift index 5f6cc6628..e309f1b92 100644 --- a/Packages/CraCore/Sources/CraCore/DialogActions.swift +++ b/Packages/CraCore/Sources/CraCore/DialogActions.swift @@ -46,7 +46,8 @@ public struct DialogActions: Equatable, Sendable { return index == cancelIndex ? "ESC" : "" } - public func isEnabled(at index: Int, input: String?, requiresInput: Bool) -> Bool { - index != defaultIndex || !requiresInput || !(input ?? "").trimmingCharacters(in: .whitespacesAndNewlines).isEmpty + /// `held`: the default action is off for now, as Add Extension is for the install sheet's first half second (#998). + public func isEnabled(at index: Int, input: String?, requiresInput: Bool, held: Bool = false) -> Bool { + index != defaultIndex || !held && (!requiresInput || !(input ?? "").trimmingCharacters(in: .whitespacesAndNewlines).isEmpty) } } diff --git a/Packages/CraCore/Sources/CraCore/DialogPalette.swift b/Packages/CraCore/Sources/CraCore/DialogPalette.swift index bf4ac505b..ed1ead941 100644 --- a/Packages/CraCore/Sources/CraCore/DialogPalette.swift +++ b/Packages/CraCore/Sources/CraCore/DialogPalette.swift @@ -44,6 +44,12 @@ public struct DialogPalette: Equatable, Sendable { /// The unticked box of Arc's older sheet's checkbox (the share warning) and its label. public var checkboxFill: RGB public var checkboxLabel: RGB + /// The arrow from the extension to the app on the install sheet (#998). Dark: #575B6B, measured on Arc 1.176.0's 2x + /// capture (30 Nov 2026), which is the title ink at 28 % over the card; Light, not captured, is the same mix. + public var arrow: RGB + /// The fill of Dia's "It can:" box on the install sheet (#998): Dia draws it #272727 on its #1C1C1C card (2x capture, + /// 30 Nov 2026), 5 % white over the card. On Arc's cards, 5 % of the title ink over the card. + public var listFill: RGB public var isDark: Bool public static let dark = DialogPalette(dark: true) @@ -103,5 +109,7 @@ public struct DialogPalette: Equatable, Sendable { checkboxFill = hex(0xE5E5E5) checkboxLabel = hex(0x171717) } + arrow = dark ? hex(0x575B6B) : card.mixed(with: title, 0.28) + listFill = card.mixed(with: title, 0.05) } } diff --git a/Packages/CraCore/Sources/CraCore/ExtensionInstallAsk.swift b/Packages/CraCore/Sources/CraCore/ExtensionInstallAsk.swift new file mode 100644 index 000000000..983523e69 --- /dev/null +++ b/Packages/CraCore/Sources/CraCore/ExtensionInstallAsk.swift @@ -0,0 +1,130 @@ +import Foundation + +/// What Chromium's extension install prompt asks (`extensions::InstallPromptData`), so that Cra can ask it in its own +/// sheet, Arc's dialog with Dia's "It can:" box (#998), instead of in Chromium's. The engine hands the prompt over as a +/// dictionary (`init(bridge:)`, keys in `Key`; engine/chromium/cra/extension_install_prompt.mm), and +/// `cra://debug?extensionInstallAsk=` makes one up. +public struct ExtensionInstallAsk: Equatable, Sendable { + /// Chromium's prompt types (`InstallPromptData::PromptType`) by what they ask. `other` is every type this list + /// doesn't name: the pending request, the parent approval, and any type a later Chromium adds. + public enum Kind: String, CaseIterable, Sendable { + case install, reEnable, permissions, external, remote, repair, request, other + } + + /// One line under "It can:", with the lines Chromium puts under it (the sites of "Read and change your data on a + /// number of websites"). + public struct Permission: Equatable, Sendable { + public var line: String + public var details: [String] + + public init(_ line: String, details: [String] = []) { + self.line = line + self.details = details + } + } + + public var kind: Kind + public var name: String + public var permissions: [Permission] + /// What else the prompt shows that Cra's sheet doesn't draw, by name (`Key.extras`). Any one leaves the prompt to + /// Chromium (`ExtensionInstallDialogText.takes`). + public var extras: [String] + + public init(kind: Kind = .install, name: String, permissions: [Permission] = [], extras: [String] = []) { + self.kind = kind + self.name = name + self.permissions = permissions + self.extras = extras + } + + /// The keys of the dictionary the engine sends for a prompt, one place for both sides. + public enum Key { + /// A `Kind` raw value. Chromium's INSTALL_PROMPT is "install", RE_ENABLE_PROMPT "reEnable", + /// PERMISSIONS_PROMPT "permissions", EXTERNAL_INSTALL_PROMPT "external", REMOTE_INSTALL_PROMPT "remote", + /// REPAIR_PROMPT "repair" and EXTENSION_REQUEST_PROMPT "request". Missing or unknown, the kind is `.other`. + public static let type = "type" + /// The extension's name as Chromium shows it. Required. + public static let name = "name" + /// An array with a dictionary for each line of "It can:", in Chromium's order (`InstallPromptPermissions`). + /// Missing means the extension asks for nothing. + public static let permissions = "permissions" + /// In a `permissions` entry: the line, a string. Required. + public static let line = "line" + /// In a `permissions` entry: an array of strings, the lines under it as Chromium shows them, each after a + /// "• " that the box leaves out. Optional. + public static let details = "details" + /// An array of strings: the parts of Chromium's prompt that Cra's sheet doesn't draw, such as "siteAccess" + /// (`ShouldWithheldPermissionsOnDialogAccept`, the on-click or on-all-sites choice), "storeData" + /// (`has_webstore_data`: the rating, the user count, the store link) or "parentApproval" + /// (`requires_parent_permission`). A name Cra doesn't know counts too. Missing means none. + public static let extras = "extras" + } + + /// A prompt from the engine's dictionary. Nil without a name, or when a key holds something of the wrong shape: + /// a line Cra couldn't read must not be left out of what the user agrees to, so the engine then shows Chromium's + /// own prompt, as it does for every prompt Cra doesn't take. + public init?(bridge: [String: Any]) { + guard let name = bridge[Key.name] as? String, !name.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty else { return nil } + var permissions: [Permission] = [] + if let value = bridge[Key.permissions] { + guard let entries = value as? [[String: Any]] else { return nil } + for entry in entries { + guard let line = entry[Key.line] as? String, !line.isEmpty else { return nil } + var details: [String] = [] + if let value = entry[Key.details] { + guard let lines = value as? [String] else { return nil } + details = lines.map(Self.withoutBullet).filter { !$0.isEmpty } + } + permissions.append(Permission(line, details: details)) + } + } + var extras: [String] = [] + if let value = bridge[Key.extras] { + guard let names = value as? [String] else { return nil } + extras = names + } + let kind = (bridge[Key.type] as? String).flatMap(Kind.init(rawValue:)) ?? .other + self.init(kind: kind, name: name, permissions: permissions, extras: extras) + } + + /// A line under a permission without the "• " Chromium puts before each (`InstallPromptPermissions`): the box draws + /// them under the permission's own bullet, as lines of their own. + private static func withoutBullet(_ line: String) -> String { + guard line.hasPrefix("•") else { return line } + return line.dropFirst().trimmingCharacters(in: .whitespaces) + } +} + +/// The words of Cra's install sheet (#998): Arc 1.176.0's dialog ("Add the “Snake” extension to Arc?", "This extension +/// requires no special permissions.", Add Extension), with Dia's "It can:" heading over the permissions in place of +/// Arc's line when there are any. Victor chose the two together on #998. +public struct ExtensionInstallDialogText: Equatable, Sendable { + public var title: String + /// Arc's line, when the extension asks for nothing; nil when the box lists what it can do. + public var message: String? + /// "It can:" over `lines`; nil without lines. + public var heading: String? + public var lines: [ExtensionInstallAsk.Permission] + + public static let accept = "Add Extension" + public static let noPermissions = "This extension requires no special permissions." + public static let permissionsHeading = "It can:" + /// How long Add Extension stays off after the sheet shows, so that a click or a Return meant for the page can't + /// accept it. Chromium's `g_install_delay_in_ms` (extension_install_dialog_view.cc on Chromium's main, read 30 Nov + /// 2026). Arc's own delay isn't measured. + public static let acceptDelay: TimeInterval = 0.5 + + public init(_ ask: ExtensionInstallAsk) { + title = "Add the “\(ask.name)” extension to Cra?" + lines = ask.permissions + message = lines.isEmpty ? Self.noPermissions : nil + heading = lines.isEmpty ? nil : Self.permissionsHeading + } + + /// Whether Cra asks this prompt in its own sheet: a plain install with nothing past its permissions. Turning an + /// extension back on, a request for more permissions, an install from outside or from another device, a repair, + /// a request to an administrator, and any prompt with a part this sheet doesn't draw stay Chromium's. + public static func takes(_ ask: ExtensionInstallAsk) -> Bool { + ask.kind == .install && ask.extras.isEmpty + } +} diff --git a/Packages/CraCore/Tests/CraCoreTests/DialogActionsTests.swift b/Packages/CraCore/Tests/CraCoreTests/DialogActionsTests.swift index 5d5550e17..7cbbd9553 100644 --- a/Packages/CraCore/Tests/CraCoreTests/DialogActionsTests.swift +++ b/Packages/CraCore/Tests/CraCoreTests/DialogActionsTests.swift @@ -60,6 +60,18 @@ struct DialogActionsTests { #expect(actions.isEnabled(at: 0, input: " Work ", requiresInput: true)) } + /// The install sheet's Add Extension is off for its first half second (#998); Cancel never is, and a name typed + /// doesn't turn a held default back on. + @Test(.bug("https://github.com/VictorKildahl/cra/issues/998")) + func aHeldDefaultIsOffAndNothingElseIs() { + let install = DialogActions([DialogAction("Add Extension"), .cancel]) + #expect(!install.isEnabled(at: 0, input: nil, requiresInput: false, held: true)) + #expect(install.isEnabled(at: 1, input: nil, requiresInput: false, held: true)) + #expect(install.isEnabled(at: 0, input: nil, requiresInput: false, held: false)) + let rename = DialogActions([DialogAction("Rename"), .cancel]) + #expect(!rename.isEnabled(at: 0, input: "Work", requiresInput: true, held: true)) + } + @Test func quitWithoutAskingAgainIsNotTheReturnOrEscapeAction() { let actions = DialogActions([DialogAction("Quit"), .cancel, DialogAction("Quit, and don’t ask again", role: .secondary)]) #expect(actions.displayOrder == [2, 1, 0]) diff --git a/Packages/CraCore/Tests/CraCoreTests/DialogPaletteTests.swift b/Packages/CraCore/Tests/CraCoreTests/DialogPaletteTests.swift index 31bf490e8..b72f2c18e 100644 --- a/Packages/CraCore/Tests/CraCoreTests/DialogPaletteTests.swift +++ b/Packages/CraCore/Tests/CraCoreTests/DialogPaletteTests.swift @@ -48,6 +48,19 @@ struct DialogPaletteTests { #expect(hex(light.body) == hex(light.card.mixed(with: .black, 0.7))) } + /// The install sheet's arrow is Arc's measured #575B6B, the title ink at 28 % over the dark card, and Dia's box is 5 % + /// of the ink over the card (#998). Light mode takes the same mixes over its own card. + @Test(.bug("https://github.com/VictorKildahl/cra/issues/998")) + func theInstallSheetsArrowAndBoxAreMixesOfTheInk() { + let dark = DialogPalette(dark: true), light = DialogPalette(dark: false) + #expect(hex(dark.arrow) == "#575B6B") + let darkArrow = dark.card.mixed(with: dark.title, 0.28) + #expect(abs(darkArrow.r - dark.arrow.r) * 255 <= 2 && abs(darkArrow.g - dark.arrow.g) * 255 <= 2 && abs(darkArrow.b - dark.arrow.b) * 255 <= 2) + #expect(hex(dark.listFill) == "#222739") + #expect(hex(light.arrow) == hex(light.card.mixed(with: light.title, 0.28))) + #expect(hex(light.listFill) == "#EFF0F4") + } + /// The blue default button and the red Delete look the same in both appearances, only their lips differ. @Test func defaultButtonsKeepTheirColours() { let dark = DialogPalette(dark: true), light = DialogPalette(dark: false) diff --git a/Packages/CraCore/Tests/CraCoreTests/ExtensionInstallAskTests.swift b/Packages/CraCore/Tests/CraCoreTests/ExtensionInstallAskTests.swift new file mode 100644 index 000000000..d54928983 --- /dev/null +++ b/Packages/CraCore/Tests/CraCoreTests/ExtensionInstallAskTests.swift @@ -0,0 +1,128 @@ +import CraCore +import Foundation +import Testing + +struct ExtensionInstallAskTests { + /// A prompt as the engine is to send it: Chromium's type, the name, the lines of "It can:" with the sites under one. + @Test(.bug("https://github.com/VictorKildahl/cra/issues/998")) + func theEnginesDictionaryReads() { + let ask = ExtensionInstallAsk(bridge: [ + "type": "install", + "name": "Claude", + "permissions": [ + ["line": "Read and change your data on a number of websites", "details": ["claude.ai", "", "anthropic.com"]], + ["line": "Display notifications"], + ], + ]) + #expect(ask == ExtensionInstallAsk(kind: .install, name: "Claude", permissions: [ + .init("Read and change your data on a number of websites", details: ["claude.ai", "anthropic.com"]), + .init("Display notifications"), + ])) + } + + /// The dictionary as the engine's hook builds it (engine/chromium/cra/extension_install_prompt.mm): every key there, + /// empty arrays too, and each line under a permission as Chromium shows it, after a "• " that the box leaves out. + @Test(.bug("https://github.com/VictorKildahl/cra/issues/998")) + func theEngineHooksDictionaryReads() { + let ask = ExtensionInstallAsk(bridge: [ + "type": "install", + "name": "Claude", + "permissions": [ + ["line": "Read and change your data on a number of websites", + "details": ["• claude.ai", "• anthropic.com", "•", "claude.com", "a • b"]], + ["line": "Display notifications", "details": [String]()], + ], + "extras": [String](), + ]) + #expect(ask == ExtensionInstallAsk(kind: .install, name: "Claude", permissions: [ + .init("Read and change your data on a number of websites", details: ["claude.ai", "anthropic.com", "claude.com", "a • b"]), + .init("Display notifications"), + ])) + #expect(ask.map(ExtensionInstallDialogText.takes) == true) + } + + /// Foundation's own types, as an Objective-C++ bridge hands them over. + @Test(.bug("https://github.com/VictorKildahl/cra/issues/998")) + func foundationTypesRead() { + let lines: NSArray = [["line": "Manage your downloads" as NSString] as NSDictionary] + let bridge: NSDictionary = ["type": "install" as NSString, "name": "Snake" as NSString, "permissions": lines, "extras": NSArray()] + let ask = ExtensionInstallAsk(bridge: bridge as! [String: Any]) + #expect(ask?.name == "Snake" && ask?.permissions == [.init("Manage your downloads")] && ask?.extras == []) + } + + @Test(.bug("https://github.com/VictorKildahl/cra/issues/998"), arguments: [ + ("install", ExtensionInstallAsk.Kind.install), ("reEnable", .reEnable), ("permissions", .permissions), + ("external", .external), ("remote", .remote), ("repair", .repair), ("request", .request), + // Chromium's pending request and parent approval, and a type a later Chromium adds. + ("pendingRequest", .other), ("parentApproval", .other), ("somethingNew", .other), ("", .other), + ]) + func eachTypeIsItsKind(type: String, kind: ExtensionInstallAsk.Kind) { + #expect(ExtensionInstallAsk(bridge: ["type": type, "name": "Snake"])?.kind == kind) + } + + /// No type at all is no kind Cra knows, so the prompt stays Chromium's. + @Test(.bug("https://github.com/VictorKildahl/cra/issues/998")) + func aMissingTypeIsOther() { + let ask = ExtensionInstallAsk(bridge: ["name": "Snake"]) + #expect(ask?.kind == .other && ask?.permissions == [] && ask?.extras == []) + } + + /// Without a name there is no title, and a key of the wrong shape could leave a line out of what the user agrees + /// to: either way Cra reads no prompt, and Chromium shows its own. + @Test(.bug("https://github.com/VictorKildahl/cra/issues/998"), arguments: [ + [:], + ["type": "install"], + ["type": "install", "name": ""], + ["type": "install", "name": " \n"], + ["type": "install", "name": 7], + ["type": "install", "name": "Snake", "permissions": "Display notifications"], + ["type": "install", "name": "Snake", "permissions": ["Display notifications"]], + ["type": "install", "name": "Snake", "permissions": [["details": ["claude.ai"]]]], + ["type": "install", "name": "Snake", "permissions": [["line": ""]]], + ["type": "install", "name": "Snake", "permissions": [["line": "Display notifications", "details": "claude.ai"]]], + ["type": "install", "name": "Snake", "extras": "siteAccess"], + ["type": "install", "name": "Snake", "extras": [1]], + ] as [[String: Any]]) + func aPromptCraCantReadIsNone(bridge: [String: Any]) { + #expect(ExtensionInstallAsk(bridge: bridge) == nil) + } + + /// Arc 1.176.0's dialog, word for word, named for Cra: its line when the extension asks for nothing. + @Test(.bug("https://github.com/VictorKildahl/cra/issues/998")) + func withNoPermissionsItSaysArcsLine() { + let text = ExtensionInstallDialogText(ExtensionInstallAsk(name: "Snake")) + #expect(text.title == "Add the “Snake” extension to Cra?") + #expect(text.message == "This extension requires no special permissions.") + #expect(text.heading == nil && text.lines.isEmpty) + #expect(ExtensionInstallDialogText.accept == "Add Extension") + } + + /// Dia's "It can:" over the permissions, in Chromium's order, in place of Arc's line. + @Test(.bug("https://github.com/VictorKildahl/cra/issues/998")) + func withPermissionsItListsThemUnderItCan() { + let lines: [ExtensionInstallAsk.Permission] = [.init("Read and change all your data on all websites"), + .init("Read your browsing history", details: ["On all your signed-in devices"])] + let text = ExtensionInstallDialogText(ExtensionInstallAsk(name: "Claude", permissions: lines)) + #expect(text.title == "Add the “Claude” extension to Cra?") + #expect(text.message == nil) + #expect(text.heading == "It can:") + #expect(text.lines == lines) + } + + /// Only a plain install is Cra's to ask; every other prompt, and an install with a part the sheet doesn't draw, + /// stays Chromium's. + @Test(.bug("https://github.com/VictorKildahl/cra/issues/998"), arguments: ExtensionInstallAsk.Kind.allCases) + func onlyAPlainInstallIsTaken(kind: ExtensionInstallAsk.Kind) { + let lines: [ExtensionInstallAsk.Permission] = [.init("Display notifications")] + #expect(ExtensionInstallDialogText.takes(ExtensionInstallAsk(kind: kind, name: "Snake")) == (kind == .install)) + #expect(ExtensionInstallDialogText.takes(ExtensionInstallAsk(kind: kind, name: "Snake", permissions: lines)) == (kind == .install)) + #expect(!ExtensionInstallDialogText.takes(ExtensionInstallAsk(kind: kind, name: "Snake", extras: ["siteAccess"]))) + #expect(!ExtensionInstallDialogText.takes(ExtensionInstallAsk(kind: kind, name: "Snake", extras: ["somethingNew"]))) + } + + /// Chromium keeps its Add button off for half a second after the prompt shows. + @Test(.bug("https://github.com/VictorKildahl/cra/issues/998")) + func addWaitsChromiumsHalfSecond() { + #expect(ExtensionInstallDialogText.acceptDelay == 0.5) + } +} diff --git a/Sources/Cra/App/AppDelegate.swift b/Sources/Cra/App/AppDelegate.swift index a89c01c3e..f9094f6e8 100644 --- a/Sources/Cra/App/AppDelegate.swift +++ b/Sources/Cra/App/AppDelegate.swift @@ -27,6 +27,7 @@ final class AppDelegate: NSObject, NSApplicationDelegate, NSMenuItemValidation { private var settingsController: SettingsWindowController? private var isRelaunching = false private lazy var extensionsMenu = ExtensionsMenu(app: self) + private lazy var extensionInstallPrompts = ExtensionInstallPrompts(clock: composition.clock) private lazy var spacesMenu = SpacesMenu(app: self) /// Arc's Little Arc windows: small, sidebar-less, disposable (see LittleWindowController). private(set) var littleWindows: [LittleWindowController] = [] @@ -985,6 +986,25 @@ final class AppDelegate: NSObject, NSApplicationDelegate, NSMenuItemValidation { } } + /// Chromium's extension install prompt for a page in `window` (#998), as the engine hands it over + /// (`browser:asksExtensionInstall:…` in CraCEF.h) or `cra://debug?extensionInstallAsk=` makes it up: Cra's sheet when + /// it takes the prompt, and the way to take the sheet down; nil leaves the prompt to Chromium. cra.log says which, + /// and later the answer. + func askExtensionInstall(_ prompt: [String: Any], icon: NSImage?, over window: NSWindow, acceptDelay: TimeInterval, + answer: @escaping (Bool) -> Void) -> (() -> Void)? { + let name = prompt[ExtensionInstallAsk.Key.name] as? String ?? "" + guard let ask = ExtensionInstallAsk(bridge: prompt), + let dialog = extensionInstallPrompts.offer(ask, icon: icon, over: window, acceptDelay: acceptDelay, answer: { accepted in + CraLog.note("extension install \(name) answered \(accepted)") + answer(accepted) + }) else { + CraLog.note("extension install asked \(name): left to Chromium") + return nil + } + CraLog.note("extension install asked \(name): \(extensionInstallPrompts.readout())") + return { [weak dialog] in dialog?.close() } + } + /// A page an extension opens as a tab. Its options page stays one tab, as in Chrome and /// Arc: opening it again selects the open one (GitHub #38). /// `profile` is the Chromium profile folder whose extension asked, nil for the focused page's. @@ -4344,6 +4364,26 @@ final class AppDelegate: NSObject, NSApplicationDelegate, NSMenuItemValidation { if dict["hold"] != "1" { dialog.close() } return } + // extensionInstallAsk=[&lines=|][&type=][&extras=,][&icon=]: a made-up extension + // install prompt, as the engine's dictionary, offered over the front window as the engine's is (#998); logs the + // sheet at once, or that Chromium keeps the prompt, and later its answer. extensionInstall=