Home | History | Annotate | Download | only in speech
      1 /*
      2  * Copyright (C) 2012 Google Inc. All rights reserved.
      3  *
      4  * Redistribution and use in source and binary forms, with or without
      5  * modification, are permitted provided that the following conditions
      6  * are met:
      7  *  * Redistributions of source code must retain the above copyright
      8  *    notice, this list of conditions and the following disclaimer.
      9  *  * Redistributions in binary form must reproduce the above copyright
     10  *    notice, this list of conditions and the following disclaimer in the
     11  *    documentation and/or other materials provided with the distribution.
     12  *
     13  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
     14  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     16  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
     17  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
     18  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     19  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
     20  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
     21  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     23  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     24  */
     25 
     26 #ifndef SpeechRecognition_h
     27 #define SpeechRecognition_h
     28 
     29 #include "bindings/v8/ScriptWrappable.h"
     30 #include "core/dom/ActiveDOMObject.h"
     31 #include "modules/EventTargetModules.h"
     32 #include "modules/speech/SpeechGrammarList.h"
     33 #include "modules/speech/SpeechRecognitionResult.h"
     34 #include "platform/heap/Handle.h"
     35 #include "wtf/Compiler.h"
     36 #include "wtf/text/WTFString.h"
     37 
     38 namespace WebCore {
     39 
     40 class ExceptionState;
     41 class ExecutionContext;
     42 class SpeechRecognitionController;
     43 class SpeechRecognitionError;
     44 
     45 class SpeechRecognition FINAL : public RefCountedGarbageCollectedWillBeGarbageCollectedFinalized<SpeechRecognition>, public ScriptWrappable, public ActiveDOMObject, public EventTargetWithInlineData {
     46     DEFINE_EVENT_TARGET_REFCOUNTING_WILL_BE_REMOVED(RefCountedGarbageCollected<SpeechRecognition>);
     47     WILL_BE_USING_GARBAGE_COLLECTED_MIXIN(SpeechRecognition);
     48 public:
     49     static SpeechRecognition* create(ExecutionContext*);
     50     virtual ~SpeechRecognition();
     51 
     52     // Attributes.
     53     SpeechGrammarList* grammars() { return m_grammars; }
     54     void setGrammars(SpeechGrammarList* grammars) { m_grammars = grammars; }
     55     String lang() { return m_lang; }
     56     void setLang(const String& lang) { m_lang = lang; }
     57     bool continuous() { return m_continuous; }
     58     void setContinuous(bool continuous) { m_continuous = continuous; }
     59     bool interimResults() { return m_interimResults; }
     60     void setInterimResults(bool interimResults) { m_interimResults = interimResults; }
     61     unsigned long maxAlternatives() { return m_maxAlternatives; }
     62     void setMaxAlternatives(unsigned long maxAlternatives) { m_maxAlternatives = maxAlternatives; }
     63 
     64     // Callable by the user.
     65     void start(ExceptionState&);
     66     void stopFunction();
     67     void abort();
     68 
     69     // Called by the SpeechRecognitionClient.
     70     void didStartAudio();
     71     void didStartSound();
     72     void didStartSpeech();
     73     void didEndSpeech();
     74     void didEndSound();
     75     void didEndAudio();
     76     void didReceiveResults(const HeapVector<Member<SpeechRecognitionResult> >& newFinalResults, const HeapVector<Member<SpeechRecognitionResult> >& currentInterimResults);
     77     void didReceiveNoMatch(SpeechRecognitionResult*);
     78     void didReceiveError(PassRefPtrWillBeRawPtr<SpeechRecognitionError>);
     79     void didStart();
     80     void didEnd();
     81 
     82     // EventTarget.
     83     virtual const AtomicString& interfaceName() const OVERRIDE;
     84     virtual ExecutionContext* executionContext() const OVERRIDE;
     85 
     86     // ActiveDOMObject.
     87     virtual bool hasPendingActivity() const OVERRIDE;
     88     virtual void stop() OVERRIDE;
     89 
     90     DEFINE_ATTRIBUTE_EVENT_LISTENER(audiostart);
     91     DEFINE_ATTRIBUTE_EVENT_LISTENER(soundstart);
     92     DEFINE_ATTRIBUTE_EVENT_LISTENER(speechstart);
     93     DEFINE_ATTRIBUTE_EVENT_LISTENER(speechend);
     94     DEFINE_ATTRIBUTE_EVENT_LISTENER(soundend);
     95     DEFINE_ATTRIBUTE_EVENT_LISTENER(audioend);
     96     DEFINE_ATTRIBUTE_EVENT_LISTENER(result);
     97     DEFINE_ATTRIBUTE_EVENT_LISTENER(nomatch);
     98     DEFINE_ATTRIBUTE_EVENT_LISTENER(error);
     99     DEFINE_ATTRIBUTE_EVENT_LISTENER(start);
    100     DEFINE_ATTRIBUTE_EVENT_LISTENER(end);
    101 
    102     virtual void trace(Visitor*) OVERRIDE;
    103 
    104 private:
    105     explicit SpeechRecognition(ExecutionContext*);
    106 
    107     Member<SpeechGrammarList> m_grammars;
    108     String m_lang;
    109     bool m_continuous;
    110     bool m_interimResults;
    111     unsigned long m_maxAlternatives;
    112 
    113     RawPtrWillBeMember<SpeechRecognitionController> m_controller;
    114     bool m_stoppedByActiveDOMObject;
    115     bool m_started;
    116     bool m_stopping;
    117     HeapVector<Member<SpeechRecognitionResult> > m_finalResults;
    118 };
    119 
    120 } // namespace WebCore
    121 
    122 #endif // SpeechRecognition_h
    123