Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / content / browser / speech / input_tag_speech_dispatcher_host.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_BROWSER_SPEECH_INPUT_TAG_SPEECH_DISPATCHER_HOST_H_
6 #define CONTENT_BROWSER_SPEECH_INPUT_TAG_SPEECH_DISPATCHER_HOST_H_
7
8 #include "base/compiler_specific.h"
9 #include "base/memory/ref_counted.h"
10 #include "base/memory/weak_ptr.h"
11 #include "content/common/content_export.h"
12 #include "content/public/browser/browser_message_filter.h"
13 #include "content/public/browser/speech_recognition_event_listener.h"
14 #include "content/public/common/speech_recognition_result.h"
15 #include "net/url_request/url_request_context_getter.h"
16
17 struct InputTagSpeechHostMsg_StartRecognition_Params;
18
19 namespace content {
20
21 class SpeechRecognitionManager;
22
23 // InputTagSpeechDispatcherHost is a delegate for Speech API messages used by
24 // RenderMessageFilter. Basically it acts as a proxy, relaying the events coming
25 // from the SpeechRecognitionManager to IPC messages (and vice versa).
26 // It's the complement of SpeechRecognitionDispatcher (owned by RenderView).
27 class CONTENT_EXPORT InputTagSpeechDispatcherHost
28     : public BrowserMessageFilter,
29       public SpeechRecognitionEventListener {
30  public:
31   InputTagSpeechDispatcherHost(
32       bool guest,
33       int render_process_id,
34       net::URLRequestContextGetter* url_request_context_getter);
35
36   base::WeakPtr<InputTagSpeechDispatcherHost> AsWeakPtr();
37
38   // SpeechRecognitionEventListener methods.
39   virtual void OnRecognitionStart(int session_id) OVERRIDE;
40   virtual void OnAudioStart(int session_id) OVERRIDE;
41   virtual void OnEnvironmentEstimationComplete(int session_id) OVERRIDE;
42   virtual void OnSoundStart(int session_id) OVERRIDE;
43   virtual void OnSoundEnd(int session_id) OVERRIDE;
44   virtual void OnAudioEnd(int session_id) OVERRIDE;
45   virtual void OnRecognitionEnd(int session_id) OVERRIDE;
46   virtual void OnRecognitionResults(
47       int session_id,
48       const SpeechRecognitionResults& results) OVERRIDE;
49   virtual void OnRecognitionError(
50       int session_id,
51       const SpeechRecognitionError& error) OVERRIDE;
52   virtual void OnAudioLevelsChange(int session_id,
53                                    float volume,
54                                    float noise_volume) OVERRIDE;
55
56   // BrowserMessageFilter implementation.
57   virtual bool OnMessageReceived(const IPC::Message& message,
58                                  bool* message_was_ok) OVERRIDE;
59   virtual void OverrideThreadForMessage(
60       const IPC::Message& message,
61       BrowserThread::ID* thread) OVERRIDE;
62
63   virtual void OnChannelClosing() OVERRIDE;
64
65  private:
66   virtual ~InputTagSpeechDispatcherHost();
67
68   void OnStartRecognition(
69       const InputTagSpeechHostMsg_StartRecognition_Params& params);
70   void OnCancelRecognition(int render_view_id, int request_id);
71   void OnStopRecording(int render_view_id, int request_id);
72
73   void StartRecognitionOnIO(
74       int embedder_render_process_id,
75       int embedder_render_view_id,
76       const InputTagSpeechHostMsg_StartRecognition_Params& params,
77       bool filter_profanities);
78
79   bool is_guest_;
80   int render_process_id_;
81   scoped_refptr<net::URLRequestContextGetter> url_request_context_getter_;
82
83   // Used for posting asynchronous tasks (on the IO thread) without worrying
84   // about this class being destroyed in the meanwhile (due to browser shutdown)
85   // since tasks pending on a destroyed WeakPtr are automatically discarded.
86   base::WeakPtrFactory<InputTagSpeechDispatcherHost> weak_factory_;
87
88   DISALLOW_COPY_AND_ASSIGN(InputTagSpeechDispatcherHost);
89 };
90
91 }  // namespace content
92
93 #endif  // CONTENT_BROWSER_SPEECH_INPUT_TAG_SPEECH_DISPATCHER_HOST_H_