Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Duckplayer: Bugfix: Update referrer from URL #3790

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions DuckDuckGo/DuckPlayer/DuckPlayerNavigationHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -603,6 +603,17 @@ final class DuckPlayerNavigationHandler: NSObject {
return url.fragment != nil && !url.fragment!.isEmpty
}

/// Checks a URL and updates the referer if present
///
/// - Parameter url: The 'URL' with referrer parameters (current URL)
private func updateReferrerIfNeeded(url: URL) {
// Get the referrer from the URL if present
let urlReferrer = getDuckPlayerParameters(url: url).referrer
if urlReferrer != .other && urlReferrer != .undefined {
referrer = urlReferrer
}
}

}

extension DuckPlayerNavigationHandler: DuckPlayerNavigationHandling {
Expand Down Expand Up @@ -643,6 +654,9 @@ extension DuckPlayerNavigationHandler: DuckPlayerNavigationHandling {
// Determine navigation type
let shouldOpenInNewTab = isOpenInNewTabEnabled && !isNewTab(navigationAction)

// Update referrer if needed
updateReferrerIfNeeded(url: url)

// Handle duck:// scheme URLs (Or direct navigation to duck player)
if url.isDuckURLScheme {

Expand Down
17 changes: 17 additions & 0 deletions DuckDuckGoTests/YoutublePlayerNavigationHandlerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,23 @@ class DuckPlayerNavigationHandlerTests: XCTestCase {
}
}

@MainActor
func testHandleNavigation_WithReferrerInURL_UpdatesDuckPlayerReferrer() async {
// Arrange
let youtubeURL = URL(string: "https://www.youtube.com/watch?v=abc123&dp_referrer=serp")!
let navigationAction = MockNavigationAction(request: URLRequest(url: youtubeURL))
playerSettings.mode = .alwaysAsk
playerSettings.openInNewTab = true
featureFlagger.enabledFeatures = [.duckPlayer, .duckPlayerOpenInNewTab]

// Act
handler.handleDuckNavigation(navigationAction, webView: mockWebView)

// Assert
XCTAssertEqual(handler.referrer, .serp)

}

@MainActor
func testHandleDelegateNavigation_DuckPlayerURL_CancelNavigationAndLoadsDuckPlayerWithParamsInTab() async {
// Arrange
Expand Down
Loading