1 // Copyright 2020 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 CHROME_BROWSER_SPEECH_SPEECH_RECOGNITION_SERVICE_H_
6 #define CHROME_BROWSER_SPEECH_SPEECH_RECOGNITION_SERVICE_H_
7 
8 #include "components/keyed_service/core/keyed_service.h"
9 #include "media/mojo/mojom/speech_recognition_service.mojom.h"
10 #include "mojo/public/cpp/bindings/remote.h"
11 
12 class PrefService;
13 
14 namespace content {
15 class BrowserContext;
16 }  // namespace content
17 
18 namespace speech {
19 
20 // Provides a mojo endpoint in the browser that allows the renderer process to
21 // launch and initialize the sandboxed speech recognition service
22 // process.
23 class SpeechRecognitionService
24     : public KeyedService,
25       public media::mojom::SpeechRecognitionServiceClient {
26  public:
27   explicit SpeechRecognitionService(content::BrowserContext* context);
28   SpeechRecognitionService(const SpeechRecognitionService&) = delete;
29   SpeechRecognitionService& operator=(const SpeechRecognitionService&) = delete;
30   ~SpeechRecognitionService() override;
31 
32   void Create(
33       mojo::PendingReceiver<media::mojom::SpeechRecognitionContext> receiver);
34 
35   // media::mojom::SpeechRecognitionServiceClient
36   void OnNetworkServiceDisconnect() override;
37 
38  private:
39   // Launches the speech recognition service in a sandboxed utility process.
40   void LaunchIfNotRunning();
41 
42   // Gets the path of the SODA configuration file for the selected language.
43   base::FilePath GetSodaConfigPath(PrefService* prefs);
44 
45   // The browser context associated with the keyed service.
46   content::BrowserContext* const context_;
47 
48   // A flag indicating whether to use the Speech On-Device API (SODA) for speech
49   // recognition.
50   bool enable_soda_ = false;
51 
52   // The remote to the speech recognition service. The browser will not launch a
53   // new speech recognition service process if this remote is already bound.
54   mojo::Remote<media::mojom::SpeechRecognitionService>
55       speech_recognition_service_;
56 
57   mojo::Receiver<media::mojom::SpeechRecognitionServiceClient>
58       speech_recognition_service_client_{this};
59 };
60 
61 }  // namespace speech
62 
63 #endif  // CHROME_BROWSER_SPEECH_SPEECH_RECOGNITION_SERVICE_H_
64