|
| 1 | +From 166e8afbf262bd0584b87a6e353a3ed2673cf3bc Mon Sep 17 00:00:00 2001 |
| 2 | +From: octocorvus < [email protected]> |
| 3 | +Date: Thu, 6 Oct 2022 20:17:24 +0000 |
| 4 | +Subject: [PATCH] initial support for blocking media autoplay |
| 5 | + |
| 6 | +This adds a setting to chrome://flags to block media autoplay. |
| 7 | +Issue #92 |
| 8 | +--- |
| 9 | + chrome/browser/about_flags.cc | 4 ++++ |
| 10 | + chrome/browser/flag-metadata.json | 7 +++++++ |
| 11 | + chrome/browser/flag_descriptions.cc | 3 +++ |
| 12 | + chrome/browser/flag_descriptions.h | 3 +++ |
| 13 | + third_party/blink/common/features.cc | 3 +++ |
| 14 | + third_party/blink/public/common/features.h | 2 ++ |
| 15 | + .../renderer/core/html/media/autoplay_policy.cc | 14 +++++++++++++- |
| 16 | + .../renderer/core/html/media/autoplay_policy.h | 2 ++ |
| 17 | + .../renderer/core/html/media/html_media_element.cc | 3 ++- |
| 18 | + 9 files changed, 39 insertions(+), 2 deletions(-) |
| 19 | + |
| 20 | +diff --git a/chrome/browser/about_flags.cc b/chrome/browser/about_flags.cc |
| 21 | +index 91b87e334bb27..5f71846e5347f 100644 |
| 22 | +--- a/chrome/browser/about_flags.cc |
| 23 | ++++ b/chrome/browser/about_flags.cc |
| 24 | +@@ -6135,6 +6135,10 @@ const FeatureEntry kFeatureEntries[] = { |
| 25 | + flag_descriptions::kDisableProcessReuseDescription, kOsDesktop, |
| 26 | + FEATURE_VALUE_TYPE(features::kDisableProcessReuse)}, |
| 27 | + |
| 28 | ++ {"block-media-autoplay", flag_descriptions::kBlockMediaAutoplay, |
| 29 | ++ flag_descriptions::kBlockMediaAutoplayDescription, kOsAll, |
| 30 | ++ FEATURE_VALUE_TYPE(blink::features::kBlockMediaAutoplay)}, |
| 31 | ++ |
| 32 | + #if !BUILDFLAG(IS_ANDROID) |
| 33 | + {"enable-accessibility-live-caption", |
| 34 | + flag_descriptions::kEnableAccessibilityLiveCaptionName, |
| 35 | +diff --git a/chrome/browser/flag-metadata.json b/chrome/browser/flag-metadata.json |
| 36 | +index 0744e987693b2..2d9d5ec743286 100644 |
| 37 | +--- a/chrome/browser/flag-metadata.json |
| 38 | ++++ b/chrome/browser/flag-metadata.json |
| 39 | +@@ -1370,6 +1370,13 @@ |
| 40 | + |
| 41 | + "expiry_milestone": 110 |
| 42 | + }, |
| 43 | ++ { |
| 44 | ++ "name": "block-media-autoplay", |
| 45 | ++ "owners": [ |
| 46 | + |
| 47 | ++ ], |
| 48 | ++ "expiry_milestone": -1 |
| 49 | ++ }, |
| 50 | + { |
| 51 | + "name": "disable-quick-answers-v2-translation", |
| 52 | + "owners": [ "croissant-eng" ], |
| 53 | +diff --git a/chrome/browser/flag_descriptions.cc b/chrome/browser/flag_descriptions.cc |
| 54 | +index 2998fc1683b13..cc30201b5014f 100644 |
| 55 | +--- a/chrome/browser/flag_descriptions.cc |
| 56 | ++++ b/chrome/browser/flag_descriptions.cc |
| 57 | +@@ -830,6 +830,9 @@ const char kDisableProcessReuseDescription[] = |
| 58 | + "unrelated tabs. This is an experimental mode that will result in more " |
| 59 | + "processes being created."; |
| 60 | + |
| 61 | ++const char kBlockMediaAutoplay[] = "Block media autoplay"; |
| 62 | ++const char kBlockMediaAutoplayDescription[] = "Block media autoplay."; |
| 63 | ++ |
| 64 | + const char kDisallowDocWrittenScriptsUiName[] = |
| 65 | + "Block scripts loaded via document.write"; |
| 66 | + const char kDisallowDocWrittenScriptsUiDescription[] = |
| 67 | +diff --git a/chrome/browser/flag_descriptions.h b/chrome/browser/flag_descriptions.h |
| 68 | +index d095089242878..482d9f5673357 100644 |
| 69 | +--- a/chrome/browser/flag_descriptions.h |
| 70 | ++++ b/chrome/browser/flag_descriptions.h |
| 71 | +@@ -415,6 +415,9 @@ extern const char kDarkLightTestDescription[]; |
| 72 | + extern const char kDisableProcessReuse[]; |
| 73 | + extern const char kDisableProcessReuseDescription[]; |
| 74 | + |
| 75 | ++extern const char kBlockMediaAutoplay[]; |
| 76 | ++extern const char kBlockMediaAutoplayDescription[]; |
| 77 | ++ |
| 78 | + extern const char kDiscountConsentV2Name[]; |
| 79 | + extern const char kDiscountConsentV2Description[]; |
| 80 | + |
| 81 | +diff --git a/third_party/blink/common/features.cc b/third_party/blink/common/features.cc |
| 82 | +index e69f9c7bab3fe..e40c3dd8986ee 100644 |
| 83 | +--- a/third_party/blink/common/features.cc |
| 84 | ++++ b/third_party/blink/common/features.cc |
| 85 | +@@ -1623,5 +1623,8 @@ const base::Feature kWebRtcThreadsUseResourceEfficientType{ |
| 86 | + const base::Feature kThrottleIntersectionObserverUMA{ |
| 87 | + "ThrottleIntersectionObserverUMA", base::FEATURE_DISABLED_BY_DEFAULT}; |
| 88 | + |
| 89 | ++const base::Feature kBlockMediaAutoplay{"BlockMediaAutoplay", |
| 90 | ++ base::FEATURE_DISABLED_BY_DEFAULT}; |
| 91 | ++ |
| 92 | + } // namespace features |
| 93 | + } // namespace blink |
| 94 | +diff --git a/third_party/blink/public/common/features.h b/third_party/blink/public/common/features.h |
| 95 | +index 73ff7dbf3936b..5b9aade655d5c 100644 |
| 96 | +--- a/third_party/blink/public/common/features.h |
| 97 | ++++ b/third_party/blink/public/common/features.h |
| 98 | +@@ -869,6 +869,8 @@ BLINK_COMMON_EXPORT extern const base::Feature |
| 99 | + // collected on 10% of animation frames. |
| 100 | + BLINK_COMMON_EXPORT extern const base::Feature kThrottleIntersectionObserverUMA; |
| 101 | + |
| 102 | ++BLINK_COMMON_EXPORT extern const base::Feature kBlockMediaAutoplay; |
| 103 | ++ |
| 104 | + } // namespace features |
| 105 | + } // namespace blink |
| 106 | + |
| 107 | +diff --git a/third_party/blink/renderer/core/html/media/autoplay_policy.cc b/third_party/blink/renderer/core/html/media/autoplay_policy.cc |
| 108 | +index 871d82556706b..59e05e0c595bc 100644 |
| 109 | +--- a/third_party/blink/renderer/core/html/media/autoplay_policy.cc |
| 110 | ++++ b/third_party/blink/renderer/core/html/media/autoplay_policy.cc |
| 111 | +@@ -4,7 +4,9 @@ |
| 112 | + |
| 113 | + #include "third_party/blink/renderer/core/html/media/autoplay_policy.h" |
| 114 | + |
| 115 | ++#include "base/feature_list.h" |
| 116 | + #include "build/build_config.h" |
| 117 | ++#include "third_party/blink/public/common/features.h" |
| 118 | + #include "third_party/blink/public/mojom/autoplay/autoplay.mojom-blink.h" |
| 119 | + #include "third_party/blink/public/mojom/permissions_policy/permissions_policy.mojom-blink.h" |
| 120 | + #include "third_party/blink/public/mojom/webpreferences/web_preferences.mojom-blink.h" |
| 121 | +@@ -217,6 +219,9 @@ void AutoplayPolicy::StopAutoplayMutedWhenVisible() { |
| 122 | + |
| 123 | + bool AutoplayPolicy::RequestAutoplayUnmute() { |
| 124 | + DCHECK_NE(0, element_->EffectiveMediaVolume()); |
| 125 | ++ if (IsAutoplayBlocked()) |
| 126 | ++ return false; |
| 127 | ++ |
| 128 | + bool was_autoplaying_muted = IsAutoplayingMutedInternal(true); |
| 129 | + |
| 130 | + TryUnlockingUserGesture(); |
| 131 | +@@ -241,6 +246,9 @@ bool AutoplayPolicy::RequestAutoplayUnmute() { |
| 132 | + } |
| 133 | + |
| 134 | + bool AutoplayPolicy::RequestAutoplayByAttribute() { |
| 135 | ++ if (IsAutoplayBlocked()) |
| 136 | ++ return false; |
| 137 | ++ |
| 138 | + if (!ShouldAutoplay()) |
| 139 | + return false; |
| 140 | + |
| 141 | +@@ -267,7 +275,7 @@ absl::optional<DOMExceptionCode> AutoplayPolicy::RequestPlay() { |
| 142 | + if (!LocalFrame::HasTransientUserActivation( |
| 143 | + element_->GetDocument().GetFrame())) { |
| 144 | + autoplay_uma_helper_->OnAutoplayInitiated(AutoplaySource::kMethod); |
| 145 | +- if (IsGestureNeededForPlayback()) |
| 146 | ++ if (IsGestureNeededForPlayback() || IsAutoplayBlocked()) |
| 147 | + return DOMExceptionCode::kNotAllowedError; |
| 148 | + } else { |
| 149 | + TryUnlockingUserGesture(); |
| 150 | +@@ -318,6 +326,10 @@ bool AutoplayPolicy::IsGestureNeededForPlayback() const { |
| 151 | + return !IsEligibleForAutoplayMuted(); |
| 152 | + } |
| 153 | + |
| 154 | ++bool AutoplayPolicy::IsAutoplayBlocked() const { |
| 155 | ++ return base::FeatureList::IsEnabled(features::kBlockMediaAutoplay); |
| 156 | ++} |
| 157 | ++ |
| 158 | + String AutoplayPolicy::GetPlayErrorMessage() const { |
| 159 | + return IsUsingDocumentUserActivationRequiredPolicy() |
| 160 | + ? kErrorAutoplayFuncUnified |
| 161 | +diff --git a/third_party/blink/renderer/core/html/media/autoplay_policy.h b/third_party/blink/renderer/core/html/media/autoplay_policy.h |
| 162 | +index ca5c996363ad8..2e0499d916eba 100644 |
| 163 | +--- a/third_party/blink/renderer/core/html/media/autoplay_policy.h |
| 164 | ++++ b/third_party/blink/renderer/core/html/media/autoplay_policy.h |
| 165 | +@@ -98,6 +98,8 @@ class CORE_EXPORT AutoplayPolicy final |
| 166 | + // gesture is currently being processed. |
| 167 | + bool IsGestureNeededForPlayback() const; |
| 168 | + |
| 169 | ++ bool IsAutoplayBlocked() const; |
| 170 | ++ |
| 171 | + // Returns an error string to be used by the HTMLMediaElement when the play() |
| 172 | + // method fails because of autoplay restrictions. |
| 173 | + String GetPlayErrorMessage() const; |
| 174 | +diff --git a/third_party/blink/renderer/core/html/media/html_media_element.cc b/third_party/blink/renderer/core/html/media/html_media_element.cc |
| 175 | +index bc96dc883958b..722bbcff8229a 100644 |
| 176 | +--- a/third_party/blink/renderer/core/html/media/html_media_element.cc |
| 177 | ++++ b/third_party/blink/renderer/core/html/media/html_media_element.cc |
| 178 | +@@ -2663,7 +2663,8 @@ String HTMLMediaElement::EffectivePreload() const { |
| 179 | + } |
| 180 | + |
| 181 | + WebMediaPlayer::Preload HTMLMediaElement::EffectivePreloadType() const { |
| 182 | +- if (Autoplay() && !autoplay_policy_->IsGestureNeededForPlayback()) |
| 183 | ++ if (Autoplay() && !autoplay_policy_->IsGestureNeededForPlayback() && |
| 184 | ++ !autoplay_policy_->IsAutoplayBlocked()) |
| 185 | + return WebMediaPlayer::kPreloadAuto; |
| 186 | + |
| 187 | + WebMediaPlayer::Preload preload = PreloadType(); |
0 commit comments