Bug 1080863 - Make nsPicoService an observer and initialize on profile-after-change. r=smaug
--- a/content/media/webspeech/synth/pico/nsPicoService.cpp
+++ b/content/media/webspeech/synth/pico/nsPicoService.cpp
@@ -406,52 +406,51 @@ NS_IMETHODIMP
PicoCallbackRunnable::OnCancel()
{
mService->mCurrentTask = nullptr;
return NS_OK;
}
NS_INTERFACE_MAP_BEGIN(nsPicoService)
NS_INTERFACE_MAP_ENTRY(nsISpeechService)
- NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsISpeechService)
+ NS_INTERFACE_MAP_ENTRY(nsIObserver)
+ NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIObserver)
NS_INTERFACE_MAP_END
NS_IMPL_ADDREF(nsPicoService)
NS_IMPL_RELEASE(nsPicoService)
-nsPicoService::nsPicoService()
- : mInitialized(false)
- , mVoicesMonitor("nsPicoService::mVoices")
- , mCurrentTask(nullptr)
- , mPicoSystem(nullptr)
- , mPicoEngine(nullptr)
- , mSgResource(nullptr)
- , mTaResource(nullptr)
- , mPicoMemArea(nullptr)
-{
- DebugOnly<nsresult> rv = NS_NewNamedThread("Pico Worker", getter_AddRefs(mThread));
- MOZ_ASSERT(NS_SUCCEEDED(rv));
- rv = mThread->Dispatch(NS_NewRunnableMethod(this, &nsPicoService::Init), NS_DISPATCH_NORMAL);
- MOZ_ASSERT(NS_SUCCEEDED(rv));
-}
-
nsPicoService::~nsPicoService()
{
// We don't worry about removing the voices because this gets
// destructed at shutdown along with the voice registry.
MonitorAutoLock autoLock(mVoicesMonitor);
mVoices.Clear();
if (mThread) {
mThread->Shutdown();
}
UnloadEngine();
}
+// nsIObserver
+
+NS_IMETHODIMP
+nsPicoService::Observe(nsISupports* aSubject, const char* aTopic,
+ const char16_t* aData)
+{
+ MOZ_ASSERT(NS_IsMainThread());
+ NS_ENSURE_TRUE(!strcmp(aTopic, "profile-after-change"), NS_ERROR_UNEXPECTED);
+
+ DebugOnly<nsresult> rv = NS_NewNamedThread("Pico Worker", getter_AddRefs(mThread));
+ MOZ_ASSERT(NS_SUCCEEDED(rv));
+ return mThread->Dispatch(
+ NS_NewRunnableMethod(this, &nsPicoService::Init), NS_DISPATCH_NORMAL);
+}
// nsISpeechService
NS_IMETHODIMP
nsPicoService::Speak(const nsAString& aText, const nsAString& aUri,
float aRate, float aPitch, nsISpeechTask* aTask)
{
NS_ENSURE_TRUE(mInitialized, NS_ERROR_NOT_AVAILABLE);
--- a/content/media/webspeech/synth/pico/nsPicoService.h
+++ b/content/media/webspeech/synth/pico/nsPicoService.h
@@ -5,42 +5,43 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#ifndef nsPicoService_h
#define nsPicoService_h
#include "mozilla/Mutex.h"
#include "nsAutoPtr.h"
#include "nsTArray.h"
+#include "nsIObserver.h"
#include "nsIThread.h"
#include "nsISpeechService.h"
#include "nsRefPtrHashtable.h"
#include "mozilla/StaticPtr.h"
#include "mozilla/Monitor.h"
namespace mozilla {
namespace dom {
class PicoVoice;
class PicoCallbackRunnable;
typedef void* pico_System;
typedef void* pico_Resource;
typedef void* pico_Engine;
-class nsPicoService : public nsISpeechService
+class nsPicoService : public nsIObserver,
+ public nsISpeechService
{
friend class PicoCallbackRunnable;
friend class PicoInitRunnable;
public:
NS_DECL_THREADSAFE_ISUPPORTS
NS_DECL_NSISPEECHSERVICE
-
- nsPicoService();
+ NS_DECL_NSIOBSERVER
static nsPicoService* GetInstance();
static already_AddRefed<nsPicoService> GetInstanceForService();
static void Shutdown();
private:
@@ -77,14 +78,25 @@ private:
pico_Resource mSgResource;
pico_Resource mTaResource;
nsAutoPtr<uint8_t> mPicoMemArea;
static StaticRefPtr<nsPicoService> sSingleton;
+
+protected:
+ nsPicoService() : mInitialized(false)
+ , mVoicesMonitor("nsPicoService::mVoices")
+ , mCurrentTask(nullptr)
+ , mPicoSystem(nullptr)
+ , mPicoEngine(nullptr)
+ , mSgResource(nullptr)
+ , mTaResource(nullptr)
+ , mPicoMemArea(nullptr)
+ {
+ }
};
-
} // namespace dom
} // namespace mozilla
#endif