Commit c94bdb45 authored by Jason Lin's avatar Jason Lin Committed by Chromium LUCI CQ

Move VmCameraMicManager::VmInfo definition to cc file

We don't actually need it in the header except for unit tests. For
unit tests, 3 new functions `Set...()` are added so they can directly
manupulated the states.

Bug: b/167491603
Change-Id: I46ac5d32e92fcf17ae246ca8c419441c94129759
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2636743
Commit-Queue: Jason Lin <lxj@google.com>
Auto-Submit: Jason Lin <lxj@google.com>
Reviewed-by: default avatarJoel Hockey <joelhockey@chromium.org>
Cr-Commit-Position: refs/heads/master@{#845993}
parent bd74fc12
......@@ -64,6 +64,41 @@ constexpr VmCameraMicManager::NotificationType
constexpr VmCameraMicManager::NotificationType
VmCameraMicManager::kCameraAndMicNotification;
class VmCameraMicManager::VmInfo {
public:
VmInfo() = default;
VmInfo(const VmInfo&) = default;
~VmInfo() = default;
NotificationType notification_type() const { return notification_type_; }
void SetMicActive(bool active) {
notification_type_.set(static_cast<size_t>(DeviceType::kMic), active);
}
void SetCameraAccessing(bool accessing) {
camera_accessing_ = accessing;
OnCameraUpdated();
}
void SetCameraPrivacyIsOn(bool on) {
camera_privacy_is_on_ = on;
OnCameraUpdated();
}
private:
void OnCameraUpdated() {
notification_type_.set(static_cast<size_t>(DeviceType::kCamera),
camera_accessing_ && !camera_privacy_is_on_);
}
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;
NotificationType notification_type_;
};
VmCameraMicManager* VmCameraMicManager::Get() {
static base::NoDestructor<VmCameraMicManager> manager;
return manager.get();
......@@ -187,12 +222,15 @@ void VmCameraMicManager::OnActiveClientChange(
if (type == cros::mojom::CameraClientType::PLUGINVM) {
content::GetUIThreadTaskRunner({})->PostTask(
FROM_HERE,
base::BindOnce(&VmCameraMicManager::UpdateVmInfoAndNotifications,
base::Unretained(this), VmType::kPluginVm,
&VmInfo::SetCameraAccessing, is_active));
base::BindOnce(&VmCameraMicManager::SetCameraAccessing,
base::Unretained(this), VmType::kPluginVm, is_active));
}
}
void VmCameraMicManager::SetCameraAccessing(VmType vm, bool accessing) {
UpdateVmInfoAndNotifications(vm, &VmInfo::SetCameraAccessing, accessing);
}
void VmCameraMicManager::OnCameraPrivacySwitchStatusChanged(
cros::mojom::CameraPrivacySwitchState state) {
using cros::mojom::CameraPrivacySwitchState;
......@@ -207,14 +245,16 @@ void VmCameraMicManager::OnCameraPrivacySwitchStatusChanged(
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_) {
VmType vm = vm_and_info.first;
content::GetUIThreadTaskRunner({})->PostTask(
FROM_HERE,
base::BindOnce(&VmCameraMicManager::UpdateVmInfoAndNotifications,
base::Unretained(this), vm,
&VmInfo::SetCameraPrivacyIsOn, is_on));
UpdateVmInfoAndNotifications(/*vm=*/vm_and_info.first,
&VmInfo::SetCameraPrivacyIsOn, is_on);
}
}
......@@ -325,29 +365,6 @@ void VmCameraMicManager::CloseNotification(VmType vm, NotificationType type) {
GetNotificationId(vm, type));
}
VmCameraMicManager::VmInfo::VmInfo() = default;
VmCameraMicManager::VmInfo::VmInfo(const VmInfo&) = default;
VmCameraMicManager::VmInfo::~VmInfo() = default;
void VmCameraMicManager::VmInfo::SetMicActive(bool active) {
notification_type_.set(static_cast<size_t>(DeviceType::kMic), active);
}
void VmCameraMicManager::VmInfo::SetCameraAccessing(bool accessing) {
camera_accessing_ = accessing;
OnCameraUpdated();
}
void VmCameraMicManager::VmInfo::SetCameraPrivacyIsOn(bool on) {
camera_privacy_is_on_ = on;
OnCameraUpdated();
}
void VmCameraMicManager::VmInfo::OnCameraUpdated() {
notification_type_.set(static_cast<size_t>(DeviceType::kCamera),
camera_accessing_ && !camera_privacy_is_on_);
}
VmCameraMicManager::VmNotificationObserver::VmNotificationObserver() = default;
VmCameraMicManager::VmNotificationObserver::~VmNotificationObserver() = default;
......@@ -380,11 +397,15 @@ void VmCameraMicManager::OnNumberOfInputStreamsWithPermissionChanged() {
auto it = clients_and_numbers.find(cras_client_type);
bool active = (it != clients_and_numbers.end() && it->second != 0);
UpdateVmInfoAndNotifications(vm, &VmInfo::SetMicActive, active);
SetMicActive(vm, active);
};
update(CrasAudioHandler::ClientType::VM_TERMINA, VmType::kCrostiniVm);
update(CrasAudioHandler::ClientType::VM_PLUGIN, VmType::kPluginVm);
}
void VmCameraMicManager::SetMicActive(VmType vm, bool active) {
UpdateVmInfoAndNotifications(vm, &VmInfo::SetMicActive, active);
}
} // namespace chromeos
......@@ -82,28 +82,7 @@ class VmCameraMicManager : public media::CameraActiveClientObserver,
static constexpr NotificationType kNoNotification{};
class VmInfo {
public:
VmInfo();
VmInfo(const VmInfo&);
~VmInfo();
NotificationType notification_type() const { return notification_type_; }
void SetMicActive(bool active);
void SetCameraAccessing(bool accessing);
void SetCameraPrivacyIsOn(bool on);
private:
void OnCameraUpdated();
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;
NotificationType notification_type_;
};
class VmInfo;
class VmNotificationObserver : public message_center::NotificationObserver {
public:
......@@ -139,6 +118,10 @@ class VmCameraMicManager : public media::CameraActiveClientObserver,
// 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 UpdateVmInfoAndNotifications(VmType vm,
......
......@@ -112,8 +112,6 @@ class VmCameraMicManagerTest : public testing::Test {
}
};
using VmInfo = VmCameraMicManager::VmInfo;
VmCameraMicManagerTest() {
// Make the profile the primary one.
auto mock_user_manager =
......@@ -140,25 +138,22 @@ class VmCameraMicManagerTest : public testing::Test {
}
void SetCameraAccessing(VmType vm, bool value) {
vm_camera_mic_manager_->UpdateVmInfoAndNotifications(
vm, &VmInfo::SetCameraAccessing, value);
vm_camera_mic_manager_->SetCameraAccessing(vm, value);
}
void SetCameraPrivacyIsOn(VmType vm, bool value) {
vm_camera_mic_manager_->UpdateVmInfoAndNotifications(
vm, &VmInfo::SetCameraPrivacyIsOn, value);
void SetCameraPrivacyIsOn(bool value) {
vm_camera_mic_manager_->SetCameraPrivacyIsOn(value);
}
void SetMicActive(VmType vm, bool value) {
vm_camera_mic_manager_->UpdateVmInfoAndNotifications(
vm, &VmInfo::SetMicActive, value);
vm_camera_mic_manager_->SetMicActive(vm, value);
}
// Note that camera privacy is always set to off by this function.
void SetActive(const ActiveMap& active_map) {
SetCameraPrivacyIsOn(false);
for (const auto& vm_and_device_active_map : active_map) {
VmType vm = vm_and_device_active_map.first;
SetCameraPrivacyIsOn(vm, false);
for (const auto& device_active : vm_and_device_active_map.second) {
switch (device_active.first) {
case kCamera:
......@@ -186,25 +181,25 @@ class VmCameraMicManagerTest : public testing::Test {
TEST_F(VmCameraMicManagerTest, CameraPrivacy) {
SetCameraAccessing(kPluginVm, false);
SetCameraPrivacyIsOn(kPluginVm, false);
SetCameraPrivacyIsOn(false);
EXPECT_FALSE(vm_camera_mic_manager_->IsDeviceActive(kCamera));
EXPECT_FALSE(
vm_camera_mic_manager_->IsNotificationActive(kCameraNotification));
SetCameraAccessing(kPluginVm, true);
SetCameraPrivacyIsOn(kPluginVm, false);
SetCameraPrivacyIsOn(false);
EXPECT_TRUE(vm_camera_mic_manager_->IsDeviceActive(kCamera));
EXPECT_TRUE(
vm_camera_mic_manager_->IsNotificationActive(kCameraNotification));
SetCameraAccessing(kPluginVm, false);
SetCameraPrivacyIsOn(kPluginVm, true);
SetCameraPrivacyIsOn(true);
EXPECT_FALSE(vm_camera_mic_manager_->IsDeviceActive(kCamera));
EXPECT_FALSE(
vm_camera_mic_manager_->IsNotificationActive(kCameraNotification));
SetCameraAccessing(kPluginVm, true);
SetCameraPrivacyIsOn(kPluginVm, true);
SetCameraPrivacyIsOn(true);
EXPECT_FALSE(vm_camera_mic_manager_->IsDeviceActive(kCamera));
EXPECT_FALSE(
vm_camera_mic_manager_->IsNotificationActive(kCameraNotification));
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment