prevent-autoplay.patch 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. ## Adds a flag to prevent autoplaying media
  2. --- a/chrome/browser/ungoogled_flag_entries.h
  3. +++ b/chrome/browser/ungoogled_flag_entries.h
  4. @@ -7,1 +7,5 @@
  5. + {"prevent-autoplay",
  6. + "PreventAutoplay",
  7. + "Prevents media from playing automatically. ungoogled-chromium flag.",
  8. + kOsAll, FEATURE_VALUE_TYPE(media::kPreventAutoplay)},
  9. #endif // CHROME_BROWSER_UNGOOGLED_FLAG_ENTRIES_H_
  10. --- a/media/base/media_switches.cc
  11. +++ b/media/base/media_switches.cc
  12. @@ -908,6 +908,8 @@ const base::Feature kBresenhamCadence{"B
  13. const base::Feature kPlaybackSpeedButton{"PlaybackSpeedButton",
  14. base::FEATURE_ENABLED_BY_DEFAULT};
  15. +const base::Feature kPreventAutoplay{"PreventAutoplay", base::FEATURE_DISABLED_BY_DEFAULT};
  16. +
  17. bool IsVideoCaptureAcceleratedJpegDecodingEnabled() {
  18. if (base::CommandLine::ForCurrentProcess()->HasSwitch(
  19. switches::kDisableAcceleratedMjpegDecode)) {
  20. --- a/media/base/media_switches.h
  21. +++ b/media/base/media_switches.h
  22. @@ -255,6 +255,8 @@ MEDIA_EXPORT extern const base::Feature
  23. MEDIA_EXPORT extern const base::Feature kDeprecateLowUsageCodecs;
  24. #endif
  25. +MEDIA_EXPORT extern const base::Feature kPreventAutoplay;
  26. +
  27. // Based on a |command_line| and the current platform, returns the effective
  28. // autoplay policy. In other words, it will take into account the default policy
  29. // if none is specified via the command line and options passed for testing.
  30. --- a/third_party/blink/renderer/core/dom/document.cc
  31. +++ b/third_party/blink/renderer/core/dom/document.cc
  32. @@ -4081,6 +4081,7 @@ void Document::SetURL(const KURL& url) {
  33. if (GetFrame()) {
  34. if (FrameScheduler* frame_scheduler = GetFrame()->GetFrameScheduler())
  35. frame_scheduler->TraceUrlChange(url_.GetString());
  36. + GetFrame()->ClearRealUserActivation();
  37. }
  38. }
  39. --- a/third_party/blink/renderer/core/frame/local_frame.h
  40. +++ b/third_party/blink/renderer/core/frame/local_frame.h
  41. @@ -722,7 +722,17 @@ class CORE_EXPORT LocalFrame final : pub
  42. void RebindTextInputHostForTesting();
  43. #endif
  44. + void RealUserActivation() { rua_time_ = base::TimeTicks::Now(); }
  45. + void ClearRealUserActivation() { rua_time_ = base::TimeTicks(); rua_allowed_ = false; }
  46. + bool HasRealUserActivation() {
  47. + if (!rua_allowed_) rua_allowed_ = (base::TimeTicks::Now() - rua_time_).InSeconds() < 1;
  48. + return rua_allowed_;
  49. + }
  50. +
  51. private:
  52. + bool rua_allowed_ = false;
  53. + base::TimeTicks rua_time_ = base::TimeTicks();
  54. +
  55. friend class FrameNavigationDisabler;
  56. // LocalFrameMojoHandler is a part of LocalFrame.
  57. friend class LocalFrameMojoHandler;
  58. --- a/third_party/blink/renderer/core/html/media/html_media_element.cc
  59. +++ b/third_party/blink/renderer/core/html/media/html_media_element.cc
  60. @@ -2530,6 +2530,7 @@ bool HTMLMediaElement::ended() const {
  61. }
  62. bool HTMLMediaElement::Autoplay() const {
  63. + if (base::FeatureList::IsEnabled(media::kPreventAutoplay)) return false;
  64. return FastHasAttribute(html_names::kAutoplayAttr);
  65. }
  66. @@ -2638,6 +2639,17 @@ ScriptPromise HTMLMediaElement::playForB
  67. absl::optional<DOMExceptionCode> HTMLMediaElement::Play() {
  68. DVLOG(2) << "play(" << *this << ")";
  69. + if (base::FeatureList::IsEnabled(media::kPreventAutoplay)) {
  70. + if (!GetDocument().GetFrame()->HasRealUserActivation()) {
  71. + if (rua_mute_state_ == -1) rua_mute_state_ = muted_;
  72. + return DOMExceptionCode::kNotAllowedError;
  73. + }
  74. + if (rua_mute_state_ > -1) {
  75. + if (muted_ != rua_mute_state_) setMuted(rua_mute_state_);
  76. + rua_mute_state_ = -1;
  77. + }
  78. + }
  79. +
  80. absl::optional<DOMExceptionCode> exception_code =
  81. autoplay_policy_->RequestPlay();
  82. --- a/third_party/blink/renderer/core/html/media/html_media_element.h
  83. +++ b/third_party/blink/renderer/core/html/media/html_media_element.h
  84. @@ -763,6 +763,8 @@ class CORE_EXPORT HTMLMediaElement
  85. // playback raters other than 1.0.
  86. bool preserves_pitch_ = true;
  87. + signed char rua_mute_state_ = -1;
  88. +
  89. // Keeps track of when the player seek event was sent to the browser process.
  90. base::TimeTicks last_seek_update_time_;
  91. --- a/third_party/blink/renderer/core/input/event_handler.cc
  92. +++ b/third_party/blink/renderer/core/input/event_handler.cc
  93. @@ -830,6 +830,7 @@ WebInputEventResult EventHandler::Handle
  94. return WebInputEventResult::kHandledSuppressed;
  95. }
  96. + frame_->RealUserActivation();
  97. LocalFrame::NotifyUserActivation(
  98. frame_, mojom::blink::UserActivationNotificationType::kInteraction,
  99. RuntimeEnabledFeatures::BrowserVerifiedUserActivationMouseEnabled());
  100. --- a/third_party/blink/renderer/core/input/gesture_manager.cc
  101. +++ b/third_party/blink/renderer/core/input/gesture_manager.cc
  102. @@ -240,6 +240,7 @@ WebInputEventResult GestureManager::Hand
  103. FlooredIntPoint(gesture_event.PositionInRootFrame());
  104. Node* tapped_node = current_hit_test.InnerNode();
  105. Element* tapped_element = current_hit_test.InnerElement();
  106. + if(tapped_node) tapped_node->GetDocument().GetFrame()->RealUserActivation();
  107. LocalFrame::NotifyUserActivation(
  108. tapped_node ? tapped_node->GetDocument().GetFrame() : nullptr,
  109. mojom::blink::UserActivationNotificationType::kInteraction);
  110. @@ -403,6 +404,7 @@ WebInputEventResult GestureManager::Hand
  111. return WebInputEventResult::kNotHandled;
  112. }
  113. + if(inner_node) inner_node->GetDocument().GetFrame()->RealUserActivation();
  114. LocalFrame::NotifyUserActivation(
  115. inner_node ? inner_node->GetDocument().GetFrame() : nullptr,
  116. mojom::blink::UserActivationNotificationType::kInteraction);
  117. --- a/third_party/blink/renderer/core/input/keyboard_event_manager.cc
  118. +++ b/third_party/blink/renderer/core/input/keyboard_event_manager.cc
  119. @@ -210,6 +210,7 @@ WebInputEventResult KeyboardEventManager
  120. if (!is_modifier && initial_key_event.dom_key != ui::DomKey::ESCAPE &&
  121. (initial_key_event.GetType() == WebInputEvent::Type::kKeyDown ||
  122. initial_key_event.GetType() == WebInputEvent::Type::kRawKeyDown)) {
  123. + frame_->RealUserActivation();
  124. LocalFrame::NotifyUserActivation(
  125. frame_, mojom::blink::UserActivationNotificationType::kInteraction,
  126. RuntimeEnabledFeatures::BrowserVerifiedUserActivationKeyboardEnabled());
  127. --- a/third_party/blink/renderer/core/input/pointer_event_manager.cc
  128. +++ b/third_party/blink/renderer/core/input/pointer_event_manager.cc
  129. @@ -650,6 +650,7 @@ WebInputEventResult PointerEventManager:
  130. // associated with so just pick the pointer event that comes.
  131. if (event.GetType() == WebInputEvent::Type::kPointerUp &&
  132. !non_hovering_pointers_canceled_ && pointer_event_target.target_frame) {
  133. + pointer_event_target.target_frame->RealUserActivation();
  134. LocalFrame::NotifyUserActivation(
  135. pointer_event_target.target_frame,
  136. mojom::blink::UserActivationNotificationType::kInteraction);
  137. --- a/third_party/blink/renderer/modules/mediasession/media_session.cc
  138. +++ b/third_party/blink/renderer/modules/mediasession/media_session.cc
  139. @@ -410,6 +410,7 @@ void MediaSession::DidReceiveAction(
  140. LocalDOMWindow* window = GetSupplementable()->DomWindow();
  141. if (!window)
  142. return;
  143. + window->GetFrame()->RealUserActivation();
  144. LocalFrame::NotifyUserActivation(
  145. window->GetFrame(),
  146. mojom::blink::UserActivationNotificationType::kInteraction);