Skip to content

Commit

Permalink
Fix some issues with handling when the adapter is started up. (#2251)
Browse files Browse the repository at this point in the history
Task/Issue URL: https://app.asana.com/0/0/1206634919441353/f
iOS PR: duckduckgo/iOS#2496
BSK PR: duckduckgo/BrowserServicesKit#674

## Description

Fixes how we handle the event of the tunnel adapter started. More
broadly, this PR tries to ensure we don't "histerically" start and stop
our monitors, since that could result in a lot of noise in our metrics.
  • Loading branch information
diegoreymendez authored Feb 24, 2024
1 parent bd5375a commit 2049db6
Show file tree
Hide file tree
Showing 13 changed files with 34 additions and 22 deletions.
2 changes: 1 addition & 1 deletion DuckDuckGo.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -13538,7 +13538,7 @@
repositoryURL = "https://github.com/duckduckgo/BrowserServicesKit";
requirement = {
kind = exactVersion;
version = 110.0.0;
version = 110.0.1;
};
};
AA06B6B52672AF8100F541C5 /* XCRemoteSwiftPackageReference "Sparkle" */ = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/duckduckgo/BrowserServicesKit",
"state" : {
"revision" : "d56b90bd229288f681f0a3a6a325ef25e3ce5f3c",
"version" : "110.0.0"
"revision" : "483427db845410f10121cf2200f5a940c9bbf70b",
"version" : "110.0.1"
}
},
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,12 @@ final class NetworkProtectionTunnelController: TunnelController, TunnelSessionPr
///
private var internalManager: NETunnelProviderManager?

/// The last known VPN status.
///
/// Should not be used for checking the current status.
///
private var previousStatus: NEVPNStatus = .invalid

// MARK: - User Defaults

/* Temporarily disabled - https://app.asana.com/0/0/1205766100762904/f
Expand Down Expand Up @@ -172,12 +178,15 @@ final class NetworkProtectionTunnelController: TunnelController, TunnelSessionPr

private func handleStatusChange(_ notification: Notification) {
guard let session = (notification.object as? NETunnelProviderSession),
let manager = session.manager as? NETunnelProviderManager else {
session.status != previousStatus,
let manager = session.manager as? NETunnelProviderManager else {

return
}

Task { @MainActor in
previousStatus = session.status

switch session.status {
case .connected:
try await enableOnDemand(tunnelManager: manager)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,6 @@ final class MacPacketTunnelProvider: PacketTunnelProvider {
providerEvents: Self.packetTunnelProviderEvents,
settings: settings)

observeConnectionStatusChanges()
observeServerChanges()
observeStatusUpdateRequests()
}
Expand All @@ -247,12 +246,11 @@ final class MacPacketTunnelProvider: PacketTunnelProvider {

/// Observe connection status changes to broadcast those changes through distributed notifications.
///
private func observeConnectionStatusChanges() {
connectionStatusPublisher.sink { [weak self] status in
self?.lastStatusChangeDate = Date()
self?.broadcast(status)
}
.store(in: &cancellables)
public override func handleConnectionStatusChange(old: ConnectionStatus, new: ConnectionStatus) {
super.handleConnectionStatusChange(old: old, new: new)

lastStatusChangeDate = Date()
broadcast(new)
}

/// Observe server changes to broadcast those changes through distributed notifications.
Expand All @@ -271,8 +269,12 @@ final class MacPacketTunnelProvider: PacketTunnelProvider {
///
private func observeStatusUpdateRequests() {
notificationCenter.publisher(for: .requestStatusUpdate).sink { [weak self] _ in
self?.broadcastConnectionStatus()
self?.broadcastLastSelectedServerInfo()
guard let self else { return }

Task { @MainActor in
self.broadcastConnectionStatus()
self.broadcastLastSelectedServerInfo()
}
}
.store(in: &cancellables)
}
Expand All @@ -281,6 +283,7 @@ final class MacPacketTunnelProvider: PacketTunnelProvider {

/// Broadcasts the current connection status.
///
@MainActor
private func broadcastConnectionStatus() {
broadcast(connectionStatus)
}
Expand Down
2 changes: 1 addition & 1 deletion LocalPackages/DataBrokerProtection/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ let package = Package(
targets: ["DataBrokerProtection"])
],
dependencies: [
.package(url: "https://github.com/duckduckgo/BrowserServicesKit", exact: "110.0.0"),
.package(url: "https://github.com/duckduckgo/BrowserServicesKit", exact: "110.0.1"),
.package(path: "../PixelKit"),
.package(path: "../SwiftUIExtensions"),
.package(path: "../XPCHelper")
Expand Down
2 changes: 1 addition & 1 deletion LocalPackages/LoginItems/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ let package = Package(
),
],
dependencies: [
.package(url: "https://github.com/duckduckgo/BrowserServicesKit", exact: "110.0.0"),
.package(url: "https://github.com/duckduckgo/BrowserServicesKit", exact: "110.0.1"),
],
targets: [
.target(
Expand Down
2 changes: 1 addition & 1 deletion LocalPackages/NetworkProtectionMac/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ let package = Package(
.library(name: "NetworkProtectionUI", targets: ["NetworkProtectionUI"])
],
dependencies: [
.package(url: "https://github.com/duckduckgo/BrowserServicesKit", exact: "110.0.0"),
.package(url: "https://github.com/duckduckgo/BrowserServicesKit", exact: "110.0.1"),
.package(path: "../XPCHelper"),
.package(path: "../SwiftUIExtensions"),
.package(path: "../LoginItems")
Expand Down
2 changes: 1 addition & 1 deletion LocalPackages/PixelKit/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ let package = Package(
)
],
dependencies: [
.package(url: "https://github.com/duckduckgo/BrowserServicesKit", exact: "110.0.0"),
.package(url: "https://github.com/duckduckgo/BrowserServicesKit", exact: "110.0.1"),
],
targets: [
.target(
Expand Down
2 changes: 1 addition & 1 deletion LocalPackages/SubscriptionUI/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ let package = Package(
targets: ["SubscriptionUI"]),
],
dependencies: [
.package(url: "https://github.com/duckduckgo/BrowserServicesKit", exact: "110.0.0"),
.package(url: "https://github.com/duckduckgo/BrowserServicesKit", exact: "110.0.1"),
.package(path: "../SwiftUIExtensions")
],
targets: [
Expand Down
2 changes: 1 addition & 1 deletion LocalPackages/SwiftUIExtensions/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ let package = Package(
.library(name: "PreferencesViews", targets: ["PreferencesViews"]),
],
dependencies: [
.package(url: "https://github.com/duckduckgo/BrowserServicesKit", exact: "110.0.0"),
.package(url: "https://github.com/duckduckgo/BrowserServicesKit", exact: "110.0.1"),
],
targets: [
.target(
Expand Down
2 changes: 1 addition & 1 deletion LocalPackages/SyncUI/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ let package = Package(
],
dependencies: [
.package(path: "../SwiftUIExtensions"),
.package(url: "https://github.com/duckduckgo/BrowserServicesKit", exact: "110.0.0"),
.package(url: "https://github.com/duckduckgo/BrowserServicesKit", exact: "110.0.1"),
],
targets: [
.target(
Expand Down
2 changes: 1 addition & 1 deletion LocalPackages/SystemExtensionManager/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ let package = Package(
),
],
dependencies: [
.package(url: "https://github.com/duckduckgo/BrowserServicesKit", exact: "110.0.0"),
.package(url: "https://github.com/duckduckgo/BrowserServicesKit", exact: "110.0.1"),
],
targets: [
// Targets are the basic building blocks of a package, defining a module or a test suite.
Expand Down
2 changes: 1 addition & 1 deletion LocalPackages/XPCHelper/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ let package = Package(
.library(name: "XPCHelper", targets: ["XPCHelper"]),
],
dependencies: [
.package(url: "https://github.com/duckduckgo/BrowserServicesKit", exact: "110.0.0"),
.package(url: "https://github.com/duckduckgo/BrowserServicesKit", exact: "110.0.1"),
],
targets: [
.target(
Expand Down

0 comments on commit 2049db6

Please sign in to comment.