Skip to content

Commit

Permalink
Remove all blocking functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
graeme committed Dec 22, 2022
1 parent 658442c commit d4e3f45
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 96 deletions.
25 changes: 6 additions & 19 deletions DuckDuckGo/Browser Tab/Model/Tab+Dialogs.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,29 +25,16 @@ struct SavePanelParameters {
}

struct JSAlertParameters {
let hasDomainShownAlert: Bool
let domain: String
let prompt: String
let defaultInputText: String?
}

struct JSAlertResult<JSCallbackArgument> {
let completionArgument: JSCallbackArgument
let shouldBlockNext: Bool
}

extension JSAlertResult where JSCallbackArgument == Void {
init(shouldBlockNext: Bool) {
self.shouldBlockNext = shouldBlockNext
completionArgument = ()
}
}

typealias OpenPanelDialogRequest = UserDialogRequest<WKOpenPanelParameters, [URL]?>
typealias SavePanelDialogRequest = UserDialogRequest<SavePanelParameters, (url: URL, fileType: UTType?)?>
typealias ConfirmDialogRequest = UserDialogRequest<JSAlertParameters, JSAlertResult<Bool>>
typealias TextInputDialogRequest = UserDialogRequest<JSAlertParameters, JSAlertResult<String?>>
typealias AlertDialogRequest = UserDialogRequest<JSAlertParameters, JSAlertResult<Void>>
typealias ConfirmDialogRequest = UserDialogRequest<JSAlertParameters, Bool>
typealias TextInputDialogRequest = UserDialogRequest<JSAlertParameters, String?>
typealias AlertDialogRequest = UserDialogRequest<JSAlertParameters, Void>
typealias BasicAuthDialogRequest = UserDialogRequest<URLProtectionSpace, (URLSession.AuthChallengeDisposition, URLCredential?)>
typealias PrintDialogRequest = UserDialogRequest<NSPrintOperation, Bool>

Expand All @@ -59,11 +46,11 @@ enum JSAlertQuery {
func cancel() {
switch self {
case .alert(let request):
return request.submit(.init(shouldBlockNext: false))
return request.submit()
case .confirm(let request):
return request.submit(.init(completionArgument: false, shouldBlockNext: false))
return request.submit(false)
case .textInput(let request):
return request.submit(.init(completionArgument: nil, shouldBlockNext: false))
return request.submit(nil)
}
}
}
Expand Down
31 changes: 9 additions & 22 deletions DuckDuckGo/Browser Tab/Model/Tab+UIDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -223,13 +223,12 @@ extension Tab: WKUIDelegate, PrintingUserScriptDelegate {
}

func webView(_ webView: WKWebView, runJavaScriptAlertPanelWithMessage message: String, initiatedByFrame frame: WKFrameInfo, completionHandler: @escaping () -> Void) {
createAlertDialog(initiatedByFrame: frame, prompt: message) { [weak self] parameters in
.alert(.init(parameters, callback: { [weak self] result in
createAlertDialog(initiatedByFrame: frame, prompt: message) { parameters in
.alert(.init(parameters, callback: { result in
switch result {
case .failure:
completionHandler()
case .success(let alertResult):
self?.updateAlertHandling(shouldBlockNext: alertResult.shouldBlockNext)
case .success:
completionHandler()
}
}))
Expand All @@ -240,14 +239,13 @@ extension Tab: WKUIDelegate, PrintingUserScriptDelegate {
runJavaScriptConfirmPanelWithMessage message: String,
initiatedByFrame frame: WKFrameInfo,
completionHandler: @escaping (Bool) -> Void) {
createAlertDialog(initiatedByFrame: frame, prompt: message) { [weak self] parameters in
.confirm(.init(parameters, callback: { [weak self] result in
createAlertDialog(initiatedByFrame: frame, prompt: message) { parameters in
.confirm(.init(parameters, callback: { result in
switch result {
case .failure:
completionHandler(false)
case .success(let alertResult):
self?.updateAlertHandling(shouldBlockNext: alertResult.shouldBlockNext)
completionHandler(alertResult.completionArgument)
completionHandler(alertResult)
}
}))
}
Expand All @@ -258,36 +256,25 @@ extension Tab: WKUIDelegate, PrintingUserScriptDelegate {
defaultText: String?,
initiatedByFrame frame: WKFrameInfo,
completionHandler: @escaping (String?) -> Void) {
createAlertDialog(initiatedByFrame: frame, prompt: prompt, defaultInputText: defaultText) { [weak self] parameters in
.textInput(.init(parameters, callback: { [weak self] result in
createAlertDialog(initiatedByFrame: frame, prompt: prompt, defaultInputText: defaultText) { parameters in
.textInput(.init(parameters, callback: { result in
switch result {
case .failure:
completionHandler(nil)
case .success(let alertResult):
self?.updateAlertHandling(shouldBlockNext: alertResult.shouldBlockNext)
completionHandler(alertResult.completionArgument)
completionHandler(alertResult)
}
}))
}
}

private func updateAlertHandling(shouldBlockNext: Bool) {
self.alertHandlingState = shouldBlockNext ? .blocked : self.alertHandlingState
}

private func createAlertDialog(initiatedByFrame frame: WKFrameInfo, prompt: String, defaultInputText: String? = nil, queryCreator: (JSAlertParameters) -> JSAlertQuery) {
let parameters = JSAlertParameters(
hasDomainShownAlert: alertHandlingState.hasShown,
domain: frame.request.url?.host ?? "",
prompt: prompt,
defaultInputText: nil
)
let alertQuery = queryCreator(parameters)
guard alertHandlingState != .blocked else {
alertQuery.cancel()
return
}
alertHandlingState = .shown
let dialog = UserDialogType.jsDialog(alertQuery)
userInteractionDialog = UserDialog(sender: .page(domain: frame.request.url?.host), dialog: dialog)
}
Expand Down
17 changes: 0 additions & 17 deletions DuckDuckGo/Browser Tab/Model/Tab.swift
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,6 @@ final class Tab: NSObject, Identifiable, ObservableObject {
historyCoordinating.commitChanges(url: oldUrl)
}
error = nil
alertHandlingState = .notShown
userInteractionDialog = nil
Task {
await reloadIfNeeded(shouldLoadInBackground: true)
Expand Down Expand Up @@ -595,7 +594,6 @@ final class Tab: NSObject, Identifiable, ObservableObject {
}

func reload() {
alertHandlingState = .notShown
userInteractionDialog = nil
currentDownload = nil
if let error = error, let failingUrl = error.failingUrl {
Expand Down Expand Up @@ -997,21 +995,6 @@ final class Tab: NSObject, Identifiable, ObservableObject {
allowlisted: isAllowlisted,
denylisted: false)
}

// MARK: - Alerts

enum AlertHandlingState {
case notShown, shown, blocked

var hasShown: Bool {
switch self {
case .notShown: return false
case .shown, .blocked: return true
}
}
}

var alertHandlingState = AlertHandlingState.notShown
}

extension Tab: UserContentControllerDelegate {
Expand Down
1 change: 0 additions & 1 deletion DuckDuckGo/Common/Localizables/UserText.swift
Original file line number Diff line number Diff line change
Expand Up @@ -669,7 +669,6 @@ struct UserText {
static let passwordManagerLockedStatus = NSLocalizedString("autofill.manager.status.locked", value: "Locked", comment: "Locked status for password manager")
static let passwordManagerUnlockedStatus = NSLocalizedString("autofill.manager.status.unlocked", value: "Unlocked", comment: "Unlocked status for password manager")

static let alertSuppressCheckboxTitle = NSLocalizedString("alert.checkbox", value: "Suppress additional alerts until you reload the page", comment: "Checkbox for suppressing additional JS alerts")
static func alertTitle(from domain: String) -> String {
let localized = NSLocalizedString("alert.title", value: "A message from %@", comment: "Title formatted with presenting domain")
return String(format: localized, domain)
Expand Down
20 changes: 5 additions & 15 deletions DuckDuckGo/JSAlert/JSAlert.storyboard
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,21 @@
<rect key="frame" x="0.0" y="0.0" width="600" height="401"/>
</customView>
<customView wantsLayer="YES" translatesAutoresizingMaskIntoConstraints="NO" id="noe-j7-ZAB" userLabel="Alert">
<rect key="frame" x="70" y="44" width="460" height="313"/>
<rect key="frame" x="70" y="68" width="460" height="265"/>
<subviews>
<stackView distribution="fill" orientation="vertical" alignment="leading" spacing="32" horizontalStackHuggingPriority="249.99998474121094" verticalStackHuggingPriority="249.99998474121094" detachesHiddenViews="YES" translatesAutoresizingMaskIntoConstraints="NO" id="BYu-y3-Dei">
<rect key="frame" x="16" y="16" width="428" height="277"/>
<rect key="frame" x="16" y="16" width="428" height="229"/>
<subviews>
<textField horizontalHuggingPriority="251" verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="5Pw-96-1If" userLabel="Title">
<rect key="frame" x="2" y="256" width="275" height="17"/>
<rect key="frame" x="2" y="208" width="275" height="17"/>
<textFieldCell key="cell" lineBreakMode="clipping" alignment="left" title="A message from [website-domain.com]:" id="GiS-GQ-kmc">
<font key="font" metaFont="systemSemibold" size="14"/>
<color key="textColor" name="labelColor" catalog="System" colorSpace="catalog"/>
<color key="backgroundColor" name="textBackgroundColor" catalog="System" colorSpace="catalog"/>
</textFieldCell>
</textField>
<scrollView borderType="none" horizontalLineScroll="10" horizontalPageScroll="10" verticalLineScroll="10" verticalPageScroll="10" hasHorizontalScroller="NO" translatesAutoresizingMaskIntoConstraints="NO" id="wPe-go-2pG">
<rect key="frame" x="0.0" y="174" width="428" height="50"/>
<rect key="frame" x="0.0" y="126" width="428" height="50"/>
<clipView key="contentView" drawsBackground="NO" id="U3X-ky-w05">
<rect key="frame" x="0.0" y="0.0" width="428" height="50"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
Expand Down Expand Up @@ -71,20 +71,13 @@
</scroller>
</scrollView>
<textField verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="c5C-SO-hh3">
<rect key="frame" x="0.0" y="112" width="428" height="30"/>
<rect key="frame" x="0.0" y="64" width="428" height="30"/>
<textFieldCell key="cell" controlSize="large" scrollable="YES" lineBreakMode="clipping" selectable="YES" editable="YES" sendsActionOnEndEditing="YES" borderStyle="bezel" bezelStyle="round" id="oZB-mG-ksB">
<font key="font" metaFont="system"/>
<color key="textColor" name="controlTextColor" catalog="System" colorSpace="catalog"/>
<color key="backgroundColor" name="textBackgroundColor" catalog="System" colorSpace="catalog"/>
</textFieldCell>
</textField>
<button verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="A57-I3-KMY">
<rect key="frame" x="2" y="63" width="65" height="18"/>
<buttonCell key="cell" type="check" title="Check" bezelStyle="regularSquare" imagePosition="left" inset="2" id="cRU-Cw-dCU">
<behavior key="behavior" changeContents="YES" doesNotDimImage="YES" lightByContents="YES"/>
<font key="font" metaFont="system"/>
</buttonCell>
</button>
<stackView distribution="fillEqually" orientation="horizontal" alignment="centerY" horizontalStackHuggingPriority="249.99998474121094" verticalStackHuggingPriority="249.99998474121094" translatesAutoresizingMaskIntoConstraints="NO" id="8jY-yL-rjW" userLabel="Push Buttons Stack View">
<rect key="frame" x="0.0" y="4" width="428" height="28"/>
<subviews>
Expand Down Expand Up @@ -132,14 +125,12 @@ Gw
<integer value="1000"/>
<integer value="1000"/>
<integer value="1000"/>
<integer value="1000"/>
</visibilityPriorities>
<customSpacing>
<real value="3.4028234663852886e+38"/>
<real value="3.4028234663852886e+38"/>
<real value="3.4028234663852886e+38"/>
<real value="3.4028234663852886e+38"/>
<real value="3.4028234663852886e+38"/>
</customSpacing>
</stackView>
</subviews>
Expand Down Expand Up @@ -168,7 +159,6 @@ Gw
<connections>
<outlet property="alertView" destination="noe-j7-ZAB" id="1SU-Pb-sKN"/>
<outlet property="backgroundView" destination="FkK-fl-t4d" id="HW4-Hb-0xy"/>
<outlet property="blockingCheckbox" destination="A57-I3-KMY" id="gou-LL-6I3"/>
<outlet property="cancelButton" destination="aC8-rX-2ac" id="GjH-oJ-Fag"/>
<outlet property="messageText" destination="I2t-vk-rnv" id="geF-um-7Mk"/>
<outlet property="okButton" destination="QXr-Qh-2zl" id="IBk-Bz-Arw"/>
Expand Down
11 changes: 1 addition & 10 deletions DuckDuckGo/JSAlert/JSAlertController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,11 @@ final class JSAlertController: NSViewController {
@IBOutlet var scrollView: NSScrollView!
@IBOutlet var messageText: NSTextView!
@IBOutlet var textField: NSTextField!
@IBOutlet var blockingCheckbox: NSButton!
@IBOutlet var okButton: NSButton!
@IBOutlet var cancelButton: NSButton!

private let viewModel: JSAlertViewModel

private var isBlockingCheckboxOn: Bool {
return blockingCheckbox.state == .on
}

static func create(_ query: JSAlertQuery) -> JSAlertController {
let instance = NSStoryboard(name: Constants.storyboardName, bundle: nil).instantiateInitialController { coder in
return JSAlertController(query: query, coder: coder)
Expand Down Expand Up @@ -73,8 +68,6 @@ final class JSAlertController: NSViewController {
print("Scroll text inset: \(messageText.textContainerInset)")
messageText.textContainer?.lineFragmentPadding = 0.0
messageText.isEditable = false

verticalStackView.setCustomSpacing(14.0, after: blockingCheckbox)
}

override func viewWillAppear() {
Expand Down Expand Up @@ -112,7 +105,7 @@ final class JSAlertController: NSViewController {

@IBAction func okAction(_ sender: NSButton) {
view.window?.endEditing(for: nil)
viewModel.confirm(text: textField.stringValue, shouldBlockAlerts: isBlockingCheckboxOn)
viewModel.confirm(text: textField.stringValue)
}

@IBAction func cancelAction(_ sender: Any?) {
Expand Down Expand Up @@ -141,11 +134,9 @@ final class JSAlertController: NSViewController {
scrollViewHeight.constant = messageText.textSize.height + 4

textField.isHidden = viewModel.isTextFieldHidden
blockingCheckbox.isHidden = viewModel.isBlockingCheckboxHidden
let scrollViewSpacing = viewModel.isTextFieldHidden ? verticalStackView.spacing : 4
verticalStackView.setCustomSpacing(scrollViewSpacing, after: scrollView)
textField.stringValue = viewModel.textFieldDefaultText
blockingCheckbox.title = viewModel.checkboxText
}

private func animateIn(_ completion: @escaping () -> Void) {
Expand Down
16 changes: 4 additions & 12 deletions DuckDuckGo/JSAlert/JSAlertViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,6 @@ final class JSAlertViewModel {
}
}

var isBlockingCheckboxHidden: Bool {
return !query.parameters.hasDomainShownAlert
}

var okButtonText: String {
UserText.ok
}
Expand All @@ -55,10 +51,6 @@ final class JSAlertViewModel {
UserText.cancel
}

var checkboxText: String {
UserText.alertSuppressCheckboxTitle
}

var titleText: String {
UserText.alertTitle(from: query.parameters.domain)
}
Expand All @@ -71,14 +63,14 @@ final class JSAlertViewModel {
query.parameters.defaultInputText ?? ""
}

func confirm(text: String, shouldBlockAlerts: Bool) {
func confirm(text: String) {
switch query {
case .alert(let request):
request.submit(.init(shouldBlockNext: shouldBlockAlerts))
request.submit()
case .confirm(let request):
request.submit(.init(completionArgument: true, shouldBlockNext: shouldBlockAlerts))
request.submit(true)
case .textInput(let request):
request.submit(.init(completionArgument: text, shouldBlockNext: shouldBlockAlerts))
request.submit(text)
}
}

Expand Down

0 comments on commit d4e3f45

Please sign in to comment.