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#if ENABLE(SCRIPTED_SPEECH)
30
31#include "ActiveDOMObject.h"
32#include "EventTarget.h"
33#include "SpeechGrammarList.h"
34#include <wtf/Compiler.h>
35#include <wtf/PassRefPtr.h>
36#include <wtf/RefCounted.h>
37#include <wtf/text/WTFString.h>
38
39namespace WebCore {
40
41class ScriptExecutionContext;
42class SpeechRecognitionController;
43class SpeechRecognitionError;
44class SpeechRecognitionResult;
45class SpeechRecognitionResultList;
46
47class SpeechRecognition : public RefCounted<SpeechRecognition>, public ActiveDOMObject, public EventTarget {
48public:
49    static PassRefPtr<SpeechRecognition> create(ScriptExecutionContext*);
50    ~SpeechRecognition();
51
52    // Attributes.
53    PassRefPtr<SpeechGrammarList> grammars() { return m_grammars; }
54    void setGrammars(PassRefPtr<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(ExceptionCode&);
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 Vector<RefPtr<SpeechRecognitionResult> >& newFinalResults, const Vector<RefPtr<SpeechRecognitionResult> >& currentInterimResults);
77    void didReceiveNoMatch(PassRefPtr<SpeechRecognitionResult>);
78    void didReceiveError(PassRefPtr<SpeechRecognitionError>);
79    void didStart();
80    void didEnd();
81
82    // EventTarget.
83    virtual EventTargetInterface eventTargetInterface() const override;
84    virtual ScriptExecutionContext* scriptExecutionContext() const override;
85
86    // ActiveDOMObject.
87    virtual void stop() override;
88
89    using RefCounted<SpeechRecognition>::ref;
90    using RefCounted<SpeechRecognition>::deref;
91
92    DEFINE_ATTRIBUTE_EVENT_LISTENER(audiostart);
93    DEFINE_ATTRIBUTE_EVENT_LISTENER(soundstart);
94    DEFINE_ATTRIBUTE_EVENT_LISTENER(speechstart);
95    DEFINE_ATTRIBUTE_EVENT_LISTENER(speechend);
96    DEFINE_ATTRIBUTE_EVENT_LISTENER(soundend);
97    DEFINE_ATTRIBUTE_EVENT_LISTENER(audioend);
98    DEFINE_ATTRIBUTE_EVENT_LISTENER(result);
99    DEFINE_ATTRIBUTE_EVENT_LISTENER(nomatch);
100    DEFINE_ATTRIBUTE_EVENT_LISTENER(error);
101    DEFINE_ATTRIBUTE_EVENT_LISTENER(start);
102    DEFINE_ATTRIBUTE_EVENT_LISTENER(end);
103
104private:
105    friend class RefCounted<SpeechRecognition>;
106
107    explicit SpeechRecognition(ScriptExecutionContext*);
108
109
110    // EventTarget
111    virtual void refEventTarget() override { ref(); }
112    virtual void derefEventTarget() override { deref(); }
113    virtual EventTargetData* eventTargetData() override { return &m_eventTargetData; }
114    virtual EventTargetData& ensureEventTargetData() override { return m_eventTargetData; }
115
116    RefPtr<SpeechGrammarList> m_grammars;
117    String m_lang;
118    bool m_continuous;
119    bool m_interimResults;
120    unsigned long m_maxAlternatives;
121
122    EventTargetData m_eventTargetData;
123
124    SpeechRecognitionController* m_controller;
125    bool m_stoppedByActiveDOMObject;
126    bool m_started;
127    bool m_stopping;
128    Vector<RefPtr<SpeechRecognitionResult> > m_finalResults;
129};
130
131} // namespace WebCore
132
133#endif // ENABLE(SCRIPTED_SPEECH)
134
135#endif // SpeechRecognition_h
136