From 6c7c3c70c037b92c46ae9ef65aab1c194c6054f5 Mon Sep 17 00:00:00 2001 From: Brad Slayter Date: Thu, 21 Mar 2024 09:21:42 -0500 Subject: [PATCH] Code cleanup --- .../View/PrivacyDashboardViewController.swift | 28 ++++++++++------- DuckDuckGo/Tab/Model/Tab.swift | 31 ++++++++++--------- 2 files changed, 34 insertions(+), 25 deletions(-) diff --git a/DuckDuckGo/PrivacyDashboard/View/PrivacyDashboardViewController.swift b/DuckDuckGo/PrivacyDashboard/View/PrivacyDashboardViewController.swift index f080c1a2c3..4a5d5113f3 100644 --- a/DuckDuckGo/PrivacyDashboard/View/PrivacyDashboardViewController.swift +++ b/DuckDuckGo/PrivacyDashboard/View/PrivacyDashboardViewController.swift @@ -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) @@ -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, @@ -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]? diff --git a/DuckDuckGo/Tab/Model/Tab.swift b/DuckDuckGo/Tab/Model/Tab.swift index dc69cef03d..ad05b12141 100644 --- a/DuckDuckGo/Tab/Model/Tab.swift +++ b/DuckDuckGo/Tab/Model/Tab.swift @@ -1367,12 +1367,22 @@ 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 @@ -1380,15 +1390,8 @@ extension Tab/*: NavigationResponder*/ { // to be moved to Tab+Navigation.swift // 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, @@ -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 } }