Skip to content

Commit

Permalink
Fixes some IPC issues that were causing high CPU usage (#2582)
Browse files Browse the repository at this point in the history
Task/Issue URL:
https://app.asana.com/0/1177771139624306/1207034395541856/f

## Description

Fixes some issues with IPC causing CPU usage to spike up considerably.
  • Loading branch information
diegoreymendez authored Apr 9, 2024
1 parent 0c0e490 commit a0819ab
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,9 @@ final class NetworkProtectionNavBarButtonModel: NSObject, ObservableObject {

private var cancellables = Set<AnyCancellable>()

// MARK: - NetP Icon publisher
// MARK: - VPN

private let vpnVisibility: NetworkProtectionFeatureVisibility
private let iconPublisher: NetworkProtectionIconPublisher
private var iconPublisherCancellable: AnyCancellable?

Expand Down Expand Up @@ -70,10 +71,12 @@ final class NetworkProtectionNavBarButtonModel: NSObject, ObservableObject {

init(popoverManager: NetPPopoverManager,
pinningManager: PinningManager = LocalPinningManager.shared,
vpnVisibility: NetworkProtectionFeatureVisibility = DefaultNetworkProtectionVisibility(),
statusReporter: NetworkProtectionStatusReporter,
iconProvider: IconProvider = NavigationBarIconProvider()) {

self.popoverManager = popoverManager
self.vpnVisibility = vpnVisibility
self.networkProtectionStatusReporter = statusReporter
self.iconPublisher = NetworkProtectionIconPublisher(statusReporter: networkProtectionStatusReporter, iconProvider: iconProvider)
self.pinningManager = pinningManager
Expand Down Expand Up @@ -175,8 +178,7 @@ final class NetworkProtectionNavBarButtonModel: NSObject, ObservableObject {
@MainActor
func updateVisibility() {
// The button is visible in the case where NetP has not been activated, but the user has been invited and they haven't accepted T&Cs.
let networkProtectionVisibility = DefaultNetworkProtectionVisibility()
if networkProtectionVisibility.isNetworkProtectionBetaVisible() {
if vpnVisibility.isNetworkProtectionBetaVisible() {
if NetworkProtectionWaitlist().readyToAcceptTermsAndConditions {
showButton = true
return
Expand Down
13 changes: 9 additions & 4 deletions DuckDuckGo/Preferences/Model/PreferencesSidebarModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ final class PreferencesSidebarModel: ObservableObject {
@Published private(set) var sections: [PreferencesSection] = []
@Published var selectedTabIndex: Int = 0
@Published private(set) var selectedPane: PreferencePaneIdentifier = .defaultBrowser
private let vpnVisibility: NetworkProtectionFeatureVisibility

var selectedTabContent: AnyPublisher<Tab.TabContent, Never> {
$selectedTabIndex.map { [tabSwitcherTabs] in tabSwitcherTabs[$0] }.eraseToAnyPublisher()
Expand All @@ -43,10 +44,12 @@ final class PreferencesSidebarModel: ObservableObject {
loadSections: @escaping () -> [PreferencesSection],
tabSwitcherTabs: [Tab.TabContent],
privacyConfigurationManager: PrivacyConfigurationManaging,
syncService: DDGSyncing
syncService: DDGSyncing,
vpnVisibility: NetworkProtectionFeatureVisibility = DefaultNetworkProtectionVisibility()
) {
self.loadSections = loadSections
self.tabSwitcherTabs = tabSwitcherTabs
self.vpnVisibility = vpnVisibility

resetTabSelectionIfNeeded()
refreshSections()
Expand Down Expand Up @@ -77,11 +80,12 @@ final class PreferencesSidebarModel: ObservableObject {
tabSwitcherTabs: [Tab.TabContent] = Tab.TabContent.displayableTabTypes,
privacyConfigurationManager: PrivacyConfigurationManaging = ContentBlocking.shared.privacyConfigurationManager,
syncService: DDGSyncing,
vpnVisibility: NetworkProtectionFeatureVisibility = DefaultNetworkProtectionVisibility(),
includeDuckPlayer: Bool,
userDefaults: UserDefaults = .netP
) {
let loadSections = {
let includingVPN = DefaultNetworkProtectionVisibility().isInstalled
let includingVPN = vpnVisibility.isInstalled

return PreferencesSection.defaultSections(
includingDuckPlayer: includeDuckPlayer,
Expand All @@ -93,13 +97,14 @@ final class PreferencesSidebarModel: ObservableObject {
self.init(loadSections: loadSections,
tabSwitcherTabs: tabSwitcherTabs,
privacyConfigurationManager: privacyConfigurationManager,
syncService: syncService)
syncService: syncService,
vpnVisibility: vpnVisibility)
}

// MARK: - Setup

private func setupVPNPaneVisibility() {
DefaultNetworkProtectionVisibility().onboardStatusPublisher
vpnVisibility.onboardStatusPublisher
.receive(on: DispatchQueue.main)
.sink { [weak self] _ in
guard let self else { return }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ extension PrivacyDashboardViewController {
errors: errors,
httpStatusCodes: statusCodes,
openerContext: currentTab.inferredOpenerContext,
vpnOn: currentTab.tunnelController?.isConnected ?? false,
vpnOn: currentTab.tunnelController.isConnected,
jsPerformance: webVitals,
userRefreshCount: currentTab.refreshCountSinceLoad,
didOpenReportInfo: didOpenReportInfo,
Expand Down
21 changes: 6 additions & 15 deletions DuckDuckGo/Tab/Model/Tab.swift
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ protocol NewWindowPolicyDecisionMaker {
private let internalUserDecider: InternalUserDecider?
let pinnedTabsManager: PinnedTabsManager

private(set) var tunnelController: NetworkProtectionIPCTunnelController?
private(set) var tunnelController: NetworkProtectionIPCTunnelController

private let webViewConfiguration: WKWebViewConfiguration

Expand Down Expand Up @@ -510,6 +510,10 @@ protocol NewWindowPolicyDecisionMaker {
duckPlayer: duckPlayer,
downloadManager: downloadManager))

let ipcClient = TunnelControllerIPCClient()
ipcClient.register()
tunnelController = NetworkProtectionIPCTunnelController(ipcClient: ipcClient)

super.init()
tabGetter = { [weak self] in self }
userContentController.map(userContentControllerPromise.fulfill)
Expand All @@ -529,17 +533,6 @@ protocol NewWindowPolicyDecisionMaker {
self?.onDuckDuckGoEmailSignOut(notification)
}

netPOnboardStatusCancellabel = DefaultNetworkProtectionVisibility().onboardStatusPublisher
.receive(on: DispatchQueue.main)
.sink { [weak self] onboardingStatus in
guard onboardingStatus == .completed else { return }

let ipcClient = TunnelControllerIPCClient()
ipcClient.register()

self?.tunnelController = NetworkProtectionIPCTunnelController(ipcClient: ipcClient)
}

self.audioState = webView.audioState()
addDeallocationChecks(for: webView)
}
Expand Down Expand Up @@ -1170,8 +1163,6 @@ protocol NewWindowPolicyDecisionMaker {
private var webViewCancellables = Set<AnyCancellable>()
private var emailDidSignOutCancellable: AnyCancellable?

private var netPOnboardStatusCancellabel: AnyCancellable?

private func setupWebView(shouldLoadInBackground: Bool) {
webView.navigationDelegate = navigationDelegate
webView.uiDelegate = self
Expand Down Expand Up @@ -1456,7 +1447,7 @@ extension Tab/*: NavigationResponder*/ { // to be moved to Tab+Navigation.swift
}
}

if navigation.url.isDuckDuckGoSearch, tunnelController?.isConnected == true {
if navigation.url.isDuckDuckGoSearch, tunnelController.isConnected == true {
DailyPixel.fire(pixel: .networkProtectionEnabledOnSearch, frequency: .dailyAndCount)
}
}
Expand Down
2 changes: 2 additions & 0 deletions DuckDuckGo/Waitlist/NetworkProtectionFeatureVisibility.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ protocol NetworkProtectionFeatureVisibility {
func disableForWaitlistUsers()
@discardableResult
func disableIfUserHasNoAccess() async -> Bool

var onboardStatusPublisher: AnyPublisher<OnboardingStatus, Never> { get }
}

struct DefaultNetworkProtectionVisibility: NetworkProtectionFeatureVisibility {
Expand Down
8 changes: 6 additions & 2 deletions UnitTests/Menus/MoreOptionsMenuTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,15 @@
// limitations under the License.
//

import Combine
import NetworkProtection
import NetworkProtectionUI
import XCTest

#if SUBSCRIPTION
import Subscription
#endif

import NetworkProtection

@testable import DuckDuckGo_Privacy_Browser

final class MoreOptionsMenuTests: XCTestCase {
Expand Down Expand Up @@ -165,6 +166,9 @@ final class MoreOptionsMenuTests: XCTestCase {
}

final class NetworkProtectionVisibilityMock: NetworkProtectionFeatureVisibility {
var onboardStatusPublisher: AnyPublisher<NetworkProtectionUI.OnboardingStatus, Never> {
Just(.default).eraseToAnyPublisher()
}

var isInstalled: Bool
var visible: Bool
Expand Down

0 comments on commit a0819ab

Please sign in to comment.