Bug 1476701 - notify observer when audible autoplay occurred. r=cpearce,jaws

In our autoplay shield-study, we want to collect the information which could tell us how many website
contains audible autoplay media, but there is no way to get this information on current API desigin.

Therefore, I would like to send a new notification when autoplay occurred.

The extension code could get the information by following way,
```
Services.obs.addObserver((subject, topic, data) => {
    // DO SOMETHING
}, "AudibleAutoplayMediaOccurred");
```

MozReview-Commit-ID: 4bSYcxDZOGK
This commit is contained in:
alwu
2018-07-25 09:08:44 -07:00
parent f474a291b9
commit c96fe8806b
6 changed files with 51 additions and 6 deletions

View File

@@ -4431,6 +4431,16 @@ window._gBrowser = {
tab.finishMediaBlockTimer();
}
});
this.addEventListener("AudibleAutoplayMediaOccurred", (event) => {
let browser = event.originalTarget;
let tab = this.getTabForBrowser(browser);
if (!tab) {
return;
}
Services.obs.notifyObservers(tab, "AudibleAutoplayMediaOccurred");
});
},
};

View File

@@ -12510,9 +12510,19 @@ void
nsIDocument::SetDocTreeHadAudibleMedia()
{
nsIDocument* topLevelDoc = GetTopLevelContentDocument();
if (topLevelDoc) {
topLevelDoc->mDocTreeHadAudibleMedia = true;
if (!topLevelDoc) {
return;
}
if (!topLevelDoc->mDocTreeHadAudibleMedia) {
RefPtr<AsyncEventDispatcher> asyncDispatcher =
new AsyncEventDispatcher(topLevelDoc,
NS_LITERAL_STRING("AudibleAutoplayMediaOccurred"),
CanBubble::eYes,
ChromeOnlyDispatch::eYes);
asyncDispatcher->PostDOMEvent();
}
topLevelDoc->mDocTreeHadAudibleMedia = true;
}
void

View File

@@ -4013,7 +4013,7 @@ HTMLMediaElement::AudioChannelAgentDelayingPlayback()
}
void
HTMLMediaElement::ReportAutoplayTelemetry() const
HTMLMediaElement::UpdateHadAudibleAutoplayState() const
{
// If we're audible, and autoplaying...
if ((Volume() > 0.0 && !Muted()) &&
@@ -4086,7 +4086,7 @@ HTMLMediaElement::Play(ErrorResult& aRv)
return promise.forget();
}
ReportAutoplayTelemetry();
UpdateHadAudibleAutoplayState();
const bool handlingUserInput = EventStateManager::IsHandlingUserInput();
switch (AutoplayPolicy::IsAllowedToPlay(*this)) {
@@ -6246,7 +6246,7 @@ HTMLMediaElement::CheckAutoplayDataReady()
return;
}
ReportAutoplayTelemetry();
UpdateHadAudibleAutoplayState();
switch (AutoplayPolicy::IsAllowedToPlay(*this)) {
case nsIAutoplay::BLOCKED:
return;

View File

@@ -1781,7 +1781,7 @@ private:
already_AddRefed<PlayPromise> CreatePlayPromise(ErrorResult& aRv) const;
void ReportAutoplayTelemetry() const;
void UpdateHadAudibleAutoplayState() const;
/**
* This function is called by AfterSetAttr and OnAttrSetButNotChanged.

View File

@@ -392,6 +392,17 @@ var UnselectedTabHoverObserver = {
};
UnselectedTabHoverObserver.init();
var AudibleAutoplayObserver = {
init() {
addEventListener("AudibleAutoplayMediaOccurred", this);
},
handleEvent(event) {
sendAsyncMessage("AudibleAutoplayMediaOccurred");
}
};
AudibleAutoplayObserver.init();
addMessageListener("Browser:PurgeSessionHistory", function BrowserPurgeHistory() {
let sessionHistory = docShell.QueryInterface(Ci.nsIWebNavigation).sessionHistory;
if (!sessionHistory) {

View File

@@ -591,6 +591,16 @@
</body>
</method>
<method name="notifyAudibleAutoplayMediaOccurred">
<body>
<![CDATA[
let event = document.createEvent("Events");
event.initEvent("AudibleAutoplayMediaOccurred", true, false);
this.dispatchEvent(event);
]]>
</body>
</method>
<!--
When the pref "media.block-autoplay-until-in-foreground" is on,
Gecko delays starting playback of media resources in tabs until the
@@ -844,6 +854,7 @@
this.messageManager.addMessageListener("AudioPlayback:ActiveMediaBlockStart", this);
this.messageManager.addMessageListener("AudioPlayback:ActiveMediaBlockStop", this);
this.messageManager.addMessageListener("UnselectedTabHover:Toggle", this);
this.messageManager.addMessageListener("AudibleAutoplayMediaOccurred", this);
if (this.hasAttribute("selectmenulist")) {
this.messageManager.addMessageListener("Forms:ShowDropDown", this);
@@ -972,6 +983,9 @@
++this._unselectedTabHoverMessageListenerCount > 0 :
--this._unselectedTabHoverMessageListenerCount == 0;
break;
case "AudibleAutoplayMediaOccurred":
this.notifyAudibleAutoplayMediaOccurred();
break;
case "Forms:ShowDropDown": {
if (!this._selectParentHelper) {
this._selectParentHelper =