Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / content / renderer / speech_recognition_dispatcher.h
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #ifndef CONTENT_RENDERER_SPEECH_RECOGNITION_DISPATCHER_H_
6 #define CONTENT_RENDERER_SPEECH_RECOGNITION_DISPATCHER_H_
7
8 #include <map>
9
10 #include "base/basictypes.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/memory/shared_memory.h"
13 #include "base/sync_socket.h"
14 #include "content/public/common/speech_recognition_result.h"
15 #include "content/public/renderer/render_view_observer.h"
16 #include "third_party/WebKit/public/platform/WebMediaStreamTrack.h"
17 #include "third_party/WebKit/public/platform/WebVector.h"
18 #include "third_party/WebKit/public/web/WebSpeechRecognitionHandle.h"
19 #include "third_party/WebKit/public/web/WebSpeechRecognizer.h"
20
21 namespace media {
22 class AudioParameters;
23 }
24
25 namespace content {
26 class RenderViewImpl;
27 #if defined(ENABLE_WEBRTC)
28 class SpeechRecognitionAudioSink;
29 #endif
30 struct SpeechRecognitionError;
31 struct SpeechRecognitionResult;
32
33 // SpeechRecognitionDispatcher is a delegate for methods used by WebKit for
34 // scripted JS speech APIs. It's the complement of
35 // SpeechRecognitionDispatcherHost (owned by RenderViewHost).
36 class SpeechRecognitionDispatcher : public RenderViewObserver,
37                                     public blink::WebSpeechRecognizer {
38  public:
39   explicit SpeechRecognitionDispatcher(RenderViewImpl* render_view);
40   virtual ~SpeechRecognitionDispatcher();
41
42   // Aborts all speech recognitions.
43   void AbortAllRecognitions();
44
45  private:
46   // RenderViewObserver implementation.
47   bool OnMessageReceived(const IPC::Message& message) override;
48
49   // blink::WebSpeechRecognizer implementation.
50   virtual void start(const blink::WebSpeechRecognitionHandle&,
51                      const blink::WebSpeechRecognitionParams&,
52                      blink::WebSpeechRecognizerClient*);
53   virtual void stop(const blink::WebSpeechRecognitionHandle&,
54                     blink::WebSpeechRecognizerClient*);
55   virtual void abort(const blink::WebSpeechRecognitionHandle&,
56                      blink::WebSpeechRecognizerClient*);
57
58   void OnRecognitionStarted(int request_id);
59   void OnAudioStarted(int request_id);
60   void OnSoundStarted(int request_id);
61   void OnSoundEnded(int request_id);
62   void OnAudioEnded(int request_id);
63   void OnErrorOccurred(int request_id, const SpeechRecognitionError& error);
64   void OnRecognitionEnded(int request_id);
65   void OnResultsRetrieved(int request_id,
66                           const SpeechRecognitionResults& result);
67   void OnAudioReceiverReady(int session_id,
68                              const media::AudioParameters& params,
69                              const base::SharedMemoryHandle handle,
70                              const base::SyncSocket::TransitDescriptor socket);
71
72   void ResetAudioSink();
73
74   int GetOrCreateIDForHandle(const blink::WebSpeechRecognitionHandle& handle);
75   bool HandleExists(const blink::WebSpeechRecognitionHandle& handle);
76   const blink::WebSpeechRecognitionHandle& GetHandleFromID(int handle_id);
77
78   // The WebKit client class that we use to send events back to the JS world.
79   blink::WebSpeechRecognizerClient* recognizer_client_;
80
81 #if defined(ENABLE_WEBRTC)
82   // Media stream audio track that the speech recognition connects to.
83   // Accessed on the render thread.
84   blink::WebMediaStreamTrack audio_track_;
85
86   // Audio sink used to provide audio from the track.
87   scoped_ptr<SpeechRecognitionAudioSink> speech_audio_sink_;
88 #endif
89
90   typedef std::map<int, blink::WebSpeechRecognitionHandle> HandleMap;
91   HandleMap handle_map_;
92   int next_id_;
93
94   DISALLOW_COPY_AND_ASSIGN(SpeechRecognitionDispatcher);
95 };
96
97 }  // namespace content
98
99 #endif  // CONTENT_RENDERER_SPEECH_RECOGNITION_DISPATCHER_H_