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

Fix Youtube Player Links #3757

Merged
merged 1 commit into from
Dec 24, 2024
Merged
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
13 changes: 8 additions & 5 deletions DuckDuckGo/DuckPlayer/DuckPlayerNavigationHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -438,11 +438,11 @@ final class DuckPlayerNavigationHandler: NSObject {
let referrerValue = queryItems.first(where: { $0.name == Constants.duckPlayerReferrerParameter })?.value
let allowFirstVideoValue = queryItems.first(where: { $0.name == Constants.allowFirstVideoParameter })?.value
let isNewTabValue = queryItems.first(where: { $0.name == Constants.newTabParameter })?.value
let youtubeEmbedURI = queryItems.first(where: { $0.name == Constants.youtubeEmbedURI })?.value
let youtubeEmbedURI = queryItems.first(where: { $0.name == Constants.youtubeEmbedURI })?.value ?? ""

// Use the from(string:) method to parse referrer
let referrer = DuckPlayerReferrer(string: referrerValue ?? "")
let allowFirstVideo = allowFirstVideoValue == "1" || youtubeEmbedURI.map(\.isEmpty) ?? false
let allowFirstVideo = allowFirstVideoValue == "1" || !youtubeEmbedURI.isEmpty
let isNewTab = isNewTabValue == "1"

return DuckPlayerParameters(referrer: referrer, isNewTap: isNewTab, allowFirstVideo: allowFirstVideo)
Expand Down Expand Up @@ -720,16 +720,19 @@ extension DuckPlayerNavigationHandler: DuckPlayerNavigationHandling {

guard videoID != lastWatchInYoutubeVideo else {
lastURLChangeHandling = Date()
return .handled
return .handled(.newVideo)
}

let parameters = getDuckPlayerParameters(url: url)

// If this is an internal Youtube Link (i.e Clicking in youtube logo in the player)
// Do not handle it

// If the URL has the allow first video, we just don't handle it
if parameters.allowFirstVideo {
lastWatchInYoutubeVideo = videoID
lastURLChangeHandling = Date()
return .handled
return .handled(.allowFirstVideo)
}

guard duckPlayerMode == .enabled else {
Expand All @@ -743,7 +746,7 @@ extension DuckPlayerNavigationHandler: DuckPlayerNavigationHandling {
})
lastURLChangeHandling = Date()
Logger.duckPlayer.debug("Handling URL change for \(webView.url?.absoluteString ?? "")")
return .handled
return .handled(.duckPlayerEnabled)
} else {

}
Expand Down
13 changes: 10 additions & 3 deletions DuckDuckGo/DuckPlayer/DuckPlayerNavigationHandling.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,17 +42,24 @@ extension DuckPlayerReferrer {
enum DuckPlayerNavigationHandlerURLChangeResult {

/// Possible reasons for not handling a URL change.
enum HandlingResult {
enum NotHandledResult {
case featureOff
case invalidURL
case duckPlayerDisabled
case isNotYoutubeWatch
case disabledForVideo
case duplicateNavigation
}

/// Possible reasons for handling a URL change.
enum HandledResult {
case newVideo
case allowFirstVideo
case duckPlayerEnabled
}

case handled
case notHandled(HandlingResult)
case handled(HandledResult)
case notHandled(NotHandledResult)
}

/// Represents the direction of navigation in the Duck Player.
Expand Down
22 changes: 21 additions & 1 deletion DuckDuckGoTests/YoutublePlayerNavigationHandlerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ class DuckPlayerNavigationHandlerTests: XCTestCase {
let result2 = handler.handleURLChange(webView: mockWebView)

// Assert
if case .handled = result1 {
if case .handled(.duckPlayerEnabled) = result1 {
// Success
} else {
XCTFail("Expected first call to return .handled")
Expand Down Expand Up @@ -371,6 +371,26 @@ class DuckPlayerNavigationHandlerTests: XCTestCase {
}
}

@MainActor
func testHandleURLChange_WithYoutubeEmbedURIParam_ReturnsHandled() async {
// Arrange
let youtubeURL = URL(string: "https://www.youtube.com/watch?v=abc123&&embeds_referring_euri=true")!
mockWebView.setCurrentURL(youtubeURL)
playerSettings.mode = .enabled
featureFlagger.enabledFeatures = [.duckPlayer, .duckPlayerOpenInNewTab]

// Act
let result = handler.handleURLChange(webView: mockWebView)

// Assert
if case .handled(.allowFirstVideo) = result {
// Success
} else {
XCTFail("Expected first call to return .handled")
}
}


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