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
15struct SpeechRecognitionHostMsg_StartRequest_Params;
16
17namespace content {
18
19class SpeechRecognitionManager;
20struct 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).
26class 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  virtual void OnRecognitionStart(int session_id) OVERRIDE;
38  virtual void OnAudioStart(int session_id) OVERRIDE;
39  virtual void OnEnvironmentEstimationComplete(int session_id) OVERRIDE;
40  virtual void OnSoundStart(int session_id) OVERRIDE;
41  virtual void OnSoundEnd(int session_id) OVERRIDE;
42  virtual void OnAudioEnd(int session_id) OVERRIDE;
43  virtual void OnRecognitionEnd(int session_id) OVERRIDE;
44  virtual void OnRecognitionResults(
45      int session_id,
46      const SpeechRecognitionResults& results) OVERRIDE;
47  virtual void OnRecognitionError(
48      int session_id,
49      const SpeechRecognitionError& error) OVERRIDE;
50  virtual void OnAudioLevelsChange(int session_id,
51                                   float volume,
52                                   float noise_volume) OVERRIDE;
53
54  // BrowserMessageFilter implementation.
55  virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE;
56  virtual void OverrideThreadForMessage(
57      const IPC::Message& message,
58      BrowserThread::ID* thread) OVERRIDE;
59
60  virtual void OnChannelClosing() OVERRIDE;
61
62 private:
63  virtual ~SpeechRecognitionDispatcherHost();
64
65  void OnStartRequest(
66      const SpeechRecognitionHostMsg_StartRequest_Params& params);
67  void OnStartRequestOnIO(
68      int embedder_render_process_id,
69      int embedder_render_view_id,
70      const SpeechRecognitionHostMsg_StartRequest_Params& params,
71      int params_render_frame_id,
72      bool filter_profanities);
73  void OnAbortRequest(int render_view_id, int request_id);
74  void OnStopCaptureRequest(int render_view_id, int request_id);
75  void OnAbortAllRequests(int render_view_id);
76
77  int render_process_id_;
78  scoped_refptr<net::URLRequestContextGetter> context_getter_;
79
80  // Used for posting asynchronous tasks (on the IO thread) without worrying
81  // about this class being destroyed in the meanwhile (due to browser shutdown)
82  // since tasks pending on a destroyed WeakPtr are automatically discarded.
83  base::WeakPtrFactory<SpeechRecognitionDispatcherHost> weak_factory_;
84
85  DISALLOW_COPY_AND_ASSIGN(SpeechRecognitionDispatcherHost);
86};
87
88}  // namespace content
89
90#endif  // CONTENT_BROWSER_SPEECH_SPEECH_RECOGNITION_DISPATCHER_HOST_H_
91