File: vm_camera_mic_manager.cc

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 (636 lines) | stat: -rw-r--r-- 24,246 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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
// 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.

#include "chrome/browser/ash/camera_mic/vm_camera_mic_manager.h"

#include <string>
#include <tuple>
#include <utility>

#include "ash/constants/ash_constants.h"
#include "ash/constants/ash_features.h"
#include "ash/constants/notifier_catalogs.h"
#include "ash/public/cpp/notification_utils.h"
#include "ash/public/cpp/vm_camera_mic_constants.h"
#include "ash/system/privacy/privacy_indicators_controller.h"
#include "ash/webui/settings/public/constants/routes.mojom.h"
#include "base/feature_list.h"
#include "base/functional/bind.h"
#include "base/memory/raw_ptr.h"
#include "base/no_destructor.h"
#include "base/notreached.h"
#include "base/system/sys_info.h"
#include "base/task/thread_pool.h"
#include "base/time/time.h"
#include "base/timer/timer.h"
#include "chrome/app/vector_icons/vector_icons.h"
#include "chrome/browser/ash/borealis/borealis_util.h"
#include "chrome/browser/ash/plugin_vm/plugin_vm_util.h"
#include "chrome/browser/ash/video_conference/video_conference_ash_feature_client.h"
#include "chrome/browser/notifications/notification_display_service.h"
#include "chrome/browser/notifications/notification_display_service_factory.h"
#include "chrome/browser/ui/chrome_pages.h"
#include "chrome/browser/ui/settings_window_manager_chromeos.h"
#include "chrome/browser/ui/webui/ash/settings/app_management/app_management_uma.h"
#include "chrome/grit/generated_resources.h"
#include "components/vector_icons/vector_icons.h"
#include "content/public/browser/browser_task_traits.h"
#include "content/public/browser/browser_thread.h"
#include "media/capture/video/chromeos/mojom/cros_camera_service.mojom-shared.h"
#include "media/capture/video/chromeos/public/cros_features.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/message_center/public/cpp/message_center_constants.h"
#include "ui/message_center/public/cpp/notification.h"
#include "ui/message_center/public/cpp/notification_delegate.h"
#include "ui/message_center/public/cpp/notification_types.h"

namespace ash {

namespace {

const char kNotificationIdPrefix[] = "vm_camera_mic_manager";

}  // namespace

constexpr VmCameraMicManager::NotificationType
    VmCameraMicManager::kNoNotification;
constexpr VmCameraMicManager::NotificationType
    VmCameraMicManager::kMicNotification;
constexpr VmCameraMicManager::NotificationType
    VmCameraMicManager::kCameraNotification;
constexpr VmCameraMicManager::NotificationType
    VmCameraMicManager::kCameraAndMicNotification;
constexpr base::TimeDelta VmCameraMicManager::kDebounceTime;

// VmInfo stores the camera/mic information for a VM. It also controls the
// notifications for the VM. We either do not display a notification at all, or
// display a single notification, which can be a "camera", "mic", or a "camera
// and mic" notification.
//
// Some apps will quickly turn on and off devices multiple times (e.g. skype in
// Parallels does this about 5 times when starting a meeting). To avoid flashing
// multiple notifications, we implement a debounce algorithm here. The debounce
// algorithm needs to handle the following situations:
//
// * when a VM opens the camera and mic subsequently with a small delay
//   in-between, we should only show the "camera and mic" notification, instead
//   of showing the "camera" notification first and then switching to the
//   "camera and mic" one.
// * when a VM turns on a device and then immediately turns it off (e.g. taking
//   a photo), we should make sure the notification is shown (for a short period
//   of time). So, the debounce algorithm should not naively accumulate device
//   changes and then only act on the final accumulated state.
//
//
// How the debounce algorithm works
// ================================
//
// Basically, when a new device update comes, the algorithm starts a debounce
// period for `kDebounceTime`, during which we record the changes, and we update
// the notification one or more times afterwards.
//
// The type of notification is represented by `NotificationType`, which is a
// bitset of two bits. For a "camera" notification, only the camera bit is set
// (i.e. `10`). A "camera and mic" notification sets both bits (i.e. `11`). If
// no notification should be shown, we set both bits to 0 (i.e. `00`).
//
// Our algorithm maintains 3 `NotificationType` variables (see
// `notifications_`):
//
// * active: this is what is currently displaying. When we say setting `active`
//           to some value, we also mean updating the displaying notification.
// * target: this is updated immediately whenever device updates come in, so it
//           represents the latest state. If a device is turned on and off
//           immediately, obviously the effect is erased from target. This is
//           why we need another variable `stage`.
// * stage: this is updated immediately whenever a device is turned *on*.
//          Turning off a device does not affect this directly.
//
// This algorithm for updating `target` and `stage` is implemented in
// `OnDeviceUpdated()`, which also starts/stops the debounce timer if necessary.
//
// When `active == stage == target`, we are "stable" --- we don't need to do
// anything (until the next device update). And
// `SyncNotificationAndIndicators()` is normally what brings us to stable. It is
// called when the timer expired. This is what it does:
//
// * If `active != stage`, we set `active = stage`. Timer is reset if we are
//   still not stable.
// * Otherwise, `active == stage != target`. we set `active = stage = target`.
//   We reach the stable state now.
//
// Here is an example where the mic is turned on, the camera is turned on and
// then off immediately, and mic is turned off at the end after some time. We
// denote the state of the system with 6 bits: <active>-<stage>-<target>.
//
// 1: 00-00-00  # Stable, nothing is on.
// 2: 00-01-01  # Mic turning on, debounce timer is started.
// 3: 00-11-11  # Camera turning on, still in debounce period.
// 4: 00-11-01  # Camera turning off, still in debounce period.
// 5: 11-11-01  # Timer expired. `SyncNotificationAndIndicators()` sets
//              # `active=stage` (shows "camera and mic" notification). Reset
//              # the timer.
// 6: 01-01-01  # Timer expired. `SyncNotificationAndIndicators()` sets
//              # `active=stage=target` (shows mic notification).  We are stable
//              # now.
// 7: 01-01-00  # Mic turning off, debounce timer is started.
// 8: 00-00-00  # Timer expired. Same as 6, but no notification is shown now.
//              # Reach stable again.
class VmCameraMicManager::VmInfo : public message_center::NotificationObserver {
 public:
  VmInfo(Profile* profile,
         VmType vm_type,
         int name_id,
         base::RepeatingClosure on_notification_changed)
      : profile_(profile),
        vm_type_(vm_type),
        name_id_(name_id),
        notification_changed_callback_(on_notification_changed),
        debounce_timer_(
            FROM_HERE,
            kDebounceTime,
            base::BindRepeating(&VmInfo::SyncNotificationAndIndicators,
                                // Unretained because the timer
                                // cannot outlive the parent.
                                base::Unretained(this))) {}
  ~VmInfo() = default;

  VmType vm_type() const { return vm_type_; }
  int name_id() const { return name_id_; }
  NotificationType notification_type() const { return notifications_.active; }

  void SetMicActive(bool active) {
    OnDeviceUpdated(DeviceType::kMic, active);

    if (features::IsVideoConferenceEnabled()) {
      VideoConferenceAshFeatureClient* vc_ash_feature_client =
          VideoConferenceAshFeatureClient::Get();
      // Only calls `OnVmDeviceUpdated()` if `VideoConferenceAshFeatureClient`
      // has initialized, otherwise it will be handled at
      // `VideoConferenceAshFeatureClient` initialization.
      if (vc_ash_feature_client) {
        vc_ash_feature_client->OnVmDeviceUpdated(vm_type_, DeviceType::kMic,
                                                 active);
      }
    }
  }

  void SetCameraAccessing(bool accessing) {
    camera_accessing_ = accessing;
    OnCameraUpdated();

    if (features::IsVideoConferenceEnabled()) {
      VideoConferenceAshFeatureClient* vc_ash_feature_client =
          VideoConferenceAshFeatureClient::Get();
      // Only calls `OnVmDeviceUpdated()` if `VideoConferenceAshFeatureClient`
      // has initialized, otherwise it will be handled at
      // `VideoConferenceAshFeatureClient` initialization.
      if (vc_ash_feature_client) {
        vc_ash_feature_client->OnVmDeviceUpdated(vm_type_, DeviceType::kCamera,
                                                 accessing);
      }
    }
  }
  void SetCameraPrivacyIsOn(bool on) {
    camera_privacy_is_on_ = on;
    OnCameraUpdated();
  }

 private:
  void OnCameraUpdated() {
    OnDeviceUpdated(DeviceType::kCamera,
                    camera_accessing_ && !camera_privacy_is_on_);
  }

  // See document at the beginning of class.
  void OnDeviceUpdated(DeviceType device, bool value) {
    size_t device_index = static_cast<size_t>(device);

    notifications_.target.set(device_index, value);
    if (value) {
      notifications_.stage.set(device_index, value);
    }

    VLOG(1) << "update stage/target vm_type=" << static_cast<int>(vm_type_)
            << " state: " << notifications_.active << "-"
            << notifications_.stage << "-" << notifications_.target;

    SyncTimer();
  }

  void SyncTimer() {
    const bool stable = notifications_.active == notifications_.stage &&
                        notifications_.active == notifications_.target;
    const bool should_run_timer = !stable;
    const bool is_running = debounce_timer_.IsRunning();

    if (should_run_timer && !is_running) {
      debounce_timer_.Reset();
    } else if (!should_run_timer && is_running) {
      debounce_timer_.Stop();
    }
  }

  void UpdateActiveNotificationAndIndicators(
      NotificationType new_notification) {
    DCHECK_NE(notifications_.active, new_notification);

    auto app_name = l10n_util::GetStringUTF16(name_id_);
    auto delegate = base::MakeRefCounted<PrivacyIndicatorsNotificationDelegate>(
        /*launch_settings=*/base::BindRepeating(
            &VmCameraMicManager::VmInfo::OpenSettings,
            weak_ptr_factory_.GetWeakPtr()));

    // Privacy indicators is only enabled when Video Conference is disabled.
    bool privacy_indicators_enabled = !features::IsVideoConferenceEnabled();

    if (notifications_.active != kNoNotification) {
      if (privacy_indicators_enabled) {
        PrivacyIndicatorsController::Get()->UpdatePrivacyIndicators(
            /*app_id=*/GetNotificationId(vm_type_, notifications_.active),
            app_name, /*is_camera_used=*/false, /*is_microphone_used=*/false,
            delegate, PrivacyIndicatorsSource::kLinuxVm);
      } else {
        CloseNotification(notifications_.active);
      }
    }

    if (new_notification != kNoNotification) {
      // Privacy indicator is only enabled when Video Conference is disabled.
      if (privacy_indicators_enabled) {
        PrivacyIndicatorsController::Get()->UpdatePrivacyIndicators(
            /*app_id=*/GetNotificationId(vm_type_, new_notification), app_name,
            /*is_camera_used=*/
            new_notification[static_cast<size_t>(DeviceType::kCamera)],
            /*is_microphone_used=*/
            new_notification[static_cast<size_t>(DeviceType::kMic)], delegate,
            PrivacyIndicatorsSource::kLinuxVm);
      } else {
        OpenNotification(new_notification);
      }
    }

    notifications_.active = new_notification;
    notification_changed_callback_.Run();
  }

  // See document at the beginning of class.
  void SyncNotificationAndIndicators() {
    if (notifications_.active != notifications_.stage) {
      UpdateActiveNotificationAndIndicators(notifications_.stage);
      SyncTimer();

      VLOG(1) << "sync from stage. vm_type=" << static_cast<int>(vm_type_)
              << " state: " << notifications_.active << "-"
              << notifications_.stage << "-" << notifications_.target;
      return;
    }

    // Only target notification is different.
    DCHECK_NE(notifications_.active, notifications_.target);
    notifications_.stage = notifications_.target;
    UpdateActiveNotificationAndIndicators(notifications_.target);
    VLOG(1) << "sync from target. vm_type=" << static_cast<int>(vm_type_)
            << " state: " << notifications_.active << "-"
            << notifications_.stage << "-" << notifications_.target;
    // No need to call `SyncTimer()` because we have reached the stable state
    // here.
  }

  void OpenNotification(NotificationType type) const {
    CHECK(features::IsVideoConferenceEnabled());
    CHECK_NE(type, kNoNotification);

    const gfx::VectorIcon* source_icon = nullptr;
    int message_id;
    if (type[static_cast<size_t>(DeviceType::kCamera)]) {
      source_icon = &::vector_icons::kVideocamIcon;
      if (type[static_cast<size_t>(DeviceType::kMic)]) {
        message_id = IDS_APP_USING_CAMERA_MIC_NOTIFICATION_MESSAGE;
      } else {
        message_id = IDS_APP_USING_CAMERA_NOTIFICATION_MESSAGE;
      }
    } else {
      DCHECK_EQ(type, kMicNotification);
      source_icon = &::vector_icons::kMicIcon;
      message_id = IDS_APP_USING_MIC_NOTIFICATION_MESSAGE;
    }

    message_center::RichNotificationData rich_notification_data;
    rich_notification_data.vector_small_image = source_icon;
    rich_notification_data.pinned = true;
    rich_notification_data.buttons.emplace_back(
        l10n_util::GetStringUTF16(IDS_INTERNAL_APP_SETTINGS));
    rich_notification_data.fullscreen_visibility =
        message_center::FullscreenVisibility::OVER_USER;

    message_center::Notification notification(
        message_center::NOTIFICATION_TYPE_SIMPLE,
        GetNotificationId(vm_type_, type),
        /*title=*/
        l10n_util::GetStringFUTF16(message_id,
                                   l10n_util::GetStringUTF16(name_id_)),
        /*message=*/std::u16string(),
        /*icon=*/ui::ImageModel(),
        /*display_source=*/
        l10n_util::GetStringUTF16(IDS_CHROME_OS_NOTIFICATION_SOURCE),
        /*origin_url=*/GURL(),
        message_center::NotifierId(
            message_center::NotifierType::SYSTEM_COMPONENT,
            kVmCameraMicNotifierId, NotificationCatalogName::kVMCameraMic),
        rich_notification_data,
        base::MakeRefCounted<message_center::ThunkNotificationDelegate>(
            weak_ptr_factory_.GetMutableWeakPtr()));

    NotificationDisplayServiceFactory::GetForProfile(profile_)->Display(
        NotificationHandler::Type::TRANSIENT, notification,
        /*metadata=*/nullptr);
  }

  void CloseNotification(NotificationType type) const {
    CHECK(features::IsVideoConferenceEnabled());
    CHECK_NE(type, kNoNotification);

    NotificationDisplayServiceFactory::GetForProfile(profile_)->Close(
        NotificationHandler::Type::TRANSIENT,
        GetNotificationId(vm_type_, type));
  }

  // message_center::NotificationObserver:
  //
  // This open the settings page if the button is clicked on the notification.
  void Click(const std::optional<int>& button_index,
             const std::optional<std::u16string>& reply) override {
    OpenSettings();
  }

  // Opens the settings page.
  void OpenSettings() const {
    switch (vm_type_) {
      case VmType::kCrostiniVm:
        chrome::SettingsWindowManager::GetInstance()->ShowOSSettings(
            profile_, chromeos::settings::mojom::kCrostiniDetailsSubpagePath);
        break;
      case VmType::kPluginVm:
        chrome::ShowAppManagementPage(
            profile_, plugin_vm::kPluginVmShelfAppId,
            settings::AppManagementEntryPoint::kNotificationPluginVm);
        break;
      case VmType::kBorealis:
        chrome::ShowAppManagementPage(
            profile_, borealis::kClientAppId,
            settings::AppManagementEntryPoint::kAppManagementMainViewBorealis);
        break;
    }
  }

  const raw_ptr<Profile, LeakedDanglingUntriaged> profile_;
  const VmType vm_type_;
  const int name_id_;
  base::RepeatingClosure notification_changed_callback_;

  bool camera_accessing_ = false;
  // We don't actually need to store this separately for each VM, but this
  // makes code simpler.
  bool camera_privacy_is_on_ = false;

  // See document at the beginning of class.
  struct {
    NotificationType active;
    NotificationType stage;
    NotificationType target;
  } notifications_;

  base::RetainingOneShotTimer debounce_timer_;

  base::WeakPtrFactory<VmInfo> weak_ptr_factory_{this};
};

VmCameraMicManager* VmCameraMicManager::Get() {
  static base::NoDestructor<VmCameraMicManager> manager;
  return manager.get();
}

VmCameraMicManager::VmCameraMicManager() = default;

void VmCameraMicManager::OnPrimaryUserSessionStarted(Profile* primary_profile) {
  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);

  primary_profile_ = primary_profile;

  auto emplace_vm_info = [this](VmType vm, int name_id) {
    vm_info_map_.emplace(
        std::piecewise_construct, std::forward_as_tuple(vm),
        std::forward_as_tuple(
            primary_profile_, vm, name_id,
            base::BindRepeating(&VmCameraMicManager::NotifyActiveChanged,
                                base::Unretained(this))));
  };

  emplace_vm_info(VmType::kCrostiniVm, IDS_CROSTINI_LINUX);
  emplace_vm_info(VmType::kPluginVm, IDS_PLUGIN_VM_APP_NAME);
  emplace_vm_info(VmType::kBorealis, IDS_BOREALIS_APP_NAME);

  // Only do the subscription in real ChromeOS environment.
  if (base::SysInfo::IsRunningOnChromeOS()) {
    base::ThreadPool::PostTaskAndReplyWithResult(
        FROM_HERE, {base::MayBlock()},
        base::BindOnce(media::ShouldUseCrosCameraService),
        base::BindOnce(&VmCameraMicManager::MaybeSubscribeToCameraService,
                       base::Unretained(this)));

    CrasAudioHandler::Get()->AddAudioObserver(this);
    // Fetch the current value.
    content::GetUIThreadTaskRunner({})->PostTask(
        FROM_HERE,
        base::BindOnce(
            &VmCameraMicManager::OnNumberOfInputStreamsWithPermissionChanged,
            base::Unretained(this)));
  }
}

// The class is supposed to be used as a singleton with `base::NoDestructor`,
// so we do not do clean up (e.g. deregister as observers) here.
VmCameraMicManager::~VmCameraMicManager() = default;

void VmCameraMicManager::MaybeSubscribeToCameraService(
    bool should_use_cros_camera_service) {
  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);

  if (!should_use_cros_camera_service) {
    return;
  }

  auto* camera = media::CameraHalDispatcherImpl::GetInstance();
  // OnActiveClientChange() will be called automatically after the
  // subscription, so there is no need to get the current status here.
  camera->AddActiveClientObserver(this);
  auto privacy_switch_state = cros::mojom::CameraPrivacySwitchState::UNKNOWN;
  auto device_id_to_privacy_switch_state =
      camera->AddCameraPrivacySwitchObserver(this);
  // TODO(b/255249223): Handle multiple cameras with privacy controls
  // properly.
  for (const auto& it : device_id_to_privacy_switch_state) {
    cros::mojom::CameraPrivacySwitchState state = it.second;
    if (state == cros::mojom::CameraPrivacySwitchState::ON) {
      privacy_switch_state = state;
      break;
    } else if (state == cros::mojom::CameraPrivacySwitchState::OFF) {
      privacy_switch_state = state;
    }
  }
  OnCameraHWPrivacySwitchStateChanged(
      /*device_id=*/std::string(), privacy_switch_state);
}

void VmCameraMicManager::UpdateVmInfo(VmType vm,
                                      void (VmInfo::*updator)(bool),
                                      bool value) {
  auto it = vm_info_map_.find(vm);
  CHECK(it != vm_info_map_.end());
  auto& vm_info = it->second;

  (vm_info.*updator)(value);
}

bool VmCameraMicManager::IsDeviceActive(DeviceType device) const {
  for (const auto& vm_info : vm_info_map_) {
    const NotificationType& notification_type =
        vm_info.second.notification_type();
    if (notification_type[static_cast<size_t>(device)]) {
      return true;
    }
  }
  return false;
}

bool VmCameraMicManager::IsDeviceActive(VmType vm, DeviceType device) const {
  auto it = vm_info_map_.find(vm);
  if (it == vm_info_map_.end()) {
    return false;
  }
  const NotificationType& notification_type = it->second.notification_type();
  if (notification_type[static_cast<size_t>(device)]) {
    return true;
  }
  return false;
}

bool VmCameraMicManager::IsNotificationActive(
    NotificationType notification) const {
  for (const auto& vm_info : vm_info_map_) {
    if (vm_info.second.notification_type() == notification) {
      return true;
    }
  }
  return false;
}

void VmCameraMicManager::OnActiveClientChange(
    cros::mojom::CameraClientType type,
    bool is_new_active_client,
    const base::flat_set<std::string>& active_device_ids) {
  // Crostini does not support camera yet.
  bool client_active_state_changed =
      is_new_active_client || active_device_ids.empty();

  if (client_active_state_changed &&
      type == cros::mojom::CameraClientType::PLUGINVM) {
    content::GetUIThreadTaskRunner({})->PostTask(
        FROM_HERE, base::BindOnce(&VmCameraMicManager::SetCameraAccessing,
                                  base::Unretained(this), VmType::kPluginVm,
                                  !active_device_ids.empty()));
  }
}

void VmCameraMicManager::SetCameraAccessing(VmType vm, bool accessing) {
  UpdateVmInfo(vm, &VmInfo::SetCameraAccessing, accessing);
}

void VmCameraMicManager::OnCameraHWPrivacySwitchStateChanged(
    const std::string& device_id,
    cros::mojom::CameraPrivacySwitchState state) {
  using cros::mojom::CameraPrivacySwitchState;
  bool is_on;
  switch (state) {
    case CameraPrivacySwitchState::UNKNOWN:
    case CameraPrivacySwitchState::OFF:
      is_on = false;
      break;
    case CameraPrivacySwitchState::ON:
      is_on = true;
      break;
  }

  content::GetUIThreadTaskRunner({})->PostTask(
      FROM_HERE, base::BindOnce(&VmCameraMicManager::SetCameraPrivacyIsOn,
                                base::Unretained(this), is_on));
}

void VmCameraMicManager::SetCameraPrivacyIsOn(bool is_on) {
  DCHECK(!vm_info_map_.empty());
  for (auto& vm_and_info : vm_info_map_) {
    UpdateVmInfo(/*vm=*/vm_and_info.first, &VmInfo::SetCameraPrivacyIsOn,
                 is_on);
  }
}

void VmCameraMicManager::AddObserver(Observer* observer) {
  observers_.AddObserver(observer);
}

void VmCameraMicManager::RemoveObserver(Observer* observer) {
  observers_.RemoveObserver(observer);
}

void VmCameraMicManager::NotifyActiveChanged() {
  for (Observer& observer : observers_) {
    observer.OnVmCameraMicActiveChanged(this);
  }
}

std::string VmCameraMicManager::GetNotificationId(VmType vm,
                                                  NotificationType type) {
  std::string id = kNotificationIdPrefix;

  switch (vm) {
    case VmType::kCrostiniVm:
      id.append("-crostini");
      break;
    case VmType::kPluginVm:
      id.append("-pluginvm");
      break;
    case VmType::kBorealis:
      id.append("-borealis");
      break;
  }

  id.append(type.to_string());

  return id;
}

void VmCameraMicManager::OnNumberOfInputStreamsWithPermissionChanged() {
  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);

  const auto& clients_and_numbers =
      CrasAudioHandler::Get()->GetNumberOfInputStreamsWithPermission();

  auto update = [&](CrasAudioHandler::ClientType cras_client_type, VmType vm) {
    auto it = clients_and_numbers.find(cras_client_type);
    bool active = (it != clients_and_numbers.end() && it->second != 0);

    SetMicActive(vm, active);
  };

  update(CrasAudioHandler::ClientType::VM_TERMINA, VmType::kCrostiniVm);
  update(CrasAudioHandler::ClientType::VM_PLUGIN, VmType::kPluginVm);
  update(CrasAudioHandler::ClientType::VM_BOREALIS, VmType::kBorealis);
}

void VmCameraMicManager::SetMicActive(VmType vm, bool active) {
  UpdateVmInfo(vm, &VmInfo::SetMicActive, active);
}

}  // namespace ash