diff --git a/dom/audiochannel/AudioChannelAgent.cpp b/dom/audiochannel/AudioChannelAgent.cpp index cf8ee78280dc..7aa48a249301 100644 --- a/dom/audiochannel/AudioChannelAgent.cpp +++ b/dom/audiochannel/AudioChannelAgent.cpp @@ -114,7 +114,7 @@ AudioChannelAgent::InitInternal(nsIDOMWindow* aWindow, int32_t aChannelType, /* boolean startPlaying (); */ NS_IMETHODIMP AudioChannelAgent::StartPlaying(int32_t *_retval) { - AudioChannelService *service = AudioChannelService::GetAudioChannelService(); + AudioChannelService *service = AudioChannelService::GetOrCreateAudioChannelService(); if (mAudioChannelType == AUDIO_AGENT_CHANNEL_ERROR || service == nullptr || mIsRegToService) { return NS_ERROR_FAILURE; @@ -135,7 +135,7 @@ NS_IMETHODIMP AudioChannelAgent::StopPlaying(void) return NS_ERROR_FAILURE; } - AudioChannelService *service = AudioChannelService::GetAudioChannelService(); + AudioChannelService *service = AudioChannelService::GetOrCreateAudioChannelService(); service->UnregisterAudioChannelAgent(this); mIsRegToService = false; return NS_OK; @@ -150,7 +150,7 @@ NS_IMETHODIMP AudioChannelAgent::SetVisibilityState(bool visible) mVisible = visible; if (mIsRegToService && oldVisibility != mVisible && callback) { - AudioChannelService *service = AudioChannelService::GetAudioChannelService(); + AudioChannelService *service = AudioChannelService::GetOrCreateAudioChannelService(); callback->CanPlayChanged(service->GetState(this, !mVisible)); } return NS_OK; @@ -160,7 +160,7 @@ void AudioChannelAgent::NotifyAudioChannelStateChanged() { nsCOMPtr callback = GetCallback(); if (callback) { - AudioChannelService *service = AudioChannelService::GetAudioChannelService(); + AudioChannelService *service = AudioChannelService::GetOrCreateAudioChannelService(); callback->CanPlayChanged(service->GetState(this, !mVisible)); } } diff --git a/dom/audiochannel/AudioChannelService.cpp b/dom/audiochannel/AudioChannelService.cpp index 286cc3e87424..611d76718493 100644 --- a/dom/audiochannel/AudioChannelService.cpp +++ b/dom/audiochannel/AudioChannelService.cpp @@ -58,6 +58,20 @@ AudioChannelService::GetAudioChannelService() return AudioChannelServiceChild::GetAudioChannelService(); } + return gAudioChannelService; + +} + +// static +AudioChannelService* +AudioChannelService::GetOrCreateAudioChannelService() +{ + MOZ_ASSERT(NS_IsMainThread()); + + if (XRE_GetProcessType() != GeckoProcessType_Default) { + return AudioChannelServiceChild::GetOrCreateAudioChannelService(); + } + // If we already exist, exit early if (gAudioChannelService) { return gAudioChannelService; @@ -65,7 +79,7 @@ AudioChannelService::GetAudioChannelService() // Create new instance, register, return nsRefPtr service = new AudioChannelService(); - NS_ENSURE_TRUE(service, nullptr); + MOZ_ASSERT(service); gAudioChannelService = service; return gAudioChannelService; diff --git a/dom/audiochannel/AudioChannelService.h b/dom/audiochannel/AudioChannelService.h index 544fd38a7049..d3853fa688f1 100644 --- a/dom/audiochannel/AudioChannelService.h +++ b/dom/audiochannel/AudioChannelService.h @@ -36,13 +36,18 @@ public: NS_DECL_NSITIMERCALLBACK /** - * Returns the AudioChannelServce singleton. Only to be called from main - * thread. - * - * @return NS_OK on proper assignment, NS_ERROR_FAILURE otherwise. + * Returns the AudioChannelServce singleton or null if the process havn't create it before. + * Only to be called from main thread. */ static AudioChannelService* GetAudioChannelService(); + /** + * Returns the AudioChannelServce singleton. + * If AudioChannelServce is not exist, create and return new one. + * Only to be called from main thread. + */ + static AudioChannelService* GetOrCreateAudioChannelService(); + /** * Shutdown the singleton. */ diff --git a/dom/audiochannel/AudioChannelServiceChild.cpp b/dom/audiochannel/AudioChannelServiceChild.cpp index 40310fcca087..31348eb21d40 100644 --- a/dom/audiochannel/AudioChannelServiceChild.cpp +++ b/dom/audiochannel/AudioChannelServiceChild.cpp @@ -32,6 +32,16 @@ AudioChannelServiceChild::GetAudioChannelService() { MOZ_ASSERT(NS_IsMainThread()); + return gAudioChannelServiceChild; + +} + +// static +AudioChannelService* +AudioChannelServiceChild::GetOrCreateAudioChannelService() +{ + MOZ_ASSERT(NS_IsMainThread()); + // If we already exist, exit early if (gAudioChannelServiceChild) { return gAudioChannelServiceChild; @@ -39,7 +49,7 @@ AudioChannelServiceChild::GetAudioChannelService() // Create new instance, register, return nsRefPtr service = new AudioChannelServiceChild(); - NS_ENSURE_TRUE(service, nullptr); + MOZ_ASSERT(service); gAudioChannelServiceChild = service; return gAudioChannelServiceChild; diff --git a/dom/audiochannel/AudioChannelServiceChild.h b/dom/audiochannel/AudioChannelServiceChild.h index cf56150e135f..03488f7ab29d 100644 --- a/dom/audiochannel/AudioChannelServiceChild.h +++ b/dom/audiochannel/AudioChannelServiceChild.h @@ -21,13 +21,18 @@ class AudioChannelServiceChild : public AudioChannelService public: /** - * Returns the AudioChannelServce singleton. Only to be called from main - * thread. - * - * @return NS_OK on proper assignment, NS_ERROR_FAILURE otherwise. + * Returns the AudioChannelServce singleton or null if the process havn't create it before. + * Only to be called from main thread. */ static AudioChannelService* GetAudioChannelService(); + /** + * Returns the AudioChannelServce singleton. + * If AudioChannelServce is not exist, create and return new one. + * Only to be called from main thread. + */ + static AudioChannelService* GetOrCreateAudioChannelService(); + static void Shutdown(); virtual void RegisterAudioChannelAgent(AudioChannelAgent* aAgent, diff --git a/dom/base/nsGlobalWindow.cpp b/dom/base/nsGlobalWindow.cpp index 861b9a02c109..b1854b323419 100644 --- a/dom/base/nsGlobalWindow.cpp +++ b/dom/base/nsGlobalWindow.cpp @@ -3797,10 +3797,8 @@ void nsPIDOMWindow::RefreshMediaElements() { nsRefPtr service = - AudioChannelService::GetAudioChannelService(); - if (service) { - service->RefreshAgentsVolume(this); - } + AudioChannelService::GetOrCreateAudioChannelService(); + service->RefreshAgentsVolume(this); } void diff --git a/dom/ipc/ContentParent.cpp b/dom/ipc/ContentParent.cpp index acbdfac3883d..a85aa0f00033 100755 --- a/dom/ipc/ContentParent.cpp +++ b/dom/ipc/ContentParent.cpp @@ -2352,12 +2352,12 @@ ContentParent::RecvAudioChannelGetState(const AudioChannel& aChannel, AudioChannelState* aState) { nsRefPtr service = - AudioChannelService::GetAudioChannelService(); + AudioChannelService::GetOrCreateAudioChannelService(); *aState = AUDIO_CHANNEL_STATE_NORMAL; - if (service) { - *aState = service->GetStateInternal(aChannel, mChildID, - aElementHidden, aElementWasHidden); - } + MOZ_ASSERT(service); + *aState = service->GetStateInternal(aChannel, mChildID, + aElementHidden, aElementWasHidden); + return true; } @@ -2366,10 +2366,10 @@ ContentParent::RecvAudioChannelRegisterType(const AudioChannel& aChannel, const bool& aWithVideo) { nsRefPtr service = - AudioChannelService::GetAudioChannelService(); - if (service) { - service->RegisterType(aChannel, mChildID, aWithVideo); - } + AudioChannelService::GetOrCreateAudioChannelService(); + MOZ_ASSERT(service); + service->RegisterType(aChannel, mChildID, aWithVideo); + return true; } @@ -2379,10 +2379,10 @@ ContentParent::RecvAudioChannelUnregisterType(const AudioChannel& aChannel, const bool& aWithVideo) { nsRefPtr service = - AudioChannelService::GetAudioChannelService(); - if (service) { - service->UnregisterType(aChannel, aElementHidden, mChildID, aWithVideo); - } + AudioChannelService::GetOrCreateAudioChannelService(); + MOZ_ASSERT(service); + service->UnregisterType(aChannel, aElementHidden, mChildID, aWithVideo); + return true; } @@ -2390,10 +2390,10 @@ bool ContentParent::RecvAudioChannelChangedNotification() { nsRefPtr service = - AudioChannelService::GetAudioChannelService(); - if (service) { - service->SendAudioChannelChangedNotification(ChildID()); - } + AudioChannelService::GetOrCreateAudioChannelService(); + MOZ_ASSERT(service); + service->SendAudioChannelChangedNotification(ChildID()); + return true; } @@ -2402,11 +2402,10 @@ ContentParent::RecvAudioChannelChangeDefVolChannel(const int32_t& aChannel, const bool& aHidden) { nsRefPtr service = - AudioChannelService::GetAudioChannelService(); - if (service) { - service->SetDefaultVolumeControlChannelInternal(aChannel, - aHidden, mChildID); - } + AudioChannelService::GetOrCreateAudioChannelService(); + MOZ_ASSERT(service); + service->SetDefaultVolumeControlChannelInternal(aChannel, + aHidden, mChildID); return true; } diff --git a/dom/ipc/ProcessPriorityManager.cpp b/dom/ipc/ProcessPriorityManager.cpp index 4ee176bf4612..ada05a71cf07 100644 --- a/dom/ipc/ProcessPriorityManager.cpp +++ b/dom/ipc/ProcessPriorityManager.cpp @@ -995,7 +995,7 @@ ParticularProcessPriorityManager::ComputePriority() return PROCESS_PRIORITY_BACKGROUND_PERCEIVABLE; } - AudioChannelService* service = AudioChannelService::GetAudioChannelService(); + AudioChannelService* service = AudioChannelService::GetOrCreateAudioChannelService(); if (service->ProcessContentOrNormalChannelIsActive(ChildID())) { return PROCESS_PRIORITY_BACKGROUND_PERCEIVABLE; } diff --git a/dom/speakermanager/SpeakerManager.cpp b/dom/speakermanager/SpeakerManager.cpp index 1f418769a4e6..1506df2f3aa3 100644 --- a/dom/speakermanager/SpeakerManager.cpp +++ b/dom/speakermanager/SpeakerManager.cpp @@ -201,7 +201,7 @@ SpeakerManager::HandleEvent(nsIDOMEvent* aEvent) // the background, we switch 'speakerforced' to false. if (!mVisible && mForcespeaker) { AudioChannelService* audioChannelService = - AudioChannelService::GetAudioChannelService(); + AudioChannelService::GetOrCreateAudioChannelService(); if (audioChannelService && !audioChannelService->AnyAudioChannelIsActive()) { service->ForceSpeaker(false, mVisible); } diff --git a/dom/speakermanager/SpeakerManagerService.cpp b/dom/speakermanager/SpeakerManagerService.cpp index f35a81b150c6..48f998cdfb5e 100644 --- a/dom/speakermanager/SpeakerManagerService.cpp +++ b/dom/speakermanager/SpeakerManagerService.cpp @@ -194,7 +194,7 @@ SpeakerManagerService::SpeakerManagerService() } } AudioChannelService* audioChannelService = - AudioChannelService::GetAudioChannelService(); + AudioChannelService::GetOrCreateAudioChannelService(); if (audioChannelService) { audioChannelService->RegisterSpeakerManager(this); } @@ -204,7 +204,7 @@ SpeakerManagerService::~SpeakerManagerService() { MOZ_COUNT_DTOR(SpeakerManagerService); AudioChannelService* audioChannelService = - AudioChannelService::GetAudioChannelService(); + AudioChannelService::GetOrCreateAudioChannelService(); if (audioChannelService) audioChannelService->UnregisterSpeakerManager(this); } diff --git a/dom/speakermanager/SpeakerManagerServiceChild.cpp b/dom/speakermanager/SpeakerManagerServiceChild.cpp index a819d6fdadfa..cb066d1e3f65 100644 --- a/dom/speakermanager/SpeakerManagerServiceChild.cpp +++ b/dom/speakermanager/SpeakerManagerServiceChild.cpp @@ -96,7 +96,7 @@ SpeakerManagerServiceChild::SetAudioChannelActive(bool aIsActive) SpeakerManagerServiceChild::SpeakerManagerServiceChild() { MOZ_ASSERT(NS_IsMainThread()); - AudioChannelService* audioChannelService = AudioChannelService::GetAudioChannelService(); + AudioChannelService* audioChannelService = AudioChannelService::GetOrCreateAudioChannelService(); if (audioChannelService) { audioChannelService->RegisterSpeakerManager(this); } @@ -105,7 +105,7 @@ SpeakerManagerServiceChild::SpeakerManagerServiceChild() SpeakerManagerServiceChild::~SpeakerManagerServiceChild() { - AudioChannelService* audioChannelService = AudioChannelService::GetAudioChannelService(); + AudioChannelService* audioChannelService = AudioChannelService::GetOrCreateAudioChannelService(); if (audioChannelService) { audioChannelService->UnregisterSpeakerManager(this); } diff --git a/dom/system/gonk/AudioChannelManager.cpp b/dom/system/gonk/AudioChannelManager.cpp index 50cae6c89b44..462a8d189da3 100644 --- a/dom/system/gonk/AudioChannelManager.cpp +++ b/dom/system/gonk/AudioChannelManager.cpp @@ -132,7 +132,7 @@ AudioChannelManager::NotifyVolumeControlChannelChanged() bool isActive = false; docshell->GetIsActive(&isActive); - AudioChannelService* service = AudioChannelService::GetAudioChannelService(); + AudioChannelService* service = AudioChannelService::GetOrCreateAudioChannelService(); if (isActive) { service->SetDefaultVolumeControlChannel(mVolumeChannel, isActive); } else { diff --git a/dom/system/gonk/AudioManager.cpp b/dom/system/gonk/AudioManager.cpp index 82517df7a7e7..a86a4b47c761 100644 --- a/dom/system/gonk/AudioManager.cpp +++ b/dom/system/gonk/AudioManager.cpp @@ -316,8 +316,8 @@ AudioManager::HandleAudioChannelProcessChanged() return; } - AudioChannelService *service = AudioChannelService::GetAudioChannelService(); - NS_ENSURE_TRUE_VOID(service); + AudioChannelService *service = AudioChannelService::GetOrCreateAudioChannelService(); + MOZ_ASSERT(service); bool telephonyChannelIsActive = service->TelephonyChannelIsActive(); telephonyChannelIsActive ? SetPhoneState(PHONE_STATE_IN_COMMUNICATION) : diff --git a/layout/build/nsLayoutModule.cpp b/layout/build/nsLayoutModule.cpp index 2e53f8285e39..c465ab1e70c9 100644 --- a/layout/build/nsLayoutModule.cpp +++ b/layout/build/nsLayoutModule.cpp @@ -590,7 +590,7 @@ NS_GENERIC_FACTORY_CONSTRUCTOR_INIT(Geolocation, Init) NS_GENERIC_FACTORY_SINGLETON_CONSTRUCTOR(nsGeolocationService, nsGeolocationService::GetGeolocationService) -NS_GENERIC_FACTORY_SINGLETON_CONSTRUCTOR(AudioChannelService, AudioChannelService::GetAudioChannelService) +NS_GENERIC_FACTORY_SINGLETON_CONSTRUCTOR(AudioChannelService, AudioChannelService::GetOrCreateAudioChannelService) NS_GENERIC_FACTORY_SINGLETON_CONSTRUCTOR(DataStoreService, DataStoreService::GetOrCreate)