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