-
Notifications
You must be signed in to change notification settings - Fork 6.6k
/
vm_camera_mic_manager.h
119 lines (89 loc) · 4.11 KB
/
vm_camera_mic_manager.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CHROME_BROWSER_CHROMEOS_CAMERA_MIC_VM_CAMERA_MIC_MANAGER_H_
#define CHROME_BROWSER_CHROMEOS_CAMERA_MIC_VM_CAMERA_MIC_MANAGER_H_
#include <bitset>
#include <memory>
#include "ash/components/audio/cras_audio_handler.h"
#include "base/callback_forward.h"
#include "base/containers/flat_map.h"
#include "base/memory/weak_ptr.h"
#include "base/observer_list.h"
#include "base/observer_list_types.h"
#include "base/scoped_observation.h"
#include "components/keyed_service/core/keyed_service.h"
#include "media/capture/video/chromeos/camera_hal_dispatcher_impl.h"
#include "ui/message_center/public/cpp/notification.h"
#include "ui/message_center/public/cpp/notification_delegate.h"
class Profile;
namespace chromeos {
// This class manages camera/mic access (and the access notifications) for VMs
// (crostini and parallels for now). All of the notifications are sent to the
// primary profile since all VMs support only the primary profile. We might need
// to change this if we extend this class to support the browser, in which case
// we will also need to make the notification ids different for different
// profiles.
class VmCameraMicManager : public media::CameraActiveClientObserver,
public media::CameraPrivacySwitchObserver,
public CrasAudioHandler::AudioObserver {
public:
enum class VmType { kCrostiniVm, kPluginVm };
enum class DeviceType {
kMic,
kCamera,
kMaxValue = kCamera,
};
using NotificationType =
std::bitset<static_cast<size_t>(DeviceType::kMaxValue) + 1>;
static constexpr NotificationType kMicNotification{
1 << static_cast<size_t>(DeviceType::kMic)};
static constexpr NotificationType kCameraNotification{
1 << static_cast<size_t>(DeviceType::kCamera)};
static constexpr NotificationType kCameraAndMicNotification{
(1 << static_cast<size_t>(DeviceType::kMic)) |
(1 << static_cast<size_t>(DeviceType::kCamera))};
static constexpr base::TimeDelta kDebounceTime =
base::TimeDelta::FromMilliseconds(300);
class Observer : public base::CheckedObserver {
public:
virtual void OnVmCameraMicActiveChanged(VmCameraMicManager*) {}
};
static VmCameraMicManager* Get();
VmCameraMicManager();
~VmCameraMicManager() override;
void OnPrimaryUserSessionStarted(Profile* primary_profile);
VmCameraMicManager(const VmCameraMicManager&) = delete;
VmCameraMicManager& operator=(const VmCameraMicManager&) = delete;
void AddObserver(Observer* observer);
void RemoveObserver(Observer* observer);
// Return true if any of the VMs is using the device. Note that if the camera
// privacy switch is on, this always returns false for `kCamera`.
bool IsDeviceActive(DeviceType device) const;
// Return true if any of the VMs is displaying the `notification`.
bool IsNotificationActive(NotificationType notification) const;
private:
friend class VmCameraMicManagerTest;
static constexpr NotificationType kNoNotification{};
class VmInfo;
void MaybeSubscribeToCameraService(bool should_use_cros_camera_service);
// media::CameraActiveClientObserver
void OnActiveClientChange(cros::mojom::CameraClientType type,
bool is_active) override;
// media::CameraPrivacySwitchObserver
void OnCameraPrivacySwitchStatusChanged(
cros::mojom::CameraPrivacySwitchState state) override;
// CrasAudioHandler::AudioObserver
void OnNumberOfInputStreamsWithPermissionChanged() override;
void SetCameraAccessing(VmType vm, bool accessing);
void SetCameraPrivacyIsOn(bool is_on);
void SetMicActive(VmType vm, bool active);
static std::string GetNotificationId(VmType vm, NotificationType type);
void UpdateVmInfo(VmType vm, void (VmInfo::*updator)(bool), bool value);
void NotifyActiveChanged();
Profile* primary_profile_ = nullptr;
std::map<VmType, VmInfo> vm_info_map_;
base::ObserverList<Observer> observers_;
};
} // namespace chromeos
#endif // CHROME_BROWSER_CHROMEOS_CAMERA_MIC_VM_CAMERA_MIC_MANAGER_H_