Skip to content

Commit

Permalink
Code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
SlayterDev committed Mar 21, 2024
1 parent b4b2607 commit 6c7c3c7
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -286,8 +286,8 @@ extension PrivacyDashboardViewController: PrivacyDashboardToggleReportDelegate {
Task { @MainActor in
do {
let report = try await makeBrokenSiteReport(source: source,
didOpenReportInfo: didOpenReportInfo,
toggleReportCounter: toggleReportCounter)
didOpenReportInfo: didOpenReportInfo,
toggleReportCounter: toggleReportCounter)
try toggleProtectionsOffReporter.report(report, reportMode: .toggle)
} catch {
os_log("Failed to generate or send the broken site report: %@", type: .error, error.localizedDescription)
Expand All @@ -305,6 +305,20 @@ extension PrivacyDashboardViewController {
case failedToFetchTheCurrentURL
}

private func calculateWebVitals(performanceMetrics: PerformanceMetricsSubfeature?, privacyConfig: PrivacyConfiguration) async -> [Double]? {
var webVitalsResult: [Double]?
if privacyConfig.isEnabled(featureKey: .performanceMetrics) {
webVitalsResult = await withCheckedContinuation({ continuation in
guard let performanceMetrics else { continuation.resume(returning: nil); return }
performanceMetrics.notifyHandler { result in
continuation.resume(returning: result)
}
})
}

return webVitalsResult
}

private func makeBrokenSiteReport(category: String = "",
description: String = "",
source: BrokenSiteReport.Source,
Expand All @@ -325,15 +339,7 @@ extension PrivacyDashboardViewController {
let configuration = ContentBlocking.shared.privacyConfigurationManager.privacyConfig
let protectionsState = configuration.isFeature(.contentBlocking, enabledForDomain: currentTab.content.url?.host)

var webVitals: [Double]?
if configuration.isEnabled(featureKey: .performanceMetrics) {
webVitals = await withCheckedContinuation { continuation in
guard let performanceMetrics = currentTab.performanceMetrics else { continuation.resume(returning: nil); return }
performanceMetrics.notifyHandler { result in
continuation.resume(returning: result)
}
}
}
let webVitals = await calculateWebVitals(performanceMetrics: currentTab.performanceMetrics, privacyConfig: configuration)

var errors: [Error]?
var statusCodes: [Int]?
Expand Down
31 changes: 17 additions & 14 deletions DuckDuckGo/Tab/Model/Tab.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1367,28 +1367,31 @@ extension Tab/*: NavigationResponder*/ { // to be moved to Tab+Navigation.swift
committedURL = navigation.url
}

func resetRefreshCount() {
refreshCountSinceLoad = 0
func resetRefreshCountIfNeeded(action: NavigationAction) {
switch action.navigationType {
case .reload, .other:
break
default:
refreshCountSinceLoad = 0
}
}

func setOpenerContext(_ context: BrokenSiteReport.OpenerContext?) {
inferredOpenerContext = context
func setOpenerContextIfNeeded(action: NavigationAction) {
switch action.navigationType {
case .linkActivated, .formSubmitted:
inferredOpenerContext = .navigation
default:
break
}
}

@MainActor
func decidePolicy(for navigationAction: NavigationAction, preferences: inout NavigationPreferences) async -> NavigationActionPolicy? {
// allow local file navigations
if navigationAction.url.isFileURL { return .allow }

switch navigationAction.navigationType {
case .linkActivated, .formSubmitted:
resetRefreshCount()
setOpenerContext(.navigation)
case .reload, .other:
break
default:
resetRefreshCount()
}
resetRefreshCountIfNeeded(action: navigationAction)
setOpenerContextIfNeeded(action: navigationAction)

// when navigating to a URL with basic auth username/password, cache it and redirect to a trimmed URL
if let mainFrame = navigationAction.mainFrameTarget,
Expand Down Expand Up @@ -1459,7 +1462,7 @@ extension Tab/*: NavigationResponder*/ { // to be moved to Tab+Navigation.swift

Task { @MainActor in
if await webView.isCurrentSiteReferredFromDuckDuckGo {
setOpenerContext(.serp)
inferredOpenerContext = .serp
}
}

Expand Down

0 comments on commit 6c7c3c7

Please sign in to comment.