Skip to content

Commit

Permalink
address PR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
tapanmodh committed Jun 24, 2024
1 parent e6613e4 commit 14a4d00
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@
@JNINamespace("chrome::android")
public class BackgroundVideoPlaybackTabHelper {

public static void sendOrientationChangeEvent(WebContents webContents, boolean isFullScreen) {
BackgroundVideoPlaybackTabHelperJni.get()
.sendOrientationChangeEvent(webContents, isFullScreen);
public static void toggleFullscreen(WebContents webContents, boolean isFullScreen) {
BackgroundVideoPlaybackTabHelperJni.get().toggleFullscreen(webContents, isFullScreen);
}

public static boolean isPlayingMedia(WebContents webContents) {
Expand All @@ -24,7 +23,7 @@ public static boolean isPlayingMedia(WebContents webContents) {

@NativeMethods
interface Natives {
void sendOrientationChangeEvent(WebContents webContents, boolean isFullScreen);
void toggleFullscreen(WebContents webContents, boolean isFullScreen);

boolean isPlayingMedia(WebContents webContents);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -386,8 +386,7 @@ public void onUserLeaveHint() {
&& isYTVideoUrl(currentTab.getUrl())
&& !isInPictureInPictureMode()
&& BackgroundVideoPlaybackTabHelper.isPlayingMedia(currentTab.getWebContents())) {
BackgroundVideoPlaybackTabHelper.sendOrientationChangeEvent(
currentTab.getWebContents(), true);
BackgroundVideoPlaybackTabHelper.toggleFullscreen(currentTab.getWebContents(), true);
try {
enterPictureInPictureMode(new PictureInPictureParams.Builder().build());
} catch (IllegalStateException | IllegalArgumentException e) {
Expand Down Expand Up @@ -1415,7 +1414,7 @@ public void performOnConfigurationChanged(Configuration newConfig) {
&& currentTab.getUrl() != null
&& isYTVideoUrl(currentTab.getUrl())
&& !isInPictureInPictureMode()) {
BackgroundVideoPlaybackTabHelper.sendOrientationChangeEvent(
BackgroundVideoPlaybackTabHelper.toggleFullscreen(
currentTab.getWebContents(),
newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE);
}
Expand Down
2 changes: 0 additions & 2 deletions browser/android/preferences/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import("//brave/components/ai_chat/core/common/buildflags/buildflags.gni")
import("//build/config/android/rules.gni")

source_set("preferences") {
# Remove when https://github.com/brave/brave-browser/issues/10657 is resolved
check_includes = false
sources = [
"background_video_playback_tab_helper.cc",
"background_video_playback_tab_helper.h",
Expand Down
108 changes: 55 additions & 53 deletions browser/android/preferences/background_video_playback_tab_helper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,26 @@

#include <string>

#include "base/strings/utf_string_conversions.h"
#include "brave/browser/android/preferences/features.h"
#include "brave/build/android/jni_headers/BackgroundVideoPlaybackTabHelper_jni.h"
#include "brave/components/brave_shields/content/browser/brave_shields_util.h"
#include "brave/components/constants/pref_names.h"
#include "brave/components/script_injector/common/mojom/script_injector.mojom.h"
#include "chrome/browser/content_settings/host_content_settings_map_factory.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/common/chrome_isolated_world_ids.h"
#include "components/prefs/pref_service.h"
#include "content/browser/media/session/media_session_impl.h"
#include "content/browser/web_contents/web_contents_impl.h"
#include "content/public/browser/navigation_controller.h"
#include "content/public/browser/navigation_entry.h"
#include "content/public/browser/navigation_handle.h"
#include "content/public/browser/web_contents.h"
#include "mojo/public/cpp/bindings/associated_remote.h"
#include "net/base/registry_controlled_domains/registry_controlled_domain.h"
#include "third_party/blink/public/common/associated_interfaces/associated_interface_provider.h"
#include "url/gurl.h"

namespace {
bool is_media_playing_ = false;

const char16_t k_youtube_background_playback_script[] =
u"(function() {"
" if (document._addEventListener === undefined) {"
Expand Down Expand Up @@ -62,6 +62,14 @@ bool IsBackgroundVideoPlaybackEnabled(content::WebContents* contents) {

return true;
}

mojo::AssociatedRemote<script_injector::mojom::ScriptInjector> GetRemote(
content::RenderFrameHost* rfh) {
mojo::AssociatedRemote<script_injector::mojom::ScriptInjector>
script_injector_remote;
rfh->GetRemoteAssociatedInterfaces()->GetInterface(&script_injector_remote);
return script_injector_remote;
}
} // namespace

BackgroundVideoPlaybackTabHelper::BackgroundVideoPlaybackTabHelper(
Expand All @@ -84,73 +92,67 @@ void BackgroundVideoPlaybackTabHelper::DidFinishNavigation(
}
}

void BackgroundVideoPlaybackTabHelper::MediaStartedPlaying(
const MediaPlayerInfo& /*video_type*/,
const content::MediaPlayerId& id) {
is_media_playing_ = true;
}

void BackgroundVideoPlaybackTabHelper::MediaStoppedPlaying(
const MediaPlayerInfo& /*video_type*/,
const content::MediaPlayerId& id,
WebContentsObserver::MediaStoppedReason /*reason*/) {
is_media_playing_ = false;
}

namespace chrome {
namespace android {

mojo::AssociatedRemote<script_injector::mojom::ScriptInjector> GetRemote(
content::RenderFrameHost* rfh) {
mojo::AssociatedRemote<script_injector::mojom::ScriptInjector>
script_injector_remote;
rfh->GetRemoteAssociatedInterfaces()->GetInterface(&script_injector_remote);
return script_injector_remote;
}
void JNI_BackgroundVideoPlaybackTabHelper_SendOrientationChangeEvent(
void JNI_BackgroundVideoPlaybackTabHelper_ToggleFullscreen(
JNIEnv* env,
const base::android::JavaParamRef<jobject>& jweb_contents,
jboolean is_full_screen) {
content::WebContents* web_contents =
content::WebContents::FromJavaWebContents(jweb_contents);
// Injecting this script to make youtube video fullscreen on landscape mode
// and exit fullscreen on portrait mode.
std::string script;
if (is_full_screen) {
script =
"if(!document.fullscreenElement) {"
" var fullscreenBtn = "
" document.getElementsByClassName('fullscreen-icon');"
" if(fullscreenBtn && fullscreenBtn.length > 0) {"
" fullscreenBtn[0].click();"
" } else {"
" var moviePlayer = document.getElementById('movie_player');"
" if (moviePlayer) {"
" moviePlayer.click();"
" }"
" setTimeout(() => {"
" var fullscreenBtn = "
" document.getElementsByClassName('fullscreen-icon');"
" if(fullscreenBtn && fullscreenBtn.length > 0) {"
" fullscreenBtn[0].click();"
" }"
" }, 50);"
" }"
"}";
constexpr const char16_t script[] =
uR"js(if(!document.fullscreenElement) {
var fullscreenBtn =
document.getElementsByClassName('fullscreen-icon');
if(fullscreenBtn && fullscreenBtn.length > 0) {
fullscreenBtn[0].click();
} else {
var moviePlayer = document.getElementById('movie_player');
if (moviePlayer) {
moviePlayer.click();
}
setTimeout(() => {
var fullscreenBtn =
document.getElementsByClassName('fullscreen-icon');
if(fullscreenBtn && fullscreenBtn.length > 0) {
fullscreenBtn[0].click();
}
}, 50);
}
} )js";
GetRemote(web_contents->GetPrimaryMainFrame())
->RequestAsyncExecuteScript(
ISOLATED_WORLD_ID_BRAVE_INTERNAL, script,
blink::mojom::UserActivationOption::kActivate,
blink::mojom::PromiseResultOption::kAwait, base::NullCallback());
} else {
script =
"if(document.fullscreenElement) {"
" document.exitFullscreen();"
"}";
if (web_contents->HasActiveEffectivelyFullscreenVideo()) {
web_contents->ExitFullscreen(true);
}
}
GetRemote(web_contents->GetPrimaryMainFrame())
->RequestAsyncExecuteScript(
ISOLATED_WORLD_ID_BRAVE_INTERNAL, base::UTF8ToUTF16(script),
blink::mojom::UserActivationOption::kActivate,
blink::mojom::PromiseResultOption::kAwait, base::NullCallback());
}

jboolean JNI_BackgroundVideoPlaybackTabHelper_IsPlayingMedia(
JNIEnv* env,
const base::android::JavaParamRef<jobject>& jweb_contents) {
content::WebContents* web_contents =
content::WebContents::FromJavaWebContents(jweb_contents);
content::MediaSessionImpl* media_session_impl =
content::MediaSessionImpl::Get(web_contents);
if (!media_session_impl) {
return false;
}
media_session::mojom::MediaSessionInfoPtr current_info =
media_session_impl->GetMediaSessionInfoSync();
return current_info->playback_state ==
media_session::mojom::MediaPlaybackState::kPlaying;
return is_media_playing_;
}
} // namespace android
} // namespace chrome
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@ class BackgroundVideoPlaybackTabHelper
void DidFinishNavigation(
content::NavigationHandle* navigation_handle) override;

void MediaStartedPlaying(const MediaPlayerInfo& video_type,
const content::MediaPlayerId& id) override;
void MediaStoppedPlaying(
const MediaPlayerInfo& video_type,
const content::MediaPlayerId& id,
WebContentsObserver::MediaStoppedReason reason) override;

WEB_CONTENTS_USER_DATA_KEY_DECL();
};

Expand Down

0 comments on commit 14a4d00

Please sign in to comment.