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

[Android] youtube quality settings #24164

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open

Conversation

tapanmodh
Copy link
Contributor

@tapanmodh tapanmodh commented Jun 12, 2024

Resolves brave/brave-browser#32842

Submitter Checklist:

Reviewer Checklist:

  • A security review is not needed, or a link to one is included in the PR description
  • New files have MPL-2.0 license header
  • Adequate test coverage exists to prevent regressions
  • Major classes, functions and non-trivial code blocks are well-commented
  • Changes in component dependencies are properly reflected in gn
  • Code follows the style guide
  • Test plan is specified in PR before merging

After-merge Checklist:

Test Plan:

  1. HD Definition Playback On
    Go to Settings -> Media -> High Definition Playback -> Select On
    Restart brave
    Play any youtube video which has 720p resolution then it will play video with 720p resolution
  2. HD Definition Playback Allow over Wi-Fi
    Go to Settings -> Media -> High Definition Playback -> Select Allow over Wi-Fi
    Restart brave
    Mobile data: Youtube video play with default resolution decided by youtube.com player
    On Wi-Fi: Play any youtube video which has 720p resolution then it will play video with 720p resolution
  3. HD Definition Playback Off
    Youtube video play with default resolution decided by youtube.com player

@tapanmodh tapanmodh added this to the 1.69.x - Nightly milestone Jun 25, 2024
@tapanmodh tapanmodh marked this pull request as ready for review June 25, 2024 13:06
@tapanmodh tapanmodh requested review from deeppandya and a team as code owners June 25, 2024 13:06
@Override
public void setDefaultQuality(@YTVideoQuality int defaultQuality) {
UserPrefs.get(getProfile()).setInteger(BravePref.YT_VIDEO_QUALITY_PREF, defaultQuality);
BraveRelaunchUtils.askForRelaunch(getActivity());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we really need to ask for relaunch? I think as a max we should reload a current tab or even don't do anything at all.

rbAllowWifi.setChecked(true);
} else if (mQuality == YTVideoQuality.OFF) {
rbOff.setChecked(true);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's probably a good idea to setChecked to false for all 3 before the if or make default values in xml

Copy link
Member

@SergeyZhukovsky SergeyZhukovsky left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

++

Copy link
Contributor

@deeppandya deeppandya left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Copy link
Collaborator

@mkarolin mkarolin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

strings++

Copy link
Contributor

@stoletheminerals stoletheminerals left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

security lgtm. cc @bridiver for the JS inject

@tapanmodh tapanmodh marked this pull request as draft August 5, 2024 14:51
@tapanmodh tapanmodh marked this pull request as ready for review August 6, 2024 20:03
@tapanmodh tapanmodh requested a review from a team as a code owner August 6, 2024 20:03
@tapanmodh
Copy link
Contributor Author

@stoletheminerals implementation changed so I have created new security/privacy review: https://github.com/brave/reviews/issues/1720

PrefService* pref_service) {
#if BUILDFLAG(IS_ANDROID)
int qualityPref = pref_service->GetInteger(kYTVideoQualityPref);
return qualityPref == static_cast<int>(settings::YTVideoQuality::kOn) ||
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is not something BraveRendererUpdater should have to understand. You should send the raw pref value and also this is not going to correctly update when the connection type changes

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we have to send value based on preference set by user in the settings and connection type and for that we need connection type only when site starts loading. we are not listening for connection type changes.

@@ -47,7 +47,7 @@ const char kObservingScriptletEntryPoint[] =

const char kScriptletInitScript[] =
R"((function() {
let text = '(function() {\nconst scriptletGlobals = (() => {\nconst forwardedMapMethods = ["has", "get", "set"];\nconst handler = {\nget(target, prop) { if (forwardedMapMethods.includes(prop)) { return Map.prototype[prop].bind(target) } return target.get(prop); },\nset(target, prop, value) { if (!forwardedMapMethods.includes(prop)) { target.set(prop, value); } }\n};\nreturn new Proxy(new Map(%s), handler);\n})();\nlet deAmpEnabled = %s;\n' + %s + '})()';
let text = '(function() {\nconst scriptletGlobals = (() => {\nconst forwardedMapMethods = ["has", "get", "set"];\nconst handler = {\nget(target, prop) { if (forwardedMapMethods.includes(prop)) { return Map.prototype[prop].bind(target) } return target.get(prop); },\nset(target, prop, value) { if (!forwardedMapMethods.includes(prop)) { target.set(prop, value); } }\n};\nreturn new Proxy(new Map(%s), handler);\n})();\nlet deAmpEnabled = %s;\nlet isYtHdQualityPlaybackEnabled = %s;\n' + %s + '})()';
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this makes sense as part of cosmetic filter or adblock. Either this class should be made into a more generic handler and renamed or we should create something new here. wdyt @antonok-edm

@@ -9,6 +9,7 @@

#include "base/check_is_test.h"
#include "base/functional/bind.h"
#include "brave/browser/android/constants.h"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missing android guard

namespace settings {

// GENERATED_JAVA_ENUM_PACKAGE: org.chromium.chrome.browser.settings
enum class YTVideoQuality { kOn, kAllowOverWifi, kOff };
Copy link
Collaborator

@bridiver bridiver Aug 8, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

youtube_constants.h or youtube/constants.h and also please don't abbreviate. Use Youtube/youtube instead of YT in general for clarity

android/BUILD.gn Outdated
@@ -14,6 +14,7 @@ declare_args() {

java_cpp_enum("brave_android_java_enums_srcjar") {
sources = [
"//brave/browser/android/constants.h",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this file should have it's own java_cpp_enum in https://github.com/brave/brave-core/pull/24164/files#diff-c528ac841614c7466a1d915247b27506c4318d8bea37e40f0c63b186e24b28a0

The decentralized_dns one should also be fixed in a follow-up.

@@ -115,6 +115,7 @@ inline constexpr char kPlayYTVideoInBrowserEnabled[] =
"brave.play_yt_video_in_browser_enabled";
inline constexpr char kBackgroundVideoPlaybackEnabled[] =
"brave.background_video_playback";
inline constexpr char kYTVideoQualityPref[] = "brave.yt_video_quality";
Copy link
Collaborator

@bridiver bridiver Aug 8, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please move this into pref_names.h in the same directory as the enum along with its own java_cpp_strings target

The existing java_pref_names_srcjar has the same problem as https://github.com/brave/brave-core/pull/24164/files#r1708392864 above

if (chromeTabbedActivity != null && chromeTabbedActivity.getActivityTab() != null) {
Tab currentTab = chromeTabbedActivity.getActivityTab();
if (currentTab.getUrl().domainIs(BraveConstants.YOUTUBE_DOMAIN)) {
currentTab.reload();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do we need to reload the tab?

new cosmetic_filters::CosmeticFiltersJsRenderFrameObserver(
render_frame, ISOLATED_WORLD_ID_BRAVE_INTERNAL, dynamic_params_closure);
render_frame, ISOLATED_WORLD_ID_BRAVE_INTERNAL, dynamic_params_closure,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

reported by reviewdog 🐶
[semgrep] Security hotspot found (ISOLATED_WORLD). A security-team member should analyze the code security for possible vulnerabilities.

Source: https://github.com/brave/security-action/blob/main/assets/semgrep_rules/client/brave-isolated-world.yaml


Cc @thypon @diracdeltas @bridiver

Copy link
Contributor

[puLL-Merge] - brave/brave-core@24164

Description

This PR introduces a new feature for YouTube video quality settings on Android devices. It adds a preference to control HD video playback on YouTube, allowing users to choose between always enabling HD quality, enabling it only on Wi-Fi, or disabling it.

Changes

Changes

  1. android/brave_java_resources.gni and android/brave_java_sources.gni:

    • Added new XML resource files and Java source files for the YouTube quality preferences.
  2. BraveMediaRadioButtonYoutubeQualityPreference.java and BraveMediaYoutubeQualityPreferences.java:

    • New Java classes to handle the UI and logic for YouTube quality preferences.
  3. brave_media_radio_button_youtube_quality_preference.xml and brave_media_youtube_quality_preferences.xml:

    • New XML layout files for the YouTube quality preference UI.
  4. media_preferences.xml:

    • Added a new preference item for YouTube high definition playback.
  5. browser/android/youtube/:

    • New directory with constants.h and pref_names.h for YouTube-related constants and preferences.
  6. browser/brave_profile_prefs.cc:

    • Registered the new YouTube video quality preference.
  7. browser/profiles/brave_renderer_updater.cc:

    • Updated to include the new YouTube HD quality playback setting in the renderer configuration.
  8. browser/ui/android/strings/android_brave_strings.grd:

    • Added new string resources for the YouTube quality settings.
  9. common/brave_renderer_configuration.mojom:

    • Added a new field is_youtube_hd_quality_playback_enabled to the DynamicParams struct.
  10. components/cosmetic_filters/renderer/:

    • Updated to pass the YouTube HD quality setting to the JavaScript handler.
  11. renderer/brave_content_renderer_client.cc:

    • Modified to include the YouTube HD quality setting when creating the cosmetic filters observer.

Possible Issues

  • The implementation assumes that the YouTube domain remains constant. If YouTube changes its domain structure, the reload functionality in BraveMediaYoutubeQualityPreferences.java might need to be updated.

Security Hotspots

No significant security hotspots were identified in this change. The new feature primarily deals with user preferences and does not introduce new attack surfaces or handle sensitive data.

@@ -39,7 +39,8 @@ class CosmeticFiltersJSHandler {
// filtering is enabled, and returns whether or not to proceed with cosmetic
// filtering.
bool ProcessURL(const GURL& url, std::optional<base::OnceClosure> callback);
void ApplyRules(bool de_amp_enabled);
void ApplyRules(bool de_amp_enabled,
bool is_youtube_hd_quality_playback_enabled);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is definitely not what we want to do here, we should selectively enable these scripts on the browser side by using different data sources, but also right now this is going to be disabled when adblock is disabled which is clearly not what we want here

Copy link
Collaborator

@bridiver bridiver left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kjozwiak
Copy link
Member

Removing from 1.70.x as the above needs to go into master first which is currently on 1.72.x. Once the above is merged, the milestone will be added automatically.

@kjozwiak kjozwiak removed this from the 1.70.x - Release milestone Sep 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Android] YT Improvements: Default video quality settings
10 participants