Bug 884407 - Use an enum in SpeechRecognitionError. r=smaug

This commit is contained in:
Guilherme Gonçalves 2013-06-25 20:08:58 -04:00
parent 33e78dc0da
commit a764e011fd
11 changed files with 153 additions and 78 deletions

View File

@ -0,0 +1,50 @@
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "nsDOMClassInfoID.h"
#include "SpeechRecognitionError.h"
namespace mozilla {
namespace dom {
SpeechRecognitionError::SpeechRecognitionError(mozilla::dom::EventTarget* aOwner, nsPresContext* aPresContext, nsEvent* aEvent)
: nsDOMEvent(aOwner, aPresContext, aEvent),
mError()
{}
SpeechRecognitionError::~SpeechRecognitionError() {}
already_AddRefed<SpeechRecognitionError>
SpeechRecognitionError::Constructor(const GlobalObject& aGlobal,
const nsAString& aType,
const SpeechRecognitionErrorInit& aParam,
ErrorResult& aRv)
{
nsCOMPtr<mozilla::dom::EventTarget> t = do_QueryInterface(aGlobal.Get());
nsRefPtr<SpeechRecognitionError> e = new SpeechRecognitionError(t, nullptr, nullptr);
bool trusted = e->Init(t);
e->InitSpeechRecognitionError(aType, aParam.mBubbles, aParam.mCancelable, aParam.mError, aParam.mMessage, aRv);
e->SetTrusted(trusted);
return e.forget();
}
void
SpeechRecognitionError::InitSpeechRecognitionError(const nsAString& aType,
bool aCanBubble,
bool aCancelable,
SpeechRecognitionErrorCode aError,
const nsAString& aMessage,
ErrorResult& aRv)
{
aRv = nsDOMEvent::InitEvent(aType, aCanBubble, aCancelable);
NS_ENSURE_SUCCESS_VOID(aRv.ErrorCode());
mError = aError;
mMessage = aMessage;
return;
}
} // namespace dom
} // namespace mozilla

View File

@ -0,0 +1,61 @@
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#ifndef SpeechRecognitionError_h__
#define SpeechRecognitionError_h__
#include "nsDOMEvent.h"
#include "mozilla/dom/SpeechRecognitionErrorBinding.h"
namespace mozilla {
namespace dom {
class SpeechRecognitionError : public nsDOMEvent
{
public:
SpeechRecognitionError(mozilla::dom::EventTarget* aOwner,
nsPresContext* aPresContext,
nsEvent* aEvent);
virtual ~SpeechRecognitionError();
static already_AddRefed<SpeechRecognitionError> Constructor(const GlobalObject& aGlobal,
const nsAString& aType,
const SpeechRecognitionErrorInit& aParam,
ErrorResult& aRv);
virtual JSObject* WrapObject(JSContext* aCx, JS::Handle<JSObject*> aScope) MOZ_OVERRIDE
{
return mozilla::dom::SpeechRecognitionErrorBinding::Wrap(aCx, aScope, this);
}
void
GetMessage(nsAString& aString)
{
aString = mMessage;
}
SpeechRecognitionErrorCode
Error()
{
return mError;
}
void
InitSpeechRecognitionError(const nsAString& aType,
bool aCanBubble,
bool aCancelable,
SpeechRecognitionErrorCode aError,
const nsAString& aMessage,
ErrorResult& aRv);
protected:
SpeechRecognitionErrorCode mError;
nsString mMessage;
};
}
}
#endif // SpeechRecognitionError_h__

View File

@ -16,6 +16,7 @@ EXPORTS += [
]
EXPORTS.mozilla.dom += [
'SpeechRecognitionError.h',
'Touch.h',
]
@ -59,4 +60,5 @@ CPP_SOURCES += [
'nsIMEStateManager.cpp',
'nsPaintRequest.cpp',
'nsPrivateTextRange.cpp',
'SpeechRecognitionError.cpp',
]

View File

@ -521,11 +521,10 @@ SpeechRecognition::AbortError(SpeechEvent* aEvent)
void
SpeechRecognition::NotifyError(SpeechEvent* aEvent)
{
nsCOMPtr<nsIDOMEvent> domEvent = do_QueryInterface(aEvent->mError);
domEvent->SetTrusted(true);
aEvent->mError->SetTrusted(true);
bool defaultActionEnabled;
this->DispatchEvent(domEvent, &defaultActionEnabled);
this->DispatchEvent(aEvent->mError, &defaultActionEnabled);
return;
}
@ -576,7 +575,7 @@ SpeechRecognition::Observe(nsISupports* aSubject, const char* aTopic,
StateBetween(STATE_IDLE, STATE_WAITING_FOR_SPEECH)) {
DispatchError(SpeechRecognition::EVENT_AUDIO_ERROR,
nsIDOMSpeechRecognitionError::NO_SPEECH,
SpeechRecognitionErrorCode::No_speech,
NS_LITERAL_STRING("No speech detected (timeout)"));
} else if (!strcmp(aTopic, SPEECH_RECOGNITION_TEST_END_TOPIC)) {
nsCOMPtr<nsIObserverService> obs = services::GetObserverService();
@ -602,7 +601,7 @@ SpeechRecognition::ProcessTestEventRequest(nsISupports* aSubject, const nsAStrin
Abort();
} else if (aEventName.EqualsLiteral("EVENT_AUDIO_ERROR")) {
DispatchError(SpeechRecognition::EVENT_AUDIO_ERROR,
nsIDOMSpeechRecognitionError::AUDIO_CAPTURE, // TODO different codes?
SpeechRecognitionErrorCode::Audio_capture, // TODO different codes?
NS_LITERAL_STRING("AUDIO_ERROR test event"));
} else if (aEventName.EqualsLiteral("EVENT_AUDIO_DATA")) {
StartRecording(static_cast<DOMMediaStream*>(aSubject));
@ -748,19 +747,21 @@ SpeechRecognition::Abort()
}
void
SpeechRecognition::DispatchError(EventType aErrorType, int aErrorCode,
SpeechRecognition::DispatchError(EventType aErrorType,
SpeechRecognitionErrorCode aErrorCode,
const nsAString& aMessage)
{
MOZ_ASSERT(NS_IsMainThread());
MOZ_ASSERT(aErrorType == EVENT_RECOGNITIONSERVICE_ERROR ||
aErrorType == EVENT_AUDIO_ERROR, "Invalid error type!");
nsCOMPtr<nsIDOMEvent> domEvent;
NS_NewDOMSpeechRecognitionError(getter_AddRefs(domEvent), nullptr, nullptr, nullptr);
nsRefPtr<SpeechRecognitionError> srError =
new SpeechRecognitionError(nullptr, nullptr, nullptr);
nsCOMPtr<nsIDOMSpeechRecognitionError> srError = do_QueryInterface(domEvent);
ErrorResult err;
srError->InitSpeechRecognitionError(NS_LITERAL_STRING("error"), true, false,
aErrorCode, aMessage);
aErrorCode, aMessage, err);
nsRefPtr<SpeechEvent> event = new SpeechEvent(this, aErrorType);
event->mError = srError;
NS_DispatchToMainThread(event);
@ -1002,12 +1003,12 @@ NS_IMPL_ISUPPORTS1(SpeechRecognition::GetUserMediaErrorCallback, nsIDOMGetUserMe
NS_IMETHODIMP
SpeechRecognition::GetUserMediaErrorCallback::OnError(const nsAString& aError)
{
int errorCode;
SpeechRecognitionErrorCode errorCode;
if (aError.Equals(NS_LITERAL_STRING("PERMISSION_DENIED"))) {
errorCode = nsIDOMSpeechRecognitionError::NOT_ALLOWED;
errorCode = SpeechRecognitionErrorCode::Not_allowed;
} else {
errorCode = nsIDOMSpeechRecognitionError::AUDIO_CAPTURE;
errorCode = SpeechRecognitionErrorCode::Audio_capture;
}
mRecognition->DispatchError(SpeechRecognition::EVENT_AUDIO_ERROR, errorCode,

View File

@ -28,7 +28,7 @@
#include "nsISpeechRecognitionService.h"
#include "endpointer.h"
#include "nsIDOMSpeechRecognitionError.h"
#include "mozilla/dom/SpeechRecognitionError.h"
struct JSContext;
class nsIDOMWindow;
@ -127,7 +127,7 @@ public:
EVENT_COUNT
};
void DispatchError(EventType aErrorType, int aErrorCode, const nsAString& aMessage);
void DispatchError(EventType aErrorType, SpeechRecognitionErrorCode aErrorCode, const nsAString& aMessage);
uint32_t FillSamplesBuffer(const int16_t* aSamples, uint32_t aSampleCount);
uint32_t SplitSamplesBuffer(const int16_t* aSamplesBuffer, uint32_t aSampleCount, nsTArray<already_AddRefed<SharedBuffer> >& aResult);
AudioSegment* CreateAudioSegment(nsTArray<already_AddRefed<SharedBuffer> >& aChunks);
@ -284,7 +284,7 @@ public:
NS_IMETHOD Run() MOZ_OVERRIDE;
AudioSegment* mAudioSegment;
nsRefPtr<SpeechRecognitionResultList> mRecognitionResultList; // TODO: make this a session being passed which also has index and stuff
nsCOMPtr<nsIDOMSpeechRecognitionError> mError;
nsRefPtr<SpeechRecognitionError> mError;
friend class SpeechRecognition;
private:

View File

@ -8,7 +8,6 @@ MODULE = 'content'
XPIDL_MODULE = 'dom_webspeechrecognition'
XPIDL_SOURCES = [
'nsIDOMSpeechRecognitionError.idl',
'nsIDOMSpeechRecognitionEvent.idl',
'nsISpeechRecognitionService.idl'
]

View File

@ -1,39 +0,0 @@
/* -*- Mode: IDL; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/.
*
* The origin of this IDL file is
* http://dvcs.w3.org/hg/speech-api/raw-file/tip/speechapi.html
*
* Copyright © 2012 W3C® (MIT, ERCIM, Keio), All Rights Reserved. W3C
* liability, trademark and document use rules apply.
*/
#include "nsIDOMEvent.idl"
[scriptable, builtinclass, uuid(5ddc5a46-e7db-4c5c-8ed4-80cf5d88fca3)]
interface nsIDOMSpeechRecognitionError : nsIDOMEvent {
const unsigned long NO_SPEECH = 0;
const unsigned long ABORTED = 1;
const unsigned long AUDIO_CAPTURE = 2;
const unsigned long NETWORK = 3;
const unsigned long NOT_ALLOWED = 4;
const unsigned long SERVICE_NOT_ALLOWED = 5;
const unsigned long BAD_GRAMMAR = 6;
const unsigned long LANGUAGE_NOT_SUPPORTED = 7;
[noscript] void initSpeechRecognitionError(in DOMString eventTypeArg,
in boolean canBubbleArg,
in boolean cancelableArg,
in unsigned long error,
in DOMString message);
readonly attribute unsigned long error;
readonly attribute DOMString message;
};
dictionary SpeechRecognitionErrorInit : EventInit {
unsigned long error;
DOMString message;
};

View File

@ -74,7 +74,7 @@ FakeSpeechRecognitionService::Observe(nsISupports* aSubject, const char* aTopic,
if (eventName.EqualsLiteral("EVENT_RECOGNITIONSERVICE_ERROR")) {
mRecognition->DispatchError(SpeechRecognition::EVENT_RECOGNITIONSERVICE_ERROR,
nsIDOMSpeechRecognitionError::NETWORK, // TODO different codes?
SpeechRecognitionErrorCode::Network, // TODO different codes?
NS_LITERAL_STRING("RECOGNITIONSERVICE_ERROR test event"));
} else if (eventName.EqualsLiteral("EVENT_RECOGNITIONSERVICE_FINAL_RESULT")) {

View File

@ -5,14 +5,14 @@ const SPEECH_RECOGNITION_TEST_REQUEST_EVENT_TOPIC = "SpeechRecognitionTest:Reque
const SPEECH_RECOGNITION_TEST_END_TOPIC = "SpeechRecognitionTest:End";
var errorCodes = {
NO_SPEECH : 0,
ABORTED : 1,
AUDIO_CAPTURE : 2,
NETWORK : 3,
NOT_ALLOWED : 4,
SERVICE_NOT_ALLOWED : 5,
BAD_GRAMMAR : 6,
LANGUAGE_NOT_SUPPORTED : 7
NO_SPEECH : "no-speech",
ABORTED : "aborted",
AUDIO_CAPTURE : "audio-capture",
NETWORK : "network",
NOT_ALLOWED : "not-allowed",
SERVICE_NOT_ALLOWED : "service-not-allowed",
BAD_GRAMMAR : "bad-grammar",
LANGUAGE_NOT_SUPPORTED : "language-not-supported"
};
netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");

View File

@ -4,24 +4,26 @@
* You can obtain one at http://mozilla.org/MPL/2.0/.
*/
[Constructor(DOMString type, optional SpeechRecognitionErrorInit eventInitDict), HeaderFile="GeneratedEventClasses.h"]
enum SpeechRecognitionErrorCode {
"no-speech",
"aborted",
"audio-capture",
"network",
"not-allowed",
"service-not-allowed",
"bad-grammar",
"language-not-supported"
};
[Constructor(DOMString type, optional SpeechRecognitionErrorInit eventInitDict)]
interface SpeechRecognitionError : Event
{
const unsigned long NO_SPEECH = 0;
const unsigned long ABORTED = 1;
const unsigned long AUDIO_CAPTURE = 2;
const unsigned long NETWORK = 3;
const unsigned long NOT_ALLOWED = 4;
const unsigned long SERVICE_NOT_ALLOWED = 5;
const unsigned long BAD_GRAMMAR = 6;
const unsigned long LANGUAGE_NOT_SUPPORTED = 7;
readonly attribute unsigned long error;
readonly attribute SpeechRecognitionErrorCode error;
readonly attribute DOMString? message;
};
dictionary SpeechRecognitionErrorInit : EventInit
{
unsigned long error = 0;
SpeechRecognitionErrorCode error = "no-speech";
DOMString message = "";
};

View File

@ -57,7 +57,6 @@ simple_events = [
#endif
#ifdef MOZ_WEBSPEECH
'SpeechRecognitionEvent',
'SpeechRecognitionError',
#endif
]