File: vm_camera_mic_manager.h

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (122 lines) | stat: -rw-r--r-- 4,422 bytes parent folder | download | duplicates (6)
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
120
121
122
// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROME_BROWSER_ASH_CAMERA_MIC_VM_CAMERA_MIC_MANAGER_H_
#define CHROME_BROWSER_ASH_CAMERA_MIC_VM_CAMERA_MIC_MANAGER_H_

#include <bitset>
#include <memory>

#include "base/memory/raw_ptr.h"
#include "base/observer_list.h"
#include "base/observer_list_types.h"
#include "base/scoped_observation.h"
#include "chromeos/ash/components/audio/cras_audio_handler.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 ash {

// 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, kBorealis };

  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::Milliseconds(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 the selected VM is using the device. Note that if the camera
  // privacy switch is on, this always returns false for `kCamera`.
  bool IsDeviceActive(VmType vm, 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_new_active_client,
      const base::flat_set<std::string>& active_device_ids) override;

  // media::CameraPrivacySwitchObserver
  void OnCameraHWPrivacySwitchStateChanged(
      const std::string& device_id,
      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();

  raw_ptr<Profile, LeakedDanglingUntriaged> primary_profile_ = nullptr;
  std::map<VmType, VmInfo> vm_info_map_;

  base::ObserverList<Observer> observers_;
};

}  // namespace ash

#endif  // CHROME_BROWSER_ASH_CAMERA_MIC_VM_CAMERA_MIC_MANAGER_H_