From 994213b5690f07f940b338519060589cb54870be Mon Sep 17 00:00:00 2001 From: Daniel Bernal Date: Mon, 16 Dec 2024 16:17:25 +0100 Subject: [PATCH 1/4] =?UTF-8?q?Allow=20Youtube=E2=80=99s=20internal=20navi?= =?UTF-8?q?gation?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../DuckPlayer/DuckPlayerNavigationHandler.swift | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/DuckDuckGo/DuckPlayer/DuckPlayerNavigationHandler.swift b/DuckDuckGo/DuckPlayer/DuckPlayerNavigationHandler.swift index 05160f8381..e4156f5e85 100644 --- a/DuckDuckGo/DuckPlayer/DuckPlayerNavigationHandler.swift +++ b/DuckDuckGo/DuckPlayer/DuckPlayerNavigationHandler.swift @@ -936,8 +936,19 @@ extension DuckPlayerNavigationHandler: DuckPlayerNavigationHandling { return false } + // Allow Youtube's internal navigation when DuckPlayer is enabled and user is watching on Youtube + // This is to prevent DuckPlayer from interfering with Youtube's internal navigation for search and settings + // https://app.asana.com/0/1204099484721401/1208930843675395/f + var isYoutubeInternalNavigation = false + if let (destinationVideoID, _) = url.youtubeVideoParams, + let (originVideoID, _) = webView.url?.youtubeVideoParams, + destinationVideoID == originVideoID, + duckPlayerMode == .enabled { + isYoutubeInternalNavigation = true + } + // Redirect to Duck Player if enabled - if url.isYoutubeWatch && duckPlayerMode == .enabled && !isDuckPlayerRedirect(url: url) { + if url.isYoutubeWatch && duckPlayerMode == .enabled && !isDuckPlayerRedirect(url: url) && !isYoutubeInternalNavigation { redirectToDuckPlayerVideo(url: url, webView: webView) return true } From c59e80f896e2ebe6de90b5fa5253a388c6041be8 Mon Sep 17 00:00:00 2001 From: Daniel Bernal Date: Mon, 16 Dec 2024 16:53:40 +0100 Subject: [PATCH 2/4] Simplify Logic and Add test --- .../DuckPlayerNavigationHandler.swift | 5 ++--- ...YoutublePlayerNavigationHandlerTests.swift | 20 +++++++++++++++++++ 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/DuckDuckGo/DuckPlayer/DuckPlayerNavigationHandler.swift b/DuckDuckGo/DuckPlayer/DuckPlayerNavigationHandler.swift index e4156f5e85..13afd90eb0 100644 --- a/DuckDuckGo/DuckPlayer/DuckPlayerNavigationHandler.swift +++ b/DuckDuckGo/DuckPlayer/DuckPlayerNavigationHandler.swift @@ -939,16 +939,15 @@ extension DuckPlayerNavigationHandler: DuckPlayerNavigationHandling { // Allow Youtube's internal navigation when DuckPlayer is enabled and user is watching on Youtube // This is to prevent DuckPlayer from interfering with Youtube's internal navigation for search and settings // https://app.asana.com/0/1204099484721401/1208930843675395/f - var isYoutubeInternalNavigation = false if let (destinationVideoID, _) = url.youtubeVideoParams, let (originVideoID, _) = webView.url?.youtubeVideoParams, destinationVideoID == originVideoID, duckPlayerMode == .enabled { - isYoutubeInternalNavigation = true + return false } // Redirect to Duck Player if enabled - if url.isYoutubeWatch && duckPlayerMode == .enabled && !isDuckPlayerRedirect(url: url) && !isYoutubeInternalNavigation { + if url.isYoutubeWatch && duckPlayerMode == .enabled && !isDuckPlayerRedirect(url: url) { redirectToDuckPlayerVideo(url: url, webView: webView) return true } diff --git a/DuckDuckGoTests/YoutublePlayerNavigationHandlerTests.swift b/DuckDuckGoTests/YoutublePlayerNavigationHandlerTests.swift index 4b968fcd68..3ae7bc73ea 100644 --- a/DuckDuckGoTests/YoutublePlayerNavigationHandlerTests.swift +++ b/DuckDuckGoTests/YoutublePlayerNavigationHandlerTests.swift @@ -582,4 +582,24 @@ class DuckPlayerNavigationHandlerTests: XCTestCase { XCTAssertNil(tabNavigator.openedURL, "No new tabs should open") } + @MainActor + func testHandleDelegateNavigation_YoutubeWatchURLWithDuckPlayerEnabledAndSameVideoNavigation_ReturnsFalse() async { + // Arrange + let youtubeURL = URL(string: "https://www.youtube.com/watch?v=abc123")! + let youtubeInternalURL = URL(string: "https://www.youtube.com/watch?v=abc123&settings")! + let request = URLRequest(url: youtubeURL) + let mockFrameInfo = MockFrameInfo(isMainFrame: true) + let navigationAction = MockNavigationAction(request: request, targetFrame: mockFrameInfo) + playerSettings.mode = .enabled + featureFlagger.enabledFeatures = [.duckPlayer, .duckPlayerOpenInNewTab] + + mockWebView.setCurrentURL(youtubeInternalURL) + + // Act + let shouldCancel = handler.handleDelegateNavigation(navigationAction: navigationAction, webView: mockWebView) + + // Assert + XCTAssertFalse(shouldCancel, "Expected navigation NOT to be cancelled as it's Youtube Internal navigation") + } + } From 8c31c85f27f755040d91df0168d82ad7de537cb1 Mon Sep 17 00:00:00 2001 From: Daniel Bernal Date: Wed, 18 Dec 2024 17:31:21 +0100 Subject: [PATCH 3/4] Dont open new tab for Youtube videos --- DuckDuckGo/DuckPlayer/DuckPlayerNavigationHandler.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/DuckDuckGo/DuckPlayer/DuckPlayerNavigationHandler.swift b/DuckDuckGo/DuckPlayer/DuckPlayerNavigationHandler.swift index 13afd90eb0..8ea855cfca 100644 --- a/DuckDuckGo/DuckPlayer/DuckPlayerNavigationHandler.swift +++ b/DuckDuckGo/DuckPlayer/DuckPlayerNavigationHandler.swift @@ -954,7 +954,7 @@ extension DuckPlayerNavigationHandler: DuckPlayerNavigationHandling { // Redirect to Youtube + DuckPlayer Overlay if Ask Mode if url.isYoutubeWatch && duckPlayerMode == .alwaysAsk && !isDuckPlayerRedirect(url: url) { - redirectToYouTubeVideo(url: url, webView: webView, allowFirstVideo: false) + redirectToYouTubeVideo(url: url, webView: webView, allowFirstVideo: false, disableNewTab: true) return true } From 6d5bb0d8f71bfda6904ff783198167c9aaaaa98a Mon Sep 17 00:00:00 2001 From: Daniel Bernal Date: Wed, 18 Dec 2024 17:32:08 +0100 Subject: [PATCH 4/4] =?UTF-8?q?Don=E2=80=99t=20open=20new=20tabs=20at=20la?= =?UTF-8?q?unch=20when=20in=20alwaysAsk=20mode?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- DuckDuckGo/DuckPlayer/DuckPlayerNavigationHandler.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/DuckDuckGo/DuckPlayer/DuckPlayerNavigationHandler.swift b/DuckDuckGo/DuckPlayer/DuckPlayerNavigationHandler.swift index 13afd90eb0..8ea855cfca 100644 --- a/DuckDuckGo/DuckPlayer/DuckPlayerNavigationHandler.swift +++ b/DuckDuckGo/DuckPlayer/DuckPlayerNavigationHandler.swift @@ -954,7 +954,7 @@ extension DuckPlayerNavigationHandler: DuckPlayerNavigationHandling { // Redirect to Youtube + DuckPlayer Overlay if Ask Mode if url.isYoutubeWatch && duckPlayerMode == .alwaysAsk && !isDuckPlayerRedirect(url: url) { - redirectToYouTubeVideo(url: url, webView: webView, allowFirstVideo: false) + redirectToYouTubeVideo(url: url, webView: webView, allowFirstVideo: false, disableNewTab: true) return true }