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:
@@ -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");
|
||||
});
|
||||
},
|
||||
};
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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.
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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 =
|
||||
|
||||
Reference in New Issue
Block a user