Bug 1733902 - part1 : create a new sync attribute on top-level browsing context for whether we should delay media from starting, and use it to replace the old attribute on outer windows. r=nika,emilio

Brief recap:
Before we have blocking autoplay, we had a feature which will delay media from start if the tab hasn't been visited by users, in order to prevent an unvisited background tab suddenly play sounds, which is annoying and might be unexpected to users.

In this patch:
The attribute we use to check whether we need to delay media is on the outer window, which is not Fission compatible if the top level window and its child windows are on the different process.

If the top level window has been visited, then all its child window should follow the same result. Therefore, we need to move the attribute to the browsing context in order to sync it across different windows.

Differential Revision: https://phabricator.services.mozilla.com/D128126
This commit is contained in:
alwu
2021-11-01 22:26:40 +00:00
parent fc30c132da
commit 0c767c1481
6 changed files with 65 additions and 52 deletions

View File

@@ -58,6 +58,7 @@
#include "mozilla/ResultExtensions.h"
#include "mozilla/Services.h"
#include "mozilla/StaticPrefs_fission.h"
#include "mozilla/StaticPrefs_media.h"
#include "mozilla/StaticPrefs_page_load.h"
#include "mozilla/StaticPtr.h"
#include "mozilla/URLQueryStringStripper.h"
@@ -409,6 +410,11 @@ already_AddRefed<BrowsingContext> BrowsingContext::CreateDetached(
fields.mAllowJavascript = inherit ? inherit->GetAllowJavascript() : true;
if (!parentBC) {
fields.mShouldDelayMediaFromStart =
StaticPrefs::media_block_autoplay_until_in_foreground();
}
RefPtr<BrowsingContext> context;
if (XRE_IsParentProcess()) {
context = new CanonicalBrowsingContext(parentWC, group, id,
@@ -2818,6 +2824,26 @@ void BrowsingContext::DidSet(FieldIndex<IDX_Muted>) {
});
}
bool BrowsingContext::CanSet(FieldIndex<IDX_ShouldDelayMediaFromStart>,
const bool& aValue, ContentParent* aSource) {
return IsTop();
}
void BrowsingContext::DidSet(FieldIndex<IDX_ShouldDelayMediaFromStart>,
bool aOldValue) {
MOZ_ASSERT(IsTop(), "Set attribute on non top-level context!");
if (aOldValue == GetShouldDelayMediaFromStart()) {
return;
}
if (!GetShouldDelayMediaFromStart()) {
PreOrderWalk([&](BrowsingContext* aContext) {
if (nsPIDOMWindowOuter* win = aContext->GetDOMWindow()) {
win->ActivateMediaComponents();
}
});
}
}
bool BrowsingContext::CanSet(FieldIndex<IDX_OverrideDPPX>, const float& aValue,
ContentParent* aSource) {
return XRE_IsParentProcess() && !aSource && IsTop();