Bug 1060659 - SpeechGrammarList should both initialize SpeechRecognitionService and set a grammar on it. r=smaug

This commit is contained in:
Andre Natal 2014-10-01 21:37:00 +02:00
parent c3993f77b6
commit a670476201
8 changed files with 81 additions and 53 deletions

View File

@ -8,6 +8,9 @@
#include "mozilla/dom/SpeechGrammarListBinding.h"
#include "mozilla/ErrorResult.h"
#include "nsCOMPtr.h"
#include "nsXPCOMStrings.h"
#include "SpeechRecognition.h"
namespace mozilla {
namespace dom {
@ -20,9 +23,10 @@ NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(SpeechGrammarList)
NS_INTERFACE_MAP_ENTRY(nsISupports)
NS_INTERFACE_MAP_END
SpeechGrammarList::SpeechGrammarList(nsISupports* aParent)
SpeechGrammarList::SpeechGrammarList(nsISupports* aParent, nsISpeechRecognitionService* aRecognitionService)
: mParent(aParent)
{
this->mRecognitionService = aRecognitionService;
SetIsDOMBinding();
}
@ -30,11 +34,20 @@ SpeechGrammarList::~SpeechGrammarList()
{
}
SpeechGrammarList*
already_AddRefed<SpeechGrammarList>
SpeechGrammarList::Constructor(const GlobalObject& aGlobal,
ErrorResult& aRv)
{
return new SpeechGrammarList(aGlobal.GetAsSupports());
nsCOMPtr<nsISpeechRecognitionService> recognitionService;
recognitionService = GetSpeechRecognitionService();
if (!recognitionService) {
aRv.Throw(NS_ERROR_NOT_AVAILABLE);
return nullptr;
} else {
nsRefPtr<SpeechGrammarList> speechGrammarList =
new SpeechGrammarList(aGlobal.GetAsSupports(), recognitionService);
return speechGrammarList.forget();
}
}
JSObject*
@ -76,7 +89,7 @@ SpeechGrammarList::AddFromString(const nsAString& aString,
const Optional<float>& aWeight,
ErrorResult& aRv)
{
aRv.Throw(NS_ERROR_NOT_IMPLEMENTED);
mRecognitionService->ValidateAndSetGrammarList(this, nullptr);
return;
}

View File

@ -11,6 +11,7 @@
#include "nsCOMPtr.h"
#include "nsCycleCollectionParticipant.h"
#include "nsWrapperCache.h"
#include "nsISpeechRecognitionService.h"
struct JSContext;
@ -28,13 +29,12 @@ class SpeechGrammarList MOZ_FINAL : public nsISupports,
public nsWrapperCache
{
public:
explicit SpeechGrammarList(nsISupports* aParent);
explicit SpeechGrammarList(nsISupports* aParent, nsISpeechRecognitionService* aRecognitionService);
NS_DECL_CYCLE_COLLECTING_ISUPPORTS
NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(SpeechGrammarList)
SpeechGrammarList* Constructor(const GlobalObject& aGlobal,
ErrorResult& aRv);
static already_AddRefed<SpeechGrammarList> Constructor(const GlobalObject& aGlobal, ErrorResult& aRv);
nsISupports* GetParentObject() const;
@ -50,6 +50,8 @@ public:
already_AddRefed<SpeechGrammar> IndexedGetter(uint32_t aIndex, bool& aPresent, ErrorResult& aRv);
nsCOMPtr<nsISpeechRecognitionService> mRecognitionService;
private:
~SpeechGrammarList();

View File

@ -57,6 +57,36 @@ GetSpeechRecognitionLog()
#define SR_LOG(...)
#endif
already_AddRefed<nsISpeechRecognitionService>
GetSpeechRecognitionService()
{
nsAutoCString speechRecognitionServiceCID;
nsAdoptingCString prefValue =
Preferences::GetCString(PREFERENCE_DEFAULT_RECOGNITION_SERVICE);
nsAutoCString speechRecognitionService;
if (!prefValue.get() || prefValue.IsEmpty()) {
speechRecognitionService = DEFAULT_RECOGNITION_SERVICE;
} else {
speechRecognitionService = prefValue;
}
if (!SpeechRecognition::mTestConfig.mFakeRecognitionService){
speechRecognitionServiceCID =
NS_LITERAL_CSTRING(NS_SPEECH_RECOGNITION_SERVICE_CONTRACTID_PREFIX) +
speechRecognitionService;
} else {
speechRecognitionServiceCID =
NS_SPEECH_RECOGNITION_SERVICE_CONTRACTID_PREFIX "fake";
}
nsresult aRv;
nsCOMPtr<nsISpeechRecognitionService> recognitionService;
recognitionService = do_GetService(speechRecognitionServiceCID.get(), &aRv);
return recognitionService.forget();
}
NS_INTERFACE_MAP_BEGIN(SpeechRecognition)
NS_INTERFACE_MAP_ENTRY(nsIObserver)
NS_INTERFACE_MAP_END_INHERITING(DOMEventTargetHelper)
@ -328,33 +358,6 @@ SpeechRecognition::ProcessAudioSegment(AudioSegment* aSegment, TrackRate aTrackR
return samples;
}
void
SpeechRecognition::GetRecognitionServiceCID(nsACString& aResultCID)
{
if (mTestConfig.mFakeRecognitionService) {
aResultCID =
NS_SPEECH_RECOGNITION_SERVICE_CONTRACTID_PREFIX "fake";
return;
}
nsAdoptingCString prefValue =
Preferences::GetCString(PREFERENCE_DEFAULT_RECOGNITION_SERVICE);
nsAutoCString speechRecognitionService;
if (!prefValue.get() || prefValue.IsEmpty()) {
speechRecognitionService = DEFAULT_RECOGNITION_SERVICE;
} else {
speechRecognitionService = prefValue;
}
aResultCID =
NS_LITERAL_CSTRING(NS_SPEECH_RECOGNITION_SERVICE_CONTRACTID_PREFIX) +
speechRecognitionService;
return;
}
/****************************************************************************
* FSM Transition functions
*
@ -691,13 +694,10 @@ SpeechRecognition::Start(const Optional<NonNull<DOMMediaStream>>& aStream, Error
return;
}
nsAutoCString speechRecognitionServiceCID;
GetRecognitionServiceCID(speechRecognitionServiceCID);
mRecognitionService = GetSpeechRecognitionService();
NS_ENSURE_TRUE_VOID(mRecognitionService);
nsresult rv;
mRecognitionService = do_GetService(speechRecognitionServiceCID.get(), &rv);
NS_ENSURE_SUCCESS_VOID(rv);
rv = mRecognitionService->Initialize(this);
NS_ENSURE_SUCCESS_VOID(rv);

View File

@ -53,6 +53,8 @@ PRLogModuleInfo* GetSpeechRecognitionLog();
#define SR_LOG(...)
#endif
already_AddRefed<nsISpeechRecognitionService> GetSpeechRecognitionService();
class SpeechRecognition MOZ_FINAL : public DOMEventTargetHelper,
public nsIObserver,
public SupportsWeakPtr<SpeechRecognition>
@ -234,8 +236,6 @@ private:
nsRefPtr<SpeechStreamListener> mSpeechListener;
nsCOMPtr<nsISpeechRecognitionService> mRecognitionService;
void GetRecognitionServiceCID(nsACString& aResultCID);
FSMState mCurrentState;
Endpointer mEndpointer;

View File

@ -7,15 +7,33 @@
%{C++
#include "mozilla/WeakPtr.h"
namespace mozilla {
class AudioSegment;
namespace dom {
class SpeechRecognition;
class SpeechRecognitionResultList;
class SpeechGrammarList;
class SpeechGrammar;
}
}
%}
native SpeechRecognitionWeakPtr(mozilla::WeakPtr<mozilla::dom::SpeechRecognition>);
[ptr] native AudioSegmentPtr(mozilla::AudioSegment);
[ptr] native SpeechGrammarListPtr(mozilla::dom::SpeechGrammarList);
[ptr] native SpeechGrammarPtr(mozilla::dom::SpeechGrammar);
[uuid(374583f0-4507-11e4-a183-164230d1df67)]
interface nsISpeechGrammarCompilationCallback : nsISupports {
void grammarCompilationEnd(in SpeechGrammarPtr grammarObject, in boolean success);
};
[uuid(857f3fa2-a980-4d3e-a959-a2f53af74232)]
interface nsISpeechRecognitionService : nsISupports {
void initialize(in SpeechRecognitionWeakPtr aSpeechRecognition);
void processAudioSegment(in AudioSegmentPtr aAudioSegment, in long aSampleRate);
void validateAndSetGrammarList(in SpeechGrammarListPtr aSpeechGramarList, in nsISpeechGrammarCompilationCallback aCallback);
void soundEnd();
void abort();
};

View File

@ -51,6 +51,12 @@ FakeSpeechRecognitionService::SoundEnd()
return NS_OK;
}
NS_IMETHODIMP
FakeSpeechRecognitionService::ValidateAndSetGrammarList(mozilla::dom::SpeechGrammarList*, nsISpeechGrammarCompilationCallback*)
{
return NS_OK;
}
NS_IMETHODIMP
FakeSpeechRecognitionService::Abort()
{

View File

@ -9,17 +9,6 @@
#include "nsCOMPtr.h"
#include "nsIObserver.h"
// nsISpeechRecognitionService needs these declarations
namespace mozilla {
class AudioSegment;
namespace dom {
class SpeechRecognition;
class SpeechRecognitionResultList;
}
}
#include "nsISpeechRecognitionService.h"
#define NS_FAKE_SPEECH_RECOGNITION_SERVICE_CID \

View File

@ -10,7 +10,7 @@
* liability, trademark and document use rules apply.
*/
[Pref="media.webspeech.recognition.enable"]
[Constructor, Pref="media.webspeech.recognition.enable"]
interface SpeechGrammarList {
readonly attribute unsigned long length;
[Throws]