1 // Copyright 2018 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 CHROMEOS_SERVICES_ASSISTANT_PLATFORM_API_IMPL_H_
6 #define CHROMEOS_SERVICES_ASSISTANT_PLATFORM_API_IMPL_H_
7 
8 #include <memory>
9 #include <string>
10 #include <utility>
11 #include <vector>
12 
13 #include "chromeos/audio/cras_audio_handler.h"
14 #include "chromeos/services/assistant/cros_platform_api.h"
15 #include "chromeos/services/assistant/platform/audio_input_provider_impl.h"
16 #include "chromeos/services/assistant/platform/audio_output_provider_impl.h"
17 #include "chromeos/services/assistant/platform/file_provider_impl.h"
18 #include "chromeos/services/assistant/platform/network_provider_impl.h"
19 #include "chromeos/services/assistant/platform/system_provider_impl.h"
20 #include "chromeos/services/assistant/public/cpp/assistant_service.h"
21 #include "libassistant/shared/public/platform_auth.h"
22 #include "mojo/public/cpp/bindings/pending_remote.h"
23 #include "services/device/public/mojom/battery_monitor.mojom.h"
24 
25 namespace chromeos {
26 class PowerManagerClient;
27 
28 namespace assistant {
29 
30 class AssistantMediaSession;
31 
32 // Platform API required by the voice assistant.
33 class PlatformApiImpl : public CrosPlatformApi,
34                         CrasAudioHandler::AudioObserver {
35  public:
36   PlatformApiImpl(
37       AssistantMediaSession* media_session,
38       PowerManagerClient* power_manager_client,
39       CrasAudioHandler* cras_audio_handler,
40       mojo::PendingRemote<device::mojom::BatteryMonitor> battery_monitor,
41       scoped_refptr<base::SequencedTaskRunner> main_thread_task_runner,
42       scoped_refptr<base::SingleThreadTaskRunner> background_task_runner,
43       std::string pref_locale);
44   ~PlatformApiImpl() override;
45 
46   // assistant_client::PlatformApi overrides
47   AudioInputProviderImpl& GetAudioInputProvider() override;
48   assistant_client::AudioOutputProvider& GetAudioOutputProvider() override;
49   assistant_client::AuthProvider& GetAuthProvider() override;
50   assistant_client::FileProvider& GetFileProvider() override;
51   assistant_client::NetworkProvider& GetNetworkProvider() override;
52   assistant_client::SystemProvider& GetSystemProvider() override;
53 
54   // chromeos::CrasAudioHandler::AudioObserver overrides
55   void OnAudioNodesChanged() override;
56 
57   // Called when the mic state associated with the interaction is changed.
58   void SetMicState(bool mic_open) override;
59 
60   void OnConversationTurnStarted() override;
61   void OnConversationTurnFinished() override;
62 
63   // Called when hotword enabled status changed.
64   void OnHotwordEnabled(bool enable) override;
65 
66  private:
67   // ChromeOS does not use auth manager, so we don't yet need to implement a
68   // real auth provider.
69   class FakeAuthProvider : public assistant_client::AuthProvider {
70    public:
71     FakeAuthProvider() = default;
72     ~FakeAuthProvider() override = default;
73 
74     // assistant_client::AuthProvider overrides
75     std::string GetAuthClientId() override;
76     std::vector<std::string> GetClientCertificateChain() override;
77 
78     void CreateCredentialAttestationJwt(
79         const std::string& authorization_code,
80         const std::vector<std::pair<std::string, std::string>>& claims,
81         CredentialCallback attestation_callback) override;
82 
83     void CreateRefreshAssertionJwt(
84         const std::string& key_identifier,
85         const std::vector<std::pair<std::string, std::string>>& claims,
86         AssertionCallback assertion_callback) override;
87 
88     void CreateDeviceAttestationJwt(
89         const std::vector<std::pair<std::string, std::string>>& claims,
90         AssertionCallback attestation_callback) override;
91 
92     std::string GetAttestationCertFingerprint() override;
93 
94     void RemoveCredentialKey(const std::string& key_identifier) override;
95 
96     void Reset() override;
97   };
98 
99   AudioInputProviderImpl audio_input_provider_;
100   AudioOutputProviderImpl audio_output_provider_;
101   FakeAuthProvider auth_provider_;
102   FileProviderImpl file_provider_;
103   NetworkProviderImpl network_provider_;
104   std::unique_ptr<SystemProviderImpl> system_provider_;
105   std::string pref_locale_;
106 
107   CrasAudioHandler* const cras_audio_handler_;
108 
109   DISALLOW_COPY_AND_ASSIGN(PlatformApiImpl);
110 };
111 
112 }  // namespace assistant
113 }  // namespace chromeos
114 
115 #endif  // CHROMEOS_SERVICES_ASSISTANT_PLATFORM_API_IMPL_H_
116