Improve YT video check (#30156)

This commit is contained in:
Simone Arpe
2025-07-22 19:56:20 +02:00
committed by GitHub
parent 5176aee44b
commit 6ed3822029
3 changed files with 16 additions and 7 deletions
@@ -263,7 +263,7 @@ void YouTubeScriptInjectorTabHelper::MaybeSetFullscreen() {
weak_factory_.GetWeakPtr(), rfh->GetGlobalFrameToken()));
}
bool YouTubeScriptInjectorTabHelper::IsYouTubeVideo() const {
bool YouTubeScriptInjectorTabHelper::IsYouTubeVideo(bool mobileOnly) const {
const GURL& url = web_contents()->GetLastCommittedURL();
if (!url.is_valid() || url.is_empty()) {
return false;
@@ -276,6 +276,15 @@ bool YouTubeScriptInjectorTabHelper::IsYouTubeVideo() const {
return false;
}
// If mobileOnly is true, require host to be exactly "m.youtube.com"
// (case-insensitive).
if (mobileOnly) {
const std::string& host = url.host();
if (!base::EqualsCaseInsensitiveASCII(host, "m.youtube.com")) {
return false;
}
}
// Check if path is exactly "/watch" (case sensitive).
const auto path = url.path_piece();
constexpr std::string_view watch_path = "/watch";
@@ -346,7 +355,7 @@ void YouTubeScriptInjectorTabHelper::OnFullscreenScriptComplete(
bool YouTubeScriptInjectorTabHelper::IsPictureInPictureAvailable() const {
return base::FeatureList::IsEnabled(
preferences::features::kBravePictureInPictureForYouTubeVideos) &&
IsYouTubeVideo() && web_contents() &&
IsYouTubeVideo(true) && web_contents() &&
web_contents()->IsDocumentOnLoadCompletedInPrimaryMainFrame();
}
@@ -23,7 +23,7 @@ class YouTubeScriptInjectorTabHelper
YouTubeScriptInjectorTabHelper& operator=(
const YouTubeScriptInjectorTabHelper&) = delete;
~YouTubeScriptInjectorTabHelper() override;
bool IsYouTubeVideo() const;
bool IsYouTubeVideo(bool mobileOnly = false) const;
void MaybeSetFullscreen();
// Fullscreen state management using PageUserData
@@ -85,7 +85,7 @@ TEST_F(YouTubeScriptInjectorTabHelperTest,
EXPECT_TRUE(GetHelper()->IsYouTubeVideo());
NavigateToURL(GURL("https://m.youtube.com/watch?v=abcdefg"));
EXPECT_TRUE(GetHelper()->IsYouTubeVideo());
EXPECT_TRUE(GetHelper()->IsYouTubeVideo(true));
NavigateToURL(GURL("https://www.youtube.com/watch?v=abcdefg&foo=bar"));
EXPECT_TRUE(GetHelper()->IsYouTubeVideo());
@@ -124,7 +124,7 @@ TEST_F(YouTubeScriptInjectorTabHelperTest, YouTubeDomainCorrectPathSubdomain) {
// Test fullscreen state management with PageUserData.
TEST_F(YouTubeScriptInjectorTabHelperTest, FullscreenStateManagement) {
// Navigate to a YouTube video.
NavigateToURL(GURL("https://www.youtube.com/watch?v=abcdefg"));
NavigateToURL(GURL("https://m.youtube.com/watch?v=abcdefg"));
// Initially, no fullscreen request should be recorded.
EXPECT_FALSE(GetHelper()->HasFullscreenBeenRequested());
@@ -145,14 +145,14 @@ TEST_F(YouTubeScriptInjectorTabHelperTest, FullscreenStateManagement) {
// Test fullscreen state resets on navigation.
TEST_F(YouTubeScriptInjectorTabHelperTest, FullscreenStateResetsOnNavigation) {
// Navigate to first YouTube video.
NavigateToURL(GURL("https://www.youtube.com/watch?v=abcdefg"));
NavigateToURL(GURL("https://m.youtube.com/watch?v=abcdefg"));
// Set fullscreen requested for first page.
GetHelper()->SetFullscreenRequested(true);
EXPECT_TRUE(GetHelper()->HasFullscreenBeenRequested());
// Navigate to second YouTube video.
NavigateToURL(GURL("https://www.youtube.com/watch?v=1234567"));
NavigateToURL(GURL("https://m.youtube.com/watch?v=1234567"));
// State should reset for new page.
EXPECT_FALSE(GetHelper()->HasFullscreenBeenRequested());