Bug 1113086 - AudioChannel policy in Browser API - patch 6 - media-playback has to be dispatched async, r=alwu

This commit is contained in:
Andrea Marchesini 2015-06-18 15:56:59 +01:00
Родитель 1e56637452
Коммит 717e1e2a10
1 изменённых файлов: 83 добавлений и 38 удалений

Просмотреть файл

@ -43,34 +43,59 @@ namespace {
// If true, any new AudioChannelAgent will be muted when created. // If true, any new AudioChannelAgent will be muted when created.
bool sAudioChannelMutedByDefault = false; bool sAudioChannelMutedByDefault = false;
class NotifyChannelActiveRunnable final : public nsRunnable
{
public:
NotifyChannelActiveRunnable(uint64_t aWindowID, AudioChannel aAudioChannel,
bool aActive)
: mWindowID(aWindowID)
, mAudioChannel(aAudioChannel)
, mActive(aActive)
{}
NS_IMETHOD Run() override
{
nsCOMPtr<nsIObserverService> observerService =
services::GetObserverService();
if (NS_WARN_IF(!observerService)) {
return NS_ERROR_FAILURE;
}
nsCOMPtr<nsISupportsPRUint64> wrapper =
do_CreateInstance(NS_SUPPORTS_PRUINT64_CONTRACTID);
if (NS_WARN_IF(!wrapper)) {
return NS_ERROR_FAILURE;
}
wrapper->SetData(mWindowID);
nsAutoString name;
AudioChannelService::GetAudioChannelString(mAudioChannel, name);
nsAutoCString topic;
topic.Assign("audiochannel-activity-");
topic.Append(NS_ConvertUTF16toUTF8(name));
observerService->NotifyObservers(wrapper, topic.get(),
mActive
? MOZ_UTF16("active")
: MOZ_UTF16("inactive"));
return NS_OK;
}
private:
const uint64_t mWindowID;
const AudioChannel mAudioChannel;
const bool mActive;
};
void void
NotifyChannelActive(uint64_t aWindowID, AudioChannel aAudioChannel, NotifyChannelActive(uint64_t aWindowID, AudioChannel aAudioChannel,
bool aActive) bool aActive)
{ {
nsCOMPtr<nsIObserverService> observerService = nsRefPtr<nsRunnable> runnable =
services::GetObserverService(); new NotifyChannelActiveRunnable(aWindowID, aAudioChannel, aActive);
if (NS_WARN_IF(!observerService)) { NS_DispatchToCurrentThread(runnable);
return;
}
nsCOMPtr<nsISupportsPRUint64> wrapper =
do_CreateInstance(NS_SUPPORTS_PRUINT64_CONTRACTID);
if (!wrapper) {
return;
}
wrapper->SetData(aWindowID);
nsAutoString name;
AudioChannelService::GetAudioChannelString(aAudioChannel, name);
nsAutoCString topic;
topic.Assign("audiochannel-activity-");
topic.Append(NS_ConvertUTF16toUTF8(name));
observerService->NotifyObservers(wrapper, topic.get(),
aActive
? MOZ_UTF16("active") : MOZ_UTF16("inactive"));
} }
already_AddRefed<nsPIDOMWindow> already_AddRefed<nsPIDOMWindow>
@ -94,6 +119,34 @@ IsParentProcess()
return XRE_GetProcessType() == GeckoProcessType_Default; return XRE_GetProcessType() == GeckoProcessType_Default;
} }
class MediaPlaybackRunnable : public nsRunnable
{
public:
MediaPlaybackRunnable(nsIDOMWindow* aWindow, bool aActive)
: mWindow(aWindow)
, mActive(aActive)
{}
NS_IMETHOD Run()
{
nsCOMPtr<nsIObserverService> observerService =
services::GetObserverService();
if (observerService) {
observerService->NotifyObservers(
ToSupports(mWindow),
"media-playback",
mActive ? NS_LITERAL_STRING("active").get()
: NS_LITERAL_STRING("inactive").get());
}
return NS_OK;
}
private:
nsCOMPtr<nsIDOMWindow> mWindow;
bool mActive;
};
} // anonymous namespace } // anonymous namespace
StaticRefPtr<AudioChannelService> gAudioChannelService; StaticRefPtr<AudioChannelService> gAudioChannelService;
@ -208,13 +261,9 @@ AudioChannelService::RegisterAudioChannelAgent(AudioChannelAgent* aAgent,
// If this is the first agent for this window, we must notify the observers. // If this is the first agent for this window, we must notify the observers.
if (winData->mAgents.Length() == 1) { if (winData->mAgents.Length() == 1) {
nsCOMPtr<nsIObserverService> observerService = nsRefPtr<MediaPlaybackRunnable> runnable =
services::GetObserverService(); new MediaPlaybackRunnable(aAgent->Window(), true /* active */);
if (observerService) { NS_DispatchToCurrentThread(runnable);
observerService->NotifyObservers(ToSupports(aAgent->Window()),
"media-playback",
NS_LITERAL_STRING("active").get());
}
} }
MaybeSendStatusUpdate(); MaybeSendStatusUpdate();
@ -258,13 +307,9 @@ AudioChannelService::UnregisterAudioChannelAgent(AudioChannelAgent* aAgent)
// If this is the last agent for this window, we must notify the observers. // If this is the last agent for this window, we must notify the observers.
if (winData->mAgents.IsEmpty()) { if (winData->mAgents.IsEmpty()) {
nsCOMPtr<nsIObserverService> observerService = nsRefPtr<MediaPlaybackRunnable> runnable =
services::GetObserverService(); new MediaPlaybackRunnable(aAgent->Window(), false /* active */);
if (observerService) { NS_DispatchToCurrentThread(runnable);
observerService->NotifyObservers(ToSupports(aAgent->Window()),
"media-playback",
NS_LITERAL_STRING("inactive").get());
}
} }
MaybeSendStatusUpdate(); MaybeSendStatusUpdate();