Skip to content

Commit

Permalink
Fix Youtube Player Links
Browse files Browse the repository at this point in the history
  • Loading branch information
afterxleep committed Dec 23, 2024
1 parent 4933c2d commit 67ab3ab
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 9 deletions.
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

0 comments on commit 67ab3ab

Please sign in to comment.