From 56cc6c7fec4480f9201cca732ab218514a70a0c6 Mon Sep 17 00:00:00 2001 From: bwaresiak Date: Wed, 18 Sep 2024 13:26:13 +0200 Subject: [PATCH] Fix Network Controller leak and Dependency Provider usage (#3367) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Task/Issue URL: https://app.asana.com/0/414235014887631/1208331423616717/f Tech Design URL: CC: **Description**: Fix Network Controller leak and Dependency Provider usage so it doesn't generate multiple instances unnecessarily. **Steps to test this PR**: Run app with allocations & leaks Instruments to ensure no extra instances of Network related classes are created. Smoke test app instantiation and lifecycle around DI changes. **Definition of Done (Internal Only)**: * [ ] Does this PR satisfy our [Definition of Done](https://app.asana.com/0/1202500774821704/1207634633537039/f)? --- ###### Internal references: [Software Engineering Expectations](https://app.asana.com/0/59792373528535/199064865822552) [Technical Design Template](https://app.asana.com/0/59792373528535/184709971311943) --------- Co-authored-by: Chris Brind --- DuckDuckGo/NetworkProtectionTunnelController.swift | 7 +++++-- DuckDuckGo/StubAutofillLoginImportStateProvider.swift | 4 ++-- DuckDuckGo/TabManager.swift | 4 ++-- DuckDuckGo/TabViewControllerLongPressMenuExtension.swift | 2 +- 4 files changed, 10 insertions(+), 7 deletions(-) diff --git a/DuckDuckGo/NetworkProtectionTunnelController.swift b/DuckDuckGo/NetworkProtectionTunnelController.swift index fe6e1620ee..c7b8ebee6d 100644 --- a/DuckDuckGo/NetworkProtectionTunnelController.swift +++ b/DuckDuckGo/NetworkProtectionTunnelController.swift @@ -322,7 +322,8 @@ final class NetworkProtectionTunnelController: TunnelController, TunnelSessionPr private func subscribeToConfigurationChanges() { notificationCenter.publisher(for: .NEVPNConfigurationChange) .receive(on: DispatchQueue.main) - .sink { _ in + .sink { [weak self] _ in + guard let self = self else { return } Task { @MainActor in guard let manager = self.internalManager else { return @@ -346,7 +347,9 @@ final class NetworkProtectionTunnelController: TunnelController, TunnelSessionPr private func subscribeToStatusChanges() { notificationCenter.publisher(for: .NEVPNStatusDidChange) - .sink(receiveValue: handleStatusChange(_:)) + .sink { [weak self] value in + self?.handleStatusChange(value) + } .store(in: &cancellables) } diff --git a/DuckDuckGo/StubAutofillLoginImportStateProvider.swift b/DuckDuckGo/StubAutofillLoginImportStateProvider.swift index 326659f898..2b9c6382ab 100644 --- a/DuckDuckGo/StubAutofillLoginImportStateProvider.swift +++ b/DuckDuckGo/StubAutofillLoginImportStateProvider.swift @@ -26,10 +26,10 @@ struct StubAutofillLoginImportStateProvider: AutofillLoginImportStateProvider { var credentialsImportPromptPresentationCount: Int = 0 var isAutofillEnabled: Bool { - AppDependencyProvider().appSettings.autofillCredentialsEnabled + AppDependencyProvider.shared.appSettings.autofillCredentialsEnabled } func hasNeverPromptWebsitesFor(_ domain: String) -> Bool { - AppDependencyProvider().autofillNeverPromptWebsitesManager.hasNeverPromptWebsitesFor(domain: domain) + AppDependencyProvider.shared.autofillNeverPromptWebsitesManager.hasNeverPromptWebsitesFor(domain: domain) } } diff --git a/DuckDuckGo/TabManager.swift b/DuckDuckGo/TabManager.swift index 1d7fadf8ca..24077c9454 100644 --- a/DuckDuckGo/TabManager.swift +++ b/DuckDuckGo/TabManager.swift @@ -89,7 +89,7 @@ class TabManager { contextualOnboardingPresenter: contextualOnboardingPresenter, contextualOnboardingLogic: contextualOnboardingLogic, onboardingPixelReporter: onboardingPixelReporter, - featureFlagger: AppDependencyProvider().featureFlagger) + featureFlagger: AppDependencyProvider.shared.featureFlagger) controller.applyInheritedAttribution(inheritedAttribution) controller.attachWebView(configuration: configuration, andLoadRequest: url == nil ? nil : URLRequest.userInitiated(url!), @@ -167,7 +167,7 @@ class TabManager { contextualOnboardingPresenter: contextualOnboardingPresenter, contextualOnboardingLogic: contextualOnboardingLogic, onboardingPixelReporter: onboardingPixelReporter, - featureFlagger: AppDependencyProvider().featureFlagger) + featureFlagger: AppDependencyProvider.shared.featureFlagger) controller.attachWebView(configuration: configCopy, andLoadRequest: request, consumeCookies: !model.hasActiveTabs, diff --git a/DuckDuckGo/TabViewControllerLongPressMenuExtension.swift b/DuckDuckGo/TabViewControllerLongPressMenuExtension.swift index 938abdacd5..38770be4e7 100644 --- a/DuckDuckGo/TabViewControllerLongPressMenuExtension.swift +++ b/DuckDuckGo/TabViewControllerLongPressMenuExtension.swift @@ -111,7 +111,7 @@ extension TabViewController { contextualOnboardingPresenter: contextualOnboardingPresenter, contextualOnboardingLogic: contextualOnboardingLogic, onboardingPixelReporter: onboardingPixelReporter, - featureFlagger: AppDependencyProvider().featureFlagger) + featureFlagger: AppDependencyProvider.shared.featureFlagger) tabController.isLinkPreview = true let configuration = WKWebViewConfiguration.nonPersistent() tabController.attachWebView(configuration: configuration, andLoadRequest: URLRequest.userInitiated(url), consumeCookies: false)